(highlight_trailing_whitespace): On character terminals,
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
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 the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
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 macintosh
198 #include "macterm.h"
199 #endif
200
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Names of text properties relevant for redisplay. */
258
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
261
262 /* Symbols used in text property values. */
263
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
268
269 /* Non-nil means highlight trailing whitespace. */
270
271 Lisp_Object Vshow_trailing_whitespace;
272
273 /* Name of the face used to highlight trailing whitespace. */
274
275 Lisp_Object Qtrailing_whitespace;
276
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
279
280 Lisp_Object Qimage;
281
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
284
285 int noninteractive_need_newline;
286
287 /* Non-zero means print newline to message log before next message. */
288
289 static int message_log_need_newline;
290
291 \f
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
296
297 static struct text_pos this_line_start_pos;
298
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
301
302 static struct text_pos this_line_end_pos;
303
304 /* The vertical positions and the height of this line. */
305
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
309
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
312
313 static int this_line_start_x;
314
315 /* Buffer that this_line_.* variables are referring to. */
316
317 static struct buffer *this_line_buffer;
318
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
321
322 int truncate_partial_width_windows;
323
324 /* A flag to control how to display unibyte 8-bit character. */
325
326 int unibyte_display_via_language_environment;
327
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
331
332 int multiple_frames;
333
334 Lisp_Object Vglobal_mode_string;
335
336 /* Marker for where to display an arrow on top of the buffer text. */
337
338 Lisp_Object Voverlay_arrow_position;
339
340 /* String to display for the arrow. Only used on terminal frames. */
341
342 Lisp_Object Voverlay_arrow_string;
343
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
347
348 static Lisp_Object last_arrow_position, last_arrow_string;
349
350 /* Like mode-line-format, but for the title bar on a visible frame. */
351
352 Lisp_Object Vframe_title_format;
353
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
355
356 Lisp_Object Vicon_title_format;
357
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
361
362 static Lisp_Object Vwindow_size_change_functions;
363
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
365
366 /* Nonzero if overlay arrow has been displayed once in this window. */
367
368 static int overlay_arrow_seen;
369
370 /* Nonzero means highlight the region even in nonselected windows. */
371
372 int highlight_nonselected_windows;
373
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
376
377 static int scroll_step;
378
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
381
382 static int scroll_conservatively;
383
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
388
389 int scroll_margin;
390
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
394
395 int buffer_shared;
396
397 /* Vector containing glyphs for an ellipsis `...'. */
398
399 static Lisp_Object default_invis_vector[3];
400
401 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
404
405 This variable is deprecated. */
406
407 int mode_line_inverse_video;
408
409 /* Prompt to display in front of the mini-buffer contents. */
410
411 Lisp_Object minibuf_prompt;
412
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
415
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
418
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
423 Lisp_Object echo_area_window;
424
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430 Lisp_Object Vmessage_stack;
431
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
434
435 int message_enable_multibyte;
436
437 /* True if we should redraw the mode lines on the next redisplay. */
438
439 int update_mode_lines;
440
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
443
444 int windows_or_buffers_changed;
445
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
448
449 int line_number_displayed;
450
451 /* Maximum buffer size for which to display line numbers. */
452
453 Lisp_Object Vline_number_display_limit;
454
455 /* line width to consider when repostioning for line number display */
456
457 static int line_number_display_limit_width;
458
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
461
462 Lisp_Object Vmessage_log_max;
463
464 /* The name of the *Messages* buffer, a string. */
465
466 static Lisp_Object Vmessages_buffer_name;
467
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
470
471 Lisp_Object echo_area_buffer[2];
472
473 /* The buffers referenced from echo_area_buffer. */
474
475 static Lisp_Object echo_buffer[2];
476
477 /* A vector saved used in with_area_buffer to reduce consing. */
478
479 static Lisp_Object Vwith_echo_area_save_vector;
480
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
483
484 static int display_last_displayed_message_p;
485
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
488
489 int message_buf_print;
490
491 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
492
493 Lisp_Object Qinhibit_menubar_update;
494 int inhibit_menubar_update;
495
496 /* Maximum height for resizing mini-windows. Either a float
497 specifying a fraction of the available height, or an integer
498 specifying a number of lines. */
499
500 Lisp_Object Vmax_mini_window_height;
501
502 /* Non-zero means messages should be displayed with truncated
503 lines instead of being continued. */
504
505 int message_truncate_lines;
506 Lisp_Object Qmessage_truncate_lines;
507
508 /* Non-zero means we want a hollow cursor in windows that are not
509 selected. Zero means there's no cursor in such windows. */
510
511 int cursor_in_non_selected_windows;
512
513 /* A scratch glyph row with contents used for generating truncation
514 glyphs. Also used in direct_output_for_insert. */
515
516 #define MAX_SCRATCH_GLYPHS 100
517 struct glyph_row scratch_glyph_row;
518 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
519
520 /* Ascent and height of the last line processed by move_it_to. */
521
522 static int last_max_ascent, last_height;
523
524 /* Non-zero if there's a help-echo in the echo area. */
525
526 int help_echo_showing_p;
527
528 /* If >= 0, computed, exact values of mode-line and header-line height
529 to use in the macros CURRENT_MODE_LINE_HEIGHT and
530 CURRENT_HEADER_LINE_HEIGHT. */
531
532 int current_mode_line_height, current_header_line_height;
533
534 /* The maximum distance to look ahead for text properties. Values
535 that are too small let us call compute_char_face and similar
536 functions too often which is expensive. Values that are too large
537 let us call compute_char_face and alike too often because we
538 might not be interested in text properties that far away. */
539
540 #define TEXT_PROP_DISTANCE_LIMIT 100
541
542 #if GLYPH_DEBUG
543
544 /* Non-zero means print traces of redisplay if compiled with
545 GLYPH_DEBUG != 0. */
546
547 int trace_redisplay_p;
548
549 #endif /* GLYPH_DEBUG */
550
551 #ifdef DEBUG_TRACE_MOVE
552 /* Non-zero means trace with TRACE_MOVE to stderr. */
553 int trace_move;
554
555 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
556 #else
557 #define TRACE_MOVE(x) (void) 0
558 #endif
559
560 /* Non-zero means automatically scroll windows horizontally to make
561 point visible. */
562
563 int automatic_hscrolling_p;
564
565 /* A list of symbols, one for each supported image type. */
566
567 Lisp_Object Vimage_types;
568
569 /* The variable `resize-mini-windows'. If nil, don't resize
570 mini-windows. If t, always resize them to fit the text they
571 display. If `grow-only', let mini-windows grow only until they
572 become empty. */
573
574 Lisp_Object Vresize_mini_windows;
575
576 /* Value returned from text property handlers (see below). */
577
578 enum prop_handled
579 {
580 HANDLED_NORMALLY,
581 HANDLED_RECOMPUTE_PROPS,
582 HANDLED_OVERLAY_STRING_CONSUMED,
583 HANDLED_RETURN
584 };
585
586 /* A description of text properties that redisplay is interested
587 in. */
588
589 struct props
590 {
591 /* The name of the property. */
592 Lisp_Object *name;
593
594 /* A unique index for the property. */
595 enum prop_idx idx;
596
597 /* A handler function called to set up iterator IT from the property
598 at IT's current position. Value is used to steer handle_stop. */
599 enum prop_handled (*handler) P_ ((struct it *it));
600 };
601
602 static enum prop_handled handle_face_prop P_ ((struct it *));
603 static enum prop_handled handle_invisible_prop P_ ((struct it *));
604 static enum prop_handled handle_display_prop P_ ((struct it *));
605 static enum prop_handled handle_composition_prop P_ ((struct it *));
606 static enum prop_handled handle_overlay_change P_ ((struct it *));
607 static enum prop_handled handle_fontified_prop P_ ((struct it *));
608
609 /* Properties handled by iterators. */
610
611 static struct props it_props[] =
612 {
613 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
614 /* Handle `face' before `display' because some sub-properties of
615 `display' need to know the face. */
616 {&Qface, FACE_PROP_IDX, handle_face_prop},
617 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
618 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
619 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
620 {NULL, 0, NULL}
621 };
622
623 /* Value is the position described by X. If X is a marker, value is
624 the marker_position of X. Otherwise, value is X. */
625
626 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
627
628 /* Enumeration returned by some move_it_.* functions internally. */
629
630 enum move_it_result
631 {
632 /* Not used. Undefined value. */
633 MOVE_UNDEFINED,
634
635 /* Move ended at the requested buffer position or ZV. */
636 MOVE_POS_MATCH_OR_ZV,
637
638 /* Move ended at the requested X pixel position. */
639 MOVE_X_REACHED,
640
641 /* Move within a line ended at the end of a line that must be
642 continued. */
643 MOVE_LINE_CONTINUED,
644
645 /* Move within a line ended at the end of a line that would
646 be displayed truncated. */
647 MOVE_LINE_TRUNCATED,
648
649 /* Move within a line ended at a line end. */
650 MOVE_NEWLINE_OR_CR
651 };
652
653
654 \f
655 /* Function prototypes. */
656
657 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
658 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
659 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
660 static int redisplay_mode_lines P_ ((Lisp_Object, int));
661 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
662 static int invisible_text_between_p P_ ((struct it *, int, int));
663 static int next_element_from_ellipsis P_ ((struct it *));
664 static void pint2str P_ ((char *, int, int));
665 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
666 struct text_pos));
667 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
668 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
669 static void store_frame_title_char P_ ((char));
670 static int store_frame_title P_ ((unsigned char *, int, int));
671 static void x_consider_frame_title P_ ((Lisp_Object));
672 static void handle_stop P_ ((struct it *));
673 static int tool_bar_lines_needed P_ ((struct frame *));
674 static int single_display_prop_intangible_p P_ ((Lisp_Object));
675 static void ensure_echo_area_buffers P_ ((void));
676 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
677 struct glyph_row *,
678 struct glyph_row *));
679 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
680 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
681 static int with_echo_area_buffer P_ ((struct window *, int,
682 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
683 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
684 static void clear_garbaged_frames P_ ((void));
685 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
686 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
687 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
688 static int display_echo_area P_ ((struct window *));
689 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
690 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
692 static int string_char_and_length P_ ((unsigned char *, int, int *));
693 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
694 struct text_pos));
695 static int compute_window_start_on_continuation_line P_ ((struct window *));
696 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
697 static void insert_left_trunc_glyphs P_ ((struct it *));
698 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
699 static void extend_face_to_end_of_line P_ ((struct it *));
700 static int append_space P_ ((struct it *, int));
701 static void make_cursor_line_fully_visible P_ ((struct window *));
702 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
703 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
704 static int trailing_whitespace_p P_ ((int));
705 static int message_log_check_duplicate P_ ((int, int, int, int));
706 int invisible_p P_ ((Lisp_Object, Lisp_Object));
707 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
708 static void push_it P_ ((struct it *));
709 static void pop_it P_ ((struct it *));
710 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
711 static void redisplay_internal P_ ((int));
712 static int echo_area_display P_ ((int));
713 static void redisplay_windows P_ ((Lisp_Object));
714 static void redisplay_window P_ ((Lisp_Object, int));
715 static void update_menu_bar P_ ((struct frame *, int));
716 static int try_window_reusing_current_matrix P_ ((struct window *));
717 static int try_window_id P_ ((struct window *));
718 static int display_line P_ ((struct it *));
719 static int display_mode_lines P_ ((struct window *));
720 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
721 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
722 static char *decode_mode_spec P_ ((struct window *, int, int, int));
723 static void display_menu_bar P_ ((struct window *));
724 static int display_count_lines P_ ((int, int, int, int, int *));
725 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
726 int, int, struct it *, int, int, int, int));
727 static void compute_line_metrics P_ ((struct it *));
728 static void run_redisplay_end_trigger_hook P_ ((struct it *));
729 static int get_overlay_strings P_ ((struct it *));
730 static void next_overlay_string P_ ((struct it *));
731 static void reseat P_ ((struct it *, struct text_pos, int));
732 static void reseat_1 P_ ((struct it *, struct text_pos, int));
733 static void back_to_previous_visible_line_start P_ ((struct it *));
734 static void reseat_at_previous_visible_line_start P_ ((struct it *));
735 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
736 static int next_element_from_display_vector P_ ((struct it *));
737 static int next_element_from_string P_ ((struct it *));
738 static int next_element_from_c_string P_ ((struct it *));
739 static int next_element_from_buffer P_ ((struct it *));
740 static int next_element_from_composition P_ ((struct it *));
741 static int next_element_from_image P_ ((struct it *));
742 static int next_element_from_stretch P_ ((struct it *));
743 static void load_overlay_strings P_ ((struct it *));
744 static void init_from_display_pos P_ ((struct it *, struct window *,
745 struct display_pos *));
746 static void reseat_to_string P_ ((struct it *, unsigned char *,
747 Lisp_Object, int, int, int, int));
748 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
749 int, int, int));
750 void move_it_vertically_backward P_ ((struct it *, int));
751 static void init_to_row_start P_ ((struct it *, struct window *,
752 struct glyph_row *));
753 static void init_to_row_end P_ ((struct it *, struct window *,
754 struct glyph_row *));
755 static void back_to_previous_line_start P_ ((struct it *));
756 static int forward_to_next_line_start P_ ((struct it *, int *));
757 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
758 Lisp_Object, int));
759 static struct text_pos string_pos P_ ((int, Lisp_Object));
760 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
761 static int number_of_chars P_ ((unsigned char *, int));
762 static void compute_stop_pos P_ ((struct it *));
763 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
764 Lisp_Object));
765 static int face_before_or_after_it_pos P_ ((struct it *, int));
766 static int next_overlay_change P_ ((int));
767 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
768 Lisp_Object, struct text_pos *,
769 int));
770 static int underlying_face_id P_ ((struct it *));
771
772 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
773 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
774
775 #ifdef HAVE_WINDOW_SYSTEM
776
777 static void update_tool_bar P_ ((struct frame *, int));
778 static void build_desired_tool_bar_string P_ ((struct frame *f));
779 static int redisplay_tool_bar P_ ((struct frame *));
780 static void display_tool_bar_line P_ ((struct it *));
781
782 #endif /* HAVE_WINDOW_SYSTEM */
783
784 \f
785 /***********************************************************************
786 Window display dimensions
787 ***********************************************************************/
788
789 /* Return the window-relative maximum y + 1 for glyph rows displaying
790 text in window W. This is the height of W minus the height of a
791 mode line, if any. */
792
793 INLINE int
794 window_text_bottom_y (w)
795 struct window *w;
796 {
797 struct frame *f = XFRAME (w->frame);
798 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
799
800 if (WINDOW_WANTS_MODELINE_P (w))
801 height -= CURRENT_MODE_LINE_HEIGHT (w);
802 return height;
803 }
804
805
806 /* Return the pixel width of display area AREA of window W. AREA < 0
807 means return the total width of W, not including bitmap areas to
808 the left and right of the window. */
809
810 INLINE int
811 window_box_width (w, area)
812 struct window *w;
813 int area;
814 {
815 struct frame *f = XFRAME (w->frame);
816 int width = XFASTINT (w->width);
817
818 if (!w->pseudo_window_p)
819 {
820 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
821
822 if (area == TEXT_AREA)
823 {
824 if (INTEGERP (w->left_margin_width))
825 width -= XFASTINT (w->left_margin_width);
826 if (INTEGERP (w->right_margin_width))
827 width -= XFASTINT (w->right_margin_width);
828 }
829 else if (area == LEFT_MARGIN_AREA)
830 width = (INTEGERP (w->left_margin_width)
831 ? XFASTINT (w->left_margin_width) : 0);
832 else if (area == RIGHT_MARGIN_AREA)
833 width = (INTEGERP (w->right_margin_width)
834 ? XFASTINT (w->right_margin_width) : 0);
835 }
836
837 return width * CANON_X_UNIT (f);
838 }
839
840
841 /* Return the pixel height of the display area of window W, not
842 including mode lines of W, if any.. */
843
844 INLINE int
845 window_box_height (w)
846 struct window *w;
847 {
848 struct frame *f = XFRAME (w->frame);
849 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
850
851 xassert (height >= 0);
852
853 /* Note: the code below that determines the mode-line/header-line
854 height is essentially the same as that contained in the macro
855 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
856 the appropriate glyph row has its `mode_line_p' flag set,
857 and if it doesn't, uses estimate_mode_line_height instead. */
858
859 if (WINDOW_WANTS_MODELINE_P (w))
860 {
861 struct glyph_row *ml_row
862 = (w->current_matrix && w->current_matrix->rows
863 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
864 : 0);
865 if (ml_row && ml_row->mode_line_p)
866 height -= ml_row->height;
867 else
868 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
869 }
870
871 if (WINDOW_WANTS_HEADER_LINE_P (w))
872 {
873 struct glyph_row *hl_row
874 = (w->current_matrix && w->current_matrix->rows
875 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
876 : 0);
877 if (hl_row && hl_row->mode_line_p)
878 height -= hl_row->height;
879 else
880 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
881 }
882
883 return height;
884 }
885
886
887 /* Return the frame-relative coordinate of the left edge of display
888 area AREA of window W. AREA < 0 means return the left edge of the
889 whole window, to the right of any bitmap area at the left side of
890 W. */
891
892 INLINE int
893 window_box_left (w, area)
894 struct window *w;
895 int area;
896 {
897 struct frame *f = XFRAME (w->frame);
898 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
899
900 if (!w->pseudo_window_p)
901 {
902 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
903 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
904
905 if (area == TEXT_AREA)
906 x += window_box_width (w, LEFT_MARGIN_AREA);
907 else if (area == RIGHT_MARGIN_AREA)
908 x += (window_box_width (w, LEFT_MARGIN_AREA)
909 + window_box_width (w, TEXT_AREA));
910 }
911
912 return x;
913 }
914
915
916 /* Return the frame-relative coordinate of the right edge of display
917 area AREA of window W. AREA < 0 means return the left edge of the
918 whole window, to the left of any bitmap area at the right side of
919 W. */
920
921 INLINE int
922 window_box_right (w, area)
923 struct window *w;
924 int area;
925 {
926 return window_box_left (w, area) + window_box_width (w, area);
927 }
928
929
930 /* Get the bounding box of the display area AREA of window W, without
931 mode lines, in frame-relative coordinates. AREA < 0 means the
932 whole window, not including bitmap areas to the left and right of
933 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
934 coordinates of the upper-left corner of the box. Return in
935 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
936
937 INLINE void
938 window_box (w, area, box_x, box_y, box_width, box_height)
939 struct window *w;
940 int area;
941 int *box_x, *box_y, *box_width, *box_height;
942 {
943 struct frame *f = XFRAME (w->frame);
944
945 *box_width = window_box_width (w, area);
946 *box_height = window_box_height (w);
947 *box_x = window_box_left (w, area);
948 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
949 + XFASTINT (w->top) * CANON_Y_UNIT (f));
950 if (WINDOW_WANTS_HEADER_LINE_P (w))
951 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
952 }
953
954
955 /* Get the bounding box of the display area AREA of window W, without
956 mode lines. AREA < 0 means the whole window, not including bitmap
957 areas to the left and right of the window. Return in *TOP_LEFT_X
958 and TOP_LEFT_Y the frame-relative pixel coordinates of the
959 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
960 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
961 box. */
962
963 INLINE void
964 window_box_edges (w, area, top_left_x, top_left_y,
965 bottom_right_x, bottom_right_y)
966 struct window *w;
967 int area;
968 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
969 {
970 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
971 bottom_right_y);
972 *bottom_right_x += *top_left_x;
973 *bottom_right_y += *top_left_y;
974 }
975
976
977 \f
978 /***********************************************************************
979 Utilities
980 ***********************************************************************/
981
982 /* Return the bottom y-position of the line the iterator IT is in.
983 This can modify IT's settings. */
984
985 int
986 line_bottom_y (it)
987 struct it *it;
988 {
989 int line_height = it->max_ascent + it->max_descent;
990 int line_top_y = it->current_y;
991
992 if (line_height == 0)
993 {
994 if (last_height)
995 line_height = last_height;
996 else if (IT_CHARPOS (*it) < ZV)
997 {
998 move_it_by_lines (it, 1, 1);
999 line_height = (it->max_ascent || it->max_descent
1000 ? it->max_ascent + it->max_descent
1001 : last_height);
1002 }
1003 else
1004 {
1005 struct glyph_row *row = it->glyph_row;
1006
1007 /* Use the default character height. */
1008 it->glyph_row = NULL;
1009 it->what = IT_CHARACTER;
1010 it->c = ' ';
1011 it->len = 1;
1012 PRODUCE_GLYPHS (it);
1013 line_height = it->ascent + it->descent;
1014 it->glyph_row = row;
1015 }
1016 }
1017
1018 return line_top_y + line_height;
1019 }
1020
1021
1022 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1023 1 if POS is visible and the line containing POS is fully visible.
1024 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1025 and header-lines heights. */
1026
1027 int
1028 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1029 struct window *w;
1030 int charpos, *fully, exact_mode_line_heights_p;
1031 {
1032 struct it it;
1033 struct text_pos top;
1034 int visible_p;
1035 struct buffer *old_buffer = NULL;
1036
1037 if (XBUFFER (w->buffer) != current_buffer)
1038 {
1039 old_buffer = current_buffer;
1040 set_buffer_internal_1 (XBUFFER (w->buffer));
1041 }
1042
1043 *fully = visible_p = 0;
1044 SET_TEXT_POS_FROM_MARKER (top, w->start);
1045
1046 /* Compute exact mode line heights, if requested. */
1047 if (exact_mode_line_heights_p)
1048 {
1049 if (WINDOW_WANTS_MODELINE_P (w))
1050 current_mode_line_height
1051 = display_mode_line (w, MODE_LINE_FACE_ID,
1052 current_buffer->mode_line_format);
1053
1054 if (WINDOW_WANTS_HEADER_LINE_P (w))
1055 current_header_line_height
1056 = display_mode_line (w, HEADER_LINE_FACE_ID,
1057 current_buffer->header_line_format);
1058 }
1059
1060 start_display (&it, w, top);
1061 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1062 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1063
1064 /* Note that we may overshoot because of invisible text. */
1065 if (IT_CHARPOS (it) >= charpos)
1066 {
1067 int top_y = it.current_y;
1068 int bottom_y = line_bottom_y (&it);
1069 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1070
1071 if (top_y < window_top_y)
1072 visible_p = bottom_y > window_top_y;
1073 else if (top_y < it.last_visible_y)
1074 {
1075 visible_p = 1;
1076 *fully = bottom_y <= it.last_visible_y;
1077 }
1078 }
1079 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1080 {
1081 move_it_by_lines (&it, 1, 0);
1082 if (charpos < IT_CHARPOS (it))
1083 {
1084 visible_p = 1;
1085 *fully = 0;
1086 }
1087 }
1088
1089 if (old_buffer)
1090 set_buffer_internal_1 (old_buffer);
1091
1092 current_header_line_height = current_mode_line_height = -1;
1093 return visible_p;
1094 }
1095
1096
1097 /* Return the next character from STR which is MAXLEN bytes long.
1098 Return in *LEN the length of the character. This is like
1099 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1100 we find one, we return a `?', but with the length of the invalid
1101 character. */
1102
1103 static INLINE int
1104 string_char_and_length (str, maxlen, len)
1105 unsigned char *str;
1106 int maxlen, *len;
1107 {
1108 int c;
1109
1110 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1111 if (!CHAR_VALID_P (c, 1))
1112 /* We may not change the length here because other places in Emacs
1113 don't use this function, i.e. they silently accept invalid
1114 characters. */
1115 c = '?';
1116
1117 return c;
1118 }
1119
1120
1121
1122 /* Given a position POS containing a valid character and byte position
1123 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1124
1125 static struct text_pos
1126 string_pos_nchars_ahead (pos, string, nchars)
1127 struct text_pos pos;
1128 Lisp_Object string;
1129 int nchars;
1130 {
1131 xassert (STRINGP (string) && nchars >= 0);
1132
1133 if (STRING_MULTIBYTE (string))
1134 {
1135 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1136 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1137 int len;
1138
1139 while (nchars--)
1140 {
1141 string_char_and_length (p, rest, &len);
1142 p += len, rest -= len;
1143 xassert (rest >= 0);
1144 CHARPOS (pos) += 1;
1145 BYTEPOS (pos) += len;
1146 }
1147 }
1148 else
1149 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1150
1151 return pos;
1152 }
1153
1154
1155 /* Value is the text position, i.e. character and byte position,
1156 for character position CHARPOS in STRING. */
1157
1158 static INLINE struct text_pos
1159 string_pos (charpos, string)
1160 int charpos;
1161 Lisp_Object string;
1162 {
1163 struct text_pos pos;
1164 xassert (STRINGP (string));
1165 xassert (charpos >= 0);
1166 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1167 return pos;
1168 }
1169
1170
1171 /* Value is a text position, i.e. character and byte position, for
1172 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1173 means recognize multibyte characters. */
1174
1175 static struct text_pos
1176 c_string_pos (charpos, s, multibyte_p)
1177 int charpos;
1178 unsigned char *s;
1179 int multibyte_p;
1180 {
1181 struct text_pos pos;
1182
1183 xassert (s != NULL);
1184 xassert (charpos >= 0);
1185
1186 if (multibyte_p)
1187 {
1188 int rest = strlen (s), len;
1189
1190 SET_TEXT_POS (pos, 0, 0);
1191 while (charpos--)
1192 {
1193 string_char_and_length (s, rest, &len);
1194 s += len, rest -= len;
1195 xassert (rest >= 0);
1196 CHARPOS (pos) += 1;
1197 BYTEPOS (pos) += len;
1198 }
1199 }
1200 else
1201 SET_TEXT_POS (pos, charpos, charpos);
1202
1203 return pos;
1204 }
1205
1206
1207 /* Value is the number of characters in C string S. MULTIBYTE_P
1208 non-zero means recognize multibyte characters. */
1209
1210 static int
1211 number_of_chars (s, multibyte_p)
1212 unsigned char *s;
1213 int multibyte_p;
1214 {
1215 int nchars;
1216
1217 if (multibyte_p)
1218 {
1219 int rest = strlen (s), len;
1220 unsigned char *p = (unsigned char *) s;
1221
1222 for (nchars = 0; rest > 0; ++nchars)
1223 {
1224 string_char_and_length (p, rest, &len);
1225 rest -= len, p += len;
1226 }
1227 }
1228 else
1229 nchars = strlen (s);
1230
1231 return nchars;
1232 }
1233
1234
1235 /* Compute byte position NEWPOS->bytepos corresponding to
1236 NEWPOS->charpos. POS is a known position in string STRING.
1237 NEWPOS->charpos must be >= POS.charpos. */
1238
1239 static void
1240 compute_string_pos (newpos, pos, string)
1241 struct text_pos *newpos, pos;
1242 Lisp_Object string;
1243 {
1244 xassert (STRINGP (string));
1245 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1246
1247 if (STRING_MULTIBYTE (string))
1248 *newpos = string_pos_nchars_ahead (pos, string,
1249 CHARPOS (*newpos) - CHARPOS (pos));
1250 else
1251 BYTEPOS (*newpos) = CHARPOS (*newpos);
1252 }
1253
1254
1255 \f
1256 /***********************************************************************
1257 Lisp form evaluation
1258 ***********************************************************************/
1259
1260 /* Error handler for safe_eval and safe_call. */
1261
1262 static Lisp_Object
1263 safe_eval_handler (arg)
1264 Lisp_Object arg;
1265 {
1266 add_to_log ("Error during redisplay: %s", arg, Qnil);
1267 return Qnil;
1268 }
1269
1270
1271 /* Evaluate SEXPR and return the result, or nil if something went
1272 wrong. */
1273
1274 Lisp_Object
1275 safe_eval (sexpr)
1276 Lisp_Object sexpr;
1277 {
1278 int count = BINDING_STACK_SIZE ();
1279 struct gcpro gcpro1;
1280 Lisp_Object val;
1281
1282 GCPRO1 (sexpr);
1283 specbind (Qinhibit_redisplay, Qt);
1284 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1285 UNGCPRO;
1286 return unbind_to (count, val);
1287 }
1288
1289
1290 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1291 Return the result, or nil if something went wrong. */
1292
1293 Lisp_Object
1294 safe_call (nargs, args)
1295 int nargs;
1296 Lisp_Object *args;
1297 {
1298 int count = BINDING_STACK_SIZE ();
1299 Lisp_Object val;
1300 struct gcpro gcpro1;
1301
1302 GCPRO1 (args[0]);
1303 gcpro1.nvars = nargs;
1304 specbind (Qinhibit_redisplay, Qt);
1305 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1306 safe_eval_handler);
1307 UNGCPRO;
1308 return unbind_to (count, val);
1309 }
1310
1311
1312 /* Call function FN with one argument ARG.
1313 Return the result, or nil if something went wrong. */
1314
1315 Lisp_Object
1316 safe_call1 (fn, arg)
1317 Lisp_Object fn, arg;
1318 {
1319 Lisp_Object args[2];
1320 args[0] = fn;
1321 args[1] = arg;
1322 return safe_call (2, args);
1323 }
1324
1325
1326 \f
1327 /***********************************************************************
1328 Debugging
1329 ***********************************************************************/
1330
1331 #if 0
1332
1333 /* Define CHECK_IT to perform sanity checks on iterators.
1334 This is for debugging. It is too slow to do unconditionally. */
1335
1336 static void
1337 check_it (it)
1338 struct it *it;
1339 {
1340 if (it->method == next_element_from_string)
1341 {
1342 xassert (STRINGP (it->string));
1343 xassert (IT_STRING_CHARPOS (*it) >= 0);
1344 }
1345 else if (it->method == next_element_from_buffer)
1346 {
1347 /* Check that character and byte positions agree. */
1348 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1349 }
1350
1351 if (it->dpvec)
1352 xassert (it->current.dpvec_index >= 0);
1353 else
1354 xassert (it->current.dpvec_index < 0);
1355 }
1356
1357 #define CHECK_IT(IT) check_it ((IT))
1358
1359 #else /* not 0 */
1360
1361 #define CHECK_IT(IT) (void) 0
1362
1363 #endif /* not 0 */
1364
1365
1366 #if GLYPH_DEBUG
1367
1368 /* Check that the window end of window W is what we expect it
1369 to be---the last row in the current matrix displaying text. */
1370
1371 static void
1372 check_window_end (w)
1373 struct window *w;
1374 {
1375 if (!MINI_WINDOW_P (w)
1376 && !NILP (w->window_end_valid))
1377 {
1378 struct glyph_row *row;
1379 xassert ((row = MATRIX_ROW (w->current_matrix,
1380 XFASTINT (w->window_end_vpos)),
1381 !row->enabled_p
1382 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1383 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1384 }
1385 }
1386
1387 #define CHECK_WINDOW_END(W) check_window_end ((W))
1388
1389 #else /* not GLYPH_DEBUG */
1390
1391 #define CHECK_WINDOW_END(W) (void) 0
1392
1393 #endif /* not GLYPH_DEBUG */
1394
1395
1396 \f
1397 /***********************************************************************
1398 Iterator initialization
1399 ***********************************************************************/
1400
1401 /* Initialize IT for displaying current_buffer in window W, starting
1402 at character position CHARPOS. CHARPOS < 0 means that no buffer
1403 position is specified which is useful when the iterator is assigned
1404 a position later. BYTEPOS is the byte position corresponding to
1405 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1406
1407 If ROW is not null, calls to produce_glyphs with IT as parameter
1408 will produce glyphs in that row.
1409
1410 BASE_FACE_ID is the id of a base face to use. It must be one of
1411 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1412 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1413 displaying the tool-bar.
1414
1415 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1416 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1417 corresponding mode line glyph row of the desired matrix of W. */
1418
1419 void
1420 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1421 struct it *it;
1422 struct window *w;
1423 int charpos, bytepos;
1424 struct glyph_row *row;
1425 enum face_id base_face_id;
1426 {
1427 int highlight_region_p;
1428
1429 /* Some precondition checks. */
1430 xassert (w != NULL && it != NULL);
1431 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1432
1433 /* If face attributes have been changed since the last redisplay,
1434 free realized faces now because they depend on face definitions
1435 that might have changed. */
1436 if (face_change_count)
1437 {
1438 face_change_count = 0;
1439 free_all_realized_faces (Qnil);
1440 }
1441
1442 /* Use one of the mode line rows of W's desired matrix if
1443 appropriate. */
1444 if (row == NULL)
1445 {
1446 if (base_face_id == MODE_LINE_FACE_ID)
1447 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1448 else if (base_face_id == HEADER_LINE_FACE_ID)
1449 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1450 }
1451
1452 /* Clear IT. */
1453 bzero (it, sizeof *it);
1454 it->current.overlay_string_index = -1;
1455 it->current.dpvec_index = -1;
1456 it->base_face_id = base_face_id;
1457
1458 /* The window in which we iterate over current_buffer: */
1459 XSETWINDOW (it->window, w);
1460 it->w = w;
1461 it->f = XFRAME (w->frame);
1462
1463 /* Extra space between lines (on window systems only). */
1464 if (base_face_id == DEFAULT_FACE_ID
1465 && FRAME_WINDOW_P (it->f))
1466 {
1467 if (NATNUMP (current_buffer->extra_line_spacing))
1468 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1469 else if (it->f->extra_line_spacing > 0)
1470 it->extra_line_spacing = it->f->extra_line_spacing;
1471 }
1472
1473 /* If realized faces have been removed, e.g. because of face
1474 attribute changes of named faces, recompute them. When running
1475 in batch mode, the face cache of Vterminal_frame is null. If
1476 we happen to get called, make a dummy face cache. */
1477 if (
1478 #ifndef WINDOWSNT
1479 noninteractive &&
1480 #endif
1481 FRAME_FACE_CACHE (it->f) == NULL)
1482 init_frame_faces (it->f);
1483 if (FRAME_FACE_CACHE (it->f)->used == 0)
1484 recompute_basic_faces (it->f);
1485
1486 /* Current value of the `space-width', and 'height' properties. */
1487 it->space_width = Qnil;
1488 it->font_height = Qnil;
1489
1490 /* Are control characters displayed as `^C'? */
1491 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1492
1493 /* -1 means everything between a CR and the following line end
1494 is invisible. >0 means lines indented more than this value are
1495 invisible. */
1496 it->selective = (INTEGERP (current_buffer->selective_display)
1497 ? XFASTINT (current_buffer->selective_display)
1498 : (!NILP (current_buffer->selective_display)
1499 ? -1 : 0));
1500 it->selective_display_ellipsis_p
1501 = !NILP (current_buffer->selective_display_ellipses);
1502
1503 /* Display table to use. */
1504 it->dp = window_display_table (w);
1505
1506 /* Are multibyte characters enabled in current_buffer? */
1507 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1508
1509 /* Non-zero if we should highlight the region. */
1510 highlight_region_p
1511 = (!NILP (Vtransient_mark_mode)
1512 && !NILP (current_buffer->mark_active)
1513 && XMARKER (current_buffer->mark)->buffer != 0);
1514
1515 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1516 start and end of a visible region in window IT->w. Set both to
1517 -1 to indicate no region. */
1518 if (highlight_region_p
1519 /* Maybe highlight only in selected window. */
1520 && (/* Either show region everywhere. */
1521 highlight_nonselected_windows
1522 /* Or show region in the selected window. */
1523 || w == XWINDOW (selected_window)
1524 /* Or show the region if we are in the mini-buffer and W is
1525 the window the mini-buffer refers to. */
1526 || (MINI_WINDOW_P (XWINDOW (selected_window))
1527 && w == XWINDOW (Vminibuf_scroll_window))))
1528 {
1529 int charpos = marker_position (current_buffer->mark);
1530 it->region_beg_charpos = min (PT, charpos);
1531 it->region_end_charpos = max (PT, charpos);
1532 }
1533 else
1534 it->region_beg_charpos = it->region_end_charpos = -1;
1535
1536 /* Get the position at which the redisplay_end_trigger hook should
1537 be run, if it is to be run at all. */
1538 if (MARKERP (w->redisplay_end_trigger)
1539 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1540 it->redisplay_end_trigger_charpos
1541 = marker_position (w->redisplay_end_trigger);
1542 else if (INTEGERP (w->redisplay_end_trigger))
1543 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1544
1545 /* Correct bogus values of tab_width. */
1546 it->tab_width = XINT (current_buffer->tab_width);
1547 if (it->tab_width <= 0 || it->tab_width > 1000)
1548 it->tab_width = 8;
1549
1550 /* Are lines in the display truncated? */
1551 it->truncate_lines_p
1552 = (base_face_id != DEFAULT_FACE_ID
1553 || XINT (it->w->hscroll)
1554 || (truncate_partial_width_windows
1555 && !WINDOW_FULL_WIDTH_P (it->w))
1556 || !NILP (current_buffer->truncate_lines));
1557
1558 /* Get dimensions of truncation and continuation glyphs. These are
1559 displayed as bitmaps under X, so we don't need them for such
1560 frames. */
1561 if (!FRAME_WINDOW_P (it->f))
1562 {
1563 if (it->truncate_lines_p)
1564 {
1565 /* We will need the truncation glyph. */
1566 xassert (it->glyph_row == NULL);
1567 produce_special_glyphs (it, IT_TRUNCATION);
1568 it->truncation_pixel_width = it->pixel_width;
1569 }
1570 else
1571 {
1572 /* We will need the continuation glyph. */
1573 xassert (it->glyph_row == NULL);
1574 produce_special_glyphs (it, IT_CONTINUATION);
1575 it->continuation_pixel_width = it->pixel_width;
1576 }
1577
1578 /* Reset these values to zero becaue the produce_special_glyphs
1579 above has changed them. */
1580 it->pixel_width = it->ascent = it->descent = 0;
1581 it->phys_ascent = it->phys_descent = 0;
1582 }
1583
1584 /* Set this after getting the dimensions of truncation and
1585 continuation glyphs, so that we don't produce glyphs when calling
1586 produce_special_glyphs, above. */
1587 it->glyph_row = row;
1588 it->area = TEXT_AREA;
1589
1590 /* Get the dimensions of the display area. The display area
1591 consists of the visible window area plus a horizontally scrolled
1592 part to the left of the window. All x-values are relative to the
1593 start of this total display area. */
1594 if (base_face_id != DEFAULT_FACE_ID)
1595 {
1596 /* Mode lines, menu bar in terminal frames. */
1597 it->first_visible_x = 0;
1598 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1599 }
1600 else
1601 {
1602 it->first_visible_x
1603 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1604 it->last_visible_x = (it->first_visible_x
1605 + window_box_width (w, TEXT_AREA));
1606
1607 /* If we truncate lines, leave room for the truncator glyph(s) at
1608 the right margin. Otherwise, leave room for the continuation
1609 glyph(s). Truncation and continuation glyphs are not inserted
1610 for window-based redisplay. */
1611 if (!FRAME_WINDOW_P (it->f))
1612 {
1613 if (it->truncate_lines_p)
1614 it->last_visible_x -= it->truncation_pixel_width;
1615 else
1616 it->last_visible_x -= it->continuation_pixel_width;
1617 }
1618
1619 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1620 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1621 }
1622
1623 /* Leave room for a border glyph. */
1624 if (!FRAME_WINDOW_P (it->f)
1625 && !WINDOW_RIGHTMOST_P (it->w))
1626 it->last_visible_x -= 1;
1627
1628 it->last_visible_y = window_text_bottom_y (w);
1629
1630 /* For mode lines and alike, arrange for the first glyph having a
1631 left box line if the face specifies a box. */
1632 if (base_face_id != DEFAULT_FACE_ID)
1633 {
1634 struct face *face;
1635
1636 it->face_id = base_face_id;
1637
1638 /* If we have a boxed mode line, make the first character appear
1639 with a left box line. */
1640 face = FACE_FROM_ID (it->f, base_face_id);
1641 if (face->box != FACE_NO_BOX)
1642 it->start_of_box_run_p = 1;
1643 }
1644
1645 /* If a buffer position was specified, set the iterator there,
1646 getting overlays and face properties from that position. */
1647 if (charpos > 0)
1648 {
1649 it->end_charpos = ZV;
1650 it->face_id = -1;
1651 IT_CHARPOS (*it) = charpos;
1652
1653 /* Compute byte position if not specified. */
1654 if (bytepos <= 0)
1655 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1656 else
1657 IT_BYTEPOS (*it) = bytepos;
1658
1659 /* Compute faces etc. */
1660 reseat (it, it->current.pos, 1);
1661 }
1662
1663 CHECK_IT (it);
1664 }
1665
1666
1667 /* Initialize IT for the display of window W with window start POS. */
1668
1669 void
1670 start_display (it, w, pos)
1671 struct it *it;
1672 struct window *w;
1673 struct text_pos pos;
1674 {
1675 int start_at_line_beg_p;
1676 struct glyph_row *row;
1677 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1678 int first_y;
1679
1680 row = w->desired_matrix->rows + first_vpos;
1681 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1682 first_y = it->current_y;
1683
1684 /* If window start is not at a line start, move back to the line
1685 start. This makes sure that we take continuation lines into
1686 account. */
1687 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1688 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1689 if (!start_at_line_beg_p)
1690 reseat_at_previous_visible_line_start (it);
1691
1692 /* If window start is not at a line start, skip forward to POS to
1693 get the correct continuation_lines_width and current_x. */
1694 if (!start_at_line_beg_p)
1695 {
1696 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1697
1698 /* If lines are continued, this line may end in the middle of a
1699 multi-glyph character (e.g. a control character displayed as
1700 \003, or in the middle of an overlay string). In this case
1701 move_it_to above will not have taken us to the start of
1702 the continuation line but to the end of the continued line. */
1703 if (!it->truncate_lines_p)
1704 {
1705 if (it->current_x > 0)
1706 {
1707 if (it->current.dpvec_index >= 0
1708 || it->current.overlay_string_index >= 0)
1709 {
1710 set_iterator_to_next (it, 1);
1711 move_it_in_display_line_to (it, -1, -1, 0);
1712 }
1713
1714 it->continuation_lines_width += it->current_x;
1715 }
1716
1717 /* We're starting a new display line, not affected by the
1718 height of the continued line, so clear the appropriate
1719 fields in the iterator structure. */
1720 it->max_ascent = it->max_descent = 0;
1721 it->max_phys_ascent = it->max_phys_descent = 0;
1722 }
1723
1724 it->current_y = first_y;
1725 it->vpos = 0;
1726 it->current_x = it->hpos = 0;
1727 }
1728
1729 #if 0 /* Don't assert the following because start_display is sometimes
1730 called intentionally with a window start that is not at a
1731 line start. Please leave this code in as a comment. */
1732
1733 /* Window start should be on a line start, now. */
1734 xassert (it->continuation_lines_width
1735 || IT_CHARPOS (it) == BEGV
1736 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1737 #endif /* 0 */
1738 }
1739
1740
1741 /* Initialize IT for stepping through current_buffer in window W,
1742 starting at position POS that includes overlay string and display
1743 vector/ control character translation position information. */
1744
1745 static void
1746 init_from_display_pos (it, w, pos)
1747 struct it *it;
1748 struct window *w;
1749 struct display_pos *pos;
1750 {
1751 /* Keep in mind: the call to reseat in init_iterator skips invisible
1752 text, so we might end up at a position different from POS. This
1753 is only a problem when POS is a row start after a newline and an
1754 overlay starts there with an after-string, and the overlay has an
1755 invisible property. Since we don't skip invisible text in
1756 display_line and elsewhere immediately after consuming the
1757 newline before the row start, such a POS will not be in a string,
1758 but the call to init_iterator below will move us to the
1759 after-string. */
1760 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1761 NULL, DEFAULT_FACE_ID);
1762
1763 /* If position is within an overlay string, set up IT to
1764 the right overlay string. */
1765 if (pos->overlay_string_index >= 0)
1766 {
1767 int relative_index;
1768
1769 /* We already have the first chunk of overlay strings in
1770 IT->overlay_strings. Load more until the one for
1771 pos->overlay_string_index is in IT->overlay_strings. */
1772 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1773 {
1774 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1775 it->current.overlay_string_index = 0;
1776 while (n--)
1777 {
1778 load_overlay_strings (it);
1779 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1780 }
1781 }
1782
1783 it->current.overlay_string_index = pos->overlay_string_index;
1784 relative_index = (it->current.overlay_string_index
1785 % OVERLAY_STRING_CHUNK_SIZE);
1786 it->string = it->overlay_strings[relative_index];
1787 xassert (STRINGP (it->string));
1788 it->current.string_pos = pos->string_pos;
1789 it->method = next_element_from_string;
1790 }
1791 else if (it->current.overlay_string_index >= 0)
1792 {
1793 /* If POS says we're already after an overlay string ending at
1794 POS, make sure to pop the iterator because it will be in
1795 front of that overlay string. When POS is ZV, we've thereby
1796 also ``processed'' overlay strings at ZV. */
1797 while (it->sp)
1798 pop_it (it);
1799 it->current.overlay_string_index = -1;
1800 it->method = next_element_from_buffer;
1801 if (CHARPOS (pos->pos) == ZV)
1802 it->overlay_strings_at_end_processed_p = 1;
1803 }
1804
1805 if (CHARPOS (pos->string_pos) >= 0)
1806 {
1807 /* Recorded position is not in an overlay string, but in another
1808 string. This can only be a string from a `display' property.
1809 IT should already be filled with that string. */
1810 it->current.string_pos = pos->string_pos;
1811 xassert (STRINGP (it->string));
1812 }
1813
1814 /* Restore position in display vector translations or control
1815 character translations. */
1816 if (pos->dpvec_index >= 0)
1817 {
1818 /* This fills IT->dpvec. */
1819 get_next_display_element (it);
1820 xassert (it->dpvec && it->current.dpvec_index == 0);
1821 it->current.dpvec_index = pos->dpvec_index;
1822 }
1823
1824 CHECK_IT (it);
1825 }
1826
1827
1828 /* Initialize IT for stepping through current_buffer in window W
1829 starting at ROW->start. */
1830
1831 static void
1832 init_to_row_start (it, w, row)
1833 struct it *it;
1834 struct window *w;
1835 struct glyph_row *row;
1836 {
1837 init_from_display_pos (it, w, &row->start);
1838 it->continuation_lines_width = row->continuation_lines_width;
1839 CHECK_IT (it);
1840 }
1841
1842
1843 /* Initialize IT for stepping through current_buffer in window W
1844 starting in the line following ROW, i.e. starting at ROW->end. */
1845
1846 static void
1847 init_to_row_end (it, w, row)
1848 struct it *it;
1849 struct window *w;
1850 struct glyph_row *row;
1851 {
1852 init_from_display_pos (it, w, &row->end);
1853
1854 if (row->continued_p)
1855 it->continuation_lines_width = (row->continuation_lines_width
1856 + row->pixel_width);
1857 CHECK_IT (it);
1858 }
1859
1860
1861
1862 \f
1863 /***********************************************************************
1864 Text properties
1865 ***********************************************************************/
1866
1867 /* Called when IT reaches IT->stop_charpos. Handle text property and
1868 overlay changes. Set IT->stop_charpos to the next position where
1869 to stop. */
1870
1871 static void
1872 handle_stop (it)
1873 struct it *it;
1874 {
1875 enum prop_handled handled;
1876 int handle_overlay_change_p = 1;
1877 struct props *p;
1878
1879 it->dpvec = NULL;
1880 it->current.dpvec_index = -1;
1881
1882 do
1883 {
1884 handled = HANDLED_NORMALLY;
1885
1886 /* Call text property handlers. */
1887 for (p = it_props; p->handler; ++p)
1888 {
1889 handled = p->handler (it);
1890
1891 if (handled == HANDLED_RECOMPUTE_PROPS)
1892 break;
1893 else if (handled == HANDLED_RETURN)
1894 return;
1895 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1896 handle_overlay_change_p = 0;
1897 }
1898
1899 if (handled != HANDLED_RECOMPUTE_PROPS)
1900 {
1901 /* Don't check for overlay strings below when set to deliver
1902 characters from a display vector. */
1903 if (it->method == next_element_from_display_vector)
1904 handle_overlay_change_p = 0;
1905
1906 /* Handle overlay changes. */
1907 if (handle_overlay_change_p)
1908 handled = handle_overlay_change (it);
1909
1910 /* Determine where to stop next. */
1911 if (handled == HANDLED_NORMALLY)
1912 compute_stop_pos (it);
1913 }
1914 }
1915 while (handled == HANDLED_RECOMPUTE_PROPS);
1916 }
1917
1918
1919 /* Compute IT->stop_charpos from text property and overlay change
1920 information for IT's current position. */
1921
1922 static void
1923 compute_stop_pos (it)
1924 struct it *it;
1925 {
1926 register INTERVAL iv, next_iv;
1927 Lisp_Object object, limit, position;
1928
1929 /* If nowhere else, stop at the end. */
1930 it->stop_charpos = it->end_charpos;
1931
1932 if (STRINGP (it->string))
1933 {
1934 /* Strings are usually short, so don't limit the search for
1935 properties. */
1936 object = it->string;
1937 limit = Qnil;
1938 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1939 }
1940 else
1941 {
1942 int charpos;
1943
1944 /* If next overlay change is in front of the current stop pos
1945 (which is IT->end_charpos), stop there. Note: value of
1946 next_overlay_change is point-max if no overlay change
1947 follows. */
1948 charpos = next_overlay_change (IT_CHARPOS (*it));
1949 if (charpos < it->stop_charpos)
1950 it->stop_charpos = charpos;
1951
1952 /* If showing the region, we have to stop at the region
1953 start or end because the face might change there. */
1954 if (it->region_beg_charpos > 0)
1955 {
1956 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1957 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1958 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1959 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1960 }
1961
1962 /* Set up variables for computing the stop position from text
1963 property changes. */
1964 XSETBUFFER (object, current_buffer);
1965 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1966 XSETFASTINT (position, IT_CHARPOS (*it));
1967
1968 }
1969
1970 /* Get the interval containing IT's position. Value is a null
1971 interval if there isn't such an interval. */
1972 iv = validate_interval_range (object, &position, &position, 0);
1973 if (!NULL_INTERVAL_P (iv))
1974 {
1975 Lisp_Object values_here[LAST_PROP_IDX];
1976 struct props *p;
1977
1978 /* Get properties here. */
1979 for (p = it_props; p->handler; ++p)
1980 values_here[p->idx] = textget (iv->plist, *p->name);
1981
1982 /* Look for an interval following iv that has different
1983 properties. */
1984 for (next_iv = next_interval (iv);
1985 (!NULL_INTERVAL_P (next_iv)
1986 && (NILP (limit)
1987 || XFASTINT (limit) > next_iv->position));
1988 next_iv = next_interval (next_iv))
1989 {
1990 for (p = it_props; p->handler; ++p)
1991 {
1992 Lisp_Object new_value;
1993
1994 new_value = textget (next_iv->plist, *p->name);
1995 if (!EQ (values_here[p->idx], new_value))
1996 break;
1997 }
1998
1999 if (p->handler)
2000 break;
2001 }
2002
2003 if (!NULL_INTERVAL_P (next_iv))
2004 {
2005 if (INTEGERP (limit)
2006 && next_iv->position >= XFASTINT (limit))
2007 /* No text property change up to limit. */
2008 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2009 else
2010 /* Text properties change in next_iv. */
2011 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2012 }
2013 }
2014
2015 xassert (STRINGP (it->string)
2016 || (it->stop_charpos >= BEGV
2017 && it->stop_charpos >= IT_CHARPOS (*it)));
2018 }
2019
2020
2021 /* Return the position of the next overlay change after POS in
2022 current_buffer. Value is point-max if no overlay change
2023 follows. This is like `next-overlay-change' but doesn't use
2024 xmalloc. */
2025
2026 static int
2027 next_overlay_change (pos)
2028 int pos;
2029 {
2030 int noverlays;
2031 int endpos;
2032 Lisp_Object *overlays;
2033 int len;
2034 int i;
2035
2036 /* Get all overlays at the given position. */
2037 len = 10;
2038 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2039 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2040 if (noverlays > len)
2041 {
2042 len = noverlays;
2043 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2044 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2045 }
2046
2047 /* If any of these overlays ends before endpos,
2048 use its ending point instead. */
2049 for (i = 0; i < noverlays; ++i)
2050 {
2051 Lisp_Object oend;
2052 int oendpos;
2053
2054 oend = OVERLAY_END (overlays[i]);
2055 oendpos = OVERLAY_POSITION (oend);
2056 endpos = min (endpos, oendpos);
2057 }
2058
2059 return endpos;
2060 }
2061
2062
2063 \f
2064 /***********************************************************************
2065 Fontification
2066 ***********************************************************************/
2067
2068 /* Handle changes in the `fontified' property of the current buffer by
2069 calling hook functions from Qfontification_functions to fontify
2070 regions of text. */
2071
2072 static enum prop_handled
2073 handle_fontified_prop (it)
2074 struct it *it;
2075 {
2076 Lisp_Object prop, pos;
2077 enum prop_handled handled = HANDLED_NORMALLY;
2078
2079 /* Get the value of the `fontified' property at IT's current buffer
2080 position. (The `fontified' property doesn't have a special
2081 meaning in strings.) If the value is nil, call functions from
2082 Qfontification_functions. */
2083 if (!STRINGP (it->string)
2084 && it->s == NULL
2085 && !NILP (Vfontification_functions)
2086 && !NILP (Vrun_hooks)
2087 && (pos = make_number (IT_CHARPOS (*it)),
2088 prop = Fget_char_property (pos, Qfontified, Qnil),
2089 NILP (prop)))
2090 {
2091 int count = BINDING_STACK_SIZE ();
2092 Lisp_Object val;
2093
2094 val = Vfontification_functions;
2095 specbind (Qfontification_functions, Qnil);
2096 specbind (Qafter_change_functions, Qnil);
2097
2098 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2099 safe_call1 (val, pos);
2100 else
2101 {
2102 Lisp_Object globals, fn;
2103 struct gcpro gcpro1, gcpro2;
2104
2105 globals = Qnil;
2106 GCPRO2 (val, globals);
2107
2108 for (; CONSP (val); val = XCDR (val))
2109 {
2110 fn = XCAR (val);
2111
2112 if (EQ (fn, Qt))
2113 {
2114 /* A value of t indicates this hook has a local
2115 binding; it means to run the global binding too.
2116 In a global value, t should not occur. If it
2117 does, we must ignore it to avoid an endless
2118 loop. */
2119 for (globals = Fdefault_value (Qfontification_functions);
2120 CONSP (globals);
2121 globals = XCDR (globals))
2122 {
2123 fn = XCAR (globals);
2124 if (!EQ (fn, Qt))
2125 safe_call1 (fn, pos);
2126 }
2127 }
2128 else
2129 safe_call1 (fn, pos);
2130 }
2131
2132 UNGCPRO;
2133 }
2134
2135 unbind_to (count, Qnil);
2136
2137 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2138 something. This avoids an endless loop if they failed to
2139 fontify the text for which reason ever. */
2140 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2141 handled = HANDLED_RECOMPUTE_PROPS;
2142 }
2143
2144 return handled;
2145 }
2146
2147
2148 \f
2149 /***********************************************************************
2150 Faces
2151 ***********************************************************************/
2152
2153 /* Set up iterator IT from face properties at its current position.
2154 Called from handle_stop. */
2155
2156 static enum prop_handled
2157 handle_face_prop (it)
2158 struct it *it;
2159 {
2160 int new_face_id, next_stop;
2161
2162 if (!STRINGP (it->string))
2163 {
2164 new_face_id
2165 = face_at_buffer_position (it->w,
2166 IT_CHARPOS (*it),
2167 it->region_beg_charpos,
2168 it->region_end_charpos,
2169 &next_stop,
2170 (IT_CHARPOS (*it)
2171 + TEXT_PROP_DISTANCE_LIMIT),
2172 0);
2173
2174 /* Is this a start of a run of characters with box face?
2175 Caveat: this can be called for a freshly initialized
2176 iterator; face_id is -1 is this case. We know that the new
2177 face will not change until limit, i.e. if the new face has a
2178 box, all characters up to limit will have one. But, as
2179 usual, we don't know whether limit is really the end. */
2180 if (new_face_id != it->face_id)
2181 {
2182 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2183
2184 /* If new face has a box but old face has not, this is
2185 the start of a run of characters with box, i.e. it has
2186 a shadow on the left side. The value of face_id of the
2187 iterator will be -1 if this is the initial call that gets
2188 the face. In this case, we have to look in front of IT's
2189 position and see whether there is a face != new_face_id. */
2190 it->start_of_box_run_p
2191 = (new_face->box != FACE_NO_BOX
2192 && (it->face_id >= 0
2193 || IT_CHARPOS (*it) == BEG
2194 || new_face_id != face_before_it_pos (it)));
2195 it->face_box_p = new_face->box != FACE_NO_BOX;
2196 }
2197 }
2198 else
2199 {
2200 int base_face_id, bufpos;
2201
2202 if (it->current.overlay_string_index >= 0)
2203 bufpos = IT_CHARPOS (*it);
2204 else
2205 bufpos = 0;
2206
2207 /* For strings from a buffer, i.e. overlay strings or strings
2208 from a `display' property, use the face at IT's current
2209 buffer position as the base face to merge with, so that
2210 overlay strings appear in the same face as surrounding
2211 text, unless they specify their own faces. */
2212 base_face_id = underlying_face_id (it);
2213
2214 new_face_id = face_at_string_position (it->w,
2215 it->string,
2216 IT_STRING_CHARPOS (*it),
2217 bufpos,
2218 it->region_beg_charpos,
2219 it->region_end_charpos,
2220 &next_stop,
2221 base_face_id, 0);
2222
2223 #if 0 /* This shouldn't be neccessary. Let's check it. */
2224 /* If IT is used to display a mode line we would really like to
2225 use the mode line face instead of the frame's default face. */
2226 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2227 && new_face_id == DEFAULT_FACE_ID)
2228 new_face_id = MODE_LINE_FACE_ID;
2229 #endif
2230
2231 /* Is this a start of a run of characters with box? Caveat:
2232 this can be called for a freshly allocated iterator; face_id
2233 is -1 is this case. We know that the new face will not
2234 change until the next check pos, i.e. if the new face has a
2235 box, all characters up to that position will have a
2236 box. But, as usual, we don't know whether that position
2237 is really the end. */
2238 if (new_face_id != it->face_id)
2239 {
2240 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2241 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2242
2243 /* If new face has a box but old face hasn't, this is the
2244 start of a run of characters with box, i.e. it has a
2245 shadow on the left side. */
2246 it->start_of_box_run_p
2247 = new_face->box && (old_face == NULL || !old_face->box);
2248 it->face_box_p = new_face->box != FACE_NO_BOX;
2249 }
2250 }
2251
2252 it->face_id = new_face_id;
2253 return HANDLED_NORMALLY;
2254 }
2255
2256
2257 /* Return the ID of the face ``underlying'' IT's current position,
2258 which is in a string. If the iterator is associated with a
2259 buffer, return the face at IT's current buffer position.
2260 Otherwise, use the iterator's base_face_id. */
2261
2262 static int
2263 underlying_face_id (it)
2264 struct it *it;
2265 {
2266 int face_id = it->base_face_id, i;
2267
2268 xassert (STRINGP (it->string));
2269
2270 for (i = it->sp - 1; i >= 0; --i)
2271 if (NILP (it->stack[i].string))
2272 face_id = it->stack[i].face_id;
2273
2274 return face_id;
2275 }
2276
2277
2278 /* Compute the face one character before or after the current position
2279 of IT. BEFORE_P non-zero means get the face in front of IT's
2280 position. Value is the id of the face. */
2281
2282 static int
2283 face_before_or_after_it_pos (it, before_p)
2284 struct it *it;
2285 int before_p;
2286 {
2287 int face_id, limit;
2288 int next_check_charpos;
2289 struct text_pos pos;
2290
2291 xassert (it->s == NULL);
2292
2293 if (STRINGP (it->string))
2294 {
2295 int bufpos, base_face_id;
2296
2297 /* No face change past the end of the string (for the case
2298 we are padding with spaces). No face change before the
2299 string start. */
2300 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2301 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2302 return it->face_id;
2303
2304 /* Set pos to the position before or after IT's current position. */
2305 if (before_p)
2306 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2307 else
2308 /* For composition, we must check the character after the
2309 composition. */
2310 pos = (it->what == IT_COMPOSITION
2311 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2312 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2313
2314 if (it->current.overlay_string_index >= 0)
2315 bufpos = IT_CHARPOS (*it);
2316 else
2317 bufpos = 0;
2318
2319 base_face_id = underlying_face_id (it);
2320
2321 /* Get the face for ASCII, or unibyte. */
2322 face_id = face_at_string_position (it->w,
2323 it->string,
2324 CHARPOS (pos),
2325 bufpos,
2326 it->region_beg_charpos,
2327 it->region_end_charpos,
2328 &next_check_charpos,
2329 base_face_id, 0);
2330
2331 /* Correct the face for charsets different from ASCII. Do it
2332 for the multibyte case only. The face returned above is
2333 suitable for unibyte text if IT->string is unibyte. */
2334 if (STRING_MULTIBYTE (it->string))
2335 {
2336 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2337 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2338 int c, len;
2339 struct face *face = FACE_FROM_ID (it->f, face_id);
2340
2341 c = string_char_and_length (p, rest, &len);
2342 face_id = FACE_FOR_CHAR (it->f, face, c);
2343 }
2344 }
2345 else
2346 {
2347 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2348 || (IT_CHARPOS (*it) <= BEGV && before_p))
2349 return it->face_id;
2350
2351 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2352 pos = it->current.pos;
2353
2354 if (before_p)
2355 DEC_TEXT_POS (pos, it->multibyte_p);
2356 else
2357 {
2358 if (it->what == IT_COMPOSITION)
2359 /* For composition, we must check the position after the
2360 composition. */
2361 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2362 else
2363 INC_TEXT_POS (pos, it->multibyte_p);
2364 }
2365
2366 /* Determine face for CHARSET_ASCII, or unibyte. */
2367 face_id = face_at_buffer_position (it->w,
2368 CHARPOS (pos),
2369 it->region_beg_charpos,
2370 it->region_end_charpos,
2371 &next_check_charpos,
2372 limit, 0);
2373
2374 /* Correct the face for charsets different from ASCII. Do it
2375 for the multibyte case only. The face returned above is
2376 suitable for unibyte text if current_buffer is unibyte. */
2377 if (it->multibyte_p)
2378 {
2379 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2380 struct face *face = FACE_FROM_ID (it->f, face_id);
2381 face_id = FACE_FOR_CHAR (it->f, face, c);
2382 }
2383 }
2384
2385 return face_id;
2386 }
2387
2388
2389 \f
2390 /***********************************************************************
2391 Invisible text
2392 ***********************************************************************/
2393
2394 /* Set up iterator IT from invisible properties at its current
2395 position. Called from handle_stop. */
2396
2397 static enum prop_handled
2398 handle_invisible_prop (it)
2399 struct it *it;
2400 {
2401 enum prop_handled handled = HANDLED_NORMALLY;
2402
2403 if (STRINGP (it->string))
2404 {
2405 extern Lisp_Object Qinvisible;
2406 Lisp_Object prop, end_charpos, limit, charpos;
2407
2408 /* Get the value of the invisible text property at the
2409 current position. Value will be nil if there is no such
2410 property. */
2411 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2412 prop = Fget_text_property (charpos, Qinvisible, it->string);
2413
2414 if (!NILP (prop)
2415 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2416 {
2417 handled = HANDLED_RECOMPUTE_PROPS;
2418
2419 /* Get the position at which the next change of the
2420 invisible text property can be found in IT->string.
2421 Value will be nil if the property value is the same for
2422 all the rest of IT->string. */
2423 XSETINT (limit, XSTRING (it->string)->size);
2424 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2425 it->string, limit);
2426
2427 /* Text at current position is invisible. The next
2428 change in the property is at position end_charpos.
2429 Move IT's current position to that position. */
2430 if (INTEGERP (end_charpos)
2431 && XFASTINT (end_charpos) < XFASTINT (limit))
2432 {
2433 struct text_pos old;
2434 old = it->current.string_pos;
2435 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2436 compute_string_pos (&it->current.string_pos, old, it->string);
2437 }
2438 else
2439 {
2440 /* The rest of the string is invisible. If this is an
2441 overlay string, proceed with the next overlay string
2442 or whatever comes and return a character from there. */
2443 if (it->current.overlay_string_index >= 0)
2444 {
2445 next_overlay_string (it);
2446 /* Don't check for overlay strings when we just
2447 finished processing them. */
2448 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2449 }
2450 else
2451 {
2452 struct Lisp_String *s = XSTRING (it->string);
2453 IT_STRING_CHARPOS (*it) = s->size;
2454 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2455 }
2456 }
2457 }
2458 }
2459 else
2460 {
2461 int visible_p, newpos, next_stop;
2462 Lisp_Object pos, prop;
2463
2464 /* First of all, is there invisible text at this position? */
2465 XSETFASTINT (pos, IT_CHARPOS (*it));
2466 prop = Fget_char_property (pos, Qinvisible, it->window);
2467
2468 /* If we are on invisible text, skip over it. */
2469 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2470 && IT_CHARPOS (*it) < it->end_charpos)
2471 {
2472 /* Record whether we have to display an ellipsis for the
2473 invisible text. */
2474 int display_ellipsis_p
2475 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2476
2477 handled = HANDLED_RECOMPUTE_PROPS;
2478
2479 /* Loop skipping over invisible text. The loop is left at
2480 ZV or with IT on the first char being visible again. */
2481 do
2482 {
2483 /* Try to skip some invisible text. Return value is the
2484 position reached which can be equal to IT's position
2485 if there is nothing invisible here. This skips both
2486 over invisible text properties and overlays with
2487 invisible property. */
2488 newpos = skip_invisible (IT_CHARPOS (*it),
2489 &next_stop, ZV, it->window);
2490
2491 /* If we skipped nothing at all we weren't at invisible
2492 text in the first place. If everything to the end of
2493 the buffer was skipped, end the loop. */
2494 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2495 visible_p = 1;
2496 else
2497 {
2498 /* We skipped some characters but not necessarily
2499 all there are. Check if we ended up on visible
2500 text. Fget_char_property returns the property of
2501 the char before the given position, i.e. if we
2502 get visible_p = 1, this means that the char at
2503 newpos is visible. */
2504 XSETFASTINT (pos, newpos);
2505 prop = Fget_char_property (pos, Qinvisible, it->window);
2506 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2507 }
2508
2509 /* If we ended up on invisible text, proceed to
2510 skip starting with next_stop. */
2511 if (!visible_p)
2512 IT_CHARPOS (*it) = next_stop;
2513 }
2514 while (!visible_p);
2515
2516 /* The position newpos is now either ZV or on visible text. */
2517 IT_CHARPOS (*it) = newpos;
2518 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2519
2520 /* Maybe return `...' next for the end of the invisible text. */
2521 if (display_ellipsis_p)
2522 {
2523 if (it->dp
2524 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2525 {
2526 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2527 it->dpvec = v->contents;
2528 it->dpend = v->contents + v->size;
2529 }
2530 else
2531 {
2532 /* Default `...'. */
2533 it->dpvec = default_invis_vector;
2534 it->dpend = default_invis_vector + 3;
2535 }
2536
2537 /* The ellipsis display does not replace the display of
2538 the character at the new position. Indicate this by
2539 setting IT->dpvec_char_len to zero. */
2540 it->dpvec_char_len = 0;
2541
2542 it->current.dpvec_index = 0;
2543 it->method = next_element_from_display_vector;
2544 }
2545 }
2546 }
2547
2548 return handled;
2549 }
2550
2551
2552 \f
2553 /***********************************************************************
2554 'display' property
2555 ***********************************************************************/
2556
2557 /* Set up iterator IT from `display' property at its current position.
2558 Called from handle_stop. */
2559
2560 static enum prop_handled
2561 handle_display_prop (it)
2562 struct it *it;
2563 {
2564 Lisp_Object prop, object;
2565 struct text_pos *position;
2566 int display_replaced_p = 0;
2567
2568 if (STRINGP (it->string))
2569 {
2570 object = it->string;
2571 position = &it->current.string_pos;
2572 }
2573 else
2574 {
2575 object = it->w->buffer;
2576 position = &it->current.pos;
2577 }
2578
2579 /* Reset those iterator values set from display property values. */
2580 it->font_height = Qnil;
2581 it->space_width = Qnil;
2582 it->voffset = 0;
2583
2584 /* We don't support recursive `display' properties, i.e. string
2585 values that have a string `display' property, that have a string
2586 `display' property etc. */
2587 if (!it->string_from_display_prop_p)
2588 it->area = TEXT_AREA;
2589
2590 prop = Fget_char_property (make_number (position->charpos),
2591 Qdisplay, object);
2592 if (NILP (prop))
2593 return HANDLED_NORMALLY;
2594
2595 if (CONSP (prop)
2596 && CONSP (XCAR (prop))
2597 && !EQ (Qmargin, XCAR (XCAR (prop))))
2598 {
2599 /* A list of sub-properties. */
2600 for (; CONSP (prop); prop = XCDR (prop))
2601 {
2602 if (handle_single_display_prop (it, XCAR (prop), object,
2603 position, display_replaced_p))
2604 display_replaced_p = 1;
2605 }
2606 }
2607 else if (VECTORP (prop))
2608 {
2609 int i;
2610 for (i = 0; i < ASIZE (prop); ++i)
2611 if (handle_single_display_prop (it, AREF (prop, i), object,
2612 position, display_replaced_p))
2613 display_replaced_p = 1;
2614 }
2615 else
2616 {
2617 if (handle_single_display_prop (it, prop, object, position, 0))
2618 display_replaced_p = 1;
2619 }
2620
2621 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2622 }
2623
2624
2625 /* Value is the position of the end of the `display' property starting
2626 at START_POS in OBJECT. */
2627
2628 static struct text_pos
2629 display_prop_end (it, object, start_pos)
2630 struct it *it;
2631 Lisp_Object object;
2632 struct text_pos start_pos;
2633 {
2634 Lisp_Object end;
2635 struct text_pos end_pos;
2636
2637 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2638 Qdisplay, object, Qnil);
2639 CHARPOS (end_pos) = XFASTINT (end);
2640 if (STRINGP (object))
2641 compute_string_pos (&end_pos, start_pos, it->string);
2642 else
2643 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2644
2645 return end_pos;
2646 }
2647
2648
2649 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2650 is the object in which the `display' property was found. *POSITION
2651 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2652 means that we previously saw a display sub-property which already
2653 replaced text display with something else, for example an image;
2654 ignore such properties after the first one has been processed.
2655
2656 If PROP is a `space' or `image' sub-property, set *POSITION to the
2657 end position of the `display' property.
2658
2659 Value is non-zero something was found which replaces the display
2660 of buffer or string text. */
2661
2662 static int
2663 handle_single_display_prop (it, prop, object, position,
2664 display_replaced_before_p)
2665 struct it *it;
2666 Lisp_Object prop;
2667 Lisp_Object object;
2668 struct text_pos *position;
2669 int display_replaced_before_p;
2670 {
2671 Lisp_Object value;
2672 int replaces_text_display_p = 0;
2673 Lisp_Object form;
2674
2675 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2676 evaluated. If the result is nil, VALUE is ignored. */
2677 form = Qt;
2678 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2679 {
2680 prop = XCDR (prop);
2681 if (!CONSP (prop))
2682 return 0;
2683 form = XCAR (prop);
2684 prop = XCDR (prop);
2685 }
2686
2687 if (!NILP (form) && !EQ (form, Qt))
2688 {
2689 struct gcpro gcpro1;
2690 struct text_pos end_pos, pt;
2691
2692 GCPRO1 (form);
2693 end_pos = display_prop_end (it, object, *position);
2694
2695 /* Temporarily set point to the end position, and then evaluate
2696 the form. This makes `(eolp)' work as FORM. */
2697 if (BUFFERP (object))
2698 {
2699 CHARPOS (pt) = PT;
2700 BYTEPOS (pt) = PT_BYTE;
2701 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2702 }
2703
2704 form = safe_eval (form);
2705
2706 if (BUFFERP (object))
2707 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2708 UNGCPRO;
2709 }
2710
2711 if (NILP (form))
2712 return 0;
2713
2714 if (CONSP (prop)
2715 && EQ (XCAR (prop), Qheight)
2716 && CONSP (XCDR (prop)))
2717 {
2718 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2719 return 0;
2720
2721 /* `(height HEIGHT)'. */
2722 it->font_height = XCAR (XCDR (prop));
2723 if (!NILP (it->font_height))
2724 {
2725 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2726 int new_height = -1;
2727
2728 if (CONSP (it->font_height)
2729 && (EQ (XCAR (it->font_height), Qplus)
2730 || EQ (XCAR (it->font_height), Qminus))
2731 && CONSP (XCDR (it->font_height))
2732 && INTEGERP (XCAR (XCDR (it->font_height))))
2733 {
2734 /* `(+ N)' or `(- N)' where N is an integer. */
2735 int steps = XINT (XCAR (XCDR (it->font_height)));
2736 if (EQ (XCAR (it->font_height), Qplus))
2737 steps = - steps;
2738 it->face_id = smaller_face (it->f, it->face_id, steps);
2739 }
2740 else if (FUNCTIONP (it->font_height))
2741 {
2742 /* Call function with current height as argument.
2743 Value is the new height. */
2744 Lisp_Object height;
2745 height = safe_call1 (it->font_height,
2746 face->lface[LFACE_HEIGHT_INDEX]);
2747 if (NUMBERP (height))
2748 new_height = XFLOATINT (height);
2749 }
2750 else if (NUMBERP (it->font_height))
2751 {
2752 /* Value is a multiple of the canonical char height. */
2753 struct face *face;
2754
2755 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2756 new_height = (XFLOATINT (it->font_height)
2757 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2758 }
2759 else
2760 {
2761 /* Evaluate IT->font_height with `height' bound to the
2762 current specified height to get the new height. */
2763 Lisp_Object value;
2764 int count = BINDING_STACK_SIZE ();
2765
2766 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2767 value = safe_eval (it->font_height);
2768 unbind_to (count, Qnil);
2769
2770 if (NUMBERP (value))
2771 new_height = XFLOATINT (value);
2772 }
2773
2774 if (new_height > 0)
2775 it->face_id = face_with_height (it->f, it->face_id, new_height);
2776 }
2777 }
2778 else if (CONSP (prop)
2779 && EQ (XCAR (prop), Qspace_width)
2780 && CONSP (XCDR (prop)))
2781 {
2782 /* `(space_width WIDTH)'. */
2783 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2784 return 0;
2785
2786 value = XCAR (XCDR (prop));
2787 if (NUMBERP (value) && XFLOATINT (value) > 0)
2788 it->space_width = value;
2789 }
2790 else if (CONSP (prop)
2791 && EQ (XCAR (prop), Qraise)
2792 && CONSP (XCDR (prop)))
2793 {
2794 /* `(raise FACTOR)'. */
2795 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2796 return 0;
2797
2798 #ifdef HAVE_WINDOW_SYSTEM
2799 value = XCAR (XCDR (prop));
2800 if (NUMBERP (value))
2801 {
2802 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2803 it->voffset = - (XFLOATINT (value)
2804 * (FONT_HEIGHT (face->font)));
2805 }
2806 #endif /* HAVE_WINDOW_SYSTEM */
2807 }
2808 else if (!it->string_from_display_prop_p)
2809 {
2810 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2811 VALUE) or `((margin nil) VALUE)' or VALUE. */
2812 Lisp_Object location, value;
2813 struct text_pos start_pos;
2814 int valid_p;
2815
2816 /* Characters having this form of property are not displayed, so
2817 we have to find the end of the property. */
2818 start_pos = *position;
2819 *position = display_prop_end (it, object, start_pos);
2820 value = Qnil;
2821
2822 /* Let's stop at the new position and assume that all
2823 text properties change there. */
2824 it->stop_charpos = position->charpos;
2825
2826 location = Qunbound;
2827 if (CONSP (prop) && CONSP (XCAR (prop)))
2828 {
2829 Lisp_Object tem;
2830
2831 value = XCDR (prop);
2832 if (CONSP (value))
2833 value = XCAR (value);
2834
2835 tem = XCAR (prop);
2836 if (EQ (XCAR (tem), Qmargin)
2837 && (tem = XCDR (tem),
2838 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2839 (NILP (tem)
2840 || EQ (tem, Qleft_margin)
2841 || EQ (tem, Qright_margin))))
2842 location = tem;
2843 }
2844
2845 if (EQ (location, Qunbound))
2846 {
2847 location = Qnil;
2848 value = prop;
2849 }
2850
2851 #ifdef HAVE_WINDOW_SYSTEM
2852 if (FRAME_TERMCAP_P (it->f))
2853 valid_p = STRINGP (value);
2854 else
2855 valid_p = (STRINGP (value)
2856 || (CONSP (value) && EQ (XCAR (value), Qspace))
2857 || valid_image_p (value));
2858 #else /* not HAVE_WINDOW_SYSTEM */
2859 valid_p = STRINGP (value);
2860 #endif /* not HAVE_WINDOW_SYSTEM */
2861
2862 if ((EQ (location, Qleft_margin)
2863 || EQ (location, Qright_margin)
2864 || NILP (location))
2865 && valid_p
2866 && !display_replaced_before_p)
2867 {
2868 replaces_text_display_p = 1;
2869
2870 /* Save current settings of IT so that we can restore them
2871 when we are finished with the glyph property value. */
2872 push_it (it);
2873
2874 if (NILP (location))
2875 it->area = TEXT_AREA;
2876 else if (EQ (location, Qleft_margin))
2877 it->area = LEFT_MARGIN_AREA;
2878 else
2879 it->area = RIGHT_MARGIN_AREA;
2880
2881 if (STRINGP (value))
2882 {
2883 it->string = value;
2884 it->multibyte_p = STRING_MULTIBYTE (it->string);
2885 it->current.overlay_string_index = -1;
2886 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2887 it->end_charpos = it->string_nchars
2888 = XSTRING (it->string)->size;
2889 it->method = next_element_from_string;
2890 it->stop_charpos = 0;
2891 it->string_from_display_prop_p = 1;
2892 /* Say that we haven't consumed the characters with
2893 `display' property yet. The call to pop_it in
2894 set_iterator_to_next will clean this up. */
2895 *position = start_pos;
2896 }
2897 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2898 {
2899 it->method = next_element_from_stretch;
2900 it->object = value;
2901 it->current.pos = it->position = start_pos;
2902 }
2903 #ifdef HAVE_WINDOW_SYSTEM
2904 else
2905 {
2906 it->what = IT_IMAGE;
2907 it->image_id = lookup_image (it->f, value);
2908 it->position = start_pos;
2909 it->object = NILP (object) ? it->w->buffer : object;
2910 it->method = next_element_from_image;
2911
2912 /* Say that we haven't consumed the characters with
2913 `display' property yet. The call to pop_it in
2914 set_iterator_to_next will clean this up. */
2915 *position = start_pos;
2916 }
2917 #endif /* HAVE_WINDOW_SYSTEM */
2918 }
2919 else
2920 /* Invalid property or property not supported. Restore
2921 the position to what it was before. */
2922 *position = start_pos;
2923 }
2924
2925 return replaces_text_display_p;
2926 }
2927
2928
2929 /* Check if PROP is a display sub-property value whose text should be
2930 treated as intangible. */
2931
2932 static int
2933 single_display_prop_intangible_p (prop)
2934 Lisp_Object prop;
2935 {
2936 /* Skip over `when FORM'. */
2937 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2938 {
2939 prop = XCDR (prop);
2940 if (!CONSP (prop))
2941 return 0;
2942 prop = XCDR (prop);
2943 }
2944
2945 if (!CONSP (prop))
2946 return 0;
2947
2948 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2949 we don't need to treat text as intangible. */
2950 if (EQ (XCAR (prop), Qmargin))
2951 {
2952 prop = XCDR (prop);
2953 if (!CONSP (prop))
2954 return 0;
2955
2956 prop = XCDR (prop);
2957 if (!CONSP (prop)
2958 || EQ (XCAR (prop), Qleft_margin)
2959 || EQ (XCAR (prop), Qright_margin))
2960 return 0;
2961 }
2962
2963 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2964 }
2965
2966
2967 /* Check if PROP is a display property value whose text should be
2968 treated as intangible. */
2969
2970 int
2971 display_prop_intangible_p (prop)
2972 Lisp_Object prop;
2973 {
2974 if (CONSP (prop)
2975 && CONSP (XCAR (prop))
2976 && !EQ (Qmargin, XCAR (XCAR (prop))))
2977 {
2978 /* A list of sub-properties. */
2979 while (CONSP (prop))
2980 {
2981 if (single_display_prop_intangible_p (XCAR (prop)))
2982 return 1;
2983 prop = XCDR (prop);
2984 }
2985 }
2986 else if (VECTORP (prop))
2987 {
2988 /* A vector of sub-properties. */
2989 int i;
2990 for (i = 0; i < ASIZE (prop); ++i)
2991 if (single_display_prop_intangible_p (AREF (prop, i)))
2992 return 1;
2993 }
2994 else
2995 return single_display_prop_intangible_p (prop);
2996
2997 return 0;
2998 }
2999
3000
3001 /* Return 1 if PROP is a display sub-property value containing STRING. */
3002
3003 static int
3004 single_display_prop_string_p (prop, string)
3005 Lisp_Object prop, string;
3006 {
3007 extern Lisp_Object Qwhen, Qmargin;
3008
3009 if (EQ (string, prop))
3010 return 1;
3011
3012 /* Skip over `when FORM'. */
3013 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3014 {
3015 prop = XCDR (prop);
3016 if (!CONSP (prop))
3017 return 0;
3018 prop = XCDR (prop);
3019 }
3020
3021 if (CONSP (prop))
3022 /* Skip over `margin LOCATION'. */
3023 if (EQ (XCAR (prop), Qmargin))
3024 {
3025 prop = XCDR (prop);
3026 if (!CONSP (prop))
3027 return 0;
3028
3029 prop = XCDR (prop);
3030 if (!CONSP (prop))
3031 return 0;
3032 }
3033
3034 return CONSP (prop) && EQ (XCAR (prop), string);
3035 }
3036
3037
3038 /* Return 1 if STRING appears in the `display' property PROP. */
3039
3040 static int
3041 display_prop_string_p (prop, string)
3042 Lisp_Object prop, string;
3043 {
3044 extern Lisp_Object Qwhen, Qmargin;
3045
3046 if (CONSP (prop)
3047 && CONSP (XCAR (prop))
3048 && !EQ (Qmargin, XCAR (XCAR (prop))))
3049 {
3050 /* A list of sub-properties. */
3051 while (CONSP (prop))
3052 {
3053 if (single_display_prop_string_p (XCAR (prop), string))
3054 return 1;
3055 prop = XCDR (prop);
3056 }
3057 }
3058 else if (VECTORP (prop))
3059 {
3060 /* A vector of sub-properties. */
3061 int i;
3062 for (i = 0; i < ASIZE (prop); ++i)
3063 if (single_display_prop_string_p (AREF (prop, i), string))
3064 return 1;
3065 }
3066 else
3067 return single_display_prop_string_p (prop, string);
3068
3069 return 0;
3070 }
3071
3072
3073 /* Determine from which buffer position in W's buffer STRING comes
3074 from. AROUND_CHARPOS is an approximate position where it could
3075 be from. Value is the buffer position or 0 if it couldn't be
3076 determined.
3077
3078 W's buffer must be current.
3079
3080 This function is necessary because we don't record buffer positions
3081 in glyphs generated from strings (to keep struct glyph small).
3082 This function may only use code that doesn't eval because it is
3083 called asynchronously from note_mouse_highlight. */
3084
3085 int
3086 string_buffer_position (w, string, around_charpos)
3087 struct window *w;
3088 Lisp_Object string;
3089 int around_charpos;
3090 {
3091 Lisp_Object around = make_number (around_charpos);
3092 Lisp_Object limit, prop, pos;
3093 const int MAX_DISTANCE = 1000;
3094 int found = 0;
3095
3096 pos = around_charpos;
3097 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3098 while (!found && !EQ (pos, limit))
3099 {
3100 prop = Fget_char_property (pos, Qdisplay, Qnil);
3101 if (!NILP (prop) && display_prop_string_p (prop, string))
3102 found = 1;
3103 else
3104 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3105 }
3106
3107 if (!found)
3108 {
3109 pos = around_charpos;
3110 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3111 while (!found && !EQ (pos, limit))
3112 {
3113 prop = Fget_char_property (pos, Qdisplay, Qnil);
3114 if (!NILP (prop) && display_prop_string_p (prop, string))
3115 found = 1;
3116 else
3117 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3118 limit);
3119 }
3120 }
3121
3122 return found ? XINT (pos) : 0;
3123 }
3124
3125
3126 \f
3127 /***********************************************************************
3128 `composition' property
3129 ***********************************************************************/
3130
3131 /* Set up iterator IT from `composition' property at its current
3132 position. Called from handle_stop. */
3133
3134 static enum prop_handled
3135 handle_composition_prop (it)
3136 struct it *it;
3137 {
3138 Lisp_Object prop, string;
3139 int pos, pos_byte, end;
3140 enum prop_handled handled = HANDLED_NORMALLY;
3141
3142 if (STRINGP (it->string))
3143 {
3144 pos = IT_STRING_CHARPOS (*it);
3145 pos_byte = IT_STRING_BYTEPOS (*it);
3146 string = it->string;
3147 }
3148 else
3149 {
3150 pos = IT_CHARPOS (*it);
3151 pos_byte = IT_BYTEPOS (*it);
3152 string = Qnil;
3153 }
3154
3155 /* If there's a valid composition and point is not inside of the
3156 composition (in the case that the composition is from the current
3157 buffer), draw a glyph composed from the composition components. */
3158 if (find_composition (pos, -1, &pos, &end, &prop, string)
3159 && COMPOSITION_VALID_P (pos, end, prop)
3160 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3161 {
3162 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3163
3164 if (id >= 0)
3165 {
3166 it->method = next_element_from_composition;
3167 it->cmp_id = id;
3168 it->cmp_len = COMPOSITION_LENGTH (prop);
3169 /* For a terminal, draw only the first character of the
3170 components. */
3171 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3172 it->len = (STRINGP (it->string)
3173 ? string_char_to_byte (it->string, end)
3174 : CHAR_TO_BYTE (end)) - pos_byte;
3175 it->stop_charpos = end;
3176 handled = HANDLED_RETURN;
3177 }
3178 }
3179
3180 return handled;
3181 }
3182
3183
3184 \f
3185 /***********************************************************************
3186 Overlay strings
3187 ***********************************************************************/
3188
3189 /* The following structure is used to record overlay strings for
3190 later sorting in load_overlay_strings. */
3191
3192 struct overlay_entry
3193 {
3194 Lisp_Object overlay;
3195 Lisp_Object string;
3196 int priority;
3197 int after_string_p;
3198 };
3199
3200
3201 /* Set up iterator IT from overlay strings at its current position.
3202 Called from handle_stop. */
3203
3204 static enum prop_handled
3205 handle_overlay_change (it)
3206 struct it *it;
3207 {
3208 if (!STRINGP (it->string) && get_overlay_strings (it))
3209 return HANDLED_RECOMPUTE_PROPS;
3210 else
3211 return HANDLED_NORMALLY;
3212 }
3213
3214
3215 /* Set up the next overlay string for delivery by IT, if there is an
3216 overlay string to deliver. Called by set_iterator_to_next when the
3217 end of the current overlay string is reached. If there are more
3218 overlay strings to display, IT->string and
3219 IT->current.overlay_string_index are set appropriately here.
3220 Otherwise IT->string is set to nil. */
3221
3222 static void
3223 next_overlay_string (it)
3224 struct it *it;
3225 {
3226 ++it->current.overlay_string_index;
3227 if (it->current.overlay_string_index == it->n_overlay_strings)
3228 {
3229 /* No more overlay strings. Restore IT's settings to what
3230 they were before overlay strings were processed, and
3231 continue to deliver from current_buffer. */
3232 pop_it (it);
3233 xassert (it->stop_charpos >= BEGV
3234 && it->stop_charpos <= it->end_charpos);
3235 it->string = Qnil;
3236 it->current.overlay_string_index = -1;
3237 SET_TEXT_POS (it->current.string_pos, -1, -1);
3238 it->n_overlay_strings = 0;
3239 it->method = next_element_from_buffer;
3240
3241 /* If we're at the end of the buffer, record that we have
3242 processed the overlay strings there already, so that
3243 next_element_from_buffer doesn't try it again. */
3244 if (IT_CHARPOS (*it) >= it->end_charpos)
3245 it->overlay_strings_at_end_processed_p = 1;
3246 }
3247 else
3248 {
3249 /* There are more overlay strings to process. If
3250 IT->current.overlay_string_index has advanced to a position
3251 where we must load IT->overlay_strings with more strings, do
3252 it. */
3253 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3254
3255 if (it->current.overlay_string_index && i == 0)
3256 load_overlay_strings (it);
3257
3258 /* Initialize IT to deliver display elements from the overlay
3259 string. */
3260 it->string = it->overlay_strings[i];
3261 it->multibyte_p = STRING_MULTIBYTE (it->string);
3262 SET_TEXT_POS (it->current.string_pos, 0, 0);
3263 it->method = next_element_from_string;
3264 it->stop_charpos = 0;
3265 }
3266
3267 CHECK_IT (it);
3268 }
3269
3270
3271 /* Compare two overlay_entry structures E1 and E2. Used as a
3272 comparison function for qsort in load_overlay_strings. Overlay
3273 strings for the same position are sorted so that
3274
3275 1. All after-strings come in front of before-strings, except
3276 when they come from the same overlay.
3277
3278 2. Within after-strings, strings are sorted so that overlay strings
3279 from overlays with higher priorities come first.
3280
3281 2. Within before-strings, strings are sorted so that overlay
3282 strings from overlays with higher priorities come last.
3283
3284 Value is analogous to strcmp. */
3285
3286
3287 static int
3288 compare_overlay_entries (e1, e2)
3289 void *e1, *e2;
3290 {
3291 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3292 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3293 int result;
3294
3295 if (entry1->after_string_p != entry2->after_string_p)
3296 {
3297 /* Let after-strings appear in front of before-strings if
3298 they come from different overlays. */
3299 if (EQ (entry1->overlay, entry2->overlay))
3300 result = entry1->after_string_p ? 1 : -1;
3301 else
3302 result = entry1->after_string_p ? -1 : 1;
3303 }
3304 else if (entry1->after_string_p)
3305 /* After-strings sorted in order of decreasing priority. */
3306 result = entry2->priority - entry1->priority;
3307 else
3308 /* Before-strings sorted in order of increasing priority. */
3309 result = entry1->priority - entry2->priority;
3310
3311 return result;
3312 }
3313
3314
3315 /* Load the vector IT->overlay_strings with overlay strings from IT's
3316 current buffer position. Set IT->n_overlays to the total number of
3317 overlay strings found.
3318
3319 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3320 a time. On entry into load_overlay_strings,
3321 IT->current.overlay_string_index gives the number of overlay
3322 strings that have already been loaded by previous calls to this
3323 function.
3324
3325 IT->add_overlay_start contains an additional overlay start
3326 position to consider for taking overlay strings from, if non-zero.
3327 This position comes into play when the overlay has an `invisible'
3328 property, and both before and after-strings. When we've skipped to
3329 the end of the overlay, because of its `invisible' property, we
3330 nevertheless want its before-string to appear.
3331 IT->add_overlay_start will contain the overlay start position
3332 in this case.
3333
3334 Overlay strings are sorted so that after-string strings come in
3335 front of before-string strings. Within before and after-strings,
3336 strings are sorted by overlay priority. See also function
3337 compare_overlay_entries. */
3338
3339 static void
3340 load_overlay_strings (it)
3341 struct it *it;
3342 {
3343 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3344 Lisp_Object ov, overlay, window, str, invisible;
3345 int start, end;
3346 int size = 20;
3347 int n = 0, i, j, invis_p;
3348 struct overlay_entry *entries
3349 = (struct overlay_entry *) alloca (size * sizeof *entries);
3350 int charpos = IT_CHARPOS (*it);
3351
3352 /* Append the overlay string STRING of overlay OVERLAY to vector
3353 `entries' which has size `size' and currently contains `n'
3354 elements. AFTER_P non-zero means STRING is an after-string of
3355 OVERLAY. */
3356 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3357 do \
3358 { \
3359 Lisp_Object priority; \
3360 \
3361 if (n == size) \
3362 { \
3363 int new_size = 2 * size; \
3364 struct overlay_entry *old = entries; \
3365 entries = \
3366 (struct overlay_entry *) alloca (new_size \
3367 * sizeof *entries); \
3368 bcopy (old, entries, size * sizeof *entries); \
3369 size = new_size; \
3370 } \
3371 \
3372 entries[n].string = (STRING); \
3373 entries[n].overlay = (OVERLAY); \
3374 priority = Foverlay_get ((OVERLAY), Qpriority); \
3375 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3376 entries[n].after_string_p = (AFTER_P); \
3377 ++n; \
3378 } \
3379 while (0)
3380
3381 /* Process overlay before the overlay center. */
3382 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3383 {
3384 overlay = XCAR (ov);
3385 xassert (OVERLAYP (overlay));
3386 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3387 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3388
3389 if (end < charpos)
3390 break;
3391
3392 /* Skip this overlay if it doesn't start or end at IT's current
3393 position. */
3394 if (end != charpos && start != charpos)
3395 continue;
3396
3397 /* Skip this overlay if it doesn't apply to IT->w. */
3398 window = Foverlay_get (overlay, Qwindow);
3399 if (WINDOWP (window) && XWINDOW (window) != it->w)
3400 continue;
3401
3402 /* If the text ``under'' the overlay is invisible, both before-
3403 and after-strings from this overlay are visible; start and
3404 end position are indistinguishable. */
3405 invisible = Foverlay_get (overlay, Qinvisible);
3406 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3407
3408 /* If overlay has a non-empty before-string, record it. */
3409 if ((start == charpos || (end == charpos && invis_p))
3410 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3411 && XSTRING (str)->size)
3412 RECORD_OVERLAY_STRING (overlay, str, 0);
3413
3414 /* If overlay has a non-empty after-string, record it. */
3415 if ((end == charpos || (start == charpos && invis_p))
3416 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3417 && XSTRING (str)->size)
3418 RECORD_OVERLAY_STRING (overlay, str, 1);
3419 }
3420
3421 /* Process overlays after the overlay center. */
3422 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3423 {
3424 overlay = XCAR (ov);
3425 xassert (OVERLAYP (overlay));
3426 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3427 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3428
3429 if (start > charpos)
3430 break;
3431
3432 /* Skip this overlay if it doesn't start or end at IT's current
3433 position. */
3434 if (end != charpos && start != charpos)
3435 continue;
3436
3437 /* Skip this overlay if it doesn't apply to IT->w. */
3438 window = Foverlay_get (overlay, Qwindow);
3439 if (WINDOWP (window) && XWINDOW (window) != it->w)
3440 continue;
3441
3442 /* If the text ``under'' the overlay is invisible, it has a zero
3443 dimension, and both before- and after-strings apply. */
3444 invisible = Foverlay_get (overlay, Qinvisible);
3445 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3446
3447 /* If overlay has a non-empty before-string, record it. */
3448 if ((start == charpos || (end == charpos && invis_p))
3449 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3450 && XSTRING (str)->size)
3451 RECORD_OVERLAY_STRING (overlay, str, 0);
3452
3453 /* If overlay has a non-empty after-string, record it. */
3454 if ((end == charpos || (start == charpos && invis_p))
3455 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3456 && XSTRING (str)->size)
3457 RECORD_OVERLAY_STRING (overlay, str, 1);
3458 }
3459
3460 #undef RECORD_OVERLAY_STRING
3461
3462 /* Sort entries. */
3463 if (n > 1)
3464 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3465
3466 /* Record the total number of strings to process. */
3467 it->n_overlay_strings = n;
3468
3469 /* IT->current.overlay_string_index is the number of overlay strings
3470 that have already been consumed by IT. Copy some of the
3471 remaining overlay strings to IT->overlay_strings. */
3472 i = 0;
3473 j = it->current.overlay_string_index;
3474 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3475 it->overlay_strings[i++] = entries[j++].string;
3476
3477 CHECK_IT (it);
3478 }
3479
3480
3481 /* Get the first chunk of overlay strings at IT's current buffer
3482 position. Value is non-zero if at least one overlay string was
3483 found. */
3484
3485 static int
3486 get_overlay_strings (it)
3487 struct it *it;
3488 {
3489 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3490 process. This fills IT->overlay_strings with strings, and sets
3491 IT->n_overlay_strings to the total number of strings to process.
3492 IT->pos.overlay_string_index has to be set temporarily to zero
3493 because load_overlay_strings needs this; it must be set to -1
3494 when no overlay strings are found because a zero value would
3495 indicate a position in the first overlay string. */
3496 it->current.overlay_string_index = 0;
3497 load_overlay_strings (it);
3498
3499 /* If we found overlay strings, set up IT to deliver display
3500 elements from the first one. Otherwise set up IT to deliver
3501 from current_buffer. */
3502 if (it->n_overlay_strings)
3503 {
3504 /* Make sure we know settings in current_buffer, so that we can
3505 restore meaningful values when we're done with the overlay
3506 strings. */
3507 compute_stop_pos (it);
3508 xassert (it->face_id >= 0);
3509
3510 /* Save IT's settings. They are restored after all overlay
3511 strings have been processed. */
3512 xassert (it->sp == 0);
3513 push_it (it);
3514
3515 /* Set up IT to deliver display elements from the first overlay
3516 string. */
3517 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3518 it->stop_charpos = 0;
3519 it->string = it->overlay_strings[0];
3520 it->multibyte_p = STRING_MULTIBYTE (it->string);
3521 xassert (STRINGP (it->string));
3522 it->method = next_element_from_string;
3523 }
3524 else
3525 {
3526 it->string = Qnil;
3527 it->current.overlay_string_index = -1;
3528 it->method = next_element_from_buffer;
3529 }
3530
3531 CHECK_IT (it);
3532
3533 /* Value is non-zero if we found at least one overlay string. */
3534 return STRINGP (it->string);
3535 }
3536
3537
3538 \f
3539 /***********************************************************************
3540 Saving and restoring state
3541 ***********************************************************************/
3542
3543 /* Save current settings of IT on IT->stack. Called, for example,
3544 before setting up IT for an overlay string, to be able to restore
3545 IT's settings to what they were after the overlay string has been
3546 processed. */
3547
3548 static void
3549 push_it (it)
3550 struct it *it;
3551 {
3552 struct iterator_stack_entry *p;
3553
3554 xassert (it->sp < 2);
3555 p = it->stack + it->sp;
3556
3557 p->stop_charpos = it->stop_charpos;
3558 xassert (it->face_id >= 0);
3559 p->face_id = it->face_id;
3560 p->string = it->string;
3561 p->pos = it->current;
3562 p->end_charpos = it->end_charpos;
3563 p->string_nchars = it->string_nchars;
3564 p->area = it->area;
3565 p->multibyte_p = it->multibyte_p;
3566 p->space_width = it->space_width;
3567 p->font_height = it->font_height;
3568 p->voffset = it->voffset;
3569 p->string_from_display_prop_p = it->string_from_display_prop_p;
3570 ++it->sp;
3571 }
3572
3573
3574 /* Restore IT's settings from IT->stack. Called, for example, when no
3575 more overlay strings must be processed, and we return to delivering
3576 display elements from a buffer, or when the end of a string from a
3577 `display' property is reached and we return to delivering display
3578 elements from an overlay string, or from a buffer. */
3579
3580 static void
3581 pop_it (it)
3582 struct it *it;
3583 {
3584 struct iterator_stack_entry *p;
3585
3586 xassert (it->sp > 0);
3587 --it->sp;
3588 p = it->stack + it->sp;
3589 it->stop_charpos = p->stop_charpos;
3590 it->face_id = p->face_id;
3591 it->string = p->string;
3592 it->current = p->pos;
3593 it->end_charpos = p->end_charpos;
3594 it->string_nchars = p->string_nchars;
3595 it->area = p->area;
3596 it->multibyte_p = p->multibyte_p;
3597 it->space_width = p->space_width;
3598 it->font_height = p->font_height;
3599 it->voffset = p->voffset;
3600 it->string_from_display_prop_p = p->string_from_display_prop_p;
3601 }
3602
3603
3604 \f
3605 /***********************************************************************
3606 Moving over lines
3607 ***********************************************************************/
3608
3609 /* Set IT's current position to the previous line start. */
3610
3611 static void
3612 back_to_previous_line_start (it)
3613 struct it *it;
3614 {
3615 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3616 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3617 }
3618
3619
3620 /* Move IT to the next line start.
3621
3622 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3623 we skipped over part of the text (as opposed to moving the iterator
3624 continuously over the text). Otherwise, don't change the value
3625 of *SKIPPED_P.
3626
3627 Newlines may come from buffer text, overlay strings, or strings
3628 displayed via the `display' property. That's the reason we can't
3629 simply use find_next_newline_no_quit.
3630
3631 Note that this function may not skip over invisible text that is so
3632 because of text properties and immediately follows a newline. If
3633 it would, function reseat_at_next_visible_line_start, when called
3634 from set_iterator_to_next, would effectively make invisible
3635 characters following a newline part of the wrong glyph row, which
3636 leads to wrong cursor motion. */
3637
3638 static int
3639 forward_to_next_line_start (it, skipped_p)
3640 struct it *it;
3641 int *skipped_p;
3642 {
3643 int old_selective, newline_found_p, n;
3644 const int MAX_NEWLINE_DISTANCE = 500;
3645
3646 /* If already on a newline, just consume it to avoid unintended
3647 skipping over invisible text below. */
3648 if (it->what == IT_CHARACTER
3649 && it->c == '\n'
3650 && CHARPOS (it->position) == IT_CHARPOS (*it))
3651 {
3652 set_iterator_to_next (it, 0);
3653 it->c = 0;
3654 return 1;
3655 }
3656
3657 /* Don't handle selective display in the following. It's (a)
3658 unnecessary because it's done by the caller, and (b) leads to an
3659 infinite recursion because next_element_from_ellipsis indirectly
3660 calls this function. */
3661 old_selective = it->selective;
3662 it->selective = 0;
3663
3664 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3665 from buffer text. */
3666 for (n = newline_found_p = 0;
3667 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3668 n += STRINGP (it->string) ? 0 : 1)
3669 {
3670 if (!get_next_display_element (it))
3671 break;
3672 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3673 set_iterator_to_next (it, 0);
3674 }
3675
3676 /* If we didn't find a newline near enough, see if we can use a
3677 short-cut. */
3678 if (n == MAX_NEWLINE_DISTANCE)
3679 {
3680 int start = IT_CHARPOS (*it);
3681 int limit = find_next_newline_no_quit (start, 1);
3682 Lisp_Object pos;
3683
3684 xassert (!STRINGP (it->string));
3685
3686 /* If there isn't any `display' property in sight, and no
3687 overlays, we can just use the position of the newline in
3688 buffer text. */
3689 if (it->stop_charpos >= limit
3690 || ((pos = Fnext_single_property_change (make_number (start),
3691 Qdisplay,
3692 Qnil, make_number (limit)),
3693 NILP (pos))
3694 && next_overlay_change (start) == ZV))
3695 {
3696 IT_CHARPOS (*it) = limit;
3697 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3698 *skipped_p = newline_found_p = 1;
3699 }
3700 else
3701 {
3702 while (get_next_display_element (it)
3703 && !newline_found_p)
3704 {
3705 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3706 set_iterator_to_next (it, 0);
3707 }
3708 }
3709 }
3710
3711 it->selective = old_selective;
3712 return newline_found_p;
3713 }
3714
3715
3716 /* Set IT's current position to the previous visible line start. Skip
3717 invisible text that is so either due to text properties or due to
3718 selective display. Caution: this does not change IT->current_x and
3719 IT->hpos. */
3720
3721 static void
3722 back_to_previous_visible_line_start (it)
3723 struct it *it;
3724 {
3725 int visible_p = 0;
3726
3727 /* Go back one newline if not on BEGV already. */
3728 if (IT_CHARPOS (*it) > BEGV)
3729 back_to_previous_line_start (it);
3730
3731 /* Move over lines that are invisible because of selective display
3732 or text properties. */
3733 while (IT_CHARPOS (*it) > BEGV
3734 && !visible_p)
3735 {
3736 visible_p = 1;
3737
3738 /* If selective > 0, then lines indented more than that values
3739 are invisible. */
3740 if (it->selective > 0
3741 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3742 it->selective))
3743 visible_p = 0;
3744 else
3745 {
3746 Lisp_Object prop;
3747
3748 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3749 Qinvisible, it->window);
3750 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3751 visible_p = 0;
3752 }
3753
3754 /* Back one more newline if the current one is invisible. */
3755 if (!visible_p)
3756 back_to_previous_line_start (it);
3757 }
3758
3759 xassert (IT_CHARPOS (*it) >= BEGV);
3760 xassert (IT_CHARPOS (*it) == BEGV
3761 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3762 CHECK_IT (it);
3763 }
3764
3765
3766 /* Reseat iterator IT at the previous visible line start. Skip
3767 invisible text that is so either due to text properties or due to
3768 selective display. At the end, update IT's overlay information,
3769 face information etc. */
3770
3771 static void
3772 reseat_at_previous_visible_line_start (it)
3773 struct it *it;
3774 {
3775 back_to_previous_visible_line_start (it);
3776 reseat (it, it->current.pos, 1);
3777 CHECK_IT (it);
3778 }
3779
3780
3781 /* Reseat iterator IT on the next visible line start in the current
3782 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3783 preceding the line start. Skip over invisible text that is so
3784 because of selective display. Compute faces, overlays etc at the
3785 new position. Note that this function does not skip over text that
3786 is invisible because of text properties. */
3787
3788 static void
3789 reseat_at_next_visible_line_start (it, on_newline_p)
3790 struct it *it;
3791 int on_newline_p;
3792 {
3793 int newline_found_p, skipped_p = 0;
3794
3795 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3796
3797 /* Skip over lines that are invisible because they are indented
3798 more than the value of IT->selective. */
3799 if (it->selective > 0)
3800 while (IT_CHARPOS (*it) < ZV
3801 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3802 it->selective))
3803 {
3804 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3805 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3806 }
3807
3808 /* Position on the newline if that's what's requested. */
3809 if (on_newline_p && newline_found_p)
3810 {
3811 if (STRINGP (it->string))
3812 {
3813 if (IT_STRING_CHARPOS (*it) > 0)
3814 {
3815 --IT_STRING_CHARPOS (*it);
3816 --IT_STRING_BYTEPOS (*it);
3817 }
3818 }
3819 else if (IT_CHARPOS (*it) > BEGV)
3820 {
3821 --IT_CHARPOS (*it);
3822 --IT_BYTEPOS (*it);
3823 reseat (it, it->current.pos, 0);
3824 }
3825 }
3826 else if (skipped_p)
3827 reseat (it, it->current.pos, 0);
3828
3829 CHECK_IT (it);
3830 }
3831
3832
3833 \f
3834 /***********************************************************************
3835 Changing an iterator's position
3836 ***********************************************************************/
3837
3838 /* Change IT's current position to POS in current_buffer. If FORCE_P
3839 is non-zero, always check for text properties at the new position.
3840 Otherwise, text properties are only looked up if POS >=
3841 IT->check_charpos of a property. */
3842
3843 static void
3844 reseat (it, pos, force_p)
3845 struct it *it;
3846 struct text_pos pos;
3847 int force_p;
3848 {
3849 int original_pos = IT_CHARPOS (*it);
3850
3851 reseat_1 (it, pos, 0);
3852
3853 /* Determine where to check text properties. Avoid doing it
3854 where possible because text property lookup is very expensive. */
3855 if (force_p
3856 || CHARPOS (pos) > it->stop_charpos
3857 || CHARPOS (pos) < original_pos)
3858 handle_stop (it);
3859
3860 CHECK_IT (it);
3861 }
3862
3863
3864 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3865 IT->stop_pos to POS, also. */
3866
3867 static void
3868 reseat_1 (it, pos, set_stop_p)
3869 struct it *it;
3870 struct text_pos pos;
3871 int set_stop_p;
3872 {
3873 /* Don't call this function when scanning a C string. */
3874 xassert (it->s == NULL);
3875
3876 /* POS must be a reasonable value. */
3877 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3878
3879 it->current.pos = it->position = pos;
3880 XSETBUFFER (it->object, current_buffer);
3881 it->end_charpos = ZV;
3882 it->dpvec = NULL;
3883 it->current.dpvec_index = -1;
3884 it->current.overlay_string_index = -1;
3885 IT_STRING_CHARPOS (*it) = -1;
3886 IT_STRING_BYTEPOS (*it) = -1;
3887 it->string = Qnil;
3888 it->method = next_element_from_buffer;
3889 it->sp = 0;
3890 it->face_before_selective_p = 0;
3891
3892 if (set_stop_p)
3893 it->stop_charpos = CHARPOS (pos);
3894 }
3895
3896
3897 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3898 If S is non-null, it is a C string to iterate over. Otherwise,
3899 STRING gives a Lisp string to iterate over.
3900
3901 If PRECISION > 0, don't return more then PRECISION number of
3902 characters from the string.
3903
3904 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3905 characters have been returned. FIELD_WIDTH < 0 means an infinite
3906 field width.
3907
3908 MULTIBYTE = 0 means disable processing of multibyte characters,
3909 MULTIBYTE > 0 means enable it,
3910 MULTIBYTE < 0 means use IT->multibyte_p.
3911
3912 IT must be initialized via a prior call to init_iterator before
3913 calling this function. */
3914
3915 static void
3916 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3917 struct it *it;
3918 unsigned char *s;
3919 Lisp_Object string;
3920 int charpos;
3921 int precision, field_width, multibyte;
3922 {
3923 /* No region in strings. */
3924 it->region_beg_charpos = it->region_end_charpos = -1;
3925
3926 /* No text property checks performed by default, but see below. */
3927 it->stop_charpos = -1;
3928
3929 /* Set iterator position and end position. */
3930 bzero (&it->current, sizeof it->current);
3931 it->current.overlay_string_index = -1;
3932 it->current.dpvec_index = -1;
3933 xassert (charpos >= 0);
3934
3935 /* Use the setting of MULTIBYTE if specified. */
3936 if (multibyte >= 0)
3937 it->multibyte_p = multibyte > 0;
3938
3939 if (s == NULL)
3940 {
3941 xassert (STRINGP (string));
3942 it->string = string;
3943 it->s = NULL;
3944 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3945 it->method = next_element_from_string;
3946 it->current.string_pos = string_pos (charpos, string);
3947 }
3948 else
3949 {
3950 it->s = s;
3951 it->string = Qnil;
3952
3953 /* Note that we use IT->current.pos, not it->current.string_pos,
3954 for displaying C strings. */
3955 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3956 if (it->multibyte_p)
3957 {
3958 it->current.pos = c_string_pos (charpos, s, 1);
3959 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3960 }
3961 else
3962 {
3963 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3964 it->end_charpos = it->string_nchars = strlen (s);
3965 }
3966
3967 it->method = next_element_from_c_string;
3968 }
3969
3970 /* PRECISION > 0 means don't return more than PRECISION characters
3971 from the string. */
3972 if (precision > 0 && it->end_charpos - charpos > precision)
3973 it->end_charpos = it->string_nchars = charpos + precision;
3974
3975 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3976 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3977 FIELD_WIDTH < 0 means infinite field width. This is useful for
3978 padding with `-' at the end of a mode line. */
3979 if (field_width < 0)
3980 field_width = INFINITY;
3981 if (field_width > it->end_charpos - charpos)
3982 it->end_charpos = charpos + field_width;
3983
3984 /* Use the standard display table for displaying strings. */
3985 if (DISP_TABLE_P (Vstandard_display_table))
3986 it->dp = XCHAR_TABLE (Vstandard_display_table);
3987
3988 it->stop_charpos = charpos;
3989 CHECK_IT (it);
3990 }
3991
3992
3993 \f
3994 /***********************************************************************
3995 Iteration
3996 ***********************************************************************/
3997
3998 /* Load IT's display element fields with information about the next
3999 display element from the current position of IT. Value is zero if
4000 end of buffer (or C string) is reached. */
4001
4002 int
4003 get_next_display_element (it)
4004 struct it *it;
4005 {
4006 /* Non-zero means that we found an display element. Zero means that
4007 we hit the end of what we iterate over. Performance note: the
4008 function pointer `method' used here turns out to be faster than
4009 using a sequence of if-statements. */
4010 int success_p = (*it->method) (it);
4011
4012 if (it->what == IT_CHARACTER)
4013 {
4014 /* Map via display table or translate control characters.
4015 IT->c, IT->len etc. have been set to the next character by
4016 the function call above. If we have a display table, and it
4017 contains an entry for IT->c, translate it. Don't do this if
4018 IT->c itself comes from a display table, otherwise we could
4019 end up in an infinite recursion. (An alternative could be to
4020 count the recursion depth of this function and signal an
4021 error when a certain maximum depth is reached.) Is it worth
4022 it? */
4023 if (success_p && it->dpvec == NULL)
4024 {
4025 Lisp_Object dv;
4026
4027 if (it->dp
4028 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4029 VECTORP (dv)))
4030 {
4031 struct Lisp_Vector *v = XVECTOR (dv);
4032
4033 /* Return the first character from the display table
4034 entry, if not empty. If empty, don't display the
4035 current character. */
4036 if (v->size)
4037 {
4038 it->dpvec_char_len = it->len;
4039 it->dpvec = v->contents;
4040 it->dpend = v->contents + v->size;
4041 it->current.dpvec_index = 0;
4042 it->method = next_element_from_display_vector;
4043 success_p = get_next_display_element (it);
4044 }
4045 else
4046 {
4047 set_iterator_to_next (it, 0);
4048 success_p = get_next_display_element (it);
4049 }
4050 }
4051
4052 /* Translate control characters into `\003' or `^C' form.
4053 Control characters coming from a display table entry are
4054 currently not translated because we use IT->dpvec to hold
4055 the translation. This could easily be changed but I
4056 don't believe that it is worth doing.
4057
4058 Non-printable multibyte characters are also translated
4059 octal form. */
4060 else if ((it->c < ' '
4061 && (it->area != TEXT_AREA
4062 || (it->c != '\n' && it->c != '\t')))
4063 || (it->c >= 127
4064 && it->len == 1)
4065 || !CHAR_PRINTABLE_P (it->c))
4066 {
4067 /* IT->c is a control character which must be displayed
4068 either as '\003' or as `^C' where the '\\' and '^'
4069 can be defined in the display table. Fill
4070 IT->ctl_chars with glyphs for what we have to
4071 display. Then, set IT->dpvec to these glyphs. */
4072 GLYPH g;
4073
4074 if (it->c < 128 && it->ctl_arrow_p)
4075 {
4076 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4077 if (it->dp
4078 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4079 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4080 g = XINT (DISP_CTRL_GLYPH (it->dp));
4081 else
4082 g = FAST_MAKE_GLYPH ('^', 0);
4083 XSETINT (it->ctl_chars[0], g);
4084
4085 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4086 XSETINT (it->ctl_chars[1], g);
4087
4088 /* Set up IT->dpvec and return first character from it. */
4089 it->dpvec_char_len = it->len;
4090 it->dpvec = it->ctl_chars;
4091 it->dpend = it->dpvec + 2;
4092 it->current.dpvec_index = 0;
4093 it->method = next_element_from_display_vector;
4094 get_next_display_element (it);
4095 }
4096 else
4097 {
4098 unsigned char str[MAX_MULTIBYTE_LENGTH];
4099 int len;
4100 int i;
4101 GLYPH escape_glyph;
4102
4103 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4104 if (it->dp
4105 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4106 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4107 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4108 else
4109 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4110
4111 if (SINGLE_BYTE_CHAR_P (it->c))
4112 str[0] = it->c, len = 1;
4113 else
4114 len = CHAR_STRING (it->c, str);
4115
4116 for (i = 0; i < len; i++)
4117 {
4118 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4119 /* Insert three more glyphs into IT->ctl_chars for
4120 the octal display of the character. */
4121 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4122 XSETINT (it->ctl_chars[i * 4 + 1], g);
4123 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4124 XSETINT (it->ctl_chars[i * 4 + 2], g);
4125 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4126 XSETINT (it->ctl_chars[i * 4 + 3], g);
4127 }
4128
4129 /* Set up IT->dpvec and return the first character
4130 from it. */
4131 it->dpvec_char_len = it->len;
4132 it->dpvec = it->ctl_chars;
4133 it->dpend = it->dpvec + len * 4;
4134 it->current.dpvec_index = 0;
4135 it->method = next_element_from_display_vector;
4136 get_next_display_element (it);
4137 }
4138 }
4139 }
4140
4141 /* Adjust face id for a multibyte character. There are no
4142 multibyte character in unibyte text. */
4143 if (it->multibyte_p
4144 && success_p
4145 && FRAME_WINDOW_P (it->f))
4146 {
4147 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4148 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4149 }
4150 }
4151
4152 /* Is this character the last one of a run of characters with
4153 box? If yes, set IT->end_of_box_run_p to 1. */
4154 if (it->face_box_p
4155 && it->s == NULL)
4156 {
4157 int face_id;
4158 struct face *face;
4159
4160 it->end_of_box_run_p
4161 = ((face_id = face_after_it_pos (it),
4162 face_id != it->face_id)
4163 && (face = FACE_FROM_ID (it->f, face_id),
4164 face->box == FACE_NO_BOX));
4165 }
4166
4167 /* Value is 0 if end of buffer or string reached. */
4168 return success_p;
4169 }
4170
4171
4172 /* Move IT to the next display element.
4173
4174 RESEAT_P non-zero means if called on a newline in buffer text,
4175 skip to the next visible line start.
4176
4177 Functions get_next_display_element and set_iterator_to_next are
4178 separate because I find this arrangement easier to handle than a
4179 get_next_display_element function that also increments IT's
4180 position. The way it is we can first look at an iterator's current
4181 display element, decide whether it fits on a line, and if it does,
4182 increment the iterator position. The other way around we probably
4183 would either need a flag indicating whether the iterator has to be
4184 incremented the next time, or we would have to implement a
4185 decrement position function which would not be easy to write. */
4186
4187 void
4188 set_iterator_to_next (it, reseat_p)
4189 struct it *it;
4190 int reseat_p;
4191 {
4192 /* Reset flags indicating start and end of a sequence of characters
4193 with box. Reset them at the start of this function because
4194 moving the iterator to a new position might set them. */
4195 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4196
4197 if (it->method == next_element_from_buffer)
4198 {
4199 /* The current display element of IT is a character from
4200 current_buffer. Advance in the buffer, and maybe skip over
4201 invisible lines that are so because of selective display. */
4202 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4203 reseat_at_next_visible_line_start (it, 0);
4204 else
4205 {
4206 xassert (it->len != 0);
4207 IT_BYTEPOS (*it) += it->len;
4208 IT_CHARPOS (*it) += 1;
4209 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4210 }
4211 }
4212 else if (it->method == next_element_from_composition)
4213 {
4214 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4215 if (STRINGP (it->string))
4216 {
4217 IT_STRING_BYTEPOS (*it) += it->len;
4218 IT_STRING_CHARPOS (*it) += it->cmp_len;
4219 it->method = next_element_from_string;
4220 goto consider_string_end;
4221 }
4222 else
4223 {
4224 IT_BYTEPOS (*it) += it->len;
4225 IT_CHARPOS (*it) += it->cmp_len;
4226 it->method = next_element_from_buffer;
4227 }
4228 }
4229 else if (it->method == next_element_from_c_string)
4230 {
4231 /* Current display element of IT is from a C string. */
4232 IT_BYTEPOS (*it) += it->len;
4233 IT_CHARPOS (*it) += 1;
4234 }
4235 else if (it->method == next_element_from_display_vector)
4236 {
4237 /* Current display element of IT is from a display table entry.
4238 Advance in the display table definition. Reset it to null if
4239 end reached, and continue with characters from buffers/
4240 strings. */
4241 ++it->current.dpvec_index;
4242
4243 /* Restore face of the iterator to what they were before the
4244 display vector entry (these entries may contain faces). */
4245 it->face_id = it->saved_face_id;
4246
4247 if (it->dpvec + it->current.dpvec_index == it->dpend)
4248 {
4249 if (it->s)
4250 it->method = next_element_from_c_string;
4251 else if (STRINGP (it->string))
4252 it->method = next_element_from_string;
4253 else
4254 it->method = next_element_from_buffer;
4255
4256 it->dpvec = NULL;
4257 it->current.dpvec_index = -1;
4258
4259 /* Skip over characters which were displayed via IT->dpvec. */
4260 if (it->dpvec_char_len < 0)
4261 reseat_at_next_visible_line_start (it, 1);
4262 else if (it->dpvec_char_len > 0)
4263 {
4264 it->len = it->dpvec_char_len;
4265 set_iterator_to_next (it, reseat_p);
4266 }
4267 }
4268 }
4269 else if (it->method == next_element_from_string)
4270 {
4271 /* Current display element is a character from a Lisp string. */
4272 xassert (it->s == NULL && STRINGP (it->string));
4273 IT_STRING_BYTEPOS (*it) += it->len;
4274 IT_STRING_CHARPOS (*it) += 1;
4275
4276 consider_string_end:
4277
4278 if (it->current.overlay_string_index >= 0)
4279 {
4280 /* IT->string is an overlay string. Advance to the
4281 next, if there is one. */
4282 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4283 next_overlay_string (it);
4284 }
4285 else
4286 {
4287 /* IT->string is not an overlay string. If we reached
4288 its end, and there is something on IT->stack, proceed
4289 with what is on the stack. This can be either another
4290 string, this time an overlay string, or a buffer. */
4291 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4292 && it->sp > 0)
4293 {
4294 pop_it (it);
4295 if (!STRINGP (it->string))
4296 it->method = next_element_from_buffer;
4297 }
4298 }
4299 }
4300 else if (it->method == next_element_from_image
4301 || it->method == next_element_from_stretch)
4302 {
4303 /* The position etc with which we have to proceed are on
4304 the stack. The position may be at the end of a string,
4305 if the `display' property takes up the whole string. */
4306 pop_it (it);
4307 it->image_id = 0;
4308 if (STRINGP (it->string))
4309 {
4310 it->method = next_element_from_string;
4311 goto consider_string_end;
4312 }
4313 else
4314 it->method = next_element_from_buffer;
4315 }
4316 else
4317 /* There are no other methods defined, so this should be a bug. */
4318 abort ();
4319
4320 xassert (it->method != next_element_from_string
4321 || (STRINGP (it->string)
4322 && IT_STRING_CHARPOS (*it) >= 0));
4323 }
4324
4325
4326 /* Load IT's display element fields with information about the next
4327 display element which comes from a display table entry or from the
4328 result of translating a control character to one of the forms `^C'
4329 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4330
4331 static int
4332 next_element_from_display_vector (it)
4333 struct it *it;
4334 {
4335 /* Precondition. */
4336 xassert (it->dpvec && it->current.dpvec_index >= 0);
4337
4338 /* Remember the current face id in case glyphs specify faces.
4339 IT's face is restored in set_iterator_to_next. */
4340 it->saved_face_id = it->face_id;
4341
4342 if (INTEGERP (*it->dpvec)
4343 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4344 {
4345 int lface_id;
4346 GLYPH g;
4347
4348 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4349 it->c = FAST_GLYPH_CHAR (g);
4350 it->len = CHAR_BYTES (it->c);
4351
4352 /* The entry may contain a face id to use. Such a face id is
4353 the id of a Lisp face, not a realized face. A face id of
4354 zero means no face is specified. */
4355 lface_id = FAST_GLYPH_FACE (g);
4356 if (lface_id)
4357 {
4358 /* The function returns -1 if lface_id is invalid. */
4359 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4360 if (face_id >= 0)
4361 it->face_id = face_id;
4362 }
4363 }
4364 else
4365 /* Display table entry is invalid. Return a space. */
4366 it->c = ' ', it->len = 1;
4367
4368 /* Don't change position and object of the iterator here. They are
4369 still the values of the character that had this display table
4370 entry or was translated, and that's what we want. */
4371 it->what = IT_CHARACTER;
4372 return 1;
4373 }
4374
4375
4376 /* Load IT with the next display element from Lisp string IT->string.
4377 IT->current.string_pos is the current position within the string.
4378 If IT->current.overlay_string_index >= 0, the Lisp string is an
4379 overlay string. */
4380
4381 static int
4382 next_element_from_string (it)
4383 struct it *it;
4384 {
4385 struct text_pos position;
4386
4387 xassert (STRINGP (it->string));
4388 xassert (IT_STRING_CHARPOS (*it) >= 0);
4389 position = it->current.string_pos;
4390
4391 /* Time to check for invisible text? */
4392 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4393 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4394 {
4395 handle_stop (it);
4396
4397 /* Since a handler may have changed IT->method, we must
4398 recurse here. */
4399 return get_next_display_element (it);
4400 }
4401
4402 if (it->current.overlay_string_index >= 0)
4403 {
4404 /* Get the next character from an overlay string. In overlay
4405 strings, There is no field width or padding with spaces to
4406 do. */
4407 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4408 {
4409 it->what = IT_EOB;
4410 return 0;
4411 }
4412 else if (STRING_MULTIBYTE (it->string))
4413 {
4414 int remaining = (STRING_BYTES (XSTRING (it->string))
4415 - IT_STRING_BYTEPOS (*it));
4416 unsigned char *s = (XSTRING (it->string)->data
4417 + IT_STRING_BYTEPOS (*it));
4418 it->c = string_char_and_length (s, remaining, &it->len);
4419 }
4420 else
4421 {
4422 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4423 it->len = 1;
4424 }
4425 }
4426 else
4427 {
4428 /* Get the next character from a Lisp string that is not an
4429 overlay string. Such strings come from the mode line, for
4430 example. We may have to pad with spaces, or truncate the
4431 string. See also next_element_from_c_string. */
4432 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4433 {
4434 it->what = IT_EOB;
4435 return 0;
4436 }
4437 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4438 {
4439 /* Pad with spaces. */
4440 it->c = ' ', it->len = 1;
4441 CHARPOS (position) = BYTEPOS (position) = -1;
4442 }
4443 else if (STRING_MULTIBYTE (it->string))
4444 {
4445 int maxlen = (STRING_BYTES (XSTRING (it->string))
4446 - IT_STRING_BYTEPOS (*it));
4447 unsigned char *s = (XSTRING (it->string)->data
4448 + IT_STRING_BYTEPOS (*it));
4449 it->c = string_char_and_length (s, maxlen, &it->len);
4450 }
4451 else
4452 {
4453 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4454 it->len = 1;
4455 }
4456 }
4457
4458 /* Record what we have and where it came from. Note that we store a
4459 buffer position in IT->position although it could arguably be a
4460 string position. */
4461 it->what = IT_CHARACTER;
4462 it->object = it->string;
4463 it->position = position;
4464 return 1;
4465 }
4466
4467
4468 /* Load IT with next display element from C string IT->s.
4469 IT->string_nchars is the maximum number of characters to return
4470 from the string. IT->end_charpos may be greater than
4471 IT->string_nchars when this function is called, in which case we
4472 may have to return padding spaces. Value is zero if end of string
4473 reached, including padding spaces. */
4474
4475 static int
4476 next_element_from_c_string (it)
4477 struct it *it;
4478 {
4479 int success_p = 1;
4480
4481 xassert (it->s);
4482 it->what = IT_CHARACTER;
4483 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4484 it->object = Qnil;
4485
4486 /* IT's position can be greater IT->string_nchars in case a field
4487 width or precision has been specified when the iterator was
4488 initialized. */
4489 if (IT_CHARPOS (*it) >= it->end_charpos)
4490 {
4491 /* End of the game. */
4492 it->what = IT_EOB;
4493 success_p = 0;
4494 }
4495 else if (IT_CHARPOS (*it) >= it->string_nchars)
4496 {
4497 /* Pad with spaces. */
4498 it->c = ' ', it->len = 1;
4499 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4500 }
4501 else if (it->multibyte_p)
4502 {
4503 /* Implementation note: The calls to strlen apparently aren't a
4504 performance problem because there is no noticeable performance
4505 difference between Emacs running in unibyte or multibyte mode. */
4506 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4507 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4508 maxlen, &it->len);
4509 }
4510 else
4511 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4512
4513 return success_p;
4514 }
4515
4516
4517 /* Set up IT to return characters from an ellipsis, if appropriate.
4518 The definition of the ellipsis glyphs may come from a display table
4519 entry. This function Fills IT with the first glyph from the
4520 ellipsis if an ellipsis is to be displayed. */
4521
4522 static int
4523 next_element_from_ellipsis (it)
4524 struct it *it;
4525 {
4526 if (it->selective_display_ellipsis_p)
4527 {
4528 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4529 {
4530 /* Use the display table definition for `...'. Invalid glyphs
4531 will be handled by the method returning elements from dpvec. */
4532 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4533 it->dpvec_char_len = it->len;
4534 it->dpvec = v->contents;
4535 it->dpend = v->contents + v->size;
4536 it->current.dpvec_index = 0;
4537 it->method = next_element_from_display_vector;
4538 }
4539 else
4540 {
4541 /* Use default `...' which is stored in default_invis_vector. */
4542 it->dpvec_char_len = it->len;
4543 it->dpvec = default_invis_vector;
4544 it->dpend = default_invis_vector + 3;
4545 it->current.dpvec_index = 0;
4546 it->method = next_element_from_display_vector;
4547 }
4548 }
4549 else
4550 {
4551 /* The face at the current position may be different from the
4552 face we find after the invisible text. Remember what it
4553 was in IT->saved_face_id, and signal that it's there by
4554 setting face_before_selective_p. */
4555 it->saved_face_id = it->face_id;
4556 it->method = next_element_from_buffer;
4557 reseat_at_next_visible_line_start (it, 1);
4558 it->face_before_selective_p = 1;
4559 }
4560
4561 return get_next_display_element (it);
4562 }
4563
4564
4565 /* Deliver an image display element. The iterator IT is already
4566 filled with image information (done in handle_display_prop). Value
4567 is always 1. */
4568
4569
4570 static int
4571 next_element_from_image (it)
4572 struct it *it;
4573 {
4574 it->what = IT_IMAGE;
4575 return 1;
4576 }
4577
4578
4579 /* Fill iterator IT with next display element from a stretch glyph
4580 property. IT->object is the value of the text property. Value is
4581 always 1. */
4582
4583 static int
4584 next_element_from_stretch (it)
4585 struct it *it;
4586 {
4587 it->what = IT_STRETCH;
4588 return 1;
4589 }
4590
4591
4592 /* Load IT with the next display element from current_buffer. Value
4593 is zero if end of buffer reached. IT->stop_charpos is the next
4594 position at which to stop and check for text properties or buffer
4595 end. */
4596
4597 static int
4598 next_element_from_buffer (it)
4599 struct it *it;
4600 {
4601 int success_p = 1;
4602
4603 /* Check this assumption, otherwise, we would never enter the
4604 if-statement, below. */
4605 xassert (IT_CHARPOS (*it) >= BEGV
4606 && IT_CHARPOS (*it) <= it->stop_charpos);
4607
4608 if (IT_CHARPOS (*it) >= it->stop_charpos)
4609 {
4610 if (IT_CHARPOS (*it) >= it->end_charpos)
4611 {
4612 int overlay_strings_follow_p;
4613
4614 /* End of the game, except when overlay strings follow that
4615 haven't been returned yet. */
4616 if (it->overlay_strings_at_end_processed_p)
4617 overlay_strings_follow_p = 0;
4618 else
4619 {
4620 it->overlay_strings_at_end_processed_p = 1;
4621 overlay_strings_follow_p = get_overlay_strings (it);
4622 }
4623
4624 if (overlay_strings_follow_p)
4625 success_p = get_next_display_element (it);
4626 else
4627 {
4628 it->what = IT_EOB;
4629 it->position = it->current.pos;
4630 success_p = 0;
4631 }
4632 }
4633 else
4634 {
4635 handle_stop (it);
4636 return get_next_display_element (it);
4637 }
4638 }
4639 else
4640 {
4641 /* No face changes, overlays etc. in sight, so just return a
4642 character from current_buffer. */
4643 unsigned char *p;
4644
4645 /* Maybe run the redisplay end trigger hook. Performance note:
4646 This doesn't seem to cost measurable time. */
4647 if (it->redisplay_end_trigger_charpos
4648 && it->glyph_row
4649 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4650 run_redisplay_end_trigger_hook (it);
4651
4652 /* Get the next character, maybe multibyte. */
4653 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4654 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4655 {
4656 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4657 - IT_BYTEPOS (*it));
4658 it->c = string_char_and_length (p, maxlen, &it->len);
4659 }
4660 else
4661 it->c = *p, it->len = 1;
4662
4663 /* Record what we have and where it came from. */
4664 it->what = IT_CHARACTER;;
4665 it->object = it->w->buffer;
4666 it->position = it->current.pos;
4667
4668 /* Normally we return the character found above, except when we
4669 really want to return an ellipsis for selective display. */
4670 if (it->selective)
4671 {
4672 if (it->c == '\n')
4673 {
4674 /* A value of selective > 0 means hide lines indented more
4675 than that number of columns. */
4676 if (it->selective > 0
4677 && IT_CHARPOS (*it) + 1 < ZV
4678 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4679 IT_BYTEPOS (*it) + 1,
4680 it->selective))
4681 {
4682 success_p = next_element_from_ellipsis (it);
4683 it->dpvec_char_len = -1;
4684 }
4685 }
4686 else if (it->c == '\r' && it->selective == -1)
4687 {
4688 /* A value of selective == -1 means that everything from the
4689 CR to the end of the line is invisible, with maybe an
4690 ellipsis displayed for it. */
4691 success_p = next_element_from_ellipsis (it);
4692 it->dpvec_char_len = -1;
4693 }
4694 }
4695 }
4696
4697 /* Value is zero if end of buffer reached. */
4698 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4699 return success_p;
4700 }
4701
4702
4703 /* Run the redisplay end trigger hook for IT. */
4704
4705 static void
4706 run_redisplay_end_trigger_hook (it)
4707 struct it *it;
4708 {
4709 Lisp_Object args[3];
4710
4711 /* IT->glyph_row should be non-null, i.e. we should be actually
4712 displaying something, or otherwise we should not run the hook. */
4713 xassert (it->glyph_row);
4714
4715 /* Set up hook arguments. */
4716 args[0] = Qredisplay_end_trigger_functions;
4717 args[1] = it->window;
4718 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4719 it->redisplay_end_trigger_charpos = 0;
4720
4721 /* Since we are *trying* to run these functions, don't try to run
4722 them again, even if they get an error. */
4723 it->w->redisplay_end_trigger = Qnil;
4724 Frun_hook_with_args (3, args);
4725
4726 /* Notice if it changed the face of the character we are on. */
4727 handle_face_prop (it);
4728 }
4729
4730
4731 /* Deliver a composition display element. The iterator IT is already
4732 filled with composition information (done in
4733 handle_composition_prop). Value is always 1. */
4734
4735 static int
4736 next_element_from_composition (it)
4737 struct it *it;
4738 {
4739 it->what = IT_COMPOSITION;
4740 it->position = (STRINGP (it->string)
4741 ? it->current.string_pos
4742 : it->current.pos);
4743 return 1;
4744 }
4745
4746
4747 \f
4748 /***********************************************************************
4749 Moving an iterator without producing glyphs
4750 ***********************************************************************/
4751
4752 /* Move iterator IT to a specified buffer or X position within one
4753 line on the display without producing glyphs.
4754
4755 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4756 whichever is reached first.
4757
4758 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4759
4760 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4761 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4762 TO_X includes the amount by which a window is horizontally
4763 scrolled.
4764
4765 Value is
4766
4767 MOVE_POS_MATCH_OR_ZV
4768 - when TO_POS or ZV was reached.
4769
4770 MOVE_X_REACHED
4771 -when TO_X was reached before TO_POS or ZV were reached.
4772
4773 MOVE_LINE_CONTINUED
4774 - when we reached the end of the display area and the line must
4775 be continued.
4776
4777 MOVE_LINE_TRUNCATED
4778 - when we reached the end of the display area and the line is
4779 truncated.
4780
4781 MOVE_NEWLINE_OR_CR
4782 - when we stopped at a line end, i.e. a newline or a CR and selective
4783 display is on. */
4784
4785 static enum move_it_result
4786 move_it_in_display_line_to (it, to_charpos, to_x, op)
4787 struct it *it;
4788 int to_charpos, to_x, op;
4789 {
4790 enum move_it_result result = MOVE_UNDEFINED;
4791 struct glyph_row *saved_glyph_row;
4792
4793 /* Don't produce glyphs in produce_glyphs. */
4794 saved_glyph_row = it->glyph_row;
4795 it->glyph_row = NULL;
4796
4797 while (1)
4798 {
4799 int x, i, ascent = 0, descent = 0;
4800
4801 /* Stop when ZV or TO_CHARPOS reached. */
4802 if (!get_next_display_element (it)
4803 || ((op & MOVE_TO_POS) != 0
4804 && BUFFERP (it->object)
4805 && IT_CHARPOS (*it) >= to_charpos))
4806 {
4807 result = MOVE_POS_MATCH_OR_ZV;
4808 break;
4809 }
4810
4811 /* The call to produce_glyphs will get the metrics of the
4812 display element IT is loaded with. We record in x the
4813 x-position before this display element in case it does not
4814 fit on the line. */
4815 x = it->current_x;
4816
4817 /* Remember the line height so far in case the next element doesn't
4818 fit on the line. */
4819 if (!it->truncate_lines_p)
4820 {
4821 ascent = it->max_ascent;
4822 descent = it->max_descent;
4823 }
4824
4825 PRODUCE_GLYPHS (it);
4826
4827 if (it->area != TEXT_AREA)
4828 {
4829 set_iterator_to_next (it, 1);
4830 continue;
4831 }
4832
4833 /* The number of glyphs we get back in IT->nglyphs will normally
4834 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4835 character on a terminal frame, or (iii) a line end. For the
4836 second case, IT->nglyphs - 1 padding glyphs will be present
4837 (on X frames, there is only one glyph produced for a
4838 composite character.
4839
4840 The behavior implemented below means, for continuation lines,
4841 that as many spaces of a TAB as fit on the current line are
4842 displayed there. For terminal frames, as many glyphs of a
4843 multi-glyph character are displayed in the current line, too.
4844 This is what the old redisplay code did, and we keep it that
4845 way. Under X, the whole shape of a complex character must
4846 fit on the line or it will be completely displayed in the
4847 next line.
4848
4849 Note that both for tabs and padding glyphs, all glyphs have
4850 the same width. */
4851 if (it->nglyphs)
4852 {
4853 /* More than one glyph or glyph doesn't fit on line. All
4854 glyphs have the same width. */
4855 int single_glyph_width = it->pixel_width / it->nglyphs;
4856 int new_x;
4857
4858 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4859 {
4860 new_x = x + single_glyph_width;
4861
4862 /* We want to leave anything reaching TO_X to the caller. */
4863 if ((op & MOVE_TO_X) && new_x > to_x)
4864 {
4865 it->current_x = x;
4866 result = MOVE_X_REACHED;
4867 break;
4868 }
4869 else if (/* Lines are continued. */
4870 !it->truncate_lines_p
4871 && (/* And glyph doesn't fit on the line. */
4872 new_x > it->last_visible_x
4873 /* Or it fits exactly and we're on a window
4874 system frame. */
4875 || (new_x == it->last_visible_x
4876 && FRAME_WINDOW_P (it->f))))
4877 {
4878 if (/* IT->hpos == 0 means the very first glyph
4879 doesn't fit on the line, e.g. a wide image. */
4880 it->hpos == 0
4881 || (new_x == it->last_visible_x
4882 && FRAME_WINDOW_P (it->f)))
4883 {
4884 ++it->hpos;
4885 it->current_x = new_x;
4886 if (i == it->nglyphs - 1)
4887 set_iterator_to_next (it, 1);
4888 }
4889 else
4890 {
4891 it->current_x = x;
4892 it->max_ascent = ascent;
4893 it->max_descent = descent;
4894 }
4895
4896 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4897 IT_CHARPOS (*it)));
4898 result = MOVE_LINE_CONTINUED;
4899 break;
4900 }
4901 else if (new_x > it->first_visible_x)
4902 {
4903 /* Glyph is visible. Increment number of glyphs that
4904 would be displayed. */
4905 ++it->hpos;
4906 }
4907 else
4908 {
4909 /* Glyph is completely off the left margin of the display
4910 area. Nothing to do. */
4911 }
4912 }
4913
4914 if (result != MOVE_UNDEFINED)
4915 break;
4916 }
4917 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4918 {
4919 /* Stop when TO_X specified and reached. This check is
4920 necessary here because of lines consisting of a line end,
4921 only. The line end will not produce any glyphs and we
4922 would never get MOVE_X_REACHED. */
4923 xassert (it->nglyphs == 0);
4924 result = MOVE_X_REACHED;
4925 break;
4926 }
4927
4928 /* Is this a line end? If yes, we're done. */
4929 if (ITERATOR_AT_END_OF_LINE_P (it))
4930 {
4931 result = MOVE_NEWLINE_OR_CR;
4932 break;
4933 }
4934
4935 /* The current display element has been consumed. Advance
4936 to the next. */
4937 set_iterator_to_next (it, 1);
4938
4939 /* Stop if lines are truncated and IT's current x-position is
4940 past the right edge of the window now. */
4941 if (it->truncate_lines_p
4942 && it->current_x >= it->last_visible_x)
4943 {
4944 result = MOVE_LINE_TRUNCATED;
4945 break;
4946 }
4947 }
4948
4949 /* Restore the iterator settings altered at the beginning of this
4950 function. */
4951 it->glyph_row = saved_glyph_row;
4952 return result;
4953 }
4954
4955
4956 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4957 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4958 the description of enum move_operation_enum.
4959
4960 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4961 screen line, this function will set IT to the next position >
4962 TO_CHARPOS. */
4963
4964 void
4965 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4966 struct it *it;
4967 int to_charpos, to_x, to_y, to_vpos;
4968 int op;
4969 {
4970 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4971 int line_height;
4972 int reached = 0;
4973
4974 for (;;)
4975 {
4976 if (op & MOVE_TO_VPOS)
4977 {
4978 /* If no TO_CHARPOS and no TO_X specified, stop at the
4979 start of the line TO_VPOS. */
4980 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4981 {
4982 if (it->vpos == to_vpos)
4983 {
4984 reached = 1;
4985 break;
4986 }
4987 else
4988 skip = move_it_in_display_line_to (it, -1, -1, 0);
4989 }
4990 else
4991 {
4992 /* TO_VPOS >= 0 means stop at TO_X in the line at
4993 TO_VPOS, or at TO_POS, whichever comes first. */
4994 if (it->vpos == to_vpos)
4995 {
4996 reached = 2;
4997 break;
4998 }
4999
5000 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5001
5002 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5003 {
5004 reached = 3;
5005 break;
5006 }
5007 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5008 {
5009 /* We have reached TO_X but not in the line we want. */
5010 skip = move_it_in_display_line_to (it, to_charpos,
5011 -1, MOVE_TO_POS);
5012 if (skip == MOVE_POS_MATCH_OR_ZV)
5013 {
5014 reached = 4;
5015 break;
5016 }
5017 }
5018 }
5019 }
5020 else if (op & MOVE_TO_Y)
5021 {
5022 struct it it_backup;
5023
5024 /* TO_Y specified means stop at TO_X in the line containing
5025 TO_Y---or at TO_CHARPOS if this is reached first. The
5026 problem is that we can't really tell whether the line
5027 contains TO_Y before we have completely scanned it, and
5028 this may skip past TO_X. What we do is to first scan to
5029 TO_X.
5030
5031 If TO_X is not specified, use a TO_X of zero. The reason
5032 is to make the outcome of this function more predictable.
5033 If we didn't use TO_X == 0, we would stop at the end of
5034 the line which is probably not what a caller would expect
5035 to happen. */
5036 skip = move_it_in_display_line_to (it, to_charpos,
5037 ((op & MOVE_TO_X)
5038 ? to_x : 0),
5039 (MOVE_TO_X
5040 | (op & MOVE_TO_POS)));
5041
5042 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5043 if (skip == MOVE_POS_MATCH_OR_ZV)
5044 {
5045 reached = 5;
5046 break;
5047 }
5048
5049 /* If TO_X was reached, we would like to know whether TO_Y
5050 is in the line. This can only be said if we know the
5051 total line height which requires us to scan the rest of
5052 the line. */
5053 if (skip == MOVE_X_REACHED)
5054 {
5055 it_backup = *it;
5056 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5057 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5058 op & MOVE_TO_POS);
5059 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5060 }
5061
5062 /* Now, decide whether TO_Y is in this line. */
5063 line_height = it->max_ascent + it->max_descent;
5064 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5065
5066 if (to_y >= it->current_y
5067 && to_y < it->current_y + line_height)
5068 {
5069 if (skip == MOVE_X_REACHED)
5070 /* If TO_Y is in this line and TO_X was reached above,
5071 we scanned too far. We have to restore IT's settings
5072 to the ones before skipping. */
5073 *it = it_backup;
5074 reached = 6;
5075 }
5076 else if (skip == MOVE_X_REACHED)
5077 {
5078 skip = skip2;
5079 if (skip == MOVE_POS_MATCH_OR_ZV)
5080 reached = 7;
5081 }
5082
5083 if (reached)
5084 break;
5085 }
5086 else
5087 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5088
5089 switch (skip)
5090 {
5091 case MOVE_POS_MATCH_OR_ZV:
5092 reached = 8;
5093 goto out;
5094
5095 case MOVE_NEWLINE_OR_CR:
5096 set_iterator_to_next (it, 1);
5097 it->continuation_lines_width = 0;
5098 break;
5099
5100 case MOVE_LINE_TRUNCATED:
5101 it->continuation_lines_width = 0;
5102 reseat_at_next_visible_line_start (it, 0);
5103 if ((op & MOVE_TO_POS) != 0
5104 && IT_CHARPOS (*it) > to_charpos)
5105 {
5106 reached = 9;
5107 goto out;
5108 }
5109 break;
5110
5111 case MOVE_LINE_CONTINUED:
5112 it->continuation_lines_width += it->current_x;
5113 break;
5114
5115 default:
5116 abort ();
5117 }
5118
5119 /* Reset/increment for the next run. */
5120 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5121 it->current_x = it->hpos = 0;
5122 it->current_y += it->max_ascent + it->max_descent;
5123 ++it->vpos;
5124 last_height = it->max_ascent + it->max_descent;
5125 last_max_ascent = it->max_ascent;
5126 it->max_ascent = it->max_descent = 0;
5127 }
5128
5129 out:
5130
5131 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5132 }
5133
5134
5135 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5136
5137 If DY > 0, move IT backward at least that many pixels. DY = 0
5138 means move IT backward to the preceding line start or BEGV. This
5139 function may move over more than DY pixels if IT->current_y - DY
5140 ends up in the middle of a line; in this case IT->current_y will be
5141 set to the top of the line moved to. */
5142
5143 void
5144 move_it_vertically_backward (it, dy)
5145 struct it *it;
5146 int dy;
5147 {
5148 int nlines, h, line_height;
5149 struct it it2;
5150 int start_pos = IT_CHARPOS (*it);
5151
5152 xassert (dy >= 0);
5153
5154 /* Estimate how many newlines we must move back. */
5155 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5156
5157 /* Set the iterator's position that many lines back. */
5158 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5159 back_to_previous_visible_line_start (it);
5160
5161 /* Reseat the iterator here. When moving backward, we don't want
5162 reseat to skip forward over invisible text, set up the iterator
5163 to deliver from overlay strings at the new position etc. So,
5164 use reseat_1 here. */
5165 reseat_1 (it, it->current.pos, 1);
5166
5167 /* We are now surely at a line start. */
5168 it->current_x = it->hpos = 0;
5169
5170 /* Move forward and see what y-distance we moved. First move to the
5171 start of the next line so that we get its height. We need this
5172 height to be able to tell whether we reached the specified
5173 y-distance. */
5174 it2 = *it;
5175 it2.max_ascent = it2.max_descent = 0;
5176 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5177 MOVE_TO_POS | MOVE_TO_VPOS);
5178 xassert (IT_CHARPOS (*it) >= BEGV);
5179 line_height = it2.max_ascent + it2.max_descent;
5180
5181 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5182 xassert (IT_CHARPOS (*it) >= BEGV);
5183 h = it2.current_y - it->current_y;
5184 nlines = it2.vpos - it->vpos;
5185
5186 /* Correct IT's y and vpos position. */
5187 it->vpos -= nlines;
5188 it->current_y -= h;
5189
5190 if (dy == 0)
5191 {
5192 /* DY == 0 means move to the start of the screen line. The
5193 value of nlines is > 0 if continuation lines were involved. */
5194 if (nlines > 0)
5195 move_it_by_lines (it, nlines, 1);
5196 xassert (IT_CHARPOS (*it) <= start_pos);
5197 }
5198 else if (nlines)
5199 {
5200 /* The y-position we try to reach. Note that h has been
5201 subtracted in front of the if-statement. */
5202 int target_y = it->current_y + h - dy;
5203
5204 /* If we did not reach target_y, try to move further backward if
5205 we can. If we moved too far backward, try to move forward. */
5206 if (target_y < it->current_y
5207 && IT_CHARPOS (*it) > BEGV)
5208 {
5209 move_it_vertically (it, target_y - it->current_y);
5210 xassert (IT_CHARPOS (*it) >= BEGV);
5211 }
5212 else if (target_y >= it->current_y + line_height
5213 && IT_CHARPOS (*it) < ZV)
5214 {
5215 move_it_vertically (it, target_y - (it->current_y + line_height));
5216 xassert (IT_CHARPOS (*it) >= BEGV);
5217 }
5218 }
5219 }
5220
5221
5222 /* Move IT by a specified amount of pixel lines DY. DY negative means
5223 move backwards. DY = 0 means move to start of screen line. At the
5224 end, IT will be on the start of a screen line. */
5225
5226 void
5227 move_it_vertically (it, dy)
5228 struct it *it;
5229 int dy;
5230 {
5231 if (dy <= 0)
5232 move_it_vertically_backward (it, -dy);
5233 else if (dy > 0)
5234 {
5235 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5236 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5237 MOVE_TO_POS | MOVE_TO_Y);
5238 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5239
5240 /* If buffer ends in ZV without a newline, move to the start of
5241 the line to satisfy the post-condition. */
5242 if (IT_CHARPOS (*it) == ZV
5243 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5244 move_it_by_lines (it, 0, 0);
5245 }
5246 }
5247
5248
5249 /* Move iterator IT past the end of the text line it is in. */
5250
5251 void
5252 move_it_past_eol (it)
5253 struct it *it;
5254 {
5255 enum move_it_result rc;
5256
5257 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5258 if (rc == MOVE_NEWLINE_OR_CR)
5259 set_iterator_to_next (it, 0);
5260 }
5261
5262
5263 #if 0 /* Currently not used. */
5264
5265 /* Return non-zero if some text between buffer positions START_CHARPOS
5266 and END_CHARPOS is invisible. IT->window is the window for text
5267 property lookup. */
5268
5269 static int
5270 invisible_text_between_p (it, start_charpos, end_charpos)
5271 struct it *it;
5272 int start_charpos, end_charpos;
5273 {
5274 Lisp_Object prop, limit;
5275 int invisible_found_p;
5276
5277 xassert (it != NULL && start_charpos <= end_charpos);
5278
5279 /* Is text at START invisible? */
5280 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5281 it->window);
5282 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5283 invisible_found_p = 1;
5284 else
5285 {
5286 limit = Fnext_single_char_property_change (make_number (start_charpos),
5287 Qinvisible, Qnil,
5288 make_number (end_charpos));
5289 invisible_found_p = XFASTINT (limit) < end_charpos;
5290 }
5291
5292 return invisible_found_p;
5293 }
5294
5295 #endif /* 0 */
5296
5297
5298 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5299 negative means move up. DVPOS == 0 means move to the start of the
5300 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5301 NEED_Y_P is zero, IT->current_y will be left unchanged.
5302
5303 Further optimization ideas: If we would know that IT->f doesn't use
5304 a face with proportional font, we could be faster for
5305 truncate-lines nil. */
5306
5307 void
5308 move_it_by_lines (it, dvpos, need_y_p)
5309 struct it *it;
5310 int dvpos, need_y_p;
5311 {
5312 struct position pos;
5313
5314 if (!FRAME_WINDOW_P (it->f))
5315 {
5316 struct text_pos textpos;
5317
5318 /* We can use vmotion on frames without proportional fonts. */
5319 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5320 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5321 reseat (it, textpos, 1);
5322 it->vpos += pos.vpos;
5323 it->current_y += pos.vpos;
5324 }
5325 else if (dvpos == 0)
5326 {
5327 /* DVPOS == 0 means move to the start of the screen line. */
5328 move_it_vertically_backward (it, 0);
5329 xassert (it->current_x == 0 && it->hpos == 0);
5330 }
5331 else if (dvpos > 0)
5332 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5333 else
5334 {
5335 struct it it2;
5336 int start_charpos, i;
5337
5338 /* Go back -DVPOS visible lines and reseat the iterator there. */
5339 start_charpos = IT_CHARPOS (*it);
5340 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5341 back_to_previous_visible_line_start (it);
5342 reseat (it, it->current.pos, 1);
5343 it->current_x = it->hpos = 0;
5344
5345 /* Above call may have moved too far if continuation lines
5346 are involved. Scan forward and see if it did. */
5347 it2 = *it;
5348 it2.vpos = it2.current_y = 0;
5349 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5350 it->vpos -= it2.vpos;
5351 it->current_y -= it2.current_y;
5352 it->current_x = it->hpos = 0;
5353
5354 /* If we moved too far, move IT some lines forward. */
5355 if (it2.vpos > -dvpos)
5356 {
5357 int delta = it2.vpos + dvpos;
5358 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5359 }
5360 }
5361 }
5362
5363
5364 \f
5365 /***********************************************************************
5366 Messages
5367 ***********************************************************************/
5368
5369
5370 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5371 to *Messages*. */
5372
5373 void
5374 add_to_log (format, arg1, arg2)
5375 char *format;
5376 Lisp_Object arg1, arg2;
5377 {
5378 Lisp_Object args[3];
5379 Lisp_Object msg, fmt;
5380 char *buffer;
5381 int len;
5382 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5383
5384 fmt = msg = Qnil;
5385 GCPRO4 (fmt, msg, arg1, arg2);
5386
5387 args[0] = fmt = build_string (format);
5388 args[1] = arg1;
5389 args[2] = arg2;
5390 msg = Fformat (3, args);
5391
5392 len = STRING_BYTES (XSTRING (msg)) + 1;
5393 buffer = (char *) alloca (len);
5394 strcpy (buffer, XSTRING (msg)->data);
5395
5396 message_dolog (buffer, len - 1, 1, 0);
5397 UNGCPRO;
5398 }
5399
5400
5401 /* Output a newline in the *Messages* buffer if "needs" one. */
5402
5403 void
5404 message_log_maybe_newline ()
5405 {
5406 if (message_log_need_newline)
5407 message_dolog ("", 0, 1, 0);
5408 }
5409
5410
5411 /* Add a string M of length NBYTES to the message log, optionally
5412 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5413 nonzero, means interpret the contents of M as multibyte. This
5414 function calls low-level routines in order to bypass text property
5415 hooks, etc. which might not be safe to run. */
5416
5417 void
5418 message_dolog (m, nbytes, nlflag, multibyte)
5419 char *m;
5420 int nbytes, nlflag, multibyte;
5421 {
5422 if (!NILP (Vmessage_log_max))
5423 {
5424 struct buffer *oldbuf;
5425 Lisp_Object oldpoint, oldbegv, oldzv;
5426 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5427 int point_at_end = 0;
5428 int zv_at_end = 0;
5429 Lisp_Object old_deactivate_mark, tem;
5430 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5431
5432 old_deactivate_mark = Vdeactivate_mark;
5433 oldbuf = current_buffer;
5434 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5435 current_buffer->undo_list = Qt;
5436
5437 oldpoint = Fpoint_marker ();
5438 oldbegv = Fpoint_min_marker ();
5439 oldzv = Fpoint_max_marker ();
5440 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5441
5442 if (PT == Z)
5443 point_at_end = 1;
5444 if (ZV == Z)
5445 zv_at_end = 1;
5446
5447 BEGV = BEG;
5448 BEGV_BYTE = BEG_BYTE;
5449 ZV = Z;
5450 ZV_BYTE = Z_BYTE;
5451 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5452
5453 /* Insert the string--maybe converting multibyte to single byte
5454 or vice versa, so that all the text fits the buffer. */
5455 if (multibyte
5456 && NILP (current_buffer->enable_multibyte_characters))
5457 {
5458 int i, c, char_bytes;
5459 unsigned char work[1];
5460
5461 /* Convert a multibyte string to single-byte
5462 for the *Message* buffer. */
5463 for (i = 0; i < nbytes; i += nbytes)
5464 {
5465 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5466 work[0] = (SINGLE_BYTE_CHAR_P (c)
5467 ? c
5468 : multibyte_char_to_unibyte (c, Qnil));
5469 insert_1_both (work, 1, 1, 1, 0, 0);
5470 }
5471 }
5472 else if (! multibyte
5473 && ! NILP (current_buffer->enable_multibyte_characters))
5474 {
5475 int i, c, char_bytes;
5476 unsigned char *msg = (unsigned char *) m;
5477 unsigned char str[MAX_MULTIBYTE_LENGTH];
5478 /* Convert a single-byte string to multibyte
5479 for the *Message* buffer. */
5480 for (i = 0; i < nbytes; i++)
5481 {
5482 c = unibyte_char_to_multibyte (msg[i]);
5483 char_bytes = CHAR_STRING (c, str);
5484 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5485 }
5486 }
5487 else if (nbytes)
5488 insert_1 (m, nbytes, 1, 0, 0);
5489
5490 if (nlflag)
5491 {
5492 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5493 insert_1 ("\n", 1, 1, 0, 0);
5494
5495 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5496 this_bol = PT;
5497 this_bol_byte = PT_BYTE;
5498
5499 if (this_bol > BEG)
5500 {
5501 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5502 prev_bol = PT;
5503 prev_bol_byte = PT_BYTE;
5504
5505 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5506 this_bol, this_bol_byte);
5507 if (dup)
5508 {
5509 del_range_both (prev_bol, prev_bol_byte,
5510 this_bol, this_bol_byte, 0);
5511 if (dup > 1)
5512 {
5513 char dupstr[40];
5514 int duplen;
5515
5516 /* If you change this format, don't forget to also
5517 change message_log_check_duplicate. */
5518 sprintf (dupstr, " [%d times]", dup);
5519 duplen = strlen (dupstr);
5520 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5521 insert_1 (dupstr, duplen, 1, 0, 1);
5522 }
5523 }
5524 }
5525
5526 if (NATNUMP (Vmessage_log_max))
5527 {
5528 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5529 -XFASTINT (Vmessage_log_max) - 1, 0);
5530 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5531 }
5532 }
5533 BEGV = XMARKER (oldbegv)->charpos;
5534 BEGV_BYTE = marker_byte_position (oldbegv);
5535
5536 if (zv_at_end)
5537 {
5538 ZV = Z;
5539 ZV_BYTE = Z_BYTE;
5540 }
5541 else
5542 {
5543 ZV = XMARKER (oldzv)->charpos;
5544 ZV_BYTE = marker_byte_position (oldzv);
5545 }
5546
5547 if (point_at_end)
5548 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5549 else
5550 /* We can't do Fgoto_char (oldpoint) because it will run some
5551 Lisp code. */
5552 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5553 XMARKER (oldpoint)->bytepos);
5554
5555 UNGCPRO;
5556 free_marker (oldpoint);
5557 free_marker (oldbegv);
5558 free_marker (oldzv);
5559
5560 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5561 set_buffer_internal (oldbuf);
5562 if (NILP (tem))
5563 windows_or_buffers_changed = old_windows_or_buffers_changed;
5564 message_log_need_newline = !nlflag;
5565 Vdeactivate_mark = old_deactivate_mark;
5566 }
5567 }
5568
5569
5570 /* We are at the end of the buffer after just having inserted a newline.
5571 (Note: We depend on the fact we won't be crossing the gap.)
5572 Check to see if the most recent message looks a lot like the previous one.
5573 Return 0 if different, 1 if the new one should just replace it, or a
5574 value N > 1 if we should also append " [N times]". */
5575
5576 static int
5577 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5578 int prev_bol, this_bol;
5579 int prev_bol_byte, this_bol_byte;
5580 {
5581 int i;
5582 int len = Z_BYTE - 1 - this_bol_byte;
5583 int seen_dots = 0;
5584 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5585 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5586
5587 for (i = 0; i < len; i++)
5588 {
5589 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5590 seen_dots = 1;
5591 if (p1[i] != p2[i])
5592 return seen_dots;
5593 }
5594 p1 += len;
5595 if (*p1 == '\n')
5596 return 2;
5597 if (*p1++ == ' ' && *p1++ == '[')
5598 {
5599 int n = 0;
5600 while (*p1 >= '0' && *p1 <= '9')
5601 n = n * 10 + *p1++ - '0';
5602 if (strncmp (p1, " times]\n", 8) == 0)
5603 return n+1;
5604 }
5605 return 0;
5606 }
5607
5608
5609 /* Display an echo area message M with a specified length of NBYTES
5610 bytes. The string may include null characters. If M is 0, clear
5611 out any existing message, and let the mini-buffer text show
5612 through.
5613
5614 The buffer M must continue to exist until after the echo area gets
5615 cleared or some other message gets displayed there. This means do
5616 not pass text that is stored in a Lisp string; do not pass text in
5617 a buffer that was alloca'd. */
5618
5619 void
5620 message2 (m, nbytes, multibyte)
5621 char *m;
5622 int nbytes;
5623 int multibyte;
5624 {
5625 /* First flush out any partial line written with print. */
5626 message_log_maybe_newline ();
5627 if (m)
5628 message_dolog (m, nbytes, 1, multibyte);
5629 message2_nolog (m, nbytes, multibyte);
5630 }
5631
5632
5633 /* The non-logging counterpart of message2. */
5634
5635 void
5636 message2_nolog (m, nbytes, multibyte)
5637 char *m;
5638 int nbytes;
5639 {
5640 struct frame *sf = SELECTED_FRAME ();
5641 message_enable_multibyte = multibyte;
5642
5643 if (noninteractive)
5644 {
5645 if (noninteractive_need_newline)
5646 putc ('\n', stderr);
5647 noninteractive_need_newline = 0;
5648 if (m)
5649 fwrite (m, nbytes, 1, stderr);
5650 if (cursor_in_echo_area == 0)
5651 fprintf (stderr, "\n");
5652 fflush (stderr);
5653 }
5654 /* A null message buffer means that the frame hasn't really been
5655 initialized yet. Error messages get reported properly by
5656 cmd_error, so this must be just an informative message; toss it. */
5657 else if (INTERACTIVE
5658 && sf->glyphs_initialized_p
5659 && FRAME_MESSAGE_BUF (sf))
5660 {
5661 Lisp_Object mini_window;
5662 struct frame *f;
5663
5664 /* Get the frame containing the mini-buffer
5665 that the selected frame is using. */
5666 mini_window = FRAME_MINIBUF_WINDOW (sf);
5667 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5668
5669 FRAME_SAMPLE_VISIBILITY (f);
5670 if (FRAME_VISIBLE_P (sf)
5671 && ! FRAME_VISIBLE_P (f))
5672 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5673
5674 if (m)
5675 {
5676 set_message (m, Qnil, nbytes, multibyte);
5677 if (minibuffer_auto_raise)
5678 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5679 }
5680 else
5681 clear_message (1, 1);
5682
5683 do_pending_window_change (0);
5684 echo_area_display (1);
5685 do_pending_window_change (0);
5686 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5687 (*frame_up_to_date_hook) (f);
5688 }
5689 }
5690
5691
5692 /* Display an echo area message M with a specified length of NBYTES
5693 bytes. The string may include null characters. If M is not a
5694 string, clear out any existing message, and let the mini-buffer
5695 text show through. */
5696
5697 void
5698 message3 (m, nbytes, multibyte)
5699 Lisp_Object m;
5700 int nbytes;
5701 int multibyte;
5702 {
5703 struct gcpro gcpro1;
5704
5705 GCPRO1 (m);
5706
5707 /* First flush out any partial line written with print. */
5708 message_log_maybe_newline ();
5709 if (STRINGP (m))
5710 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5711 message3_nolog (m, nbytes, multibyte);
5712
5713 UNGCPRO;
5714 }
5715
5716
5717 /* The non-logging version of message3. */
5718
5719 void
5720 message3_nolog (m, nbytes, multibyte)
5721 Lisp_Object m;
5722 int nbytes, multibyte;
5723 {
5724 struct frame *sf = SELECTED_FRAME ();
5725 message_enable_multibyte = multibyte;
5726
5727 if (noninteractive)
5728 {
5729 if (noninteractive_need_newline)
5730 putc ('\n', stderr);
5731 noninteractive_need_newline = 0;
5732 if (STRINGP (m))
5733 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5734 if (cursor_in_echo_area == 0)
5735 fprintf (stderr, "\n");
5736 fflush (stderr);
5737 }
5738 /* A null message buffer means that the frame hasn't really been
5739 initialized yet. Error messages get reported properly by
5740 cmd_error, so this must be just an informative message; toss it. */
5741 else if (INTERACTIVE
5742 && sf->glyphs_initialized_p
5743 && FRAME_MESSAGE_BUF (sf))
5744 {
5745 Lisp_Object mini_window;
5746 Lisp_Object frame;
5747 struct frame *f;
5748
5749 /* Get the frame containing the mini-buffer
5750 that the selected frame is using. */
5751 mini_window = FRAME_MINIBUF_WINDOW (sf);
5752 frame = XWINDOW (mini_window)->frame;
5753 f = XFRAME (frame);
5754
5755 FRAME_SAMPLE_VISIBILITY (f);
5756 if (FRAME_VISIBLE_P (sf)
5757 && !FRAME_VISIBLE_P (f))
5758 Fmake_frame_visible (frame);
5759
5760 if (STRINGP (m) && XSTRING (m)->size)
5761 {
5762 set_message (NULL, m, nbytes, multibyte);
5763 if (minibuffer_auto_raise)
5764 Fraise_frame (frame);
5765 }
5766 else
5767 clear_message (1, 1);
5768
5769 do_pending_window_change (0);
5770 echo_area_display (1);
5771 do_pending_window_change (0);
5772 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5773 (*frame_up_to_date_hook) (f);
5774 }
5775 }
5776
5777
5778 /* Display a null-terminated echo area message M. If M is 0, clear
5779 out any existing message, and let the mini-buffer text show through.
5780
5781 The buffer M must continue to exist until after the echo area gets
5782 cleared or some other message gets displayed there. Do not pass
5783 text that is stored in a Lisp string. Do not pass text in a buffer
5784 that was alloca'd. */
5785
5786 void
5787 message1 (m)
5788 char *m;
5789 {
5790 message2 (m, (m ? strlen (m) : 0), 0);
5791 }
5792
5793
5794 /* The non-logging counterpart of message1. */
5795
5796 void
5797 message1_nolog (m)
5798 char *m;
5799 {
5800 message2_nolog (m, (m ? strlen (m) : 0), 0);
5801 }
5802
5803 /* Display a message M which contains a single %s
5804 which gets replaced with STRING. */
5805
5806 void
5807 message_with_string (m, string, log)
5808 char *m;
5809 Lisp_Object string;
5810 int log;
5811 {
5812 if (noninteractive)
5813 {
5814 if (m)
5815 {
5816 if (noninteractive_need_newline)
5817 putc ('\n', stderr);
5818 noninteractive_need_newline = 0;
5819 fprintf (stderr, m, XSTRING (string)->data);
5820 if (cursor_in_echo_area == 0)
5821 fprintf (stderr, "\n");
5822 fflush (stderr);
5823 }
5824 }
5825 else if (INTERACTIVE)
5826 {
5827 /* The frame whose minibuffer we're going to display the message on.
5828 It may be larger than the selected frame, so we need
5829 to use its buffer, not the selected frame's buffer. */
5830 Lisp_Object mini_window;
5831 struct frame *f, *sf = SELECTED_FRAME ();
5832
5833 /* Get the frame containing the minibuffer
5834 that the selected frame is using. */
5835 mini_window = FRAME_MINIBUF_WINDOW (sf);
5836 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5837
5838 /* A null message buffer means that the frame hasn't really been
5839 initialized yet. Error messages get reported properly by
5840 cmd_error, so this must be just an informative message; toss it. */
5841 if (FRAME_MESSAGE_BUF (f))
5842 {
5843 int len;
5844 char *a[1];
5845 a[0] = (char *) XSTRING (string)->data;
5846
5847 len = doprnt (FRAME_MESSAGE_BUF (f),
5848 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5849
5850 if (log)
5851 message2 (FRAME_MESSAGE_BUF (f), len,
5852 STRING_MULTIBYTE (string));
5853 else
5854 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5855 STRING_MULTIBYTE (string));
5856
5857 /* Print should start at the beginning of the message
5858 buffer next time. */
5859 message_buf_print = 0;
5860 }
5861 }
5862 }
5863
5864
5865 /* Dump an informative message to the minibuf. If M is 0, clear out
5866 any existing message, and let the mini-buffer text show through. */
5867
5868 /* VARARGS 1 */
5869 void
5870 message (m, a1, a2, a3)
5871 char *m;
5872 EMACS_INT a1, a2, a3;
5873 {
5874 if (noninteractive)
5875 {
5876 if (m)
5877 {
5878 if (noninteractive_need_newline)
5879 putc ('\n', stderr);
5880 noninteractive_need_newline = 0;
5881 fprintf (stderr, m, a1, a2, a3);
5882 if (cursor_in_echo_area == 0)
5883 fprintf (stderr, "\n");
5884 fflush (stderr);
5885 }
5886 }
5887 else if (INTERACTIVE)
5888 {
5889 /* The frame whose mini-buffer we're going to display the message
5890 on. It may be larger than the selected frame, so we need to
5891 use its buffer, not the selected frame's buffer. */
5892 Lisp_Object mini_window;
5893 struct frame *f, *sf = SELECTED_FRAME ();
5894
5895 /* Get the frame containing the mini-buffer
5896 that the selected frame is using. */
5897 mini_window = FRAME_MINIBUF_WINDOW (sf);
5898 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5899
5900 /* A null message buffer means that the frame hasn't really been
5901 initialized yet. Error messages get reported properly by
5902 cmd_error, so this must be just an informative message; toss
5903 it. */
5904 if (FRAME_MESSAGE_BUF (f))
5905 {
5906 if (m)
5907 {
5908 int len;
5909 #ifdef NO_ARG_ARRAY
5910 char *a[3];
5911 a[0] = (char *) a1;
5912 a[1] = (char *) a2;
5913 a[2] = (char *) a3;
5914
5915 len = doprnt (FRAME_MESSAGE_BUF (f),
5916 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5917 #else
5918 len = doprnt (FRAME_MESSAGE_BUF (f),
5919 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5920 (char **) &a1);
5921 #endif /* NO_ARG_ARRAY */
5922
5923 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5924 }
5925 else
5926 message1 (0);
5927
5928 /* Print should start at the beginning of the message
5929 buffer next time. */
5930 message_buf_print = 0;
5931 }
5932 }
5933 }
5934
5935
5936 /* The non-logging version of message. */
5937
5938 void
5939 message_nolog (m, a1, a2, a3)
5940 char *m;
5941 EMACS_INT a1, a2, a3;
5942 {
5943 Lisp_Object old_log_max;
5944 old_log_max = Vmessage_log_max;
5945 Vmessage_log_max = Qnil;
5946 message (m, a1, a2, a3);
5947 Vmessage_log_max = old_log_max;
5948 }
5949
5950
5951 /* Display the current message in the current mini-buffer. This is
5952 only called from error handlers in process.c, and is not time
5953 critical. */
5954
5955 void
5956 update_echo_area ()
5957 {
5958 if (!NILP (echo_area_buffer[0]))
5959 {
5960 Lisp_Object string;
5961 string = Fcurrent_message ();
5962 message3 (string, XSTRING (string)->size,
5963 !NILP (current_buffer->enable_multibyte_characters));
5964 }
5965 }
5966
5967
5968 /* Make sure echo area buffers in echo_buffers[] are life. If they
5969 aren't, make new ones. */
5970
5971 static void
5972 ensure_echo_area_buffers ()
5973 {
5974 int i;
5975
5976 for (i = 0; i < 2; ++i)
5977 if (!BUFFERP (echo_buffer[i])
5978 || NILP (XBUFFER (echo_buffer[i])->name))
5979 {
5980 char name[30];
5981 Lisp_Object old_buffer;
5982 int j;
5983
5984 old_buffer = echo_buffer[i];
5985 sprintf (name, " *Echo Area %d*", i);
5986 echo_buffer[i] = Fget_buffer_create (build_string (name));
5987 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5988
5989 for (j = 0; j < 2; ++j)
5990 if (EQ (old_buffer, echo_area_buffer[j]))
5991 echo_area_buffer[j] = echo_buffer[i];
5992 }
5993 }
5994
5995
5996 /* Call FN with args A1..A4 with either the current or last displayed
5997 echo_area_buffer as current buffer.
5998
5999 WHICH zero means use the current message buffer
6000 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6001 from echo_buffer[] and clear it.
6002
6003 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6004 suitable buffer from echo_buffer[] and clear it.
6005
6006 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6007 that the current message becomes the last displayed one, make
6008 choose a suitable buffer for echo_area_buffer[0], and clear it.
6009
6010 Value is what FN returns. */
6011
6012 static int
6013 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6014 struct window *w;
6015 int which;
6016 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6017 EMACS_INT a1;
6018 Lisp_Object a2;
6019 EMACS_INT a3, a4;
6020 {
6021 Lisp_Object buffer;
6022 int this_one, the_other, clear_buffer_p, rc;
6023 int count = BINDING_STACK_SIZE ();
6024
6025 /* If buffers aren't life, make new ones. */
6026 ensure_echo_area_buffers ();
6027
6028 clear_buffer_p = 0;
6029
6030 if (which == 0)
6031 this_one = 0, the_other = 1;
6032 else if (which > 0)
6033 this_one = 1, the_other = 0;
6034 else
6035 {
6036 this_one = 0, the_other = 1;
6037 clear_buffer_p = 1;
6038
6039 /* We need a fresh one in case the current echo buffer equals
6040 the one containing the last displayed echo area message. */
6041 if (!NILP (echo_area_buffer[this_one])
6042 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6043 echo_area_buffer[this_one] = Qnil;
6044 }
6045
6046 /* Choose a suitable buffer from echo_buffer[] is we don't
6047 have one. */
6048 if (NILP (echo_area_buffer[this_one]))
6049 {
6050 echo_area_buffer[this_one]
6051 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6052 ? echo_buffer[the_other]
6053 : echo_buffer[this_one]);
6054 clear_buffer_p = 1;
6055 }
6056
6057 buffer = echo_area_buffer[this_one];
6058
6059 record_unwind_protect (unwind_with_echo_area_buffer,
6060 with_echo_area_buffer_unwind_data (w));
6061
6062 /* Make the echo area buffer current. Note that for display
6063 purposes, it is not necessary that the displayed window's buffer
6064 == current_buffer, except for text property lookup. So, let's
6065 only set that buffer temporarily here without doing a full
6066 Fset_window_buffer. We must also change w->pointm, though,
6067 because otherwise an assertions in unshow_buffer fails, and Emacs
6068 aborts. */
6069 set_buffer_internal_1 (XBUFFER (buffer));
6070 if (w)
6071 {
6072 w->buffer = buffer;
6073 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6074 }
6075
6076 current_buffer->undo_list = Qt;
6077 current_buffer->read_only = Qnil;
6078 specbind (Qinhibit_read_only, Qt);
6079
6080 if (clear_buffer_p && Z > BEG)
6081 del_range (BEG, Z);
6082
6083 xassert (BEGV >= BEG);
6084 xassert (ZV <= Z && ZV >= BEGV);
6085
6086 rc = fn (a1, a2, a3, a4);
6087
6088 xassert (BEGV >= BEG);
6089 xassert (ZV <= Z && ZV >= BEGV);
6090
6091 unbind_to (count, Qnil);
6092 return rc;
6093 }
6094
6095
6096 /* Save state that should be preserved around the call to the function
6097 FN called in with_echo_area_buffer. */
6098
6099 static Lisp_Object
6100 with_echo_area_buffer_unwind_data (w)
6101 struct window *w;
6102 {
6103 int i = 0;
6104 Lisp_Object vector;
6105
6106 /* Reduce consing by keeping one vector in
6107 Vwith_echo_area_save_vector. */
6108 vector = Vwith_echo_area_save_vector;
6109 Vwith_echo_area_save_vector = Qnil;
6110
6111 if (NILP (vector))
6112 vector = Fmake_vector (make_number (7), Qnil);
6113
6114 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6115 AREF (vector, i) = Vdeactivate_mark, ++i;
6116 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6117
6118 if (w)
6119 {
6120 XSETWINDOW (AREF (vector, i), w); ++i;
6121 AREF (vector, i) = w->buffer; ++i;
6122 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6123 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6124 }
6125 else
6126 {
6127 int end = i + 4;
6128 for (; i < end; ++i)
6129 AREF (vector, i) = Qnil;
6130 }
6131
6132 xassert (i == ASIZE (vector));
6133 return vector;
6134 }
6135
6136
6137 /* Restore global state from VECTOR which was created by
6138 with_echo_area_buffer_unwind_data. */
6139
6140 static Lisp_Object
6141 unwind_with_echo_area_buffer (vector)
6142 Lisp_Object vector;
6143 {
6144 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6145 Vdeactivate_mark = AREF (vector, 1);
6146 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6147
6148 if (WINDOWP (AREF (vector, 3)))
6149 {
6150 struct window *w;
6151 Lisp_Object buffer, charpos, bytepos;
6152
6153 w = XWINDOW (AREF (vector, 3));
6154 buffer = AREF (vector, 4);
6155 charpos = AREF (vector, 5);
6156 bytepos = AREF (vector, 6);
6157
6158 w->buffer = buffer;
6159 set_marker_both (w->pointm, buffer,
6160 XFASTINT (charpos), XFASTINT (bytepos));
6161 }
6162
6163 Vwith_echo_area_save_vector = vector;
6164 return Qnil;
6165 }
6166
6167
6168 /* Set up the echo area for use by print functions. MULTIBYTE_P
6169 non-zero means we will print multibyte. */
6170
6171 void
6172 setup_echo_area_for_printing (multibyte_p)
6173 int multibyte_p;
6174 {
6175 ensure_echo_area_buffers ();
6176
6177 if (!message_buf_print)
6178 {
6179 /* A message has been output since the last time we printed.
6180 Choose a fresh echo area buffer. */
6181 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6182 echo_area_buffer[0] = echo_buffer[1];
6183 else
6184 echo_area_buffer[0] = echo_buffer[0];
6185
6186 /* Switch to that buffer and clear it. */
6187 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6188 current_buffer->truncate_lines = Qnil;
6189
6190 if (Z > BEG)
6191 {
6192 int count = BINDING_STACK_SIZE ();
6193 specbind (Qinhibit_read_only, Qt);
6194 del_range (BEG, Z);
6195 unbind_to (count, Qnil);
6196 }
6197 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6198
6199 /* Set up the buffer for the multibyteness we need. */
6200 if (multibyte_p
6201 != !NILP (current_buffer->enable_multibyte_characters))
6202 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6203
6204 /* Raise the frame containing the echo area. */
6205 if (minibuffer_auto_raise)
6206 {
6207 struct frame *sf = SELECTED_FRAME ();
6208 Lisp_Object mini_window;
6209 mini_window = FRAME_MINIBUF_WINDOW (sf);
6210 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6211 }
6212
6213 message_log_maybe_newline ();
6214 message_buf_print = 1;
6215 }
6216 else
6217 {
6218 if (NILP (echo_area_buffer[0]))
6219 {
6220 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6221 echo_area_buffer[0] = echo_buffer[1];
6222 else
6223 echo_area_buffer[0] = echo_buffer[0];
6224 }
6225
6226 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6227 {
6228 /* Someone switched buffers between print requests. */
6229 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6230 current_buffer->truncate_lines = Qnil;
6231 }
6232 }
6233 }
6234
6235
6236 /* Display an echo area message in window W. Value is non-zero if W's
6237 height is changed. If display_last_displayed_message_p is
6238 non-zero, display the message that was last displayed, otherwise
6239 display the current message. */
6240
6241 static int
6242 display_echo_area (w)
6243 struct window *w;
6244 {
6245 int i, no_message_p, window_height_changed_p, count;
6246
6247 /* Temporarily disable garbage collections while displaying the echo
6248 area. This is done because a GC can print a message itself.
6249 That message would modify the echo area buffer's contents while a
6250 redisplay of the buffer is going on, and seriously confuse
6251 redisplay. */
6252 count = inhibit_garbage_collection ();
6253
6254 /* If there is no message, we must call display_echo_area_1
6255 nevertheless because it resizes the window. But we will have to
6256 reset the echo_area_buffer in question to nil at the end because
6257 with_echo_area_buffer will sets it to an empty buffer. */
6258 i = display_last_displayed_message_p ? 1 : 0;
6259 no_message_p = NILP (echo_area_buffer[i]);
6260
6261 window_height_changed_p
6262 = with_echo_area_buffer (w, display_last_displayed_message_p,
6263 display_echo_area_1,
6264 (EMACS_INT) w, Qnil, 0, 0);
6265
6266 if (no_message_p)
6267 echo_area_buffer[i] = Qnil;
6268
6269 unbind_to (count, Qnil);
6270 return window_height_changed_p;
6271 }
6272
6273
6274 /* Helper for display_echo_area. Display the current buffer which
6275 contains the current echo area message in window W, a mini-window,
6276 a pointer to which is passed in A1. A2..A4 are currently not used.
6277 Change the height of W so that all of the message is displayed.
6278 Value is non-zero if height of W was changed. */
6279
6280 static int
6281 display_echo_area_1 (a1, a2, a3, a4)
6282 EMACS_INT a1;
6283 Lisp_Object a2;
6284 EMACS_INT a3, a4;
6285 {
6286 struct window *w = (struct window *) a1;
6287 Lisp_Object window;
6288 struct text_pos start;
6289 int window_height_changed_p = 0;
6290
6291 /* Do this before displaying, so that we have a large enough glyph
6292 matrix for the display. */
6293 window_height_changed_p = resize_mini_window (w, 0);
6294
6295 /* Display. */
6296 clear_glyph_matrix (w->desired_matrix);
6297 XSETWINDOW (window, w);
6298 SET_TEXT_POS (start, BEG, BEG_BYTE);
6299 try_window (window, start);
6300
6301 return window_height_changed_p;
6302 }
6303
6304
6305 /* Resize the echo area window to exactly the size needed for the
6306 currently displayed message, if there is one. */
6307
6308 void
6309 resize_echo_area_axactly ()
6310 {
6311 if (BUFFERP (echo_area_buffer[0])
6312 && WINDOWP (echo_area_window))
6313 {
6314 struct window *w = XWINDOW (echo_area_window);
6315 int resized_p;
6316
6317 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6318 (EMACS_INT) w, Qnil, 0, 0);
6319 if (resized_p)
6320 {
6321 ++windows_or_buffers_changed;
6322 ++update_mode_lines;
6323 redisplay_internal (0);
6324 }
6325 }
6326 }
6327
6328
6329 /* Callback function for with_echo_area_buffer, when used from
6330 resize_echo_area_axactly. A1 contains a pointer to the window to
6331 resize, A2 to A4 are not used. Value is what resize_mini_window
6332 returns. */
6333
6334 static int
6335 resize_mini_window_1 (a1, a2, a3, a4)
6336 EMACS_INT a1;
6337 Lisp_Object a2;
6338 EMACS_INT a3, a4;
6339 {
6340 return resize_mini_window ((struct window *) a1, 1);
6341 }
6342
6343
6344 /* Resize mini-window W to fit the size of its contents. EXACT:P
6345 means size the window exactly to the size needed. Otherwise, it's
6346 only enlarged until W's buffer is empty. Value is non-zero if
6347 the window height has been changed. */
6348
6349 int
6350 resize_mini_window (w, exact_p)
6351 struct window *w;
6352 int exact_p;
6353 {
6354 struct frame *f = XFRAME (w->frame);
6355 int window_height_changed_p = 0;
6356
6357 xassert (MINI_WINDOW_P (w));
6358
6359 /* Nil means don't try to resize. */
6360 if (NILP (Vresize_mini_windows)
6361 || (FRAME_X_P (f) && f->output_data.x == NULL))
6362 return 0;
6363
6364 if (!FRAME_MINIBUF_ONLY_P (f))
6365 {
6366 struct it it;
6367 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6368 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6369 int height, max_height;
6370 int unit = CANON_Y_UNIT (f);
6371 struct text_pos start;
6372 struct buffer *old_current_buffer = NULL;
6373
6374 if (current_buffer != XBUFFER (w->buffer))
6375 {
6376 old_current_buffer = current_buffer;
6377 set_buffer_internal (XBUFFER (w->buffer));
6378 }
6379
6380 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6381
6382 /* Compute the max. number of lines specified by the user. */
6383 if (FLOATP (Vmax_mini_window_height))
6384 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6385 else if (INTEGERP (Vmax_mini_window_height))
6386 max_height = XINT (Vmax_mini_window_height);
6387 else
6388 max_height = total_height / 4;
6389
6390 /* Correct that max. height if it's bogus. */
6391 max_height = max (1, max_height);
6392 max_height = min (total_height, max_height);
6393
6394 /* Find out the height of the text in the window. */
6395 if (it.truncate_lines_p)
6396 height = 1;
6397 else
6398 {
6399 last_height = 0;
6400 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6401 if (it.max_ascent == 0 && it.max_descent == 0)
6402 height = it.current_y + last_height;
6403 else
6404 height = it.current_y + it.max_ascent + it.max_descent;
6405 height -= it.extra_line_spacing;
6406 height = (height + unit - 1) / unit;
6407 }
6408
6409 /* Compute a suitable window start. */
6410 if (height > max_height)
6411 {
6412 height = max_height;
6413 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6414 move_it_vertically_backward (&it, (height - 1) * unit);
6415 start = it.current.pos;
6416 }
6417 else
6418 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6419 SET_MARKER_FROM_TEXT_POS (w->start, start);
6420
6421 if (EQ (Vresize_mini_windows, Qgrow_only))
6422 {
6423 /* Let it grow only, until we display an empty message, in which
6424 case the window shrinks again. */
6425 if (height > XFASTINT (w->height))
6426 {
6427 int old_height = XFASTINT (w->height);
6428 freeze_window_starts (f, 1);
6429 grow_mini_window (w, height - XFASTINT (w->height));
6430 window_height_changed_p = XFASTINT (w->height) != old_height;
6431 }
6432 else if (height < XFASTINT (w->height)
6433 && (exact_p || BEGV == ZV))
6434 {
6435 int old_height = XFASTINT (w->height);
6436 freeze_window_starts (f, 0);
6437 shrink_mini_window (w);
6438 window_height_changed_p = XFASTINT (w->height) != old_height;
6439 }
6440 }
6441 else
6442 {
6443 /* Always resize to exact size needed. */
6444 if (height > XFASTINT (w->height))
6445 {
6446 int old_height = XFASTINT (w->height);
6447 freeze_window_starts (f, 1);
6448 grow_mini_window (w, height - XFASTINT (w->height));
6449 window_height_changed_p = XFASTINT (w->height) != old_height;
6450 }
6451 else if (height < XFASTINT (w->height))
6452 {
6453 int old_height = XFASTINT (w->height);
6454 freeze_window_starts (f, 0);
6455 shrink_mini_window (w);
6456
6457 if (height)
6458 {
6459 freeze_window_starts (f, 1);
6460 grow_mini_window (w, height - XFASTINT (w->height));
6461 }
6462
6463 window_height_changed_p = XFASTINT (w->height) != old_height;
6464 }
6465 }
6466
6467 if (old_current_buffer)
6468 set_buffer_internal (old_current_buffer);
6469 }
6470
6471 return window_height_changed_p;
6472 }
6473
6474
6475 /* Value is the current message, a string, or nil if there is no
6476 current message. */
6477
6478 Lisp_Object
6479 current_message ()
6480 {
6481 Lisp_Object msg;
6482
6483 if (NILP (echo_area_buffer[0]))
6484 msg = Qnil;
6485 else
6486 {
6487 with_echo_area_buffer (0, 0, current_message_1,
6488 (EMACS_INT) &msg, Qnil, 0, 0);
6489 if (NILP (msg))
6490 echo_area_buffer[0] = Qnil;
6491 }
6492
6493 return msg;
6494 }
6495
6496
6497 static int
6498 current_message_1 (a1, a2, a3, a4)
6499 EMACS_INT a1;
6500 Lisp_Object a2;
6501 EMACS_INT a3, a4;
6502 {
6503 Lisp_Object *msg = (Lisp_Object *) a1;
6504
6505 if (Z > BEG)
6506 *msg = make_buffer_string (BEG, Z, 1);
6507 else
6508 *msg = Qnil;
6509 return 0;
6510 }
6511
6512
6513 /* Push the current message on Vmessage_stack for later restauration
6514 by restore_message. Value is non-zero if the current message isn't
6515 empty. This is a relatively infrequent operation, so it's not
6516 worth optimizing. */
6517
6518 int
6519 push_message ()
6520 {
6521 Lisp_Object msg;
6522 msg = current_message ();
6523 Vmessage_stack = Fcons (msg, Vmessage_stack);
6524 return STRINGP (msg);
6525 }
6526
6527
6528 /* Handler for record_unwind_protect calling pop_message. */
6529
6530 Lisp_Object
6531 push_message_unwind (dummy)
6532 Lisp_Object dummy;
6533 {
6534 pop_message ();
6535 return Qnil;
6536 }
6537
6538
6539 /* Restore message display from the top of Vmessage_stack. */
6540
6541 void
6542 restore_message ()
6543 {
6544 Lisp_Object msg;
6545
6546 xassert (CONSP (Vmessage_stack));
6547 msg = XCAR (Vmessage_stack);
6548 if (STRINGP (msg))
6549 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6550 else
6551 message3_nolog (msg, 0, 0);
6552 }
6553
6554
6555 /* Pop the top-most entry off Vmessage_stack. */
6556
6557 void
6558 pop_message ()
6559 {
6560 xassert (CONSP (Vmessage_stack));
6561 Vmessage_stack = XCDR (Vmessage_stack);
6562 }
6563
6564
6565 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6566 exits. If the stack is not empty, we have a missing pop_message
6567 somewhere. */
6568
6569 void
6570 check_message_stack ()
6571 {
6572 if (!NILP (Vmessage_stack))
6573 abort ();
6574 }
6575
6576
6577 /* Truncate to NCHARS what will be displayed in the echo area the next
6578 time we display it---but don't redisplay it now. */
6579
6580 void
6581 truncate_echo_area (nchars)
6582 int nchars;
6583 {
6584 if (nchars == 0)
6585 echo_area_buffer[0] = Qnil;
6586 /* A null message buffer means that the frame hasn't really been
6587 initialized yet. Error messages get reported properly by
6588 cmd_error, so this must be just an informative message; toss it. */
6589 else if (!noninteractive
6590 && INTERACTIVE
6591 && !NILP (echo_area_buffer[0]))
6592 {
6593 struct frame *sf = SELECTED_FRAME ();
6594 if (FRAME_MESSAGE_BUF (sf))
6595 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6596 }
6597 }
6598
6599
6600 /* Helper function for truncate_echo_area. Truncate the current
6601 message to at most NCHARS characters. */
6602
6603 static int
6604 truncate_message_1 (nchars, a2, a3, a4)
6605 EMACS_INT nchars;
6606 Lisp_Object a2;
6607 EMACS_INT a3, a4;
6608 {
6609 if (BEG + nchars < Z)
6610 del_range (BEG + nchars, Z);
6611 if (Z == BEG)
6612 echo_area_buffer[0] = Qnil;
6613 return 0;
6614 }
6615
6616
6617 /* Set the current message to a substring of S or STRING.
6618
6619 If STRING is a Lisp string, set the message to the first NBYTES
6620 bytes from STRING. NBYTES zero means use the whole string. If
6621 STRING is multibyte, the message will be displayed multibyte.
6622
6623 If S is not null, set the message to the first LEN bytes of S. LEN
6624 zero means use the whole string. MULTIBYTE_P non-zero means S is
6625 multibyte. Display the message multibyte in that case. */
6626
6627 void
6628 set_message (s, string, nbytes, multibyte_p)
6629 char *s;
6630 Lisp_Object string;
6631 int nbytes;
6632 {
6633 message_enable_multibyte
6634 = ((s && multibyte_p)
6635 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6636
6637 with_echo_area_buffer (0, -1, set_message_1,
6638 (EMACS_INT) s, string, nbytes, multibyte_p);
6639 message_buf_print = 0;
6640 help_echo_showing_p = 0;
6641 }
6642
6643
6644 /* Helper function for set_message. Arguments have the same meaning
6645 as there, with A1 corresponding to S and A2 corresponding to STRING
6646 This function is called with the echo area buffer being
6647 current. */
6648
6649 static int
6650 set_message_1 (a1, a2, nbytes, multibyte_p)
6651 EMACS_INT a1;
6652 Lisp_Object a2;
6653 EMACS_INT nbytes, multibyte_p;
6654 {
6655 char *s = (char *) a1;
6656 Lisp_Object string = a2;
6657
6658 xassert (BEG == Z);
6659
6660 /* Change multibyteness of the echo buffer appropriately. */
6661 if (message_enable_multibyte
6662 != !NILP (current_buffer->enable_multibyte_characters))
6663 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6664
6665 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6666
6667 /* Insert new message at BEG. */
6668 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6669
6670 if (STRINGP (string))
6671 {
6672 int nchars;
6673
6674 if (nbytes == 0)
6675 nbytes = XSTRING (string)->size_byte;
6676 nchars = string_byte_to_char (string, nbytes);
6677
6678 /* This function takes care of single/multibyte conversion. We
6679 just have to ensure that the echo area buffer has the right
6680 setting of enable_multibyte_characters. */
6681 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6682 }
6683 else if (s)
6684 {
6685 if (nbytes == 0)
6686 nbytes = strlen (s);
6687
6688 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6689 {
6690 /* Convert from multi-byte to single-byte. */
6691 int i, c, n;
6692 unsigned char work[1];
6693
6694 /* Convert a multibyte string to single-byte. */
6695 for (i = 0; i < nbytes; i += n)
6696 {
6697 c = string_char_and_length (s + i, nbytes - i, &n);
6698 work[0] = (SINGLE_BYTE_CHAR_P (c)
6699 ? c
6700 : multibyte_char_to_unibyte (c, Qnil));
6701 insert_1_both (work, 1, 1, 1, 0, 0);
6702 }
6703 }
6704 else if (!multibyte_p
6705 && !NILP (current_buffer->enable_multibyte_characters))
6706 {
6707 /* Convert from single-byte to multi-byte. */
6708 int i, c, n;
6709 unsigned char *msg = (unsigned char *) s;
6710 unsigned char str[MAX_MULTIBYTE_LENGTH];
6711
6712 /* Convert a single-byte string to multibyte. */
6713 for (i = 0; i < nbytes; i++)
6714 {
6715 c = unibyte_char_to_multibyte (msg[i]);
6716 n = CHAR_STRING (c, str);
6717 insert_1_both (str, 1, n, 1, 0, 0);
6718 }
6719 }
6720 else
6721 insert_1 (s, nbytes, 1, 0, 0);
6722 }
6723
6724 return 0;
6725 }
6726
6727
6728 /* Clear messages. CURRENT_P non-zero means clear the current
6729 message. LAST_DISPLAYED_P non-zero means clear the message
6730 last displayed. */
6731
6732 void
6733 clear_message (current_p, last_displayed_p)
6734 int current_p, last_displayed_p;
6735 {
6736 if (current_p)
6737 echo_area_buffer[0] = Qnil;
6738
6739 if (last_displayed_p)
6740 echo_area_buffer[1] = Qnil;
6741
6742 message_buf_print = 0;
6743 }
6744
6745 /* Clear garbaged frames.
6746
6747 This function is used where the old redisplay called
6748 redraw_garbaged_frames which in turn called redraw_frame which in
6749 turn called clear_frame. The call to clear_frame was a source of
6750 flickering. I believe a clear_frame is not necessary. It should
6751 suffice in the new redisplay to invalidate all current matrices,
6752 and ensure a complete redisplay of all windows. */
6753
6754 static void
6755 clear_garbaged_frames ()
6756 {
6757 if (frame_garbaged)
6758 {
6759 Lisp_Object tail, frame;
6760
6761 FOR_EACH_FRAME (tail, frame)
6762 {
6763 struct frame *f = XFRAME (frame);
6764
6765 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6766 {
6767 clear_current_matrices (f);
6768 f->garbaged = 0;
6769 }
6770 }
6771
6772 frame_garbaged = 0;
6773 ++windows_or_buffers_changed;
6774 }
6775 }
6776
6777
6778 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6779 is non-zero update selected_frame. Value is non-zero if the
6780 mini-windows height has been changed. */
6781
6782 static int
6783 echo_area_display (update_frame_p)
6784 int update_frame_p;
6785 {
6786 Lisp_Object mini_window;
6787 struct window *w;
6788 struct frame *f;
6789 int window_height_changed_p = 0;
6790 struct frame *sf = SELECTED_FRAME ();
6791
6792 mini_window = FRAME_MINIBUF_WINDOW (sf);
6793 w = XWINDOW (mini_window);
6794 f = XFRAME (WINDOW_FRAME (w));
6795
6796 /* Don't display if frame is invisible or not yet initialized. */
6797 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6798 return 0;
6799
6800 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6801 #ifndef macintosh
6802 #ifdef HAVE_WINDOW_SYSTEM
6803 /* When Emacs starts, selected_frame may be a visible terminal
6804 frame, even if we run under a window system. If we let this
6805 through, a message would be displayed on the terminal. */
6806 if (EQ (selected_frame, Vterminal_frame)
6807 && !NILP (Vwindow_system))
6808 return 0;
6809 #endif /* HAVE_WINDOW_SYSTEM */
6810 #endif
6811
6812 /* Redraw garbaged frames. */
6813 if (frame_garbaged)
6814 clear_garbaged_frames ();
6815
6816 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6817 {
6818 echo_area_window = mini_window;
6819 window_height_changed_p = display_echo_area (w);
6820 w->must_be_updated_p = 1;
6821
6822 /* Update the display, unless called from redisplay_internal.
6823 Also don't update the screen during redisplay itself. The
6824 update will happen at the end of redisplay, and an update
6825 here could cause confusion. */
6826 if (update_frame_p && !redisplaying_p)
6827 {
6828 int n = 0;
6829
6830 /* If the display update has been interrupted by pending
6831 input, update mode lines in the frame. Due to the
6832 pending input, it might have been that redisplay hasn't
6833 been called, so that mode lines above the echo area are
6834 garbaged. This looks odd, so we prevent it here. */
6835 if (!display_completed)
6836 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6837
6838 if (window_height_changed_p
6839 /* Don't do this if Emacs is shutting down. Redisplay
6840 needs to run hooks. */
6841 && !NILP (Vrun_hooks))
6842 {
6843 /* Must update other windows. Likewise as in other
6844 cases, don't let this update be interrupted by
6845 pending input. */
6846 int count = BINDING_STACK_SIZE ();
6847 specbind (Qredisplay_dont_pause, Qt);
6848 windows_or_buffers_changed = 1;
6849 redisplay_internal (0);
6850 unbind_to (count, Qnil);
6851 }
6852 else if (FRAME_WINDOW_P (f) && n == 0)
6853 {
6854 /* Window configuration is the same as before.
6855 Can do with a display update of the echo area,
6856 unless we displayed some mode lines. */
6857 update_single_window (w, 1);
6858 rif->flush_display (f);
6859 }
6860 else
6861 update_frame (f, 1, 1);
6862
6863 /* If cursor is in the echo area, make sure that the next
6864 redisplay displays the minibuffer, so that the cursor will
6865 be replaced with what the minibuffer wants. */
6866 if (cursor_in_echo_area)
6867 ++windows_or_buffers_changed;
6868 }
6869 }
6870 else if (!EQ (mini_window, selected_window))
6871 windows_or_buffers_changed++;
6872
6873 /* Last displayed message is now the current message. */
6874 echo_area_buffer[1] = echo_area_buffer[0];
6875
6876 /* Prevent redisplay optimization in redisplay_internal by resetting
6877 this_line_start_pos. This is done because the mini-buffer now
6878 displays the message instead of its buffer text. */
6879 if (EQ (mini_window, selected_window))
6880 CHARPOS (this_line_start_pos) = 0;
6881
6882 return window_height_changed_p;
6883 }
6884
6885
6886 \f
6887 /***********************************************************************
6888 Frame Titles
6889 ***********************************************************************/
6890
6891
6892 #ifdef HAVE_WINDOW_SYSTEM
6893
6894 /* A buffer for constructing frame titles in it; allocated from the
6895 heap in init_xdisp and resized as needed in store_frame_title_char. */
6896
6897 static char *frame_title_buf;
6898
6899 /* The buffer's end, and a current output position in it. */
6900
6901 static char *frame_title_buf_end;
6902 static char *frame_title_ptr;
6903
6904
6905 /* Store a single character C for the frame title in frame_title_buf.
6906 Re-allocate frame_title_buf if necessary. */
6907
6908 static void
6909 store_frame_title_char (c)
6910 char c;
6911 {
6912 /* If output position has reached the end of the allocated buffer,
6913 double the buffer's size. */
6914 if (frame_title_ptr == frame_title_buf_end)
6915 {
6916 int len = frame_title_ptr - frame_title_buf;
6917 int new_size = 2 * len * sizeof *frame_title_buf;
6918 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6919 frame_title_buf_end = frame_title_buf + new_size;
6920 frame_title_ptr = frame_title_buf + len;
6921 }
6922
6923 *frame_title_ptr++ = c;
6924 }
6925
6926
6927 /* Store part of a frame title in frame_title_buf, beginning at
6928 frame_title_ptr. STR is the string to store. Do not copy
6929 characters that yield more columns than PRECISION; PRECISION <= 0
6930 means copy the whole string. Pad with spaces until FIELD_WIDTH
6931 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6932 pad. Called from display_mode_element when it is used to build a
6933 frame title. */
6934
6935 static int
6936 store_frame_title (str, field_width, precision)
6937 unsigned char *str;
6938 int field_width, precision;
6939 {
6940 int n = 0;
6941 int dummy, nbytes, width;
6942
6943 /* Copy at most PRECISION chars from STR. */
6944 nbytes = strlen (str);
6945 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6946 while (nbytes--)
6947 store_frame_title_char (*str++);
6948
6949 /* Fill up with spaces until FIELD_WIDTH reached. */
6950 while (field_width > 0
6951 && n < field_width)
6952 {
6953 store_frame_title_char (' ');
6954 ++n;
6955 }
6956
6957 return n;
6958 }
6959
6960
6961 /* Set the title of FRAME, if it has changed. The title format is
6962 Vicon_title_format if FRAME is iconified, otherwise it is
6963 frame_title_format. */
6964
6965 static void
6966 x_consider_frame_title (frame)
6967 Lisp_Object frame;
6968 {
6969 struct frame *f = XFRAME (frame);
6970
6971 if (FRAME_WINDOW_P (f)
6972 || FRAME_MINIBUF_ONLY_P (f)
6973 || f->explicit_name)
6974 {
6975 /* Do we have more than one visible frame on this X display? */
6976 Lisp_Object tail;
6977 Lisp_Object fmt;
6978 struct buffer *obuf;
6979 int len;
6980 struct it it;
6981
6982 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6983 {
6984 struct frame *tf = XFRAME (XCAR (tail));
6985
6986 if (tf != f
6987 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6988 && !FRAME_MINIBUF_ONLY_P (tf)
6989 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6990 break;
6991 }
6992
6993 /* Set global variable indicating that multiple frames exist. */
6994 multiple_frames = CONSP (tail);
6995
6996 /* Switch to the buffer of selected window of the frame. Set up
6997 frame_title_ptr so that display_mode_element will output into it;
6998 then display the title. */
6999 obuf = current_buffer;
7000 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7001 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7002 frame_title_ptr = frame_title_buf;
7003 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7004 NULL, DEFAULT_FACE_ID);
7005 display_mode_element (&it, 0, -1, -1, fmt);
7006 len = frame_title_ptr - frame_title_buf;
7007 frame_title_ptr = NULL;
7008 set_buffer_internal (obuf);
7009
7010 /* Set the title only if it's changed. This avoids consing in
7011 the common case where it hasn't. (If it turns out that we've
7012 already wasted too much time by walking through the list with
7013 display_mode_element, then we might need to optimize at a
7014 higher level than this.) */
7015 if (! STRINGP (f->name)
7016 || STRING_BYTES (XSTRING (f->name)) != len
7017 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7018 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7019 }
7020 }
7021
7022 #else /* not HAVE_WINDOW_SYSTEM */
7023
7024 #define frame_title_ptr ((char *)0)
7025 #define store_frame_title(str, mincol, maxcol) 0
7026
7027 #endif /* not HAVE_WINDOW_SYSTEM */
7028
7029
7030
7031 \f
7032 /***********************************************************************
7033 Menu Bars
7034 ***********************************************************************/
7035
7036
7037 /* Prepare for redisplay by updating menu-bar item lists when
7038 appropriate. This can call eval. */
7039
7040 void
7041 prepare_menu_bars ()
7042 {
7043 int all_windows;
7044 struct gcpro gcpro1, gcpro2;
7045 struct frame *f;
7046 Lisp_Object tooltip_frame;
7047
7048 #ifdef HAVE_X_WINDOWS
7049 tooltip_frame = tip_frame;
7050 #else
7051 tooltip_frame = Qnil;
7052 #endif
7053
7054 /* Update all frame titles based on their buffer names, etc. We do
7055 this before the menu bars so that the buffer-menu will show the
7056 up-to-date frame titles. */
7057 #ifdef HAVE_WINDOW_SYSTEM
7058 if (windows_or_buffers_changed || update_mode_lines)
7059 {
7060 Lisp_Object tail, frame;
7061
7062 FOR_EACH_FRAME (tail, frame)
7063 {
7064 f = XFRAME (frame);
7065 if (!EQ (frame, tooltip_frame)
7066 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7067 x_consider_frame_title (frame);
7068 }
7069 }
7070 #endif /* HAVE_WINDOW_SYSTEM */
7071
7072 /* Update the menu bar item lists, if appropriate. This has to be
7073 done before any actual redisplay or generation of display lines. */
7074 all_windows = (update_mode_lines
7075 || buffer_shared > 1
7076 || windows_or_buffers_changed);
7077 if (all_windows)
7078 {
7079 Lisp_Object tail, frame;
7080 int count = BINDING_STACK_SIZE ();
7081
7082 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7083
7084 FOR_EACH_FRAME (tail, frame)
7085 {
7086 f = XFRAME (frame);
7087
7088 /* Ignore tooltip frame. */
7089 if (EQ (frame, tooltip_frame))
7090 continue;
7091
7092 /* If a window on this frame changed size, report that to
7093 the user and clear the size-change flag. */
7094 if (FRAME_WINDOW_SIZES_CHANGED (f))
7095 {
7096 Lisp_Object functions;
7097
7098 /* Clear flag first in case we get an error below. */
7099 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7100 functions = Vwindow_size_change_functions;
7101 GCPRO2 (tail, functions);
7102
7103 while (CONSP (functions))
7104 {
7105 call1 (XCAR (functions), frame);
7106 functions = XCDR (functions);
7107 }
7108 UNGCPRO;
7109 }
7110
7111 GCPRO1 (tail);
7112 update_menu_bar (f, 0);
7113 #ifdef HAVE_WINDOW_SYSTEM
7114 update_tool_bar (f, 0);
7115 #endif
7116 UNGCPRO;
7117 }
7118
7119 unbind_to (count, Qnil);
7120 }
7121 else
7122 {
7123 struct frame *sf = SELECTED_FRAME ();
7124 update_menu_bar (sf, 1);
7125 #ifdef HAVE_WINDOW_SYSTEM
7126 update_tool_bar (sf, 1);
7127 #endif
7128 }
7129
7130 /* Motif needs this. See comment in xmenu.c. Turn it off when
7131 pending_menu_activation is not defined. */
7132 #ifdef USE_X_TOOLKIT
7133 pending_menu_activation = 0;
7134 #endif
7135 }
7136
7137
7138 /* Update the menu bar item list for frame F. This has to be done
7139 before we start to fill in any display lines, because it can call
7140 eval.
7141
7142 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7143
7144 static void
7145 update_menu_bar (f, save_match_data)
7146 struct frame *f;
7147 int save_match_data;
7148 {
7149 Lisp_Object window;
7150 register struct window *w;
7151
7152 /* If called recursively during a menu update, do nothing. This can
7153 happen when, for instance, an activate-menubar-hook causes a
7154 redisplay. */
7155 if (inhibit_menubar_update)
7156 return;
7157
7158 window = FRAME_SELECTED_WINDOW (f);
7159 w = XWINDOW (window);
7160
7161 if (update_mode_lines)
7162 w->update_mode_line = Qt;
7163
7164 if (FRAME_WINDOW_P (f)
7165 ?
7166 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7167 FRAME_EXTERNAL_MENU_BAR (f)
7168 #else
7169 FRAME_MENU_BAR_LINES (f) > 0
7170 #endif
7171 : FRAME_MENU_BAR_LINES (f) > 0)
7172 {
7173 /* If the user has switched buffers or windows, we need to
7174 recompute to reflect the new bindings. But we'll
7175 recompute when update_mode_lines is set too; that means
7176 that people can use force-mode-line-update to request
7177 that the menu bar be recomputed. The adverse effect on
7178 the rest of the redisplay algorithm is about the same as
7179 windows_or_buffers_changed anyway. */
7180 if (windows_or_buffers_changed
7181 || !NILP (w->update_mode_line)
7182 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7183 < BUF_MODIFF (XBUFFER (w->buffer)))
7184 != !NILP (w->last_had_star))
7185 || ((!NILP (Vtransient_mark_mode)
7186 && !NILP (XBUFFER (w->buffer)->mark_active))
7187 != !NILP (w->region_showing)))
7188 {
7189 struct buffer *prev = current_buffer;
7190 int count = BINDING_STACK_SIZE ();
7191
7192 specbind (Qinhibit_menubar_update, Qt);
7193
7194 set_buffer_internal_1 (XBUFFER (w->buffer));
7195 if (save_match_data)
7196 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7197 if (NILP (Voverriding_local_map_menu_flag))
7198 {
7199 specbind (Qoverriding_terminal_local_map, Qnil);
7200 specbind (Qoverriding_local_map, Qnil);
7201 }
7202
7203 /* Run the Lucid hook. */
7204 safe_run_hooks (Qactivate_menubar_hook);
7205
7206 /* If it has changed current-menubar from previous value,
7207 really recompute the menu-bar from the value. */
7208 if (! NILP (Vlucid_menu_bar_dirty_flag))
7209 call0 (Qrecompute_lucid_menubar);
7210
7211 safe_run_hooks (Qmenu_bar_update_hook);
7212 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7213
7214 /* Redisplay the menu bar in case we changed it. */
7215 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7216 if (FRAME_WINDOW_P (f)
7217 #if defined (macintosh)
7218 /* All frames on Mac OS share the same menubar. So only the
7219 selected frame should be allowed to set it. */
7220 && f == SELECTED_FRAME ()
7221 #endif
7222 )
7223 set_frame_menubar (f, 0, 0);
7224 else
7225 /* On a terminal screen, the menu bar is an ordinary screen
7226 line, and this makes it get updated. */
7227 w->update_mode_line = Qt;
7228 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7229 /* In the non-toolkit version, the menu bar is an ordinary screen
7230 line, and this makes it get updated. */
7231 w->update_mode_line = Qt;
7232 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7233
7234 unbind_to (count, Qnil);
7235 set_buffer_internal_1 (prev);
7236 }
7237 }
7238 }
7239
7240
7241 \f
7242 /***********************************************************************
7243 Tool-bars
7244 ***********************************************************************/
7245
7246 #ifdef HAVE_WINDOW_SYSTEM
7247
7248 /* Update the tool-bar item list for frame F. This has to be done
7249 before we start to fill in any display lines. Called from
7250 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7251 and restore it here. */
7252
7253 static void
7254 update_tool_bar (f, save_match_data)
7255 struct frame *f;
7256 int save_match_data;
7257 {
7258 if (WINDOWP (f->tool_bar_window)
7259 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7260 {
7261 Lisp_Object window;
7262 struct window *w;
7263
7264 window = FRAME_SELECTED_WINDOW (f);
7265 w = XWINDOW (window);
7266
7267 /* If the user has switched buffers or windows, we need to
7268 recompute to reflect the new bindings. But we'll
7269 recompute when update_mode_lines is set too; that means
7270 that people can use force-mode-line-update to request
7271 that the menu bar be recomputed. The adverse effect on
7272 the rest of the redisplay algorithm is about the same as
7273 windows_or_buffers_changed anyway. */
7274 if (windows_or_buffers_changed
7275 || !NILP (w->update_mode_line)
7276 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7277 < BUF_MODIFF (XBUFFER (w->buffer)))
7278 != !NILP (w->last_had_star))
7279 || ((!NILP (Vtransient_mark_mode)
7280 && !NILP (XBUFFER (w->buffer)->mark_active))
7281 != !NILP (w->region_showing)))
7282 {
7283 struct buffer *prev = current_buffer;
7284 int count = BINDING_STACK_SIZE ();
7285
7286 /* Set current_buffer to the buffer of the selected
7287 window of the frame, so that we get the right local
7288 keymaps. */
7289 set_buffer_internal_1 (XBUFFER (w->buffer));
7290
7291 /* Save match data, if we must. */
7292 if (save_match_data)
7293 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7294
7295 /* Make sure that we don't accidentally use bogus keymaps. */
7296 if (NILP (Voverriding_local_map_menu_flag))
7297 {
7298 specbind (Qoverriding_terminal_local_map, Qnil);
7299 specbind (Qoverriding_local_map, Qnil);
7300 }
7301
7302 /* Build desired tool-bar items from keymaps. */
7303 f->tool_bar_items
7304 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7305
7306 /* Redisplay the tool-bar in case we changed it. */
7307 w->update_mode_line = Qt;
7308
7309 unbind_to (count, Qnil);
7310 set_buffer_internal_1 (prev);
7311 }
7312 }
7313 }
7314
7315
7316 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7317 F's desired tool-bar contents. F->tool_bar_items must have
7318 been set up previously by calling prepare_menu_bars. */
7319
7320 static void
7321 build_desired_tool_bar_string (f)
7322 struct frame *f;
7323 {
7324 int i, size, size_needed;
7325 struct gcpro gcpro1, gcpro2, gcpro3;
7326 Lisp_Object image, plist, props;
7327
7328 image = plist = props = Qnil;
7329 GCPRO3 (image, plist, props);
7330
7331 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7332 Otherwise, make a new string. */
7333
7334 /* The size of the string we might be able to reuse. */
7335 size = (STRINGP (f->desired_tool_bar_string)
7336 ? XSTRING (f->desired_tool_bar_string)->size
7337 : 0);
7338
7339 /* We need one space in the string for each image. */
7340 size_needed = f->n_tool_bar_items;
7341
7342 /* Reuse f->desired_tool_bar_string, if possible. */
7343 if (size < size_needed || NILP (f->desired_tool_bar_string))
7344 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7345 make_number (' '));
7346 else
7347 {
7348 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7349 Fremove_text_properties (make_number (0), make_number (size),
7350 props, f->desired_tool_bar_string);
7351 }
7352
7353 /* Put a `display' property on the string for the images to display,
7354 put a `menu_item' property on tool-bar items with a value that
7355 is the index of the item in F's tool-bar item vector. */
7356 for (i = 0; i < f->n_tool_bar_items; ++i)
7357 {
7358 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7359
7360 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7361 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7362 int hmargin, vmargin, relief, idx, end;
7363 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7364 extern Lisp_Object Qlaplace;
7365
7366 /* If image is a vector, choose the image according to the
7367 button state. */
7368 image = PROP (TOOL_BAR_ITEM_IMAGES);
7369 if (VECTORP (image))
7370 {
7371 if (enabled_p)
7372 idx = (selected_p
7373 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7374 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7375 else
7376 idx = (selected_p
7377 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7378 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7379
7380 xassert (ASIZE (image) >= idx);
7381 image = AREF (image, idx);
7382 }
7383 else
7384 idx = -1;
7385
7386 /* Ignore invalid image specifications. */
7387 if (!valid_image_p (image))
7388 continue;
7389
7390 /* Display the tool-bar button pressed, or depressed. */
7391 plist = Fcopy_sequence (XCDR (image));
7392
7393 /* Compute margin and relief to draw. */
7394 relief = (tool_bar_button_relief > 0
7395 ? tool_bar_button_relief
7396 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7397 hmargin = vmargin = relief;
7398
7399 if (INTEGERP (Vtool_bar_button_margin)
7400 && XINT (Vtool_bar_button_margin) > 0)
7401 {
7402 hmargin += XFASTINT (Vtool_bar_button_margin);
7403 vmargin += XFASTINT (Vtool_bar_button_margin);
7404 }
7405 else if (CONSP (Vtool_bar_button_margin))
7406 {
7407 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7408 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7409 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7410
7411 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7412 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7413 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7414 }
7415
7416 if (auto_raise_tool_bar_buttons_p)
7417 {
7418 /* Add a `:relief' property to the image spec if the item is
7419 selected. */
7420 if (selected_p)
7421 {
7422 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7423 hmargin -= relief;
7424 vmargin -= relief;
7425 }
7426 }
7427 else
7428 {
7429 /* If image is selected, display it pressed, i.e. with a
7430 negative relief. If it's not selected, display it with a
7431 raised relief. */
7432 plist = Fplist_put (plist, QCrelief,
7433 (selected_p
7434 ? make_number (-relief)
7435 : make_number (relief)));
7436 hmargin -= relief;
7437 vmargin -= relief;
7438 }
7439
7440 /* Put a margin around the image. */
7441 if (hmargin || vmargin)
7442 {
7443 if (hmargin == vmargin)
7444 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7445 else
7446 plist = Fplist_put (plist, QCmargin,
7447 Fcons (make_number (hmargin),
7448 make_number (vmargin)));
7449 }
7450
7451 /* If button is not enabled, and we don't have special images
7452 for the disabled state, make the image appear disabled by
7453 applying an appropriate algorithm to it. */
7454 if (!enabled_p && idx < 0)
7455 plist = Fplist_put (plist, QCconversion, Qdisabled);
7456
7457 /* Put a `display' text property on the string for the image to
7458 display. Put a `menu-item' property on the string that gives
7459 the start of this item's properties in the tool-bar items
7460 vector. */
7461 image = Fcons (Qimage, plist);
7462 props = list4 (Qdisplay, image,
7463 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7464
7465 /* Let the last image hide all remaining spaces in the tool bar
7466 string. The string can be longer than needed when we reuse a
7467 previous string. */
7468 if (i + 1 == f->n_tool_bar_items)
7469 end = XSTRING (f->desired_tool_bar_string)->size;
7470 else
7471 end = i + 1;
7472 Fadd_text_properties (make_number (i), make_number (end),
7473 props, f->desired_tool_bar_string);
7474 #undef PROP
7475 }
7476
7477 UNGCPRO;
7478 }
7479
7480
7481 /* Display one line of the tool-bar of frame IT->f. */
7482
7483 static void
7484 display_tool_bar_line (it)
7485 struct it *it;
7486 {
7487 struct glyph_row *row = it->glyph_row;
7488 int max_x = it->last_visible_x;
7489 struct glyph *last;
7490
7491 prepare_desired_row (row);
7492 row->y = it->current_y;
7493
7494 /* Note that this isn't made use of if the face hasn't a box,
7495 so there's no need to check the face here. */
7496 it->start_of_box_run_p = 1;
7497
7498 while (it->current_x < max_x)
7499 {
7500 int x_before, x, n_glyphs_before, i, nglyphs;
7501
7502 /* Get the next display element. */
7503 if (!get_next_display_element (it))
7504 break;
7505
7506 /* Produce glyphs. */
7507 x_before = it->current_x;
7508 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7509 PRODUCE_GLYPHS (it);
7510
7511 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7512 i = 0;
7513 x = x_before;
7514 while (i < nglyphs)
7515 {
7516 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7517
7518 if (x + glyph->pixel_width > max_x)
7519 {
7520 /* Glyph doesn't fit on line. */
7521 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7522 it->current_x = x;
7523 goto out;
7524 }
7525
7526 ++it->hpos;
7527 x += glyph->pixel_width;
7528 ++i;
7529 }
7530
7531 /* Stop at line ends. */
7532 if (ITERATOR_AT_END_OF_LINE_P (it))
7533 break;
7534
7535 set_iterator_to_next (it, 1);
7536 }
7537
7538 out:;
7539
7540 row->displays_text_p = row->used[TEXT_AREA] != 0;
7541 extend_face_to_end_of_line (it);
7542 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7543 last->right_box_line_p = 1;
7544 if (last == row->glyphs[TEXT_AREA])
7545 last->left_box_line_p = 1;
7546 compute_line_metrics (it);
7547
7548 /* If line is empty, make it occupy the rest of the tool-bar. */
7549 if (!row->displays_text_p)
7550 {
7551 row->height = row->phys_height = it->last_visible_y - row->y;
7552 row->ascent = row->phys_ascent = 0;
7553 }
7554
7555 row->full_width_p = 1;
7556 row->continued_p = 0;
7557 row->truncated_on_left_p = 0;
7558 row->truncated_on_right_p = 0;
7559
7560 it->current_x = it->hpos = 0;
7561 it->current_y += row->height;
7562 ++it->vpos;
7563 ++it->glyph_row;
7564 }
7565
7566
7567 /* Value is the number of screen lines needed to make all tool-bar
7568 items of frame F visible. */
7569
7570 static int
7571 tool_bar_lines_needed (f)
7572 struct frame *f;
7573 {
7574 struct window *w = XWINDOW (f->tool_bar_window);
7575 struct it it;
7576
7577 /* Initialize an iterator for iteration over
7578 F->desired_tool_bar_string in the tool-bar window of frame F. */
7579 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7580 it.first_visible_x = 0;
7581 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7582 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7583
7584 while (!ITERATOR_AT_END_P (&it))
7585 {
7586 it.glyph_row = w->desired_matrix->rows;
7587 clear_glyph_row (it.glyph_row);
7588 display_tool_bar_line (&it);
7589 }
7590
7591 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7592 }
7593
7594
7595 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7596 0, 1, 0,
7597 "Return the number of lines occupied by the tool bar of FRAME.")
7598 (frame)
7599 Lisp_Object frame;
7600 {
7601 struct frame *f;
7602 struct window *w;
7603 int nlines = 0;
7604
7605 if (NILP (frame))
7606 frame = selected_frame;
7607 else
7608 CHECK_FRAME (frame, 0);
7609 f = XFRAME (frame);
7610
7611 if (WINDOWP (f->tool_bar_window)
7612 || (w = XWINDOW (f->tool_bar_window),
7613 XFASTINT (w->height) > 0))
7614 {
7615 update_tool_bar (f, 1);
7616 if (f->n_tool_bar_items)
7617 {
7618 build_desired_tool_bar_string (f);
7619 nlines = tool_bar_lines_needed (f);
7620 }
7621 }
7622
7623 return make_number (nlines);
7624 }
7625
7626
7627 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7628 height should be changed. */
7629
7630 static int
7631 redisplay_tool_bar (f)
7632 struct frame *f;
7633 {
7634 struct window *w;
7635 struct it it;
7636 struct glyph_row *row;
7637 int change_height_p = 0;
7638
7639 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7640 do anything. This means you must start with tool-bar-lines
7641 non-zero to get the auto-sizing effect. Or in other words, you
7642 can turn off tool-bars by specifying tool-bar-lines zero. */
7643 if (!WINDOWP (f->tool_bar_window)
7644 || (w = XWINDOW (f->tool_bar_window),
7645 XFASTINT (w->height) == 0))
7646 return 0;
7647
7648 /* Set up an iterator for the tool-bar window. */
7649 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7650 it.first_visible_x = 0;
7651 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7652 row = it.glyph_row;
7653
7654 /* Build a string that represents the contents of the tool-bar. */
7655 build_desired_tool_bar_string (f);
7656 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7657
7658 /* Display as many lines as needed to display all tool-bar items. */
7659 while (it.current_y < it.last_visible_y)
7660 display_tool_bar_line (&it);
7661
7662 /* It doesn't make much sense to try scrolling in the tool-bar
7663 window, so don't do it. */
7664 w->desired_matrix->no_scrolling_p = 1;
7665 w->must_be_updated_p = 1;
7666
7667 if (auto_resize_tool_bars_p)
7668 {
7669 int nlines;
7670
7671 /* If we couldn't display everything, change the tool-bar's
7672 height. */
7673 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7674 change_height_p = 1;
7675
7676 /* If there are blank lines at the end, except for a partially
7677 visible blank line at the end that is smaller than
7678 CANON_Y_UNIT, change the tool-bar's height. */
7679 row = it.glyph_row - 1;
7680 if (!row->displays_text_p
7681 && row->height >= CANON_Y_UNIT (f))
7682 change_height_p = 1;
7683
7684 /* If row displays tool-bar items, but is partially visible,
7685 change the tool-bar's height. */
7686 if (row->displays_text_p
7687 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7688 change_height_p = 1;
7689
7690 /* Resize windows as needed by changing the `tool-bar-lines'
7691 frame parameter. */
7692 if (change_height_p
7693 && (nlines = tool_bar_lines_needed (f),
7694 nlines != XFASTINT (w->height)))
7695 {
7696 extern Lisp_Object Qtool_bar_lines;
7697 Lisp_Object frame;
7698 int old_height = XFASTINT (w->height);
7699
7700 XSETFRAME (frame, f);
7701 clear_glyph_matrix (w->desired_matrix);
7702 Fmodify_frame_parameters (frame,
7703 Fcons (Fcons (Qtool_bar_lines,
7704 make_number (nlines)),
7705 Qnil));
7706 if (XFASTINT (w->height) != old_height)
7707 fonts_changed_p = 1;
7708 }
7709 }
7710
7711 return change_height_p;
7712 }
7713
7714
7715 /* Get information about the tool-bar item which is displayed in GLYPH
7716 on frame F. Return in *PROP_IDX the index where tool-bar item
7717 properties start in F->tool_bar_items. Value is zero if
7718 GLYPH doesn't display a tool-bar item. */
7719
7720 int
7721 tool_bar_item_info (f, glyph, prop_idx)
7722 struct frame *f;
7723 struct glyph *glyph;
7724 int *prop_idx;
7725 {
7726 Lisp_Object prop;
7727 int success_p;
7728
7729 /* Get the text property `menu-item' at pos. The value of that
7730 property is the start index of this item's properties in
7731 F->tool_bar_items. */
7732 prop = Fget_text_property (make_number (glyph->charpos),
7733 Qmenu_item, f->current_tool_bar_string);
7734 if (INTEGERP (prop))
7735 {
7736 *prop_idx = XINT (prop);
7737 success_p = 1;
7738 }
7739 else
7740 success_p = 0;
7741
7742 return success_p;
7743 }
7744
7745 #endif /* HAVE_WINDOW_SYSTEM */
7746
7747
7748 \f
7749 /************************************************************************
7750 Horizontal scrolling
7751 ************************************************************************/
7752
7753 static int hscroll_window_tree P_ ((Lisp_Object));
7754 static int hscroll_windows P_ ((Lisp_Object));
7755
7756 /* For all leaf windows in the window tree rooted at WINDOW, set their
7757 hscroll value so that PT is (i) visible in the window, and (ii) so
7758 that it is not within a certain margin at the window's left and
7759 right border. Value is non-zero if any window's hscroll has been
7760 changed. */
7761
7762 static int
7763 hscroll_window_tree (window)
7764 Lisp_Object window;
7765 {
7766 int hscrolled_p = 0;
7767
7768 while (WINDOWP (window))
7769 {
7770 struct window *w = XWINDOW (window);
7771
7772 if (WINDOWP (w->hchild))
7773 hscrolled_p |= hscroll_window_tree (w->hchild);
7774 else if (WINDOWP (w->vchild))
7775 hscrolled_p |= hscroll_window_tree (w->vchild);
7776 else if (w->cursor.vpos >= 0)
7777 {
7778 int hscroll_margin, text_area_x, text_area_y;
7779 int text_area_width, text_area_height;
7780 struct glyph_row *current_cursor_row
7781 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7782 struct glyph_row *desired_cursor_row
7783 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7784 struct glyph_row *cursor_row
7785 = (desired_cursor_row->enabled_p
7786 ? desired_cursor_row
7787 : current_cursor_row);
7788
7789 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7790 &text_area_width, &text_area_height);
7791
7792 /* Scroll when cursor is inside this scroll margin. */
7793 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7794
7795 if ((XFASTINT (w->hscroll)
7796 && w->cursor.x < hscroll_margin)
7797 || (cursor_row->enabled_p
7798 && cursor_row->truncated_on_right_p
7799 && (w->cursor.x > text_area_width - hscroll_margin)))
7800 {
7801 struct it it;
7802 int hscroll;
7803 struct buffer *saved_current_buffer;
7804 int pt;
7805
7806 /* Find point in a display of infinite width. */
7807 saved_current_buffer = current_buffer;
7808 current_buffer = XBUFFER (w->buffer);
7809
7810 if (w == XWINDOW (selected_window))
7811 pt = BUF_PT (current_buffer);
7812 else
7813 {
7814 pt = marker_position (w->pointm);
7815 pt = max (BEGV, pt);
7816 pt = min (ZV, pt);
7817 }
7818
7819 /* Move iterator to pt starting at cursor_row->start in
7820 a line with infinite width. */
7821 init_to_row_start (&it, w, cursor_row);
7822 it.last_visible_x = INFINITY;
7823 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7824 current_buffer = saved_current_buffer;
7825
7826 /* Center cursor in window. */
7827 hscroll = (max (0, it.current_x - text_area_width / 2)
7828 / CANON_X_UNIT (it.f));
7829 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
7830
7831 /* Don't call Fset_window_hscroll if value hasn't
7832 changed because it will prevent redisplay
7833 optimizations. */
7834 if (XFASTINT (w->hscroll) != hscroll)
7835 {
7836 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7837 w->hscroll = make_number (hscroll);
7838 hscrolled_p = 1;
7839 }
7840 }
7841 }
7842
7843 window = w->next;
7844 }
7845
7846 /* Value is non-zero if hscroll of any leaf window has been changed. */
7847 return hscrolled_p;
7848 }
7849
7850
7851 /* Set hscroll so that cursor is visible and not inside horizontal
7852 scroll margins for all windows in the tree rooted at WINDOW. See
7853 also hscroll_window_tree above. Value is non-zero if any window's
7854 hscroll has been changed. If it has, desired matrices on the frame
7855 of WINDOW are cleared. */
7856
7857 static int
7858 hscroll_windows (window)
7859 Lisp_Object window;
7860 {
7861 int hscrolled_p;
7862
7863 if (automatic_hscrolling_p)
7864 {
7865 hscrolled_p = hscroll_window_tree (window);
7866 if (hscrolled_p)
7867 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7868 }
7869 else
7870 hscrolled_p = 0;
7871 return hscrolled_p;
7872 }
7873
7874
7875 \f
7876 /************************************************************************
7877 Redisplay
7878 ************************************************************************/
7879
7880 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7881 to a non-zero value. This is sometimes handy to have in a debugger
7882 session. */
7883
7884 #if GLYPH_DEBUG
7885
7886 /* First and last unchanged row for try_window_id. */
7887
7888 int debug_first_unchanged_at_end_vpos;
7889 int debug_last_unchanged_at_beg_vpos;
7890
7891 /* Delta vpos and y. */
7892
7893 int debug_dvpos, debug_dy;
7894
7895 /* Delta in characters and bytes for try_window_id. */
7896
7897 int debug_delta, debug_delta_bytes;
7898
7899 /* Values of window_end_pos and window_end_vpos at the end of
7900 try_window_id. */
7901
7902 int debug_end_pos, debug_end_vpos;
7903
7904 /* Append a string to W->desired_matrix->method. FMT is a printf
7905 format string. A1...A9 are a supplement for a variable-length
7906 argument list. If trace_redisplay_p is non-zero also printf the
7907 resulting string to stderr. */
7908
7909 static void
7910 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7911 struct window *w;
7912 char *fmt;
7913 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7914 {
7915 char buffer[512];
7916 char *method = w->desired_matrix->method;
7917 int len = strlen (method);
7918 int size = sizeof w->desired_matrix->method;
7919 int remaining = size - len - 1;
7920
7921 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7922 if (len && remaining)
7923 {
7924 method[len] = '|';
7925 --remaining, ++len;
7926 }
7927
7928 strncpy (method + len, buffer, remaining);
7929
7930 if (trace_redisplay_p)
7931 fprintf (stderr, "%p (%s): %s\n",
7932 w,
7933 ((BUFFERP (w->buffer)
7934 && STRINGP (XBUFFER (w->buffer)->name))
7935 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7936 : "no buffer"),
7937 buffer);
7938 }
7939
7940 #endif /* GLYPH_DEBUG */
7941
7942
7943 /* This counter is used to clear the face cache every once in a while
7944 in redisplay_internal. It is incremented for each redisplay.
7945 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7946 cleared. */
7947
7948 #define CLEAR_FACE_CACHE_COUNT 10000
7949 static int clear_face_cache_count;
7950
7951 /* Record the previous terminal frame we displayed. */
7952
7953 static struct frame *previous_terminal_frame;
7954
7955 /* Non-zero while redisplay_internal is in progress. */
7956
7957 int redisplaying_p;
7958
7959
7960 /* Value is non-zero if all changes in window W, which displays
7961 current_buffer, are in the text between START and END. START is a
7962 buffer position, END is given as a distance from Z. Used in
7963 redisplay_internal for display optimization. */
7964
7965 static INLINE int
7966 text_outside_line_unchanged_p (w, start, end)
7967 struct window *w;
7968 int start, end;
7969 {
7970 int unchanged_p = 1;
7971
7972 /* If text or overlays have changed, see where. */
7973 if (XFASTINT (w->last_modified) < MODIFF
7974 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7975 {
7976 /* Gap in the line? */
7977 if (GPT < start || Z - GPT < end)
7978 unchanged_p = 0;
7979
7980 /* Changes start in front of the line, or end after it? */
7981 if (unchanged_p
7982 && (BEG_UNCHANGED < start - 1
7983 || END_UNCHANGED < end))
7984 unchanged_p = 0;
7985
7986 /* If selective display, can't optimize if changes start at the
7987 beginning of the line. */
7988 if (unchanged_p
7989 && INTEGERP (current_buffer->selective_display)
7990 && XINT (current_buffer->selective_display) > 0
7991 && (BEG_UNCHANGED < start || GPT <= start))
7992 unchanged_p = 0;
7993 }
7994
7995 return unchanged_p;
7996 }
7997
7998
7999 /* Do a frame update, taking possible shortcuts into account. This is
8000 the main external entry point for redisplay.
8001
8002 If the last redisplay displayed an echo area message and that message
8003 is no longer requested, we clear the echo area or bring back the
8004 mini-buffer if that is in use. */
8005
8006 void
8007 redisplay ()
8008 {
8009 redisplay_internal (0);
8010 }
8011
8012 /* Return 1 if point moved out of or into a composition. Otherwise
8013 return 0. PREV_BUF and PREV_PT are the last point buffer and
8014 position. BUF and PT are the current point buffer and position. */
8015
8016 int
8017 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8018 struct buffer *prev_buf, *buf;
8019 int prev_pt, pt;
8020 {
8021 int start, end;
8022 Lisp_Object prop;
8023 Lisp_Object buffer;
8024
8025 XSETBUFFER (buffer, buf);
8026 /* Check a composition at the last point if point moved within the
8027 same buffer. */
8028 if (prev_buf == buf)
8029 {
8030 if (prev_pt == pt)
8031 /* Point didn't move. */
8032 return 0;
8033
8034 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8035 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8036 && COMPOSITION_VALID_P (start, end, prop)
8037 && start < prev_pt && end > prev_pt)
8038 /* The last point was within the composition. Return 1 iff
8039 point moved out of the composition. */
8040 return (pt <= start || pt >= end);
8041 }
8042
8043 /* Check a composition at the current point. */
8044 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8045 && find_composition (pt, -1, &start, &end, &prop, buffer)
8046 && COMPOSITION_VALID_P (start, end, prop)
8047 && start < pt && end > pt);
8048 }
8049
8050 /* Reconsider the setting of B->clip_changed which is displayed
8051 in window W. */
8052
8053 static INLINE void
8054 reconsider_clip_changes (w, b)
8055 struct window *w;
8056 struct buffer *b;
8057 {
8058 if (b->prevent_redisplay_optimizations_p)
8059 b->clip_changed = 1;
8060 else if (b->clip_changed
8061 && !NILP (w->window_end_valid)
8062 && w->current_matrix->buffer == b
8063 && w->current_matrix->zv == BUF_ZV (b)
8064 && w->current_matrix->begv == BUF_BEGV (b))
8065 b->clip_changed = 0;
8066
8067 /* If display wasn't paused, and W is not a tool bar window, see if
8068 point has been moved into or out of a composition. In that case,
8069 we set b->clip_changed to 1 to force updating the screen. If
8070 b->clip_changed has already been set to 1, we can skip this
8071 check. */
8072 if (!b->clip_changed
8073 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8074 {
8075 int pt;
8076
8077 if (w == XWINDOW (selected_window))
8078 pt = BUF_PT (current_buffer);
8079 else
8080 pt = marker_position (w->pointm);
8081
8082 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8083 || pt != XINT (w->last_point))
8084 && check_point_in_composition (w->current_matrix->buffer,
8085 XINT (w->last_point),
8086 XBUFFER (w->buffer), pt))
8087 b->clip_changed = 1;
8088 }
8089 }
8090
8091
8092 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8093 response to any user action; therefore, we should preserve the echo
8094 area. (Actually, our caller does that job.) Perhaps in the future
8095 avoid recentering windows if it is not necessary; currently that
8096 causes some problems. */
8097
8098 static void
8099 redisplay_internal (preserve_echo_area)
8100 int preserve_echo_area;
8101 {
8102 struct window *w = XWINDOW (selected_window);
8103 struct frame *f = XFRAME (w->frame);
8104 int pause;
8105 int must_finish = 0;
8106 struct text_pos tlbufpos, tlendpos;
8107 int number_of_visible_frames;
8108 int count;
8109 struct frame *sf = SELECTED_FRAME ();
8110
8111 /* Non-zero means redisplay has to consider all windows on all
8112 frames. Zero means, only selected_window is considered. */
8113 int consider_all_windows_p;
8114
8115 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8116
8117 /* No redisplay if running in batch mode or frame is not yet fully
8118 initialized, or redisplay is explicitly turned off by setting
8119 Vinhibit_redisplay. */
8120 if (noninteractive
8121 || !NILP (Vinhibit_redisplay)
8122 || !f->glyphs_initialized_p)
8123 return;
8124
8125 /* The flag redisplay_performed_directly_p is set by
8126 direct_output_for_insert when it already did the whole screen
8127 update necessary. */
8128 if (redisplay_performed_directly_p)
8129 {
8130 redisplay_performed_directly_p = 0;
8131 if (!hscroll_windows (selected_window))
8132 return;
8133 }
8134
8135 #ifdef USE_X_TOOLKIT
8136 if (popup_activated ())
8137 return;
8138 #endif
8139
8140 /* I don't think this happens but let's be paranoid. */
8141 if (redisplaying_p)
8142 return;
8143
8144 /* Record a function that resets redisplaying_p to its old value
8145 when we leave this function. */
8146 count = BINDING_STACK_SIZE ();
8147 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8148 ++redisplaying_p;
8149
8150 retry:
8151 pause = 0;
8152 reconsider_clip_changes (w, current_buffer);
8153
8154 /* If new fonts have been loaded that make a glyph matrix adjustment
8155 necessary, do it. */
8156 if (fonts_changed_p)
8157 {
8158 adjust_glyphs (NULL);
8159 ++windows_or_buffers_changed;
8160 fonts_changed_p = 0;
8161 }
8162
8163 /* If face_change_count is non-zero, init_iterator will free all
8164 realized faces, which includes the faces referenced from current
8165 matrices. So, we can't reuse current matrices in this case. */
8166 if (face_change_count)
8167 ++windows_or_buffers_changed;
8168
8169 if (! FRAME_WINDOW_P (sf)
8170 && previous_terminal_frame != sf)
8171 {
8172 /* Since frames on an ASCII terminal share the same display
8173 area, displaying a different frame means redisplay the whole
8174 thing. */
8175 windows_or_buffers_changed++;
8176 SET_FRAME_GARBAGED (sf);
8177 XSETFRAME (Vterminal_frame, sf);
8178 }
8179 previous_terminal_frame = sf;
8180
8181 /* Set the visible flags for all frames. Do this before checking
8182 for resized or garbaged frames; they want to know if their frames
8183 are visible. See the comment in frame.h for
8184 FRAME_SAMPLE_VISIBILITY. */
8185 {
8186 Lisp_Object tail, frame;
8187
8188 number_of_visible_frames = 0;
8189
8190 FOR_EACH_FRAME (tail, frame)
8191 {
8192 struct frame *f = XFRAME (frame);
8193
8194 FRAME_SAMPLE_VISIBILITY (f);
8195 if (FRAME_VISIBLE_P (f))
8196 ++number_of_visible_frames;
8197 clear_desired_matrices (f);
8198 }
8199 }
8200
8201 /* Notice any pending interrupt request to change frame size. */
8202 do_pending_window_change (1);
8203
8204 /* Clear frames marked as garbaged. */
8205 if (frame_garbaged)
8206 clear_garbaged_frames ();
8207
8208 /* Build menubar and tool-bar items. */
8209 prepare_menu_bars ();
8210
8211 if (windows_or_buffers_changed)
8212 update_mode_lines++;
8213
8214 /* Detect case that we need to write or remove a star in the mode line. */
8215 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8216 {
8217 w->update_mode_line = Qt;
8218 if (buffer_shared > 1)
8219 update_mode_lines++;
8220 }
8221
8222 /* If %c is in the mode line, update it if needed. */
8223 if (!NILP (w->column_number_displayed)
8224 /* This alternative quickly identifies a common case
8225 where no change is needed. */
8226 && !(PT == XFASTINT (w->last_point)
8227 && XFASTINT (w->last_modified) >= MODIFF
8228 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8229 && XFASTINT (w->column_number_displayed) != current_column ())
8230 w->update_mode_line = Qt;
8231
8232 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8233
8234 /* The variable buffer_shared is set in redisplay_window and
8235 indicates that we redisplay a buffer in different windows. See
8236 there. */
8237 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8238
8239 /* If specs for an arrow have changed, do thorough redisplay
8240 to ensure we remove any arrow that should no longer exist. */
8241 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8242 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8243 consider_all_windows_p = windows_or_buffers_changed = 1;
8244
8245 /* Normally the message* functions will have already displayed and
8246 updated the echo area, but the frame may have been trashed, or
8247 the update may have been preempted, so display the echo area
8248 again here. Checking both message buffers captures the case that
8249 the echo area should be cleared. */
8250 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
8251 {
8252 int window_height_changed_p = echo_area_display (0);
8253 must_finish = 1;
8254
8255 if (fonts_changed_p)
8256 goto retry;
8257 else if (window_height_changed_p)
8258 {
8259 consider_all_windows_p = 1;
8260 ++update_mode_lines;
8261 ++windows_or_buffers_changed;
8262
8263 /* If window configuration was changed, frames may have been
8264 marked garbaged. Clear them or we will experience
8265 surprises wrt scrolling. */
8266 if (frame_garbaged)
8267 clear_garbaged_frames ();
8268 }
8269 }
8270 else if (EQ (selected_window, minibuf_window)
8271 && (current_buffer->clip_changed
8272 || XFASTINT (w->last_modified) < MODIFF
8273 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8274 && resize_mini_window (w, 0))
8275 {
8276 /* Resized active mini-window to fit the size of what it is
8277 showing if its contents might have changed. */
8278 must_finish = 1;
8279 consider_all_windows_p = 1;
8280 ++windows_or_buffers_changed;
8281 ++update_mode_lines;
8282
8283 /* If window configuration was changed, frames may have been
8284 marked garbaged. Clear them or we will experience
8285 surprises wrt scrolling. */
8286 if (frame_garbaged)
8287 clear_garbaged_frames ();
8288 }
8289
8290
8291 /* If showing the region, and mark has changed, we must redisplay
8292 the whole window. The assignment to this_line_start_pos prevents
8293 the optimization directly below this if-statement. */
8294 if (((!NILP (Vtransient_mark_mode)
8295 && !NILP (XBUFFER (w->buffer)->mark_active))
8296 != !NILP (w->region_showing))
8297 || (!NILP (w->region_showing)
8298 && !EQ (w->region_showing,
8299 Fmarker_position (XBUFFER (w->buffer)->mark))))
8300 CHARPOS (this_line_start_pos) = 0;
8301
8302 /* Optimize the case that only the line containing the cursor in the
8303 selected window has changed. Variables starting with this_ are
8304 set in display_line and record information about the line
8305 containing the cursor. */
8306 tlbufpos = this_line_start_pos;
8307 tlendpos = this_line_end_pos;
8308 if (!consider_all_windows_p
8309 && CHARPOS (tlbufpos) > 0
8310 && NILP (w->update_mode_line)
8311 && !current_buffer->clip_changed
8312 && FRAME_VISIBLE_P (XFRAME (w->frame))
8313 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8314 /* Make sure recorded data applies to current buffer, etc. */
8315 && this_line_buffer == current_buffer
8316 && current_buffer == XBUFFER (w->buffer)
8317 && NILP (w->force_start)
8318 /* Point must be on the line that we have info recorded about. */
8319 && PT >= CHARPOS (tlbufpos)
8320 && PT <= Z - CHARPOS (tlendpos)
8321 /* All text outside that line, including its final newline,
8322 must be unchanged */
8323 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8324 CHARPOS (tlendpos)))
8325 {
8326 if (CHARPOS (tlbufpos) > BEGV
8327 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8328 && (CHARPOS (tlbufpos) == ZV
8329 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8330 /* Former continuation line has disappeared by becoming empty */
8331 goto cancel;
8332 else if (XFASTINT (w->last_modified) < MODIFF
8333 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8334 || MINI_WINDOW_P (w))
8335 {
8336 /* We have to handle the case of continuation around a
8337 wide-column character (See the comment in indent.c around
8338 line 885).
8339
8340 For instance, in the following case:
8341
8342 -------- Insert --------
8343 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8344 J_I_ ==> J_I_ `^^' are cursors.
8345 ^^ ^^
8346 -------- --------
8347
8348 As we have to redraw the line above, we should goto cancel. */
8349
8350 struct it it;
8351 int line_height_before = this_line_pixel_height;
8352
8353 /* Note that start_display will handle the case that the
8354 line starting at tlbufpos is a continuation lines. */
8355 start_display (&it, w, tlbufpos);
8356
8357 /* Implementation note: It this still necessary? */
8358 if (it.current_x != this_line_start_x)
8359 goto cancel;
8360
8361 TRACE ((stderr, "trying display optimization 1\n"));
8362 w->cursor.vpos = -1;
8363 overlay_arrow_seen = 0;
8364 it.vpos = this_line_vpos;
8365 it.current_y = this_line_y;
8366 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8367 display_line (&it);
8368
8369 /* If line contains point, is not continued,
8370 and ends at same distance from eob as before, we win */
8371 if (w->cursor.vpos >= 0
8372 /* Line is not continued, otherwise this_line_start_pos
8373 would have been set to 0 in display_line. */
8374 && CHARPOS (this_line_start_pos)
8375 /* Line ends as before. */
8376 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8377 /* Line has same height as before. Otherwise other lines
8378 would have to be shifted up or down. */
8379 && this_line_pixel_height == line_height_before)
8380 {
8381 /* If this is not the window's last line, we must adjust
8382 the charstarts of the lines below. */
8383 if (it.current_y < it.last_visible_y)
8384 {
8385 struct glyph_row *row
8386 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8387 int delta, delta_bytes;
8388
8389 if (Z - CHARPOS (tlendpos) == ZV)
8390 {
8391 /* This line ends at end of (accessible part of)
8392 buffer. There is no newline to count. */
8393 delta = (Z
8394 - CHARPOS (tlendpos)
8395 - MATRIX_ROW_START_CHARPOS (row));
8396 delta_bytes = (Z_BYTE
8397 - BYTEPOS (tlendpos)
8398 - MATRIX_ROW_START_BYTEPOS (row));
8399 }
8400 else
8401 {
8402 /* This line ends in a newline. Must take
8403 account of the newline and the rest of the
8404 text that follows. */
8405 delta = (Z
8406 - CHARPOS (tlendpos)
8407 - MATRIX_ROW_START_CHARPOS (row));
8408 delta_bytes = (Z_BYTE
8409 - BYTEPOS (tlendpos)
8410 - MATRIX_ROW_START_BYTEPOS (row));
8411 }
8412
8413 increment_matrix_positions (w->current_matrix,
8414 this_line_vpos + 1,
8415 w->current_matrix->nrows,
8416 delta, delta_bytes);
8417 }
8418
8419 /* If this row displays text now but previously didn't,
8420 or vice versa, w->window_end_vpos may have to be
8421 adjusted. */
8422 if ((it.glyph_row - 1)->displays_text_p)
8423 {
8424 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8425 XSETINT (w->window_end_vpos, this_line_vpos);
8426 }
8427 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8428 && this_line_vpos > 0)
8429 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8430 w->window_end_valid = Qnil;
8431
8432 /* Update hint: No need to try to scroll in update_window. */
8433 w->desired_matrix->no_scrolling_p = 1;
8434
8435 #if GLYPH_DEBUG
8436 *w->desired_matrix->method = 0;
8437 debug_method_add (w, "optimization 1");
8438 #endif
8439 goto update;
8440 }
8441 else
8442 goto cancel;
8443 }
8444 else if (/* Cursor position hasn't changed. */
8445 PT == XFASTINT (w->last_point)
8446 /* Make sure the cursor was last displayed
8447 in this window. Otherwise we have to reposition it. */
8448 && 0 <= w->cursor.vpos
8449 && XINT (w->height) > w->cursor.vpos)
8450 {
8451 if (!must_finish)
8452 {
8453 do_pending_window_change (1);
8454
8455 /* We used to always goto end_of_redisplay here, but this
8456 isn't enough if we have a blinking cursor. */
8457 if (w->cursor_off_p == w->last_cursor_off_p)
8458 goto end_of_redisplay;
8459 }
8460 goto update;
8461 }
8462 /* If highlighting the region, or if the cursor is in the echo area,
8463 then we can't just move the cursor. */
8464 else if (! (!NILP (Vtransient_mark_mode)
8465 && !NILP (current_buffer->mark_active))
8466 && (EQ (selected_window, current_buffer->last_selected_window)
8467 || highlight_nonselected_windows)
8468 && NILP (w->region_showing)
8469 && NILP (Vshow_trailing_whitespace)
8470 && !cursor_in_echo_area)
8471 {
8472 struct it it;
8473 struct glyph_row *row;
8474
8475 /* Skip from tlbufpos to PT and see where it is. Note that
8476 PT may be in invisible text. If so, we will end at the
8477 next visible position. */
8478 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8479 NULL, DEFAULT_FACE_ID);
8480 it.current_x = this_line_start_x;
8481 it.current_y = this_line_y;
8482 it.vpos = this_line_vpos;
8483
8484 /* The call to move_it_to stops in front of PT, but
8485 moves over before-strings. */
8486 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8487
8488 if (it.vpos == this_line_vpos
8489 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8490 row->enabled_p))
8491 {
8492 xassert (this_line_vpos == it.vpos);
8493 xassert (this_line_y == it.current_y);
8494 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8495 goto update;
8496 }
8497 else
8498 goto cancel;
8499 }
8500
8501 cancel:
8502 /* Text changed drastically or point moved off of line. */
8503 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8504 }
8505
8506 CHARPOS (this_line_start_pos) = 0;
8507 consider_all_windows_p |= buffer_shared > 1;
8508 ++clear_face_cache_count;
8509
8510
8511 /* Build desired matrices, and update the display. If
8512 consider_all_windows_p is non-zero, do it for all windows on all
8513 frames. Otherwise do it for selected_window, only. */
8514
8515 if (consider_all_windows_p)
8516 {
8517 Lisp_Object tail, frame;
8518 int i, n = 0, size = 50;
8519 struct frame **updated
8520 = (struct frame **) alloca (size * sizeof *updated);
8521
8522 /* Clear the face cache eventually. */
8523 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8524 {
8525 clear_face_cache (0);
8526 clear_face_cache_count = 0;
8527 }
8528
8529 /* Recompute # windows showing selected buffer. This will be
8530 incremented each time such a window is displayed. */
8531 buffer_shared = 0;
8532
8533 FOR_EACH_FRAME (tail, frame)
8534 {
8535 struct frame *f = XFRAME (frame);
8536
8537 if (FRAME_WINDOW_P (f) || f == sf)
8538 {
8539 /* Mark all the scroll bars to be removed; we'll redeem
8540 the ones we want when we redisplay their windows. */
8541 if (condemn_scroll_bars_hook)
8542 condemn_scroll_bars_hook (f);
8543
8544 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8545 redisplay_windows (FRAME_ROOT_WINDOW (f));
8546
8547 /* Any scroll bars which redisplay_windows should have
8548 nuked should now go away. */
8549 if (judge_scroll_bars_hook)
8550 judge_scroll_bars_hook (f);
8551
8552 /* If fonts changed, display again. */
8553 if (fonts_changed_p)
8554 goto retry;
8555
8556 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8557 {
8558 /* See if we have to hscroll. */
8559 if (hscroll_windows (f->root_window))
8560 goto retry;
8561
8562 /* Prevent various kinds of signals during display
8563 update. stdio is not robust about handling
8564 signals, which can cause an apparent I/O
8565 error. */
8566 if (interrupt_input)
8567 unrequest_sigio ();
8568 stop_polling ();
8569
8570 /* Update the display. */
8571 set_window_update_flags (XWINDOW (f->root_window), 1);
8572 pause |= update_frame (f, 0, 0);
8573 if (pause)
8574 break;
8575
8576 if (n == size)
8577 {
8578 int nbytes = size * sizeof *updated;
8579 struct frame **p = (struct frame **) alloca (2 * nbytes);
8580 bcopy (updated, p, nbytes);
8581 size *= 2;
8582 }
8583
8584 updated[n++] = f;
8585 }
8586 }
8587 }
8588
8589 /* Do the mark_window_display_accurate after all windows have
8590 been redisplayed because this call resets flags in buffers
8591 which are needed for proper redisplay. */
8592 for (i = 0; i < n; ++i)
8593 {
8594 struct frame *f = updated[i];
8595 mark_window_display_accurate (f->root_window, 1);
8596 if (frame_up_to_date_hook)
8597 frame_up_to_date_hook (f);
8598 }
8599 }
8600 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8601 {
8602 Lisp_Object mini_window;
8603 struct frame *mini_frame;
8604
8605 redisplay_window (selected_window, 1);
8606
8607 /* Compare desired and current matrices, perform output. */
8608 update:
8609
8610 /* If fonts changed, display again. */
8611 if (fonts_changed_p)
8612 goto retry;
8613
8614 /* Prevent various kinds of signals during display update.
8615 stdio is not robust about handling signals,
8616 which can cause an apparent I/O error. */
8617 if (interrupt_input)
8618 unrequest_sigio ();
8619 stop_polling ();
8620
8621 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8622 {
8623 if (hscroll_windows (selected_window))
8624 goto retry;
8625
8626 XWINDOW (selected_window)->must_be_updated_p = 1;
8627 pause = update_frame (sf, 0, 0);
8628 }
8629
8630 /* We may have called echo_area_display at the top of this
8631 function. If the echo area is on another frame, that may
8632 have put text on a frame other than the selected one, so the
8633 above call to update_frame would not have caught it. Catch
8634 it here. */
8635 mini_window = FRAME_MINIBUF_WINDOW (sf);
8636 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8637
8638 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8639 {
8640 XWINDOW (mini_window)->must_be_updated_p = 1;
8641 pause |= update_frame (mini_frame, 0, 0);
8642 if (!pause && hscroll_windows (mini_window))
8643 goto retry;
8644 }
8645 }
8646
8647 /* If display was paused because of pending input, make sure we do a
8648 thorough update the next time. */
8649 if (pause)
8650 {
8651 /* Prevent the optimization at the beginning of
8652 redisplay_internal that tries a single-line update of the
8653 line containing the cursor in the selected window. */
8654 CHARPOS (this_line_start_pos) = 0;
8655
8656 /* Let the overlay arrow be updated the next time. */
8657 if (!NILP (last_arrow_position))
8658 {
8659 last_arrow_position = Qt;
8660 last_arrow_string = Qt;
8661 }
8662
8663 /* If we pause after scrolling, some rows in the current
8664 matrices of some windows are not valid. */
8665 if (!WINDOW_FULL_WIDTH_P (w)
8666 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8667 update_mode_lines = 1;
8668 }
8669
8670 /* Now text on frame agrees with windows, so put info into the
8671 windows for partial redisplay to follow. */
8672 if (!pause)
8673 {
8674 register struct buffer *b = XBUFFER (w->buffer);
8675
8676 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8677 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8678 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8679 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8680
8681 if (consider_all_windows_p)
8682 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8683 else
8684 {
8685 XSETFASTINT (w->last_point, BUF_PT (b));
8686 w->last_cursor = w->cursor;
8687 w->last_cursor_off_p = w->cursor_off_p;
8688
8689 b->clip_changed = 0;
8690 b->prevent_redisplay_optimizations_p = 0;
8691 w->update_mode_line = Qnil;
8692 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8693 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8694 w->last_had_star
8695 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8696 ? Qt : Qnil);
8697
8698 /* Record if we are showing a region, so can make sure to
8699 update it fully at next redisplay. */
8700 w->region_showing = (!NILP (Vtransient_mark_mode)
8701 && (EQ (selected_window,
8702 current_buffer->last_selected_window)
8703 || highlight_nonselected_windows)
8704 && !NILP (XBUFFER (w->buffer)->mark_active)
8705 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8706 : Qnil);
8707
8708 w->window_end_valid = w->buffer;
8709 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8710 last_arrow_string = Voverlay_arrow_string;
8711 if (frame_up_to_date_hook != 0)
8712 (*frame_up_to_date_hook) (sf);
8713
8714 w->current_matrix->buffer = b;
8715 w->current_matrix->begv = BUF_BEGV (b);
8716 w->current_matrix->zv = BUF_ZV (b);
8717 }
8718
8719 update_mode_lines = 0;
8720 windows_or_buffers_changed = 0;
8721 }
8722
8723 /* Start SIGIO interrupts coming again. Having them off during the
8724 code above makes it less likely one will discard output, but not
8725 impossible, since there might be stuff in the system buffer here.
8726 But it is much hairier to try to do anything about that. */
8727 if (interrupt_input)
8728 request_sigio ();
8729 start_polling ();
8730
8731 /* If a frame has become visible which was not before, redisplay
8732 again, so that we display it. Expose events for such a frame
8733 (which it gets when becoming visible) don't call the parts of
8734 redisplay constructing glyphs, so simply exposing a frame won't
8735 display anything in this case. So, we have to display these
8736 frames here explicitly. */
8737 if (!pause)
8738 {
8739 Lisp_Object tail, frame;
8740 int new_count = 0;
8741
8742 FOR_EACH_FRAME (tail, frame)
8743 {
8744 int this_is_visible = 0;
8745
8746 if (XFRAME (frame)->visible)
8747 this_is_visible = 1;
8748 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8749 if (XFRAME (frame)->visible)
8750 this_is_visible = 1;
8751
8752 if (this_is_visible)
8753 new_count++;
8754 }
8755
8756 if (new_count != number_of_visible_frames)
8757 windows_or_buffers_changed++;
8758 }
8759
8760 /* Change frame size now if a change is pending. */
8761 do_pending_window_change (1);
8762
8763 /* If we just did a pending size change, or have additional
8764 visible frames, redisplay again. */
8765 if (windows_or_buffers_changed && !pause)
8766 goto retry;
8767
8768 end_of_redisplay:;
8769
8770 unbind_to (count, Qnil);
8771 }
8772
8773
8774 /* Redisplay, but leave alone any recent echo area message unless
8775 another message has been requested in its place.
8776
8777 This is useful in situations where you need to redisplay but no
8778 user action has occurred, making it inappropriate for the message
8779 area to be cleared. See tracking_off and
8780 wait_reading_process_input for examples of these situations.
8781
8782 FROM_WHERE is an integer saying from where this function was
8783 called. This is useful for debugging. */
8784
8785 void
8786 redisplay_preserve_echo_area (from_where)
8787 int from_where;
8788 {
8789 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8790
8791 if (!NILP (echo_area_buffer[1]))
8792 {
8793 /* We have a previously displayed message, but no current
8794 message. Redisplay the previous message. */
8795 display_last_displayed_message_p = 1;
8796 redisplay_internal (1);
8797 display_last_displayed_message_p = 0;
8798 }
8799 else
8800 redisplay_internal (1);
8801 }
8802
8803
8804 /* Function registered with record_unwind_protect in
8805 redisplay_internal. Clears the flag indicating that a redisplay is
8806 in progress. */
8807
8808 static Lisp_Object
8809 unwind_redisplay (old_redisplaying_p)
8810 Lisp_Object old_redisplaying_p;
8811 {
8812 redisplaying_p = XFASTINT (old_redisplaying_p);
8813 return Qnil;
8814 }
8815
8816
8817 /* Mark the display of windows in the window tree rooted at WINDOW as
8818 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8819 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8820 the next time redisplay_internal is called. */
8821
8822 void
8823 mark_window_display_accurate (window, accurate_p)
8824 Lisp_Object window;
8825 int accurate_p;
8826 {
8827 struct window *w;
8828
8829 for (; !NILP (window); window = w->next)
8830 {
8831 w = XWINDOW (window);
8832
8833 if (BUFFERP (w->buffer))
8834 {
8835 struct buffer *b = XBUFFER (w->buffer);
8836
8837 XSETFASTINT (w->last_modified,
8838 accurate_p ? BUF_MODIFF (b) : 0);
8839 XSETFASTINT (w->last_overlay_modified,
8840 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8841 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8842 ? Qt : Qnil);
8843
8844 #if 0 /* I don't think this is necessary because display_line does it.
8845 Let's check it. */
8846 /* Record if we are showing a region, so can make sure to
8847 update it fully at next redisplay. */
8848 w->region_showing
8849 = (!NILP (Vtransient_mark_mode)
8850 && (w == XWINDOW (current_buffer->last_selected_window)
8851 || highlight_nonselected_windows)
8852 && (!NILP (b->mark_active)
8853 ? Fmarker_position (b->mark)
8854 : Qnil));
8855 #endif
8856
8857 if (accurate_p)
8858 {
8859 b->clip_changed = 0;
8860 b->prevent_redisplay_optimizations_p = 0;
8861 w->current_matrix->buffer = b;
8862 w->current_matrix->begv = BUF_BEGV (b);
8863 w->current_matrix->zv = BUF_ZV (b);
8864 w->last_cursor = w->cursor;
8865 w->last_cursor_off_p = w->cursor_off_p;
8866 if (w == XWINDOW (selected_window))
8867 w->last_point = make_number (BUF_PT (b));
8868 else
8869 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8870 }
8871 }
8872
8873 w->window_end_valid = w->buffer;
8874 w->update_mode_line = Qnil;
8875
8876 if (!NILP (w->vchild))
8877 mark_window_display_accurate (w->vchild, accurate_p);
8878 if (!NILP (w->hchild))
8879 mark_window_display_accurate (w->hchild, accurate_p);
8880 }
8881
8882 if (accurate_p)
8883 {
8884 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8885 last_arrow_string = Voverlay_arrow_string;
8886 }
8887 else
8888 {
8889 /* Force a thorough redisplay the next time by setting
8890 last_arrow_position and last_arrow_string to t, which is
8891 unequal to any useful value of Voverlay_arrow_... */
8892 last_arrow_position = Qt;
8893 last_arrow_string = Qt;
8894 }
8895 }
8896
8897
8898 /* Return value in display table DP (Lisp_Char_Table *) for character
8899 C. Since a display table doesn't have any parent, we don't have to
8900 follow parent. Do not call this function directly but use the
8901 macro DISP_CHAR_VECTOR. */
8902
8903 Lisp_Object
8904 disp_char_vector (dp, c)
8905 struct Lisp_Char_Table *dp;
8906 int c;
8907 {
8908 int code[4], i;
8909 Lisp_Object val;
8910
8911 if (SINGLE_BYTE_CHAR_P (c))
8912 return (dp->contents[c]);
8913
8914 SPLIT_CHAR (c, code[0], code[1], code[2]);
8915 if (code[1] < 32)
8916 code[1] = -1;
8917 else if (code[2] < 32)
8918 code[2] = -1;
8919
8920 /* Here, the possible range of code[0] (== charset ID) is
8921 128..max_charset. Since the top level char table contains data
8922 for multibyte characters after 256th element, we must increment
8923 code[0] by 128 to get a correct index. */
8924 code[0] += 128;
8925 code[3] = -1; /* anchor */
8926
8927 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8928 {
8929 val = dp->contents[code[i]];
8930 if (!SUB_CHAR_TABLE_P (val))
8931 return (NILP (val) ? dp->defalt : val);
8932 }
8933
8934 /* Here, val is a sub char table. We return the default value of
8935 it. */
8936 return (dp->defalt);
8937 }
8938
8939
8940 \f
8941 /***********************************************************************
8942 Window Redisplay
8943 ***********************************************************************/
8944
8945 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8946
8947 static void
8948 redisplay_windows (window)
8949 Lisp_Object window;
8950 {
8951 while (!NILP (window))
8952 {
8953 struct window *w = XWINDOW (window);
8954
8955 if (!NILP (w->hchild))
8956 redisplay_windows (w->hchild);
8957 else if (!NILP (w->vchild))
8958 redisplay_windows (w->vchild);
8959 else
8960 redisplay_window (window, 0);
8961
8962 window = w->next;
8963 }
8964 }
8965
8966
8967 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8968 DELTA is the number of bytes by which positions recorded in ROW
8969 differ from current buffer positions. */
8970
8971 void
8972 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8973 struct window *w;
8974 struct glyph_row *row;
8975 struct glyph_matrix *matrix;
8976 int delta, delta_bytes, dy, dvpos;
8977 {
8978 struct glyph *glyph = row->glyphs[TEXT_AREA];
8979 struct glyph *end = glyph + row->used[TEXT_AREA];
8980 int x = row->x;
8981 int pt_old = PT - delta;
8982
8983 /* Skip over glyphs not having an object at the start of the row.
8984 These are special glyphs like truncation marks on terminal
8985 frames. */
8986 if (row->displays_text_p)
8987 while (glyph < end
8988 && INTEGERP (glyph->object)
8989 && glyph->charpos < 0)
8990 {
8991 x += glyph->pixel_width;
8992 ++glyph;
8993 }
8994
8995 while (glyph < end
8996 && !INTEGERP (glyph->object)
8997 && (!BUFFERP (glyph->object)
8998 || glyph->charpos < pt_old))
8999 {
9000 x += glyph->pixel_width;
9001 ++glyph;
9002 }
9003
9004 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9005 w->cursor.x = x;
9006 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9007 w->cursor.y = row->y + dy;
9008
9009 if (w == XWINDOW (selected_window))
9010 {
9011 if (!row->continued_p
9012 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9013 && row->x == 0)
9014 {
9015 this_line_buffer = XBUFFER (w->buffer);
9016
9017 CHARPOS (this_line_start_pos)
9018 = MATRIX_ROW_START_CHARPOS (row) + delta;
9019 BYTEPOS (this_line_start_pos)
9020 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9021
9022 CHARPOS (this_line_end_pos)
9023 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9024 BYTEPOS (this_line_end_pos)
9025 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9026
9027 this_line_y = w->cursor.y;
9028 this_line_pixel_height = row->height;
9029 this_line_vpos = w->cursor.vpos;
9030 this_line_start_x = row->x;
9031 }
9032 else
9033 CHARPOS (this_line_start_pos) = 0;
9034 }
9035 }
9036
9037
9038 /* Run window scroll functions, if any, for WINDOW with new window
9039 start STARTP. Sets the window start of WINDOW to that position.
9040
9041 We assume that the window's buffer is really current. */
9042
9043 static INLINE struct text_pos
9044 run_window_scroll_functions (window, startp)
9045 Lisp_Object window;
9046 struct text_pos startp;
9047 {
9048 struct window *w = XWINDOW (window);
9049 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9050
9051 if (current_buffer != XBUFFER (w->buffer))
9052 abort ();
9053
9054 if (!NILP (Vwindow_scroll_functions))
9055 {
9056 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9057 make_number (CHARPOS (startp)));
9058 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9059 /* In case the hook functions switch buffers. */
9060 if (current_buffer != XBUFFER (w->buffer))
9061 set_buffer_internal_1 (XBUFFER (w->buffer));
9062 }
9063
9064 return startp;
9065 }
9066
9067
9068 /* Modify the desired matrix of window W and W->vscroll so that the
9069 line containing the cursor is fully visible. */
9070
9071 static void
9072 make_cursor_line_fully_visible (w)
9073 struct window *w;
9074 {
9075 struct glyph_matrix *matrix;
9076 struct glyph_row *row;
9077 int window_height;
9078
9079 /* It's not always possible to find the cursor, e.g, when a window
9080 is full of overlay strings. Don't do anything in that case. */
9081 if (w->cursor.vpos < 0)
9082 return;
9083
9084 matrix = w->desired_matrix;
9085 row = MATRIX_ROW (matrix, w->cursor.vpos);
9086
9087 /* If the cursor row is not partially visible, there's nothing
9088 to do. */
9089 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9090 return;
9091
9092 /* If the row the cursor is in is taller than the window's height,
9093 it's not clear what to do, so do nothing. */
9094 window_height = window_box_height (w);
9095 if (row->height >= window_height)
9096 return;
9097
9098 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9099 {
9100 int dy = row->height - row->visible_height;
9101 w->vscroll = 0;
9102 w->cursor.y += dy;
9103 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9104 }
9105 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9106 {
9107 int dy = - (row->height - row->visible_height);
9108 w->vscroll = dy;
9109 w->cursor.y += dy;
9110 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9111 }
9112
9113 /* When we change the cursor y-position of the selected window,
9114 change this_line_y as well so that the display optimization for
9115 the cursor line of the selected window in redisplay_internal uses
9116 the correct y-position. */
9117 if (w == XWINDOW (selected_window))
9118 this_line_y = w->cursor.y;
9119 }
9120
9121
9122 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9123 non-zero means only WINDOW is redisplayed in redisplay_internal.
9124 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9125 in redisplay_window to bring a partially visible line into view in
9126 the case that only the cursor has moved.
9127
9128 Value is
9129
9130 1 if scrolling succeeded
9131
9132 0 if scrolling didn't find point.
9133
9134 -1 if new fonts have been loaded so that we must interrupt
9135 redisplay, adjust glyph matrices, and try again. */
9136
9137 static int
9138 try_scrolling (window, just_this_one_p, scroll_conservatively,
9139 scroll_step, temp_scroll_step)
9140 Lisp_Object window;
9141 int just_this_one_p;
9142 int scroll_conservatively, scroll_step;
9143 int temp_scroll_step;
9144 {
9145 struct window *w = XWINDOW (window);
9146 struct frame *f = XFRAME (w->frame);
9147 struct text_pos scroll_margin_pos;
9148 struct text_pos pos;
9149 struct text_pos startp;
9150 struct it it;
9151 Lisp_Object window_end;
9152 int this_scroll_margin;
9153 int dy = 0;
9154 int scroll_max;
9155 int rc;
9156 int amount_to_scroll = 0;
9157 Lisp_Object aggressive;
9158 int height;
9159
9160 #if GLYPH_DEBUG
9161 debug_method_add (w, "try_scrolling");
9162 #endif
9163
9164 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9165
9166 /* Compute scroll margin height in pixels. We scroll when point is
9167 within this distance from the top or bottom of the window. */
9168 if (scroll_margin > 0)
9169 {
9170 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9171 this_scroll_margin *= CANON_Y_UNIT (f);
9172 }
9173 else
9174 this_scroll_margin = 0;
9175
9176 /* Compute how much we should try to scroll maximally to bring point
9177 into view. */
9178 if (scroll_step || scroll_conservatively || temp_scroll_step)
9179 scroll_max = max (scroll_step,
9180 max (scroll_conservatively, temp_scroll_step));
9181 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9182 || NUMBERP (current_buffer->scroll_up_aggressively))
9183 /* We're trying to scroll because of aggressive scrolling
9184 but no scroll_step is set. Choose an arbitrary one. Maybe
9185 there should be a variable for this. */
9186 scroll_max = 10;
9187 else
9188 scroll_max = 0;
9189 scroll_max *= CANON_Y_UNIT (f);
9190
9191 /* Decide whether we have to scroll down. Start at the window end
9192 and move this_scroll_margin up to find the position of the scroll
9193 margin. */
9194 window_end = Fwindow_end (window, Qt);
9195 CHARPOS (scroll_margin_pos) = XINT (window_end);
9196 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9197 if (this_scroll_margin)
9198 {
9199 start_display (&it, w, scroll_margin_pos);
9200 move_it_vertically (&it, - this_scroll_margin);
9201 scroll_margin_pos = it.current.pos;
9202 }
9203
9204 if (PT >= CHARPOS (scroll_margin_pos))
9205 {
9206 int y0;
9207
9208 /* Point is in the scroll margin at the bottom of the window, or
9209 below. Compute a new window start that makes point visible. */
9210
9211 /* Compute the distance from the scroll margin to PT.
9212 Give up if the distance is greater than scroll_max. */
9213 start_display (&it, w, scroll_margin_pos);
9214 y0 = it.current_y;
9215 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9216 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9217
9218 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9219 end, which is one line below the window. The iterator's
9220 current_y will be same as y0 in that case, but we have to
9221 scroll a line to make PT visible. That's the reason why 1 is
9222 added below. */
9223 dy = 1 + it.current_y - y0;
9224
9225 if (dy > scroll_max)
9226 return 0;
9227
9228 /* Move the window start down. If scrolling conservatively,
9229 move it just enough down to make point visible. If
9230 scroll_step is set, move it down by scroll_step. */
9231 start_display (&it, w, startp);
9232
9233 if (scroll_conservatively)
9234 amount_to_scroll
9235 = max (max (dy, CANON_Y_UNIT (f)),
9236 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9237 else if (scroll_step || temp_scroll_step)
9238 amount_to_scroll = scroll_max;
9239 else
9240 {
9241 aggressive = current_buffer->scroll_down_aggressively;
9242 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9243 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9244 if (NUMBERP (aggressive))
9245 amount_to_scroll = XFLOATINT (aggressive) * height;
9246 }
9247
9248 if (amount_to_scroll <= 0)
9249 return 0;
9250
9251 move_it_vertically (&it, amount_to_scroll);
9252 startp = it.current.pos;
9253 }
9254 else
9255 {
9256 /* See if point is inside the scroll margin at the top of the
9257 window. */
9258 scroll_margin_pos = startp;
9259 if (this_scroll_margin)
9260 {
9261 start_display (&it, w, startp);
9262 move_it_vertically (&it, this_scroll_margin);
9263 scroll_margin_pos = it.current.pos;
9264 }
9265
9266 if (PT < CHARPOS (scroll_margin_pos))
9267 {
9268 /* Point is in the scroll margin at the top of the window or
9269 above what is displayed in the window. */
9270 int y0;
9271
9272 /* Compute the vertical distance from PT to the scroll
9273 margin position. Give up if distance is greater than
9274 scroll_max. */
9275 SET_TEXT_POS (pos, PT, PT_BYTE);
9276 start_display (&it, w, pos);
9277 y0 = it.current_y;
9278 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9279 it.last_visible_y, -1,
9280 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9281 dy = it.current_y - y0;
9282 if (dy > scroll_max)
9283 return 0;
9284
9285 /* Compute new window start. */
9286 start_display (&it, w, startp);
9287
9288 if (scroll_conservatively)
9289 amount_to_scroll =
9290 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9291 else if (scroll_step || temp_scroll_step)
9292 amount_to_scroll = scroll_max;
9293 else
9294 {
9295 aggressive = current_buffer->scroll_up_aggressively;
9296 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9297 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9298 if (NUMBERP (aggressive))
9299 amount_to_scroll = XFLOATINT (aggressive) * height;
9300 }
9301
9302 if (amount_to_scroll <= 0)
9303 return 0;
9304
9305 move_it_vertically (&it, - amount_to_scroll);
9306 startp = it.current.pos;
9307 }
9308 }
9309
9310 /* Run window scroll functions. */
9311 startp = run_window_scroll_functions (window, startp);
9312
9313 /* Display the window. Give up if new fonts are loaded, or if point
9314 doesn't appear. */
9315 if (!try_window (window, startp))
9316 rc = -1;
9317 else if (w->cursor.vpos < 0)
9318 {
9319 clear_glyph_matrix (w->desired_matrix);
9320 rc = 0;
9321 }
9322 else
9323 {
9324 /* Maybe forget recorded base line for line number display. */
9325 if (!just_this_one_p
9326 || current_buffer->clip_changed
9327 || BEG_UNCHANGED < CHARPOS (startp))
9328 w->base_line_number = Qnil;
9329
9330 /* If cursor ends up on a partially visible line, shift display
9331 lines up or down. */
9332 make_cursor_line_fully_visible (w);
9333 rc = 1;
9334 }
9335
9336 return rc;
9337 }
9338
9339
9340 /* Compute a suitable window start for window W if display of W starts
9341 on a continuation line. Value is non-zero if a new window start
9342 was computed.
9343
9344 The new window start will be computed, based on W's width, starting
9345 from the start of the continued line. It is the start of the
9346 screen line with the minimum distance from the old start W->start. */
9347
9348 static int
9349 compute_window_start_on_continuation_line (w)
9350 struct window *w;
9351 {
9352 struct text_pos pos, start_pos;
9353 int window_start_changed_p = 0;
9354
9355 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9356
9357 /* If window start is on a continuation line... Window start may be
9358 < BEGV in case there's invisible text at the start of the
9359 buffer (M-x rmail, for example). */
9360 if (CHARPOS (start_pos) > BEGV
9361 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9362 {
9363 struct it it;
9364 struct glyph_row *row;
9365
9366 /* Handle the case that the window start is out of range. */
9367 if (CHARPOS (start_pos) < BEGV)
9368 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9369 else if (CHARPOS (start_pos) > ZV)
9370 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9371
9372 /* Find the start of the continued line. This should be fast
9373 because scan_buffer is fast (newline cache). */
9374 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9375 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9376 row, DEFAULT_FACE_ID);
9377 reseat_at_previous_visible_line_start (&it);
9378
9379 /* If the line start is "too far" away from the window start,
9380 say it takes too much time to compute a new window start. */
9381 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9382 < XFASTINT (w->height) * XFASTINT (w->width))
9383 {
9384 int min_distance, distance;
9385
9386 /* Move forward by display lines to find the new window
9387 start. If window width was enlarged, the new start can
9388 be expected to be > the old start. If window width was
9389 decreased, the new window start will be < the old start.
9390 So, we're looking for the display line start with the
9391 minimum distance from the old window start. */
9392 pos = it.current.pos;
9393 min_distance = INFINITY;
9394 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9395 distance < min_distance)
9396 {
9397 min_distance = distance;
9398 pos = it.current.pos;
9399 move_it_by_lines (&it, 1, 0);
9400 }
9401
9402 /* Set the window start there. */
9403 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9404 window_start_changed_p = 1;
9405 }
9406 }
9407
9408 return window_start_changed_p;
9409 }
9410
9411
9412 /* Try cursor movement in case text has not changes in window WINDOW,
9413 with window start STARTP. Value is
9414
9415 1 if successful
9416
9417 0 if this method cannot be used
9418
9419 -1 if we know we have to scroll the display. *SCROLL_STEP is
9420 set to 1, under certain circumstances, if we want to scroll as
9421 if scroll-step were set to 1. See the code. */
9422
9423 static int
9424 try_cursor_movement (window, startp, scroll_step)
9425 Lisp_Object window;
9426 struct text_pos startp;
9427 int *scroll_step;
9428 {
9429 struct window *w = XWINDOW (window);
9430 struct frame *f = XFRAME (w->frame);
9431 int rc = 0;
9432
9433 /* Handle case where text has not changed, only point, and it has
9434 not moved off the frame. */
9435 if (/* Point may be in this window. */
9436 PT >= CHARPOS (startp)
9437 /* Selective display hasn't changed. */
9438 && !current_buffer->clip_changed
9439 /* Function force-mode-line-update is used to force a thorough
9440 redisplay. It sets either windows_or_buffers_changed or
9441 update_mode_lines. So don't take a shortcut here for these
9442 cases. */
9443 && !update_mode_lines
9444 && !windows_or_buffers_changed
9445 /* Can't use this case if highlighting a region. When a
9446 region exists, cursor movement has to do more than just
9447 set the cursor. */
9448 && !(!NILP (Vtransient_mark_mode)
9449 && !NILP (current_buffer->mark_active))
9450 && NILP (w->region_showing)
9451 && NILP (Vshow_trailing_whitespace)
9452 /* Right after splitting windows, last_point may be nil. */
9453 && INTEGERP (w->last_point)
9454 /* This code is not used for mini-buffer for the sake of the case
9455 of redisplaying to replace an echo area message; since in
9456 that case the mini-buffer contents per se are usually
9457 unchanged. This code is of no real use in the mini-buffer
9458 since the handling of this_line_start_pos, etc., in redisplay
9459 handles the same cases. */
9460 && !EQ (window, minibuf_window)
9461 /* When splitting windows or for new windows, it happens that
9462 redisplay is called with a nil window_end_vpos or one being
9463 larger than the window. This should really be fixed in
9464 window.c. I don't have this on my list, now, so we do
9465 approximately the same as the old redisplay code. --gerd. */
9466 && INTEGERP (w->window_end_vpos)
9467 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9468 && (FRAME_WINDOW_P (f)
9469 || !MARKERP (Voverlay_arrow_position)
9470 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9471 {
9472 int this_scroll_margin;
9473 struct glyph_row *row;
9474
9475 #if GLYPH_DEBUG
9476 debug_method_add (w, "cursor movement");
9477 #endif
9478
9479 /* Scroll if point within this distance from the top or bottom
9480 of the window. This is a pixel value. */
9481 this_scroll_margin = max (0, scroll_margin);
9482 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9483 this_scroll_margin *= CANON_Y_UNIT (f);
9484
9485 /* Start with the row the cursor was displayed during the last
9486 not paused redisplay. Give up if that row is not valid. */
9487 if (w->last_cursor.vpos < 0
9488 || w->last_cursor.vpos >= w->current_matrix->nrows)
9489 rc = -1;
9490 else
9491 {
9492 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9493 if (row->mode_line_p)
9494 ++row;
9495 if (!row->enabled_p)
9496 rc = -1;
9497 }
9498
9499 if (rc == 0)
9500 {
9501 int scroll_p = 0;
9502 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9503
9504 if (PT > XFASTINT (w->last_point))
9505 {
9506 /* Point has moved forward. */
9507 while (MATRIX_ROW_END_CHARPOS (row) < PT
9508 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9509 {
9510 xassert (row->enabled_p);
9511 ++row;
9512 }
9513
9514 /* The end position of a row equals the start position
9515 of the next row. If PT is there, we would rather
9516 display it in the next line. */
9517 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9518 && MATRIX_ROW_END_CHARPOS (row) == PT
9519 && !cursor_row_p (w, row))
9520 ++row;
9521
9522 /* If within the scroll margin, scroll. Note that
9523 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9524 the next line would be drawn, and that
9525 this_scroll_margin can be zero. */
9526 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9527 || PT > MATRIX_ROW_END_CHARPOS (row)
9528 /* Line is completely visible last line in window
9529 and PT is to be set in the next line. */
9530 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9531 && PT == MATRIX_ROW_END_CHARPOS (row)
9532 && !row->ends_at_zv_p
9533 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9534 scroll_p = 1;
9535 }
9536 else if (PT < XFASTINT (w->last_point))
9537 {
9538 /* Cursor has to be moved backward. Note that PT >=
9539 CHARPOS (startp) because of the outer
9540 if-statement. */
9541 while (!row->mode_line_p
9542 && (MATRIX_ROW_START_CHARPOS (row) > PT
9543 || (MATRIX_ROW_START_CHARPOS (row) == PT
9544 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9545 && (row->y > this_scroll_margin
9546 || CHARPOS (startp) == BEGV))
9547 {
9548 xassert (row->enabled_p);
9549 --row;
9550 }
9551
9552 /* Consider the following case: Window starts at BEGV,
9553 there is invisible, intangible text at BEGV, so that
9554 display starts at some point START > BEGV. It can
9555 happen that we are called with PT somewhere between
9556 BEGV and START. Try to handle that case. */
9557 if (row < w->current_matrix->rows
9558 || row->mode_line_p)
9559 {
9560 row = w->current_matrix->rows;
9561 if (row->mode_line_p)
9562 ++row;
9563 }
9564
9565 /* Due to newlines in overlay strings, we may have to
9566 skip forward over overlay strings. */
9567 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9568 && MATRIX_ROW_END_CHARPOS (row) == PT
9569 && !cursor_row_p (w, row))
9570 ++row;
9571
9572 /* If within the scroll margin, scroll. */
9573 if (row->y < this_scroll_margin
9574 && CHARPOS (startp) != BEGV)
9575 scroll_p = 1;
9576 }
9577
9578 if (PT < MATRIX_ROW_START_CHARPOS (row)
9579 || PT > MATRIX_ROW_END_CHARPOS (row))
9580 {
9581 /* if PT is not in the glyph row, give up. */
9582 rc = -1;
9583 }
9584 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9585 {
9586 if (PT == MATRIX_ROW_END_CHARPOS (row)
9587 && !row->ends_at_zv_p
9588 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9589 rc = -1;
9590 else if (row->height > window_box_height (w))
9591 {
9592 /* If we end up in a partially visible line, let's
9593 make it fully visible, except when it's taller
9594 than the window, in which case we can't do much
9595 about it. */
9596 *scroll_step = 1;
9597 rc = -1;
9598 }
9599 else
9600 {
9601 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9602 try_window (window, startp);
9603 make_cursor_line_fully_visible (w);
9604 rc = 1;
9605 }
9606 }
9607 else if (scroll_p)
9608 rc = -1;
9609 else
9610 {
9611 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9612 rc = 1;
9613 }
9614 }
9615 }
9616
9617 return rc;
9618 }
9619
9620
9621 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9622 selected_window is redisplayed. */
9623
9624 static void
9625 redisplay_window (window, just_this_one_p)
9626 Lisp_Object window;
9627 int just_this_one_p;
9628 {
9629 struct window *w = XWINDOW (window);
9630 struct frame *f = XFRAME (w->frame);
9631 struct buffer *buffer = XBUFFER (w->buffer);
9632 struct buffer *old = current_buffer;
9633 struct text_pos lpoint, opoint, startp;
9634 int update_mode_line;
9635 int tem;
9636 struct it it;
9637 /* Record it now because it's overwritten. */
9638 int current_matrix_up_to_date_p = 0;
9639 int temp_scroll_step = 0;
9640 int count = BINDING_STACK_SIZE ();
9641 int rc;
9642
9643 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9644 opoint = lpoint;
9645
9646 /* W must be a leaf window here. */
9647 xassert (!NILP (w->buffer));
9648 #if GLYPH_DEBUG
9649 *w->desired_matrix->method = 0;
9650 #endif
9651
9652 specbind (Qinhibit_point_motion_hooks, Qt);
9653
9654 reconsider_clip_changes (w, buffer);
9655
9656 /* Has the mode line to be updated? */
9657 update_mode_line = (!NILP (w->update_mode_line)
9658 || update_mode_lines
9659 || buffer->clip_changed);
9660
9661 if (MINI_WINDOW_P (w))
9662 {
9663 if (w == XWINDOW (echo_area_window)
9664 && !NILP (echo_area_buffer[0]))
9665 {
9666 if (update_mode_line)
9667 /* We may have to update a tty frame's menu bar or a
9668 tool-bar. Example `M-x C-h C-h C-g'. */
9669 goto finish_menu_bars;
9670 else
9671 /* We've already displayed the echo area glyphs in this window. */
9672 goto finish_scroll_bars;
9673 }
9674 else if (w != XWINDOW (minibuf_window))
9675 {
9676 /* W is a mini-buffer window, but it's not the currently
9677 active one, so clear it. */
9678 int yb = window_text_bottom_y (w);
9679 struct glyph_row *row;
9680 int y;
9681
9682 for (y = 0, row = w->desired_matrix->rows;
9683 y < yb;
9684 y += row->height, ++row)
9685 blank_row (w, row, y);
9686 goto finish_scroll_bars;
9687 }
9688 }
9689
9690 /* Otherwise set up data on this window; select its buffer and point
9691 value. */
9692 /* Really select the buffer, for the sake of buffer-local
9693 variables. */
9694 set_buffer_internal_1 (XBUFFER (w->buffer));
9695 SET_TEXT_POS (opoint, PT, PT_BYTE);
9696
9697 current_matrix_up_to_date_p
9698 = (!NILP (w->window_end_valid)
9699 && !current_buffer->clip_changed
9700 && XFASTINT (w->last_modified) >= MODIFF
9701 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9702
9703 /* When windows_or_buffers_changed is non-zero, we can't rely on
9704 the window end being valid, so set it to nil there. */
9705 if (windows_or_buffers_changed)
9706 {
9707 /* If window starts on a continuation line, maybe adjust the
9708 window start in case the window's width changed. */
9709 if (XMARKER (w->start)->buffer == current_buffer)
9710 compute_window_start_on_continuation_line (w);
9711
9712 w->window_end_valid = Qnil;
9713 }
9714
9715 /* Some sanity checks. */
9716 CHECK_WINDOW_END (w);
9717 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9718 abort ();
9719 if (BYTEPOS (opoint) < CHARPOS (opoint))
9720 abort ();
9721
9722 /* If %c is in mode line, update it if needed. */
9723 if (!NILP (w->column_number_displayed)
9724 /* This alternative quickly identifies a common case
9725 where no change is needed. */
9726 && !(PT == XFASTINT (w->last_point)
9727 && XFASTINT (w->last_modified) >= MODIFF
9728 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9729 && XFASTINT (w->column_number_displayed) != current_column ())
9730 update_mode_line = 1;
9731
9732 /* Count number of windows showing the selected buffer. An indirect
9733 buffer counts as its base buffer. */
9734 if (!just_this_one_p)
9735 {
9736 struct buffer *current_base, *window_base;
9737 current_base = current_buffer;
9738 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9739 if (current_base->base_buffer)
9740 current_base = current_base->base_buffer;
9741 if (window_base->base_buffer)
9742 window_base = window_base->base_buffer;
9743 if (current_base == window_base)
9744 buffer_shared++;
9745 }
9746
9747 /* Point refers normally to the selected window. For any other
9748 window, set up appropriate value. */
9749 if (!EQ (window, selected_window))
9750 {
9751 int new_pt = XMARKER (w->pointm)->charpos;
9752 int new_pt_byte = marker_byte_position (w->pointm);
9753 if (new_pt < BEGV)
9754 {
9755 new_pt = BEGV;
9756 new_pt_byte = BEGV_BYTE;
9757 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9758 }
9759 else if (new_pt > (ZV - 1))
9760 {
9761 new_pt = ZV;
9762 new_pt_byte = ZV_BYTE;
9763 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9764 }
9765
9766 /* We don't use SET_PT so that the point-motion hooks don't run. */
9767 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9768 }
9769
9770 /* If any of the character widths specified in the display table
9771 have changed, invalidate the width run cache. It's true that
9772 this may be a bit late to catch such changes, but the rest of
9773 redisplay goes (non-fatally) haywire when the display table is
9774 changed, so why should we worry about doing any better? */
9775 if (current_buffer->width_run_cache)
9776 {
9777 struct Lisp_Char_Table *disptab = buffer_display_table ();
9778
9779 if (! disptab_matches_widthtab (disptab,
9780 XVECTOR (current_buffer->width_table)))
9781 {
9782 invalidate_region_cache (current_buffer,
9783 current_buffer->width_run_cache,
9784 BEG, Z);
9785 recompute_width_table (current_buffer, disptab);
9786 }
9787 }
9788
9789 /* If window-start is screwed up, choose a new one. */
9790 if (XMARKER (w->start)->buffer != current_buffer)
9791 goto recenter;
9792
9793 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9794
9795 /* If someone specified a new starting point but did not insist,
9796 check whether it can be used. */
9797 if (!NILP (w->optional_new_start)
9798 && CHARPOS (startp) >= BEGV
9799 && CHARPOS (startp) <= ZV)
9800 {
9801 w->optional_new_start = Qnil;
9802 start_display (&it, w, startp);
9803 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9804 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9805 if (IT_CHARPOS (it) == PT)
9806 w->force_start = Qt;
9807 }
9808
9809 /* Handle case where place to start displaying has been specified,
9810 unless the specified location is outside the accessible range. */
9811 if (!NILP (w->force_start)
9812 || w->frozen_window_start_p)
9813 {
9814 w->force_start = Qnil;
9815 w->vscroll = 0;
9816 w->window_end_valid = Qnil;
9817
9818 /* Forget any recorded base line for line number display. */
9819 if (!current_matrix_up_to_date_p
9820 || current_buffer->clip_changed)
9821 w->base_line_number = Qnil;
9822
9823 /* Redisplay the mode line. Select the buffer properly for that.
9824 Also, run the hook window-scroll-functions
9825 because we have scrolled. */
9826 /* Note, we do this after clearing force_start because
9827 if there's an error, it is better to forget about force_start
9828 than to get into an infinite loop calling the hook functions
9829 and having them get more errors. */
9830 if (!update_mode_line
9831 || ! NILP (Vwindow_scroll_functions))
9832 {
9833 update_mode_line = 1;
9834 w->update_mode_line = Qt;
9835 startp = run_window_scroll_functions (window, startp);
9836 }
9837
9838 XSETFASTINT (w->last_modified, 0);
9839 XSETFASTINT (w->last_overlay_modified, 0);
9840 if (CHARPOS (startp) < BEGV)
9841 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9842 else if (CHARPOS (startp) > ZV)
9843 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9844
9845 /* Redisplay, then check if cursor has been set during the
9846 redisplay. Give up if new fonts were loaded. */
9847 if (!try_window (window, startp))
9848 {
9849 w->force_start = Qt;
9850 clear_glyph_matrix (w->desired_matrix);
9851 goto finish_scroll_bars;
9852 }
9853
9854 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9855 {
9856 /* If point does not appear, try to move point so it does
9857 appear. The desired matrix has been built above, so we
9858 can use it here. */
9859 int window_height;
9860 struct glyph_row *row;
9861
9862 window_height = window_box_height (w) / 2;
9863 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9864 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9865 ++row;
9866
9867 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9868 MATRIX_ROW_START_BYTEPOS (row));
9869
9870 if (w != XWINDOW (selected_window))
9871 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9872 else if (current_buffer == old)
9873 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9874
9875 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9876
9877 /* If we are highlighting the region, then we just changed
9878 the region, so redisplay to show it. */
9879 if (!NILP (Vtransient_mark_mode)
9880 && !NILP (current_buffer->mark_active))
9881 {
9882 clear_glyph_matrix (w->desired_matrix);
9883 if (!try_window (window, startp))
9884 goto finish_scroll_bars;
9885 }
9886 }
9887
9888 make_cursor_line_fully_visible (w);
9889 #if GLYPH_DEBUG
9890 debug_method_add (w, "forced window start");
9891 #endif
9892 goto done;
9893 }
9894
9895 /* Handle case where text has not changed, only point, and it has
9896 not moved off the frame. */
9897 if (current_matrix_up_to_date_p
9898 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9899 rc != 0))
9900 {
9901 if (rc == -1)
9902 goto try_to_scroll;
9903 else
9904 goto done;
9905 }
9906 /* If current starting point was originally the beginning of a line
9907 but no longer is, find a new starting point. */
9908 else if (!NILP (w->start_at_line_beg)
9909 && !(CHARPOS (startp) <= BEGV
9910 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9911 {
9912 #if GLYPH_DEBUG
9913 debug_method_add (w, "recenter 1");
9914 #endif
9915 goto recenter;
9916 }
9917
9918 /* Try scrolling with try_window_id. */
9919 else if (/* Windows and buffers haven't changed. */
9920 !windows_or_buffers_changed
9921 /* Window must be either use window-based redisplay or
9922 be full width. */
9923 && (FRAME_WINDOW_P (f)
9924 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9925 && !MINI_WINDOW_P (w)
9926 /* Point is not known NOT to appear in window. */
9927 && PT >= CHARPOS (startp)
9928 && XFASTINT (w->last_modified)
9929 /* Window is not hscrolled. */
9930 && XFASTINT (w->hscroll) == 0
9931 /* Selective display has not changed. */
9932 && !current_buffer->clip_changed
9933 /* Current matrix is up to date. */
9934 && !NILP (w->window_end_valid)
9935 /* Can't use this case if highlighting a region because
9936 a cursor movement will do more than just set the cursor. */
9937 && !(!NILP (Vtransient_mark_mode)
9938 && !NILP (current_buffer->mark_active))
9939 && NILP (w->region_showing)
9940 && NILP (Vshow_trailing_whitespace)
9941 /* Overlay arrow position and string not changed. */
9942 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9943 && EQ (last_arrow_string, Voverlay_arrow_string)
9944 /* Value is > 0 if update has been done, it is -1 if we
9945 know that the same window start will not work. It is 0
9946 if unsuccessful for some other reason. */
9947 && (tem = try_window_id (w)) != 0)
9948 {
9949 #if GLYPH_DEBUG
9950 debug_method_add (w, "try_window_id %d", tem);
9951 #endif
9952
9953 if (fonts_changed_p)
9954 goto finish_scroll_bars;
9955 if (tem > 0)
9956 goto done;
9957
9958 /* Otherwise try_window_id has returned -1 which means that we
9959 don't want the alternative below this comment to execute. */
9960 }
9961 else if (CHARPOS (startp) >= BEGV
9962 && CHARPOS (startp) <= ZV
9963 && PT >= CHARPOS (startp)
9964 && (CHARPOS (startp) < ZV
9965 /* Avoid starting at end of buffer. */
9966 || CHARPOS (startp) == BEGV
9967 || (XFASTINT (w->last_modified) >= MODIFF
9968 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9969 {
9970 #if GLYPH_DEBUG
9971 debug_method_add (w, "same window start");
9972 #endif
9973
9974 /* Try to redisplay starting at same place as before.
9975 If point has not moved off frame, accept the results. */
9976 if (!current_matrix_up_to_date_p
9977 /* Don't use try_window_reusing_current_matrix in this case
9978 because a window scroll function can have changed the
9979 buffer. */
9980 || !NILP (Vwindow_scroll_functions)
9981 || MINI_WINDOW_P (w)
9982 || !try_window_reusing_current_matrix (w))
9983 {
9984 IF_DEBUG (debug_method_add (w, "1"));
9985 try_window (window, startp);
9986 }
9987
9988 if (fonts_changed_p)
9989 goto finish_scroll_bars;
9990
9991 if (w->cursor.vpos >= 0)
9992 {
9993 if (!just_this_one_p
9994 || current_buffer->clip_changed
9995 || BEG_UNCHANGED < CHARPOS (startp))
9996 /* Forget any recorded base line for line number display. */
9997 w->base_line_number = Qnil;
9998
9999 make_cursor_line_fully_visible (w);
10000 goto done;
10001 }
10002 else
10003 clear_glyph_matrix (w->desired_matrix);
10004 }
10005
10006 try_to_scroll:
10007
10008 XSETFASTINT (w->last_modified, 0);
10009 XSETFASTINT (w->last_overlay_modified, 0);
10010
10011 /* Redisplay the mode line. Select the buffer properly for that. */
10012 if (!update_mode_line)
10013 {
10014 update_mode_line = 1;
10015 w->update_mode_line = Qt;
10016 }
10017
10018 /* Try to scroll by specified few lines. */
10019 if ((scroll_conservatively
10020 || scroll_step
10021 || temp_scroll_step
10022 || NUMBERP (current_buffer->scroll_up_aggressively)
10023 || NUMBERP (current_buffer->scroll_down_aggressively))
10024 && !current_buffer->clip_changed
10025 && CHARPOS (startp) >= BEGV
10026 && CHARPOS (startp) <= ZV)
10027 {
10028 /* The function returns -1 if new fonts were loaded, 1 if
10029 successful, 0 if not successful. */
10030 int rc = try_scrolling (window, just_this_one_p,
10031 scroll_conservatively,
10032 scroll_step,
10033 temp_scroll_step);
10034 if (rc > 0)
10035 goto done;
10036 else if (rc < 0)
10037 goto finish_scroll_bars;
10038 }
10039
10040 /* Finally, just choose place to start which centers point */
10041
10042 recenter:
10043
10044 #if GLYPH_DEBUG
10045 debug_method_add (w, "recenter");
10046 #endif
10047
10048 /* w->vscroll = 0; */
10049
10050 /* Forget any previously recorded base line for line number display. */
10051 if (!current_matrix_up_to_date_p
10052 || current_buffer->clip_changed)
10053 w->base_line_number = Qnil;
10054
10055 /* Move backward half the height of the window. */
10056 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10057 it.current_y = it.last_visible_y;
10058 move_it_vertically_backward (&it, it.last_visible_y / 2);
10059 xassert (IT_CHARPOS (it) >= BEGV);
10060
10061 /* The function move_it_vertically_backward may move over more
10062 than the specified y-distance. If it->w is small, e.g. a
10063 mini-buffer window, we may end up in front of the window's
10064 display area. Start displaying at the start of the line
10065 containing PT in this case. */
10066 if (it.current_y <= 0)
10067 {
10068 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10069 move_it_vertically (&it, 0);
10070 xassert (IT_CHARPOS (it) <= PT);
10071 it.current_y = 0;
10072 }
10073
10074 it.current_x = it.hpos = 0;
10075
10076 /* Set startp here explicitly in case that helps avoid an infinite loop
10077 in case the window-scroll-functions functions get errors. */
10078 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10079
10080 /* Run scroll hooks. */
10081 startp = run_window_scroll_functions (window, it.current.pos);
10082
10083 /* Redisplay the window. */
10084 if (!current_matrix_up_to_date_p
10085 || windows_or_buffers_changed
10086 /* Don't use try_window_reusing_current_matrix in this case
10087 because it can have changed the buffer. */
10088 || !NILP (Vwindow_scroll_functions)
10089 || !just_this_one_p
10090 || MINI_WINDOW_P (w)
10091 || !try_window_reusing_current_matrix (w))
10092 try_window (window, startp);
10093
10094 /* If new fonts have been loaded (due to fontsets), give up. We
10095 have to start a new redisplay since we need to re-adjust glyph
10096 matrices. */
10097 if (fonts_changed_p)
10098 goto finish_scroll_bars;
10099
10100 /* If cursor did not appear assume that the middle of the window is
10101 in the first line of the window. Do it again with the next line.
10102 (Imagine a window of height 100, displaying two lines of height
10103 60. Moving back 50 from it->last_visible_y will end in the first
10104 line.) */
10105 if (w->cursor.vpos < 0)
10106 {
10107 if (!NILP (w->window_end_valid)
10108 && PT >= Z - XFASTINT (w->window_end_pos))
10109 {
10110 clear_glyph_matrix (w->desired_matrix);
10111 move_it_by_lines (&it, 1, 0);
10112 try_window (window, it.current.pos);
10113 }
10114 else if (PT < IT_CHARPOS (it))
10115 {
10116 clear_glyph_matrix (w->desired_matrix);
10117 move_it_by_lines (&it, -1, 0);
10118 try_window (window, it.current.pos);
10119 }
10120 else
10121 {
10122 /* Not much we can do about it. */
10123 }
10124 }
10125
10126 /* Consider the following case: Window starts at BEGV, there is
10127 invisible, intangible text at BEGV, so that display starts at
10128 some point START > BEGV. It can happen that we are called with
10129 PT somewhere between BEGV and START. Try to handle that case. */
10130 if (w->cursor.vpos < 0)
10131 {
10132 struct glyph_row *row = w->current_matrix->rows;
10133 if (row->mode_line_p)
10134 ++row;
10135 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10136 }
10137
10138 make_cursor_line_fully_visible (w);
10139
10140 done:
10141
10142 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10143 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10144 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10145 ? Qt : Qnil);
10146
10147 /* Display the mode line, if we must. */
10148 if ((update_mode_line
10149 /* If window not full width, must redo its mode line
10150 if (a) the window to its side is being redone and
10151 (b) we do a frame-based redisplay. This is a consequence
10152 of how inverted lines are drawn in frame-based redisplay. */
10153 || (!just_this_one_p
10154 && !FRAME_WINDOW_P (f)
10155 && !WINDOW_FULL_WIDTH_P (w))
10156 /* Line number to display. */
10157 || INTEGERP (w->base_line_pos)
10158 /* Column number is displayed and different from the one displayed. */
10159 || (!NILP (w->column_number_displayed)
10160 && XFASTINT (w->column_number_displayed) != current_column ()))
10161 /* This means that the window has a mode line. */
10162 && (WINDOW_WANTS_MODELINE_P (w)
10163 || WINDOW_WANTS_HEADER_LINE_P (w)))
10164 {
10165 Lisp_Object old_selected_frame;
10166
10167 old_selected_frame = selected_frame;
10168
10169 XSETFRAME (selected_frame, f);
10170 display_mode_lines (w);
10171 selected_frame = old_selected_frame;
10172
10173 /* If mode line height has changed, arrange for a thorough
10174 immediate redisplay using the correct mode line height. */
10175 if (WINDOW_WANTS_MODELINE_P (w)
10176 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10177 {
10178 fonts_changed_p = 1;
10179 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10180 = DESIRED_MODE_LINE_HEIGHT (w);
10181 }
10182
10183 /* If top line height has changed, arrange for a thorough
10184 immediate redisplay using the correct mode line height. */
10185 if (WINDOW_WANTS_HEADER_LINE_P (w)
10186 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10187 {
10188 fonts_changed_p = 1;
10189 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10190 = DESIRED_HEADER_LINE_HEIGHT (w);
10191 }
10192
10193 if (fonts_changed_p)
10194 goto finish_scroll_bars;
10195 }
10196
10197 if (!line_number_displayed
10198 && !BUFFERP (w->base_line_pos))
10199 {
10200 w->base_line_pos = Qnil;
10201 w->base_line_number = Qnil;
10202 }
10203
10204 finish_menu_bars:
10205
10206 /* When we reach a frame's selected window, redo the frame's menu bar. */
10207 if (update_mode_line
10208 && EQ (FRAME_SELECTED_WINDOW (f), window))
10209 {
10210 int redisplay_menu_p = 0;
10211
10212 if (FRAME_WINDOW_P (f))
10213 {
10214 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10215 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10216 #else
10217 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10218 #endif
10219 }
10220 else
10221 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10222
10223 if (redisplay_menu_p)
10224 display_menu_bar (w);
10225
10226 #ifdef HAVE_WINDOW_SYSTEM
10227 if (WINDOWP (f->tool_bar_window)
10228 && (FRAME_TOOL_BAR_LINES (f) > 0
10229 || auto_resize_tool_bars_p))
10230 redisplay_tool_bar (f);
10231 #endif
10232 }
10233
10234 finish_scroll_bars:
10235
10236 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10237 {
10238 int start, end, whole;
10239
10240 /* Calculate the start and end positions for the current window.
10241 At some point, it would be nice to choose between scrollbars
10242 which reflect the whole buffer size, with special markers
10243 indicating narrowing, and scrollbars which reflect only the
10244 visible region.
10245
10246 Note that mini-buffers sometimes aren't displaying any text. */
10247 if (!MINI_WINDOW_P (w)
10248 || (w == XWINDOW (minibuf_window)
10249 && NILP (echo_area_buffer[0])))
10250 {
10251 whole = ZV - BEGV;
10252 start = marker_position (w->start) - BEGV;
10253 /* I don't think this is guaranteed to be right. For the
10254 moment, we'll pretend it is. */
10255 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10256
10257 if (end < start)
10258 end = start;
10259 if (whole < (end - start))
10260 whole = end - start;
10261 }
10262 else
10263 start = end = whole = 0;
10264
10265 /* Indicate what this scroll bar ought to be displaying now. */
10266 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10267
10268 /* Note that we actually used the scroll bar attached to this
10269 window, so it shouldn't be deleted at the end of redisplay. */
10270 redeem_scroll_bar_hook (w);
10271 }
10272
10273 /* Restore current_buffer and value of point in it. */
10274 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10275 set_buffer_internal_1 (old);
10276 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10277
10278 unbind_to (count, Qnil);
10279 }
10280
10281
10282 /* Build the complete desired matrix of WINDOW with a window start
10283 buffer position POS. Value is non-zero if successful. It is zero
10284 if fonts were loaded during redisplay which makes re-adjusting
10285 glyph matrices necessary. */
10286
10287 int
10288 try_window (window, pos)
10289 Lisp_Object window;
10290 struct text_pos pos;
10291 {
10292 struct window *w = XWINDOW (window);
10293 struct it it;
10294 struct glyph_row *last_text_row = NULL;
10295
10296 /* Make POS the new window start. */
10297 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10298
10299 /* Mark cursor position as unknown. No overlay arrow seen. */
10300 w->cursor.vpos = -1;
10301 overlay_arrow_seen = 0;
10302
10303 /* Initialize iterator and info to start at POS. */
10304 start_display (&it, w, pos);
10305
10306 /* Display all lines of W. */
10307 while (it.current_y < it.last_visible_y)
10308 {
10309 if (display_line (&it))
10310 last_text_row = it.glyph_row - 1;
10311 if (fonts_changed_p)
10312 return 0;
10313 }
10314
10315 /* If bottom moved off end of frame, change mode line percentage. */
10316 if (XFASTINT (w->window_end_pos) <= 0
10317 && Z != IT_CHARPOS (it))
10318 w->update_mode_line = Qt;
10319
10320 /* Set window_end_pos to the offset of the last character displayed
10321 on the window from the end of current_buffer. Set
10322 window_end_vpos to its row number. */
10323 if (last_text_row)
10324 {
10325 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10326 w->window_end_bytepos
10327 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10328 XSETFASTINT (w->window_end_pos,
10329 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10330 XSETFASTINT (w->window_end_vpos,
10331 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10332 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10333 ->displays_text_p);
10334 }
10335 else
10336 {
10337 w->window_end_bytepos = 0;
10338 XSETFASTINT (w->window_end_pos, 0);
10339 XSETFASTINT (w->window_end_vpos, 0);
10340 }
10341
10342 /* But that is not valid info until redisplay finishes. */
10343 w->window_end_valid = Qnil;
10344 return 1;
10345 }
10346
10347
10348 \f
10349 /************************************************************************
10350 Window redisplay reusing current matrix when buffer has not changed
10351 ************************************************************************/
10352
10353 /* Try redisplay of window W showing an unchanged buffer with a
10354 different window start than the last time it was displayed by
10355 reusing its current matrix. Value is non-zero if successful.
10356 W->start is the new window start. */
10357
10358 static int
10359 try_window_reusing_current_matrix (w)
10360 struct window *w;
10361 {
10362 struct frame *f = XFRAME (w->frame);
10363 struct glyph_row *row, *bottom_row;
10364 struct it it;
10365 struct run run;
10366 struct text_pos start, new_start;
10367 int nrows_scrolled, i;
10368 struct glyph_row *last_text_row;
10369 struct glyph_row *last_reused_text_row;
10370 struct glyph_row *start_row;
10371 int start_vpos, min_y, max_y;
10372
10373 if (/* This function doesn't handle terminal frames. */
10374 !FRAME_WINDOW_P (f)
10375 /* Don't try to reuse the display if windows have been split
10376 or such. */
10377 || windows_or_buffers_changed)
10378 return 0;
10379
10380 /* Can't do this if region may have changed. */
10381 if ((!NILP (Vtransient_mark_mode)
10382 && !NILP (current_buffer->mark_active))
10383 || !NILP (w->region_showing)
10384 || !NILP (Vshow_trailing_whitespace))
10385 return 0;
10386
10387 /* If top-line visibility has changed, give up. */
10388 if (WINDOW_WANTS_HEADER_LINE_P (w)
10389 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10390 return 0;
10391
10392 /* Give up if old or new display is scrolled vertically. We could
10393 make this function handle this, but right now it doesn't. */
10394 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10395 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10396 return 0;
10397
10398 /* The variable new_start now holds the new window start. The old
10399 start `start' can be determined from the current matrix. */
10400 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10401 start = start_row->start.pos;
10402 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10403
10404 /* Clear the desired matrix for the display below. */
10405 clear_glyph_matrix (w->desired_matrix);
10406
10407 if (CHARPOS (new_start) <= CHARPOS (start))
10408 {
10409 int first_row_y;
10410
10411 IF_DEBUG (debug_method_add (w, "twu1"));
10412
10413 /* Display up to a row that can be reused. The variable
10414 last_text_row is set to the last row displayed that displays
10415 text. Note that it.vpos == 0 if or if not there is a
10416 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10417 start_display (&it, w, new_start);
10418 first_row_y = it.current_y;
10419 w->cursor.vpos = -1;
10420 last_text_row = last_reused_text_row = NULL;
10421
10422 while (it.current_y < it.last_visible_y
10423 && IT_CHARPOS (it) < CHARPOS (start)
10424 && !fonts_changed_p)
10425 if (display_line (&it))
10426 last_text_row = it.glyph_row - 1;
10427
10428 /* A value of current_y < last_visible_y means that we stopped
10429 at the previous window start, which in turn means that we
10430 have at least one reusable row. */
10431 if (it.current_y < it.last_visible_y)
10432 {
10433 /* IT.vpos always starts from 0; it counts text lines. */
10434 nrows_scrolled = it.vpos;
10435
10436 /* Find PT if not already found in the lines displayed. */
10437 if (w->cursor.vpos < 0)
10438 {
10439 int dy = it.current_y - first_row_y;
10440
10441 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10442 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10443 {
10444 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10445 && PT < MATRIX_ROW_END_CHARPOS (row))
10446 {
10447 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10448 dy, nrows_scrolled);
10449 break;
10450 }
10451
10452 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10453 break;
10454
10455 ++row;
10456 }
10457
10458 /* Give up if point was not found. This shouldn't
10459 happen often; not more often than with try_window
10460 itself. */
10461 if (w->cursor.vpos < 0)
10462 {
10463 clear_glyph_matrix (w->desired_matrix);
10464 return 0;
10465 }
10466 }
10467
10468 /* Scroll the display. Do it before the current matrix is
10469 changed. The problem here is that update has not yet
10470 run, i.e. part of the current matrix is not up to date.
10471 scroll_run_hook will clear the cursor, and use the
10472 current matrix to get the height of the row the cursor is
10473 in. */
10474 run.current_y = first_row_y;
10475 run.desired_y = it.current_y;
10476 run.height = it.last_visible_y - it.current_y;
10477
10478 if (run.height > 0 && run.current_y != run.desired_y)
10479 {
10480 update_begin (f);
10481 rif->update_window_begin_hook (w);
10482 rif->clear_mouse_face (w);
10483 rif->scroll_run_hook (w, &run);
10484 rif->update_window_end_hook (w, 0, 0);
10485 update_end (f);
10486 }
10487
10488 /* Shift current matrix down by nrows_scrolled lines. */
10489 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10490 rotate_matrix (w->current_matrix,
10491 start_vpos,
10492 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10493 nrows_scrolled);
10494
10495 /* Disable lines that must be updated. */
10496 for (i = 0; i < it.vpos; ++i)
10497 (start_row + i)->enabled_p = 0;
10498
10499 /* Re-compute Y positions. */
10500 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10501 max_y = it.last_visible_y;
10502 for (row = start_row + nrows_scrolled;
10503 row < bottom_row;
10504 ++row)
10505 {
10506 row->y = it.current_y;
10507
10508 if (row->y < min_y)
10509 row->visible_height = row->height - (min_y - row->y);
10510 else if (row->y + row->height > max_y)
10511 row->visible_height
10512 = row->height - (row->y + row->height - max_y);
10513 else
10514 row->visible_height = row->height;
10515
10516 it.current_y += row->height;
10517
10518 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10519 last_reused_text_row = row;
10520 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10521 break;
10522 }
10523
10524 /* Disable lines in the current matrix which are now
10525 below the window. */
10526 for (++row; row < bottom_row; ++row)
10527 row->enabled_p = 0;
10528 }
10529
10530 /* Update window_end_pos etc.; last_reused_text_row is the last
10531 reused row from the current matrix containing text, if any.
10532 The value of last_text_row is the last displayed line
10533 containing text. */
10534 if (last_reused_text_row)
10535 {
10536 w->window_end_bytepos
10537 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10538 XSETFASTINT (w->window_end_pos,
10539 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10540 XSETFASTINT (w->window_end_vpos,
10541 MATRIX_ROW_VPOS (last_reused_text_row,
10542 w->current_matrix));
10543 }
10544 else if (last_text_row)
10545 {
10546 w->window_end_bytepos
10547 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10548 XSETFASTINT (w->window_end_pos,
10549 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10550 XSETFASTINT (w->window_end_vpos,
10551 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10552 }
10553 else
10554 {
10555 /* This window must be completely empty. */
10556 w->window_end_bytepos = 0;
10557 XSETFASTINT (w->window_end_pos, 0);
10558 XSETFASTINT (w->window_end_vpos, 0);
10559 }
10560 w->window_end_valid = Qnil;
10561
10562 /* Update hint: don't try scrolling again in update_window. */
10563 w->desired_matrix->no_scrolling_p = 1;
10564
10565 #if GLYPH_DEBUG
10566 debug_method_add (w, "try_window_reusing_current_matrix 1");
10567 #endif
10568 return 1;
10569 }
10570 else if (CHARPOS (new_start) > CHARPOS (start))
10571 {
10572 struct glyph_row *pt_row, *row;
10573 struct glyph_row *first_reusable_row;
10574 struct glyph_row *first_row_to_display;
10575 int dy;
10576 int yb = window_text_bottom_y (w);
10577
10578 IF_DEBUG (debug_method_add (w, "twu2"));
10579
10580 /* Find the row starting at new_start, if there is one. Don't
10581 reuse a partially visible line at the end. */
10582 first_reusable_row = start_row;
10583 while (first_reusable_row->enabled_p
10584 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10585 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10586 < CHARPOS (new_start)))
10587 ++first_reusable_row;
10588
10589 /* Give up if there is no row to reuse. */
10590 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10591 || !first_reusable_row->enabled_p
10592 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10593 != CHARPOS (new_start)))
10594 return 0;
10595
10596 /* We can reuse fully visible rows beginning with
10597 first_reusable_row to the end of the window. Set
10598 first_row_to_display to the first row that cannot be reused.
10599 Set pt_row to the row containing point, if there is any. */
10600 first_row_to_display = first_reusable_row;
10601 pt_row = NULL;
10602 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10603 {
10604 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10605 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10606 pt_row = first_row_to_display;
10607
10608 ++first_row_to_display;
10609 }
10610
10611 /* Start displaying at the start of first_row_to_display. */
10612 xassert (first_row_to_display->y < yb);
10613 init_to_row_start (&it, w, first_row_to_display);
10614 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10615 - start_vpos);
10616 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10617 - nrows_scrolled);
10618 it.current_y = (first_row_to_display->y - first_reusable_row->y
10619 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10620
10621 /* Display lines beginning with first_row_to_display in the
10622 desired matrix. Set last_text_row to the last row displayed
10623 that displays text. */
10624 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10625 if (pt_row == NULL)
10626 w->cursor.vpos = -1;
10627 last_text_row = NULL;
10628 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10629 if (display_line (&it))
10630 last_text_row = it.glyph_row - 1;
10631
10632 /* Give up If point isn't in a row displayed or reused. */
10633 if (w->cursor.vpos < 0)
10634 {
10635 clear_glyph_matrix (w->desired_matrix);
10636 return 0;
10637 }
10638
10639 /* If point is in a reused row, adjust y and vpos of the cursor
10640 position. */
10641 if (pt_row)
10642 {
10643 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10644 w->current_matrix);
10645 w->cursor.y -= first_reusable_row->y;
10646 }
10647
10648 /* Scroll the display. */
10649 run.current_y = first_reusable_row->y;
10650 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10651 run.height = it.last_visible_y - run.current_y;
10652 dy = run.current_y - run.desired_y;
10653
10654 if (run.height)
10655 {
10656 struct frame *f = XFRAME (WINDOW_FRAME (w));
10657 update_begin (f);
10658 rif->update_window_begin_hook (w);
10659 rif->clear_mouse_face (w);
10660 rif->scroll_run_hook (w, &run);
10661 rif->update_window_end_hook (w, 0, 0);
10662 update_end (f);
10663 }
10664
10665 /* Adjust Y positions of reused rows. */
10666 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10667 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10668 max_y = it.last_visible_y;
10669 for (row = first_reusable_row; row < first_row_to_display; ++row)
10670 {
10671 row->y -= dy;
10672 if (row->y < min_y)
10673 row->visible_height = row->height - (min_y - row->y);
10674 else if (row->y + row->height > max_y)
10675 row->visible_height
10676 = row->height - (row->y + row->height - max_y);
10677 else
10678 row->visible_height = row->height;
10679 }
10680
10681 /* Disable rows not reused. */
10682 while (row < bottom_row)
10683 {
10684 row->enabled_p = 0;
10685 ++row;
10686 }
10687
10688 /* Scroll the current matrix. */
10689 xassert (nrows_scrolled > 0);
10690 rotate_matrix (w->current_matrix,
10691 start_vpos,
10692 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10693 -nrows_scrolled);
10694
10695 /* Adjust window end. A null value of last_text_row means that
10696 the window end is in reused rows which in turn means that
10697 only its vpos can have changed. */
10698 if (last_text_row)
10699 {
10700 w->window_end_bytepos
10701 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10702 XSETFASTINT (w->window_end_pos,
10703 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10704 XSETFASTINT (w->window_end_vpos,
10705 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10706 }
10707 else
10708 {
10709 XSETFASTINT (w->window_end_vpos,
10710 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10711 }
10712
10713 w->window_end_valid = Qnil;
10714 w->desired_matrix->no_scrolling_p = 1;
10715
10716 #if GLYPH_DEBUG
10717 debug_method_add (w, "try_window_reusing_current_matrix 2");
10718 #endif
10719 return 1;
10720 }
10721
10722 return 0;
10723 }
10724
10725
10726 \f
10727 /************************************************************************
10728 Window redisplay reusing current matrix when buffer has changed
10729 ************************************************************************/
10730
10731 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10732 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10733 int *, int *));
10734 static struct glyph_row *
10735 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10736 struct glyph_row *));
10737
10738
10739 /* Return the last row in MATRIX displaying text. If row START is
10740 non-null, start searching with that row. IT gives the dimensions
10741 of the display. Value is null if matrix is empty; otherwise it is
10742 a pointer to the row found. */
10743
10744 static struct glyph_row *
10745 find_last_row_displaying_text (matrix, it, start)
10746 struct glyph_matrix *matrix;
10747 struct it *it;
10748 struct glyph_row *start;
10749 {
10750 struct glyph_row *row, *row_found;
10751
10752 /* Set row_found to the last row in IT->w's current matrix
10753 displaying text. The loop looks funny but think of partially
10754 visible lines. */
10755 row_found = NULL;
10756 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10757 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10758 {
10759 xassert (row->enabled_p);
10760 row_found = row;
10761 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10762 break;
10763 ++row;
10764 }
10765
10766 return row_found;
10767 }
10768
10769
10770 /* Return the last row in the current matrix of W that is not affected
10771 by changes at the start of current_buffer that occurred since the
10772 last time W was redisplayed. Value is null if no such row exists.
10773
10774 The global variable beg_unchanged has to contain the number of
10775 bytes unchanged at the start of current_buffer. BEG +
10776 beg_unchanged is the buffer position of the first changed byte in
10777 current_buffer. Characters at positions < BEG + beg_unchanged are
10778 at the same buffer positions as they were when the current matrix
10779 was built. */
10780
10781 static struct glyph_row *
10782 find_last_unchanged_at_beg_row (w)
10783 struct window *w;
10784 {
10785 int first_changed_pos = BEG + BEG_UNCHANGED;
10786 struct glyph_row *row;
10787 struct glyph_row *row_found = NULL;
10788 int yb = window_text_bottom_y (w);
10789
10790 /* Find the last row displaying unchanged text. */
10791 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10792 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10793 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10794 {
10795 if (/* If row ends before first_changed_pos, it is unchanged,
10796 except in some case. */
10797 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10798 /* When row ends in ZV and we write at ZV it is not
10799 unchanged. */
10800 && !row->ends_at_zv_p
10801 /* When first_changed_pos is the end of a continued line,
10802 row is not unchanged because it may be no longer
10803 continued. */
10804 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10805 && row->continued_p))
10806 row_found = row;
10807
10808 /* Stop if last visible row. */
10809 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10810 break;
10811
10812 ++row;
10813 }
10814
10815 return row_found;
10816 }
10817
10818
10819 /* Find the first glyph row in the current matrix of W that is not
10820 affected by changes at the end of current_buffer since the last
10821 time the window was redisplayed. Return in *DELTA the number of
10822 chars by which buffer positions in unchanged text at the end of
10823 current_buffer must be adjusted. Return in *DELTA_BYTES the
10824 corresponding number of bytes. Value is null if no such row
10825 exists, i.e. all rows are affected by changes. */
10826
10827 static struct glyph_row *
10828 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10829 struct window *w;
10830 int *delta, *delta_bytes;
10831 {
10832 struct glyph_row *row;
10833 struct glyph_row *row_found = NULL;
10834
10835 *delta = *delta_bytes = 0;
10836
10837 /* Display must not have been paused, otherwise the current matrix
10838 is not up to date. */
10839 if (NILP (w->window_end_valid))
10840 abort ();
10841
10842 /* A value of window_end_pos >= END_UNCHANGED means that the window
10843 end is in the range of changed text. If so, there is no
10844 unchanged row at the end of W's current matrix. */
10845 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10846 return NULL;
10847
10848 /* Set row to the last row in W's current matrix displaying text. */
10849 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10850
10851 /* If matrix is entirely empty, no unchanged row exists. */
10852 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10853 {
10854 /* The value of row is the last glyph row in the matrix having a
10855 meaningful buffer position in it. The end position of row
10856 corresponds to window_end_pos. This allows us to translate
10857 buffer positions in the current matrix to current buffer
10858 positions for characters not in changed text. */
10859 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10860 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10861 int last_unchanged_pos, last_unchanged_pos_old;
10862 struct glyph_row *first_text_row
10863 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10864
10865 *delta = Z - Z_old;
10866 *delta_bytes = Z_BYTE - Z_BYTE_old;
10867
10868 /* Set last_unchanged_pos to the buffer position of the last
10869 character in the buffer that has not been changed. Z is the
10870 index + 1 of the last byte in current_buffer, i.e. by
10871 subtracting end_unchanged we get the index of the last
10872 unchanged character, and we have to add BEG to get its buffer
10873 position. */
10874 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10875 last_unchanged_pos_old = last_unchanged_pos - *delta;
10876
10877 /* Search backward from ROW for a row displaying a line that
10878 starts at a minimum position >= last_unchanged_pos_old. */
10879 for (; row > first_text_row; --row)
10880 {
10881 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10882 abort ();
10883
10884 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10885 row_found = row;
10886 }
10887 }
10888
10889 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10890 abort ();
10891
10892 return row_found;
10893 }
10894
10895
10896 /* Make sure that glyph rows in the current matrix of window W
10897 reference the same glyph memory as corresponding rows in the
10898 frame's frame matrix. This function is called after scrolling W's
10899 current matrix on a terminal frame in try_window_id and
10900 try_window_reusing_current_matrix. */
10901
10902 static void
10903 sync_frame_with_window_matrix_rows (w)
10904 struct window *w;
10905 {
10906 struct frame *f = XFRAME (w->frame);
10907 struct glyph_row *window_row, *window_row_end, *frame_row;
10908
10909 /* Preconditions: W must be a leaf window and full-width. Its frame
10910 must have a frame matrix. */
10911 xassert (NILP (w->hchild) && NILP (w->vchild));
10912 xassert (WINDOW_FULL_WIDTH_P (w));
10913 xassert (!FRAME_WINDOW_P (f));
10914
10915 /* If W is a full-width window, glyph pointers in W's current matrix
10916 have, by definition, to be the same as glyph pointers in the
10917 corresponding frame matrix. */
10918 window_row = w->current_matrix->rows;
10919 window_row_end = window_row + w->current_matrix->nrows;
10920 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10921 while (window_row < window_row_end)
10922 {
10923 int area;
10924
10925 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10926 frame_row->glyphs[area] = window_row->glyphs[area];
10927
10928 /* Disable frame rows whose corresponding window rows have
10929 been disabled in try_window_id. */
10930 if (!window_row->enabled_p)
10931 frame_row->enabled_p = 0;
10932
10933 ++window_row, ++frame_row;
10934 }
10935 }
10936
10937
10938 /* Find the glyph row in window W containing CHARPOS. Consider all
10939 rows between START and END (not inclusive). END null means search
10940 all rows to the end of the display area of W. Value is the row
10941 containing CHARPOS or null. */
10942
10943 static struct glyph_row *
10944 row_containing_pos (w, charpos, start, end)
10945 struct window *w;
10946 int charpos;
10947 struct glyph_row *start, *end;
10948 {
10949 struct glyph_row *row = start;
10950 int last_y;
10951
10952 /* If we happen to start on a header-line, skip that. */
10953 if (row->mode_line_p)
10954 ++row;
10955
10956 if ((end && row >= end) || !row->enabled_p)
10957 return NULL;
10958
10959 last_y = window_text_bottom_y (w);
10960
10961 while ((end == NULL || row < end)
10962 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10963 /* The end position of a row equals the start
10964 position of the next row. If CHARPOS is there, we
10965 would rather display it in the next line, except
10966 when this line ends in ZV. */
10967 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10968 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10969 || !row->ends_at_zv_p)))
10970 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10971 ++row;
10972
10973 /* Give up if CHARPOS not found. */
10974 if ((end && row >= end)
10975 || charpos < MATRIX_ROW_START_CHARPOS (row)
10976 || charpos > MATRIX_ROW_END_CHARPOS (row))
10977 row = NULL;
10978
10979 return row;
10980 }
10981
10982
10983 /* Try to redisplay window W by reusing its existing display. W's
10984 current matrix must be up to date when this function is called,
10985 i.e. window_end_valid must not be nil.
10986
10987 Value is
10988
10989 1 if display has been updated
10990 0 if otherwise unsuccessful
10991 -1 if redisplay with same window start is known not to succeed
10992
10993 The following steps are performed:
10994
10995 1. Find the last row in the current matrix of W that is not
10996 affected by changes at the start of current_buffer. If no such row
10997 is found, give up.
10998
10999 2. Find the first row in W's current matrix that is not affected by
11000 changes at the end of current_buffer. Maybe there is no such row.
11001
11002 3. Display lines beginning with the row + 1 found in step 1 to the
11003 row found in step 2 or, if step 2 didn't find a row, to the end of
11004 the window.
11005
11006 4. If cursor is not known to appear on the window, give up.
11007
11008 5. If display stopped at the row found in step 2, scroll the
11009 display and current matrix as needed.
11010
11011 6. Maybe display some lines at the end of W, if we must. This can
11012 happen under various circumstances, like a partially visible line
11013 becoming fully visible, or because newly displayed lines are displayed
11014 in smaller font sizes.
11015
11016 7. Update W's window end information. */
11017
11018 /* Check that window end is what we expect it to be. */
11019
11020 static int
11021 try_window_id (w)
11022 struct window *w;
11023 {
11024 struct frame *f = XFRAME (w->frame);
11025 struct glyph_matrix *current_matrix = w->current_matrix;
11026 struct glyph_matrix *desired_matrix = w->desired_matrix;
11027 struct glyph_row *last_unchanged_at_beg_row;
11028 struct glyph_row *first_unchanged_at_end_row;
11029 struct glyph_row *row;
11030 struct glyph_row *bottom_row;
11031 int bottom_vpos;
11032 struct it it;
11033 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11034 struct text_pos start_pos;
11035 struct run run;
11036 int first_unchanged_at_end_vpos = 0;
11037 struct glyph_row *last_text_row, *last_text_row_at_end;
11038 struct text_pos start;
11039
11040 SET_TEXT_POS_FROM_MARKER (start, w->start);
11041
11042 /* Check pre-conditions. Window end must be valid, otherwise
11043 the current matrix would not be up to date. */
11044 xassert (!NILP (w->window_end_valid));
11045 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11046 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11047
11048 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11049 only if buffer has really changed. The reason is that the gap is
11050 initially at Z for freshly visited files. The code below would
11051 set end_unchanged to 0 in that case. */
11052 if (MODIFF > SAVE_MODIFF
11053 /* This seems to happen sometimes after saving a buffer. */
11054 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11055 {
11056 if (GPT - BEG < BEG_UNCHANGED)
11057 BEG_UNCHANGED = GPT - BEG;
11058 if (Z - GPT < END_UNCHANGED)
11059 END_UNCHANGED = Z - GPT;
11060 }
11061
11062 /* If window starts after a line end, and the last change is in
11063 front of that newline, then changes don't affect the display.
11064 This case happens with stealth-fontification. Note that although
11065 the display is unchanged, glyph positions in the matrix have to
11066 be adjusted, of course. */
11067 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11068 if (CHARPOS (start) > BEGV
11069 && Z - END_UNCHANGED < CHARPOS (start) - 1
11070 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11071 && PT < MATRIX_ROW_END_CHARPOS (row))
11072 {
11073 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11074 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11075
11076 if (delta)
11077 {
11078 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11079 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11080
11081 increment_matrix_positions (w->current_matrix,
11082 MATRIX_ROW_VPOS (r0, current_matrix),
11083 MATRIX_ROW_VPOS (r1, current_matrix),
11084 delta, delta_bytes);
11085 }
11086
11087 #if 0 /* If changes are all in front of the window start, the
11088 distance of the last displayed glyph from Z hasn't
11089 changed. */
11090 w->window_end_pos
11091 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11092 w->window_end_bytepos
11093 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11094 #endif
11095
11096 row = row_containing_pos (w, PT, r0, NULL);
11097 if (row == NULL)
11098 return 0;
11099
11100 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11101 return 1;
11102 }
11103
11104 /* Return quickly if changes are all below what is displayed in the
11105 window, and if PT is in the window. */
11106 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
11107 && PT < MATRIX_ROW_END_CHARPOS (row))
11108 {
11109 /* We have to update window end positions because the buffer's
11110 size has changed. */
11111 w->window_end_pos
11112 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11113 w->window_end_bytepos
11114 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11115
11116 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11117 row = row_containing_pos (w, PT, row, NULL);
11118 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11119 return 2;
11120 }
11121
11122 /* Check that window start agrees with the start of the first glyph
11123 row in its current matrix. Check this after we know the window
11124 start is not in changed text, otherwise positions would not be
11125 comparable. */
11126 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11127 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11128 return 0;
11129
11130 /* Compute the position at which we have to start displaying new
11131 lines. Some of the lines at the top of the window might be
11132 reusable because they are not displaying changed text. Find the
11133 last row in W's current matrix not affected by changes at the
11134 start of current_buffer. Value is null if changes start in the
11135 first line of window. */
11136 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11137 if (last_unchanged_at_beg_row)
11138 {
11139 /* Avoid starting to display in the moddle of a character, a TAB
11140 for instance. This is easier than to set up the iterator
11141 exactly, and it's not a frequent case, so the additional
11142 effort wouldn't really pay off. */
11143 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11144 && last_unchanged_at_beg_row > w->current_matrix->rows)
11145 --last_unchanged_at_beg_row;
11146
11147 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11148 return 0;
11149
11150 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11151 start_pos = it.current.pos;
11152
11153 /* Start displaying new lines in the desired matrix at the same
11154 vpos we would use in the current matrix, i.e. below
11155 last_unchanged_at_beg_row. */
11156 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11157 current_matrix);
11158 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11159 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11160
11161 xassert (it.hpos == 0 && it.current_x == 0);
11162 }
11163 else
11164 {
11165 /* There are no reusable lines at the start of the window.
11166 Start displaying in the first line. */
11167 start_display (&it, w, start);
11168 start_pos = it.current.pos;
11169 }
11170
11171 /* Find the first row that is not affected by changes at the end of
11172 the buffer. Value will be null if there is no unchanged row, in
11173 which case we must redisplay to the end of the window. delta
11174 will be set to the value by which buffer positions beginning with
11175 first_unchanged_at_end_row have to be adjusted due to text
11176 changes. */
11177 first_unchanged_at_end_row
11178 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11179 IF_DEBUG (debug_delta = delta);
11180 IF_DEBUG (debug_delta_bytes = delta_bytes);
11181
11182 /* Set stop_pos to the buffer position up to which we will have to
11183 display new lines. If first_unchanged_at_end_row != NULL, this
11184 is the buffer position of the start of the line displayed in that
11185 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11186 that we don't stop at a buffer position. */
11187 stop_pos = 0;
11188 if (first_unchanged_at_end_row)
11189 {
11190 xassert (last_unchanged_at_beg_row == NULL
11191 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11192
11193 /* If this is a continuation line, move forward to the next one
11194 that isn't. Changes in lines above affect this line.
11195 Caution: this may move first_unchanged_at_end_row to a row
11196 not displaying text. */
11197 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11198 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11199 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11200 < it.last_visible_y))
11201 ++first_unchanged_at_end_row;
11202
11203 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11204 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11205 >= it.last_visible_y))
11206 first_unchanged_at_end_row = NULL;
11207 else
11208 {
11209 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11210 + delta);
11211 first_unchanged_at_end_vpos
11212 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11213 xassert (stop_pos >= Z - END_UNCHANGED);
11214 }
11215 }
11216 else if (last_unchanged_at_beg_row == NULL)
11217 return 0;
11218
11219
11220 #if GLYPH_DEBUG
11221
11222 /* Either there is no unchanged row at the end, or the one we have
11223 now displays text. This is a necessary condition for the window
11224 end pos calculation at the end of this function. */
11225 xassert (first_unchanged_at_end_row == NULL
11226 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11227
11228 debug_last_unchanged_at_beg_vpos
11229 = (last_unchanged_at_beg_row
11230 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11231 : -1);
11232 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11233
11234 #endif /* GLYPH_DEBUG != 0 */
11235
11236
11237 /* Display new lines. Set last_text_row to the last new line
11238 displayed which has text on it, i.e. might end up as being the
11239 line where the window_end_vpos is. */
11240 w->cursor.vpos = -1;
11241 last_text_row = NULL;
11242 overlay_arrow_seen = 0;
11243 while (it.current_y < it.last_visible_y
11244 && !fonts_changed_p
11245 && (first_unchanged_at_end_row == NULL
11246 || IT_CHARPOS (it) < stop_pos))
11247 {
11248 if (display_line (&it))
11249 last_text_row = it.glyph_row - 1;
11250 }
11251
11252 if (fonts_changed_p)
11253 return -1;
11254
11255
11256 /* Compute differences in buffer positions, y-positions etc. for
11257 lines reused at the bottom of the window. Compute what we can
11258 scroll. */
11259 if (first_unchanged_at_end_row
11260 /* No lines reused because we displayed everything up to the
11261 bottom of the window. */
11262 && it.current_y < it.last_visible_y)
11263 {
11264 dvpos = (it.vpos
11265 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11266 current_matrix));
11267 dy = it.current_y - first_unchanged_at_end_row->y;
11268 run.current_y = first_unchanged_at_end_row->y;
11269 run.desired_y = run.current_y + dy;
11270 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11271 }
11272 else
11273 {
11274 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11275 first_unchanged_at_end_row = NULL;
11276 }
11277 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11278
11279
11280 /* Find the cursor if not already found. We have to decide whether
11281 PT will appear on this window (it sometimes doesn't, but this is
11282 not a very frequent case.) This decision has to be made before
11283 the current matrix is altered. A value of cursor.vpos < 0 means
11284 that PT is either in one of the lines beginning at
11285 first_unchanged_at_end_row or below the window. Don't care for
11286 lines that might be displayed later at the window end; as
11287 mentioned, this is not a frequent case. */
11288 if (w->cursor.vpos < 0)
11289 {
11290 /* Cursor in unchanged rows at the top? */
11291 if (PT < CHARPOS (start_pos)
11292 && last_unchanged_at_beg_row)
11293 {
11294 row = row_containing_pos (w, PT,
11295 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11296 last_unchanged_at_beg_row + 1);
11297 if (row)
11298 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11299 }
11300
11301 /* Start from first_unchanged_at_end_row looking for PT. */
11302 else if (first_unchanged_at_end_row)
11303 {
11304 row = row_containing_pos (w, PT - delta,
11305 first_unchanged_at_end_row, NULL);
11306 if (row)
11307 set_cursor_from_row (w, row, w->current_matrix, delta,
11308 delta_bytes, dy, dvpos);
11309 }
11310
11311 /* Give up if cursor was not found. */
11312 if (w->cursor.vpos < 0)
11313 {
11314 clear_glyph_matrix (w->desired_matrix);
11315 return -1;
11316 }
11317 }
11318
11319 /* Don't let the cursor end in the scroll margins. */
11320 {
11321 int this_scroll_margin, cursor_height;
11322
11323 this_scroll_margin = max (0, scroll_margin);
11324 this_scroll_margin = min (this_scroll_margin,
11325 XFASTINT (w->height) / 4);
11326 this_scroll_margin *= CANON_Y_UNIT (it.f);
11327 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11328
11329 if ((w->cursor.y < this_scroll_margin
11330 && CHARPOS (start) > BEGV)
11331 /* Don't take scroll margin into account at the bottom because
11332 old redisplay didn't do it either. */
11333 || w->cursor.y + cursor_height > it.last_visible_y)
11334 {
11335 w->cursor.vpos = -1;
11336 clear_glyph_matrix (w->desired_matrix);
11337 return -1;
11338 }
11339 }
11340
11341 /* Scroll the display. Do it before changing the current matrix so
11342 that xterm.c doesn't get confused about where the cursor glyph is
11343 found. */
11344 if (dy && run.height)
11345 {
11346 update_begin (f);
11347
11348 if (FRAME_WINDOW_P (f))
11349 {
11350 rif->update_window_begin_hook (w);
11351 rif->clear_mouse_face (w);
11352 rif->scroll_run_hook (w, &run);
11353 rif->update_window_end_hook (w, 0, 0);
11354 }
11355 else
11356 {
11357 /* Terminal frame. In this case, dvpos gives the number of
11358 lines to scroll by; dvpos < 0 means scroll up. */
11359 int first_unchanged_at_end_vpos
11360 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11361 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11362 int end = XFASTINT (w->top) + window_internal_height (w);
11363
11364 /* Perform the operation on the screen. */
11365 if (dvpos > 0)
11366 {
11367 /* Scroll last_unchanged_at_beg_row to the end of the
11368 window down dvpos lines. */
11369 set_terminal_window (end);
11370
11371 /* On dumb terminals delete dvpos lines at the end
11372 before inserting dvpos empty lines. */
11373 if (!scroll_region_ok)
11374 ins_del_lines (end - dvpos, -dvpos);
11375
11376 /* Insert dvpos empty lines in front of
11377 last_unchanged_at_beg_row. */
11378 ins_del_lines (from, dvpos);
11379 }
11380 else if (dvpos < 0)
11381 {
11382 /* Scroll up last_unchanged_at_beg_vpos to the end of
11383 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11384 set_terminal_window (end);
11385
11386 /* Delete dvpos lines in front of
11387 last_unchanged_at_beg_vpos. ins_del_lines will set
11388 the cursor to the given vpos and emit |dvpos| delete
11389 line sequences. */
11390 ins_del_lines (from + dvpos, dvpos);
11391
11392 /* On a dumb terminal insert dvpos empty lines at the
11393 end. */
11394 if (!scroll_region_ok)
11395 ins_del_lines (end + dvpos, -dvpos);
11396 }
11397
11398 set_terminal_window (0);
11399 }
11400
11401 update_end (f);
11402 }
11403
11404 /* Shift reused rows of the current matrix to the right position.
11405 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11406 text. */
11407 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11408 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11409 if (dvpos < 0)
11410 {
11411 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11412 bottom_vpos, dvpos);
11413 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11414 bottom_vpos, 0);
11415 }
11416 else if (dvpos > 0)
11417 {
11418 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11419 bottom_vpos, dvpos);
11420 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11421 first_unchanged_at_end_vpos + dvpos, 0);
11422 }
11423
11424 /* For frame-based redisplay, make sure that current frame and window
11425 matrix are in sync with respect to glyph memory. */
11426 if (!FRAME_WINDOW_P (f))
11427 sync_frame_with_window_matrix_rows (w);
11428
11429 /* Adjust buffer positions in reused rows. */
11430 if (delta)
11431 increment_matrix_positions (current_matrix,
11432 first_unchanged_at_end_vpos + dvpos,
11433 bottom_vpos, delta, delta_bytes);
11434
11435 /* Adjust Y positions. */
11436 if (dy)
11437 shift_glyph_matrix (w, current_matrix,
11438 first_unchanged_at_end_vpos + dvpos,
11439 bottom_vpos, dy);
11440
11441 if (first_unchanged_at_end_row)
11442 first_unchanged_at_end_row += dvpos;
11443
11444 /* If scrolling up, there may be some lines to display at the end of
11445 the window. */
11446 last_text_row_at_end = NULL;
11447 if (dy < 0)
11448 {
11449 /* Set last_row to the glyph row in the current matrix where the
11450 window end line is found. It has been moved up or down in
11451 the matrix by dvpos. */
11452 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11453 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11454
11455 /* If last_row is the window end line, it should display text. */
11456 xassert (last_row->displays_text_p);
11457
11458 /* If window end line was partially visible before, begin
11459 displaying at that line. Otherwise begin displaying with the
11460 line following it. */
11461 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11462 {
11463 init_to_row_start (&it, w, last_row);
11464 it.vpos = last_vpos;
11465 it.current_y = last_row->y;
11466 }
11467 else
11468 {
11469 init_to_row_end (&it, w, last_row);
11470 it.vpos = 1 + last_vpos;
11471 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11472 ++last_row;
11473 }
11474
11475 /* We may start in a continuation line. If so, we have to get
11476 the right continuation_lines_width and current_x. */
11477 it.continuation_lines_width = last_row->continuation_lines_width;
11478 it.hpos = it.current_x = 0;
11479
11480 /* Display the rest of the lines at the window end. */
11481 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11482 while (it.current_y < it.last_visible_y
11483 && !fonts_changed_p)
11484 {
11485 /* Is it always sure that the display agrees with lines in
11486 the current matrix? I don't think so, so we mark rows
11487 displayed invalid in the current matrix by setting their
11488 enabled_p flag to zero. */
11489 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11490 if (display_line (&it))
11491 last_text_row_at_end = it.glyph_row - 1;
11492 }
11493 }
11494
11495 /* Update window_end_pos and window_end_vpos. */
11496 if (first_unchanged_at_end_row
11497 && first_unchanged_at_end_row->y < it.last_visible_y
11498 && !last_text_row_at_end)
11499 {
11500 /* Window end line if one of the preserved rows from the current
11501 matrix. Set row to the last row displaying text in current
11502 matrix starting at first_unchanged_at_end_row, after
11503 scrolling. */
11504 xassert (first_unchanged_at_end_row->displays_text_p);
11505 row = find_last_row_displaying_text (w->current_matrix, &it,
11506 first_unchanged_at_end_row);
11507 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11508
11509 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11510 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11511 XSETFASTINT (w->window_end_vpos,
11512 MATRIX_ROW_VPOS (row, w->current_matrix));
11513 }
11514 else if (last_text_row_at_end)
11515 {
11516 XSETFASTINT (w->window_end_pos,
11517 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11518 w->window_end_bytepos
11519 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11520 XSETFASTINT (w->window_end_vpos,
11521 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11522 }
11523 else if (last_text_row)
11524 {
11525 /* We have displayed either to the end of the window or at the
11526 end of the window, i.e. the last row with text is to be found
11527 in the desired matrix. */
11528 XSETFASTINT (w->window_end_pos,
11529 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11530 w->window_end_bytepos
11531 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11532 XSETFASTINT (w->window_end_vpos,
11533 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11534 }
11535 else if (first_unchanged_at_end_row == NULL
11536 && last_text_row == NULL
11537 && last_text_row_at_end == NULL)
11538 {
11539 /* Displayed to end of window, but no line containing text was
11540 displayed. Lines were deleted at the end of the window. */
11541 int vpos;
11542 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11543
11544 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11545 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11546 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11547 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11548 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11549 break;
11550
11551 w->window_end_vpos = make_number (vpos);
11552 }
11553 else
11554 abort ();
11555
11556 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11557 debug_end_vpos = XFASTINT (w->window_end_vpos));
11558
11559 /* Record that display has not been completed. */
11560 w->window_end_valid = Qnil;
11561 w->desired_matrix->no_scrolling_p = 1;
11562 return 3;
11563 }
11564
11565
11566 \f
11567 /***********************************************************************
11568 More debugging support
11569 ***********************************************************************/
11570
11571 #if GLYPH_DEBUG
11572
11573 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11574 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11575 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11576
11577
11578 /* Dump the contents of glyph matrix MATRIX on stderr.
11579
11580 GLYPHS 0 means don't show glyph contents.
11581 GLYPHS 1 means show glyphs in short form
11582 GLYPHS > 1 means show glyphs in long form. */
11583
11584 void
11585 dump_glyph_matrix (matrix, glyphs)
11586 struct glyph_matrix *matrix;
11587 int glyphs;
11588 {
11589 int i;
11590 for (i = 0; i < matrix->nrows; ++i)
11591 dump_glyph_row (matrix, i, glyphs);
11592 }
11593
11594
11595 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11596 the glyph row and area where the glyph comes from. */
11597
11598 void
11599 dump_glyph (row, glyph, area)
11600 struct glyph_row *row;
11601 struct glyph *glyph;
11602 int area;
11603 {
11604 if (glyph->type == CHAR_GLYPH)
11605 {
11606 fprintf (stderr,
11607 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11608 glyph - row->glyphs[TEXT_AREA],
11609 'C',
11610 glyph->charpos,
11611 (BUFFERP (glyph->object)
11612 ? 'B'
11613 : (STRINGP (glyph->object)
11614 ? 'S'
11615 : '-')),
11616 glyph->pixel_width,
11617 glyph->u.ch,
11618 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11619 ? glyph->u.ch
11620 : '.'),
11621 glyph->face_id,
11622 glyph->left_box_line_p,
11623 glyph->right_box_line_p);
11624 }
11625 else if (glyph->type == STRETCH_GLYPH)
11626 {
11627 fprintf (stderr,
11628 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11629 glyph - row->glyphs[TEXT_AREA],
11630 'S',
11631 glyph->charpos,
11632 (BUFFERP (glyph->object)
11633 ? 'B'
11634 : (STRINGP (glyph->object)
11635 ? 'S'
11636 : '-')),
11637 glyph->pixel_width,
11638 0,
11639 '.',
11640 glyph->face_id,
11641 glyph->left_box_line_p,
11642 glyph->right_box_line_p);
11643 }
11644 else if (glyph->type == IMAGE_GLYPH)
11645 {
11646 fprintf (stderr,
11647 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11648 glyph - row->glyphs[TEXT_AREA],
11649 'I',
11650 glyph->charpos,
11651 (BUFFERP (glyph->object)
11652 ? 'B'
11653 : (STRINGP (glyph->object)
11654 ? 'S'
11655 : '-')),
11656 glyph->pixel_width,
11657 glyph->u.img_id,
11658 '.',
11659 glyph->face_id,
11660 glyph->left_box_line_p,
11661 glyph->right_box_line_p);
11662 }
11663 }
11664
11665
11666 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11667 GLYPHS 0 means don't show glyph contents.
11668 GLYPHS 1 means show glyphs in short form
11669 GLYPHS > 1 means show glyphs in long form. */
11670
11671 void
11672 dump_glyph_row (matrix, vpos, glyphs)
11673 struct glyph_matrix *matrix;
11674 int vpos, glyphs;
11675 {
11676 struct glyph_row *row;
11677
11678 if (vpos < 0 || vpos >= matrix->nrows)
11679 return;
11680
11681 row = MATRIX_ROW (matrix, vpos);
11682
11683 if (glyphs != 1)
11684 {
11685 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
11686 fprintf (stderr, "=======================================================================\n");
11687
11688 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11689 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11690 row - matrix->rows,
11691 MATRIX_ROW_START_CHARPOS (row),
11692 MATRIX_ROW_END_CHARPOS (row),
11693 row->used[TEXT_AREA],
11694 row->contains_overlapping_glyphs_p,
11695 row->enabled_p,
11696 row->inverse_p,
11697 row->truncated_on_left_p,
11698 row->truncated_on_right_p,
11699 row->overlay_arrow_p,
11700 row->continued_p,
11701 MATRIX_ROW_CONTINUATION_LINE_P (row),
11702 row->displays_text_p,
11703 row->ends_at_zv_p,
11704 row->fill_line_p,
11705 row->ends_in_middle_of_char_p,
11706 row->starts_in_middle_of_char_p,
11707 row->mouse_face_p,
11708 row->x,
11709 row->y,
11710 row->pixel_width,
11711 row->height,
11712 row->visible_height,
11713 row->ascent,
11714 row->phys_ascent);
11715 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11716 row->end.overlay_string_index);
11717 fprintf (stderr, "%9d %5d\n",
11718 CHARPOS (row->start.string_pos),
11719 CHARPOS (row->end.string_pos));
11720 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11721 row->end.dpvec_index);
11722 }
11723
11724 if (glyphs > 1)
11725 {
11726 int area;
11727
11728 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11729 {
11730 struct glyph *glyph, *glyph_end;
11731 glyph = row->glyphs[area];
11732 glyph_end = glyph + row->used[area];
11733
11734 /* Glyph for a line end in text. */
11735 if (glyph == glyph_end && glyph->charpos > 0)
11736 ++glyph_end;
11737
11738 if (glyph < glyph_end)
11739 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11740
11741 for (; glyph < glyph_end; ++glyph)
11742 dump_glyph (row, glyph, area);
11743 }
11744 }
11745 else if (glyphs == 1)
11746 {
11747 int area;
11748
11749 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11750 {
11751 char *s = (char *) alloca (row->used[area] + 1);
11752 int i;
11753
11754 for (i = 0; i < row->used[area]; ++i)
11755 {
11756 struct glyph *glyph = row->glyphs[area] + i;
11757 if (glyph->type == CHAR_GLYPH
11758 && glyph->u.ch < 0x80
11759 && glyph->u.ch >= ' ')
11760 s[i] = glyph->u.ch;
11761 else
11762 s[i] = '.';
11763 }
11764
11765 s[i] = '\0';
11766 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11767 }
11768 }
11769 }
11770
11771
11772 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11773 Sdump_glyph_matrix, 0, 1, "p",
11774 "Dump the current matrix of the selected window to stderr.\n\
11775 Shows contents of glyph row structures. With non-nil\n\
11776 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11777 glyphs in short form, otherwise show glyphs in long form.")
11778 (glyphs)
11779 Lisp_Object glyphs;
11780 {
11781 struct window *w = XWINDOW (selected_window);
11782 struct buffer *buffer = XBUFFER (w->buffer);
11783
11784 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11785 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11786 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11787 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11788 fprintf (stderr, "=============================================\n");
11789 dump_glyph_matrix (w->current_matrix,
11790 NILP (glyphs) ? 0 : XINT (glyphs));
11791 return Qnil;
11792 }
11793
11794
11795 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11796 "Dump glyph row ROW to stderr.\n\
11797 GLYPH 0 means don't dump glyphs.\n\
11798 GLYPH 1 means dump glyphs in short form.\n\
11799 GLYPH > 1 or omitted means dump glyphs in long form.")
11800 (row, glyphs)
11801 Lisp_Object row, glyphs;
11802 {
11803 CHECK_NUMBER (row, 0);
11804 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11805 XINT (row),
11806 INTEGERP (glyphs) ? XINT (glyphs) : 2);
11807 return Qnil;
11808 }
11809
11810
11811 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11812 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11813 GLYPH 0 means don't dump glyphs.\n\
11814 GLYPH 1 means dump glyphs in short form.\n\
11815 GLYPH > 1 or omitted means dump glyphs in long form.")
11816 (row, glyphs)
11817 Lisp_Object row, glyphs;
11818 {
11819 struct frame *sf = SELECTED_FRAME ();
11820 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11821 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
11822 return Qnil;
11823 }
11824
11825
11826 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11827 Strace_redisplay_toggle, 0, 0, "",
11828 "Toggle tracing of redisplay.")
11829 ()
11830 {
11831 trace_redisplay_p = !trace_redisplay_p;
11832 return Qnil;
11833 }
11834
11835
11836 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11837 "Print STRING to stderr.")
11838 (string)
11839 Lisp_Object string;
11840 {
11841 CHECK_STRING (string, 0);
11842 fprintf (stderr, "%s", XSTRING (string)->data);
11843 return Qnil;
11844 }
11845
11846 #endif /* GLYPH_DEBUG */
11847
11848
11849 \f
11850 /***********************************************************************
11851 Building Desired Matrix Rows
11852 ***********************************************************************/
11853
11854 /* Return a temporary glyph row holding the glyphs of an overlay
11855 arrow. Only used for non-window-redisplay windows. */
11856
11857 static struct glyph_row *
11858 get_overlay_arrow_glyph_row (w)
11859 struct window *w;
11860 {
11861 struct frame *f = XFRAME (WINDOW_FRAME (w));
11862 struct buffer *buffer = XBUFFER (w->buffer);
11863 struct buffer *old = current_buffer;
11864 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11865 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11866 unsigned char *arrow_end = arrow_string + arrow_len;
11867 unsigned char *p;
11868 struct it it;
11869 int multibyte_p;
11870 int n_glyphs_before;
11871
11872 set_buffer_temp (buffer);
11873 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11874 it.glyph_row->used[TEXT_AREA] = 0;
11875 SET_TEXT_POS (it.position, 0, 0);
11876
11877 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11878 p = arrow_string;
11879 while (p < arrow_end)
11880 {
11881 Lisp_Object face, ilisp;
11882
11883 /* Get the next character. */
11884 if (multibyte_p)
11885 it.c = string_char_and_length (p, arrow_len, &it.len);
11886 else
11887 it.c = *p, it.len = 1;
11888 p += it.len;
11889
11890 /* Get its face. */
11891 XSETFASTINT (ilisp, p - arrow_string);
11892 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11893 it.face_id = compute_char_face (f, it.c, face);
11894
11895 /* Compute its width, get its glyphs. */
11896 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11897 SET_TEXT_POS (it.position, -1, -1);
11898 PRODUCE_GLYPHS (&it);
11899
11900 /* If this character doesn't fit any more in the line, we have
11901 to remove some glyphs. */
11902 if (it.current_x > it.last_visible_x)
11903 {
11904 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11905 break;
11906 }
11907 }
11908
11909 set_buffer_temp (old);
11910 return it.glyph_row;
11911 }
11912
11913
11914 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11915 glyphs are only inserted for terminal frames since we can't really
11916 win with truncation glyphs when partially visible glyphs are
11917 involved. Which glyphs to insert is determined by
11918 produce_special_glyphs. */
11919
11920 static void
11921 insert_left_trunc_glyphs (it)
11922 struct it *it;
11923 {
11924 struct it truncate_it;
11925 struct glyph *from, *end, *to, *toend;
11926
11927 xassert (!FRAME_WINDOW_P (it->f));
11928
11929 /* Get the truncation glyphs. */
11930 truncate_it = *it;
11931 truncate_it.current_x = 0;
11932 truncate_it.face_id = DEFAULT_FACE_ID;
11933 truncate_it.glyph_row = &scratch_glyph_row;
11934 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11935 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11936 truncate_it.object = make_number (0);
11937 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11938
11939 /* Overwrite glyphs from IT with truncation glyphs. */
11940 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11941 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11942 to = it->glyph_row->glyphs[TEXT_AREA];
11943 toend = to + it->glyph_row->used[TEXT_AREA];
11944
11945 while (from < end)
11946 *to++ = *from++;
11947
11948 /* There may be padding glyphs left over. Overwrite them too. */
11949 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11950 {
11951 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11952 while (from < end)
11953 *to++ = *from++;
11954 }
11955
11956 if (to > toend)
11957 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11958 }
11959
11960
11961 /* Compute the pixel height and width of IT->glyph_row.
11962
11963 Most of the time, ascent and height of a display line will be equal
11964 to the max_ascent and max_height values of the display iterator
11965 structure. This is not the case if
11966
11967 1. We hit ZV without displaying anything. In this case, max_ascent
11968 and max_height will be zero.
11969
11970 2. We have some glyphs that don't contribute to the line height.
11971 (The glyph row flag contributes_to_line_height_p is for future
11972 pixmap extensions).
11973
11974 The first case is easily covered by using default values because in
11975 these cases, the line height does not really matter, except that it
11976 must not be zero. */
11977
11978 static void
11979 compute_line_metrics (it)
11980 struct it *it;
11981 {
11982 struct glyph_row *row = it->glyph_row;
11983 int area, i;
11984
11985 if (FRAME_WINDOW_P (it->f))
11986 {
11987 int i, header_line_height;
11988
11989 /* The line may consist of one space only, that was added to
11990 place the cursor on it. If so, the row's height hasn't been
11991 computed yet. */
11992 if (row->height == 0)
11993 {
11994 if (it->max_ascent + it->max_descent == 0)
11995 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11996 row->ascent = it->max_ascent;
11997 row->height = it->max_ascent + it->max_descent;
11998 row->phys_ascent = it->max_phys_ascent;
11999 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12000 }
12001
12002 /* Compute the width of this line. */
12003 row->pixel_width = row->x;
12004 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12005 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12006
12007 xassert (row->pixel_width >= 0);
12008 xassert (row->ascent >= 0 && row->height > 0);
12009
12010 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12011 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12012
12013 /* If first line's physical ascent is larger than its logical
12014 ascent, use the physical ascent, and make the row taller.
12015 This makes accented characters fully visible. */
12016 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12017 && row->phys_ascent > row->ascent)
12018 {
12019 row->height += row->phys_ascent - row->ascent;
12020 row->ascent = row->phys_ascent;
12021 }
12022
12023 /* Compute how much of the line is visible. */
12024 row->visible_height = row->height;
12025
12026 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12027 if (row->y < header_line_height)
12028 row->visible_height -= header_line_height - row->y;
12029 else
12030 {
12031 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12032 if (row->y + row->height > max_y)
12033 row->visible_height -= row->y + row->height - max_y;
12034 }
12035 }
12036 else
12037 {
12038 row->pixel_width = row->used[TEXT_AREA];
12039 row->ascent = row->phys_ascent = 0;
12040 row->height = row->phys_height = row->visible_height = 1;
12041 }
12042
12043 /* Compute a hash code for this row. */
12044 row->hash = 0;
12045 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12046 for (i = 0; i < row->used[area]; ++i)
12047 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12048 + row->glyphs[area][i].u.val
12049 + row->glyphs[area][i].face_id
12050 + row->glyphs[area][i].padding_p
12051 + (row->glyphs[area][i].type << 2));
12052
12053 it->max_ascent = it->max_descent = 0;
12054 it->max_phys_ascent = it->max_phys_descent = 0;
12055 }
12056
12057
12058 /* Append one space to the glyph row of iterator IT if doing a
12059 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12060 space have the default face, otherwise let it have the same face as
12061 IT->face_id. Value is non-zero if a space was added.
12062
12063 This function is called to make sure that there is always one glyph
12064 at the end of a glyph row that the cursor can be set on under
12065 window-systems. (If there weren't such a glyph we would not know
12066 how wide and tall a box cursor should be displayed).
12067
12068 At the same time this space let's a nicely handle clearing to the
12069 end of the line if the row ends in italic text. */
12070
12071 static int
12072 append_space (it, default_face_p)
12073 struct it *it;
12074 int default_face_p;
12075 {
12076 if (FRAME_WINDOW_P (it->f))
12077 {
12078 int n = it->glyph_row->used[TEXT_AREA];
12079
12080 if (it->glyph_row->glyphs[TEXT_AREA] + n
12081 < it->glyph_row->glyphs[1 + TEXT_AREA])
12082 {
12083 /* Save some values that must not be changed.
12084 Must save IT->c and IT->len because otherwise
12085 ITERATOR_AT_END_P wouldn't work anymore after
12086 append_space has been called. */
12087 enum display_element_type saved_what = it->what;
12088 int saved_c = it->c, saved_len = it->len;
12089 int saved_x = it->current_x;
12090 int saved_face_id = it->face_id;
12091 struct text_pos saved_pos;
12092 Lisp_Object saved_object;
12093 struct face *face;
12094
12095 saved_object = it->object;
12096 saved_pos = it->position;
12097
12098 it->what = IT_CHARACTER;
12099 bzero (&it->position, sizeof it->position);
12100 it->object = make_number (0);
12101 it->c = ' ';
12102 it->len = 1;
12103
12104 if (default_face_p)
12105 it->face_id = DEFAULT_FACE_ID;
12106 else if (it->face_before_selective_p)
12107 it->face_id = it->saved_face_id;
12108 face = FACE_FROM_ID (it->f, it->face_id);
12109 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12110
12111 PRODUCE_GLYPHS (it);
12112
12113 it->current_x = saved_x;
12114 it->object = saved_object;
12115 it->position = saved_pos;
12116 it->what = saved_what;
12117 it->face_id = saved_face_id;
12118 it->len = saved_len;
12119 it->c = saved_c;
12120 return 1;
12121 }
12122 }
12123
12124 return 0;
12125 }
12126
12127
12128 /* Extend the face of the last glyph in the text area of IT->glyph_row
12129 to the end of the display line. Called from display_line.
12130 If the glyph row is empty, add a space glyph to it so that we
12131 know the face to draw. Set the glyph row flag fill_line_p. */
12132
12133 static void
12134 extend_face_to_end_of_line (it)
12135 struct it *it;
12136 {
12137 struct face *face;
12138 struct frame *f = it->f;
12139
12140 /* If line is already filled, do nothing. */
12141 if (it->current_x >= it->last_visible_x)
12142 return;
12143
12144 /* Face extension extends the background and box of IT->face_id
12145 to the end of the line. If the background equals the background
12146 of the frame, we don't have to do anything. */
12147 if (it->face_before_selective_p)
12148 face = FACE_FROM_ID (it->f, it->saved_face_id);
12149 else
12150 face = FACE_FROM_ID (f, it->face_id);
12151
12152 if (FRAME_WINDOW_P (f)
12153 && face->box == FACE_NO_BOX
12154 && face->background == FRAME_BACKGROUND_PIXEL (f)
12155 && !face->stipple)
12156 return;
12157
12158 /* Set the glyph row flag indicating that the face of the last glyph
12159 in the text area has to be drawn to the end of the text area. */
12160 it->glyph_row->fill_line_p = 1;
12161
12162 /* If current character of IT is not ASCII, make sure we have the
12163 ASCII face. This will be automatically undone the next time
12164 get_next_display_element returns a multibyte character. Note
12165 that the character will always be single byte in unibyte text. */
12166 if (!SINGLE_BYTE_CHAR_P (it->c))
12167 {
12168 it->face_id = FACE_FOR_CHAR (f, face, 0);
12169 }
12170
12171 if (FRAME_WINDOW_P (f))
12172 {
12173 /* If the row is empty, add a space with the current face of IT,
12174 so that we know which face to draw. */
12175 if (it->glyph_row->used[TEXT_AREA] == 0)
12176 {
12177 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12178 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12179 it->glyph_row->used[TEXT_AREA] = 1;
12180 }
12181 }
12182 else
12183 {
12184 /* Save some values that must not be changed. */
12185 int saved_x = it->current_x;
12186 struct text_pos saved_pos;
12187 Lisp_Object saved_object;
12188 enum display_element_type saved_what = it->what;
12189 int saved_face_id = it->face_id;
12190
12191 saved_object = it->object;
12192 saved_pos = it->position;
12193
12194 it->what = IT_CHARACTER;
12195 bzero (&it->position, sizeof it->position);
12196 it->object = make_number (0);
12197 it->c = ' ';
12198 it->len = 1;
12199 it->face_id = face->id;
12200
12201 PRODUCE_GLYPHS (it);
12202
12203 while (it->current_x <= it->last_visible_x)
12204 PRODUCE_GLYPHS (it);
12205
12206 /* Don't count these blanks really. It would let us insert a left
12207 truncation glyph below and make us set the cursor on them, maybe. */
12208 it->current_x = saved_x;
12209 it->object = saved_object;
12210 it->position = saved_pos;
12211 it->what = saved_what;
12212 it->face_id = saved_face_id;
12213 }
12214 }
12215
12216
12217 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12218 trailing whitespace. */
12219
12220 static int
12221 trailing_whitespace_p (charpos)
12222 int charpos;
12223 {
12224 int bytepos = CHAR_TO_BYTE (charpos);
12225 int c = 0;
12226
12227 while (bytepos < ZV_BYTE
12228 && (c = FETCH_CHAR (bytepos),
12229 c == ' ' || c == '\t'))
12230 ++bytepos;
12231
12232 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12233 {
12234 if (bytepos != PT_BYTE)
12235 return 1;
12236 }
12237 return 0;
12238 }
12239
12240
12241 /* Highlight trailing whitespace, if any, in ROW. */
12242
12243 void
12244 highlight_trailing_whitespace (f, row)
12245 struct frame *f;
12246 struct glyph_row *row;
12247 {
12248 int used = row->used[TEXT_AREA];
12249
12250 if (used)
12251 {
12252 struct glyph *start = row->glyphs[TEXT_AREA];
12253 struct glyph *glyph = start + used - 1;
12254
12255 /* Skip over space glyphs inserted to display the cursor at the
12256 end of a line, and for extending the face of the last glyph
12257 to the end of the line on terminals. */
12258 while (glyph >= start
12259 && glyph->type == CHAR_GLYPH
12260 && glyph->u.ch == ' '
12261 && INTEGERP (glyph->object))
12262 --glyph;
12263
12264 /* If last glyph is a space or stretch, and it's trailing
12265 whitespace, set the face of all trailing whitespace glyphs in
12266 IT->glyph_row to `trailing-whitespace'. */
12267 if (glyph >= start
12268 && BUFFERP (glyph->object)
12269 && (glyph->type == STRETCH_GLYPH
12270 || (glyph->type == CHAR_GLYPH
12271 && glyph->u.ch == ' '))
12272 && trailing_whitespace_p (glyph->charpos))
12273 {
12274 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12275
12276 while (glyph >= start
12277 && BUFFERP (glyph->object)
12278 && (glyph->type == STRETCH_GLYPH
12279 || (glyph->type == CHAR_GLYPH
12280 && glyph->u.ch == ' ')))
12281 (glyph--)->face_id = face_id;
12282 }
12283 }
12284 }
12285
12286
12287 /* Value is non-zero if glyph row ROW in window W should be
12288 used to hold the cursor. */
12289
12290 static int
12291 cursor_row_p (w, row)
12292 struct window *w;
12293 struct glyph_row *row;
12294 {
12295 int cursor_row_p = 1;
12296
12297 if (PT == MATRIX_ROW_END_CHARPOS (row))
12298 {
12299 /* If the row ends with a newline from a string, we don't want
12300 the cursor there (if the row is continued it doesn't end in a
12301 newline). */
12302 if (CHARPOS (row->end.string_pos) >= 0
12303 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12304 cursor_row_p = row->continued_p;
12305
12306 /* If the row ends at ZV, display the cursor at the end of that
12307 row instead of at the start of the row below. */
12308 else if (row->ends_at_zv_p)
12309 cursor_row_p = 1;
12310 else
12311 cursor_row_p = 0;
12312 }
12313
12314 return cursor_row_p;
12315 }
12316
12317
12318 /* Construct the glyph row IT->glyph_row in the desired matrix of
12319 IT->w from text at the current position of IT. See dispextern.h
12320 for an overview of struct it. Value is non-zero if
12321 IT->glyph_row displays text, as opposed to a line displaying ZV
12322 only. */
12323
12324 static int
12325 display_line (it)
12326 struct it *it;
12327 {
12328 struct glyph_row *row = it->glyph_row;
12329
12330 /* We always start displaying at hpos zero even if hscrolled. */
12331 xassert (it->hpos == 0 && it->current_x == 0);
12332
12333 /* We must not display in a row that's not a text row. */
12334 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12335 < it->w->desired_matrix->nrows);
12336
12337 /* Is IT->w showing the region? */
12338 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12339
12340 /* Clear the result glyph row and enable it. */
12341 prepare_desired_row (row);
12342
12343 row->y = it->current_y;
12344 row->start = it->current;
12345 row->continuation_lines_width = it->continuation_lines_width;
12346 row->displays_text_p = 1;
12347 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12348 it->starts_in_middle_of_char_p = 0;
12349
12350 /* Arrange the overlays nicely for our purposes. Usually, we call
12351 display_line on only one line at a time, in which case this
12352 can't really hurt too much, or we call it on lines which appear
12353 one after another in the buffer, in which case all calls to
12354 recenter_overlay_lists but the first will be pretty cheap. */
12355 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12356
12357 /* Move over display elements that are not visible because we are
12358 hscrolled. This may stop at an x-position < IT->first_visible_x
12359 if the first glyph is partially visible or if we hit a line end. */
12360 if (it->current_x < it->first_visible_x)
12361 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12362 MOVE_TO_POS | MOVE_TO_X);
12363
12364 /* Get the initial row height. This is either the height of the
12365 text hscrolled, if there is any, or zero. */
12366 row->ascent = it->max_ascent;
12367 row->height = it->max_ascent + it->max_descent;
12368 row->phys_ascent = it->max_phys_ascent;
12369 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12370
12371 /* Loop generating characters. The loop is left with IT on the next
12372 character to display. */
12373 while (1)
12374 {
12375 int n_glyphs_before, hpos_before, x_before;
12376 int x, i, nglyphs;
12377 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12378
12379 /* Retrieve the next thing to display. Value is zero if end of
12380 buffer reached. */
12381 if (!get_next_display_element (it))
12382 {
12383 /* Maybe add a space at the end of this line that is used to
12384 display the cursor there under X. Set the charpos of the
12385 first glyph of blank lines not corresponding to any text
12386 to -1. */
12387 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12388 || row->used[TEXT_AREA] == 0)
12389 {
12390 row->glyphs[TEXT_AREA]->charpos = -1;
12391 row->displays_text_p = 0;
12392
12393 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12394 row->indicate_empty_line_p = 1;
12395 }
12396
12397 it->continuation_lines_width = 0;
12398 row->ends_at_zv_p = 1;
12399 break;
12400 }
12401
12402 /* Now, get the metrics of what we want to display. This also
12403 generates glyphs in `row' (which is IT->glyph_row). */
12404 n_glyphs_before = row->used[TEXT_AREA];
12405 x = it->current_x;
12406
12407 /* Remember the line height so far in case the next element doesn't
12408 fit on the line. */
12409 if (!it->truncate_lines_p)
12410 {
12411 ascent = it->max_ascent;
12412 descent = it->max_descent;
12413 phys_ascent = it->max_phys_ascent;
12414 phys_descent = it->max_phys_descent;
12415 }
12416
12417 PRODUCE_GLYPHS (it);
12418
12419 /* If this display element was in marginal areas, continue with
12420 the next one. */
12421 if (it->area != TEXT_AREA)
12422 {
12423 row->ascent = max (row->ascent, it->max_ascent);
12424 row->height = max (row->height, it->max_ascent + it->max_descent);
12425 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12426 row->phys_height = max (row->phys_height,
12427 it->max_phys_ascent + it->max_phys_descent);
12428 set_iterator_to_next (it, 1);
12429 continue;
12430 }
12431
12432 /* Does the display element fit on the line? If we truncate
12433 lines, we should draw past the right edge of the window. If
12434 we don't truncate, we want to stop so that we can display the
12435 continuation glyph before the right margin. If lines are
12436 continued, there are two possible strategies for characters
12437 resulting in more than 1 glyph (e.g. tabs): Display as many
12438 glyphs as possible in this line and leave the rest for the
12439 continuation line, or display the whole element in the next
12440 line. Original redisplay did the former, so we do it also. */
12441 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12442 hpos_before = it->hpos;
12443 x_before = x;
12444
12445 if (/* Not a newline. */
12446 nglyphs > 0
12447 /* Glyphs produced fit entirely in the line. */
12448 && it->current_x < it->last_visible_x)
12449 {
12450 it->hpos += nglyphs;
12451 row->ascent = max (row->ascent, it->max_ascent);
12452 row->height = max (row->height, it->max_ascent + it->max_descent);
12453 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12454 row->phys_height = max (row->phys_height,
12455 it->max_phys_ascent + it->max_phys_descent);
12456 if (it->current_x - it->pixel_width < it->first_visible_x)
12457 row->x = x - it->first_visible_x;
12458 }
12459 else
12460 {
12461 int new_x;
12462 struct glyph *glyph;
12463
12464 for (i = 0; i < nglyphs; ++i, x = new_x)
12465 {
12466 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12467 new_x = x + glyph->pixel_width;
12468
12469 if (/* Lines are continued. */
12470 !it->truncate_lines_p
12471 && (/* Glyph doesn't fit on the line. */
12472 new_x > it->last_visible_x
12473 /* Or it fits exactly on a window system frame. */
12474 || (new_x == it->last_visible_x
12475 && FRAME_WINDOW_P (it->f))))
12476 {
12477 /* End of a continued line. */
12478
12479 if (it->hpos == 0
12480 || (new_x == it->last_visible_x
12481 && FRAME_WINDOW_P (it->f)))
12482 {
12483 /* Current glyph is the only one on the line or
12484 fits exactly on the line. We must continue
12485 the line because we can't draw the cursor
12486 after the glyph. */
12487 row->continued_p = 1;
12488 it->current_x = new_x;
12489 it->continuation_lines_width += new_x;
12490 ++it->hpos;
12491 if (i == nglyphs - 1)
12492 set_iterator_to_next (it, 1);
12493 }
12494 else if (CHAR_GLYPH_PADDING_P (*glyph)
12495 && !FRAME_WINDOW_P (it->f))
12496 {
12497 /* A padding glyph that doesn't fit on this line.
12498 This means the whole character doesn't fit
12499 on the line. */
12500 row->used[TEXT_AREA] = n_glyphs_before;
12501
12502 /* Fill the rest of the row with continuation
12503 glyphs like in 20.x. */
12504 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12505 < row->glyphs[1 + TEXT_AREA])
12506 produce_special_glyphs (it, IT_CONTINUATION);
12507
12508 row->continued_p = 1;
12509 it->current_x = x_before;
12510 it->continuation_lines_width += x_before;
12511
12512 /* Restore the height to what it was before the
12513 element not fitting on the line. */
12514 it->max_ascent = ascent;
12515 it->max_descent = descent;
12516 it->max_phys_ascent = phys_ascent;
12517 it->max_phys_descent = phys_descent;
12518 }
12519 else
12520 {
12521 /* Display element draws past the right edge of
12522 the window. Restore positions to values
12523 before the element. The next line starts
12524 with current_x before the glyph that could
12525 not be displayed, so that TAB works right. */
12526 row->used[TEXT_AREA] = n_glyphs_before + i;
12527
12528 /* Display continuation glyphs. */
12529 if (!FRAME_WINDOW_P (it->f))
12530 produce_special_glyphs (it, IT_CONTINUATION);
12531 row->continued_p = 1;
12532
12533 it->current_x = x;
12534 it->continuation_lines_width += x;
12535 if (nglyphs > 1 && i > 0)
12536 {
12537 row->ends_in_middle_of_char_p = 1;
12538 it->starts_in_middle_of_char_p = 1;
12539 }
12540
12541 /* Restore the height to what it was before the
12542 element not fitting on the line. */
12543 it->max_ascent = ascent;
12544 it->max_descent = descent;
12545 it->max_phys_ascent = phys_ascent;
12546 it->max_phys_descent = phys_descent;
12547 }
12548
12549 break;
12550 }
12551 else if (new_x > it->first_visible_x)
12552 {
12553 /* Increment number of glyphs actually displayed. */
12554 ++it->hpos;
12555
12556 if (x < it->first_visible_x)
12557 /* Glyph is partially visible, i.e. row starts at
12558 negative X position. */
12559 row->x = x - it->first_visible_x;
12560 }
12561 else
12562 {
12563 /* Glyph is completely off the left margin of the
12564 window. This should not happen because of the
12565 move_it_in_display_line at the start of
12566 this function. */
12567 abort ();
12568 }
12569 }
12570
12571 row->ascent = max (row->ascent, it->max_ascent);
12572 row->height = max (row->height, it->max_ascent + it->max_descent);
12573 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12574 row->phys_height = max (row->phys_height,
12575 it->max_phys_ascent + it->max_phys_descent);
12576
12577 /* End of this display line if row is continued. */
12578 if (row->continued_p)
12579 break;
12580 }
12581
12582 /* Is this a line end? If yes, we're also done, after making
12583 sure that a non-default face is extended up to the right
12584 margin of the window. */
12585 if (ITERATOR_AT_END_OF_LINE_P (it))
12586 {
12587 int used_before = row->used[TEXT_AREA];
12588
12589 /* Add a space at the end of the line that is used to
12590 display the cursor there. */
12591 append_space (it, 0);
12592
12593 /* Extend the face to the end of the line. */
12594 extend_face_to_end_of_line (it);
12595
12596 /* Make sure we have the position. */
12597 if (used_before == 0)
12598 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12599
12600 /* Consume the line end. This skips over invisible lines. */
12601 set_iterator_to_next (it, 1);
12602 it->continuation_lines_width = 0;
12603 break;
12604 }
12605
12606 /* Proceed with next display element. Note that this skips
12607 over lines invisible because of selective display. */
12608 set_iterator_to_next (it, 1);
12609
12610 /* If we truncate lines, we are done when the last displayed
12611 glyphs reach past the right margin of the window. */
12612 if (it->truncate_lines_p
12613 && (FRAME_WINDOW_P (it->f)
12614 ? (it->current_x >= it->last_visible_x)
12615 : (it->current_x > it->last_visible_x)))
12616 {
12617 /* Maybe add truncation glyphs. */
12618 if (!FRAME_WINDOW_P (it->f))
12619 {
12620 int i, n;
12621
12622 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12623 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12624 break;
12625
12626 for (n = row->used[TEXT_AREA]; i < n; ++i)
12627 {
12628 row->used[TEXT_AREA] = i;
12629 produce_special_glyphs (it, IT_TRUNCATION);
12630 }
12631 }
12632
12633 row->truncated_on_right_p = 1;
12634 it->continuation_lines_width = 0;
12635 reseat_at_next_visible_line_start (it, 0);
12636 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12637 it->hpos = hpos_before;
12638 it->current_x = x_before;
12639 break;
12640 }
12641 }
12642
12643 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12644 at the left window margin. */
12645 if (it->first_visible_x
12646 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12647 {
12648 if (!FRAME_WINDOW_P (it->f))
12649 insert_left_trunc_glyphs (it);
12650 row->truncated_on_left_p = 1;
12651 }
12652
12653 /* If the start of this line is the overlay arrow-position, then
12654 mark this glyph row as the one containing the overlay arrow.
12655 This is clearly a mess with variable size fonts. It would be
12656 better to let it be displayed like cursors under X. */
12657 if (MARKERP (Voverlay_arrow_position)
12658 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12659 && (MATRIX_ROW_START_CHARPOS (row)
12660 == marker_position (Voverlay_arrow_position))
12661 && STRINGP (Voverlay_arrow_string)
12662 && ! overlay_arrow_seen)
12663 {
12664 /* Overlay arrow in window redisplay is a bitmap. */
12665 if (!FRAME_WINDOW_P (it->f))
12666 {
12667 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12668 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12669 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12670 struct glyph *p = row->glyphs[TEXT_AREA];
12671 struct glyph *p2, *end;
12672
12673 /* Copy the arrow glyphs. */
12674 while (glyph < arrow_end)
12675 *p++ = *glyph++;
12676
12677 /* Throw away padding glyphs. */
12678 p2 = p;
12679 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12680 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12681 ++p2;
12682 if (p2 > p)
12683 {
12684 while (p2 < end)
12685 *p++ = *p2++;
12686 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12687 }
12688 }
12689
12690 overlay_arrow_seen = 1;
12691 row->overlay_arrow_p = 1;
12692 }
12693
12694 /* Compute pixel dimensions of this line. */
12695 compute_line_metrics (it);
12696
12697 /* Remember the position at which this line ends. */
12698 row->end = it->current;
12699
12700 /* Maybe set the cursor. */
12701 if (it->w->cursor.vpos < 0
12702 && PT >= MATRIX_ROW_START_CHARPOS (row)
12703 && PT <= MATRIX_ROW_END_CHARPOS (row)
12704 && cursor_row_p (it->w, row))
12705 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12706
12707 /* Highlight trailing whitespace. */
12708 if (!NILP (Vshow_trailing_whitespace))
12709 highlight_trailing_whitespace (it->f, it->glyph_row);
12710
12711 /* Prepare for the next line. This line starts horizontally at (X
12712 HPOS) = (0 0). Vertical positions are incremented. As a
12713 convenience for the caller, IT->glyph_row is set to the next
12714 row to be used. */
12715 it->current_x = it->hpos = 0;
12716 it->current_y += row->height;
12717 ++it->vpos;
12718 ++it->glyph_row;
12719 return row->displays_text_p;
12720 }
12721
12722
12723 \f
12724 /***********************************************************************
12725 Menu Bar
12726 ***********************************************************************/
12727
12728 /* Redisplay the menu bar in the frame for window W.
12729
12730 The menu bar of X frames that don't have X toolkit support is
12731 displayed in a special window W->frame->menu_bar_window.
12732
12733 The menu bar of terminal frames is treated specially as far as
12734 glyph matrices are concerned. Menu bar lines are not part of
12735 windows, so the update is done directly on the frame matrix rows
12736 for the menu bar. */
12737
12738 static void
12739 display_menu_bar (w)
12740 struct window *w;
12741 {
12742 struct frame *f = XFRAME (WINDOW_FRAME (w));
12743 struct it it;
12744 Lisp_Object items;
12745 int i;
12746
12747 /* Don't do all this for graphical frames. */
12748 #ifdef HAVE_NTGUI
12749 if (!NILP (Vwindow_system))
12750 return;
12751 #endif
12752 #ifdef USE_X_TOOLKIT
12753 if (FRAME_X_P (f))
12754 return;
12755 #endif
12756 #ifdef macintosh
12757 if (FRAME_MAC_P (f))
12758 return;
12759 #endif
12760
12761 #ifdef USE_X_TOOLKIT
12762 xassert (!FRAME_WINDOW_P (f));
12763 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12764 it.first_visible_x = 0;
12765 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12766 #else /* not USE_X_TOOLKIT */
12767 if (FRAME_WINDOW_P (f))
12768 {
12769 /* Menu bar lines are displayed in the desired matrix of the
12770 dummy window menu_bar_window. */
12771 struct window *menu_w;
12772 xassert (WINDOWP (f->menu_bar_window));
12773 menu_w = XWINDOW (f->menu_bar_window);
12774 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12775 MENU_FACE_ID);
12776 it.first_visible_x = 0;
12777 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12778 }
12779 else
12780 {
12781 /* This is a TTY frame, i.e. character hpos/vpos are used as
12782 pixel x/y. */
12783 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12784 MENU_FACE_ID);
12785 it.first_visible_x = 0;
12786 it.last_visible_x = FRAME_WIDTH (f);
12787 }
12788 #endif /* not USE_X_TOOLKIT */
12789
12790 if (! mode_line_inverse_video)
12791 /* Force the menu-bar to be displayed in the default face. */
12792 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12793
12794 /* Clear all rows of the menu bar. */
12795 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12796 {
12797 struct glyph_row *row = it.glyph_row + i;
12798 clear_glyph_row (row);
12799 row->enabled_p = 1;
12800 row->full_width_p = 1;
12801 }
12802
12803 /* Display all items of the menu bar. */
12804 items = FRAME_MENU_BAR_ITEMS (it.f);
12805 for (i = 0; i < XVECTOR (items)->size; i += 4)
12806 {
12807 Lisp_Object string;
12808
12809 /* Stop at nil string. */
12810 string = AREF (items, i + 1);
12811 if (NILP (string))
12812 break;
12813
12814 /* Remember where item was displayed. */
12815 AREF (items, i + 3) = make_number (it.hpos);
12816
12817 /* Display the item, pad with one space. */
12818 if (it.current_x < it.last_visible_x)
12819 display_string (NULL, string, Qnil, 0, 0, &it,
12820 XSTRING (string)->size + 1, 0, 0, -1);
12821 }
12822
12823 /* Fill out the line with spaces. */
12824 if (it.current_x < it.last_visible_x)
12825 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12826
12827 /* Compute the total height of the lines. */
12828 compute_line_metrics (&it);
12829 }
12830
12831
12832 \f
12833 /***********************************************************************
12834 Mode Line
12835 ***********************************************************************/
12836
12837 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12838 FORCE is non-zero, redisplay mode lines unconditionally.
12839 Otherwise, redisplay only mode lines that are garbaged. Value is
12840 the number of windows whose mode lines were redisplayed. */
12841
12842 static int
12843 redisplay_mode_lines (window, force)
12844 Lisp_Object window;
12845 int force;
12846 {
12847 int nwindows = 0;
12848
12849 while (!NILP (window))
12850 {
12851 struct window *w = XWINDOW (window);
12852
12853 if (WINDOWP (w->hchild))
12854 nwindows += redisplay_mode_lines (w->hchild, force);
12855 else if (WINDOWP (w->vchild))
12856 nwindows += redisplay_mode_lines (w->vchild, force);
12857 else if (force
12858 || FRAME_GARBAGED_P (XFRAME (w->frame))
12859 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12860 {
12861 Lisp_Object old_selected_frame;
12862 struct text_pos lpoint;
12863 struct buffer *old = current_buffer;
12864
12865 /* Set the window's buffer for the mode line display. */
12866 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12867 set_buffer_internal_1 (XBUFFER (w->buffer));
12868
12869 /* Point refers normally to the selected window. For any
12870 other window, set up appropriate value. */
12871 if (!EQ (window, selected_window))
12872 {
12873 struct text_pos pt;
12874
12875 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12876 if (CHARPOS (pt) < BEGV)
12877 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12878 else if (CHARPOS (pt) > (ZV - 1))
12879 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12880 else
12881 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12882 }
12883
12884 /* Temporarily set up the selected frame. */
12885 old_selected_frame = selected_frame;
12886 selected_frame = w->frame;
12887
12888 /* Display mode lines. */
12889 clear_glyph_matrix (w->desired_matrix);
12890 if (display_mode_lines (w))
12891 {
12892 ++nwindows;
12893 w->must_be_updated_p = 1;
12894 }
12895
12896 /* Restore old settings. */
12897 selected_frame = old_selected_frame;
12898 set_buffer_internal_1 (old);
12899 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12900 }
12901
12902 window = w->next;
12903 }
12904
12905 return nwindows;
12906 }
12907
12908
12909 /* Display the mode and/or top line of window W. Value is the number
12910 of mode lines displayed. */
12911
12912 static int
12913 display_mode_lines (w)
12914 struct window *w;
12915 {
12916 int n = 0;
12917
12918 /* These will be set while the mode line specs are processed. */
12919 line_number_displayed = 0;
12920 w->column_number_displayed = Qnil;
12921
12922 if (WINDOW_WANTS_MODELINE_P (w))
12923 {
12924 display_mode_line (w, MODE_LINE_FACE_ID,
12925 current_buffer->mode_line_format);
12926 ++n;
12927 }
12928
12929 if (WINDOW_WANTS_HEADER_LINE_P (w))
12930 {
12931 display_mode_line (w, HEADER_LINE_FACE_ID,
12932 current_buffer->header_line_format);
12933 ++n;
12934 }
12935
12936 return n;
12937 }
12938
12939
12940 /* Display mode or top line of window W. FACE_ID specifies which line
12941 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12942 FORMAT is the mode line format to display. Value is the pixel
12943 height of the mode line displayed. */
12944
12945 static int
12946 display_mode_line (w, face_id, format)
12947 struct window *w;
12948 enum face_id face_id;
12949 Lisp_Object format;
12950 {
12951 struct it it;
12952 struct face *face;
12953
12954 init_iterator (&it, w, -1, -1, NULL, face_id);
12955 prepare_desired_row (it.glyph_row);
12956
12957 if (! mode_line_inverse_video)
12958 /* Force the mode-line to be displayed in the default face. */
12959 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12960
12961 /* Temporarily make frame's keyboard the current kboard so that
12962 kboard-local variables in the mode_line_format will get the right
12963 values. */
12964 push_frame_kboard (it.f);
12965 display_mode_element (&it, 0, 0, 0, format);
12966 pop_frame_kboard ();
12967
12968 /* Fill up with spaces. */
12969 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12970
12971 compute_line_metrics (&it);
12972 it.glyph_row->full_width_p = 1;
12973 it.glyph_row->mode_line_p = 1;
12974 it.glyph_row->inverse_p = 0;
12975 it.glyph_row->continued_p = 0;
12976 it.glyph_row->truncated_on_left_p = 0;
12977 it.glyph_row->truncated_on_right_p = 0;
12978
12979 /* Make a 3D mode-line have a shadow at its right end. */
12980 face = FACE_FROM_ID (it.f, face_id);
12981 extend_face_to_end_of_line (&it);
12982 if (face->box != FACE_NO_BOX)
12983 {
12984 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12985 + it.glyph_row->used[TEXT_AREA] - 1);
12986 last->right_box_line_p = 1;
12987 }
12988
12989 return it.glyph_row->height;
12990 }
12991
12992
12993 /* Contribute ELT to the mode line for window IT->w. How it
12994 translates into text depends on its data type.
12995
12996 IT describes the display environment in which we display, as usual.
12997
12998 DEPTH is the depth in recursion. It is used to prevent
12999 infinite recursion here.
13000
13001 FIELD_WIDTH is the number of characters the display of ELT should
13002 occupy in the mode line, and PRECISION is the maximum number of
13003 characters to display from ELT's representation. See
13004 display_string for details. *
13005
13006 Returns the hpos of the end of the text generated by ELT. */
13007
13008 static int
13009 display_mode_element (it, depth, field_width, precision, elt)
13010 struct it *it;
13011 int depth;
13012 int field_width, precision;
13013 Lisp_Object elt;
13014 {
13015 int n = 0, field, prec;
13016
13017 tail_recurse:
13018 if (depth > 10)
13019 goto invalid;
13020
13021 depth++;
13022
13023 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13024 {
13025 case Lisp_String:
13026 {
13027 /* A string: output it and check for %-constructs within it. */
13028 unsigned char c;
13029 unsigned char *this = XSTRING (elt)->data;
13030 unsigned char *lisp_string = this;
13031
13032 while ((precision <= 0 || n < precision)
13033 && *this
13034 && (frame_title_ptr
13035 || it->current_x < it->last_visible_x))
13036 {
13037 unsigned char *last = this;
13038
13039 /* Advance to end of string or next format specifier. */
13040 while ((c = *this++) != '\0' && c != '%')
13041 ;
13042
13043 if (this - 1 != last)
13044 {
13045 /* Output to end of string or up to '%'. Field width
13046 is length of string. Don't output more than
13047 PRECISION allows us. */
13048 --this;
13049 prec = strwidth (last, this - last);
13050 if (precision > 0 && prec > precision - n)
13051 prec = precision - n;
13052
13053 if (frame_title_ptr)
13054 n += store_frame_title (last, 0, prec);
13055 else
13056 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13057 it, 0, prec, 0, -1);
13058 }
13059 else /* c == '%' */
13060 {
13061 unsigned char *percent_position = this;
13062
13063 /* Get the specified minimum width. Zero means
13064 don't pad. */
13065 field = 0;
13066 while ((c = *this++) >= '0' && c <= '9')
13067 field = field * 10 + c - '0';
13068
13069 /* Don't pad beyond the total padding allowed. */
13070 if (field_width - n > 0 && field > field_width - n)
13071 field = field_width - n;
13072
13073 /* Note that either PRECISION <= 0 or N < PRECISION. */
13074 prec = precision - n;
13075
13076 if (c == 'M')
13077 n += display_mode_element (it, depth, field, prec,
13078 Vglobal_mode_string);
13079 else if (c != 0)
13080 {
13081 unsigned char *spec
13082 = decode_mode_spec (it->w, c, field, prec);
13083
13084 if (frame_title_ptr)
13085 n += store_frame_title (spec, field, prec);
13086 else
13087 {
13088 int nglyphs_before
13089 = it->glyph_row->used[TEXT_AREA];
13090 int charpos
13091 = percent_position - XSTRING (elt)->data;
13092 int nwritten
13093 = display_string (spec, Qnil, elt, charpos, 0, it,
13094 field, prec, 0, -1);
13095
13096 /* Assign to the glyphs written above the
13097 string where the `%x' came from, position
13098 of the `%'. */
13099 if (nwritten > 0)
13100 {
13101 struct glyph *glyph
13102 = (it->glyph_row->glyphs[TEXT_AREA]
13103 + nglyphs_before);
13104 int i;
13105
13106 for (i = 0; i < nwritten; ++i)
13107 {
13108 glyph[i].object = elt;
13109 glyph[i].charpos = charpos;
13110 }
13111
13112 n += nwritten;
13113 }
13114 }
13115 }
13116 }
13117 }
13118 }
13119 break;
13120
13121 case Lisp_Symbol:
13122 /* A symbol: process the value of the symbol recursively
13123 as if it appeared here directly. Avoid error if symbol void.
13124 Special case: if value of symbol is a string, output the string
13125 literally. */
13126 {
13127 register Lisp_Object tem;
13128 tem = Fboundp (elt);
13129 if (!NILP (tem))
13130 {
13131 tem = Fsymbol_value (elt);
13132 /* If value is a string, output that string literally:
13133 don't check for % within it. */
13134 if (STRINGP (tem))
13135 {
13136 prec = precision - n;
13137 if (frame_title_ptr)
13138 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13139 else
13140 n += display_string (NULL, tem, Qnil, 0, 0, it,
13141 0, prec, 0, -1);
13142 }
13143 else if (!EQ (tem, elt))
13144 {
13145 /* Give up right away for nil or t. */
13146 elt = tem;
13147 goto tail_recurse;
13148 }
13149 }
13150 }
13151 break;
13152
13153 case Lisp_Cons:
13154 {
13155 register Lisp_Object car, tem;
13156
13157 /* A cons cell: three distinct cases.
13158 If first element is a string or a cons, process all the elements
13159 and effectively concatenate them.
13160 If first element is a negative number, truncate displaying cdr to
13161 at most that many characters. If positive, pad (with spaces)
13162 to at least that many characters.
13163 If first element is a symbol, process the cadr or caddr recursively
13164 according to whether the symbol's value is non-nil or nil. */
13165 car = XCAR (elt);
13166 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13167 {
13168 /* An element of the form (:eval FORM) means evaluate FORM
13169 and use the result as mode line elements. */
13170 struct gcpro gcpro1;
13171 Lisp_Object spec;
13172
13173 spec = safe_eval (XCAR (XCDR (elt)));
13174 GCPRO1 (spec);
13175 n += display_mode_element (it, depth, field_width - n,
13176 precision - n, spec);
13177 UNGCPRO;
13178 }
13179 else if (SYMBOLP (car))
13180 {
13181 tem = Fboundp (car);
13182 elt = XCDR (elt);
13183 if (!CONSP (elt))
13184 goto invalid;
13185 /* elt is now the cdr, and we know it is a cons cell.
13186 Use its car if CAR has a non-nil value. */
13187 if (!NILP (tem))
13188 {
13189 tem = Fsymbol_value (car);
13190 if (!NILP (tem))
13191 {
13192 elt = XCAR (elt);
13193 goto tail_recurse;
13194 }
13195 }
13196 /* Symbol's value is nil (or symbol is unbound)
13197 Get the cddr of the original list
13198 and if possible find the caddr and use that. */
13199 elt = XCDR (elt);
13200 if (NILP (elt))
13201 break;
13202 else if (!CONSP (elt))
13203 goto invalid;
13204 elt = XCAR (elt);
13205 goto tail_recurse;
13206 }
13207 else if (INTEGERP (car))
13208 {
13209 register int lim = XINT (car);
13210 elt = XCDR (elt);
13211 if (lim < 0)
13212 {
13213 /* Negative int means reduce maximum width. */
13214 if (precision <= 0)
13215 precision = -lim;
13216 else
13217 precision = min (precision, -lim);
13218 }
13219 else if (lim > 0)
13220 {
13221 /* Padding specified. Don't let it be more than
13222 current maximum. */
13223 if (precision > 0)
13224 lim = min (precision, lim);
13225
13226 /* If that's more padding than already wanted, queue it.
13227 But don't reduce padding already specified even if
13228 that is beyond the current truncation point. */
13229 field_width = max (lim, field_width);
13230 }
13231 goto tail_recurse;
13232 }
13233 else if (STRINGP (car) || CONSP (car))
13234 {
13235 register int limit = 50;
13236 /* Limit is to protect against circular lists. */
13237 while (CONSP (elt)
13238 && --limit > 0
13239 && (precision <= 0 || n < precision))
13240 {
13241 n += display_mode_element (it, depth, field_width - n,
13242 precision - n, XCAR (elt));
13243 elt = XCDR (elt);
13244 }
13245 }
13246 }
13247 break;
13248
13249 default:
13250 invalid:
13251 if (frame_title_ptr)
13252 n += store_frame_title ("*invalid*", 0, precision - n);
13253 else
13254 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13255 precision - n, 0, 0);
13256 return n;
13257 }
13258
13259 /* Pad to FIELD_WIDTH. */
13260 if (field_width > 0 && n < field_width)
13261 {
13262 if (frame_title_ptr)
13263 n += store_frame_title ("", field_width - n, 0);
13264 else
13265 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13266 0, 0, 0);
13267 }
13268
13269 return n;
13270 }
13271
13272
13273 /* Write a null-terminated, right justified decimal representation of
13274 the positive integer D to BUF using a minimal field width WIDTH. */
13275
13276 static void
13277 pint2str (buf, width, d)
13278 register char *buf;
13279 register int width;
13280 register int d;
13281 {
13282 register char *p = buf;
13283
13284 if (d <= 0)
13285 *p++ = '0';
13286 else
13287 {
13288 while (d > 0)
13289 {
13290 *p++ = d % 10 + '0';
13291 d /= 10;
13292 }
13293 }
13294
13295 for (width -= (int) (p - buf); width > 0; --width)
13296 *p++ = ' ';
13297 *p-- = '\0';
13298 while (p > buf)
13299 {
13300 d = *buf;
13301 *buf++ = *p;
13302 *p-- = d;
13303 }
13304 }
13305
13306 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13307 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13308 type of CODING_SYSTEM. Return updated pointer into BUF. */
13309
13310 static unsigned char invalid_eol_type[] = "(*invalid*)";
13311
13312 static char *
13313 decode_mode_spec_coding (coding_system, buf, eol_flag)
13314 Lisp_Object coding_system;
13315 register char *buf;
13316 int eol_flag;
13317 {
13318 Lisp_Object val;
13319 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13320 unsigned char *eol_str;
13321 int eol_str_len;
13322 /* The EOL conversion we are using. */
13323 Lisp_Object eoltype;
13324
13325 val = Fget (coding_system, Qcoding_system);
13326 eoltype = Qnil;
13327
13328 if (!VECTORP (val)) /* Not yet decided. */
13329 {
13330 if (multibyte)
13331 *buf++ = '-';
13332 if (eol_flag)
13333 eoltype = eol_mnemonic_undecided;
13334 /* Don't mention EOL conversion if it isn't decided. */
13335 }
13336 else
13337 {
13338 Lisp_Object eolvalue;
13339
13340 eolvalue = Fget (coding_system, Qeol_type);
13341
13342 if (multibyte)
13343 *buf++ = XFASTINT (AREF (val, 1));
13344
13345 if (eol_flag)
13346 {
13347 /* The EOL conversion that is normal on this system. */
13348
13349 if (NILP (eolvalue)) /* Not yet decided. */
13350 eoltype = eol_mnemonic_undecided;
13351 else if (VECTORP (eolvalue)) /* Not yet decided. */
13352 eoltype = eol_mnemonic_undecided;
13353 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13354 eoltype = (XFASTINT (eolvalue) == 0
13355 ? eol_mnemonic_unix
13356 : (XFASTINT (eolvalue) == 1
13357 ? eol_mnemonic_dos : eol_mnemonic_mac));
13358 }
13359 }
13360
13361 if (eol_flag)
13362 {
13363 /* Mention the EOL conversion if it is not the usual one. */
13364 if (STRINGP (eoltype))
13365 {
13366 eol_str = XSTRING (eoltype)->data;
13367 eol_str_len = XSTRING (eoltype)->size;
13368 }
13369 else if (INTEGERP (eoltype)
13370 && CHAR_VALID_P (XINT (eoltype), 0))
13371 {
13372 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13373 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13374 }
13375 else
13376 {
13377 eol_str = invalid_eol_type;
13378 eol_str_len = sizeof (invalid_eol_type) - 1;
13379 }
13380 bcopy (eol_str, buf, eol_str_len);
13381 buf += eol_str_len;
13382 }
13383
13384 return buf;
13385 }
13386
13387 /* Return a string for the output of a mode line %-spec for window W,
13388 generated by character C. PRECISION >= 0 means don't return a
13389 string longer than that value. FIELD_WIDTH > 0 means pad the
13390 string returned with spaces to that value. */
13391
13392 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13393
13394 static char *
13395 decode_mode_spec (w, c, field_width, precision)
13396 struct window *w;
13397 register int c;
13398 int field_width, precision;
13399 {
13400 Lisp_Object obj;
13401 struct frame *f = XFRAME (WINDOW_FRAME (w));
13402 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13403 struct buffer *b = XBUFFER (w->buffer);
13404
13405 obj = Qnil;
13406
13407 switch (c)
13408 {
13409 case '*':
13410 if (!NILP (b->read_only))
13411 return "%";
13412 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13413 return "*";
13414 return "-";
13415
13416 case '+':
13417 /* This differs from %* only for a modified read-only buffer. */
13418 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13419 return "*";
13420 if (!NILP (b->read_only))
13421 return "%";
13422 return "-";
13423
13424 case '&':
13425 /* This differs from %* in ignoring read-only-ness. */
13426 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13427 return "*";
13428 return "-";
13429
13430 case '%':
13431 return "%";
13432
13433 case '[':
13434 {
13435 int i;
13436 char *p;
13437
13438 if (command_loop_level > 5)
13439 return "[[[... ";
13440 p = decode_mode_spec_buf;
13441 for (i = 0; i < command_loop_level; i++)
13442 *p++ = '[';
13443 *p = 0;
13444 return decode_mode_spec_buf;
13445 }
13446
13447 case ']':
13448 {
13449 int i;
13450 char *p;
13451
13452 if (command_loop_level > 5)
13453 return " ...]]]";
13454 p = decode_mode_spec_buf;
13455 for (i = 0; i < command_loop_level; i++)
13456 *p++ = ']';
13457 *p = 0;
13458 return decode_mode_spec_buf;
13459 }
13460
13461 case '-':
13462 {
13463 register int i;
13464
13465 /* Let lots_of_dashes be a string of infinite length. */
13466 if (field_width <= 0
13467 || field_width > sizeof (lots_of_dashes))
13468 {
13469 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13470 decode_mode_spec_buf[i] = '-';
13471 decode_mode_spec_buf[i] = '\0';
13472 return decode_mode_spec_buf;
13473 }
13474 else
13475 return lots_of_dashes;
13476 }
13477
13478 case 'b':
13479 obj = b->name;
13480 break;
13481
13482 case 'c':
13483 {
13484 int col = current_column ();
13485 XSETFASTINT (w->column_number_displayed, col);
13486 pint2str (decode_mode_spec_buf, field_width, col);
13487 return decode_mode_spec_buf;
13488 }
13489
13490 case 'F':
13491 /* %F displays the frame name. */
13492 if (!NILP (f->title))
13493 return (char *) XSTRING (f->title)->data;
13494 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13495 return (char *) XSTRING (f->name)->data;
13496 return "Emacs";
13497
13498 case 'f':
13499 obj = b->filename;
13500 break;
13501
13502 case 'l':
13503 {
13504 int startpos = XMARKER (w->start)->charpos;
13505 int startpos_byte = marker_byte_position (w->start);
13506 int line, linepos, linepos_byte, topline;
13507 int nlines, junk;
13508 int height = XFASTINT (w->height);
13509
13510 /* If we decided that this buffer isn't suitable for line numbers,
13511 don't forget that too fast. */
13512 if (EQ (w->base_line_pos, w->buffer))
13513 goto no_value;
13514 /* But do forget it, if the window shows a different buffer now. */
13515 else if (BUFFERP (w->base_line_pos))
13516 w->base_line_pos = Qnil;
13517
13518 /* If the buffer is very big, don't waste time. */
13519 if (INTEGERP (Vline_number_display_limit)
13520 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13521 {
13522 w->base_line_pos = Qnil;
13523 w->base_line_number = Qnil;
13524 goto no_value;
13525 }
13526
13527 if (!NILP (w->base_line_number)
13528 && !NILP (w->base_line_pos)
13529 && XFASTINT (w->base_line_pos) <= startpos)
13530 {
13531 line = XFASTINT (w->base_line_number);
13532 linepos = XFASTINT (w->base_line_pos);
13533 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13534 }
13535 else
13536 {
13537 line = 1;
13538 linepos = BUF_BEGV (b);
13539 linepos_byte = BUF_BEGV_BYTE (b);
13540 }
13541
13542 /* Count lines from base line to window start position. */
13543 nlines = display_count_lines (linepos, linepos_byte,
13544 startpos_byte,
13545 startpos, &junk);
13546
13547 topline = nlines + line;
13548
13549 /* Determine a new base line, if the old one is too close
13550 or too far away, or if we did not have one.
13551 "Too close" means it's plausible a scroll-down would
13552 go back past it. */
13553 if (startpos == BUF_BEGV (b))
13554 {
13555 XSETFASTINT (w->base_line_number, topline);
13556 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13557 }
13558 else if (nlines < height + 25 || nlines > height * 3 + 50
13559 || linepos == BUF_BEGV (b))
13560 {
13561 int limit = BUF_BEGV (b);
13562 int limit_byte = BUF_BEGV_BYTE (b);
13563 int position;
13564 int distance = (height * 2 + 30) * line_number_display_limit_width;
13565
13566 if (startpos - distance > limit)
13567 {
13568 limit = startpos - distance;
13569 limit_byte = CHAR_TO_BYTE (limit);
13570 }
13571
13572 nlines = display_count_lines (startpos, startpos_byte,
13573 limit_byte,
13574 - (height * 2 + 30),
13575 &position);
13576 /* If we couldn't find the lines we wanted within
13577 line_number_display_limit_width chars per line,
13578 give up on line numbers for this window. */
13579 if (position == limit_byte && limit == startpos - distance)
13580 {
13581 w->base_line_pos = w->buffer;
13582 w->base_line_number = Qnil;
13583 goto no_value;
13584 }
13585
13586 XSETFASTINT (w->base_line_number, topline - nlines);
13587 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13588 }
13589
13590 /* Now count lines from the start pos to point. */
13591 nlines = display_count_lines (startpos, startpos_byte,
13592 PT_BYTE, PT, &junk);
13593
13594 /* Record that we did display the line number. */
13595 line_number_displayed = 1;
13596
13597 /* Make the string to show. */
13598 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13599 return decode_mode_spec_buf;
13600 no_value:
13601 {
13602 char* p = decode_mode_spec_buf;
13603 int pad = field_width - 2;
13604 while (pad-- > 0)
13605 *p++ = ' ';
13606 *p++ = '?';
13607 *p++ = '?';
13608 *p = '\0';
13609 return decode_mode_spec_buf;
13610 }
13611 }
13612 break;
13613
13614 case 'm':
13615 obj = b->mode_name;
13616 break;
13617
13618 case 'n':
13619 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13620 return " Narrow";
13621 break;
13622
13623 case 'p':
13624 {
13625 int pos = marker_position (w->start);
13626 int total = BUF_ZV (b) - BUF_BEGV (b);
13627
13628 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13629 {
13630 if (pos <= BUF_BEGV (b))
13631 return "All";
13632 else
13633 return "Bottom";
13634 }
13635 else if (pos <= BUF_BEGV (b))
13636 return "Top";
13637 else
13638 {
13639 if (total > 1000000)
13640 /* Do it differently for a large value, to avoid overflow. */
13641 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13642 else
13643 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13644 /* We can't normally display a 3-digit number,
13645 so get us a 2-digit number that is close. */
13646 if (total == 100)
13647 total = 99;
13648 sprintf (decode_mode_spec_buf, "%2d%%", total);
13649 return decode_mode_spec_buf;
13650 }
13651 }
13652
13653 /* Display percentage of size above the bottom of the screen. */
13654 case 'P':
13655 {
13656 int toppos = marker_position (w->start);
13657 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13658 int total = BUF_ZV (b) - BUF_BEGV (b);
13659
13660 if (botpos >= BUF_ZV (b))
13661 {
13662 if (toppos <= BUF_BEGV (b))
13663 return "All";
13664 else
13665 return "Bottom";
13666 }
13667 else
13668 {
13669 if (total > 1000000)
13670 /* Do it differently for a large value, to avoid overflow. */
13671 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13672 else
13673 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13674 /* We can't normally display a 3-digit number,
13675 so get us a 2-digit number that is close. */
13676 if (total == 100)
13677 total = 99;
13678 if (toppos <= BUF_BEGV (b))
13679 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13680 else
13681 sprintf (decode_mode_spec_buf, "%2d%%", total);
13682 return decode_mode_spec_buf;
13683 }
13684 }
13685
13686 case 's':
13687 /* status of process */
13688 obj = Fget_buffer_process (w->buffer);
13689 if (NILP (obj))
13690 return "no process";
13691 #ifdef subprocesses
13692 obj = Fsymbol_name (Fprocess_status (obj));
13693 #endif
13694 break;
13695
13696 case 't': /* indicate TEXT or BINARY */
13697 #ifdef MODE_LINE_BINARY_TEXT
13698 return MODE_LINE_BINARY_TEXT (b);
13699 #else
13700 return "T";
13701 #endif
13702
13703 case 'z':
13704 /* coding-system (not including end-of-line format) */
13705 case 'Z':
13706 /* coding-system (including end-of-line type) */
13707 {
13708 int eol_flag = (c == 'Z');
13709 char *p = decode_mode_spec_buf;
13710
13711 if (! FRAME_WINDOW_P (f))
13712 {
13713 /* No need to mention EOL here--the terminal never needs
13714 to do EOL conversion. */
13715 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13716 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13717 }
13718 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13719 p, eol_flag);
13720
13721 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13722 #ifdef subprocesses
13723 obj = Fget_buffer_process (Fcurrent_buffer ());
13724 if (PROCESSP (obj))
13725 {
13726 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13727 p, eol_flag);
13728 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13729 p, eol_flag);
13730 }
13731 #endif /* subprocesses */
13732 #endif /* 0 */
13733 *p = 0;
13734 return decode_mode_spec_buf;
13735 }
13736 }
13737
13738 if (STRINGP (obj))
13739 return (char *) XSTRING (obj)->data;
13740 else
13741 return "";
13742 }
13743
13744
13745 /* Count up to COUNT lines starting from START / START_BYTE.
13746 But don't go beyond LIMIT_BYTE.
13747 Return the number of lines thus found (always nonnegative).
13748
13749 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13750
13751 static int
13752 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13753 int start, start_byte, limit_byte, count;
13754 int *byte_pos_ptr;
13755 {
13756 register unsigned char *cursor;
13757 unsigned char *base;
13758
13759 register int ceiling;
13760 register unsigned char *ceiling_addr;
13761 int orig_count = count;
13762
13763 /* If we are not in selective display mode,
13764 check only for newlines. */
13765 int selective_display = (!NILP (current_buffer->selective_display)
13766 && !INTEGERP (current_buffer->selective_display));
13767
13768 if (count > 0)
13769 {
13770 while (start_byte < limit_byte)
13771 {
13772 ceiling = BUFFER_CEILING_OF (start_byte);
13773 ceiling = min (limit_byte - 1, ceiling);
13774 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13775 base = (cursor = BYTE_POS_ADDR (start_byte));
13776 while (1)
13777 {
13778 if (selective_display)
13779 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13780 ;
13781 else
13782 while (*cursor != '\n' && ++cursor != ceiling_addr)
13783 ;
13784
13785 if (cursor != ceiling_addr)
13786 {
13787 if (--count == 0)
13788 {
13789 start_byte += cursor - base + 1;
13790 *byte_pos_ptr = start_byte;
13791 return orig_count;
13792 }
13793 else
13794 if (++cursor == ceiling_addr)
13795 break;
13796 }
13797 else
13798 break;
13799 }
13800 start_byte += cursor - base;
13801 }
13802 }
13803 else
13804 {
13805 while (start_byte > limit_byte)
13806 {
13807 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13808 ceiling = max (limit_byte, ceiling);
13809 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13810 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13811 while (1)
13812 {
13813 if (selective_display)
13814 while (--cursor != ceiling_addr
13815 && *cursor != '\n' && *cursor != 015)
13816 ;
13817 else
13818 while (--cursor != ceiling_addr && *cursor != '\n')
13819 ;
13820
13821 if (cursor != ceiling_addr)
13822 {
13823 if (++count == 0)
13824 {
13825 start_byte += cursor - base + 1;
13826 *byte_pos_ptr = start_byte;
13827 /* When scanning backwards, we should
13828 not count the newline posterior to which we stop. */
13829 return - orig_count - 1;
13830 }
13831 }
13832 else
13833 break;
13834 }
13835 /* Here we add 1 to compensate for the last decrement
13836 of CURSOR, which took it past the valid range. */
13837 start_byte += cursor - base + 1;
13838 }
13839 }
13840
13841 *byte_pos_ptr = limit_byte;
13842
13843 if (count < 0)
13844 return - orig_count + count;
13845 return orig_count - count;
13846
13847 }
13848
13849
13850 \f
13851 /***********************************************************************
13852 Displaying strings
13853 ***********************************************************************/
13854
13855 /* Display a NUL-terminated string, starting with index START.
13856
13857 If STRING is non-null, display that C string. Otherwise, the Lisp
13858 string LISP_STRING is displayed.
13859
13860 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13861 FACE_STRING. Display STRING or LISP_STRING with the face at
13862 FACE_STRING_POS in FACE_STRING:
13863
13864 Display the string in the environment given by IT, but use the
13865 standard display table, temporarily.
13866
13867 FIELD_WIDTH is the minimum number of output glyphs to produce.
13868 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13869 with spaces. If STRING has more characters, more than FIELD_WIDTH
13870 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13871
13872 PRECISION is the maximum number of characters to output from
13873 STRING. PRECISION < 0 means don't truncate the string.
13874
13875 This is roughly equivalent to printf format specifiers:
13876
13877 FIELD_WIDTH PRECISION PRINTF
13878 ----------------------------------------
13879 -1 -1 %s
13880 -1 10 %.10s
13881 10 -1 %10s
13882 20 10 %20.10s
13883
13884 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13885 display them, and < 0 means obey the current buffer's value of
13886 enable_multibyte_characters.
13887
13888 Value is the number of glyphs produced. */
13889
13890 static int
13891 display_string (string, lisp_string, face_string, face_string_pos,
13892 start, it, field_width, precision, max_x, multibyte)
13893 unsigned char *string;
13894 Lisp_Object lisp_string;
13895 Lisp_Object face_string;
13896 int face_string_pos;
13897 int start;
13898 struct it *it;
13899 int field_width, precision, max_x;
13900 int multibyte;
13901 {
13902 int hpos_at_start = it->hpos;
13903 int saved_face_id = it->face_id;
13904 struct glyph_row *row = it->glyph_row;
13905
13906 /* Initialize the iterator IT for iteration over STRING beginning
13907 with index START. We assume that IT may be modified here (which
13908 means that display_line has to do something when displaying a
13909 mini-buffer prompt, which it does). */
13910 reseat_to_string (it, string, lisp_string, start,
13911 precision, field_width, multibyte);
13912
13913 /* If displaying STRING, set up the face of the iterator
13914 from LISP_STRING, if that's given. */
13915 if (STRINGP (face_string))
13916 {
13917 int endptr;
13918 struct face *face;
13919
13920 it->face_id
13921 = face_at_string_position (it->w, face_string, face_string_pos,
13922 0, it->region_beg_charpos,
13923 it->region_end_charpos,
13924 &endptr, it->base_face_id, 0);
13925 face = FACE_FROM_ID (it->f, it->face_id);
13926 it->face_box_p = face->box != FACE_NO_BOX;
13927 }
13928
13929 /* Set max_x to the maximum allowed X position. Don't let it go
13930 beyond the right edge of the window. */
13931 if (max_x <= 0)
13932 max_x = it->last_visible_x;
13933 else
13934 max_x = min (max_x, it->last_visible_x);
13935
13936 /* Skip over display elements that are not visible. because IT->w is
13937 hscrolled. */
13938 if (it->current_x < it->first_visible_x)
13939 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13940 MOVE_TO_POS | MOVE_TO_X);
13941
13942 row->ascent = it->max_ascent;
13943 row->height = it->max_ascent + it->max_descent;
13944 row->phys_ascent = it->max_phys_ascent;
13945 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13946
13947 /* This condition is for the case that we are called with current_x
13948 past last_visible_x. */
13949 while (it->current_x < max_x)
13950 {
13951 int x_before, x, n_glyphs_before, i, nglyphs;
13952
13953 /* Get the next display element. */
13954 if (!get_next_display_element (it))
13955 break;
13956
13957 /* Produce glyphs. */
13958 x_before = it->current_x;
13959 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13960 PRODUCE_GLYPHS (it);
13961
13962 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13963 i = 0;
13964 x = x_before;
13965 while (i < nglyphs)
13966 {
13967 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13968
13969 if (!it->truncate_lines_p
13970 && x + glyph->pixel_width > max_x)
13971 {
13972 /* End of continued line or max_x reached. */
13973 if (CHAR_GLYPH_PADDING_P (*glyph))
13974 {
13975 /* A wide character is unbreakable. */
13976 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
13977 it->current_x = x_before;
13978 }
13979 else
13980 {
13981 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13982 it->current_x = x;
13983 }
13984 break;
13985 }
13986 else if (x + glyph->pixel_width > it->first_visible_x)
13987 {
13988 /* Glyph is at least partially visible. */
13989 ++it->hpos;
13990 if (x < it->first_visible_x)
13991 it->glyph_row->x = x - it->first_visible_x;
13992 }
13993 else
13994 {
13995 /* Glyph is off the left margin of the display area.
13996 Should not happen. */
13997 abort ();
13998 }
13999
14000 row->ascent = max (row->ascent, it->max_ascent);
14001 row->height = max (row->height, it->max_ascent + it->max_descent);
14002 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14003 row->phys_height = max (row->phys_height,
14004 it->max_phys_ascent + it->max_phys_descent);
14005 x += glyph->pixel_width;
14006 ++i;
14007 }
14008
14009 /* Stop if max_x reached. */
14010 if (i < nglyphs)
14011 break;
14012
14013 /* Stop at line ends. */
14014 if (ITERATOR_AT_END_OF_LINE_P (it))
14015 {
14016 it->continuation_lines_width = 0;
14017 break;
14018 }
14019
14020 set_iterator_to_next (it, 1);
14021
14022 /* Stop if truncating at the right edge. */
14023 if (it->truncate_lines_p
14024 && it->current_x >= it->last_visible_x)
14025 {
14026 /* Add truncation mark, but don't do it if the line is
14027 truncated at a padding space. */
14028 if (IT_CHARPOS (*it) < it->string_nchars)
14029 {
14030 if (!FRAME_WINDOW_P (it->f))
14031 {
14032 int i, n;
14033
14034 if (it->current_x > it->last_visible_x)
14035 {
14036 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14037 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14038 break;
14039 for (n = row->used[TEXT_AREA]; i < n; ++i)
14040 {
14041 row->used[TEXT_AREA] = i;
14042 produce_special_glyphs (it, IT_TRUNCATION);
14043 }
14044 }
14045 produce_special_glyphs (it, IT_TRUNCATION);
14046 }
14047 it->glyph_row->truncated_on_right_p = 1;
14048 }
14049 break;
14050 }
14051 }
14052
14053 /* Maybe insert a truncation at the left. */
14054 if (it->first_visible_x
14055 && IT_CHARPOS (*it) > 0)
14056 {
14057 if (!FRAME_WINDOW_P (it->f))
14058 insert_left_trunc_glyphs (it);
14059 it->glyph_row->truncated_on_left_p = 1;
14060 }
14061
14062 it->face_id = saved_face_id;
14063
14064 /* Value is number of columns displayed. */
14065 return it->hpos - hpos_at_start;
14066 }
14067
14068
14069 \f
14070 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14071 appears as an element of LIST or as the car of an element of LIST.
14072 If PROPVAL is a list, compare each element against LIST in that
14073 way, and return 1 if any element of PROPVAL is found in LIST.
14074 Otherwise return 0. This function cannot quit. */
14075
14076 int
14077 invisible_p (propval, list)
14078 register Lisp_Object propval;
14079 Lisp_Object list;
14080 {
14081 register Lisp_Object tail, proptail;
14082
14083 for (tail = list; CONSP (tail); tail = XCDR (tail))
14084 {
14085 register Lisp_Object tem;
14086 tem = XCAR (tail);
14087 if (EQ (propval, tem))
14088 return 1;
14089 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14090 return 1;
14091 }
14092
14093 if (CONSP (propval))
14094 {
14095 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14096 {
14097 Lisp_Object propelt;
14098 propelt = XCAR (proptail);
14099 for (tail = list; CONSP (tail); tail = XCDR (tail))
14100 {
14101 register Lisp_Object tem;
14102 tem = XCAR (tail);
14103 if (EQ (propelt, tem))
14104 return 1;
14105 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14106 return 1;
14107 }
14108 }
14109 }
14110
14111 return 0;
14112 }
14113
14114
14115 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14116 the cdr of that element is non-nil. If PROPVAL is a list, check
14117 each element of PROPVAL in that way, and the first time some
14118 element is found, return 1 if the cdr of that element is non-nil.
14119 Otherwise return 0. This function cannot quit. */
14120
14121 int
14122 invisible_ellipsis_p (propval, list)
14123 register Lisp_Object propval;
14124 Lisp_Object list;
14125 {
14126 register Lisp_Object tail, proptail;
14127
14128 for (tail = list; CONSP (tail); tail = XCDR (tail))
14129 {
14130 register Lisp_Object tem;
14131 tem = XCAR (tail);
14132 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14133 return ! NILP (XCDR (tem));
14134 }
14135
14136 if (CONSP (propval))
14137 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14138 {
14139 Lisp_Object propelt;
14140 propelt = XCAR (proptail);
14141 for (tail = list; CONSP (tail); tail = XCDR (tail))
14142 {
14143 register Lisp_Object tem;
14144 tem = XCAR (tail);
14145 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14146 return ! NILP (XCDR (tem));
14147 }
14148 }
14149
14150 return 0;
14151 }
14152
14153
14154 \f
14155 /***********************************************************************
14156 Initialization
14157 ***********************************************************************/
14158
14159 void
14160 syms_of_xdisp ()
14161 {
14162 Vwith_echo_area_save_vector = Qnil;
14163 staticpro (&Vwith_echo_area_save_vector);
14164
14165 Vmessage_stack = Qnil;
14166 staticpro (&Vmessage_stack);
14167
14168 Qinhibit_redisplay = intern ("inhibit-redisplay");
14169 staticpro (&Qinhibit_redisplay);
14170
14171 #if GLYPH_DEBUG
14172 defsubr (&Sdump_glyph_matrix);
14173 defsubr (&Sdump_glyph_row);
14174 defsubr (&Sdump_tool_bar_row);
14175 defsubr (&Strace_redisplay_toggle);
14176 defsubr (&Strace_to_stderr);
14177 #endif
14178 #ifdef HAVE_WINDOW_SYSTEM
14179 defsubr (&Stool_bar_lines_needed);
14180 #endif
14181
14182 staticpro (&Qmenu_bar_update_hook);
14183 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14184
14185 staticpro (&Qoverriding_terminal_local_map);
14186 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14187
14188 staticpro (&Qoverriding_local_map);
14189 Qoverriding_local_map = intern ("overriding-local-map");
14190
14191 staticpro (&Qwindow_scroll_functions);
14192 Qwindow_scroll_functions = intern ("window-scroll-functions");
14193
14194 staticpro (&Qredisplay_end_trigger_functions);
14195 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14196
14197 staticpro (&Qinhibit_point_motion_hooks);
14198 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14199
14200 QCdata = intern (":data");
14201 staticpro (&QCdata);
14202 Qdisplay = intern ("display");
14203 staticpro (&Qdisplay);
14204 Qspace_width = intern ("space-width");
14205 staticpro (&Qspace_width);
14206 Qraise = intern ("raise");
14207 staticpro (&Qraise);
14208 Qspace = intern ("space");
14209 staticpro (&Qspace);
14210 Qmargin = intern ("margin");
14211 staticpro (&Qmargin);
14212 Qleft_margin = intern ("left-margin");
14213 staticpro (&Qleft_margin);
14214 Qright_margin = intern ("right-margin");
14215 staticpro (&Qright_margin);
14216 Qalign_to = intern ("align-to");
14217 staticpro (&Qalign_to);
14218 QCalign_to = intern (":align-to");
14219 staticpro (&QCalign_to);
14220 Qrelative_width = intern ("relative-width");
14221 staticpro (&Qrelative_width);
14222 QCrelative_width = intern (":relative-width");
14223 staticpro (&QCrelative_width);
14224 QCrelative_height = intern (":relative-height");
14225 staticpro (&QCrelative_height);
14226 QCeval = intern (":eval");
14227 staticpro (&QCeval);
14228 Qwhen = intern ("when");
14229 staticpro (&Qwhen);
14230 QCfile = intern (":file");
14231 staticpro (&QCfile);
14232 Qfontified = intern ("fontified");
14233 staticpro (&Qfontified);
14234 Qfontification_functions = intern ("fontification-functions");
14235 staticpro (&Qfontification_functions);
14236 Qtrailing_whitespace = intern ("trailing-whitespace");
14237 staticpro (&Qtrailing_whitespace);
14238 Qimage = intern ("image");
14239 staticpro (&Qimage);
14240 Qmessage_truncate_lines = intern ("message-truncate-lines");
14241 staticpro (&Qmessage_truncate_lines);
14242 Qgrow_only = intern ("grow-only");
14243 staticpro (&Qgrow_only);
14244 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14245 staticpro (&Qinhibit_menubar_update);
14246
14247 last_arrow_position = Qnil;
14248 last_arrow_string = Qnil;
14249 staticpro (&last_arrow_position);
14250 staticpro (&last_arrow_string);
14251
14252 echo_buffer[0] = echo_buffer[1] = Qnil;
14253 staticpro (&echo_buffer[0]);
14254 staticpro (&echo_buffer[1]);
14255
14256 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14257 staticpro (&echo_area_buffer[0]);
14258 staticpro (&echo_area_buffer[1]);
14259
14260 Vmessages_buffer_name = build_string ("*Messages*");
14261 staticpro (&Vmessages_buffer_name);
14262
14263 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14264 "Non-nil means highlight trailing whitespace.\n\
14265 The face used for trailing whitespace is `trailing-whitespace'.");
14266 Vshow_trailing_whitespace = Qnil;
14267
14268 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14269 "Non-nil means don't actually do any redisplay.\n\
14270 This is used for internal purposes.");
14271 Vinhibit_redisplay = Qnil;
14272
14273 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14274 "String (or mode line construct) included (normally) in `mode-line-format'.");
14275 Vglobal_mode_string = Qnil;
14276
14277 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14278 "Marker for where to display an arrow on top of the buffer text.\n\
14279 This must be the beginning of a line in order to work.\n\
14280 See also `overlay-arrow-string'.");
14281 Voverlay_arrow_position = Qnil;
14282
14283 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14284 "String to display as an arrow. See also `overlay-arrow-position'.");
14285 Voverlay_arrow_string = Qnil;
14286
14287 DEFVAR_INT ("scroll-step", &scroll_step,
14288 "*The number of lines to try scrolling a window by when point moves out.\n\
14289 If that fails to bring point back on frame, point is centered instead.\n\
14290 If this is zero, point is always centered after it moves off frame.\n\
14291 If you want scrolling to always be a line at a time, you should set\n\
14292 `scroll-conservatively' to a large value rather than set this to 1.");
14293
14294 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14295 "*Scroll up to this many lines, to bring point back on screen.\n\
14296 A value of zero means to scroll the text to center point vertically\n\
14297 in the window.");
14298 scroll_conservatively = 0;
14299
14300 DEFVAR_INT ("scroll-margin", &scroll_margin,
14301 "*Number of lines of margin at the top and bottom of a window.\n\
14302 Recenter the window whenever point gets within this many lines\n\
14303 of the top or bottom of the window.");
14304 scroll_margin = 0;
14305
14306 #if GLYPH_DEBUG
14307 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14308 #endif
14309
14310 DEFVAR_BOOL ("truncate-partial-width-windows",
14311 &truncate_partial_width_windows,
14312 "*Non-nil means truncate lines in all windows less than full frame wide.");
14313 truncate_partial_width_windows = 1;
14314
14315 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14316 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14317 Any other value means to use the appropriate face, `mode-line',\n\
14318 `header-line', or `menu' respectively.\n\
14319 \n\
14320 This variable is deprecated; please change the above faces instead.");
14321 mode_line_inverse_video = 1;
14322
14323 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14324 "*Maximum buffer size for which line number should be displayed.\n\
14325 If the buffer is bigger than this, the line number does not appear\n\
14326 in the mode line. A value of nil means no limit.");
14327 Vline_number_display_limit = Qnil;
14328
14329 DEFVAR_INT ("line-number-display-limit-width",
14330 &line_number_display_limit_width,
14331 "*Maximum line width (in characters) for line number display.\n\
14332 If the average length of the lines near point is bigger than this, then the\n\
14333 line number may be omitted from the mode line.");
14334 line_number_display_limit_width = 200;
14335
14336 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14337 "*Non-nil means highlight region even in nonselected windows.");
14338 highlight_nonselected_windows = 0;
14339
14340 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14341 "Non-nil if more than one frame is visible on this display.\n\
14342 Minibuffer-only frames don't count, but iconified frames do.\n\
14343 This variable is not guaranteed to be accurate except while processing\n\
14344 `frame-title-format' and `icon-title-format'.");
14345
14346 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14347 "Template for displaying the title bar of visible frames.\n\
14348 \(Assuming the window manager supports this feature.)\n\
14349 This variable has the same structure as `mode-line-format' (which see),\n\
14350 and is used only on frames for which no explicit name has been set\n\
14351 \(see `modify-frame-parameters').");
14352 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14353 "Template for displaying the title bar of an iconified frame.\n\
14354 \(Assuming the window manager supports this feature.)\n\
14355 This variable has the same structure as `mode-line-format' (which see),\n\
14356 and is used only on frames for which no explicit name has been set\n\
14357 \(see `modify-frame-parameters').");
14358 Vicon_title_format
14359 = Vframe_title_format
14360 = Fcons (intern ("multiple-frames"),
14361 Fcons (build_string ("%b"),
14362 Fcons (Fcons (build_string (""),
14363 Fcons (intern ("invocation-name"),
14364 Fcons (build_string ("@"),
14365 Fcons (intern ("system-name"),
14366 Qnil)))),
14367 Qnil)));
14368
14369 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14370 "Maximum number of lines to keep in the message log buffer.\n\
14371 If nil, disable message logging. If t, log messages but don't truncate\n\
14372 the buffer when it becomes large.");
14373 XSETFASTINT (Vmessage_log_max, 50);
14374
14375 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14376 "Functions called before redisplay, if window sizes have changed.\n\
14377 The value should be a list of functions that take one argument.\n\
14378 Just before redisplay, for each frame, if any of its windows have changed\n\
14379 size since the last redisplay, or have been split or deleted,\n\
14380 all the functions in the list are called, with the frame as argument.");
14381 Vwindow_size_change_functions = Qnil;
14382
14383 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14384 "List of Functions to call before redisplaying a window with scrolling.\n\
14385 Each function is called with two arguments, the window\n\
14386 and its new display-start position. Note that the value of `window-end'\n\
14387 is not valid when these functions are called.");
14388 Vwindow_scroll_functions = Qnil;
14389
14390 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14391 "*Non-nil means automatically resize tool-bars.\n\
14392 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14393 It decreases a tool-bar's height when it would display blank lines\n\
14394 otherwise.");
14395 auto_resize_tool_bars_p = 1;
14396
14397 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14398 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14399 auto_raise_tool_bar_buttons_p = 1;
14400
14401 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14402 "*Margin around tool-bar buttons in pixels.\n\
14403 If an integer, use that for both horizontal and vertical margins.\n\
14404 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14405 HORZ specifying the horizontal margin, and VERT specifying the\n\
14406 vertical margin.");
14407 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14408
14409 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14410 "Relief thickness of tool-bar buttons.");
14411 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14412
14413 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14414 "List of functions to call to fontify regions of text.\n\
14415 Each function is called with one argument POS. Functions must\n\
14416 fontify a region starting at POS in the current buffer, and give\n\
14417 fontified regions the property `fontified'.\n\
14418 This variable automatically becomes buffer-local when set.");
14419 Vfontification_functions = Qnil;
14420 Fmake_variable_buffer_local (Qfontification_functions);
14421
14422 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14423 &unibyte_display_via_language_environment,
14424 "*Non-nil means display unibyte text according to language environment.\n\
14425 Specifically this means that unibyte non-ASCII characters\n\
14426 are displayed by converting them to the equivalent multibyte characters\n\
14427 according to the current language environment. As a result, they are\n\
14428 displayed according to the current fontset.");
14429 unibyte_display_via_language_environment = 0;
14430
14431 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14432 "*Maximum height for resizing mini-windows.\n\
14433 If a float, it specifies a fraction of the mini-window frame's height.\n\
14434 If an integer, it specifies a number of lines.");
14435 Vmax_mini_window_height = make_float (0.25);
14436
14437 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14438 "*How to resize mini-windows.\n\
14439 A value of nil means don't automatically resize mini-windows.\n\
14440 A value of t means resize them to fit the text displayed in them.\n\
14441 A value of `grow-only', the default, means let mini-windows grow\n\
14442 only, until their display becomes empty, at which point the windows\n\
14443 go back to their normal size.");
14444 Vresize_mini_windows = Qgrow_only;
14445
14446 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14447 &cursor_in_non_selected_windows,
14448 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14449 Nil means don't display a cursor there.");
14450 cursor_in_non_selected_windows = 1;
14451
14452 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14453 "*Non-nil means scroll the display automatically to make point visible.");
14454 automatic_hscrolling_p = 1;
14455
14456 DEFVAR_LISP ("image-types", &Vimage_types,
14457 "List of supported image types.\n\
14458 Each element of the list is a symbol for a supported image type.");
14459 Vimage_types = Qnil;
14460
14461 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14462 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14463 Bind this around calls to `message' to let it take effect.");
14464 message_truncate_lines = 0;
14465
14466 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14467 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14468 Can be used to update submenus whose contents should vary.");
14469 Vmenu_bar_update_hook = Qnil;
14470
14471 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14472 "Non-nil means don't update menu bars. Internal use only.");
14473 inhibit_menubar_update = 0;
14474 }
14475
14476
14477 /* Initialize this module when Emacs starts. */
14478
14479 void
14480 init_xdisp ()
14481 {
14482 Lisp_Object root_window;
14483 struct window *mini_w;
14484
14485 current_header_line_height = current_mode_line_height = -1;
14486
14487 CHARPOS (this_line_start_pos) = 0;
14488
14489 mini_w = XWINDOW (minibuf_window);
14490 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14491
14492 if (!noninteractive)
14493 {
14494 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14495 int i;
14496
14497 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
14498 set_window_height (root_window,
14499 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14500 0);
14501 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
14502 set_window_height (minibuf_window, 1, 0);
14503
14504 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
14505 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
14506
14507 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14508 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14509 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14510
14511 /* The default ellipsis glyphs `...'. */
14512 for (i = 0; i < 3; ++i)
14513 XSETFASTINT (default_invis_vector[i], '.');
14514 }
14515
14516 #ifdef HAVE_WINDOW_SYSTEM
14517 {
14518 /* Allocate the buffer for frame titles. */
14519 int size = 100;
14520 frame_title_buf = (char *) xmalloc (size);
14521 frame_title_buf_end = frame_title_buf + size;
14522 frame_title_ptr = NULL;
14523 }
14524 #endif /* HAVE_WINDOW_SYSTEM */
14525
14526 help_echo_showing_p = 0;
14527 }
14528
14529