]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(x_alloc_lighter_color_for_widget): New function.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
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
198 #define min(a, b) ((a) < (b) ? (a) : (b))
199 #define max(a, b) ((a) > (b) ? (a) : (b))
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
204 extern void set_frame_menubar P_ ((struct frame *f, int, int));
205 extern int pending_menu_activation;
206 #endif
207
208 extern int interrupt_input;
209 extern int command_loop_level;
210
211 extern int minibuffer_auto_raise;
212
213 extern Lisp_Object Qface;
214
215 extern Lisp_Object Voverriding_local_map;
216 extern Lisp_Object Voverriding_local_map_menu_flag;
217 extern Lisp_Object Qmenu_item;
218
219 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
220 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 Lisp_Object Qredisplay_end_trigger_functions;
222 Lisp_Object Qinhibit_point_motion_hooks;
223 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
224 Lisp_Object Qfontified;
225
226 /* Functions called to fontify regions of text. */
227
228 Lisp_Object Vfontification_functions;
229 Lisp_Object Qfontification_functions;
230
231 /* Non-zero means draw tool bar buttons raised when the mouse moves
232 over them. */
233
234 int auto_raise_tool_bar_buttons_p;
235
236 /* Margin around tool bar buttons in pixels. */
237
238 int tool_bar_button_margin;
239
240 /* Thickness of shadow to draw around tool bar buttons. */
241
242 int tool_bar_button_relief;
243
244 /* Non-zero means automatically resize tool-bars so that all tool-bar
245 items are visible, and no blank lines remain. */
246
247 int auto_resize_tool_bars_p;
248
249 /* Non-nil means don't actually do any redisplay. */
250
251 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
252
253 /* Names of text properties relevant for redisplay. */
254
255 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
256 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
257
258 /* Symbols used in text property values. */
259
260 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
261 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
262 Lisp_Object Qmargin;
263 extern Lisp_Object Qheight;
264
265 /* Non-nil means highlight trailing whitespace. */
266
267 Lisp_Object Vshow_trailing_whitespace;
268
269 /* Name of the face used to highlight trailing whitespace. */
270
271 Lisp_Object Qtrailing_whitespace;
272
273 /* The symbol `image' which is the car of the lists used to represent
274 images in Lisp. */
275
276 Lisp_Object Qimage;
277
278 /* Non-zero means print newline to stdout before next mini-buffer
279 message. */
280
281 int noninteractive_need_newline;
282
283 /* Non-zero means print newline to message log before next message. */
284
285 static int message_log_need_newline;
286
287 \f
288 /* The buffer position of the first character appearing entirely or
289 partially on the line of the selected window which contains the
290 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
291 redisplay optimization in redisplay_internal. */
292
293 static struct text_pos this_line_start_pos;
294
295 /* Number of characters past the end of the line above, including the
296 terminating newline. */
297
298 static struct text_pos this_line_end_pos;
299
300 /* The vertical positions and the height of this line. */
301
302 static int this_line_vpos;
303 static int this_line_y;
304 static int this_line_pixel_height;
305
306 /* X position at which this display line starts. Usually zero;
307 negative if first character is partially visible. */
308
309 static int this_line_start_x;
310
311 /* Buffer that this_line_.* variables are referring to. */
312
313 static struct buffer *this_line_buffer;
314
315 /* Nonzero means truncate lines in all windows less wide than the
316 frame. */
317
318 int truncate_partial_width_windows;
319
320 /* A flag to control how to display unibyte 8-bit character. */
321
322 int unibyte_display_via_language_environment;
323
324 /* Nonzero means we have more than one non-mini-buffer-only frame.
325 Not guaranteed to be accurate except while parsing
326 frame-title-format. */
327
328 int multiple_frames;
329
330 Lisp_Object Vglobal_mode_string;
331
332 /* Marker for where to display an arrow on top of the buffer text. */
333
334 Lisp_Object Voverlay_arrow_position;
335
336 /* String to display for the arrow. Only used on terminal frames. */
337
338 Lisp_Object Voverlay_arrow_string;
339
340 /* Values of those variables at last redisplay. However, if
341 Voverlay_arrow_position is a marker, last_arrow_position is its
342 numerical position. */
343
344 static Lisp_Object last_arrow_position, last_arrow_string;
345
346 /* Like mode-line-format, but for the title bar on a visible frame. */
347
348 Lisp_Object Vframe_title_format;
349
350 /* Like mode-line-format, but for the title bar on an iconified frame. */
351
352 Lisp_Object Vicon_title_format;
353
354 /* List of functions to call when a window's size changes. These
355 functions get one arg, a frame on which one or more windows' sizes
356 have changed. */
357
358 static Lisp_Object Vwindow_size_change_functions;
359
360 Lisp_Object Qmenu_bar_update_hook;
361
362 /* Nonzero if overlay arrow has been displayed once in this window. */
363
364 static int overlay_arrow_seen;
365
366 /* Nonzero means highlight the region even in nonselected windows. */
367
368 int highlight_nonselected_windows;
369
370 /* If cursor motion alone moves point off frame, try scrolling this
371 many lines up or down if that will bring it back. */
372
373 static int scroll_step;
374
375 /* Non-0 means scroll just far enough to bring point back on the
376 screen, when appropriate. */
377
378 static int scroll_conservatively;
379
380 /* Recenter the window whenever point gets within this many lines of
381 the top or bottom of the window. This value is translated into a
382 pixel value by multiplying it with CANON_Y_UNIT, which means that
383 there is really a fixed pixel height scroll margin. */
384
385 int scroll_margin;
386
387 /* Number of windows showing the buffer of the selected window (or
388 another buffer with the same base buffer). keyboard.c refers to
389 this. */
390
391 int buffer_shared;
392
393 /* Vector containing glyphs for an ellipsis `...'. */
394
395 static Lisp_Object default_invis_vector[3];
396
397 /* Nonzero means display mode line highlighted. */
398
399 int mode_line_inverse_video;
400
401 /* Prompt to display in front of the mini-buffer contents. */
402
403 Lisp_Object minibuf_prompt;
404
405 /* Width of current mini-buffer prompt. Only set after display_line
406 of the line that contains the prompt. */
407
408 int minibuf_prompt_width;
409 int minibuf_prompt_pixel_width;
410
411 /* This is the window where the echo area message was displayed. It
412 is always a mini-buffer window, but it may not be the same window
413 currently active as a mini-buffer. */
414
415 Lisp_Object echo_area_window;
416
417 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
418 pushes the current message and the value of
419 message_enable_multibyte on the stack, the function restore_message
420 pops the stack and displays MESSAGE again. */
421
422 Lisp_Object Vmessage_stack;
423
424 /* Nonzero means multibyte characters were enabled when the echo area
425 message was specified. */
426
427 int message_enable_multibyte;
428
429 /* True if we should redraw the mode lines on the next redisplay. */
430
431 int update_mode_lines;
432
433 /* Nonzero if window sizes or contents have changed since last
434 redisplay that finished */
435
436 int windows_or_buffers_changed;
437
438 /* Nonzero after display_mode_line if %l was used and it displayed a
439 line number. */
440
441 int line_number_displayed;
442
443 /* Maximum buffer size for which to display line numbers. */
444
445 Lisp_Object Vline_number_display_limit;
446
447 /* line width to consider when repostioning for line number display */
448
449 static int line_number_display_limit_width;
450
451 /* Number of lines to keep in the message log buffer. t means
452 infinite. nil means don't log at all. */
453
454 Lisp_Object Vmessage_log_max;
455
456 /* The name of the *Messages* buffer, a string. */
457
458 static Lisp_Object Vmessages_buffer_name;
459
460 /* Current, index 0, and last displayed echo area message. Either
461 buffers from echo_buffers, or nil to indicate no message. */
462
463 Lisp_Object echo_area_buffer[2];
464
465 /* The buffers referenced from echo_area_buffer. */
466
467 static Lisp_Object echo_buffer[2];
468
469 /* A vector saved used in with_area_buffer to reduce consing. */
470
471 static Lisp_Object Vwith_echo_area_save_vector;
472
473 /* Non-zero means display_echo_area should display the last echo area
474 message again. Set by redisplay_preserve_echo_area. */
475
476 static int display_last_displayed_message_p;
477
478 /* Nonzero if echo area is being used by print; zero if being used by
479 message. */
480
481 int message_buf_print;
482
483 /* Maximum height for resizing mini-windows. Either a float
484 specifying a fraction of the available height, or an integer
485 specifying a number of lines. */
486
487 Lisp_Object Vmax_mini_window_height;
488
489 /* Non-zero means messages should be displayed with truncated
490 lines instead of being continued. */
491
492 int message_truncate_lines;
493 Lisp_Object Qmessage_truncate_lines;
494
495 /* Non-zero means we want a hollow cursor in windows that are not
496 selected. Zero means there's no cursor in such windows. */
497
498 int cursor_in_non_selected_windows;
499
500 /* A scratch glyph row with contents used for generating truncation
501 glyphs. Also used in direct_output_for_insert. */
502
503 #define MAX_SCRATCH_GLYPHS 100
504 struct glyph_row scratch_glyph_row;
505 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
506
507 /* Ascent and height of the last line processed by move_it_to. */
508
509 static int last_max_ascent, last_height;
510
511 /* The maximum distance to look ahead for text properties. Values
512 that are too small let us call compute_char_face and similar
513 functions too often which is expensive. Values that are too large
514 let us call compute_char_face and alike too often because we
515 might not be interested in text properties that far away. */
516
517 #define TEXT_PROP_DISTANCE_LIMIT 100
518
519 #if GLYPH_DEBUG
520
521 /* Non-zero means print traces of redisplay if compiled with
522 GLYPH_DEBUG != 0. */
523
524 int trace_redisplay_p;
525
526 #endif /* GLYPH_DEBUG */
527
528 #ifdef DEBUG_TRACE_MOVE
529 /* Non-zero means trace with TRACE_MOVE to stderr. */
530 int trace_move;
531
532 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
533 #else
534 #define TRACE_MOVE(x) (void) 0
535 #endif
536
537 /* Non-zero means automatically scroll windows horizontally to make
538 point visible. */
539
540 int automatic_hscrolling_p;
541
542 /* A list of symbols, one for each supported image type. */
543
544 Lisp_Object Vimage_types;
545
546 /* Value returned from text property handlers (see below). */
547
548 enum prop_handled
549 {
550 HANDLED_NORMALLY,
551 HANDLED_RECOMPUTE_PROPS,
552 HANDLED_OVERLAY_STRING_CONSUMED,
553 HANDLED_RETURN
554 };
555
556 /* A description of text properties that redisplay is interested
557 in. */
558
559 struct props
560 {
561 /* The name of the property. */
562 Lisp_Object *name;
563
564 /* A unique index for the property. */
565 enum prop_idx idx;
566
567 /* A handler function called to set up iterator IT from the property
568 at IT's current position. Value is used to steer handle_stop. */
569 enum prop_handled (*handler) P_ ((struct it *it));
570 };
571
572 static enum prop_handled handle_face_prop P_ ((struct it *));
573 static enum prop_handled handle_invisible_prop P_ ((struct it *));
574 static enum prop_handled handle_display_prop P_ ((struct it *));
575 static enum prop_handled handle_composition_prop P_ ((struct it *));
576 static enum prop_handled handle_overlay_change P_ ((struct it *));
577 static enum prop_handled handle_fontified_prop P_ ((struct it *));
578
579 /* Properties handled by iterators. */
580
581 static struct props it_props[] =
582 {
583 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
584 /* Handle `face' before `display' because some sub-properties of
585 `display' need to know the face. */
586 {&Qface, FACE_PROP_IDX, handle_face_prop},
587 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
588 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
589 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
590 {NULL, 0, NULL}
591 };
592
593 /* Value is the position described by X. If X is a marker, value is
594 the marker_position of X. Otherwise, value is X. */
595
596 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
597
598 /* Enumeration returned by some move_it_.* functions internally. */
599
600 enum move_it_result
601 {
602 /* Not used. Undefined value. */
603 MOVE_UNDEFINED,
604
605 /* Move ended at the requested buffer position or ZV. */
606 MOVE_POS_MATCH_OR_ZV,
607
608 /* Move ended at the requested X pixel position. */
609 MOVE_X_REACHED,
610
611 /* Move within a line ended at the end of a line that must be
612 continued. */
613 MOVE_LINE_CONTINUED,
614
615 /* Move within a line ended at the end of a line that would
616 be displayed truncated. */
617 MOVE_LINE_TRUNCATED,
618
619 /* Move within a line ended at a line end. */
620 MOVE_NEWLINE_OR_CR
621 };
622
623
624 \f
625 /* Function prototypes. */
626
627 static int redisplay_mode_lines P_ ((Lisp_Object, int));
628 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
629 static int invisible_text_between_p P_ ((struct it *, int, int));
630 static int next_element_from_ellipsis P_ ((struct it *));
631 static void pint2str P_ ((char *, int, int));
632 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
633 struct text_pos));
634 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
635 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
636 static void store_frame_title_char P_ ((char));
637 static int store_frame_title P_ ((unsigned char *, int, int));
638 static void x_consider_frame_title P_ ((Lisp_Object));
639 static void handle_stop P_ ((struct it *));
640 static int tool_bar_lines_needed P_ ((struct frame *));
641 static int single_display_prop_intangible_p P_ ((Lisp_Object));
642 static void ensure_echo_area_buffers P_ ((void));
643 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
644 struct glyph_row *,
645 struct glyph_row *));
646 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
647 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
648 static int with_echo_area_buffer P_ ((struct window *, int,
649 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
650 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
651 static void clear_garbaged_frames P_ ((void));
652 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
653 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
654 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
655 static int display_echo_area P_ ((struct window *));
656 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
657 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
658 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
659 static int string_char_and_length P_ ((unsigned char *, int, int *));
660 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
661 struct text_pos));
662 static int compute_window_start_on_continuation_line P_ ((struct window *));
663 static Lisp_Object eval_handler P_ ((Lisp_Object));
664 static void insert_left_trunc_glyphs P_ ((struct it *));
665 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
666 static void extend_face_to_end_of_line P_ ((struct it *));
667 static int append_space P_ ((struct it *, int));
668 static void make_cursor_line_fully_visible P_ ((struct window *));
669 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
670 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
671 static int trailing_whitespace_p P_ ((int));
672 static int message_log_check_duplicate P_ ((int, int, int, int));
673 int invisible_p P_ ((Lisp_Object, Lisp_Object));
674 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
675 static void push_it P_ ((struct it *));
676 static void pop_it P_ ((struct it *));
677 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
678 static void redisplay_internal P_ ((int));
679 static int echo_area_display P_ ((int));
680 static void redisplay_windows P_ ((Lisp_Object));
681 static void redisplay_window P_ ((Lisp_Object, int));
682 static void update_menu_bar P_ ((struct frame *, int));
683 static int try_window_reusing_current_matrix P_ ((struct window *));
684 static int try_window_id P_ ((struct window *));
685 static int display_line P_ ((struct it *));
686 static int display_mode_lines P_ ((struct window *));
687 static void display_mode_line P_ ((struct window *, enum face_id,
688 Lisp_Object));
689 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
690 static char *decode_mode_spec P_ ((struct window *, int, int, int));
691 static void display_menu_bar P_ ((struct window *));
692 static int display_count_lines P_ ((int, int, int, int, int *));
693 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
694 int, int, struct it *, int, int, int, int));
695 static void compute_line_metrics P_ ((struct it *));
696 static void run_redisplay_end_trigger_hook P_ ((struct it *));
697 static int get_overlay_strings P_ ((struct it *));
698 static void next_overlay_string P_ ((struct it *));
699 void set_iterator_to_next P_ ((struct it *));
700 static void reseat P_ ((struct it *, struct text_pos, int));
701 static void reseat_1 P_ ((struct it *, struct text_pos, int));
702 static void back_to_previous_visible_line_start P_ ((struct it *));
703 static void reseat_at_previous_visible_line_start P_ ((struct it *));
704 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
705 static int next_element_from_display_vector P_ ((struct it *));
706 static int next_element_from_string P_ ((struct it *));
707 static int next_element_from_c_string P_ ((struct it *));
708 static int next_element_from_buffer P_ ((struct it *));
709 static int next_element_from_composition P_ ((struct it *));
710 static int next_element_from_image P_ ((struct it *));
711 static int next_element_from_stretch P_ ((struct it *));
712 static void load_overlay_strings P_ ((struct it *));
713 static void init_from_display_pos P_ ((struct it *, struct window *,
714 struct display_pos *));
715 static void reseat_to_string P_ ((struct it *, unsigned char *,
716 Lisp_Object, int, int, int, int));
717 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
718 int, int, int));
719 void move_it_vertically_backward P_ ((struct it *, int));
720 static void init_to_row_start P_ ((struct it *, struct window *,
721 struct glyph_row *));
722 static void init_to_row_end P_ ((struct it *, struct window *,
723 struct glyph_row *));
724 static void back_to_previous_line_start P_ ((struct it *));
725 static void forward_to_next_line_start P_ ((struct it *));
726 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
727 Lisp_Object, int));
728 static struct text_pos string_pos P_ ((int, Lisp_Object));
729 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
730 static int number_of_chars P_ ((unsigned char *, int));
731 static void compute_stop_pos P_ ((struct it *));
732 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
733 Lisp_Object));
734 static int face_before_or_after_it_pos P_ ((struct it *, int));
735 static int next_overlay_change P_ ((int));
736 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
737 Lisp_Object, struct text_pos *));
738
739 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
740 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
741
742 #ifdef HAVE_WINDOW_SYSTEM
743
744 static void update_tool_bar P_ ((struct frame *, int));
745 static void build_desired_tool_bar_string P_ ((struct frame *f));
746 static int redisplay_tool_bar P_ ((struct frame *));
747 static void display_tool_bar_line P_ ((struct it *));
748
749 #endif /* HAVE_WINDOW_SYSTEM */
750
751 \f
752 /***********************************************************************
753 Window display dimensions
754 ***********************************************************************/
755
756 /* Return the window-relative maximum y + 1 for glyph rows displaying
757 text in window W. This is the height of W minus the height of a
758 mode line, if any. */
759
760 INLINE int
761 window_text_bottom_y (w)
762 struct window *w;
763 {
764 struct frame *f = XFRAME (w->frame);
765 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
766
767 if (WINDOW_WANTS_MODELINE_P (w))
768 height -= CURRENT_MODE_LINE_HEIGHT (w);
769 return height;
770 }
771
772
773 /* Return the pixel width of display area AREA of window W. AREA < 0
774 means return the total width of W, not including bitmap areas to
775 the left and right of the window. */
776
777 INLINE int
778 window_box_width (w, area)
779 struct window *w;
780 int area;
781 {
782 struct frame *f = XFRAME (w->frame);
783 int width = XFASTINT (w->width);
784
785 if (!w->pseudo_window_p)
786 {
787 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
788
789 if (area == TEXT_AREA)
790 {
791 if (INTEGERP (w->left_margin_width))
792 width -= XFASTINT (w->left_margin_width);
793 if (INTEGERP (w->right_margin_width))
794 width -= XFASTINT (w->right_margin_width);
795 }
796 else if (area == LEFT_MARGIN_AREA)
797 width = (INTEGERP (w->left_margin_width)
798 ? XFASTINT (w->left_margin_width) : 0);
799 else if (area == RIGHT_MARGIN_AREA)
800 width = (INTEGERP (w->right_margin_width)
801 ? XFASTINT (w->right_margin_width) : 0);
802 }
803
804 return width * CANON_X_UNIT (f);
805 }
806
807
808 /* Return the pixel height of the display area of window W, not
809 including mode lines of W, if any.. */
810
811 INLINE int
812 window_box_height (w)
813 struct window *w;
814 {
815 struct frame *f = XFRAME (w->frame);
816 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
817
818 if (WINDOW_WANTS_MODELINE_P (w))
819 height -= CURRENT_MODE_LINE_HEIGHT (w);
820
821 if (WINDOW_WANTS_HEADER_LINE_P (w))
822 height -= CURRENT_HEADER_LINE_HEIGHT (w);
823
824 return height;
825 }
826
827
828 /* Return the frame-relative coordinate of the left edge of display
829 area AREA of window W. AREA < 0 means return the left edge of the
830 whole window, to the right of any bitmap area at the left side of
831 W. */
832
833 INLINE int
834 window_box_left (w, area)
835 struct window *w;
836 int area;
837 {
838 struct frame *f = XFRAME (w->frame);
839 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
840
841 if (!w->pseudo_window_p)
842 {
843 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
844 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
845
846 if (area == TEXT_AREA)
847 x += window_box_width (w, LEFT_MARGIN_AREA);
848 else if (area == RIGHT_MARGIN_AREA)
849 x += (window_box_width (w, LEFT_MARGIN_AREA)
850 + window_box_width (w, TEXT_AREA));
851 }
852
853 return x;
854 }
855
856
857 /* Return the frame-relative coordinate of the right edge of display
858 area AREA of window W. AREA < 0 means return the left edge of the
859 whole window, to the left of any bitmap area at the right side of
860 W. */
861
862 INLINE int
863 window_box_right (w, area)
864 struct window *w;
865 int area;
866 {
867 return window_box_left (w, area) + window_box_width (w, area);
868 }
869
870
871 /* Get the bounding box of the display area AREA of window W, without
872 mode lines, in frame-relative coordinates. AREA < 0 means the
873 whole window, not including bitmap areas to the left and right of
874 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
875 coordinates of the upper-left corner of the box. Return in
876 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
877
878 INLINE void
879 window_box (w, area, box_x, box_y, box_width, box_height)
880 struct window *w;
881 int area;
882 int *box_x, *box_y, *box_width, *box_height;
883 {
884 struct frame *f = XFRAME (w->frame);
885
886 *box_width = window_box_width (w, area);
887 *box_height = window_box_height (w);
888 *box_x = window_box_left (w, area);
889 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
890 + XFASTINT (w->top) * CANON_Y_UNIT (f));
891 if (WINDOW_WANTS_HEADER_LINE_P (w))
892 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
893 }
894
895
896 /* Get the bounding box of the display area AREA of window W, without
897 mode lines. AREA < 0 means the whole window, not including bitmap
898 areas to the left and right of the window. Return in *TOP_LEFT_X
899 and TOP_LEFT_Y the frame-relative pixel coordinates of the
900 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
901 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
902 box. */
903
904 INLINE void
905 window_box_edges (w, area, top_left_x, top_left_y,
906 bottom_right_x, bottom_right_y)
907 struct window *w;
908 int area;
909 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
910 {
911 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
912 bottom_right_y);
913 *bottom_right_x += *top_left_x;
914 *bottom_right_y += *top_left_y;
915 }
916
917
918 \f
919 /***********************************************************************
920 Utilities
921 ***********************************************************************/
922
923 /* Return the next character from STR which is MAXLEN bytes long.
924 Return in *LEN the length of the character. This is like
925 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
926 we find one, we return a `?', but with the length of the invalid
927 character. */
928
929 static INLINE int
930 string_char_and_length (str, maxlen, len)
931 unsigned char *str;
932 int maxlen, *len;
933 {
934 int c;
935
936 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
937 if (!CHAR_VALID_P (c, 1))
938 /* We may not change the length here because other places in Emacs
939 don't use this function, i.e. they silently accept invalid
940 characters. */
941 c = '?';
942
943 return c;
944 }
945
946
947
948 /* Given a position POS containing a valid character and byte position
949 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
950
951 static struct text_pos
952 string_pos_nchars_ahead (pos, string, nchars)
953 struct text_pos pos;
954 Lisp_Object string;
955 int nchars;
956 {
957 xassert (STRINGP (string) && nchars >= 0);
958
959 if (STRING_MULTIBYTE (string))
960 {
961 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
962 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
963 int len;
964
965 while (nchars--)
966 {
967 string_char_and_length (p, rest, &len);
968 p += len, rest -= len;
969 xassert (rest >= 0);
970 CHARPOS (pos) += 1;
971 BYTEPOS (pos) += len;
972 }
973 }
974 else
975 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
976
977 return pos;
978 }
979
980
981 /* Value is the text position, i.e. character and byte position,
982 for character position CHARPOS in STRING. */
983
984 static INLINE struct text_pos
985 string_pos (charpos, string)
986 int charpos;
987 Lisp_Object string;
988 {
989 struct text_pos pos;
990 xassert (STRINGP (string));
991 xassert (charpos >= 0);
992 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
993 return pos;
994 }
995
996
997 /* Value is a text position, i.e. character and byte position, for
998 character position CHARPOS in C string S. MULTIBYTE_P non-zero
999 means recognize multibyte characters. */
1000
1001 static struct text_pos
1002 c_string_pos (charpos, s, multibyte_p)
1003 int charpos;
1004 unsigned char *s;
1005 int multibyte_p;
1006 {
1007 struct text_pos pos;
1008
1009 xassert (s != NULL);
1010 xassert (charpos >= 0);
1011
1012 if (multibyte_p)
1013 {
1014 int rest = strlen (s), len;
1015
1016 SET_TEXT_POS (pos, 0, 0);
1017 while (charpos--)
1018 {
1019 string_char_and_length (s, rest, &len);
1020 s += len, rest -= len;
1021 xassert (rest >= 0);
1022 CHARPOS (pos) += 1;
1023 BYTEPOS (pos) += len;
1024 }
1025 }
1026 else
1027 SET_TEXT_POS (pos, charpos, charpos);
1028
1029 return pos;
1030 }
1031
1032
1033 /* Value is the number of characters in C string S. MULTIBYTE_P
1034 non-zero means recognize multibyte characters. */
1035
1036 static int
1037 number_of_chars (s, multibyte_p)
1038 unsigned char *s;
1039 int multibyte_p;
1040 {
1041 int nchars;
1042
1043 if (multibyte_p)
1044 {
1045 int rest = strlen (s), len;
1046 unsigned char *p = (unsigned char *) s;
1047
1048 for (nchars = 0; rest > 0; ++nchars)
1049 {
1050 string_char_and_length (p, rest, &len);
1051 rest -= len, p += len;
1052 }
1053 }
1054 else
1055 nchars = strlen (s);
1056
1057 return nchars;
1058 }
1059
1060
1061 /* Compute byte position NEWPOS->bytepos corresponding to
1062 NEWPOS->charpos. POS is a known position in string STRING.
1063 NEWPOS->charpos must be >= POS.charpos. */
1064
1065 static void
1066 compute_string_pos (newpos, pos, string)
1067 struct text_pos *newpos, pos;
1068 Lisp_Object string;
1069 {
1070 xassert (STRINGP (string));
1071 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1072
1073 if (STRING_MULTIBYTE (string))
1074 *newpos = string_pos_nchars_ahead (pos, string,
1075 CHARPOS (*newpos) - CHARPOS (pos));
1076 else
1077 BYTEPOS (*newpos) = CHARPOS (*newpos);
1078 }
1079
1080
1081 \f
1082 /***********************************************************************
1083 Lisp form evaluation
1084 ***********************************************************************/
1085
1086 /* Error handler for eval_form and call_function. */
1087
1088 static Lisp_Object
1089 eval_handler (arg)
1090 Lisp_Object arg;
1091 {
1092 return Qnil;
1093 }
1094
1095
1096 /* Evaluate SEXPR and return the result, or nil if something went
1097 wrong. */
1098
1099 Lisp_Object
1100 eval_form (sexpr)
1101 Lisp_Object sexpr;
1102 {
1103 int count = specpdl_ptr - specpdl;
1104 struct gcpro gcpro1;
1105 Lisp_Object val;
1106
1107 GCPRO1 (sexpr);
1108 specbind (Qinhibit_redisplay, Qt);
1109 val = internal_condition_case_1 (Feval, sexpr, Qerror, eval_handler);
1110 UNGCPRO;
1111 return unbind_to (count, val);
1112 }
1113
1114
1115 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1116 Return the result, or nil if something went wrong. */
1117
1118 Lisp_Object
1119 call_function (nargs, args)
1120 int nargs;
1121 Lisp_Object *args;
1122 {
1123 int count = specpdl_ptr - specpdl;
1124 Lisp_Object val;
1125 struct gcpro gcpro1;
1126
1127 GCPRO1 (args[0]);
1128 gcpro1.nvars = nargs;
1129 specbind (Qinhibit_redisplay, Qt);
1130 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1131 eval_handler);
1132 UNGCPRO;
1133 return unbind_to (count, val);
1134 }
1135
1136
1137 \f
1138 /***********************************************************************
1139 Debugging
1140 ***********************************************************************/
1141
1142 #if 0
1143
1144 /* Define CHECK_IT to perform sanity checks on iterators.
1145 This is for debugging. It is too slow to do unconditionally. */
1146
1147 static void
1148 check_it (it)
1149 struct it *it;
1150 {
1151 if (it->method == next_element_from_string)
1152 {
1153 xassert (STRINGP (it->string));
1154 xassert (IT_STRING_CHARPOS (*it) >= 0);
1155 }
1156 else if (it->method == next_element_from_buffer)
1157 {
1158 /* Check that character and byte positions agree. */
1159 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1160 }
1161
1162 if (it->dpvec)
1163 xassert (it->current.dpvec_index >= 0);
1164 else
1165 xassert (it->current.dpvec_index < 0);
1166 }
1167
1168 #define CHECK_IT(IT) check_it ((IT))
1169
1170 #else /* not 0 */
1171
1172 #define CHECK_IT(IT) (void) 0
1173
1174 #endif /* not 0 */
1175
1176
1177 #if GLYPH_DEBUG
1178
1179 /* Check that the window end of window W is what we expect it
1180 to be---the last row in the current matrix displaying text. */
1181
1182 static void
1183 check_window_end (w)
1184 struct window *w;
1185 {
1186 if (!MINI_WINDOW_P (w)
1187 && !NILP (w->window_end_valid))
1188 {
1189 struct glyph_row *row;
1190 xassert ((row = MATRIX_ROW (w->current_matrix,
1191 XFASTINT (w->window_end_vpos)),
1192 !row->enabled_p
1193 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1194 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1195 }
1196 }
1197
1198 #define CHECK_WINDOW_END(W) check_window_end ((W))
1199
1200 #else /* not GLYPH_DEBUG */
1201
1202 #define CHECK_WINDOW_END(W) (void) 0
1203
1204 #endif /* not GLYPH_DEBUG */
1205
1206
1207 \f
1208 /***********************************************************************
1209 Iterator initialization
1210 ***********************************************************************/
1211
1212 /* Initialize IT for displaying current_buffer in window W, starting
1213 at character position CHARPOS. CHARPOS < 0 means that no buffer
1214 position is specified which is useful when the iterator is assigned
1215 a position later. BYTEPOS is the byte position corresponding to
1216 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1217
1218 If ROW is not null, calls to produce_glyphs with IT as parameter
1219 will produce glyphs in that row.
1220
1221 BASE_FACE_ID is the id of a base face to use. It must be one of
1222 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1223 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1224 displaying the tool-bar.
1225
1226 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1227 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1228 corresponding mode line glyph row of the desired matrix of W. */
1229
1230 void
1231 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1232 struct it *it;
1233 struct window *w;
1234 int charpos, bytepos;
1235 struct glyph_row *row;
1236 enum face_id base_face_id;
1237 {
1238 int highlight_region_p;
1239
1240 /* Some precondition checks. */
1241 xassert (w != NULL && it != NULL);
1242 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1243
1244 /* If face attributes have been changed since the last redisplay,
1245 free realized faces now because they depend on face definitions
1246 that might have changed. */
1247 if (face_change_count)
1248 {
1249 face_change_count = 0;
1250 free_all_realized_faces (Qnil);
1251 }
1252
1253 /* Use one of the mode line rows of W's desired matrix if
1254 appropriate. */
1255 if (row == NULL)
1256 {
1257 if (base_face_id == MODE_LINE_FACE_ID)
1258 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1259 else if (base_face_id == HEADER_LINE_FACE_ID)
1260 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1261 }
1262
1263 /* Clear IT. */
1264 bzero (it, sizeof *it);
1265 it->current.overlay_string_index = -1;
1266 it->current.dpvec_index = -1;
1267 it->base_face_id = base_face_id;
1268
1269 /* The window in which we iterate over current_buffer: */
1270 XSETWINDOW (it->window, w);
1271 it->w = w;
1272 it->f = XFRAME (w->frame);
1273
1274 /* Extra space between lines (on window systems only). */
1275 if (base_face_id == DEFAULT_FACE_ID
1276 && FRAME_WINDOW_P (it->f))
1277 {
1278 if (NATNUMP (current_buffer->extra_line_spacing))
1279 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1280 else if (it->f->extra_line_spacing > 0)
1281 it->extra_line_spacing = it->f->extra_line_spacing;
1282 }
1283
1284 /* If realized faces have been removed, e.g. because of face
1285 attribute changes of named faces, recompute them. */
1286 if (FRAME_FACE_CACHE (it->f)->used == 0)
1287 recompute_basic_faces (it->f);
1288
1289 /* Current value of the `space-width', and 'height' properties. */
1290 it->space_width = Qnil;
1291 it->font_height = Qnil;
1292
1293 /* Are control characters displayed as `^C'? */
1294 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1295
1296 /* -1 means everything between a CR and the following line end
1297 is invisible. >0 means lines indented more than this value are
1298 invisible. */
1299 it->selective = (INTEGERP (current_buffer->selective_display)
1300 ? XFASTINT (current_buffer->selective_display)
1301 : (!NILP (current_buffer->selective_display)
1302 ? -1 : 0));
1303 it->selective_display_ellipsis_p
1304 = !NILP (current_buffer->selective_display_ellipses);
1305
1306 /* Display table to use. */
1307 it->dp = window_display_table (w);
1308
1309 /* Are multibyte characters enabled in current_buffer? */
1310 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1311
1312 /* Non-zero if we should highlight the region. */
1313 highlight_region_p
1314 = (!NILP (Vtransient_mark_mode)
1315 && !NILP (current_buffer->mark_active)
1316 && XMARKER (current_buffer->mark)->buffer != 0);
1317
1318 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1319 start and end of a visible region in window IT->w. Set both to
1320 -1 to indicate no region. */
1321 if (highlight_region_p
1322 /* Maybe highlight only in selected window. */
1323 && (/* Either show region everywhere. */
1324 highlight_nonselected_windows
1325 /* Or show region in the selected window. */
1326 || w == XWINDOW (selected_window)
1327 /* Or show the region if we are in the mini-buffer and W is
1328 the window the mini-buffer refers to. */
1329 || (MINI_WINDOW_P (XWINDOW (selected_window))
1330 && w == XWINDOW (Vminibuf_scroll_window))))
1331 {
1332 int charpos = marker_position (current_buffer->mark);
1333 it->region_beg_charpos = min (PT, charpos);
1334 it->region_end_charpos = max (PT, charpos);
1335 }
1336 else
1337 it->region_beg_charpos = it->region_end_charpos = -1;
1338
1339 /* Get the position at which the redisplay_end_trigger hook should
1340 be run, if it is to be run at all. */
1341 if (MARKERP (w->redisplay_end_trigger)
1342 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1343 it->redisplay_end_trigger_charpos
1344 = marker_position (w->redisplay_end_trigger);
1345 else if (INTEGERP (w->redisplay_end_trigger))
1346 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1347
1348 /* Correct bogus values of tab_width. */
1349 it->tab_width = XINT (current_buffer->tab_width);
1350 if (it->tab_width <= 0 || it->tab_width > 1000)
1351 it->tab_width = 8;
1352
1353 /* Are lines in the display truncated? */
1354 it->truncate_lines_p
1355 = (base_face_id != DEFAULT_FACE_ID
1356 || XINT (it->w->hscroll)
1357 || (truncate_partial_width_windows
1358 && !WINDOW_FULL_WIDTH_P (it->w))
1359 || !NILP (current_buffer->truncate_lines));
1360
1361 /* Get dimensions of truncation and continuation glyphs. These are
1362 displayed as bitmaps under X, so we don't need them for such
1363 frames. */
1364 if (!FRAME_WINDOW_P (it->f))
1365 {
1366 if (it->truncate_lines_p)
1367 {
1368 /* We will need the truncation glyph. */
1369 xassert (it->glyph_row == NULL);
1370 produce_special_glyphs (it, IT_TRUNCATION);
1371 it->truncation_pixel_width = it->pixel_width;
1372 }
1373 else
1374 {
1375 /* We will need the continuation glyph. */
1376 xassert (it->glyph_row == NULL);
1377 produce_special_glyphs (it, IT_CONTINUATION);
1378 it->continuation_pixel_width = it->pixel_width;
1379 }
1380
1381 /* Reset these values to zero becaue the produce_special_glyphs
1382 above has changed them. */
1383 it->pixel_width = it->ascent = it->descent = 0;
1384 it->phys_ascent = it->phys_descent = 0;
1385 }
1386
1387 /* Set this after getting the dimensions of truncation and
1388 continuation glyphs, so that we don't produce glyphs when calling
1389 produce_special_glyphs, above. */
1390 it->glyph_row = row;
1391 it->area = TEXT_AREA;
1392
1393 /* Get the dimensions of the display area. The display area
1394 consists of the visible window area plus a horizontally scrolled
1395 part to the left of the window. All x-values are relative to the
1396 start of this total display area. */
1397 if (base_face_id != DEFAULT_FACE_ID)
1398 {
1399 /* Mode lines, menu bar in terminal frames. */
1400 it->first_visible_x = 0;
1401 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1402 }
1403 else
1404 {
1405 it->first_visible_x
1406 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1407 it->last_visible_x = (it->first_visible_x
1408 + window_box_width (w, TEXT_AREA));
1409
1410 /* If we truncate lines, leave room for the truncator glyph(s) at
1411 the right margin. Otherwise, leave room for the continuation
1412 glyph(s). Truncation and continuation glyphs are not inserted
1413 for window-based redisplay. */
1414 if (!FRAME_WINDOW_P (it->f))
1415 {
1416 if (it->truncate_lines_p)
1417 it->last_visible_x -= it->truncation_pixel_width;
1418 else
1419 it->last_visible_x -= it->continuation_pixel_width;
1420 }
1421
1422 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1423 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1424 }
1425
1426 /* Leave room for a border glyph. */
1427 if (!FRAME_WINDOW_P (it->f)
1428 && !WINDOW_RIGHTMOST_P (it->w))
1429 it->last_visible_x -= 1;
1430
1431 it->last_visible_y = window_text_bottom_y (w);
1432
1433 /* For mode lines and alike, arrange for the first glyph having a
1434 left box line if the face specifies a box. */
1435 if (base_face_id != DEFAULT_FACE_ID)
1436 {
1437 struct face *face;
1438
1439 it->face_id = base_face_id;
1440
1441 /* If we have a boxed mode line, make the first character appear
1442 with a left box line. */
1443 face = FACE_FROM_ID (it->f, base_face_id);
1444 if (face->box != FACE_NO_BOX)
1445 it->start_of_box_run_p = 1;
1446 }
1447
1448 /* If a buffer position was specified, set the iterator there,
1449 getting overlays and face properties from that position. */
1450 if (charpos > 0)
1451 {
1452 it->end_charpos = ZV;
1453 it->face_id = -1;
1454 IT_CHARPOS (*it) = charpos;
1455
1456 /* Compute byte position if not specified. */
1457 if (bytepos <= 0)
1458 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1459 else
1460 IT_BYTEPOS (*it) = bytepos;
1461
1462 /* Compute faces etc. */
1463 reseat (it, it->current.pos, 1);
1464 }
1465
1466 CHECK_IT (it);
1467 }
1468
1469
1470 /* Initialize IT for the display of window W with window start POS. */
1471
1472 void
1473 start_display (it, w, pos)
1474 struct it *it;
1475 struct window *w;
1476 struct text_pos pos;
1477 {
1478 int start_at_line_beg_p;
1479 struct glyph_row *row;
1480 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1481 int first_y;
1482
1483 row = w->desired_matrix->rows + first_vpos;
1484 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1485 first_y = it->current_y;
1486
1487 /* If window start is not at a line start, move back to the line
1488 start. This makes sure that we take continuation lines into
1489 account. */
1490 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1491 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1492 if (!start_at_line_beg_p)
1493 reseat_at_previous_visible_line_start (it);
1494
1495 /* If window start is not at a line start, skip forward to POS to
1496 get the correct continuation_lines_width and current_x. */
1497 if (!start_at_line_beg_p)
1498 {
1499 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1500
1501 /* If lines are continued, this line may end in the middle of a
1502 multi-glyph character (e.g. a control character displayed as
1503 \003, or in the middle of an overlay string). In this case
1504 move_it_to above will not have taken us to the start of
1505 the continuation line but to the end of the continued line. */
1506 if (!it->truncate_lines_p)
1507 {
1508 if (it->current_x > 0)
1509 {
1510 if (it->current.dpvec_index >= 0
1511 || it->current.overlay_string_index >= 0)
1512 {
1513 set_iterator_to_next (it);
1514 move_it_in_display_line_to (it, -1, -1, 0);
1515 }
1516
1517 it->continuation_lines_width += it->current_x;
1518 }
1519
1520 /* We're starting a new display line, not affected by the
1521 height of the continued line, so clear the appropriate
1522 fields in the iterator structure. */
1523 it->max_ascent = it->max_descent = 0;
1524 it->max_phys_ascent = it->max_phys_descent = 0;
1525 }
1526
1527 it->current_y = first_y;
1528 it->vpos = 0;
1529 it->current_x = it->hpos = 0;
1530 }
1531
1532 #if 0 /* Don't assert the following because start_display is sometimes
1533 called intentionally with a window start that is not at a
1534 line start. Please leave this code in as a comment. */
1535
1536 /* Window start should be on a line start, now. */
1537 xassert (it->continuation_lines_width
1538 || IT_CHARPOS (it) == BEGV
1539 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1540 #endif /* 0 */
1541 }
1542
1543
1544 /* Initialize IT for stepping through current_buffer in window W,
1545 starting at position POS that includes overlay string and display
1546 vector/ control character translation position information. */
1547
1548 static void
1549 init_from_display_pos (it, w, pos)
1550 struct it *it;
1551 struct window *w;
1552 struct display_pos *pos;
1553 {
1554 /* Keep in mind: the call to reseat in init_iterator skips invisible
1555 text, so we might end up at a position different from POS. This
1556 is only a problem when POS is a row start after a newline and an
1557 overlay starts there with an after-string, and the overlay has an
1558 invisible property. Since we don't skip invisible text in
1559 display_line and elsewhere immediately after consuming the
1560 newline before the row start, such a POS will not be in a string,
1561 but the call to init_iterator below will move us to the
1562 after-string. */
1563 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1564 NULL, DEFAULT_FACE_ID);
1565
1566 /* If position is within an overlay string, set up IT to
1567 the right overlay string. */
1568 if (pos->overlay_string_index >= 0)
1569 {
1570 int relative_index;
1571
1572 /* We already have the first chunk of overlay strings in
1573 IT->overlay_strings. Load more until the one for
1574 pos->overlay_string_index is in IT->overlay_strings. */
1575 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1576 {
1577 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1578 it->current.overlay_string_index = 0;
1579 while (n--)
1580 {
1581 load_overlay_strings (it);
1582 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1583 }
1584 }
1585
1586 it->current.overlay_string_index = pos->overlay_string_index;
1587 relative_index = (it->current.overlay_string_index
1588 % OVERLAY_STRING_CHUNK_SIZE);
1589 it->string = it->overlay_strings[relative_index];
1590 it->current.string_pos = pos->string_pos;
1591 it->method = next_element_from_string;
1592 }
1593 else if (CHARPOS (pos->string_pos) >= 0)
1594 {
1595 /* Recorded position is not in an overlay string, but in another
1596 string. This can only be a string from a `display' property.
1597 IT should already be filled with that string. */
1598 it->current.string_pos = pos->string_pos;
1599 xassert (STRINGP (it->string));
1600 }
1601
1602 /* Restore position in display vector translations or control
1603 character translations. */
1604 if (pos->dpvec_index >= 0)
1605 {
1606 /* This fills IT->dpvec. */
1607 get_next_display_element (it);
1608 xassert (it->dpvec && it->current.dpvec_index == 0);
1609 it->current.dpvec_index = pos->dpvec_index;
1610 }
1611
1612 CHECK_IT (it);
1613 }
1614
1615
1616 /* Initialize IT for stepping through current_buffer in window W
1617 starting at ROW->start. */
1618
1619 static void
1620 init_to_row_start (it, w, row)
1621 struct it *it;
1622 struct window *w;
1623 struct glyph_row *row;
1624 {
1625 init_from_display_pos (it, w, &row->start);
1626 it->continuation_lines_width = row->continuation_lines_width;
1627 CHECK_IT (it);
1628 }
1629
1630
1631 /* Initialize IT for stepping through current_buffer in window W
1632 starting in the line following ROW, i.e. starting at ROW->end. */
1633
1634 static void
1635 init_to_row_end (it, w, row)
1636 struct it *it;
1637 struct window *w;
1638 struct glyph_row *row;
1639 {
1640 init_from_display_pos (it, w, &row->end);
1641
1642 if (row->continued_p)
1643 it->continuation_lines_width = (row->continuation_lines_width
1644 + row->pixel_width);
1645 CHECK_IT (it);
1646 }
1647
1648
1649
1650 \f
1651 /***********************************************************************
1652 Text properties
1653 ***********************************************************************/
1654
1655 /* Called when IT reaches IT->stop_charpos. Handle text property and
1656 overlay changes. Set IT->stop_charpos to the next position where
1657 to stop. */
1658
1659 static void
1660 handle_stop (it)
1661 struct it *it;
1662 {
1663 enum prop_handled handled;
1664 int handle_overlay_change_p = 1;
1665 struct props *p;
1666
1667 it->dpvec = NULL;
1668 it->current.dpvec_index = -1;
1669 it->add_overlay_start = 0;
1670
1671 do
1672 {
1673 handled = HANDLED_NORMALLY;
1674
1675 /* Call text property handlers. */
1676 for (p = it_props; p->handler; ++p)
1677 {
1678 handled = p->handler (it);
1679
1680 if (handled == HANDLED_RECOMPUTE_PROPS)
1681 break;
1682 else if (handled == HANDLED_RETURN)
1683 return;
1684 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1685 handle_overlay_change_p = 0;
1686 }
1687
1688 if (handled != HANDLED_RECOMPUTE_PROPS)
1689 {
1690 /* Don't check for overlay strings below when set to deliver
1691 characters from a display vector. */
1692 if (it->method == next_element_from_display_vector)
1693 handle_overlay_change_p = 0;
1694
1695 /* Handle overlay changes. */
1696 if (handle_overlay_change_p)
1697 handled = handle_overlay_change (it);
1698
1699 /* Determine where to stop next. */
1700 if (handled == HANDLED_NORMALLY)
1701 compute_stop_pos (it);
1702 }
1703 }
1704 while (handled == HANDLED_RECOMPUTE_PROPS);
1705 }
1706
1707
1708 /* Compute IT->stop_charpos from text property and overlay change
1709 information for IT's current position. */
1710
1711 static void
1712 compute_stop_pos (it)
1713 struct it *it;
1714 {
1715 register INTERVAL iv, next_iv;
1716 Lisp_Object object, limit, position;
1717
1718 /* If nowhere else, stop at the end. */
1719 it->stop_charpos = it->end_charpos;
1720
1721 if (STRINGP (it->string))
1722 {
1723 /* Strings are usually short, so don't limit the search for
1724 properties. */
1725 object = it->string;
1726 limit = Qnil;
1727 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1728 }
1729 else
1730 {
1731 int charpos;
1732
1733 /* If next overlay change is in front of the current stop pos
1734 (which is IT->end_charpos), stop there. Note: value of
1735 next_overlay_change is point-max if no overlay change
1736 follows. */
1737 charpos = next_overlay_change (IT_CHARPOS (*it));
1738 if (charpos < it->stop_charpos)
1739 it->stop_charpos = charpos;
1740
1741 /* If showing the region, we have to stop at the region
1742 start or end because the face might change there. */
1743 if (it->region_beg_charpos > 0)
1744 {
1745 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1746 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1747 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1748 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1749 }
1750
1751 /* Set up variables for computing the stop position from text
1752 property changes. */
1753 XSETBUFFER (object, current_buffer);
1754 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1755 XSETFASTINT (position, IT_CHARPOS (*it));
1756
1757 }
1758
1759 /* Get the interval containing IT's position. Value is a null
1760 interval if there isn't such an interval. */
1761 iv = validate_interval_range (object, &position, &position, 0);
1762 if (!NULL_INTERVAL_P (iv))
1763 {
1764 Lisp_Object values_here[LAST_PROP_IDX];
1765 struct props *p;
1766
1767 /* Get properties here. */
1768 for (p = it_props; p->handler; ++p)
1769 values_here[p->idx] = textget (iv->plist, *p->name);
1770
1771 /* Look for an interval following iv that has different
1772 properties. */
1773 for (next_iv = next_interval (iv);
1774 (!NULL_INTERVAL_P (next_iv)
1775 && (NILP (limit)
1776 || XFASTINT (limit) > next_iv->position));
1777 next_iv = next_interval (next_iv))
1778 {
1779 for (p = it_props; p->handler; ++p)
1780 {
1781 Lisp_Object new_value;
1782
1783 new_value = textget (next_iv->plist, *p->name);
1784 if (!EQ (values_here[p->idx], new_value))
1785 break;
1786 }
1787
1788 if (p->handler)
1789 break;
1790 }
1791
1792 if (!NULL_INTERVAL_P (next_iv))
1793 {
1794 if (INTEGERP (limit)
1795 && next_iv->position >= XFASTINT (limit))
1796 /* No text property change up to limit. */
1797 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1798 else
1799 /* Text properties change in next_iv. */
1800 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1801 }
1802 }
1803
1804 xassert (STRINGP (it->string)
1805 || (it->stop_charpos >= BEGV
1806 && it->stop_charpos >= IT_CHARPOS (*it)));
1807 }
1808
1809
1810 /* Return the position of the next overlay change after POS in
1811 current_buffer. Value is point-max if no overlay change
1812 follows. This is like `next-overlay-change' but doesn't use
1813 xmalloc. */
1814
1815 static int
1816 next_overlay_change (pos)
1817 int pos;
1818 {
1819 int noverlays;
1820 int endpos;
1821 Lisp_Object *overlays;
1822 int len;
1823 int i;
1824
1825 /* Get all overlays at the given position. */
1826 len = 10;
1827 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1828 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1829 if (noverlays > len)
1830 {
1831 len = noverlays;
1832 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1833 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1834 }
1835
1836 /* If any of these overlays ends before endpos,
1837 use its ending point instead. */
1838 for (i = 0; i < noverlays; ++i)
1839 {
1840 Lisp_Object oend;
1841 int oendpos;
1842
1843 oend = OVERLAY_END (overlays[i]);
1844 oendpos = OVERLAY_POSITION (oend);
1845 endpos = min (endpos, oendpos);
1846 }
1847
1848 return endpos;
1849 }
1850
1851
1852 \f
1853 /***********************************************************************
1854 Fontification
1855 ***********************************************************************/
1856
1857 /* Handle changes in the `fontified' property of the current buffer by
1858 calling hook functions from Qfontification_functions to fontify
1859 regions of text. */
1860
1861 static enum prop_handled
1862 handle_fontified_prop (it)
1863 struct it *it;
1864 {
1865 Lisp_Object prop, pos;
1866 enum prop_handled handled = HANDLED_NORMALLY;
1867
1868 /* Get the value of the `fontified' property at IT's current buffer
1869 position. (The `fontified' property doesn't have a special
1870 meaning in strings.) If the value is nil, call functions from
1871 Qfontification_functions. */
1872 if (!STRINGP (it->string)
1873 && it->s == NULL
1874 && !NILP (Vfontification_functions)
1875 && (pos = make_number (IT_CHARPOS (*it)),
1876 prop = Fget_char_property (pos, Qfontified, Qnil),
1877 NILP (prop)))
1878 {
1879 Lisp_Object args[2];
1880
1881 /* Run the hook functions. */
1882 args[0] = Qfontification_functions;
1883 args[1] = pos;
1884 Frun_hook_with_args (2, args);
1885
1886 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1887 something. This avoids an endless loop if they failed to
1888 fontify the text for which reason ever. */
1889 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1890 handled = HANDLED_RECOMPUTE_PROPS;
1891 }
1892
1893 return handled;
1894 }
1895
1896
1897 \f
1898 /***********************************************************************
1899 Faces
1900 ***********************************************************************/
1901
1902 /* Set up iterator IT from face properties at its current position.
1903 Called from handle_stop. */
1904
1905 static enum prop_handled
1906 handle_face_prop (it)
1907 struct it *it;
1908 {
1909 int new_face_id, next_stop;
1910
1911 if (!STRINGP (it->string))
1912 {
1913 new_face_id
1914 = face_at_buffer_position (it->w,
1915 IT_CHARPOS (*it),
1916 it->region_beg_charpos,
1917 it->region_end_charpos,
1918 &next_stop,
1919 (IT_CHARPOS (*it)
1920 + TEXT_PROP_DISTANCE_LIMIT),
1921 0);
1922
1923 /* Is this a start of a run of characters with box face?
1924 Caveat: this can be called for a freshly initialized
1925 iterator; face_id is -1 is this case. We know that the new
1926 face will not change until limit, i.e. if the new face has a
1927 box, all characters up to limit will have one. But, as
1928 usual, we don't know whether limit is really the end. */
1929 if (new_face_id != it->face_id)
1930 {
1931 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1932
1933 /* If new face has a box but old face has not, this is
1934 the start of a run of characters with box, i.e. it has
1935 a shadow on the left side. The value of face_id of the
1936 iterator will be -1 if this is the initial call that gets
1937 the face. In this case, we have to look in front of IT's
1938 position and see whether there is a face != new_face_id. */
1939 it->start_of_box_run_p
1940 = (new_face->box != FACE_NO_BOX
1941 && (it->face_id >= 0
1942 || IT_CHARPOS (*it) == BEG
1943 || new_face_id != face_before_it_pos (it)));
1944 it->face_box_p = new_face->box != FACE_NO_BOX;
1945 }
1946 }
1947 else
1948 {
1949 new_face_id
1950 = face_at_string_position (it->w,
1951 it->string,
1952 IT_STRING_CHARPOS (*it),
1953 (it->current.overlay_string_index >= 0
1954 ? IT_CHARPOS (*it)
1955 : 0),
1956 it->region_beg_charpos,
1957 it->region_end_charpos,
1958 &next_stop,
1959 it->base_face_id);
1960
1961 #if 0 /* This shouldn't be neccessary. Let's check it. */
1962 /* If IT is used to display a mode line we would really like to
1963 use the mode line face instead of the frame's default face. */
1964 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
1965 && new_face_id == DEFAULT_FACE_ID)
1966 new_face_id = MODE_LINE_FACE_ID;
1967 #endif
1968
1969 /* Is this a start of a run of characters with box? Caveat:
1970 this can be called for a freshly allocated iterator; face_id
1971 is -1 is this case. We know that the new face will not
1972 change until the next check pos, i.e. if the new face has a
1973 box, all characters up to that position will have a
1974 box. But, as usual, we don't know whether that position
1975 is really the end. */
1976 if (new_face_id != it->face_id)
1977 {
1978 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1979 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
1980
1981 /* If new face has a box but old face hasn't, this is the
1982 start of a run of characters with box, i.e. it has a
1983 shadow on the left side. */
1984 it->start_of_box_run_p
1985 = new_face->box && (old_face == NULL || !old_face->box);
1986 it->face_box_p = new_face->box != FACE_NO_BOX;
1987 }
1988 }
1989
1990 it->face_id = new_face_id;
1991 return HANDLED_NORMALLY;
1992 }
1993
1994
1995 /* Compute the face one character before or after the current position
1996 of IT. BEFORE_P non-zero means get the face in front of IT's
1997 position. Value is the id of the face. */
1998
1999 static int
2000 face_before_or_after_it_pos (it, before_p)
2001 struct it *it;
2002 int before_p;
2003 {
2004 int face_id, limit;
2005 int next_check_charpos;
2006 struct text_pos pos;
2007
2008 xassert (it->s == NULL);
2009
2010 if (STRINGP (it->string))
2011 {
2012 /* No face change past the end of the string (for the case
2013 we are padding with spaces). No face change before the
2014 string start. */
2015 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2016 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2017 return it->face_id;
2018
2019 /* Set pos to the position before or after IT's current position. */
2020 if (before_p)
2021 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2022 else
2023 /* For composition, we must check the character after the
2024 composition. */
2025 pos = (it->what == IT_COMPOSITION
2026 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2027 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2028
2029 /* Get the face for ASCII, or unibyte. */
2030 face_id
2031 = face_at_string_position (it->w,
2032 it->string,
2033 CHARPOS (pos),
2034 (it->current.overlay_string_index >= 0
2035 ? IT_CHARPOS (*it)
2036 : 0),
2037 it->region_beg_charpos,
2038 it->region_end_charpos,
2039 &next_check_charpos,
2040 it->base_face_id);
2041
2042 /* Correct the face for charsets different from ASCII. Do it
2043 for the multibyte case only. The face returned above is
2044 suitable for unibyte text if IT->string is unibyte. */
2045 if (STRING_MULTIBYTE (it->string))
2046 {
2047 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2048 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2049 int c, len;
2050 struct face *face = FACE_FROM_ID (it->f, face_id);
2051
2052 c = string_char_and_length (p, rest, &len);
2053 face_id = FACE_FOR_CHAR (it->f, face, c);
2054 }
2055 }
2056 else
2057 {
2058 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2059 || (IT_CHARPOS (*it) <= BEGV && before_p))
2060 return it->face_id;
2061
2062 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2063 pos = it->current.pos;
2064
2065 if (before_p)
2066 DEC_TEXT_POS (pos, it->multibyte_p);
2067 else
2068 {
2069 if (it->what == IT_COMPOSITION)
2070 /* For composition, we must check the position after the
2071 composition. */
2072 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2073 else
2074 INC_TEXT_POS (pos, it->multibyte_p);
2075 }
2076 /* Determine face for CHARSET_ASCII, or unibyte. */
2077 face_id = face_at_buffer_position (it->w,
2078 CHARPOS (pos),
2079 it->region_beg_charpos,
2080 it->region_end_charpos,
2081 &next_check_charpos,
2082 limit, 0);
2083
2084 /* Correct the face for charsets different from ASCII. Do it
2085 for the multibyte case only. The face returned above is
2086 suitable for unibyte text if current_buffer is unibyte. */
2087 if (it->multibyte_p)
2088 {
2089 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2090 struct face *face = FACE_FROM_ID (it->f, face_id);
2091 face_id = FACE_FOR_CHAR (it->f, face, c);
2092 }
2093 }
2094
2095 return face_id;
2096 }
2097
2098
2099 \f
2100 /***********************************************************************
2101 Invisible text
2102 ***********************************************************************/
2103
2104 /* Set up iterator IT from invisible properties at its current
2105 position. Called from handle_stop. */
2106
2107 static enum prop_handled
2108 handle_invisible_prop (it)
2109 struct it *it;
2110 {
2111 enum prop_handled handled = HANDLED_NORMALLY;
2112
2113 if (STRINGP (it->string))
2114 {
2115 extern Lisp_Object Qinvisible;
2116 Lisp_Object prop, end_charpos, limit, charpos;
2117
2118 /* Get the value of the invisible text property at the
2119 current position. Value will be nil if there is no such
2120 property. */
2121 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2122 prop = Fget_text_property (charpos, Qinvisible, it->string);
2123
2124 if (!NILP (prop)
2125 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2126 {
2127 handled = HANDLED_RECOMPUTE_PROPS;
2128
2129 /* Get the position at which the next change of the
2130 invisible text property can be found in IT->string.
2131 Value will be nil if the property value is the same for
2132 all the rest of IT->string. */
2133 XSETINT (limit, XSTRING (it->string)->size);
2134 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2135 it->string, limit);
2136
2137 /* Text at current position is invisible. The next
2138 change in the property is at position end_charpos.
2139 Move IT's current position to that position. */
2140 if (INTEGERP (end_charpos)
2141 && XFASTINT (end_charpos) < XFASTINT (limit))
2142 {
2143 struct text_pos old;
2144 old = it->current.string_pos;
2145 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2146 compute_string_pos (&it->current.string_pos, old, it->string);
2147 }
2148 else
2149 {
2150 /* The rest of the string is invisible. If this is an
2151 overlay string, proceed with the next overlay string
2152 or whatever comes and return a character from there. */
2153 if (it->current.overlay_string_index >= 0)
2154 {
2155 next_overlay_string (it);
2156 /* Don't check for overlay strings when we just
2157 finished processing them. */
2158 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2159 }
2160 else
2161 {
2162 struct Lisp_String *s = XSTRING (it->string);
2163 IT_STRING_CHARPOS (*it) = s->size;
2164 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2165 }
2166 }
2167 }
2168 }
2169 else
2170 {
2171 int visible_p, newpos, next_stop;
2172 Lisp_Object pos, prop;
2173
2174 /* First of all, is there invisible text at this position? */
2175 XSETFASTINT (pos, IT_CHARPOS (*it));
2176 prop = Fget_char_property (pos, Qinvisible, it->window);
2177
2178 /* If we are on invisible text, skip over it. */
2179 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2180 && IT_CHARPOS (*it) < it->end_charpos)
2181 {
2182 /* Record whether we have to display an ellipsis for the
2183 invisible text. */
2184 int display_ellipsis_p
2185 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2186
2187 handled = HANDLED_RECOMPUTE_PROPS;
2188 it->add_overlay_start = IT_CHARPOS (*it);
2189
2190 /* Loop skipping over invisible text. The loop is left at
2191 ZV or with IT on the first char being visible again. */
2192 do
2193 {
2194 /* Try to skip some invisible text. Return value is the
2195 position reached which can be equal to IT's position
2196 if there is nothing invisible here. This skips both
2197 over invisible text properties and overlays with
2198 invisible property. */
2199 newpos = skip_invisible (IT_CHARPOS (*it),
2200 &next_stop, ZV, it->window);
2201
2202 /* If we skipped nothing at all we weren't at invisible
2203 text in the first place. If everything to the end of
2204 the buffer was skipped, end the loop. */
2205 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2206 visible_p = 1;
2207 else
2208 {
2209 /* We skipped some characters but not necessarily
2210 all there are. Check if we ended up on visible
2211 text. Fget_char_property returns the property of
2212 the char before the given position, i.e. if we
2213 get visible_p = 1, this means that the char at
2214 newpos is visible. */
2215 XSETFASTINT (pos, newpos);
2216 prop = Fget_char_property (pos, Qinvisible, it->window);
2217 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2218 }
2219
2220 /* If we ended up on invisible text, proceed to
2221 skip starting with next_stop. */
2222 if (!visible_p)
2223 IT_CHARPOS (*it) = next_stop;
2224 }
2225 while (!visible_p);
2226
2227 /* The position newpos is now either ZV or on visible text. */
2228 IT_CHARPOS (*it) = newpos;
2229 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2230
2231 /* Maybe return `...' next for the end of the invisible text. */
2232 if (display_ellipsis_p)
2233 {
2234 if (it->dp
2235 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2236 {
2237 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2238 it->dpvec = v->contents;
2239 it->dpend = v->contents + v->size;
2240 }
2241 else
2242 {
2243 /* Default `...'. */
2244 it->dpvec = default_invis_vector;
2245 it->dpend = default_invis_vector + 3;
2246 }
2247
2248 /* The ellipsis display does not replace the display of
2249 the character at the new position. Indicate this by
2250 setting IT->dpvec_char_len to zero. */
2251 it->dpvec_char_len = 0;
2252
2253 it->current.dpvec_index = 0;
2254 it->method = next_element_from_display_vector;
2255 }
2256 }
2257 }
2258
2259 return handled;
2260 }
2261
2262
2263 \f
2264 /***********************************************************************
2265 'display' property
2266 ***********************************************************************/
2267
2268 /* Set up iterator IT from `display' property at its current position.
2269 Called from handle_stop. */
2270
2271 static enum prop_handled
2272 handle_display_prop (it)
2273 struct it *it;
2274 {
2275 Lisp_Object prop, object;
2276 struct text_pos *position;
2277 int space_or_image_found_p;
2278
2279 if (STRINGP (it->string))
2280 {
2281 object = it->string;
2282 position = &it->current.string_pos;
2283 }
2284 else
2285 {
2286 object = Qnil;
2287 position = &it->current.pos;
2288 }
2289
2290 /* Reset those iterator values set from display property values. */
2291 it->font_height = Qnil;
2292 it->space_width = Qnil;
2293 it->voffset = 0;
2294
2295 /* We don't support recursive `display' properties, i.e. string
2296 values that have a string `display' property, that have a string
2297 `display' property etc. */
2298 if (!it->string_from_display_prop_p)
2299 it->area = TEXT_AREA;
2300
2301 prop = Fget_char_property (make_number (position->charpos),
2302 Qdisplay, object);
2303 if (NILP (prop))
2304 return HANDLED_NORMALLY;
2305
2306 space_or_image_found_p = 0;
2307 if (CONSP (prop)
2308 && CONSP (XCAR (prop))
2309 && !EQ (Qmargin, XCAR (XCAR (prop))))
2310 {
2311 /* A list of sub-properties. */
2312 while (CONSP (prop))
2313 {
2314 if (handle_single_display_prop (it, XCAR (prop), object, position))
2315 space_or_image_found_p = 1;
2316 prop = XCDR (prop);
2317 }
2318 }
2319 else if (VECTORP (prop))
2320 {
2321 int i;
2322 for (i = 0; i < XVECTOR (prop)->size; ++i)
2323 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2324 object, position))
2325 space_or_image_found_p = 1;
2326 }
2327 else
2328 {
2329 if (handle_single_display_prop (it, prop, object, position))
2330 space_or_image_found_p = 1;
2331 }
2332
2333 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2334 }
2335
2336
2337 /* Value is the position of the end of the `display' property starting
2338 at START_POS in OBJECT. */
2339
2340 static struct text_pos
2341 display_prop_end (it, object, start_pos)
2342 struct it *it;
2343 Lisp_Object object;
2344 struct text_pos start_pos;
2345 {
2346 Lisp_Object end;
2347 struct text_pos end_pos;
2348
2349 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2350 Qdisplay, object, Qnil);
2351 CHARPOS (end_pos) = XFASTINT (end);
2352 if (STRINGP (object))
2353 compute_string_pos (&end_pos, start_pos, it->string);
2354 else
2355 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2356
2357 return end_pos;
2358 }
2359
2360
2361 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2362 is the object in which the `display' property was found. *POSITION
2363 is the position at which it was found.
2364
2365 If PROP is a `space' or `image' sub-property, set *POSITION to the
2366 end position of the `display' property.
2367
2368 Value is non-zero if a `space' or `image' property value was found. */
2369
2370 static int
2371 handle_single_display_prop (it, prop, object, position)
2372 struct it *it;
2373 Lisp_Object prop;
2374 Lisp_Object object;
2375 struct text_pos *position;
2376 {
2377 Lisp_Object value;
2378 int space_or_image_found_p = 0;
2379 Lisp_Object form;
2380
2381 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2382 evaluated. If the result is nil, VALUE is ignored. */
2383 form = Qt;
2384 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2385 {
2386 prop = XCDR (prop);
2387 if (!CONSP (prop))
2388 return 0;
2389 form = XCAR (prop);
2390 prop = XCDR (prop);
2391 }
2392
2393 if (!NILP (form) && !EQ (form, Qt))
2394 {
2395 struct gcpro gcpro1;
2396 struct text_pos end_pos, pt;
2397
2398 GCPRO1 (form);
2399 end_pos = display_prop_end (it, object, *position);
2400
2401 /* Temporarily set point to the end position, and then evaluate
2402 the form. This makes `(eolp)' work as FORM. */
2403 if (BUFFERP (object))
2404 {
2405 CHARPOS (pt) = PT;
2406 BYTEPOS (pt) = PT_BYTE;
2407 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2408 }
2409
2410 form = eval_form (form);
2411
2412 if (BUFFERP (object))
2413 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2414 UNGCPRO;
2415 }
2416
2417 if (NILP (form))
2418 return 0;
2419
2420 if (CONSP (prop)
2421 && EQ (XCAR (prop), Qheight)
2422 && CONSP (XCDR (prop)))
2423 {
2424 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2425 return 0;
2426
2427 /* `(height HEIGHT)'. */
2428 it->font_height = XCAR (XCDR (prop));
2429 if (!NILP (it->font_height))
2430 {
2431 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2432 int new_height = -1;
2433
2434 if (CONSP (it->font_height)
2435 && (EQ (XCAR (it->font_height), Qplus)
2436 || EQ (XCAR (it->font_height), Qminus))
2437 && CONSP (XCDR (it->font_height))
2438 && INTEGERP (XCAR (XCDR (it->font_height))))
2439 {
2440 /* `(+ N)' or `(- N)' where N is an integer. */
2441 int steps = XINT (XCAR (XCDR (it->font_height)));
2442 if (EQ (XCAR (it->font_height), Qplus))
2443 steps = - steps;
2444 it->face_id = smaller_face (it->f, it->face_id, steps);
2445 }
2446 else if (FUNCTIONP (it->font_height))
2447 {
2448 /* Call function with current height as argument.
2449 Value is the new height. */
2450 Lisp_Object args[2], height;
2451
2452 args[0] = it->font_height;
2453 args[1] = face->lface[LFACE_HEIGHT_INDEX];
2454 height = call_function (2, args);
2455
2456 if (NUMBERP (height))
2457 new_height = XFLOATINT (height);
2458 }
2459 else if (NUMBERP (it->font_height))
2460 {
2461 /* Value is a multiple of the canonical char height. */
2462 struct face *face;
2463
2464 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2465 new_height = (XFLOATINT (it->font_height)
2466 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2467 }
2468 else
2469 {
2470 /* Evaluate IT->font_height with `height' bound to the
2471 current specified height to get the new height. */
2472 Lisp_Object value;
2473 int count = specpdl_ptr - specpdl;
2474
2475 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2476 value = eval_form (it->font_height);
2477 unbind_to (count, Qnil);
2478
2479 if (NUMBERP (value))
2480 new_height = XFLOATINT (value);
2481 }
2482
2483 if (new_height > 0)
2484 it->face_id = face_with_height (it->f, it->face_id, new_height);
2485 }
2486 }
2487 else if (CONSP (prop)
2488 && EQ (XCAR (prop), Qspace_width)
2489 && CONSP (XCDR (prop)))
2490 {
2491 /* `(space_width WIDTH)'. */
2492 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2493 return 0;
2494
2495 value = XCAR (XCDR (prop));
2496 if (NUMBERP (value) && XFLOATINT (value) > 0)
2497 it->space_width = value;
2498 }
2499 else if (CONSP (prop)
2500 && EQ (XCAR (prop), Qraise)
2501 && CONSP (XCDR (prop)))
2502 {
2503 /* `(raise FACTOR)'. */
2504 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2505 return 0;
2506
2507 #ifdef HAVE_WINDOW_SYSTEM
2508 value = XCAR (XCDR (prop));
2509 if (NUMBERP (value))
2510 {
2511 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2512 it->voffset = - (XFLOATINT (value)
2513 * (FONT_HEIGHT (face->font)));
2514 }
2515 #endif /* HAVE_WINDOW_SYSTEM */
2516 }
2517 else if (!it->string_from_display_prop_p)
2518 {
2519 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2520 VALUE) or `((margin nil) VALUE)' or VALUE. */
2521 Lisp_Object location, value;
2522 struct text_pos start_pos;
2523 int valid_p;
2524
2525 /* Characters having this form of property are not displayed, so
2526 we have to find the end of the property. */
2527 start_pos = *position;
2528 *position = display_prop_end (it, object, start_pos);
2529 value = Qnil;
2530
2531 /* Let's stop at the new position and assume that all
2532 text properties change there. */
2533 it->stop_charpos = position->charpos;
2534
2535 location = Qunbound;
2536 if (CONSP (prop) && CONSP (XCAR (prop)))
2537 {
2538 Lisp_Object tem;
2539
2540 value = XCDR (prop);
2541 if (CONSP (value))
2542 value = XCAR (value);
2543
2544 tem = XCAR (prop);
2545 if (EQ (XCAR (tem), Qmargin)
2546 && (tem = XCDR (tem),
2547 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2548 (NILP (tem)
2549 || EQ (tem, Qleft_margin)
2550 || EQ (tem, Qright_margin))))
2551 location = tem;
2552 }
2553
2554 if (EQ (location, Qunbound))
2555 {
2556 location = Qnil;
2557 value = prop;
2558 }
2559
2560 #ifdef HAVE_WINDOW_SYSTEM
2561 if (FRAME_TERMCAP_P (it->f))
2562 valid_p = STRINGP (value);
2563 else
2564 valid_p = (STRINGP (value)
2565 || (CONSP (value) && EQ (XCAR (value), Qspace))
2566 || valid_image_p (value));
2567 #else /* not HAVE_WINDOW_SYSTEM */
2568 valid_p = STRINGP (value);
2569 #endif /* not HAVE_WINDOW_SYSTEM */
2570
2571 if ((EQ (location, Qleft_margin)
2572 || EQ (location, Qright_margin)
2573 || NILP (location))
2574 && valid_p)
2575 {
2576 space_or_image_found_p = 1;
2577
2578 /* Save current settings of IT so that we can restore them
2579 when we are finished with the glyph property value. */
2580 push_it (it);
2581
2582 if (NILP (location))
2583 it->area = TEXT_AREA;
2584 else if (EQ (location, Qleft_margin))
2585 it->area = LEFT_MARGIN_AREA;
2586 else
2587 it->area = RIGHT_MARGIN_AREA;
2588
2589 if (STRINGP (value))
2590 {
2591 it->string = value;
2592 it->multibyte_p = STRING_MULTIBYTE (it->string);
2593 it->current.overlay_string_index = -1;
2594 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2595 it->end_charpos = it->string_nchars
2596 = XSTRING (it->string)->size;
2597 it->method = next_element_from_string;
2598 it->stop_charpos = 0;
2599 it->string_from_display_prop_p = 1;
2600 }
2601 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2602 {
2603 it->method = next_element_from_stretch;
2604 it->object = value;
2605 it->current.pos = it->position = start_pos;
2606 }
2607 #ifdef HAVE_WINDOW_SYSTEM
2608 else
2609 {
2610 it->what = IT_IMAGE;
2611 it->image_id = lookup_image (it->f, value);
2612 it->position = start_pos;
2613 it->object = NILP (object) ? it->w->buffer : object;
2614 it->method = next_element_from_image;
2615
2616 /* Say that we haven't consumed the characters with
2617 `display' property yet. The call to pop_it in
2618 set_iterator_to_next will clean this up. */
2619 *position = start_pos;
2620 }
2621 #endif /* HAVE_WINDOW_SYSTEM */
2622 }
2623 else
2624 /* Invalid property or property not supported. Restore
2625 the position to what it was before. */
2626 *position = start_pos;
2627 }
2628
2629 return space_or_image_found_p;
2630 }
2631
2632
2633 /* Check if PROP is a display sub-property value whose text should be
2634 treated as intangible. */
2635
2636 static int
2637 single_display_prop_intangible_p (prop)
2638 Lisp_Object prop;
2639 {
2640 /* Skip over `when FORM'. */
2641 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2642 {
2643 prop = XCDR (prop);
2644 if (!CONSP (prop))
2645 return 0;
2646 prop = XCDR (prop);
2647 }
2648
2649 if (!CONSP (prop))
2650 return 0;
2651
2652 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2653 we don't need to treat text as intangible. */
2654 if (EQ (XCAR (prop), Qmargin))
2655 {
2656 prop = XCDR (prop);
2657 if (!CONSP (prop))
2658 return 0;
2659
2660 prop = XCDR (prop);
2661 if (!CONSP (prop)
2662 || EQ (XCAR (prop), Qleft_margin)
2663 || EQ (XCAR (prop), Qright_margin))
2664 return 0;
2665 }
2666
2667 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2668 }
2669
2670
2671 /* Check if PROP is a display property value whose text should be
2672 treated as intangible. */
2673
2674 int
2675 display_prop_intangible_p (prop)
2676 Lisp_Object prop;
2677 {
2678 if (CONSP (prop)
2679 && CONSP (XCAR (prop))
2680 && !EQ (Qmargin, XCAR (XCAR (prop))))
2681 {
2682 /* A list of sub-properties. */
2683 while (CONSP (prop))
2684 {
2685 if (single_display_prop_intangible_p (XCAR (prop)))
2686 return 1;
2687 prop = XCDR (prop);
2688 }
2689 }
2690 else if (VECTORP (prop))
2691 {
2692 /* A vector of sub-properties. */
2693 int i;
2694 for (i = 0; i < XVECTOR (prop)->size; ++i)
2695 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2696 return 1;
2697 }
2698 else
2699 return single_display_prop_intangible_p (prop);
2700
2701 return 0;
2702 }
2703
2704 \f
2705 /***********************************************************************
2706 `composition' property
2707 ***********************************************************************/
2708
2709 /* Set up iterator IT from `composition' property at its current
2710 position. Called from handle_stop. */
2711
2712 static enum prop_handled
2713 handle_composition_prop (it)
2714 struct it *it;
2715 {
2716 Lisp_Object prop, string;
2717 int pos, pos_byte, end;
2718 enum prop_handled handled = HANDLED_NORMALLY;
2719
2720 if (STRINGP (it->string))
2721 {
2722 pos = IT_STRING_CHARPOS (*it);
2723 pos_byte = IT_STRING_BYTEPOS (*it);
2724 string = it->string;
2725 }
2726 else
2727 {
2728 pos = IT_CHARPOS (*it);
2729 pos_byte = IT_BYTEPOS (*it);
2730 string = Qnil;
2731 }
2732
2733 /* If there's a valid composition and point is not inside of the
2734 composition (in the case that the composition is from the current
2735 buffer), draw a glyph composed from the composition components. */
2736 if (find_composition (pos, -1, &pos, &end, &prop, string)
2737 && COMPOSITION_VALID_P (pos, end, prop)
2738 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2739 {
2740 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2741
2742 if (id >= 0)
2743 {
2744 it->method = next_element_from_composition;
2745 it->cmp_id = id;
2746 it->cmp_len = COMPOSITION_LENGTH (prop);
2747 /* For a terminal, draw only the first character of the
2748 components. */
2749 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2750 it->len = (STRINGP (it->string)
2751 ? string_char_to_byte (it->string, end)
2752 : CHAR_TO_BYTE (end)) - pos_byte;
2753 it->stop_charpos = end;
2754 handled = HANDLED_RETURN;
2755 }
2756 }
2757
2758 return handled;
2759 }
2760
2761
2762 \f
2763 /***********************************************************************
2764 Overlay strings
2765 ***********************************************************************/
2766
2767 /* The following structure is used to record overlay strings for
2768 later sorting in load_overlay_strings. */
2769
2770 struct overlay_entry
2771 {
2772 Lisp_Object overlay;
2773 Lisp_Object string;
2774 int priority;
2775 int after_string_p;
2776 };
2777
2778
2779 /* Set up iterator IT from overlay strings at its current position.
2780 Called from handle_stop. */
2781
2782 static enum prop_handled
2783 handle_overlay_change (it)
2784 struct it *it;
2785 {
2786 if (!STRINGP (it->string) && get_overlay_strings (it))
2787 return HANDLED_RECOMPUTE_PROPS;
2788 else
2789 return HANDLED_NORMALLY;
2790 }
2791
2792
2793 /* Set up the next overlay string for delivery by IT, if there is an
2794 overlay string to deliver. Called by set_iterator_to_next when the
2795 end of the current overlay string is reached. If there are more
2796 overlay strings to display, IT->string and
2797 IT->current.overlay_string_index are set appropriately here.
2798 Otherwise IT->string is set to nil. */
2799
2800 static void
2801 next_overlay_string (it)
2802 struct it *it;
2803 {
2804 ++it->current.overlay_string_index;
2805 if (it->current.overlay_string_index == it->n_overlay_strings)
2806 {
2807 /* No more overlay strings. Restore IT's settings to what
2808 they were before overlay strings were processed, and
2809 continue to deliver from current_buffer. */
2810 pop_it (it);
2811 xassert (it->stop_charpos >= BEGV
2812 && it->stop_charpos <= it->end_charpos);
2813 it->string = Qnil;
2814 it->current.overlay_string_index = -1;
2815 SET_TEXT_POS (it->current.string_pos, -1, -1);
2816 it->n_overlay_strings = 0;
2817 it->method = next_element_from_buffer;
2818
2819 /* If we're at the end of the buffer, record that we have
2820 processed the overlay strings there already, so that
2821 next_element_from_buffer doesn't try it again. */
2822 if (IT_CHARPOS (*it) >= it->end_charpos)
2823 it->overlay_strings_at_end_processed_p = 1;
2824 }
2825 else
2826 {
2827 /* There are more overlay strings to process. If
2828 IT->current.overlay_string_index has advanced to a position
2829 where we must load IT->overlay_strings with more strings, do
2830 it. */
2831 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2832
2833 if (it->current.overlay_string_index && i == 0)
2834 load_overlay_strings (it);
2835
2836 /* Initialize IT to deliver display elements from the overlay
2837 string. */
2838 it->string = it->overlay_strings[i];
2839 it->multibyte_p = STRING_MULTIBYTE (it->string);
2840 SET_TEXT_POS (it->current.string_pos, 0, 0);
2841 it->method = next_element_from_string;
2842 it->stop_charpos = 0;
2843 }
2844
2845 CHECK_IT (it);
2846 }
2847
2848
2849 /* Compare two overlay_entry structures E1 and E2. Used as a
2850 comparison function for qsort in load_overlay_strings. Overlay
2851 strings for the same position are sorted so that
2852
2853 1. All after-strings come in front of before-strings, except
2854 when they come from the same overlay.
2855
2856 2. Within after-strings, strings are sorted so that overlay strings
2857 from overlays with higher priorities come first.
2858
2859 2. Within before-strings, strings are sorted so that overlay
2860 strings from overlays with higher priorities come last.
2861
2862 Value is analogous to strcmp. */
2863
2864
2865 static int
2866 compare_overlay_entries (e1, e2)
2867 void *e1, *e2;
2868 {
2869 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2870 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2871 int result;
2872
2873 if (entry1->after_string_p != entry2->after_string_p)
2874 {
2875 /* Let after-strings appear in front of before-strings if
2876 they come from different overlays. */
2877 if (EQ (entry1->overlay, entry2->overlay))
2878 result = entry1->after_string_p ? 1 : -1;
2879 else
2880 result = entry1->after_string_p ? -1 : 1;
2881 }
2882 else if (entry1->after_string_p)
2883 /* After-strings sorted in order of decreasing priority. */
2884 result = entry2->priority - entry1->priority;
2885 else
2886 /* Before-strings sorted in order of increasing priority. */
2887 result = entry1->priority - entry2->priority;
2888
2889 return result;
2890 }
2891
2892
2893 /* Load the vector IT->overlay_strings with overlay strings from IT's
2894 current buffer position. Set IT->n_overlays to the total number of
2895 overlay strings found.
2896
2897 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2898 a time. On entry into load_overlay_strings,
2899 IT->current.overlay_string_index gives the number of overlay
2900 strings that have already been loaded by previous calls to this
2901 function.
2902
2903 IT->add_overlay_start contains an additional overlay start
2904 position to consider for taking overlay strings from, if non-zero.
2905 This position comes into play when the overlay has an `invisible'
2906 property, and both before and after-strings. When we've skipped to
2907 the end of the overlay, because of its `invisible' property, we
2908 nevertheless want its before-string to appear.
2909 IT->add_overlay_start will contain the overlay start position
2910 in this case.
2911
2912 Overlay strings are sorted so that after-string strings come in
2913 front of before-string strings. Within before and after-strings,
2914 strings are sorted by overlay priority. See also function
2915 compare_overlay_entries. */
2916
2917 static void
2918 load_overlay_strings (it)
2919 struct it *it;
2920 {
2921 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2922 Lisp_Object ov, overlay, window, str;
2923 int start, end;
2924 int size = 20;
2925 int n = 0, i, j;
2926 struct overlay_entry *entries
2927 = (struct overlay_entry *) alloca (size * sizeof *entries);
2928
2929 /* Append the overlay string STRING of overlay OVERLAY to vector
2930 `entries' which has size `size' and currently contains `n'
2931 elements. AFTER_P non-zero means STRING is an after-string of
2932 OVERLAY. */
2933 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2934 do \
2935 { \
2936 Lisp_Object priority; \
2937 \
2938 if (n == size) \
2939 { \
2940 int new_size = 2 * size; \
2941 struct overlay_entry *old = entries; \
2942 entries = \
2943 (struct overlay_entry *) alloca (new_size \
2944 * sizeof *entries); \
2945 bcopy (old, entries, size * sizeof *entries); \
2946 size = new_size; \
2947 } \
2948 \
2949 entries[n].string = (STRING); \
2950 entries[n].overlay = (OVERLAY); \
2951 priority = Foverlay_get ((OVERLAY), Qpriority); \
2952 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
2953 entries[n].after_string_p = (AFTER_P); \
2954 ++n; \
2955 } \
2956 while (0)
2957
2958 /* Process overlay before the overlay center. */
2959 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
2960 {
2961 overlay = XCAR (ov);
2962 xassert (OVERLAYP (overlay));
2963 start = OVERLAY_POSITION (OVERLAY_START (overlay));
2964 end = OVERLAY_POSITION (OVERLAY_END (overlay));
2965
2966 if (end < IT_CHARPOS (*it))
2967 break;
2968
2969 /* Skip this overlay if it doesn't start or end at IT's current
2970 position. */
2971 if (end != IT_CHARPOS (*it)
2972 && start != IT_CHARPOS (*it)
2973 && it->add_overlay_start != IT_CHARPOS (*it))
2974 continue;
2975
2976 /* Skip this overlay if it doesn't apply to IT->w. */
2977 window = Foverlay_get (overlay, Qwindow);
2978 if (WINDOWP (window) && XWINDOW (window) != it->w)
2979 continue;
2980
2981 /* If overlay has a non-empty before-string, record it. */
2982 if ((start == IT_CHARPOS (*it)
2983 || start == it->add_overlay_start)
2984 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
2985 && XSTRING (str)->size)
2986 RECORD_OVERLAY_STRING (overlay, str, 0);
2987
2988 /* If overlay has a non-empty after-string, record it. */
2989 if (end == IT_CHARPOS (*it)
2990 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
2991 && XSTRING (str)->size)
2992 RECORD_OVERLAY_STRING (overlay, str, 1);
2993 }
2994
2995 /* Process overlays after the overlay center. */
2996 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
2997 {
2998 overlay = XCAR (ov);
2999 xassert (OVERLAYP (overlay));
3000 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3001 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3002
3003 if (start > IT_CHARPOS (*it))
3004 break;
3005
3006 /* Skip this overlay if it doesn't start or end at IT's current
3007 position. */
3008 if (end != IT_CHARPOS (*it)
3009 && start != IT_CHARPOS (*it)
3010 && it->add_overlay_start != IT_CHARPOS (*it))
3011 continue;
3012
3013 /* Skip this overlay if it doesn't apply to IT->w. */
3014 window = Foverlay_get (overlay, Qwindow);
3015 if (WINDOWP (window) && XWINDOW (window) != it->w)
3016 continue;
3017
3018 /* If overlay has a non-empty before-string, record it. */
3019 if ((start == IT_CHARPOS (*it)
3020 || start == it->add_overlay_start)
3021 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3022 && XSTRING (str)->size)
3023 RECORD_OVERLAY_STRING (overlay, str, 0);
3024
3025 /* If overlay has a non-empty after-string, record it. */
3026 if (end == IT_CHARPOS (*it)
3027 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3028 && XSTRING (str)->size)
3029 RECORD_OVERLAY_STRING (overlay, str, 1);
3030 }
3031
3032 #undef RECORD_OVERLAY_STRING
3033
3034 /* Sort entries. */
3035 if (n)
3036 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3037
3038 /* Record the total number of strings to process. */
3039 it->n_overlay_strings = n;
3040
3041 /* IT->current.overlay_string_index is the number of overlay strings
3042 that have already been consumed by IT. Copy some of the
3043 remaining overlay strings to IT->overlay_strings. */
3044 i = 0;
3045 j = it->current.overlay_string_index;
3046 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3047 it->overlay_strings[i++] = entries[j++].string;
3048
3049 CHECK_IT (it);
3050 }
3051
3052
3053 /* Get the first chunk of overlay strings at IT's current buffer
3054 position. Value is non-zero if at least one overlay string was
3055 found. */
3056
3057 static int
3058 get_overlay_strings (it)
3059 struct it *it;
3060 {
3061 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3062 process. This fills IT->overlay_strings with strings, and sets
3063 IT->n_overlay_strings to the total number of strings to process.
3064 IT->pos.overlay_string_index has to be set temporarily to zero
3065 because load_overlay_strings needs this; it must be set to -1
3066 when no overlay strings are found because a zero value would
3067 indicate a position in the first overlay string. */
3068 it->current.overlay_string_index = 0;
3069 load_overlay_strings (it);
3070
3071 /* If we found overlay strings, set up IT to deliver display
3072 elements from the first one. Otherwise set up IT to deliver
3073 from current_buffer. */
3074 if (it->n_overlay_strings)
3075 {
3076 /* Make sure we know settings in current_buffer, so that we can
3077 restore meaningful values when we're done with the overlay
3078 strings. */
3079 compute_stop_pos (it);
3080 xassert (it->face_id >= 0);
3081
3082 /* Save IT's settings. They are restored after all overlay
3083 strings have been processed. */
3084 xassert (it->sp == 0);
3085 push_it (it);
3086
3087 /* Set up IT to deliver display elements from the first overlay
3088 string. */
3089 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3090 it->stop_charpos = 0;
3091 it->string = it->overlay_strings[0];
3092 it->multibyte_p = STRING_MULTIBYTE (it->string);
3093 xassert (STRINGP (it->string));
3094 it->method = next_element_from_string;
3095 }
3096 else
3097 {
3098 it->string = Qnil;
3099 it->current.overlay_string_index = -1;
3100 it->method = next_element_from_buffer;
3101 }
3102
3103 CHECK_IT (it);
3104
3105 /* Value is non-zero if we found at least one overlay string. */
3106 return STRINGP (it->string);
3107 }
3108
3109
3110 \f
3111 /***********************************************************************
3112 Saving and restoring state
3113 ***********************************************************************/
3114
3115 /* Save current settings of IT on IT->stack. Called, for example,
3116 before setting up IT for an overlay string, to be able to restore
3117 IT's settings to what they were after the overlay string has been
3118 processed. */
3119
3120 static void
3121 push_it (it)
3122 struct it *it;
3123 {
3124 struct iterator_stack_entry *p;
3125
3126 xassert (it->sp < 2);
3127 p = it->stack + it->sp;
3128
3129 p->stop_charpos = it->stop_charpos;
3130 xassert (it->face_id >= 0);
3131 p->face_id = it->face_id;
3132 p->string = it->string;
3133 p->pos = it->current;
3134 p->end_charpos = it->end_charpos;
3135 p->string_nchars = it->string_nchars;
3136 p->area = it->area;
3137 p->multibyte_p = it->multibyte_p;
3138 p->space_width = it->space_width;
3139 p->font_height = it->font_height;
3140 p->voffset = it->voffset;
3141 p->string_from_display_prop_p = it->string_from_display_prop_p;
3142 ++it->sp;
3143 }
3144
3145
3146 /* Restore IT's settings from IT->stack. Called, for example, when no
3147 more overlay strings must be processed, and we return to delivering
3148 display elements from a buffer, or when the end of a string from a
3149 `display' property is reached and we return to delivering display
3150 elements from an overlay string, or from a buffer. */
3151
3152 static void
3153 pop_it (it)
3154 struct it *it;
3155 {
3156 struct iterator_stack_entry *p;
3157
3158 xassert (it->sp > 0);
3159 --it->sp;
3160 p = it->stack + it->sp;
3161 it->stop_charpos = p->stop_charpos;
3162 it->face_id = p->face_id;
3163 it->string = p->string;
3164 it->current = p->pos;
3165 it->end_charpos = p->end_charpos;
3166 it->string_nchars = p->string_nchars;
3167 it->area = p->area;
3168 it->multibyte_p = p->multibyte_p;
3169 it->space_width = p->space_width;
3170 it->font_height = p->font_height;
3171 it->voffset = p->voffset;
3172 it->string_from_display_prop_p = p->string_from_display_prop_p;
3173 }
3174
3175
3176 \f
3177 /***********************************************************************
3178 Moving over lines
3179 ***********************************************************************/
3180
3181 /* Set IT's current position to the previous line start. */
3182
3183 static void
3184 back_to_previous_line_start (it)
3185 struct it *it;
3186 {
3187 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3188 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3189 }
3190
3191
3192 /* Set IT's current position to the next line start. */
3193
3194 static void
3195 forward_to_next_line_start (it)
3196 struct it *it;
3197 {
3198 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), 1);
3199 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3200 }
3201
3202
3203 /* Set IT's current position to the previous visible line start. Skip
3204 invisible text that is so either due to text properties or due to
3205 selective display. Caution: this does not change IT->current_x and
3206 IT->hpos. */
3207
3208 static void
3209 back_to_previous_visible_line_start (it)
3210 struct it *it;
3211 {
3212 int visible_p = 0;
3213
3214 /* Go back one newline if not on BEGV already. */
3215 if (IT_CHARPOS (*it) > BEGV)
3216 back_to_previous_line_start (it);
3217
3218 /* Move over lines that are invisible because of selective display
3219 or text properties. */
3220 while (IT_CHARPOS (*it) > BEGV
3221 && !visible_p)
3222 {
3223 visible_p = 1;
3224
3225 /* If selective > 0, then lines indented more than that values
3226 are invisible. */
3227 if (it->selective > 0
3228 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3229 it->selective))
3230 visible_p = 0;
3231 else
3232 {
3233 Lisp_Object prop;
3234
3235 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3236 Qinvisible, it->window);
3237 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3238 visible_p = 0;
3239 }
3240
3241 /* Back one more newline if the current one is invisible. */
3242 if (!visible_p)
3243 back_to_previous_line_start (it);
3244 }
3245
3246 xassert (IT_CHARPOS (*it) >= BEGV);
3247 xassert (IT_CHARPOS (*it) == BEGV
3248 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3249 CHECK_IT (it);
3250 }
3251
3252
3253 /* Reseat iterator IT at the previous visible line start. Skip
3254 invisible text that is so either due to text properties or due to
3255 selective display. At the end, update IT's overlay information,
3256 face information etc. */
3257
3258 static void
3259 reseat_at_previous_visible_line_start (it)
3260 struct it *it;
3261 {
3262 back_to_previous_visible_line_start (it);
3263 reseat (it, it->current.pos, 1);
3264 CHECK_IT (it);
3265 }
3266
3267
3268 /* Reseat iterator IT on the next visible line start in the current
3269 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3270 preceding the line start. Skip over invisible text that is so
3271 because of selective display. Compute faces, overlays etc at the
3272 new position. Note that this function does not skip over text that
3273 is invisible because of text properties. */
3274
3275 static void
3276 reseat_at_next_visible_line_start (it, on_newline_p)
3277 struct it *it;
3278 int on_newline_p;
3279 {
3280 /* Restore the buffer position when currently not delivering display
3281 elements from the current buffer. This is the case, for example,
3282 when called at the end of a truncated overlay string. */
3283 while (it->sp)
3284 pop_it (it);
3285 it->method = next_element_from_buffer;
3286
3287 /* Otherwise, scan_buffer would not work. */
3288 if (IT_CHARPOS (*it) < ZV)
3289 {
3290 /* If on a newline, advance past it. Otherwise, find the next
3291 newline which automatically gives us the position following
3292 the newline. */
3293 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
3294 {
3295 ++IT_CHARPOS (*it);
3296 ++IT_BYTEPOS (*it);
3297 }
3298 else
3299 forward_to_next_line_start (it);
3300
3301 /* We must either have reached the end of the buffer or end up
3302 after a newline. */
3303 xassert (IT_CHARPOS (*it) == ZV
3304 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3305
3306 /* Skip over lines that are invisible because they are indented
3307 more than the value of IT->selective. */
3308 if (it->selective > 0)
3309 while (IT_CHARPOS (*it) < ZV
3310 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3311 it->selective))
3312 forward_to_next_line_start (it);
3313
3314 /* Position on the newline if we should. */
3315 if (on_newline_p
3316 && IT_CHARPOS (*it) > BEGV
3317 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n')
3318 {
3319 --IT_CHARPOS (*it);
3320 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3321 }
3322
3323 /* Set the iterator there. The 0 as the last parameter of
3324 reseat means don't force a text property lookup. The lookup
3325 is then only done if we've skipped past the iterator's
3326 check_charpos'es. This optimization is important because
3327 text property lookups tend to be expensive. */
3328 reseat (it, it->current.pos, 0);
3329 }
3330
3331 CHECK_IT (it);
3332 }
3333
3334
3335 \f
3336 /***********************************************************************
3337 Changing an iterator's position
3338 ***********************************************************************/
3339
3340 /* Change IT's current position to POS in current_buffer. If FORCE_P
3341 is non-zero, always check for text properties at the new position.
3342 Otherwise, text properties are only looked up if POS >=
3343 IT->check_charpos of a property. */
3344
3345 static void
3346 reseat (it, pos, force_p)
3347 struct it *it;
3348 struct text_pos pos;
3349 int force_p;
3350 {
3351 int original_pos = IT_CHARPOS (*it);
3352
3353 reseat_1 (it, pos, 0);
3354
3355 /* Determine where to check text properties. Avoid doing it
3356 where possible because text property lookup is very expensive. */
3357 if (force_p
3358 || CHARPOS (pos) > it->stop_charpos
3359 || CHARPOS (pos) < original_pos)
3360 handle_stop (it);
3361
3362 CHECK_IT (it);
3363 }
3364
3365
3366 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3367 IT->stop_pos to POS, also. */
3368
3369 static void
3370 reseat_1 (it, pos, set_stop_p)
3371 struct it *it;
3372 struct text_pos pos;
3373 int set_stop_p;
3374 {
3375 /* Don't call this function when scanning a C string. */
3376 xassert (it->s == NULL);
3377
3378 /* POS must be a reasonable value. */
3379 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3380
3381 it->current.pos = it->position = pos;
3382 XSETBUFFER (it->object, current_buffer);
3383 it->dpvec = NULL;
3384 it->current.dpvec_index = -1;
3385 it->current.overlay_string_index = -1;
3386 IT_STRING_CHARPOS (*it) = -1;
3387 IT_STRING_BYTEPOS (*it) = -1;
3388 it->string = Qnil;
3389 it->method = next_element_from_buffer;
3390 it->sp = 0;
3391
3392 if (set_stop_p)
3393 it->stop_charpos = CHARPOS (pos);
3394 }
3395
3396
3397 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3398 If S is non-null, it is a C string to iterate over. Otherwise,
3399 STRING gives a Lisp string to iterate over.
3400
3401 If PRECISION > 0, don't return more then PRECISION number of
3402 characters from the string.
3403
3404 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3405 characters have been returned. FIELD_WIDTH < 0 means an infinite
3406 field width.
3407
3408 MULTIBYTE = 0 means disable processing of multibyte characters,
3409 MULTIBYTE > 0 means enable it,
3410 MULTIBYTE < 0 means use IT->multibyte_p.
3411
3412 IT must be initialized via a prior call to init_iterator before
3413 calling this function. */
3414
3415 static void
3416 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3417 struct it *it;
3418 unsigned char *s;
3419 Lisp_Object string;
3420 int charpos;
3421 int precision, field_width, multibyte;
3422 {
3423 /* No region in strings. */
3424 it->region_beg_charpos = it->region_end_charpos = -1;
3425
3426 /* No text property checks performed by default, but see below. */
3427 it->stop_charpos = -1;
3428
3429 /* Set iterator position and end position. */
3430 bzero (&it->current, sizeof it->current);
3431 it->current.overlay_string_index = -1;
3432 it->current.dpvec_index = -1;
3433 xassert (charpos >= 0);
3434
3435 /* Use the setting of MULTIBYTE if specified. */
3436 if (multibyte >= 0)
3437 it->multibyte_p = multibyte > 0;
3438
3439 if (s == NULL)
3440 {
3441 xassert (STRINGP (string));
3442 it->string = string;
3443 it->s = NULL;
3444 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3445 it->method = next_element_from_string;
3446 it->current.string_pos = string_pos (charpos, string);
3447 }
3448 else
3449 {
3450 it->s = s;
3451 it->string = Qnil;
3452
3453 /* Note that we use IT->current.pos, not it->current.string_pos,
3454 for displaying C strings. */
3455 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3456 if (it->multibyte_p)
3457 {
3458 it->current.pos = c_string_pos (charpos, s, 1);
3459 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3460 }
3461 else
3462 {
3463 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3464 it->end_charpos = it->string_nchars = strlen (s);
3465 }
3466
3467 it->method = next_element_from_c_string;
3468 }
3469
3470 /* PRECISION > 0 means don't return more than PRECISION characters
3471 from the string. */
3472 if (precision > 0 && it->end_charpos - charpos > precision)
3473 it->end_charpos = it->string_nchars = charpos + precision;
3474
3475 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3476 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3477 FIELD_WIDTH < 0 means infinite field width. This is useful for
3478 padding with `-' at the end of a mode line. */
3479 if (field_width < 0)
3480 field_width = INFINITY;
3481 if (field_width > it->end_charpos - charpos)
3482 it->end_charpos = charpos + field_width;
3483
3484 /* Use the standard display table for displaying strings. */
3485 if (DISP_TABLE_P (Vstandard_display_table))
3486 it->dp = XCHAR_TABLE (Vstandard_display_table);
3487
3488 it->stop_charpos = charpos;
3489 CHECK_IT (it);
3490 }
3491
3492
3493 \f
3494 /***********************************************************************
3495 Iteration
3496 ***********************************************************************/
3497
3498 /* Load IT's display element fields with information about the next
3499 display element from the current position of IT. Value is zero if
3500 end of buffer (or C string) is reached. */
3501
3502 int
3503 get_next_display_element (it)
3504 struct it *it;
3505 {
3506 /* Non-zero means that we found an display element. Zero means that
3507 we hit the end of what we iterate over. Performance note: the
3508 function pointer `method' used here turns out to be faster than
3509 using a sequence of if-statements. */
3510 int success_p = (*it->method) (it);
3511
3512 if (it->what == IT_CHARACTER)
3513 {
3514 /* Map via display table or translate control characters.
3515 IT->c, IT->len etc. have been set to the next character by
3516 the function call above. If we have a display table, and it
3517 contains an entry for IT->c, translate it. Don't do this if
3518 IT->c itself comes from a display table, otherwise we could
3519 end up in an infinite recursion. (An alternative could be to
3520 count the recursion depth of this function and signal an
3521 error when a certain maximum depth is reached.) Is it worth
3522 it? */
3523 if (success_p && it->dpvec == NULL)
3524 {
3525 Lisp_Object dv;
3526
3527 if (it->dp
3528 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3529 VECTORP (dv)))
3530 {
3531 struct Lisp_Vector *v = XVECTOR (dv);
3532
3533 /* Return the first character from the display table
3534 entry, if not empty. If empty, don't display the
3535 current character. */
3536 if (v->size)
3537 {
3538 it->dpvec_char_len = it->len;
3539 it->dpvec = v->contents;
3540 it->dpend = v->contents + v->size;
3541 it->current.dpvec_index = 0;
3542 it->method = next_element_from_display_vector;
3543 }
3544
3545 success_p = get_next_display_element (it);
3546 }
3547
3548 /* Translate control characters into `\003' or `^C' form.
3549 Control characters coming from a display table entry are
3550 currently not translated because we use IT->dpvec to hold
3551 the translation. This could easily be changed but I
3552 don't believe that it is worth doing.
3553
3554 Non-printable multibyte characters are also translated
3555 octal form. */
3556 else if ((it->c < ' '
3557 && (it->area != TEXT_AREA
3558 || (it->c != '\n' && it->c != '\t')))
3559 || (it->c >= 127
3560 && it->len == 1)
3561 || !CHAR_PRINTABLE_P (it->c))
3562 {
3563 /* IT->c is a control character which must be displayed
3564 either as '\003' or as `^C' where the '\\' and '^'
3565 can be defined in the display table. Fill
3566 IT->ctl_chars with glyphs for what we have to
3567 display. Then, set IT->dpvec to these glyphs. */
3568 GLYPH g;
3569
3570 if (it->c < 128 && it->ctl_arrow_p)
3571 {
3572 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3573 if (it->dp
3574 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3575 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3576 g = XINT (DISP_CTRL_GLYPH (it->dp));
3577 else
3578 g = FAST_MAKE_GLYPH ('^', 0);
3579 XSETINT (it->ctl_chars[0], g);
3580
3581 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3582 XSETINT (it->ctl_chars[1], g);
3583
3584 /* Set up IT->dpvec and return first character from it. */
3585 it->dpvec_char_len = it->len;
3586 it->dpvec = it->ctl_chars;
3587 it->dpend = it->dpvec + 2;
3588 it->current.dpvec_index = 0;
3589 it->method = next_element_from_display_vector;
3590 get_next_display_element (it);
3591 }
3592 else
3593 {
3594 unsigned char str[MAX_MULTIBYTE_LENGTH];
3595 int len;
3596 int i;
3597 GLYPH escape_glyph;
3598
3599 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3600 if (it->dp
3601 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3602 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3603 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3604 else
3605 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3606
3607 if (SINGLE_BYTE_CHAR_P (it->c))
3608 str[0] = it->c, len = 1;
3609 else
3610 len = CHAR_STRING (it->c, str);
3611
3612 for (i = 0; i < len; i++)
3613 {
3614 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3615 /* Insert three more glyphs into IT->ctl_chars for
3616 the octal display of the character. */
3617 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3618 XSETINT (it->ctl_chars[i * 4 + 1], g);
3619 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3620 XSETINT (it->ctl_chars[i * 4 + 2], g);
3621 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3622 XSETINT (it->ctl_chars[i * 4 + 3], g);
3623 }
3624
3625 /* Set up IT->dpvec and return the first character
3626 from it. */
3627 it->dpvec_char_len = it->len;
3628 it->dpvec = it->ctl_chars;
3629 it->dpend = it->dpvec + len * 4;
3630 it->current.dpvec_index = 0;
3631 it->method = next_element_from_display_vector;
3632 get_next_display_element (it);
3633 }
3634 }
3635 }
3636
3637 /* Adjust face id for a multibyte character. There are no
3638 multibyte character in unibyte text. */
3639 if (it->multibyte_p
3640 && success_p
3641 && FRAME_WINDOW_P (it->f))
3642 {
3643 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3644 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3645 }
3646 }
3647
3648 /* Is this character the last one of a run of characters with
3649 box? If yes, set IT->end_of_box_run_p to 1. */
3650 if (it->face_box_p
3651 && it->s == NULL)
3652 {
3653 int face_id;
3654 struct face *face;
3655
3656 it->end_of_box_run_p
3657 = ((face_id = face_after_it_pos (it),
3658 face_id != it->face_id)
3659 && (face = FACE_FROM_ID (it->f, face_id),
3660 face->box == FACE_NO_BOX));
3661 }
3662
3663 /* Value is 0 if end of buffer or string reached. */
3664 return success_p;
3665 }
3666
3667
3668 /* Move IT to the next display element.
3669
3670 Functions get_next_display_element and set_iterator_to_next are
3671 separate because I find this arrangement easier to handle than a
3672 get_next_display_element function that also increments IT's
3673 position. The way it is we can first look at an iterator's current
3674 display element, decide whether it fits on a line, and if it does,
3675 increment the iterator position. The other way around we probably
3676 would either need a flag indicating whether the iterator has to be
3677 incremented the next time, or we would have to implement a
3678 decrement position function which would not be easy to write. */
3679
3680 void
3681 set_iterator_to_next (it)
3682 struct it *it;
3683 {
3684 if (it->method == next_element_from_buffer)
3685 {
3686 /* The current display element of IT is a character from
3687 current_buffer. Advance in the buffer, and maybe skip over
3688 invisible lines that are so because of selective display. */
3689 if (ITERATOR_AT_END_OF_LINE_P (it))
3690 reseat_at_next_visible_line_start (it, 0);
3691 else
3692 {
3693 xassert (it->len != 0);
3694 IT_BYTEPOS (*it) += it->len;
3695 IT_CHARPOS (*it) += 1;
3696 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3697 }
3698 }
3699 else if (it->method == next_element_from_composition)
3700 {
3701 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3702 if (STRINGP (it->string))
3703 {
3704 IT_STRING_BYTEPOS (*it) += it->len;
3705 IT_STRING_CHARPOS (*it) += it->cmp_len;
3706 it->method = next_element_from_string;
3707 goto consider_string_end;
3708 }
3709 else
3710 {
3711 IT_BYTEPOS (*it) += it->len;
3712 IT_CHARPOS (*it) += it->cmp_len;
3713 it->method = next_element_from_buffer;
3714 }
3715 }
3716 else if (it->method == next_element_from_c_string)
3717 {
3718 /* Current display element of IT is from a C string. */
3719 IT_BYTEPOS (*it) += it->len;
3720 IT_CHARPOS (*it) += 1;
3721 }
3722 else if (it->method == next_element_from_display_vector)
3723 {
3724 /* Current display element of IT is from a display table entry.
3725 Advance in the display table definition. Reset it to null if
3726 end reached, and continue with characters from buffers/
3727 strings. */
3728 ++it->current.dpvec_index;
3729
3730 /* Restore face of the iterator to what they were before the
3731 display vector entry (these entries may contain faces). */
3732 it->face_id = it->saved_face_id;
3733
3734 if (it->dpvec + it->current.dpvec_index == it->dpend)
3735 {
3736 if (it->s)
3737 it->method = next_element_from_c_string;
3738 else if (STRINGP (it->string))
3739 it->method = next_element_from_string;
3740 else
3741 it->method = next_element_from_buffer;
3742
3743 it->dpvec = NULL;
3744 it->current.dpvec_index = -1;
3745
3746 /* Skip over characters which were displayed via IT->dpvec. */
3747 if (it->dpvec_char_len < 0)
3748 reseat_at_next_visible_line_start (it, 1);
3749 else if (it->dpvec_char_len > 0)
3750 {
3751 it->len = it->dpvec_char_len;
3752 set_iterator_to_next (it);
3753 }
3754 }
3755 }
3756 else if (it->method == next_element_from_string)
3757 {
3758 /* Current display element is a character from a Lisp string. */
3759 xassert (it->s == NULL && STRINGP (it->string));
3760 IT_STRING_BYTEPOS (*it) += it->len;
3761 IT_STRING_CHARPOS (*it) += 1;
3762
3763 consider_string_end:
3764
3765 if (it->current.overlay_string_index >= 0)
3766 {
3767 /* IT->string is an overlay string. Advance to the
3768 next, if there is one. */
3769 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3770 next_overlay_string (it);
3771 }
3772 else
3773 {
3774 /* IT->string is not an overlay string. If we reached
3775 its end, and there is something on IT->stack, proceed
3776 with what is on the stack. This can be either another
3777 string, this time an overlay string, or a buffer. */
3778 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3779 && it->sp > 0)
3780 {
3781 pop_it (it);
3782 if (!STRINGP (it->string))
3783 it->method = next_element_from_buffer;
3784 }
3785 }
3786 }
3787 else if (it->method == next_element_from_image
3788 || it->method == next_element_from_stretch)
3789 {
3790 /* The position etc with which we have to proceed are on
3791 the stack. The position may be at the end of a string,
3792 if the `display' property takes up the whole string. */
3793 pop_it (it);
3794 it->image_id = 0;
3795 if (STRINGP (it->string))
3796 {
3797 it->method = next_element_from_string;
3798 goto consider_string_end;
3799 }
3800 else
3801 it->method = next_element_from_buffer;
3802 }
3803 else
3804 /* There are no other methods defined, so this should be a bug. */
3805 abort ();
3806
3807 /* Reset flags indicating start and end of a sequence of
3808 characters with box. */
3809 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3810
3811 xassert (it->method != next_element_from_string
3812 || (STRINGP (it->string)
3813 && IT_STRING_CHARPOS (*it) >= 0));
3814 }
3815
3816
3817 /* Load IT's display element fields with information about the next
3818 display element which comes from a display table entry or from the
3819 result of translating a control character to one of the forms `^C'
3820 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3821
3822 static int
3823 next_element_from_display_vector (it)
3824 struct it *it;
3825 {
3826 /* Precondition. */
3827 xassert (it->dpvec && it->current.dpvec_index >= 0);
3828
3829 /* Remember the current face id in case glyphs specify faces.
3830 IT's face is restored in set_iterator_to_next. */
3831 it->saved_face_id = it->face_id;
3832
3833 if (INTEGERP (*it->dpvec)
3834 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3835 {
3836 int lface_id;
3837 GLYPH g;
3838
3839 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3840 it->c = FAST_GLYPH_CHAR (g);
3841 it->len = CHAR_BYTES (it->c);
3842
3843 /* The entry may contain a face id to use. Such a face id is
3844 the id of a Lisp face, not a realized face. A face id of
3845 zero means no face is specified. */
3846 lface_id = FAST_GLYPH_FACE (g);
3847 if (lface_id)
3848 {
3849 /* The function returns -1 if lface_id is invalid. */
3850 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3851 if (face_id >= 0)
3852 it->face_id = face_id;
3853 }
3854 }
3855 else
3856 /* Display table entry is invalid. Return a space. */
3857 it->c = ' ', it->len = 1;
3858
3859 /* Don't change position and object of the iterator here. They are
3860 still the values of the character that had this display table
3861 entry or was translated, and that's what we want. */
3862 it->what = IT_CHARACTER;
3863 return 1;
3864 }
3865
3866
3867 /* Load IT with the next display element from Lisp string IT->string.
3868 IT->current.string_pos is the current position within the string.
3869 If IT->current.overlay_string_index >= 0, the Lisp string is an
3870 overlay string. */
3871
3872 static int
3873 next_element_from_string (it)
3874 struct it *it;
3875 {
3876 struct text_pos position;
3877
3878 xassert (STRINGP (it->string));
3879 xassert (IT_STRING_CHARPOS (*it) >= 0);
3880 position = it->current.string_pos;
3881
3882 /* Time to check for invisible text? */
3883 if (IT_STRING_CHARPOS (*it) < it->end_charpos
3884 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
3885 {
3886 handle_stop (it);
3887
3888 /* Since a handler may have changed IT->method, we must
3889 recurse here. */
3890 return get_next_display_element (it);
3891 }
3892
3893 if (it->current.overlay_string_index >= 0)
3894 {
3895 /* Get the next character from an overlay string. In overlay
3896 strings, There is no field width or padding with spaces to
3897 do. */
3898 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3899 {
3900 it->what = IT_EOB;
3901 return 0;
3902 }
3903 else if (STRING_MULTIBYTE (it->string))
3904 {
3905 int remaining = (STRING_BYTES (XSTRING (it->string))
3906 - IT_STRING_BYTEPOS (*it));
3907 unsigned char *s = (XSTRING (it->string)->data
3908 + IT_STRING_BYTEPOS (*it));
3909 it->c = string_char_and_length (s, remaining, &it->len);
3910 }
3911 else
3912 {
3913 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3914 it->len = 1;
3915 }
3916 }
3917 else
3918 {
3919 /* Get the next character from a Lisp string that is not an
3920 overlay string. Such strings come from the mode line, for
3921 example. We may have to pad with spaces, or truncate the
3922 string. See also next_element_from_c_string. */
3923 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
3924 {
3925 it->what = IT_EOB;
3926 return 0;
3927 }
3928 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
3929 {
3930 /* Pad with spaces. */
3931 it->c = ' ', it->len = 1;
3932 CHARPOS (position) = BYTEPOS (position) = -1;
3933 }
3934 else if (STRING_MULTIBYTE (it->string))
3935 {
3936 int maxlen = (STRING_BYTES (XSTRING (it->string))
3937 - IT_STRING_BYTEPOS (*it));
3938 unsigned char *s = (XSTRING (it->string)->data
3939 + IT_STRING_BYTEPOS (*it));
3940 it->c = string_char_and_length (s, maxlen, &it->len);
3941 }
3942 else
3943 {
3944 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
3945 it->len = 1;
3946 }
3947 }
3948
3949 /* Record what we have and where it came from. Note that we store a
3950 buffer position in IT->position although it could arguably be a
3951 string position. */
3952 it->what = IT_CHARACTER;
3953 it->object = it->string;
3954 it->position = position;
3955 return 1;
3956 }
3957
3958
3959 /* Load IT with next display element from C string IT->s.
3960 IT->string_nchars is the maximum number of characters to return
3961 from the string. IT->end_charpos may be greater than
3962 IT->string_nchars when this function is called, in which case we
3963 may have to return padding spaces. Value is zero if end of string
3964 reached, including padding spaces. */
3965
3966 static int
3967 next_element_from_c_string (it)
3968 struct it *it;
3969 {
3970 int success_p = 1;
3971
3972 xassert (it->s);
3973 it->what = IT_CHARACTER;
3974 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
3975 it->object = Qnil;
3976
3977 /* IT's position can be greater IT->string_nchars in case a field
3978 width or precision has been specified when the iterator was
3979 initialized. */
3980 if (IT_CHARPOS (*it) >= it->end_charpos)
3981 {
3982 /* End of the game. */
3983 it->what = IT_EOB;
3984 success_p = 0;
3985 }
3986 else if (IT_CHARPOS (*it) >= it->string_nchars)
3987 {
3988 /* Pad with spaces. */
3989 it->c = ' ', it->len = 1;
3990 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
3991 }
3992 else if (it->multibyte_p)
3993 {
3994 /* Implementation note: The calls to strlen apparently aren't a
3995 performance problem because there is no noticeable performance
3996 difference between Emacs running in unibyte or multibyte mode. */
3997 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
3998 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
3999 maxlen, &it->len);
4000 }
4001 else
4002 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4003
4004 return success_p;
4005 }
4006
4007
4008 /* Set up IT to return characters from an ellipsis, if appropriate.
4009 The definition of the ellipsis glyphs may come from a display table
4010 entry. This function Fills IT with the first glyph from the
4011 ellipsis if an ellipsis is to be displayed. */
4012
4013 static int
4014 next_element_from_ellipsis (it)
4015 struct it *it;
4016 {
4017 if (it->selective_display_ellipsis_p)
4018 {
4019 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4020 {
4021 /* Use the display table definition for `...'. Invalid glyphs
4022 will be handled by the method returning elements from dpvec. */
4023 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4024 it->dpvec_char_len = it->len;
4025 it->dpvec = v->contents;
4026 it->dpend = v->contents + v->size;
4027 it->current.dpvec_index = 0;
4028 it->method = next_element_from_display_vector;
4029 }
4030 else
4031 {
4032 /* Use default `...' which is stored in default_invis_vector. */
4033 it->dpvec_char_len = it->len;
4034 it->dpvec = default_invis_vector;
4035 it->dpend = default_invis_vector + 3;
4036 it->current.dpvec_index = 0;
4037 it->method = next_element_from_display_vector;
4038 }
4039 }
4040 else
4041 reseat_at_next_visible_line_start (it, 1);
4042
4043 return get_next_display_element (it);
4044 }
4045
4046
4047 /* Deliver an image display element. The iterator IT is already
4048 filled with image information (done in handle_display_prop). Value
4049 is always 1. */
4050
4051
4052 static int
4053 next_element_from_image (it)
4054 struct it *it;
4055 {
4056 it->what = IT_IMAGE;
4057 return 1;
4058 }
4059
4060
4061 /* Fill iterator IT with next display element from a stretch glyph
4062 property. IT->object is the value of the text property. Value is
4063 always 1. */
4064
4065 static int
4066 next_element_from_stretch (it)
4067 struct it *it;
4068 {
4069 it->what = IT_STRETCH;
4070 return 1;
4071 }
4072
4073
4074 /* Load IT with the next display element from current_buffer. Value
4075 is zero if end of buffer reached. IT->stop_charpos is the next
4076 position at which to stop and check for text properties or buffer
4077 end. */
4078
4079 static int
4080 next_element_from_buffer (it)
4081 struct it *it;
4082 {
4083 int success_p = 1;
4084
4085 /* Check this assumption, otherwise, we would never enter the
4086 if-statement, below. */
4087 xassert (IT_CHARPOS (*it) >= BEGV
4088 && IT_CHARPOS (*it) <= it->stop_charpos);
4089
4090 if (IT_CHARPOS (*it) >= it->stop_charpos)
4091 {
4092 if (IT_CHARPOS (*it) >= it->end_charpos)
4093 {
4094 int overlay_strings_follow_p;
4095
4096 /* End of the game, except when overlay strings follow that
4097 haven't been returned yet. */
4098 if (it->overlay_strings_at_end_processed_p)
4099 overlay_strings_follow_p = 0;
4100 else
4101 {
4102 it->overlay_strings_at_end_processed_p = 1;
4103 overlay_strings_follow_p = get_overlay_strings (it);
4104 }
4105
4106 if (overlay_strings_follow_p)
4107 success_p = get_next_display_element (it);
4108 else
4109 {
4110 it->what = IT_EOB;
4111 it->position = it->current.pos;
4112 success_p = 0;
4113 }
4114 }
4115 else
4116 {
4117 handle_stop (it);
4118 return get_next_display_element (it);
4119 }
4120 }
4121 else
4122 {
4123 /* No face changes, overlays etc. in sight, so just return a
4124 character from current_buffer. */
4125 unsigned char *p;
4126
4127 /* Maybe run the redisplay end trigger hook. Performance note:
4128 This doesn't seem to cost measurable time. */
4129 if (it->redisplay_end_trigger_charpos
4130 && it->glyph_row
4131 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4132 run_redisplay_end_trigger_hook (it);
4133
4134 /* Get the next character, maybe multibyte. */
4135 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4136 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4137 {
4138 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4139 - IT_BYTEPOS (*it));
4140 it->c = string_char_and_length (p, maxlen, &it->len);
4141 }
4142 else
4143 it->c = *p, it->len = 1;
4144
4145 /* Record what we have and where it came from. */
4146 it->what = IT_CHARACTER;;
4147 it->object = it->w->buffer;
4148 it->position = it->current.pos;
4149
4150 /* Normally we return the character found above, except when we
4151 really want to return an ellipsis for selective display. */
4152 if (it->selective)
4153 {
4154 if (it->c == '\n')
4155 {
4156 /* A value of selective > 0 means hide lines indented more
4157 than that number of columns. */
4158 if (it->selective > 0
4159 && IT_CHARPOS (*it) + 1 < ZV
4160 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4161 IT_BYTEPOS (*it) + 1,
4162 it->selective))
4163 {
4164 success_p = next_element_from_ellipsis (it);
4165 it->dpvec_char_len = -1;
4166 }
4167 }
4168 else if (it->c == '\r' && it->selective == -1)
4169 {
4170 /* A value of selective == -1 means that everything from the
4171 CR to the end of the line is invisible, with maybe an
4172 ellipsis displayed for it. */
4173 success_p = next_element_from_ellipsis (it);
4174 it->dpvec_char_len = -1;
4175 }
4176 }
4177 }
4178
4179 /* Value is zero if end of buffer reached. */
4180 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4181 return success_p;
4182 }
4183
4184
4185 /* Run the redisplay end trigger hook for IT. */
4186
4187 static void
4188 run_redisplay_end_trigger_hook (it)
4189 struct it *it;
4190 {
4191 Lisp_Object args[3];
4192
4193 /* IT->glyph_row should be non-null, i.e. we should be actually
4194 displaying something, or otherwise we should not run the hook. */
4195 xassert (it->glyph_row);
4196
4197 /* Set up hook arguments. */
4198 args[0] = Qredisplay_end_trigger_functions;
4199 args[1] = it->window;
4200 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4201 it->redisplay_end_trigger_charpos = 0;
4202
4203 /* Since we are *trying* to run these functions, don't try to run
4204 them again, even if they get an error. */
4205 it->w->redisplay_end_trigger = Qnil;
4206 Frun_hook_with_args (3, args);
4207
4208 /* Notice if it changed the face of the character we are on. */
4209 handle_face_prop (it);
4210 }
4211
4212
4213 /* Deliver a composition display element. The iterator IT is already
4214 filled with composition information (done in
4215 handle_composition_prop). Value is always 1. */
4216
4217 static int
4218 next_element_from_composition (it)
4219 struct it *it;
4220 {
4221 it->what = IT_COMPOSITION;
4222 it->position = (STRINGP (it->string)
4223 ? it->current.string_pos
4224 : it->current.pos);
4225 return 1;
4226 }
4227
4228
4229 \f
4230 /***********************************************************************
4231 Moving an iterator without producing glyphs
4232 ***********************************************************************/
4233
4234 /* Move iterator IT to a specified buffer or X position within one
4235 line on the display without producing glyphs.
4236
4237 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4238 whichever is reached first.
4239
4240 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4241
4242 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4243 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4244 TO_X includes the amount by which a window is horizontally
4245 scrolled.
4246
4247 Value is
4248
4249 MOVE_POS_MATCH_OR_ZV
4250 - when TO_POS or ZV was reached.
4251
4252 MOVE_X_REACHED
4253 -when TO_X was reached before TO_POS or ZV were reached.
4254
4255 MOVE_LINE_CONTINUED
4256 - when we reached the end of the display area and the line must
4257 be continued.
4258
4259 MOVE_LINE_TRUNCATED
4260 - when we reached the end of the display area and the line is
4261 truncated.
4262
4263 MOVE_NEWLINE_OR_CR
4264 - when we stopped at a line end, i.e. a newline or a CR and selective
4265 display is on. */
4266
4267 static enum move_it_result
4268 move_it_in_display_line_to (it, to_charpos, to_x, op)
4269 struct it *it;
4270 int to_charpos, to_x, op;
4271 {
4272 enum move_it_result result = MOVE_UNDEFINED;
4273 struct glyph_row *saved_glyph_row;
4274
4275 /* Don't produce glyphs in produce_glyphs. */
4276 saved_glyph_row = it->glyph_row;
4277 it->glyph_row = NULL;
4278
4279 while (1)
4280 {
4281 int x, i, ascent, descent;
4282
4283 /* Stop when ZV or TO_CHARPOS reached. */
4284 if (!get_next_display_element (it)
4285 || ((op & MOVE_TO_POS) != 0
4286 && BUFFERP (it->object)
4287 && IT_CHARPOS (*it) >= to_charpos))
4288 {
4289 result = MOVE_POS_MATCH_OR_ZV;
4290 break;
4291 }
4292
4293 /* The call to produce_glyphs will get the metrics of the
4294 display element IT is loaded with. We record in x the
4295 x-position before this display element in case it does not
4296 fit on the line. */
4297 x = it->current_x;
4298
4299 /* Remember the line height so far in case the next element doesn't
4300 fit on the line. */
4301 if (!it->truncate_lines_p)
4302 {
4303 ascent = it->max_ascent;
4304 descent = it->max_descent;
4305 }
4306
4307 PRODUCE_GLYPHS (it);
4308
4309 if (it->area != TEXT_AREA)
4310 {
4311 set_iterator_to_next (it);
4312 continue;
4313 }
4314
4315 /* The number of glyphs we get back in IT->nglyphs will normally
4316 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4317 character on a terminal frame, or (iii) a line end. For the
4318 second case, IT->nglyphs - 1 padding glyphs will be present
4319 (on X frames, there is only one glyph produced for a
4320 composite character.
4321
4322 The behavior implemented below means, for continuation lines,
4323 that as many spaces of a TAB as fit on the current line are
4324 displayed there. For terminal frames, as many glyphs of a
4325 multi-glyph character are displayed in the current line, too.
4326 This is what the old redisplay code did, and we keep it that
4327 way. Under X, the whole shape of a complex character must
4328 fit on the line or it will be completely displayed in the
4329 next line.
4330
4331 Note that both for tabs and padding glyphs, all glyphs have
4332 the same width. */
4333 if (it->nglyphs)
4334 {
4335 /* More than one glyph or glyph doesn't fit on line. All
4336 glyphs have the same width. */
4337 int single_glyph_width = it->pixel_width / it->nglyphs;
4338 int new_x;
4339
4340 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4341 {
4342 new_x = x + single_glyph_width;
4343
4344 /* We want to leave anything reaching TO_X to the caller. */
4345 if ((op & MOVE_TO_X) && new_x > to_x)
4346 {
4347 it->current_x = x;
4348 result = MOVE_X_REACHED;
4349 break;
4350 }
4351 else if (/* Lines are continued. */
4352 !it->truncate_lines_p
4353 && (/* And glyph doesn't fit on the line. */
4354 new_x > it->last_visible_x
4355 /* Or it fits exactly and we're on a window
4356 system frame. */
4357 || (new_x == it->last_visible_x
4358 && FRAME_WINDOW_P (it->f))))
4359 {
4360 if (/* IT->hpos == 0 means the very first glyph
4361 doesn't fit on the line, e.g. a wide image. */
4362 it->hpos == 0
4363 || (new_x == it->last_visible_x
4364 && FRAME_WINDOW_P (it->f)))
4365 {
4366 ++it->hpos;
4367 it->current_x = new_x;
4368 if (i == it->nglyphs - 1)
4369 set_iterator_to_next (it);
4370 }
4371 else
4372 {
4373 it->current_x = x;
4374 it->max_ascent = ascent;
4375 it->max_descent = descent;
4376 }
4377
4378 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4379 IT_CHARPOS (*it)));
4380 result = MOVE_LINE_CONTINUED;
4381 break;
4382 }
4383 else if (new_x > it->first_visible_x)
4384 {
4385 /* Glyph is visible. Increment number of glyphs that
4386 would be displayed. */
4387 ++it->hpos;
4388 }
4389 else
4390 {
4391 /* Glyph is completely off the left margin of the display
4392 area. Nothing to do. */
4393 }
4394 }
4395
4396 if (result != MOVE_UNDEFINED)
4397 break;
4398 }
4399 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4400 {
4401 /* Stop when TO_X specified and reached. This check is
4402 necessary here because of lines consisting of a line end,
4403 only. The line end will not produce any glyphs and we
4404 would never get MOVE_X_REACHED. */
4405 xassert (it->nglyphs == 0);
4406 result = MOVE_X_REACHED;
4407 break;
4408 }
4409
4410 /* Is this a line end? If yes, we're done. */
4411 if (ITERATOR_AT_END_OF_LINE_P (it))
4412 {
4413 result = MOVE_NEWLINE_OR_CR;
4414 break;
4415 }
4416
4417 /* The current display element has been consumed. Advance
4418 to the next. */
4419 set_iterator_to_next (it);
4420
4421 /* Stop if lines are truncated and IT's current x-position is
4422 past the right edge of the window now. */
4423 if (it->truncate_lines_p
4424 && it->current_x >= it->last_visible_x)
4425 {
4426 result = MOVE_LINE_TRUNCATED;
4427 break;
4428 }
4429 }
4430
4431 /* Restore the iterator settings altered at the beginning of this
4432 function. */
4433 it->glyph_row = saved_glyph_row;
4434 return result;
4435 }
4436
4437
4438 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4439 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4440 the description of enum move_operation_enum.
4441
4442 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4443 screen line, this function will set IT to the next position >
4444 TO_CHARPOS. */
4445
4446 void
4447 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4448 struct it *it;
4449 int to_charpos, to_x, to_y, to_vpos;
4450 int op;
4451 {
4452 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4453 int line_height;
4454 int reached = 0;
4455
4456 for (;;)
4457 {
4458 if (op & MOVE_TO_VPOS)
4459 {
4460 /* If no TO_CHARPOS and no TO_X specified, stop at the
4461 start of the line TO_VPOS. */
4462 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4463 {
4464 if (it->vpos == to_vpos)
4465 {
4466 reached = 1;
4467 break;
4468 }
4469 else
4470 skip = move_it_in_display_line_to (it, -1, -1, 0);
4471 }
4472 else
4473 {
4474 /* TO_VPOS >= 0 means stop at TO_X in the line at
4475 TO_VPOS, or at TO_POS, whichever comes first. */
4476 if (it->vpos == to_vpos)
4477 {
4478 reached = 2;
4479 break;
4480 }
4481
4482 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4483
4484 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4485 {
4486 reached = 3;
4487 break;
4488 }
4489 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4490 {
4491 /* We have reached TO_X but not in the line we want. */
4492 skip = move_it_in_display_line_to (it, to_charpos,
4493 -1, MOVE_TO_POS);
4494 if (skip == MOVE_POS_MATCH_OR_ZV)
4495 {
4496 reached = 4;
4497 break;
4498 }
4499 }
4500 }
4501 }
4502 else if (op & MOVE_TO_Y)
4503 {
4504 struct it it_backup;
4505
4506 /* TO_Y specified means stop at TO_X in the line containing
4507 TO_Y---or at TO_CHARPOS if this is reached first. The
4508 problem is that we can't really tell whether the line
4509 contains TO_Y before we have completely scanned it, and
4510 this may skip past TO_X. What we do is to first scan to
4511 TO_X.
4512
4513 If TO_X is not specified, use a TO_X of zero. The reason
4514 is to make the outcome of this function more predictable.
4515 If we didn't use TO_X == 0, we would stop at the end of
4516 the line which is probably not what a caller would expect
4517 to happen. */
4518 skip = move_it_in_display_line_to (it, to_charpos,
4519 ((op & MOVE_TO_X)
4520 ? to_x : 0),
4521 (MOVE_TO_X
4522 | (op & MOVE_TO_POS)));
4523
4524 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4525 if (skip == MOVE_POS_MATCH_OR_ZV)
4526 {
4527 reached = 5;
4528 break;
4529 }
4530
4531 /* If TO_X was reached, we would like to know whether TO_Y
4532 is in the line. This can only be said if we know the
4533 total line height which requires us to scan the rest of
4534 the line. */
4535 if (skip == MOVE_X_REACHED)
4536 {
4537 it_backup = *it;
4538 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4539 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4540 op & MOVE_TO_POS);
4541 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4542 }
4543
4544 /* Now, decide whether TO_Y is in this line. */
4545 line_height = it->max_ascent + it->max_descent;
4546 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4547
4548 if (to_y >= it->current_y
4549 && to_y < it->current_y + line_height)
4550 {
4551 if (skip == MOVE_X_REACHED)
4552 /* If TO_Y is in this line and TO_X was reached above,
4553 we scanned too far. We have to restore IT's settings
4554 to the ones before skipping. */
4555 *it = it_backup;
4556 reached = 6;
4557 }
4558 else if (skip == MOVE_X_REACHED)
4559 {
4560 skip = skip2;
4561 if (skip == MOVE_POS_MATCH_OR_ZV)
4562 reached = 7;
4563 }
4564
4565 if (reached)
4566 break;
4567 }
4568 else
4569 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4570
4571 switch (skip)
4572 {
4573 case MOVE_POS_MATCH_OR_ZV:
4574 reached = 8;
4575 goto out;
4576
4577 case MOVE_NEWLINE_OR_CR:
4578 set_iterator_to_next (it);
4579 it->continuation_lines_width = 0;
4580 break;
4581
4582 case MOVE_LINE_TRUNCATED:
4583 it->continuation_lines_width = 0;
4584 reseat_at_next_visible_line_start (it, 0);
4585 if ((op & MOVE_TO_POS) != 0
4586 && IT_CHARPOS (*it) > to_charpos)
4587 {
4588 reached = 9;
4589 goto out;
4590 }
4591 break;
4592
4593 case MOVE_LINE_CONTINUED:
4594 it->continuation_lines_width += it->current_x;
4595 break;
4596
4597 default:
4598 abort ();
4599 }
4600
4601 /* Reset/increment for the next run. */
4602 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4603 it->current_x = it->hpos = 0;
4604 it->current_y += it->max_ascent + it->max_descent;
4605 ++it->vpos;
4606 last_height = it->max_ascent + it->max_descent;
4607 last_max_ascent = it->max_ascent;
4608 it->max_ascent = it->max_descent = 0;
4609 }
4610
4611 out:
4612
4613 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4614 }
4615
4616
4617 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4618
4619 If DY > 0, move IT backward at least that many pixels. DY = 0
4620 means move IT backward to the preceding line start or BEGV. This
4621 function may move over more than DY pixels if IT->current_y - DY
4622 ends up in the middle of a line; in this case IT->current_y will be
4623 set to the top of the line moved to. */
4624
4625 void
4626 move_it_vertically_backward (it, dy)
4627 struct it *it;
4628 int dy;
4629 {
4630 int nlines, h, line_height;
4631 struct it it2;
4632 int start_pos = IT_CHARPOS (*it);
4633
4634 xassert (dy >= 0);
4635
4636 /* Estimate how many newlines we must move back. */
4637 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4638
4639 /* Set the iterator's position that many lines back. */
4640 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4641 back_to_previous_visible_line_start (it);
4642
4643 /* Reseat the iterator here. When moving backward, we don't want
4644 reseat to skip forward over invisible text, set up the iterator
4645 to deliver from overlay strings at the new position etc. So,
4646 use reseat_1 here. */
4647 reseat_1 (it, it->current.pos, 1);
4648
4649 /* We are now surely at a line start. */
4650 it->current_x = it->hpos = 0;
4651
4652 /* Move forward and see what y-distance we moved. First move to the
4653 start of the next line so that we get its height. We need this
4654 height to be able to tell whether we reached the specified
4655 y-distance. */
4656 it2 = *it;
4657 it2.max_ascent = it2.max_descent = 0;
4658 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4659 MOVE_TO_POS | MOVE_TO_VPOS);
4660 xassert (IT_CHARPOS (*it) >= BEGV);
4661 line_height = it2.max_ascent + it2.max_descent;
4662
4663 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4664 xassert (IT_CHARPOS (*it) >= BEGV);
4665 h = it2.current_y - it->current_y;
4666 nlines = it2.vpos - it->vpos;
4667
4668 /* Correct IT's y and vpos position. */
4669 it->vpos -= nlines;
4670 it->current_y -= h;
4671
4672 if (dy == 0)
4673 {
4674 /* DY == 0 means move to the start of the screen line. The
4675 value of nlines is > 0 if continuation lines were involved. */
4676 if (nlines > 0)
4677 move_it_by_lines (it, nlines, 1);
4678 xassert (IT_CHARPOS (*it) <= start_pos);
4679 }
4680 else if (nlines)
4681 {
4682 /* The y-position we try to reach. Note that h has been
4683 subtracted in front of the if-statement. */
4684 int target_y = it->current_y + h - dy;
4685
4686 /* If we did not reach target_y, try to move further backward if
4687 we can. If we moved too far backward, try to move forward. */
4688 if (target_y < it->current_y
4689 && IT_CHARPOS (*it) > BEGV)
4690 {
4691 move_it_vertically (it, target_y - it->current_y);
4692 xassert (IT_CHARPOS (*it) >= BEGV);
4693 }
4694 else if (target_y >= it->current_y + line_height
4695 && IT_CHARPOS (*it) < ZV)
4696 {
4697 move_it_vertically (it, target_y - (it->current_y + line_height));
4698 xassert (IT_CHARPOS (*it) >= BEGV);
4699 }
4700 }
4701 }
4702
4703
4704 /* Move IT by a specified amount of pixel lines DY. DY negative means
4705 move backwards. DY = 0 means move to start of screen line. At the
4706 end, IT will be on the start of a screen line. */
4707
4708 void
4709 move_it_vertically (it, dy)
4710 struct it *it;
4711 int dy;
4712 {
4713 if (dy <= 0)
4714 move_it_vertically_backward (it, -dy);
4715 else if (dy > 0)
4716 {
4717 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4718 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4719 MOVE_TO_POS | MOVE_TO_Y);
4720 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4721
4722 /* If buffer ends in ZV without a newline, move to the start of
4723 the line to satisfy the post-condition. */
4724 if (IT_CHARPOS (*it) == ZV
4725 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4726 move_it_by_lines (it, 0, 0);
4727 }
4728 }
4729
4730
4731 /* Return non-zero if some text between buffer positions START_CHARPOS
4732 and END_CHARPOS is invisible. IT->window is the window for text
4733 property lookup. */
4734
4735 static int
4736 invisible_text_between_p (it, start_charpos, end_charpos)
4737 struct it *it;
4738 int start_charpos, end_charpos;
4739 {
4740 Lisp_Object prop, limit;
4741 int invisible_found_p;
4742
4743 xassert (it != NULL && start_charpos <= end_charpos);
4744
4745 /* Is text at START invisible? */
4746 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4747 it->window);
4748 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4749 invisible_found_p = 1;
4750 else
4751 {
4752 limit = Fnext_single_char_property_change (make_number (start_charpos),
4753 Qinvisible, Qnil,
4754 make_number (end_charpos));
4755 invisible_found_p = XFASTINT (limit) < end_charpos;
4756 }
4757
4758 return invisible_found_p;
4759 }
4760
4761
4762 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4763 negative means move up. DVPOS == 0 means move to the start of the
4764 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4765 NEED_Y_P is zero, IT->current_y will be left unchanged.
4766
4767 Further optimization ideas: If we would know that IT->f doesn't use
4768 a face with proportional font, we could be faster for
4769 truncate-lines nil. */
4770
4771 void
4772 move_it_by_lines (it, dvpos, need_y_p)
4773 struct it *it;
4774 int dvpos, need_y_p;
4775 {
4776 struct position pos;
4777
4778 if (!FRAME_WINDOW_P (it->f))
4779 {
4780 struct text_pos textpos;
4781
4782 /* We can use vmotion on frames without proportional fonts. */
4783 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4784 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4785 reseat (it, textpos, 1);
4786 it->vpos += pos.vpos;
4787 it->current_y += pos.vpos;
4788 }
4789 else if (dvpos == 0)
4790 {
4791 /* DVPOS == 0 means move to the start of the screen line. */
4792 move_it_vertically_backward (it, 0);
4793 xassert (it->current_x == 0 && it->hpos == 0);
4794 }
4795 else if (dvpos > 0)
4796 {
4797 /* If there are no continuation lines, and if there is no
4798 selective display, try the simple method of moving forward
4799 DVPOS newlines, then see where we are. */
4800 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4801 {
4802 int shortage = 0, charpos;
4803
4804 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4805 charpos = IT_CHARPOS (*it) + 1;
4806 else
4807 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4808 &shortage, 0);
4809
4810 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4811 {
4812 struct text_pos pos;
4813 CHARPOS (pos) = charpos;
4814 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4815 reseat (it, pos, 1);
4816 it->vpos += dvpos - shortage;
4817 it->hpos = it->current_x = 0;
4818 return;
4819 }
4820 }
4821
4822 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4823 }
4824 else
4825 {
4826 struct it it2;
4827 int start_charpos, i;
4828
4829 /* If there are no continuation lines, and if there is no
4830 selective display, try the simple method of moving backward
4831 -DVPOS newlines. */
4832 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4833 {
4834 int shortage;
4835 int charpos = IT_CHARPOS (*it);
4836 int bytepos = IT_BYTEPOS (*it);
4837
4838 /* If in the middle of a line, go to its start. */
4839 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4840 {
4841 charpos = find_next_newline_no_quit (charpos, -1);
4842 bytepos = CHAR_TO_BYTE (charpos);
4843 }
4844
4845 if (charpos == BEGV)
4846 {
4847 struct text_pos pos;
4848 CHARPOS (pos) = charpos;
4849 BYTEPOS (pos) = bytepos;
4850 reseat (it, pos, 1);
4851 it->hpos = it->current_x = 0;
4852 return;
4853 }
4854 else
4855 {
4856 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4857 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4858 {
4859 struct text_pos pos;
4860 CHARPOS (pos) = charpos;
4861 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4862 reseat (it, pos, 1);
4863 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4864 it->hpos = it->current_x = 0;
4865 return;
4866 }
4867 }
4868 }
4869
4870 /* Go back -DVPOS visible lines and reseat the iterator there. */
4871 start_charpos = IT_CHARPOS (*it);
4872 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4873 back_to_previous_visible_line_start (it);
4874 reseat (it, it->current.pos, 1);
4875 it->current_x = it->hpos = 0;
4876
4877 /* Above call may have moved too far if continuation lines
4878 are involved. Scan forward and see if it did. */
4879 it2 = *it;
4880 it2.vpos = it2.current_y = 0;
4881 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
4882 it->vpos -= it2.vpos;
4883 it->current_y -= it2.current_y;
4884 it->current_x = it->hpos = 0;
4885
4886 /* If we moved too far, move IT some lines forward. */
4887 if (it2.vpos > -dvpos)
4888 {
4889 int delta = it2.vpos + dvpos;
4890 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
4891 }
4892 }
4893 }
4894
4895
4896 \f
4897 /***********************************************************************
4898 Messages
4899 ***********************************************************************/
4900
4901
4902 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
4903 to *Messages*. */
4904
4905 void
4906 add_to_log (format, arg1, arg2)
4907 char *format;
4908 Lisp_Object arg1, arg2;
4909 {
4910 Lisp_Object args[3];
4911 Lisp_Object msg, fmt;
4912 char *buffer;
4913 int len;
4914 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4915
4916 fmt = msg = Qnil;
4917 GCPRO4 (fmt, msg, arg1, arg2);
4918
4919 args[0] = fmt = build_string (format);
4920 args[1] = arg1;
4921 args[2] = arg2;
4922 msg = Fformat (3, args);
4923
4924 len = STRING_BYTES (XSTRING (msg)) + 1;
4925 buffer = (char *) alloca (len);
4926 strcpy (buffer, XSTRING (msg)->data);
4927
4928 message_dolog (buffer, len - 1, 1, 0);
4929 UNGCPRO;
4930 }
4931
4932
4933 /* Output a newline in the *Messages* buffer if "needs" one. */
4934
4935 void
4936 message_log_maybe_newline ()
4937 {
4938 if (message_log_need_newline)
4939 message_dolog ("", 0, 1, 0);
4940 }
4941
4942
4943 /* Add a string M of length LEN to the message log, optionally
4944 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
4945 nonzero, means interpret the contents of M as multibyte. This
4946 function calls low-level routines in order to bypass text property
4947 hooks, etc. which might not be safe to run. */
4948
4949 void
4950 message_dolog (m, len, nlflag, multibyte)
4951 char *m;
4952 int len, nlflag, multibyte;
4953 {
4954 if (!NILP (Vmessage_log_max))
4955 {
4956 struct buffer *oldbuf;
4957 Lisp_Object oldpoint, oldbegv, oldzv;
4958 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4959 int point_at_end = 0;
4960 int zv_at_end = 0;
4961 Lisp_Object old_deactivate_mark, tem;
4962 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4963
4964 old_deactivate_mark = Vdeactivate_mark;
4965 oldbuf = current_buffer;
4966 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
4967 current_buffer->undo_list = Qt;
4968
4969 oldpoint = Fpoint_marker ();
4970 oldbegv = Fpoint_min_marker ();
4971 oldzv = Fpoint_max_marker ();
4972 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
4973
4974 if (PT == Z)
4975 point_at_end = 1;
4976 if (ZV == Z)
4977 zv_at_end = 1;
4978
4979 BEGV = BEG;
4980 BEGV_BYTE = BEG_BYTE;
4981 ZV = Z;
4982 ZV_BYTE = Z_BYTE;
4983 TEMP_SET_PT_BOTH (Z, Z_BYTE);
4984
4985 /* Insert the string--maybe converting multibyte to single byte
4986 or vice versa, so that all the text fits the buffer. */
4987 if (multibyte
4988 && NILP (current_buffer->enable_multibyte_characters))
4989 {
4990 int i, c, nbytes;
4991 unsigned char work[1];
4992
4993 /* Convert a multibyte string to single-byte
4994 for the *Message* buffer. */
4995 for (i = 0; i < len; i += nbytes)
4996 {
4997 c = string_char_and_length (m + i, len - i, &nbytes);
4998 work[0] = (SINGLE_BYTE_CHAR_P (c)
4999 ? c
5000 : multibyte_char_to_unibyte (c, Qnil));
5001 insert_1_both (work, 1, 1, 1, 0, 0);
5002 }
5003 }
5004 else if (! multibyte
5005 && ! NILP (current_buffer->enable_multibyte_characters))
5006 {
5007 int i, c, nbytes;
5008 unsigned char *msg = (unsigned char *) m;
5009 unsigned char str[MAX_MULTIBYTE_LENGTH];
5010 /* Convert a single-byte string to multibyte
5011 for the *Message* buffer. */
5012 for (i = 0; i < len; i++)
5013 {
5014 c = unibyte_char_to_multibyte (msg[i]);
5015 nbytes = CHAR_STRING (c, str);
5016 insert_1_both (str, 1, nbytes, 1, 0, 0);
5017 }
5018 }
5019 else if (len)
5020 insert_1 (m, len, 1, 0, 0);
5021
5022 if (nlflag)
5023 {
5024 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5025 insert_1 ("\n", 1, 1, 0, 0);
5026
5027 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5028 this_bol = PT;
5029 this_bol_byte = PT_BYTE;
5030
5031 if (this_bol > BEG)
5032 {
5033 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5034 prev_bol = PT;
5035 prev_bol_byte = PT_BYTE;
5036
5037 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5038 this_bol, this_bol_byte);
5039 if (dup)
5040 {
5041 del_range_both (prev_bol, prev_bol_byte,
5042 this_bol, this_bol_byte, 0);
5043 if (dup > 1)
5044 {
5045 char dupstr[40];
5046 int duplen;
5047
5048 /* If you change this format, don't forget to also
5049 change message_log_check_duplicate. */
5050 sprintf (dupstr, " [%d times]", dup);
5051 duplen = strlen (dupstr);
5052 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5053 insert_1 (dupstr, duplen, 1, 0, 1);
5054 }
5055 }
5056 }
5057
5058 if (NATNUMP (Vmessage_log_max))
5059 {
5060 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5061 -XFASTINT (Vmessage_log_max) - 1, 0);
5062 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5063 }
5064 }
5065 BEGV = XMARKER (oldbegv)->charpos;
5066 BEGV_BYTE = marker_byte_position (oldbegv);
5067
5068 if (zv_at_end)
5069 {
5070 ZV = Z;
5071 ZV_BYTE = Z_BYTE;
5072 }
5073 else
5074 {
5075 ZV = XMARKER (oldzv)->charpos;
5076 ZV_BYTE = marker_byte_position (oldzv);
5077 }
5078
5079 if (point_at_end)
5080 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5081 else
5082 /* We can't do Fgoto_char (oldpoint) because it will run some
5083 Lisp code. */
5084 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5085 XMARKER (oldpoint)->bytepos);
5086
5087 UNGCPRO;
5088 free_marker (oldpoint);
5089 free_marker (oldbegv);
5090 free_marker (oldzv);
5091
5092 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5093 set_buffer_internal (oldbuf);
5094 if (NILP (tem))
5095 windows_or_buffers_changed = old_windows_or_buffers_changed;
5096 message_log_need_newline = !nlflag;
5097 Vdeactivate_mark = old_deactivate_mark;
5098 }
5099 }
5100
5101
5102 /* We are at the end of the buffer after just having inserted a newline.
5103 (Note: We depend on the fact we won't be crossing the gap.)
5104 Check to see if the most recent message looks a lot like the previous one.
5105 Return 0 if different, 1 if the new one should just replace it, or a
5106 value N > 1 if we should also append " [N times]". */
5107
5108 static int
5109 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5110 int prev_bol, this_bol;
5111 int prev_bol_byte, this_bol_byte;
5112 {
5113 int i;
5114 int len = Z_BYTE - 1 - this_bol_byte;
5115 int seen_dots = 0;
5116 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5117 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5118
5119 for (i = 0; i < len; i++)
5120 {
5121 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5122 && p1[i] != '\n')
5123 seen_dots = 1;
5124 if (p1[i] != p2[i])
5125 return seen_dots;
5126 }
5127 p1 += len;
5128 if (*p1 == '\n')
5129 return 2;
5130 if (*p1++ == ' ' && *p1++ == '[')
5131 {
5132 int n = 0;
5133 while (*p1 >= '0' && *p1 <= '9')
5134 n = n * 10 + *p1++ - '0';
5135 if (strncmp (p1, " times]\n", 8) == 0)
5136 return n+1;
5137 }
5138 return 0;
5139 }
5140
5141
5142 /* Display an echo area message M with a specified length of LEN
5143 chars. The string may include null characters. If M is 0, clear
5144 out any existing message, and let the mini-buffer text show through.
5145
5146 The buffer M must continue to exist until after the echo area gets
5147 cleared or some other message gets displayed there. This means do
5148 not pass text that is stored in a Lisp string; do not pass text in
5149 a buffer that was alloca'd. */
5150
5151 void
5152 message2 (m, len, multibyte)
5153 char *m;
5154 int len;
5155 int multibyte;
5156 {
5157 /* First flush out any partial line written with print. */
5158 message_log_maybe_newline ();
5159 if (m)
5160 message_dolog (m, len, 1, multibyte);
5161 message2_nolog (m, len, multibyte);
5162 }
5163
5164
5165 /* The non-logging counterpart of message2. */
5166
5167 void
5168 message2_nolog (m, len, multibyte)
5169 char *m;
5170 int len;
5171 {
5172 struct frame *sf = SELECTED_FRAME ();
5173 message_enable_multibyte = multibyte;
5174
5175 if (noninteractive)
5176 {
5177 if (noninteractive_need_newline)
5178 putc ('\n', stderr);
5179 noninteractive_need_newline = 0;
5180 if (m)
5181 fwrite (m, len, 1, stderr);
5182 if (cursor_in_echo_area == 0)
5183 fprintf (stderr, "\n");
5184 fflush (stderr);
5185 }
5186 /* A null message buffer means that the frame hasn't really been
5187 initialized yet. Error messages get reported properly by
5188 cmd_error, so this must be just an informative message; toss it. */
5189 else if (INTERACTIVE
5190 && sf->glyphs_initialized_p
5191 && FRAME_MESSAGE_BUF (sf))
5192 {
5193 Lisp_Object mini_window;
5194 struct frame *f;
5195
5196 /* Get the frame containing the mini-buffer
5197 that the selected frame is using. */
5198 mini_window = FRAME_MINIBUF_WINDOW (sf);
5199 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5200
5201 FRAME_SAMPLE_VISIBILITY (f);
5202 if (FRAME_VISIBLE_P (sf)
5203 && ! FRAME_VISIBLE_P (f))
5204 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5205
5206 if (m)
5207 {
5208 set_message (m, Qnil, len, multibyte);
5209 if (minibuffer_auto_raise)
5210 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5211 }
5212 else
5213 clear_message (1, 1);
5214
5215 do_pending_window_change (0);
5216 echo_area_display (1);
5217 do_pending_window_change (0);
5218 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5219 (*frame_up_to_date_hook) (f);
5220 }
5221 }
5222
5223
5224 /* Display an echo area message M with a specified length of NBYTES
5225 bytes. The string may include null characters. If M is not a
5226 string, clear out any existing message, and let the mini-buffer
5227 text show through. */
5228
5229 void
5230 message3 (m, nbytes, multibyte)
5231 Lisp_Object m;
5232 int nbytes;
5233 int multibyte;
5234 {
5235 struct gcpro gcpro1;
5236
5237 GCPRO1 (m);
5238
5239 /* First flush out any partial line written with print. */
5240 message_log_maybe_newline ();
5241 if (STRINGP (m))
5242 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5243 message3_nolog (m, nbytes, multibyte);
5244
5245 UNGCPRO;
5246 }
5247
5248
5249 /* The non-logging version of message3. */
5250
5251 void
5252 message3_nolog (m, nbytes, multibyte)
5253 Lisp_Object m;
5254 int nbytes, multibyte;
5255 {
5256 struct frame *sf = SELECTED_FRAME ();
5257 message_enable_multibyte = multibyte;
5258
5259 if (noninteractive)
5260 {
5261 if (noninteractive_need_newline)
5262 putc ('\n', stderr);
5263 noninteractive_need_newline = 0;
5264 if (STRINGP (m))
5265 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5266 if (cursor_in_echo_area == 0)
5267 fprintf (stderr, "\n");
5268 fflush (stderr);
5269 }
5270 /* A null message buffer means that the frame hasn't really been
5271 initialized yet. Error messages get reported properly by
5272 cmd_error, so this must be just an informative message; toss it. */
5273 else if (INTERACTIVE
5274 && sf->glyphs_initialized_p
5275 && FRAME_MESSAGE_BUF (sf))
5276 {
5277 Lisp_Object mini_window;
5278 Lisp_Object frame;
5279 struct frame *f;
5280
5281 /* Get the frame containing the mini-buffer
5282 that the selected frame is using. */
5283 mini_window = FRAME_MINIBUF_WINDOW (sf);
5284 frame = XWINDOW (mini_window)->frame;
5285 f = XFRAME (frame);
5286
5287 FRAME_SAMPLE_VISIBILITY (f);
5288 if (FRAME_VISIBLE_P (sf)
5289 && !FRAME_VISIBLE_P (f))
5290 Fmake_frame_visible (frame);
5291
5292 if (STRINGP (m) && XSTRING (m)->size)
5293 {
5294 set_message (NULL, m, nbytes, multibyte);
5295 if (minibuffer_auto_raise)
5296 Fraise_frame (frame);
5297 }
5298 else
5299 clear_message (1, 1);
5300
5301 do_pending_window_change (0);
5302 echo_area_display (1);
5303 do_pending_window_change (0);
5304 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5305 (*frame_up_to_date_hook) (f);
5306 }
5307 }
5308
5309
5310 /* Display a null-terminated echo area message M. If M is 0, clear
5311 out any existing message, and let the mini-buffer text show through.
5312
5313 The buffer M must continue to exist until after the echo area gets
5314 cleared or some other message gets displayed there. Do not pass
5315 text that is stored in a Lisp string. Do not pass text in a buffer
5316 that was alloca'd. */
5317
5318 void
5319 message1 (m)
5320 char *m;
5321 {
5322 message2 (m, (m ? strlen (m) : 0), 0);
5323 }
5324
5325
5326 /* The non-logging counterpart of message1. */
5327
5328 void
5329 message1_nolog (m)
5330 char *m;
5331 {
5332 message2_nolog (m, (m ? strlen (m) : 0), 0);
5333 }
5334
5335 /* Display a message M which contains a single %s
5336 which gets replaced with STRING. */
5337
5338 void
5339 message_with_string (m, string, log)
5340 char *m;
5341 Lisp_Object string;
5342 int log;
5343 {
5344 if (noninteractive)
5345 {
5346 if (m)
5347 {
5348 if (noninteractive_need_newline)
5349 putc ('\n', stderr);
5350 noninteractive_need_newline = 0;
5351 fprintf (stderr, m, XSTRING (string)->data);
5352 if (cursor_in_echo_area == 0)
5353 fprintf (stderr, "\n");
5354 fflush (stderr);
5355 }
5356 }
5357 else if (INTERACTIVE)
5358 {
5359 /* The frame whose minibuffer we're going to display the message on.
5360 It may be larger than the selected frame, so we need
5361 to use its buffer, not the selected frame's buffer. */
5362 Lisp_Object mini_window;
5363 struct frame *f, *sf = SELECTED_FRAME ();
5364
5365 /* Get the frame containing the minibuffer
5366 that the selected frame is using. */
5367 mini_window = FRAME_MINIBUF_WINDOW (sf);
5368 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5369
5370 /* A null message buffer means that the frame hasn't really been
5371 initialized yet. Error messages get reported properly by
5372 cmd_error, so this must be just an informative message; toss it. */
5373 if (FRAME_MESSAGE_BUF (f))
5374 {
5375 int len;
5376 char *a[1];
5377 a[0] = (char *) XSTRING (string)->data;
5378
5379 len = doprnt (FRAME_MESSAGE_BUF (f),
5380 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5381
5382 if (log)
5383 message2 (FRAME_MESSAGE_BUF (f), len,
5384 STRING_MULTIBYTE (string));
5385 else
5386 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5387 STRING_MULTIBYTE (string));
5388
5389 /* Print should start at the beginning of the message
5390 buffer next time. */
5391 message_buf_print = 0;
5392 }
5393 }
5394 }
5395
5396
5397 /* Dump an informative message to the minibuf. If M is 0, clear out
5398 any existing message, and let the mini-buffer text show through. */
5399
5400 /* VARARGS 1 */
5401 void
5402 message (m, a1, a2, a3)
5403 char *m;
5404 EMACS_INT a1, a2, a3;
5405 {
5406 if (noninteractive)
5407 {
5408 if (m)
5409 {
5410 if (noninteractive_need_newline)
5411 putc ('\n', stderr);
5412 noninteractive_need_newline = 0;
5413 fprintf (stderr, m, a1, a2, a3);
5414 if (cursor_in_echo_area == 0)
5415 fprintf (stderr, "\n");
5416 fflush (stderr);
5417 }
5418 }
5419 else if (INTERACTIVE)
5420 {
5421 /* The frame whose mini-buffer we're going to display the message
5422 on. It may be larger than the selected frame, so we need to
5423 use its buffer, not the selected frame's buffer. */
5424 Lisp_Object mini_window;
5425 struct frame *f, *sf = SELECTED_FRAME ();
5426
5427 /* Get the frame containing the mini-buffer
5428 that the selected frame is using. */
5429 mini_window = FRAME_MINIBUF_WINDOW (sf);
5430 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5431
5432 /* A null message buffer means that the frame hasn't really been
5433 initialized yet. Error messages get reported properly by
5434 cmd_error, so this must be just an informative message; toss
5435 it. */
5436 if (FRAME_MESSAGE_BUF (f))
5437 {
5438 if (m)
5439 {
5440 int len;
5441 #ifdef NO_ARG_ARRAY
5442 char *a[3];
5443 a[0] = (char *) a1;
5444 a[1] = (char *) a2;
5445 a[2] = (char *) a3;
5446
5447 len = doprnt (FRAME_MESSAGE_BUF (f),
5448 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5449 #else
5450 len = doprnt (FRAME_MESSAGE_BUF (f),
5451 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5452 (char **) &a1);
5453 #endif /* NO_ARG_ARRAY */
5454
5455 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5456 }
5457 else
5458 message1 (0);
5459
5460 /* Print should start at the beginning of the message
5461 buffer next time. */
5462 message_buf_print = 0;
5463 }
5464 }
5465 }
5466
5467
5468 /* The non-logging version of message. */
5469
5470 void
5471 message_nolog (m, a1, a2, a3)
5472 char *m;
5473 EMACS_INT a1, a2, a3;
5474 {
5475 Lisp_Object old_log_max;
5476 old_log_max = Vmessage_log_max;
5477 Vmessage_log_max = Qnil;
5478 message (m, a1, a2, a3);
5479 Vmessage_log_max = old_log_max;
5480 }
5481
5482
5483 /* Display the current message in the current mini-buffer. This is
5484 only called from error handlers in process.c, and is not time
5485 critical. */
5486
5487 void
5488 update_echo_area ()
5489 {
5490 if (!NILP (echo_area_buffer[0]))
5491 {
5492 Lisp_Object string;
5493 string = Fcurrent_message ();
5494 message3 (string, XSTRING (string)->size,
5495 !NILP (current_buffer->enable_multibyte_characters));
5496 }
5497 }
5498
5499
5500 /* Make sure echo area buffers in echo_buffers[] are life. If they
5501 aren't, make new ones. */
5502
5503 static void
5504 ensure_echo_area_buffers ()
5505 {
5506 int i;
5507
5508 for (i = 0; i < 2; ++i)
5509 if (!BUFFERP (echo_buffer[i])
5510 || NILP (XBUFFER (echo_buffer[i])->name))
5511 {
5512 char name[30];
5513 Lisp_Object old_buffer;
5514 int j;
5515
5516 old_buffer = echo_buffer[i];
5517 sprintf (name, " *Echo Area %d*", i);
5518 echo_buffer[i] = Fget_buffer_create (build_string (name));
5519 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5520
5521 for (j = 0; j < 2; ++j)
5522 if (EQ (old_buffer, echo_area_buffer[j]))
5523 echo_area_buffer[j] = echo_buffer[i];
5524 }
5525 }
5526
5527
5528 /* Call FN with args A1..A4 with either the current or last displayed
5529 echo_area_buffer as current buffer.
5530
5531 WHICH zero means use the current message buffer
5532 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5533 from echo_buffer[] and clear it.
5534
5535 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5536 suitable buffer from echo_buffer[] and clear it.
5537
5538 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5539 that the current message becomes the last displayed one, make
5540 choose a suitable buffer for echo_area_buffer[0], and clear it.
5541
5542 Value is what FN returns. */
5543
5544 static int
5545 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5546 struct window *w;
5547 int which;
5548 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5549 EMACS_INT a1;
5550 Lisp_Object a2;
5551 EMACS_INT a3, a4;
5552 {
5553 Lisp_Object buffer;
5554 int this_one, the_other, clear_buffer_p, rc;
5555 int count = specpdl_ptr - specpdl;
5556
5557 /* If buffers aren't life, make new ones. */
5558 ensure_echo_area_buffers ();
5559
5560 clear_buffer_p = 0;
5561
5562 if (which == 0)
5563 this_one = 0, the_other = 1;
5564 else if (which > 0)
5565 this_one = 1, the_other = 0;
5566 else
5567 {
5568 this_one = 0, the_other = 1;
5569 clear_buffer_p = 1;
5570
5571 /* We need a fresh one in case the current echo buffer equals
5572 the one containing the last displayed echo area message. */
5573 if (!NILP (echo_area_buffer[this_one])
5574 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5575 echo_area_buffer[this_one] = Qnil;
5576 }
5577
5578 /* Choose a suitable buffer from echo_buffer[] is we don't
5579 have one. */
5580 if (NILP (echo_area_buffer[this_one]))
5581 {
5582 echo_area_buffer[this_one]
5583 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5584 ? echo_buffer[the_other]
5585 : echo_buffer[this_one]);
5586 clear_buffer_p = 1;
5587 }
5588
5589 buffer = echo_area_buffer[this_one];
5590
5591 record_unwind_protect (unwind_with_echo_area_buffer,
5592 with_echo_area_buffer_unwind_data (w));
5593
5594 /* Make the echo area buffer current. Note that for display
5595 purposes, it is not necessary that the displayed window's buffer
5596 == current_buffer, except for text property lookup. So, let's
5597 only set that buffer temporarily here without doing a full
5598 Fset_window_buffer. We must also change w->pointm, though,
5599 because otherwise an assertions in unshow_buffer fails, and Emacs
5600 aborts. */
5601 set_buffer_internal_1 (XBUFFER (buffer));
5602 if (w)
5603 {
5604 w->buffer = buffer;
5605 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5606 }
5607
5608 current_buffer->undo_list = Qt;
5609 current_buffer->read_only = Qnil;
5610
5611 if (clear_buffer_p && Z > BEG)
5612 del_range (BEG, Z);
5613
5614 xassert (BEGV >= BEG);
5615 xassert (ZV <= Z && ZV >= BEGV);
5616
5617 rc = fn (a1, a2, a3, a4);
5618
5619 xassert (BEGV >= BEG);
5620 xassert (ZV <= Z && ZV >= BEGV);
5621
5622 unbind_to (count, Qnil);
5623 return rc;
5624 }
5625
5626
5627 /* Save state that should be preserved around the call to the function
5628 FN called in with_echo_area_buffer. */
5629
5630 static Lisp_Object
5631 with_echo_area_buffer_unwind_data (w)
5632 struct window *w;
5633 {
5634 int i = 0;
5635 Lisp_Object vector;
5636
5637 /* Reduce consing by keeping one vector in
5638 Vwith_echo_area_save_vector. */
5639 vector = Vwith_echo_area_save_vector;
5640 Vwith_echo_area_save_vector = Qnil;
5641
5642 if (NILP (vector))
5643 vector = Fmake_vector (make_number (7), Qnil);
5644
5645 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5646 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5647 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5648
5649 if (w)
5650 {
5651 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5652 XVECTOR (vector)->contents[i++] = w->buffer;
5653 XVECTOR (vector)->contents[i++]
5654 = make_number (XMARKER (w->pointm)->charpos);
5655 XVECTOR (vector)->contents[i++]
5656 = make_number (XMARKER (w->pointm)->bytepos);
5657 }
5658 else
5659 {
5660 int end = i + 4;
5661 while (i < end)
5662 XVECTOR (vector)->contents[i++] = Qnil;
5663 }
5664
5665 xassert (i == XVECTOR (vector)->size);
5666 return vector;
5667 }
5668
5669
5670 /* Restore global state from VECTOR which was created by
5671 with_echo_area_buffer_unwind_data. */
5672
5673 static Lisp_Object
5674 unwind_with_echo_area_buffer (vector)
5675 Lisp_Object vector;
5676 {
5677 int i = 0;
5678
5679 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5680 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5681 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5682
5683 if (WINDOWP (XVECTOR (vector)->contents[i]))
5684 {
5685 struct window *w;
5686 Lisp_Object buffer, charpos, bytepos;
5687
5688 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5689 buffer = XVECTOR (vector)->contents[i]; ++i;
5690 charpos = XVECTOR (vector)->contents[i]; ++i;
5691 bytepos = XVECTOR (vector)->contents[i]; ++i;
5692
5693 w->buffer = buffer;
5694 set_marker_both (w->pointm, buffer,
5695 XFASTINT (charpos), XFASTINT (bytepos));
5696 }
5697
5698 Vwith_echo_area_save_vector = vector;
5699 return Qnil;
5700 }
5701
5702
5703 /* Set up the echo area for use by print functions. MULTIBYTE_P
5704 non-zero means we will print multibyte. */
5705
5706 void
5707 setup_echo_area_for_printing (multibyte_p)
5708 int multibyte_p;
5709 {
5710 ensure_echo_area_buffers ();
5711
5712 if (!message_buf_print)
5713 {
5714 /* A message has been output since the last time we printed.
5715 Choose a fresh echo area buffer. */
5716 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5717 echo_area_buffer[0] = echo_buffer[1];
5718 else
5719 echo_area_buffer[0] = echo_buffer[0];
5720
5721 /* Switch to that buffer and clear it. */
5722 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5723 if (Z > BEG)
5724 del_range (BEG, Z);
5725 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5726
5727 /* Set up the buffer for the multibyteness we need. */
5728 if (multibyte_p
5729 != !NILP (current_buffer->enable_multibyte_characters))
5730 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5731
5732 /* Raise the frame containing the echo area. */
5733 if (minibuffer_auto_raise)
5734 {
5735 struct frame *sf = SELECTED_FRAME ();
5736 Lisp_Object mini_window;
5737 mini_window = FRAME_MINIBUF_WINDOW (sf);
5738 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5739 }
5740
5741 message_log_maybe_newline ();
5742 message_buf_print = 1;
5743 }
5744 else
5745 {
5746 if (NILP (echo_area_buffer[0]))
5747 {
5748 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5749 echo_area_buffer[0] = echo_buffer[1];
5750 else
5751 echo_area_buffer[0] = echo_buffer[0];
5752 }
5753
5754 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5755 /* Someone switched buffers between print requests. */
5756 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5757 }
5758 }
5759
5760
5761 /* Display an echo area message in window W. Value is non-zero if W's
5762 height is changed. If display_last_displayed_message_p is
5763 non-zero, display the message that was last displayed, otherwise
5764 display the current message. */
5765
5766 static int
5767 display_echo_area (w)
5768 struct window *w;
5769 {
5770 int i, no_message_p, window_height_changed_p, count;
5771
5772 /* Temporarily disable garbage collections while displaying the echo
5773 area. This is done because a GC can print a message itself.
5774 That message would modify the echo area buffer's contents while a
5775 redisplay of the buffer is going on, and seriously confuse
5776 redisplay. */
5777 count = inhibit_garbage_collection ();
5778
5779 /* If there is no message, we must call display_echo_area_1
5780 nevertheless because it resizes the window. But we will have to
5781 reset the echo_area_buffer in question to nil at the end because
5782 with_echo_area_buffer will sets it to an empty buffer. */
5783 i = display_last_displayed_message_p ? 1 : 0;
5784 no_message_p = NILP (echo_area_buffer[i]);
5785
5786 window_height_changed_p
5787 = with_echo_area_buffer (w, display_last_displayed_message_p,
5788 display_echo_area_1,
5789 (EMACS_INT) w, Qnil, 0, 0);
5790
5791 if (no_message_p)
5792 echo_area_buffer[i] = Qnil;
5793
5794 unbind_to (count, Qnil);
5795 return window_height_changed_p;
5796 }
5797
5798
5799 /* Helper for display_echo_area. Display the current buffer which
5800 contains the current echo area message in window W, a mini-window,
5801 a pointer to which is passed in A1. A2..A4 are currently not used.
5802 Change the height of W so that all of the message is displayed.
5803 Value is non-zero if height of W was changed. */
5804
5805 static int
5806 display_echo_area_1 (a1, a2, a3, a4)
5807 EMACS_INT a1;
5808 Lisp_Object a2;
5809 EMACS_INT a3, a4;
5810 {
5811 struct window *w = (struct window *) a1;
5812 Lisp_Object window;
5813 struct text_pos start;
5814 int window_height_changed_p = 0;
5815
5816 /* Do this before displaying, so that we have a large enough glyph
5817 matrix for the display. */
5818 window_height_changed_p = resize_mini_window (w, 0);
5819
5820 /* Display. */
5821 clear_glyph_matrix (w->desired_matrix);
5822 XSETWINDOW (window, w);
5823 SET_TEXT_POS (start, BEG, BEG_BYTE);
5824 try_window (window, start);
5825
5826 return window_height_changed_p;
5827 }
5828
5829
5830 /* Resize the echo area window to exactly the size needed for the
5831 currently displayed message, if there is one. */
5832
5833 void
5834 resize_echo_area_axactly ()
5835 {
5836 if (BUFFERP (echo_area_buffer[0])
5837 && WINDOWP (echo_area_window))
5838 {
5839 struct window *w = XWINDOW (echo_area_window);
5840 int resized_p;
5841
5842 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
5843 (EMACS_INT) w, Qnil, 0, 0);
5844 if (resized_p)
5845 {
5846 ++windows_or_buffers_changed;
5847 ++update_mode_lines;
5848 redisplay_internal (0);
5849 }
5850 }
5851 }
5852
5853
5854 /* Callback function for with_echo_area_buffer, when used from
5855 resize_echo_area_axactly. A1 contains a pointer to the window to
5856 resize, A2 to A4 are not used. Value is what resize_mini_window
5857 returns. */
5858
5859 static int
5860 resize_mini_window_1 (a1, a2, a3, a4)
5861 EMACS_INT a1;
5862 Lisp_Object a2;
5863 EMACS_INT a3, a4;
5864 {
5865 return resize_mini_window ((struct window *) a1, 1);
5866 }
5867
5868
5869 /* Resize mini-window W to fit the size of its contents. EXACT:P
5870 means size the window exactly to the size needed. Otherwise, it's
5871 only enlarged until W's buffer is empty. Value is non-zero if
5872 the window height has been changed. */
5873
5874 int
5875 resize_mini_window (w, exact_p)
5876 struct window *w;
5877 int exact_p;
5878 {
5879 struct frame *f = XFRAME (w->frame);
5880 int window_height_changed_p = 0;
5881
5882 xassert (MINI_WINDOW_P (w));
5883
5884 /* Nil means don't try to resize. */
5885 if (NILP (Vmax_mini_window_height)
5886 || (FRAME_X_P (f) && f->output_data.x == NULL))
5887 return 0;
5888
5889 if (!FRAME_MINIBUF_ONLY_P (f))
5890 {
5891 struct it it;
5892 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
5893 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
5894 int height, max_height;
5895 int unit = CANON_Y_UNIT (f);
5896 struct text_pos start;
5897
5898 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
5899
5900 /* Compute the max. number of lines specified by the user. */
5901 if (FLOATP (Vmax_mini_window_height))
5902 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
5903 else if (INTEGERP (Vmax_mini_window_height))
5904 max_height = XINT (Vmax_mini_window_height);
5905 else
5906 max_height = total_height / 4;
5907
5908 /* Correct that max. height if it's bogus. */
5909 max_height = max (1, max_height);
5910 max_height = min (total_height, max_height);
5911
5912 /* Find out the height of the text in the window. */
5913 if (it.truncate_lines_p)
5914 height = 1;
5915 else
5916 {
5917 last_height = 0;
5918 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
5919 if (it.max_ascent == 0 && it.max_descent == 0)
5920 height = it.current_y + last_height;
5921 else
5922 height = it.current_y + it.max_ascent + it.max_descent;
5923 height -= it.extra_line_spacing;
5924 height = (height + unit - 1) / unit;
5925 }
5926
5927 /* Compute a suitable window start. */
5928 if (height > max_height)
5929 {
5930 height = max_height;
5931 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5932 move_it_vertically_backward (&it, (height - 1) * unit);
5933 start = it.current.pos;
5934 }
5935 else
5936 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5937 SET_MARKER_FROM_TEXT_POS (w->start, start);
5938
5939 /* Let it grow only, until we display an empty message, in which
5940 case the window shrinks again. */
5941 if (height > XFASTINT (w->height))
5942 {
5943 int old_height = XFASTINT (w->height);
5944 freeze_window_starts (f, 1);
5945 grow_mini_window (w, height - XFASTINT (w->height));
5946 window_height_changed_p = XFASTINT (w->height) != old_height;
5947 }
5948 else if (height < XFASTINT (w->height)
5949 && (exact_p || BEGV == ZV))
5950 {
5951 int old_height = XFASTINT (w->height);
5952 freeze_window_starts (f, 0);
5953 shrink_mini_window (w);
5954 window_height_changed_p = XFASTINT (w->height) != old_height;
5955 }
5956 }
5957
5958 return window_height_changed_p;
5959 }
5960
5961
5962 /* Value is the current message, a string, or nil if there is no
5963 current message. */
5964
5965 Lisp_Object
5966 current_message ()
5967 {
5968 Lisp_Object msg;
5969
5970 if (NILP (echo_area_buffer[0]))
5971 msg = Qnil;
5972 else
5973 {
5974 with_echo_area_buffer (0, 0, current_message_1,
5975 (EMACS_INT) &msg, Qnil, 0, 0);
5976 if (NILP (msg))
5977 echo_area_buffer[0] = Qnil;
5978 }
5979
5980 return msg;
5981 }
5982
5983
5984 static int
5985 current_message_1 (a1, a2, a3, a4)
5986 EMACS_INT a1;
5987 Lisp_Object a2;
5988 EMACS_INT a3, a4;
5989 {
5990 Lisp_Object *msg = (Lisp_Object *) a1;
5991
5992 if (Z > BEG)
5993 *msg = make_buffer_string (BEG, Z, 1);
5994 else
5995 *msg = Qnil;
5996 return 0;
5997 }
5998
5999
6000 /* Push the current message on Vmessage_stack for later restauration
6001 by restore_message. Value is non-zero if the current message isn't
6002 empty. This is a relatively infrequent operation, so it's not
6003 worth optimizing. */
6004
6005 int
6006 push_message ()
6007 {
6008 Lisp_Object msg;
6009 msg = current_message ();
6010 Vmessage_stack = Fcons (msg, Vmessage_stack);
6011 return STRINGP (msg);
6012 }
6013
6014
6015 /* Restore message display from the top of Vmessage_stack. */
6016
6017 void
6018 restore_message ()
6019 {
6020 Lisp_Object msg;
6021
6022 xassert (CONSP (Vmessage_stack));
6023 msg = XCAR (Vmessage_stack);
6024 if (STRINGP (msg))
6025 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6026 else
6027 message3_nolog (msg, 0, 0);
6028 }
6029
6030
6031 /* Pop the top-most entry off Vmessage_stack. */
6032
6033 void
6034 pop_message ()
6035 {
6036 xassert (CONSP (Vmessage_stack));
6037 Vmessage_stack = XCDR (Vmessage_stack);
6038 }
6039
6040
6041 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6042 exits. If the stack is not empty, we have a missing pop_message
6043 somewhere. */
6044
6045 void
6046 check_message_stack ()
6047 {
6048 if (!NILP (Vmessage_stack))
6049 abort ();
6050 }
6051
6052
6053 /* Truncate to NCHARS what will be displayed in the echo area the next
6054 time we display it---but don't redisplay it now. */
6055
6056 void
6057 truncate_echo_area (nchars)
6058 int nchars;
6059 {
6060 if (nchars == 0)
6061 echo_area_buffer[0] = Qnil;
6062 /* A null message buffer means that the frame hasn't really been
6063 initialized yet. Error messages get reported properly by
6064 cmd_error, so this must be just an informative message; toss it. */
6065 else if (!noninteractive
6066 && INTERACTIVE
6067 && !NILP (echo_area_buffer[0]))
6068 {
6069 struct frame *sf = SELECTED_FRAME ();
6070 if (FRAME_MESSAGE_BUF (sf))
6071 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6072 }
6073 }
6074
6075
6076 /* Helper function for truncate_echo_area. Truncate the current
6077 message to at most NCHARS characters. */
6078
6079 static int
6080 truncate_message_1 (nchars, a2, a3, a4)
6081 EMACS_INT nchars;
6082 Lisp_Object a2;
6083 EMACS_INT a3, a4;
6084 {
6085 if (BEG + nchars < Z)
6086 del_range (BEG + nchars, Z);
6087 if (Z == BEG)
6088 echo_area_buffer[0] = Qnil;
6089 return 0;
6090 }
6091
6092
6093 /* Set the current message to a substring of S or STRING.
6094
6095 If STRING is a Lisp string, set the message to the first NBYTES
6096 bytes from STRING. NBYTES zero means use the whole string. If
6097 STRING is multibyte, the message will be displayed multibyte.
6098
6099 If S is not null, set the message to the first LEN bytes of S. LEN
6100 zero means use the whole string. MULTIBYTE_P non-zero means S is
6101 multibyte. Display the message multibyte in that case. */
6102
6103 void
6104 set_message (s, string, nbytes, multibyte_p)
6105 char *s;
6106 Lisp_Object string;
6107 int nbytes;
6108 {
6109 message_enable_multibyte
6110 = ((s && multibyte_p)
6111 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6112
6113 with_echo_area_buffer (0, -1, set_message_1,
6114 (EMACS_INT) s, string, nbytes, multibyte_p);
6115 message_buf_print = 0;
6116 }
6117
6118
6119 /* Helper function for set_message. Arguments have the same meaning
6120 as there, with A1 corresponding to S and A2 corresponding to STRING
6121 This function is called with the echo area buffer being
6122 current. */
6123
6124 static int
6125 set_message_1 (a1, a2, nbytes, multibyte_p)
6126 EMACS_INT a1;
6127 Lisp_Object a2;
6128 EMACS_INT nbytes, multibyte_p;
6129 {
6130 char *s = (char *) a1;
6131 Lisp_Object string = a2;
6132
6133 xassert (BEG == Z);
6134
6135 /* Change multibyteness of the echo buffer appropriately. */
6136 if (message_enable_multibyte
6137 != !NILP (current_buffer->enable_multibyte_characters))
6138 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6139
6140 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6141
6142 /* Insert new message at BEG. */
6143 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6144
6145 if (STRINGP (string))
6146 {
6147 int nchars;
6148
6149 if (nbytes == 0)
6150 nbytes = XSTRING (string)->size_byte;
6151 nchars = string_byte_to_char (string, nbytes);
6152
6153 /* This function takes care of single/multibyte conversion. We
6154 just have to ensure that the echo area buffer has the right
6155 setting of enable_multibyte_characters. */
6156 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6157 }
6158 else if (s)
6159 {
6160 if (nbytes == 0)
6161 nbytes = strlen (s);
6162
6163 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6164 {
6165 /* Convert from multi-byte to single-byte. */
6166 int i, c, n;
6167 unsigned char work[1];
6168
6169 /* Convert a multibyte string to single-byte. */
6170 for (i = 0; i < nbytes; i += n)
6171 {
6172 c = string_char_and_length (s + i, nbytes - i, &n);
6173 work[0] = (SINGLE_BYTE_CHAR_P (c)
6174 ? c
6175 : multibyte_char_to_unibyte (c, Qnil));
6176 insert_1_both (work, 1, 1, 1, 0, 0);
6177 }
6178 }
6179 else if (!multibyte_p
6180 && !NILP (current_buffer->enable_multibyte_characters))
6181 {
6182 /* Convert from single-byte to multi-byte. */
6183 int i, c, n;
6184 unsigned char *msg = (unsigned char *) s;
6185 unsigned char str[MAX_MULTIBYTE_LENGTH];
6186
6187 /* Convert a single-byte string to multibyte. */
6188 for (i = 0; i < nbytes; i++)
6189 {
6190 c = unibyte_char_to_multibyte (msg[i]);
6191 n = CHAR_STRING (c, str);
6192 insert_1_both (str, 1, n, 1, 0, 0);
6193 }
6194 }
6195 else
6196 insert_1 (s, nbytes, 1, 0, 0);
6197 }
6198
6199 return 0;
6200 }
6201
6202
6203 /* Clear messages. CURRENT_P non-zero means clear the current
6204 message. LAST_DISPLAYED_P non-zero means clear the message
6205 last displayed. */
6206
6207 void
6208 clear_message (current_p, last_displayed_p)
6209 int current_p, last_displayed_p;
6210 {
6211 if (current_p)
6212 echo_area_buffer[0] = Qnil;
6213
6214 if (last_displayed_p)
6215 echo_area_buffer[1] = Qnil;
6216
6217 message_buf_print = 0;
6218 }
6219
6220 /* Clear garbaged frames.
6221
6222 This function is used where the old redisplay called
6223 redraw_garbaged_frames which in turn called redraw_frame which in
6224 turn called clear_frame. The call to clear_frame was a source of
6225 flickering. I believe a clear_frame is not necessary. It should
6226 suffice in the new redisplay to invalidate all current matrices,
6227 and ensure a complete redisplay of all windows. */
6228
6229 static void
6230 clear_garbaged_frames ()
6231 {
6232 if (frame_garbaged)
6233 {
6234 Lisp_Object tail, frame;
6235
6236 FOR_EACH_FRAME (tail, frame)
6237 {
6238 struct frame *f = XFRAME (frame);
6239
6240 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6241 {
6242 clear_current_matrices (f);
6243 f->garbaged = 0;
6244 }
6245 }
6246
6247 frame_garbaged = 0;
6248 ++windows_or_buffers_changed;
6249 }
6250 }
6251
6252
6253 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6254 is non-zero update selected_frame. Value is non-zero if the
6255 mini-windows height has been changed. */
6256
6257 static int
6258 echo_area_display (update_frame_p)
6259 int update_frame_p;
6260 {
6261 Lisp_Object mini_window;
6262 struct window *w;
6263 struct frame *f;
6264 int window_height_changed_p = 0;
6265 struct frame *sf = SELECTED_FRAME ();
6266
6267 mini_window = FRAME_MINIBUF_WINDOW (sf);
6268 w = XWINDOW (mini_window);
6269 f = XFRAME (WINDOW_FRAME (w));
6270
6271 /* Don't display if frame is invisible or not yet initialized. */
6272 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6273 return 0;
6274
6275 #ifdef HAVE_WINDOW_SYSTEM
6276 /* When Emacs starts, selected_frame may be a visible terminal
6277 frame, even if we run under a window system. If we let this
6278 through, a message would be displayed on the terminal. */
6279 if (EQ (selected_frame, Vterminal_frame)
6280 && !NILP (Vwindow_system))
6281 return 0;
6282 #endif /* HAVE_WINDOW_SYSTEM */
6283
6284 /* Redraw garbaged frames. */
6285 if (frame_garbaged)
6286 clear_garbaged_frames ();
6287
6288 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6289 {
6290 echo_area_window = mini_window;
6291 window_height_changed_p = display_echo_area (w);
6292 w->must_be_updated_p = 1;
6293
6294 /* Update the display, unless called from redisplay_internal. */
6295 if (update_frame_p)
6296 {
6297 int n = 0;
6298
6299 /* If the display update has been interrupted by pending
6300 input, update mode lines in the frame. Due to the
6301 pending input, it might have been that redisplay hasn't
6302 been called, so that mode lines above the echo area are
6303 garbaged. This looks odd, so we prevent it here. */
6304 if (!display_completed)
6305 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6306
6307 if (window_height_changed_p)
6308 {
6309 /* Must update other windows. */
6310 windows_or_buffers_changed = 1;
6311 redisplay_internal (0);
6312 }
6313 else if (FRAME_WINDOW_P (f) && n == 0)
6314 {
6315 /* Window configuration is the same as before.
6316 Can do with a display update of the echo area,
6317 unless we displayed some mode lines. */
6318 update_single_window (w, 1);
6319 rif->flush_display (f);
6320 }
6321 else
6322 update_frame (f, 1, 1);
6323 }
6324 }
6325 else if (!EQ (mini_window, selected_window))
6326 windows_or_buffers_changed++;
6327
6328 /* Last displayed message is now the current message. */
6329 echo_area_buffer[1] = echo_area_buffer[0];
6330
6331 /* Prevent redisplay optimization in redisplay_internal by resetting
6332 this_line_start_pos. This is done because the mini-buffer now
6333 displays the message instead of its buffer text. */
6334 if (EQ (mini_window, selected_window))
6335 CHARPOS (this_line_start_pos) = 0;
6336
6337 return window_height_changed_p;
6338 }
6339
6340
6341 \f
6342 /***********************************************************************
6343 Frame Titles
6344 ***********************************************************************/
6345
6346
6347 #ifdef HAVE_WINDOW_SYSTEM
6348
6349 /* A buffer for constructing frame titles in it; allocated from the
6350 heap in init_xdisp and resized as needed in store_frame_title_char. */
6351
6352 static char *frame_title_buf;
6353
6354 /* The buffer's end, and a current output position in it. */
6355
6356 static char *frame_title_buf_end;
6357 static char *frame_title_ptr;
6358
6359
6360 /* Store a single character C for the frame title in frame_title_buf.
6361 Re-allocate frame_title_buf if necessary. */
6362
6363 static void
6364 store_frame_title_char (c)
6365 char c;
6366 {
6367 /* If output position has reached the end of the allocated buffer,
6368 double the buffer's size. */
6369 if (frame_title_ptr == frame_title_buf_end)
6370 {
6371 int len = frame_title_ptr - frame_title_buf;
6372 int new_size = 2 * len * sizeof *frame_title_buf;
6373 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6374 frame_title_buf_end = frame_title_buf + new_size;
6375 frame_title_ptr = frame_title_buf + len;
6376 }
6377
6378 *frame_title_ptr++ = c;
6379 }
6380
6381
6382 /* Store part of a frame title in frame_title_buf, beginning at
6383 frame_title_ptr. STR is the string to store. Do not copy more
6384 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6385 the whole string. Pad with spaces until FIELD_WIDTH number of
6386 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6387 Called from display_mode_element when it is used to build a frame
6388 title. */
6389
6390 static int
6391 store_frame_title (str, field_width, precision)
6392 unsigned char *str;
6393 int field_width, precision;
6394 {
6395 int n = 0;
6396
6397 /* Copy at most PRECISION chars from STR. */
6398 while ((precision <= 0 || n < precision)
6399 && *str)
6400 {
6401 store_frame_title_char (*str++);
6402 ++n;
6403 }
6404
6405 /* Fill up with spaces until FIELD_WIDTH reached. */
6406 while (field_width > 0
6407 && n < field_width)
6408 {
6409 store_frame_title_char (' ');
6410 ++n;
6411 }
6412
6413 return n;
6414 }
6415
6416
6417 /* Set the title of FRAME, if it has changed. The title format is
6418 Vicon_title_format if FRAME is iconified, otherwise it is
6419 frame_title_format. */
6420
6421 static void
6422 x_consider_frame_title (frame)
6423 Lisp_Object frame;
6424 {
6425 struct frame *f = XFRAME (frame);
6426
6427 if (FRAME_WINDOW_P (f)
6428 || FRAME_MINIBUF_ONLY_P (f)
6429 || f->explicit_name)
6430 {
6431 /* Do we have more than one visible frame on this X display? */
6432 Lisp_Object tail;
6433 Lisp_Object fmt;
6434 struct buffer *obuf;
6435 int len;
6436 struct it it;
6437
6438 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6439 {
6440 struct frame *tf = XFRAME (XCAR (tail));
6441
6442 if (tf != f
6443 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6444 && !FRAME_MINIBUF_ONLY_P (tf)
6445 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6446 break;
6447 }
6448
6449 /* Set global variable indicating that multiple frames exist. */
6450 multiple_frames = CONSP (tail);
6451
6452 /* Switch to the buffer of selected window of the frame. Set up
6453 frame_title_ptr so that display_mode_element will output into it;
6454 then display the title. */
6455 obuf = current_buffer;
6456 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6457 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6458 frame_title_ptr = frame_title_buf;
6459 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6460 NULL, DEFAULT_FACE_ID);
6461 len = display_mode_element (&it, 0, -1, -1, fmt);
6462 frame_title_ptr = NULL;
6463 set_buffer_internal (obuf);
6464
6465 /* Set the title only if it's changed. This avoids consing in
6466 the common case where it hasn't. (If it turns out that we've
6467 already wasted too much time by walking through the list with
6468 display_mode_element, then we might need to optimize at a
6469 higher level than this.) */
6470 if (! STRINGP (f->name)
6471 || STRING_BYTES (XSTRING (f->name)) != len
6472 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6473 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6474 }
6475 }
6476
6477 #else /* not HAVE_WINDOW_SYSTEM */
6478
6479 #define frame_title_ptr ((char *)0)
6480 #define store_frame_title(str, mincol, maxcol) 0
6481
6482 #endif /* not HAVE_WINDOW_SYSTEM */
6483
6484
6485
6486 \f
6487 /***********************************************************************
6488 Menu Bars
6489 ***********************************************************************/
6490
6491
6492 /* Prepare for redisplay by updating menu-bar item lists when
6493 appropriate. This can call eval. */
6494
6495 void
6496 prepare_menu_bars ()
6497 {
6498 int all_windows;
6499 struct gcpro gcpro1, gcpro2;
6500 struct frame *f;
6501 struct frame *tooltip_frame;
6502
6503 #ifdef HAVE_X_WINDOWS
6504 tooltip_frame = tip_frame;
6505 #else
6506 tooltip_frame = NULL;
6507 #endif
6508
6509 /* Update all frame titles based on their buffer names, etc. We do
6510 this before the menu bars so that the buffer-menu will show the
6511 up-to-date frame titles. */
6512 #ifdef HAVE_WINDOW_SYSTEM
6513 if (windows_or_buffers_changed || update_mode_lines)
6514 {
6515 Lisp_Object tail, frame;
6516
6517 FOR_EACH_FRAME (tail, frame)
6518 {
6519 f = XFRAME (frame);
6520 if (f != tooltip_frame
6521 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6522 x_consider_frame_title (frame);
6523 }
6524 }
6525 #endif /* HAVE_WINDOW_SYSTEM */
6526
6527 /* Update the menu bar item lists, if appropriate. This has to be
6528 done before any actual redisplay or generation of display lines. */
6529 all_windows = (update_mode_lines
6530 || buffer_shared > 1
6531 || windows_or_buffers_changed);
6532 if (all_windows)
6533 {
6534 Lisp_Object tail, frame;
6535 int count = specpdl_ptr - specpdl;
6536
6537 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6538
6539 FOR_EACH_FRAME (tail, frame)
6540 {
6541 f = XFRAME (frame);
6542
6543 /* Ignore tooltip frame. */
6544 if (f == tooltip_frame)
6545 continue;
6546
6547 /* If a window on this frame changed size, report that to
6548 the user and clear the size-change flag. */
6549 if (FRAME_WINDOW_SIZES_CHANGED (f))
6550 {
6551 Lisp_Object functions;
6552
6553 /* Clear flag first in case we get an error below. */
6554 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6555 functions = Vwindow_size_change_functions;
6556 GCPRO2 (tail, functions);
6557
6558 while (CONSP (functions))
6559 {
6560 call1 (XCAR (functions), frame);
6561 functions = XCDR (functions);
6562 }
6563 UNGCPRO;
6564 }
6565
6566 GCPRO1 (tail);
6567 update_menu_bar (f, 0);
6568 #ifdef HAVE_WINDOW_SYSTEM
6569 update_tool_bar (f, 0);
6570 #endif
6571 UNGCPRO;
6572 }
6573
6574 unbind_to (count, Qnil);
6575 }
6576 else
6577 {
6578 struct frame *sf = SELECTED_FRAME ();
6579 update_menu_bar (sf, 1);
6580 #ifdef HAVE_WINDOW_SYSTEM
6581 update_tool_bar (sf, 1);
6582 #endif
6583 }
6584
6585 /* Motif needs this. See comment in xmenu.c. Turn it off when
6586 pending_menu_activation is not defined. */
6587 #ifdef USE_X_TOOLKIT
6588 pending_menu_activation = 0;
6589 #endif
6590 }
6591
6592
6593 /* Update the menu bar item list for frame F. This has to be done
6594 before we start to fill in any display lines, because it can call
6595 eval.
6596
6597 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6598
6599 static void
6600 update_menu_bar (f, save_match_data)
6601 struct frame *f;
6602 int save_match_data;
6603 {
6604 Lisp_Object window;
6605 register struct window *w;
6606
6607 window = FRAME_SELECTED_WINDOW (f);
6608 w = XWINDOW (window);
6609
6610 if (update_mode_lines)
6611 w->update_mode_line = Qt;
6612
6613 if (FRAME_WINDOW_P (f)
6614 ?
6615 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6616 FRAME_EXTERNAL_MENU_BAR (f)
6617 #else
6618 FRAME_MENU_BAR_LINES (f) > 0
6619 #endif
6620 : FRAME_MENU_BAR_LINES (f) > 0)
6621 {
6622 /* If the user has switched buffers or windows, we need to
6623 recompute to reflect the new bindings. But we'll
6624 recompute when update_mode_lines is set too; that means
6625 that people can use force-mode-line-update to request
6626 that the menu bar be recomputed. The adverse effect on
6627 the rest of the redisplay algorithm is about the same as
6628 windows_or_buffers_changed anyway. */
6629 if (windows_or_buffers_changed
6630 || !NILP (w->update_mode_line)
6631 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6632 < BUF_MODIFF (XBUFFER (w->buffer)))
6633 != !NILP (w->last_had_star))
6634 || ((!NILP (Vtransient_mark_mode)
6635 && !NILP (XBUFFER (w->buffer)->mark_active))
6636 != !NILP (w->region_showing)))
6637 {
6638 struct buffer *prev = current_buffer;
6639 int count = specpdl_ptr - specpdl;
6640
6641 set_buffer_internal_1 (XBUFFER (w->buffer));
6642 if (save_match_data)
6643 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6644 if (NILP (Voverriding_local_map_menu_flag))
6645 {
6646 specbind (Qoverriding_terminal_local_map, Qnil);
6647 specbind (Qoverriding_local_map, Qnil);
6648 }
6649
6650 /* Run the Lucid hook. */
6651 call1 (Vrun_hooks, Qactivate_menubar_hook);
6652
6653 /* If it has changed current-menubar from previous value,
6654 really recompute the menu-bar from the value. */
6655 if (! NILP (Vlucid_menu_bar_dirty_flag))
6656 call0 (Qrecompute_lucid_menubar);
6657
6658 safe_run_hooks (Qmenu_bar_update_hook);
6659 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6660
6661 /* Redisplay the menu bar in case we changed it. */
6662 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
6663 if (FRAME_WINDOW_P (f))
6664 set_frame_menubar (f, 0, 0);
6665 else
6666 /* On a terminal screen, the menu bar is an ordinary screen
6667 line, and this makes it get updated. */
6668 w->update_mode_line = Qt;
6669 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6670 /* In the non-toolkit version, the menu bar is an ordinary screen
6671 line, and this makes it get updated. */
6672 w->update_mode_line = Qt;
6673 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6674
6675 unbind_to (count, Qnil);
6676 set_buffer_internal_1 (prev);
6677 }
6678 }
6679 }
6680
6681
6682 \f
6683 /***********************************************************************
6684 Tool-bars
6685 ***********************************************************************/
6686
6687 #ifdef HAVE_WINDOW_SYSTEM
6688
6689 /* Update the tool-bar item list for frame F. This has to be done
6690 before we start to fill in any display lines. Called from
6691 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6692 and restore it here. */
6693
6694 static void
6695 update_tool_bar (f, save_match_data)
6696 struct frame *f;
6697 int save_match_data;
6698 {
6699 if (WINDOWP (f->tool_bar_window)
6700 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6701 {
6702 Lisp_Object window;
6703 struct window *w;
6704
6705 window = FRAME_SELECTED_WINDOW (f);
6706 w = XWINDOW (window);
6707
6708 /* If the user has switched buffers or windows, we need to
6709 recompute to reflect the new bindings. But we'll
6710 recompute when update_mode_lines is set too; that means
6711 that people can use force-mode-line-update to request
6712 that the menu bar be recomputed. The adverse effect on
6713 the rest of the redisplay algorithm is about the same as
6714 windows_or_buffers_changed anyway. */
6715 if (windows_or_buffers_changed
6716 || !NILP (w->update_mode_line)
6717 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6718 < BUF_MODIFF (XBUFFER (w->buffer)))
6719 != !NILP (w->last_had_star))
6720 || ((!NILP (Vtransient_mark_mode)
6721 && !NILP (XBUFFER (w->buffer)->mark_active))
6722 != !NILP (w->region_showing)))
6723 {
6724 struct buffer *prev = current_buffer;
6725 int count = specpdl_ptr - specpdl;
6726
6727 /* Set current_buffer to the buffer of the selected
6728 window of the frame, so that we get the right local
6729 keymaps. */
6730 set_buffer_internal_1 (XBUFFER (w->buffer));
6731
6732 /* Save match data, if we must. */
6733 if (save_match_data)
6734 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6735
6736 /* Make sure that we don't accidentally use bogus keymaps. */
6737 if (NILP (Voverriding_local_map_menu_flag))
6738 {
6739 specbind (Qoverriding_terminal_local_map, Qnil);
6740 specbind (Qoverriding_local_map, Qnil);
6741 }
6742
6743 /* Build desired tool-bar items from keymaps. */
6744 f->desired_tool_bar_items
6745 = tool_bar_items (f->desired_tool_bar_items,
6746 &f->n_desired_tool_bar_items);
6747
6748 /* Redisplay the tool-bar in case we changed it. */
6749 w->update_mode_line = Qt;
6750
6751 unbind_to (count, Qnil);
6752 set_buffer_internal_1 (prev);
6753 }
6754 }
6755 }
6756
6757
6758 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6759 F's desired tool-bar contents. F->desired_tool_bar_items must have
6760 been set up previously by calling prepare_menu_bars. */
6761
6762 static void
6763 build_desired_tool_bar_string (f)
6764 struct frame *f;
6765 {
6766 int i, size, size_needed, string_idx;
6767 struct gcpro gcpro1, gcpro2, gcpro3;
6768 Lisp_Object image, plist, props;
6769
6770 image = plist = props = Qnil;
6771 GCPRO3 (image, plist, props);
6772
6773 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6774 Otherwise, make a new string. */
6775
6776 /* The size of the string we might be able to reuse. */
6777 size = (STRINGP (f->desired_tool_bar_string)
6778 ? XSTRING (f->desired_tool_bar_string)->size
6779 : 0);
6780
6781 /* Each image in the string we build is preceded by a space,
6782 and there is a space at the end. */
6783 size_needed = f->n_desired_tool_bar_items + 1;
6784
6785 /* Reuse f->desired_tool_bar_string, if possible. */
6786 if (size < size_needed)
6787 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6788 make_number (' '));
6789 else
6790 {
6791 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6792 Fremove_text_properties (make_number (0), make_number (size),
6793 props, f->desired_tool_bar_string);
6794 }
6795
6796 /* Put a `display' property on the string for the images to display,
6797 put a `menu_item' property on tool-bar items with a value that
6798 is the index of the item in F's tool-bar item vector. */
6799 for (i = 0, string_idx = 0;
6800 i < f->n_desired_tool_bar_items;
6801 ++i, string_idx += 1)
6802 {
6803 #define PROP(IDX) \
6804 (XVECTOR (f->desired_tool_bar_items) \
6805 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6806
6807 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6808 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6809 int margin, relief;
6810 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6811 extern Lisp_Object Qlaplace;
6812
6813 /* If image is a vector, choose the image according to the
6814 button state. */
6815 image = PROP (TOOL_BAR_ITEM_IMAGES);
6816 if (VECTORP (image))
6817 {
6818 enum tool_bar_item_image idx;
6819
6820 if (enabled_p)
6821 idx = (selected_p
6822 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6823 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6824 else
6825 idx = (selected_p
6826 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6827 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6828
6829 xassert (XVECTOR (image)->size >= idx);
6830 image = XVECTOR (image)->contents[idx];
6831 }
6832
6833 /* Ignore invalid image specifications. */
6834 if (!valid_image_p (image))
6835 continue;
6836
6837 /* Display the tool-bar button pressed, or depressed. */
6838 plist = Fcopy_sequence (XCDR (image));
6839
6840 /* Compute margin and relief to draw. */
6841 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6842 margin = relief + max (0, tool_bar_button_margin);
6843
6844 if (auto_raise_tool_bar_buttons_p)
6845 {
6846 /* Add a `:relief' property to the image spec if the item is
6847 selected. */
6848 if (selected_p)
6849 {
6850 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6851 margin -= relief;
6852 }
6853 }
6854 else
6855 {
6856 /* If image is selected, display it pressed, i.e. with a
6857 negative relief. If it's not selected, display it with a
6858 raised relief. */
6859 plist = Fplist_put (plist, QCrelief,
6860 (selected_p
6861 ? make_number (-relief)
6862 : make_number (relief)));
6863 margin -= relief;
6864 }
6865
6866 /* Put a margin around the image. */
6867 if (margin)
6868 plist = Fplist_put (plist, QCmargin, make_number (margin));
6869
6870 /* If button is not enabled, make the image appear disabled by
6871 applying an appropriate algorithm to it. */
6872 if (!enabled_p)
6873 plist = Fplist_put (plist, QCalgorithm, Qlaplace);
6874
6875 /* Put a `display' text property on the string for the image to
6876 display. Put a `menu-item' property on the string that gives
6877 the start of this item's properties in the tool-bar items
6878 vector. */
6879 image = Fcons (Qimage, plist);
6880 props = list4 (Qdisplay, image,
6881 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
6882 Fadd_text_properties (make_number (string_idx),
6883 make_number (string_idx + 1),
6884 props, f->desired_tool_bar_string);
6885 #undef PROP
6886 }
6887
6888 UNGCPRO;
6889 }
6890
6891
6892 /* Display one line of the tool-bar of frame IT->f. */
6893
6894 static void
6895 display_tool_bar_line (it)
6896 struct it *it;
6897 {
6898 struct glyph_row *row = it->glyph_row;
6899 int max_x = it->last_visible_x;
6900 struct glyph *last;
6901
6902 prepare_desired_row (row);
6903 row->y = it->current_y;
6904
6905 while (it->current_x < max_x)
6906 {
6907 int x_before, x, n_glyphs_before, i, nglyphs;
6908
6909 /* Get the next display element. */
6910 if (!get_next_display_element (it))
6911 break;
6912
6913 /* Produce glyphs. */
6914 x_before = it->current_x;
6915 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
6916 PRODUCE_GLYPHS (it);
6917
6918 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
6919 i = 0;
6920 x = x_before;
6921 while (i < nglyphs)
6922 {
6923 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
6924
6925 if (x + glyph->pixel_width > max_x)
6926 {
6927 /* Glyph doesn't fit on line. */
6928 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
6929 it->current_x = x;
6930 goto out;
6931 }
6932
6933 ++it->hpos;
6934 x += glyph->pixel_width;
6935 ++i;
6936 }
6937
6938 /* Stop at line ends. */
6939 if (ITERATOR_AT_END_OF_LINE_P (it))
6940 break;
6941
6942 set_iterator_to_next (it);
6943 }
6944
6945 out:;
6946
6947 row->displays_text_p = row->used[TEXT_AREA] != 0;
6948 extend_face_to_end_of_line (it);
6949 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
6950 last->right_box_line_p = 1;
6951 compute_line_metrics (it);
6952
6953 /* If line is empty, make it occupy the rest of the tool-bar. */
6954 if (!row->displays_text_p)
6955 {
6956 row->height = row->phys_height = it->last_visible_y - row->y;
6957 row->ascent = row->phys_ascent = 0;
6958 }
6959
6960 row->full_width_p = 1;
6961 row->continued_p = 0;
6962 row->truncated_on_left_p = 0;
6963 row->truncated_on_right_p = 0;
6964
6965 it->current_x = it->hpos = 0;
6966 it->current_y += row->height;
6967 ++it->vpos;
6968 ++it->glyph_row;
6969 }
6970
6971
6972 /* Value is the number of screen lines needed to make all tool-bar
6973 items of frame F visible. */
6974
6975 static int
6976 tool_bar_lines_needed (f)
6977 struct frame *f;
6978 {
6979 struct window *w = XWINDOW (f->tool_bar_window);
6980 struct it it;
6981
6982 /* Initialize an iterator for iteration over
6983 F->desired_tool_bar_string in the tool-bar window of frame F. */
6984 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
6985 it.first_visible_x = 0;
6986 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
6987 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
6988
6989 while (!ITERATOR_AT_END_P (&it))
6990 {
6991 it.glyph_row = w->desired_matrix->rows;
6992 clear_glyph_row (it.glyph_row);
6993 display_tool_bar_line (&it);
6994 }
6995
6996 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
6997 }
6998
6999
7000 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7001 height should be changed. */
7002
7003 static int
7004 redisplay_tool_bar (f)
7005 struct frame *f;
7006 {
7007 struct window *w;
7008 struct it it;
7009 struct glyph_row *row;
7010 int change_height_p = 0;
7011
7012 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7013 do anything. This means you must start with tool-bar-lines
7014 non-zero to get the auto-sizing effect. Or in other words, you
7015 can turn off tool-bars by specifying tool-bar-lines zero. */
7016 if (!WINDOWP (f->tool_bar_window)
7017 || (w = XWINDOW (f->tool_bar_window),
7018 XFASTINT (w->height) == 0))
7019 return 0;
7020
7021 /* Set up an iterator for the tool-bar window. */
7022 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7023 it.first_visible_x = 0;
7024 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7025 row = it.glyph_row;
7026
7027 /* Build a string that represents the contents of the tool-bar. */
7028 build_desired_tool_bar_string (f);
7029 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7030
7031 /* Display as many lines as needed to display all tool-bar items. */
7032 while (it.current_y < it.last_visible_y)
7033 display_tool_bar_line (&it);
7034
7035 /* It doesn't make much sense to try scrolling in the tool-bar
7036 window, so don't do it. */
7037 w->desired_matrix->no_scrolling_p = 1;
7038 w->must_be_updated_p = 1;
7039
7040 if (auto_resize_tool_bars_p)
7041 {
7042 int nlines;
7043
7044 /* If there are blank lines at the end, except for a partially
7045 visible blank line at the end that is smaller than
7046 CANON_Y_UNIT, change the tool-bar's height. */
7047 row = it.glyph_row - 1;
7048 if (!row->displays_text_p
7049 && row->height >= CANON_Y_UNIT (f))
7050 change_height_p = 1;
7051
7052 /* If row displays tool-bar items, but is partially visible,
7053 change the tool-bar's height. */
7054 if (row->displays_text_p
7055 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7056 change_height_p = 1;
7057
7058 /* Resize windows as needed by changing the `tool-bar-lines'
7059 frame parameter. */
7060 if (change_height_p
7061 && (nlines = tool_bar_lines_needed (f),
7062 nlines != XFASTINT (w->height)))
7063 {
7064 extern Lisp_Object Qtool_bar_lines;
7065 Lisp_Object frame;
7066
7067 XSETFRAME (frame, f);
7068 clear_glyph_matrix (w->desired_matrix);
7069 Fmodify_frame_parameters (frame,
7070 Fcons (Fcons (Qtool_bar_lines,
7071 make_number (nlines)),
7072 Qnil));
7073 fonts_changed_p = 1;
7074 }
7075 }
7076
7077 return change_height_p;
7078 }
7079
7080
7081 /* Get information about the tool-bar item which is displayed in GLYPH
7082 on frame F. Return in *PROP_IDX the index where tool-bar item
7083 properties start in F->current_tool_bar_items. Value is zero if
7084 GLYPH doesn't display a tool-bar item. */
7085
7086 int
7087 tool_bar_item_info (f, glyph, prop_idx)
7088 struct frame *f;
7089 struct glyph *glyph;
7090 int *prop_idx;
7091 {
7092 Lisp_Object prop;
7093 int success_p;
7094
7095 /* Get the text property `menu-item' at pos. The value of that
7096 property is the start index of this item's properties in
7097 F->current_tool_bar_items. */
7098 prop = Fget_text_property (make_number (glyph->charpos),
7099 Qmenu_item, f->current_tool_bar_string);
7100 if (INTEGERP (prop))
7101 {
7102 *prop_idx = XINT (prop);
7103 success_p = 1;
7104 }
7105 else
7106 success_p = 0;
7107
7108 return success_p;
7109 }
7110
7111 #endif /* HAVE_WINDOW_SYSTEM */
7112
7113
7114 \f
7115 /************************************************************************
7116 Horizontal scrolling
7117 ************************************************************************/
7118
7119 static int hscroll_window_tree P_ ((Lisp_Object));
7120 static int hscroll_windows P_ ((Lisp_Object));
7121
7122 /* For all leaf windows in the window tree rooted at WINDOW, set their
7123 hscroll value so that PT is (i) visible in the window, and (ii) so
7124 that it is not within a certain margin at the window's left and
7125 right border. Value is non-zero if any window's hscroll has been
7126 changed. */
7127
7128 static int
7129 hscroll_window_tree (window)
7130 Lisp_Object window;
7131 {
7132 int hscrolled_p = 0;
7133
7134 while (WINDOWP (window))
7135 {
7136 struct window *w = XWINDOW (window);
7137
7138 if (WINDOWP (w->hchild))
7139 hscrolled_p |= hscroll_window_tree (w->hchild);
7140 else if (WINDOWP (w->vchild))
7141 hscrolled_p |= hscroll_window_tree (w->vchild);
7142 else if (w->cursor.vpos >= 0)
7143 {
7144 int hscroll_margin, text_area_x, text_area_y;
7145 int text_area_width, text_area_height;
7146 struct glyph_row *current_cursor_row
7147 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7148 struct glyph_row *desired_cursor_row
7149 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7150 struct glyph_row *cursor_row
7151 = (desired_cursor_row->enabled_p
7152 ? desired_cursor_row
7153 : current_cursor_row);
7154
7155 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7156 &text_area_width, &text_area_height);
7157
7158 /* Scroll when cursor is inside this scroll margin. */
7159 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7160
7161 if ((XFASTINT (w->hscroll)
7162 && w->cursor.x < hscroll_margin)
7163 || (cursor_row->enabled_p
7164 && cursor_row->truncated_on_right_p
7165 && (w->cursor.x > text_area_width - hscroll_margin)))
7166 {
7167 struct it it;
7168 int hscroll;
7169 struct buffer *saved_current_buffer;
7170 int pt;
7171
7172 /* Find point in a display of infinite width. */
7173 saved_current_buffer = current_buffer;
7174 current_buffer = XBUFFER (w->buffer);
7175
7176 if (w == XWINDOW (selected_window))
7177 pt = BUF_PT (current_buffer);
7178 else
7179 {
7180 pt = marker_position (w->pointm);
7181 pt = max (BEGV, pt);
7182 pt = min (ZV, pt);
7183 }
7184
7185 /* Move iterator to pt starting at cursor_row->start in
7186 a line with infinite width. */
7187 init_to_row_start (&it, w, cursor_row);
7188 it.last_visible_x = INFINITY;
7189 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7190 current_buffer = saved_current_buffer;
7191
7192 /* Center cursor in window. */
7193 hscroll = (max (0, it.current_x - text_area_width / 2)
7194 / CANON_X_UNIT (it.f));
7195
7196 /* Don't call Fset_window_hscroll if value hasn't
7197 changed because it will prevent redisplay
7198 optimizations. */
7199 if (XFASTINT (w->hscroll) != hscroll)
7200 {
7201 Fset_window_hscroll (window, make_number (hscroll));
7202 hscrolled_p = 1;
7203 }
7204 }
7205 }
7206
7207 window = w->next;
7208 }
7209
7210 /* Value is non-zero if hscroll of any leaf window has been changed. */
7211 return hscrolled_p;
7212 }
7213
7214
7215 /* Set hscroll so that cursor is visible and not inside horizontal
7216 scroll margins for all windows in the tree rooted at WINDOW. See
7217 also hscroll_window_tree above. Value is non-zero if any window's
7218 hscroll has been changed. If it has, desired matrices on the frame
7219 of WINDOW are cleared. */
7220
7221 static int
7222 hscroll_windows (window)
7223 Lisp_Object window;
7224 {
7225 int hscrolled_p;
7226
7227 if (automatic_hscrolling_p)
7228 {
7229 hscrolled_p = hscroll_window_tree (window);
7230 if (hscrolled_p)
7231 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7232 }
7233 else
7234 hscrolled_p = 0;
7235 return hscrolled_p;
7236 }
7237
7238
7239 \f
7240 /************************************************************************
7241 Redisplay
7242 ************************************************************************/
7243
7244 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7245 to a non-zero value. This is sometimes handy to have in a debugger
7246 session. */
7247
7248 #if GLYPH_DEBUG
7249
7250 /* First and last unchanged row for try_window_id. */
7251
7252 int debug_first_unchanged_at_end_vpos;
7253 int debug_last_unchanged_at_beg_vpos;
7254
7255 /* Delta vpos and y. */
7256
7257 int debug_dvpos, debug_dy;
7258
7259 /* Delta in characters and bytes for try_window_id. */
7260
7261 int debug_delta, debug_delta_bytes;
7262
7263 /* Values of window_end_pos and window_end_vpos at the end of
7264 try_window_id. */
7265
7266 int debug_end_pos, debug_end_vpos;
7267
7268 /* Append a string to W->desired_matrix->method. FMT is a printf
7269 format string. A1...A9 are a supplement for a variable-length
7270 argument list. If trace_redisplay_p is non-zero also printf the
7271 resulting string to stderr. */
7272
7273 static void
7274 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7275 struct window *w;
7276 char *fmt;
7277 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7278 {
7279 char buffer[512];
7280 char *method = w->desired_matrix->method;
7281 int len = strlen (method);
7282 int size = sizeof w->desired_matrix->method;
7283 int remaining = size - len - 1;
7284
7285 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7286 if (len && remaining)
7287 {
7288 method[len] = '|';
7289 --remaining, ++len;
7290 }
7291
7292 strncpy (method + len, buffer, remaining);
7293
7294 if (trace_redisplay_p)
7295 fprintf (stderr, "%p (%s): %s\n",
7296 w,
7297 ((BUFFERP (w->buffer)
7298 && STRINGP (XBUFFER (w->buffer)->name))
7299 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7300 : "no buffer"),
7301 buffer);
7302 }
7303
7304 #endif /* GLYPH_DEBUG */
7305
7306
7307 /* This counter is used to clear the face cache every once in a while
7308 in redisplay_internal. It is incremented for each redisplay.
7309 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7310 cleared. */
7311
7312 #define CLEAR_FACE_CACHE_COUNT 10000
7313 static int clear_face_cache_count;
7314
7315 /* Record the previous terminal frame we displayed. */
7316
7317 static struct frame *previous_terminal_frame;
7318
7319 /* Non-zero while redisplay_internal is in progress. */
7320
7321 int redisplaying_p;
7322
7323
7324 /* Value is non-zero if all changes in window W, which displays
7325 current_buffer, are in the text between START and END. START is a
7326 buffer position, END is given as a distance from Z. Used in
7327 redisplay_internal for display optimization. */
7328
7329 static INLINE int
7330 text_outside_line_unchanged_p (w, start, end)
7331 struct window *w;
7332 int start, end;
7333 {
7334 int unchanged_p = 1;
7335
7336 /* If text or overlays have changed, see where. */
7337 if (XFASTINT (w->last_modified) < MODIFF
7338 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7339 {
7340 /* Gap in the line? */
7341 if (GPT < start || Z - GPT < end)
7342 unchanged_p = 0;
7343
7344 /* Changes start in front of the line, or end after it? */
7345 if (unchanged_p
7346 && (BEG_UNCHANGED < start - 1
7347 || END_UNCHANGED < end))
7348 unchanged_p = 0;
7349
7350 /* If selective display, can't optimize if changes start at the
7351 beginning of the line. */
7352 if (unchanged_p
7353 && INTEGERP (current_buffer->selective_display)
7354 && XINT (current_buffer->selective_display) > 0
7355 && (BEG_UNCHANGED < start || GPT <= start))
7356 unchanged_p = 0;
7357 }
7358
7359 return unchanged_p;
7360 }
7361
7362
7363 /* Do a frame update, taking possible shortcuts into account. This is
7364 the main external entry point for redisplay.
7365
7366 If the last redisplay displayed an echo area message and that message
7367 is no longer requested, we clear the echo area or bring back the
7368 mini-buffer if that is in use. */
7369
7370 void
7371 redisplay ()
7372 {
7373 redisplay_internal (0);
7374 }
7375
7376 /* Return 1 if point moved out of or into a composition. Otherwise
7377 return 0. PREV_BUF and PREV_PT are the last point buffer and
7378 position. BUF and PT are the current point buffer and position. */
7379
7380 int
7381 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7382 struct buffer *prev_buf, *buf;
7383 int prev_pt, pt;
7384 {
7385 int start, end;
7386 Lisp_Object prop;
7387 Lisp_Object buffer;
7388
7389 XSETBUFFER (buffer, buf);
7390 /* Check a composition at the last point if point moved within the
7391 same buffer. */
7392 if (prev_buf == buf)
7393 {
7394 if (prev_pt == pt)
7395 /* Point didn't move. */
7396 return 0;
7397
7398 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7399 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7400 && COMPOSITION_VALID_P (start, end, prop)
7401 && start < prev_pt && end > prev_pt)
7402 /* The last point was within the composition. Return 1 iff
7403 point moved out of the composition. */
7404 return (pt <= start || pt >= end);
7405 }
7406
7407 /* Check a composition at the current point. */
7408 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7409 && find_composition (pt, -1, &start, &end, &prop, buffer)
7410 && COMPOSITION_VALID_P (start, end, prop)
7411 && start < pt && end > pt);
7412 }
7413
7414 /* Reconsider the setting of B->clip_changed which is displayed
7415 in window W. */
7416
7417 static INLINE void
7418 reconsider_clip_changes (w, b)
7419 struct window *w;
7420 struct buffer *b;
7421 {
7422 if (b->prevent_redisplay_optimizations_p)
7423 b->clip_changed = 1;
7424 else if (b->clip_changed
7425 && !NILP (w->window_end_valid)
7426 && w->current_matrix->buffer == b
7427 && w->current_matrix->zv == BUF_ZV (b)
7428 && w->current_matrix->begv == BUF_BEGV (b))
7429 b->clip_changed = 0;
7430
7431 /* If display wasn't paused, and W is not a tool bar window, see if
7432 point has been moved into or out of a composition. In that case,
7433 we set b->clip_changed to 1 to force updating the screen. If
7434 b->clip_changed has already been set to 1, we can skip this
7435 check. */
7436 if (!b->clip_changed
7437 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7438 {
7439 int pt;
7440
7441 if (w == XWINDOW (selected_window))
7442 pt = BUF_PT (current_buffer);
7443 else
7444 pt = marker_position (w->pointm);
7445
7446 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7447 || pt != XINT (w->last_point))
7448 && check_point_in_composition (w->current_matrix->buffer,
7449 XINT (w->last_point),
7450 XBUFFER (w->buffer), pt))
7451 b->clip_changed = 1;
7452 }
7453 }
7454
7455
7456 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7457 response to any user action; therefore, we should preserve the echo
7458 area. (Actually, our caller does that job.) Perhaps in the future
7459 avoid recentering windows if it is not necessary; currently that
7460 causes some problems. */
7461
7462 static void
7463 redisplay_internal (preserve_echo_area)
7464 int preserve_echo_area;
7465 {
7466 struct window *w = XWINDOW (selected_window);
7467 struct frame *f = XFRAME (w->frame);
7468 int pause;
7469 int must_finish = 0;
7470 struct text_pos tlbufpos, tlendpos;
7471 int number_of_visible_frames;
7472 int count;
7473 struct frame *sf = SELECTED_FRAME ();
7474
7475 /* Non-zero means redisplay has to consider all windows on all
7476 frames. Zero means, only selected_window is considered. */
7477 int consider_all_windows_p;
7478
7479 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7480
7481 /* No redisplay if running in batch mode or frame is not yet fully
7482 initialized, or redisplay is explicitly turned off by setting
7483 Vinhibit_redisplay. */
7484 if (noninteractive
7485 || !NILP (Vinhibit_redisplay)
7486 || !f->glyphs_initialized_p)
7487 return;
7488
7489 /* The flag redisplay_performed_directly_p is set by
7490 direct_output_for_insert when it already did the whole screen
7491 update necessary. */
7492 if (redisplay_performed_directly_p)
7493 {
7494 redisplay_performed_directly_p = 0;
7495 if (!hscroll_windows (selected_window))
7496 return;
7497 }
7498
7499 #ifdef USE_X_TOOLKIT
7500 if (popup_activated ())
7501 return;
7502 #endif
7503
7504 /* I don't think this happens but let's be paranoid. */
7505 if (redisplaying_p)
7506 return;
7507
7508 /* Record a function that resets redisplaying_p to its old value
7509 when we leave this function. */
7510 count = specpdl_ptr - specpdl;
7511 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7512 ++redisplaying_p;
7513
7514 retry:
7515 pause = 0;
7516 reconsider_clip_changes (w, current_buffer);
7517
7518 /* If new fonts have been loaded that make a glyph matrix adjustment
7519 necessary, do it. */
7520 if (fonts_changed_p)
7521 {
7522 adjust_glyphs (NULL);
7523 ++windows_or_buffers_changed;
7524 fonts_changed_p = 0;
7525 }
7526
7527 if (! FRAME_WINDOW_P (sf)
7528 && previous_terminal_frame != sf)
7529 {
7530 /* Since frames on an ASCII terminal share the same display
7531 area, displaying a different frame means redisplay the whole
7532 thing. */
7533 windows_or_buffers_changed++;
7534 SET_FRAME_GARBAGED (sf);
7535 XSETFRAME (Vterminal_frame, sf);
7536 }
7537 previous_terminal_frame = sf;
7538
7539 /* Set the visible flags for all frames. Do this before checking
7540 for resized or garbaged frames; they want to know if their frames
7541 are visible. See the comment in frame.h for
7542 FRAME_SAMPLE_VISIBILITY. */
7543 {
7544 Lisp_Object tail, frame;
7545
7546 number_of_visible_frames = 0;
7547
7548 FOR_EACH_FRAME (tail, frame)
7549 {
7550 struct frame *f = XFRAME (frame);
7551
7552 FRAME_SAMPLE_VISIBILITY (f);
7553 if (FRAME_VISIBLE_P (f))
7554 ++number_of_visible_frames;
7555 clear_desired_matrices (f);
7556 }
7557 }
7558
7559 /* Notice any pending interrupt request to change frame size. */
7560 do_pending_window_change (1);
7561
7562 /* Clear frames marked as garbaged. */
7563 if (frame_garbaged)
7564 clear_garbaged_frames ();
7565
7566 /* Build menubar and tool-bar items. */
7567 prepare_menu_bars ();
7568
7569 if (windows_or_buffers_changed)
7570 update_mode_lines++;
7571
7572 /* Detect case that we need to write or remove a star in the mode line. */
7573 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7574 {
7575 w->update_mode_line = Qt;
7576 if (buffer_shared > 1)
7577 update_mode_lines++;
7578 }
7579
7580 /* If %c is in the mode line, update it if needed. */
7581 if (!NILP (w->column_number_displayed)
7582 /* This alternative quickly identifies a common case
7583 where no change is needed. */
7584 && !(PT == XFASTINT (w->last_point)
7585 && XFASTINT (w->last_modified) >= MODIFF
7586 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7587 && XFASTINT (w->column_number_displayed) != current_column ())
7588 w->update_mode_line = Qt;
7589
7590 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7591
7592 /* The variable buffer_shared is set in redisplay_window and
7593 indicates that we redisplay a buffer in different windows. See
7594 there. */
7595 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7596
7597 /* If specs for an arrow have changed, do thorough redisplay
7598 to ensure we remove any arrow that should no longer exist. */
7599 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7600 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7601 consider_all_windows_p = windows_or_buffers_changed = 1;
7602
7603 /* Normally the message* functions will have already displayed and
7604 updated the echo area, but the frame may have been trashed, or
7605 the update may have been preempted, so display the echo area
7606 again here. Checking both message buffers captures the case that
7607 the echo area should be cleared. */
7608 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7609 {
7610 int window_height_changed_p = echo_area_display (0);
7611 must_finish = 1;
7612
7613 if (fonts_changed_p)
7614 goto retry;
7615 else if (window_height_changed_p)
7616 {
7617 consider_all_windows_p = 1;
7618 ++update_mode_lines;
7619 ++windows_or_buffers_changed;
7620
7621 /* If window configuration was changed, frames may have been
7622 marked garbaged. Clear them or we will experience
7623 surprises wrt scrolling. */
7624 if (frame_garbaged)
7625 clear_garbaged_frames ();
7626 }
7627 }
7628 else if (EQ (selected_window, minibuf_window)
7629 && (current_buffer->clip_changed
7630 || XFASTINT (w->last_modified) < MODIFF
7631 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7632 && resize_mini_window (w, 0))
7633 {
7634 /* Resized active mini-window to fit the size of what it is
7635 showing if its contents might have changed. */
7636 must_finish = 1;
7637 consider_all_windows_p = 1;
7638 ++windows_or_buffers_changed;
7639 ++update_mode_lines;
7640
7641 /* If window configuration was changed, frames may have been
7642 marked garbaged. Clear them or we will experience
7643 surprises wrt scrolling. */
7644 if (frame_garbaged)
7645 clear_garbaged_frames ();
7646 }
7647
7648
7649 /* If showing the region, and mark has changed, we must redisplay
7650 the whole window. The assignment to this_line_start_pos prevents
7651 the optimization directly below this if-statement. */
7652 if (((!NILP (Vtransient_mark_mode)
7653 && !NILP (XBUFFER (w->buffer)->mark_active))
7654 != !NILP (w->region_showing))
7655 || (!NILP (w->region_showing)
7656 && !EQ (w->region_showing,
7657 Fmarker_position (XBUFFER (w->buffer)->mark))))
7658 CHARPOS (this_line_start_pos) = 0;
7659
7660 /* Optimize the case that only the line containing the cursor in the
7661 selected window has changed. Variables starting with this_ are
7662 set in display_line and record information about the line
7663 containing the cursor. */
7664 tlbufpos = this_line_start_pos;
7665 tlendpos = this_line_end_pos;
7666 if (!consider_all_windows_p
7667 && CHARPOS (tlbufpos) > 0
7668 && NILP (w->update_mode_line)
7669 && !current_buffer->clip_changed
7670 && FRAME_VISIBLE_P (XFRAME (w->frame))
7671 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7672 /* Make sure recorded data applies to current buffer, etc. */
7673 && this_line_buffer == current_buffer
7674 && current_buffer == XBUFFER (w->buffer)
7675 && NILP (w->force_start)
7676 /* Point must be on the line that we have info recorded about. */
7677 && PT >= CHARPOS (tlbufpos)
7678 && PT <= Z - CHARPOS (tlendpos)
7679 /* All text outside that line, including its final newline,
7680 must be unchanged */
7681 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7682 CHARPOS (tlendpos)))
7683 {
7684 if (CHARPOS (tlbufpos) > BEGV
7685 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7686 && (CHARPOS (tlbufpos) == ZV
7687 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7688 /* Former continuation line has disappeared by becoming empty */
7689 goto cancel;
7690 else if (XFASTINT (w->last_modified) < MODIFF
7691 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7692 || MINI_WINDOW_P (w))
7693 {
7694 /* We have to handle the case of continuation around a
7695 wide-column character (See the comment in indent.c around
7696 line 885).
7697
7698 For instance, in the following case:
7699
7700 -------- Insert --------
7701 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7702 J_I_ ==> J_I_ `^^' are cursors.
7703 ^^ ^^
7704 -------- --------
7705
7706 As we have to redraw the line above, we should goto cancel. */
7707
7708 struct it it;
7709 int line_height_before = this_line_pixel_height;
7710
7711 /* Note that start_display will handle the case that the
7712 line starting at tlbufpos is a continuation lines. */
7713 start_display (&it, w, tlbufpos);
7714
7715 /* Implementation note: It this still necessary? */
7716 if (it.current_x != this_line_start_x)
7717 goto cancel;
7718
7719 TRACE ((stderr, "trying display optimization 1\n"));
7720 w->cursor.vpos = -1;
7721 overlay_arrow_seen = 0;
7722 it.vpos = this_line_vpos;
7723 it.current_y = this_line_y;
7724 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7725 display_line (&it);
7726
7727 /* If line contains point, is not continued,
7728 and ends at same distance from eob as before, we win */
7729 if (w->cursor.vpos >= 0
7730 /* Line is not continued, otherwise this_line_start_pos
7731 would have been set to 0 in display_line. */
7732 && CHARPOS (this_line_start_pos)
7733 /* Line ends as before. */
7734 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7735 /* Line has same height as before. Otherwise other lines
7736 would have to be shifted up or down. */
7737 && this_line_pixel_height == line_height_before)
7738 {
7739 /* If this is not the window's last line, we must adjust
7740 the charstarts of the lines below. */
7741 if (it.current_y < it.last_visible_y)
7742 {
7743 struct glyph_row *row
7744 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7745 int delta, delta_bytes;
7746
7747 if (Z - CHARPOS (tlendpos) == ZV)
7748 {
7749 /* This line ends at end of (accessible part of)
7750 buffer. There is no newline to count. */
7751 delta = (Z
7752 - CHARPOS (tlendpos)
7753 - MATRIX_ROW_START_CHARPOS (row));
7754 delta_bytes = (Z_BYTE
7755 - BYTEPOS (tlendpos)
7756 - MATRIX_ROW_START_BYTEPOS (row));
7757 }
7758 else
7759 {
7760 /* This line ends in a newline. Must take
7761 account of the newline and the rest of the
7762 text that follows. */
7763 delta = (Z
7764 - CHARPOS (tlendpos)
7765 - MATRIX_ROW_START_CHARPOS (row));
7766 delta_bytes = (Z_BYTE
7767 - BYTEPOS (tlendpos)
7768 - MATRIX_ROW_START_BYTEPOS (row));
7769 }
7770
7771 increment_matrix_positions (w->current_matrix,
7772 this_line_vpos + 1,
7773 w->current_matrix->nrows,
7774 delta, delta_bytes);
7775 }
7776
7777 /* If this row displays text now but previously didn't,
7778 or vice versa, w->window_end_vpos may have to be
7779 adjusted. */
7780 if ((it.glyph_row - 1)->displays_text_p)
7781 {
7782 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7783 XSETINT (w->window_end_vpos, this_line_vpos);
7784 }
7785 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7786 && this_line_vpos > 0)
7787 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7788 w->window_end_valid = Qnil;
7789
7790 /* Update hint: No need to try to scroll in update_window. */
7791 w->desired_matrix->no_scrolling_p = 1;
7792
7793 #if GLYPH_DEBUG
7794 *w->desired_matrix->method = 0;
7795 debug_method_add (w, "optimization 1");
7796 #endif
7797 goto update;
7798 }
7799 else
7800 goto cancel;
7801 }
7802 else if (/* Cursor position hasn't changed. */
7803 PT == XFASTINT (w->last_point)
7804 /* Make sure the cursor was last displayed
7805 in this window. Otherwise we have to reposition it. */
7806 && 0 <= w->cursor.vpos
7807 && XINT (w->height) > w->cursor.vpos)
7808 {
7809 if (!must_finish)
7810 {
7811 do_pending_window_change (1);
7812
7813 /* We used to always goto end_of_redisplay here, but this
7814 isn't enough if we have a blinking cursor. */
7815 if (w->cursor_off_p == w->last_cursor_off_p)
7816 goto end_of_redisplay;
7817 }
7818 goto update;
7819 }
7820 /* If highlighting the region, or if the cursor is in the echo area,
7821 then we can't just move the cursor. */
7822 else if (! (!NILP (Vtransient_mark_mode)
7823 && !NILP (current_buffer->mark_active))
7824 && (EQ (selected_window, current_buffer->last_selected_window)
7825 || highlight_nonselected_windows)
7826 && NILP (w->region_showing)
7827 && NILP (Vshow_trailing_whitespace)
7828 && !cursor_in_echo_area)
7829 {
7830 struct it it;
7831 struct glyph_row *row;
7832
7833 /* Skip from tlbufpos to PT and see where it is. Note that
7834 PT may be in invisible text. If so, we will end at the
7835 next visible position. */
7836 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7837 NULL, DEFAULT_FACE_ID);
7838 it.current_x = this_line_start_x;
7839 it.current_y = this_line_y;
7840 it.vpos = this_line_vpos;
7841
7842 /* The call to move_it_to stops in front of PT, but
7843 moves over before-strings. */
7844 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7845
7846 if (it.vpos == this_line_vpos
7847 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7848 row->enabled_p))
7849 {
7850 xassert (this_line_vpos == it.vpos);
7851 xassert (this_line_y == it.current_y);
7852 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7853 goto update;
7854 }
7855 else
7856 goto cancel;
7857 }
7858
7859 cancel:
7860 /* Text changed drastically or point moved off of line. */
7861 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7862 }
7863
7864 CHARPOS (this_line_start_pos) = 0;
7865 consider_all_windows_p |= buffer_shared > 1;
7866 ++clear_face_cache_count;
7867
7868
7869 /* Build desired matrices, and update the display. If
7870 consider_all_windows_p is non-zero, do it for all windows on all
7871 frames. Otherwise do it for selected_window, only. */
7872
7873 if (consider_all_windows_p)
7874 {
7875 Lisp_Object tail, frame;
7876
7877 /* Clear the face cache eventually. */
7878 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
7879 {
7880 clear_face_cache (0);
7881 clear_face_cache_count = 0;
7882 }
7883
7884 /* Recompute # windows showing selected buffer. This will be
7885 incremented each time such a window is displayed. */
7886 buffer_shared = 0;
7887
7888 FOR_EACH_FRAME (tail, frame)
7889 {
7890 struct frame *f = XFRAME (frame);
7891
7892 if (FRAME_WINDOW_P (f) || f == sf)
7893 {
7894 /* Mark all the scroll bars to be removed; we'll redeem
7895 the ones we want when we redisplay their windows. */
7896 if (condemn_scroll_bars_hook)
7897 (*condemn_scroll_bars_hook) (f);
7898
7899 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7900 redisplay_windows (FRAME_ROOT_WINDOW (f));
7901
7902 /* Any scroll bars which redisplay_windows should have
7903 nuked should now go away. */
7904 if (judge_scroll_bars_hook)
7905 (*judge_scroll_bars_hook) (f);
7906
7907 /* If fonts changed, display again. */
7908 if (fonts_changed_p)
7909 goto retry;
7910
7911 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
7912 {
7913 /* See if we have to hscroll. */
7914 if (hscroll_windows (f->root_window))
7915 goto retry;
7916
7917 /* Prevent various kinds of signals during display
7918 update. stdio is not robust about handling
7919 signals, which can cause an apparent I/O
7920 error. */
7921 if (interrupt_input)
7922 unrequest_sigio ();
7923 stop_polling ();
7924
7925 /* Update the display. */
7926 set_window_update_flags (XWINDOW (f->root_window), 1);
7927 pause |= update_frame (f, 0, 0);
7928 if (pause)
7929 break;
7930
7931 mark_window_display_accurate (f->root_window, 1);
7932 if (frame_up_to_date_hook)
7933 frame_up_to_date_hook (f);
7934 }
7935 }
7936 }
7937 }
7938 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
7939 {
7940 Lisp_Object mini_window;
7941 struct frame *mini_frame;
7942
7943 redisplay_window (selected_window, 1);
7944
7945 /* Compare desired and current matrices, perform output. */
7946 update:
7947
7948 /* If fonts changed, display again. */
7949 if (fonts_changed_p)
7950 goto retry;
7951
7952 /* Prevent various kinds of signals during display update.
7953 stdio is not robust about handling signals,
7954 which can cause an apparent I/O error. */
7955 if (interrupt_input)
7956 unrequest_sigio ();
7957 stop_polling ();
7958
7959 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
7960 {
7961 if (hscroll_windows (selected_window))
7962 goto retry;
7963
7964 XWINDOW (selected_window)->must_be_updated_p = 1;
7965 pause = update_frame (sf, 0, 0);
7966 }
7967
7968 /* We may have called echo_area_display at the top of this
7969 function. If the echo area is on another frame, that may
7970 have put text on a frame other than the selected one, so the
7971 above call to update_frame would not have caught it. Catch
7972 it here. */
7973 mini_window = FRAME_MINIBUF_WINDOW (sf);
7974 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7975
7976 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
7977 {
7978 XWINDOW (mini_window)->must_be_updated_p = 1;
7979 pause |= update_frame (mini_frame, 0, 0);
7980 if (!pause && hscroll_windows (mini_window))
7981 goto retry;
7982 }
7983 }
7984
7985 /* If display was paused because of pending input, make sure we do a
7986 thorough update the next time. */
7987 if (pause)
7988 {
7989 /* Prevent the optimization at the beginning of
7990 redisplay_internal that tries a single-line update of the
7991 line containing the cursor in the selected window. */
7992 CHARPOS (this_line_start_pos) = 0;
7993
7994 /* Let the overlay arrow be updated the next time. */
7995 if (!NILP (last_arrow_position))
7996 {
7997 last_arrow_position = Qt;
7998 last_arrow_string = Qt;
7999 }
8000
8001 /* If we pause after scrolling, some rows in the current
8002 matrices of some windows are not valid. */
8003 if (!WINDOW_FULL_WIDTH_P (w)
8004 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8005 update_mode_lines = 1;
8006 }
8007
8008 /* Now text on frame agrees with windows, so put info into the
8009 windows for partial redisplay to follow. */
8010 if (!pause)
8011 {
8012 register struct buffer *b = XBUFFER (w->buffer);
8013
8014 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8015 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8016 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8017 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8018
8019 if (consider_all_windows_p)
8020 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8021 else
8022 {
8023 XSETFASTINT (w->last_point, BUF_PT (b));
8024 w->last_cursor = w->cursor;
8025 w->last_cursor_off_p = w->cursor_off_p;
8026
8027 b->clip_changed = 0;
8028 b->prevent_redisplay_optimizations_p = 0;
8029 w->update_mode_line = Qnil;
8030 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8031 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8032 w->last_had_star
8033 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8034 ? Qt : Qnil);
8035
8036 /* Record if we are showing a region, so can make sure to
8037 update it fully at next redisplay. */
8038 w->region_showing = (!NILP (Vtransient_mark_mode)
8039 && (EQ (selected_window,
8040 current_buffer->last_selected_window)
8041 || highlight_nonselected_windows)
8042 && !NILP (XBUFFER (w->buffer)->mark_active)
8043 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8044 : Qnil);
8045
8046 w->window_end_valid = w->buffer;
8047 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8048 last_arrow_string = Voverlay_arrow_string;
8049 if (frame_up_to_date_hook != 0)
8050 (*frame_up_to_date_hook) (sf);
8051
8052 w->current_matrix->buffer = b;
8053 w->current_matrix->begv = BUF_BEGV (b);
8054 w->current_matrix->zv = BUF_ZV (b);
8055 }
8056
8057 update_mode_lines = 0;
8058 windows_or_buffers_changed = 0;
8059 }
8060
8061 /* Start SIGIO interrupts coming again. Having them off during the
8062 code above makes it less likely one will discard output, but not
8063 impossible, since there might be stuff in the system buffer here.
8064 But it is much hairier to try to do anything about that. */
8065 if (interrupt_input)
8066 request_sigio ();
8067 start_polling ();
8068
8069 /* If a frame has become visible which was not before, redisplay
8070 again, so that we display it. Expose events for such a frame
8071 (which it gets when becoming visible) don't call the parts of
8072 redisplay constructing glyphs, so simply exposing a frame won't
8073 display anything in this case. So, we have to display these
8074 frames here explicitly. */
8075 if (!pause)
8076 {
8077 Lisp_Object tail, frame;
8078 int new_count = 0;
8079
8080 FOR_EACH_FRAME (tail, frame)
8081 {
8082 int this_is_visible = 0;
8083
8084 if (XFRAME (frame)->visible)
8085 this_is_visible = 1;
8086 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8087 if (XFRAME (frame)->visible)
8088 this_is_visible = 1;
8089
8090 if (this_is_visible)
8091 new_count++;
8092 }
8093
8094 if (new_count != number_of_visible_frames)
8095 windows_or_buffers_changed++;
8096 }
8097
8098 /* Change frame size now if a change is pending. */
8099 do_pending_window_change (1);
8100
8101 /* If we just did a pending size change, or have additional
8102 visible frames, redisplay again. */
8103 if (windows_or_buffers_changed && !pause)
8104 goto retry;
8105
8106 end_of_redisplay:;
8107
8108 unbind_to (count, Qnil);
8109 }
8110
8111
8112 /* Redisplay, but leave alone any recent echo area message unless
8113 another message has been requested in its place.
8114
8115 This is useful in situations where you need to redisplay but no
8116 user action has occurred, making it inappropriate for the message
8117 area to be cleared. See tracking_off and
8118 wait_reading_process_input for examples of these situations. */
8119
8120 void
8121 redisplay_preserve_echo_area ()
8122 {
8123 if (!NILP (echo_area_buffer[1]))
8124 {
8125 /* We have a previously displayed message, but no current
8126 message. Redisplay the previous message. */
8127 display_last_displayed_message_p = 1;
8128 redisplay_internal (1);
8129 display_last_displayed_message_p = 0;
8130 }
8131 else
8132 redisplay_internal (1);
8133 }
8134
8135
8136 /* Function registered with record_unwind_protect in
8137 redisplay_internal. Clears the flag indicating that a redisplay is
8138 in progress. */
8139
8140 static Lisp_Object
8141 unwind_redisplay (old_redisplaying_p)
8142 Lisp_Object old_redisplaying_p;
8143 {
8144 redisplaying_p = XFASTINT (old_redisplaying_p);
8145 return Qnil;
8146 }
8147
8148
8149 /* Mark the display of windows in the window tree rooted at WINDOW as
8150 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8151 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8152 the next time redisplay_internal is called. */
8153
8154 void
8155 mark_window_display_accurate (window, accurate_p)
8156 Lisp_Object window;
8157 int accurate_p;
8158 {
8159 struct window *w;
8160
8161 for (; !NILP (window); window = w->next)
8162 {
8163 w = XWINDOW (window);
8164
8165 if (BUFFERP (w->buffer))
8166 {
8167 struct buffer *b = XBUFFER (w->buffer);
8168
8169 XSETFASTINT (w->last_modified,
8170 accurate_p ? BUF_MODIFF (b) : 0);
8171 XSETFASTINT (w->last_overlay_modified,
8172 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8173 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8174 ? Qt : Qnil);
8175
8176 #if 0 /* I don't think this is necessary because display_line does it.
8177 Let's check it. */
8178 /* Record if we are showing a region, so can make sure to
8179 update it fully at next redisplay. */
8180 w->region_showing
8181 = (!NILP (Vtransient_mark_mode)
8182 && (w == XWINDOW (current_buffer->last_selected_window)
8183 || highlight_nonselected_windows)
8184 && (!NILP (b->mark_active)
8185 ? Fmarker_position (b->mark)
8186 : Qnil));
8187 #endif
8188
8189 if (accurate_p)
8190 {
8191 b->clip_changed = 0;
8192 b->prevent_redisplay_optimizations_p = 0;
8193 w->current_matrix->buffer = b;
8194 w->current_matrix->begv = BUF_BEGV (b);
8195 w->current_matrix->zv = BUF_ZV (b);
8196 w->last_cursor = w->cursor;
8197 w->last_cursor_off_p = w->cursor_off_p;
8198 if (w == XWINDOW (selected_window))
8199 w->last_point = make_number (BUF_PT (b));
8200 else
8201 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8202 }
8203 }
8204
8205 w->window_end_valid = w->buffer;
8206 w->update_mode_line = Qnil;
8207
8208 if (!NILP (w->vchild))
8209 mark_window_display_accurate (w->vchild, accurate_p);
8210 if (!NILP (w->hchild))
8211 mark_window_display_accurate (w->hchild, accurate_p);
8212 }
8213
8214 if (accurate_p)
8215 {
8216 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8217 last_arrow_string = Voverlay_arrow_string;
8218 }
8219 else
8220 {
8221 /* Force a thorough redisplay the next time by setting
8222 last_arrow_position and last_arrow_string to t, which is
8223 unequal to any useful value of Voverlay_arrow_... */
8224 last_arrow_position = Qt;
8225 last_arrow_string = Qt;
8226 }
8227 }
8228
8229
8230 /* Return value in display table DP (Lisp_Char_Table *) for character
8231 C. Since a display table doesn't have any parent, we don't have to
8232 follow parent. Do not call this function directly but use the
8233 macro DISP_CHAR_VECTOR. */
8234
8235 Lisp_Object
8236 disp_char_vector (dp, c)
8237 struct Lisp_Char_Table *dp;
8238 int c;
8239 {
8240 int code[4], i;
8241 Lisp_Object val;
8242
8243 if (SINGLE_BYTE_CHAR_P (c))
8244 return (dp->contents[c]);
8245
8246 SPLIT_CHAR (c, code[0], code[1], code[2]);
8247 if (code[1] < 32)
8248 code[1] = -1;
8249 else if (code[2] < 32)
8250 code[2] = -1;
8251
8252 /* Here, the possible range of code[0] (== charset ID) is
8253 128..max_charset. Since the top level char table contains data
8254 for multibyte characters after 256th element, we must increment
8255 code[0] by 128 to get a correct index. */
8256 code[0] += 128;
8257 code[3] = -1; /* anchor */
8258
8259 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8260 {
8261 val = dp->contents[code[i]];
8262 if (!SUB_CHAR_TABLE_P (val))
8263 return (NILP (val) ? dp->defalt : val);
8264 }
8265
8266 /* Here, val is a sub char table. We return the default value of
8267 it. */
8268 return (dp->defalt);
8269 }
8270
8271
8272 \f
8273 /***********************************************************************
8274 Window Redisplay
8275 ***********************************************************************/
8276
8277 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8278
8279 static void
8280 redisplay_windows (window)
8281 Lisp_Object window;
8282 {
8283 while (!NILP (window))
8284 {
8285 struct window *w = XWINDOW (window);
8286
8287 if (!NILP (w->hchild))
8288 redisplay_windows (w->hchild);
8289 else if (!NILP (w->vchild))
8290 redisplay_windows (w->vchild);
8291 else
8292 redisplay_window (window, 0);
8293
8294 window = w->next;
8295 }
8296 }
8297
8298
8299 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8300 DELTA is the number of bytes by which positions recorded in ROW
8301 differ from current buffer positions. */
8302
8303 void
8304 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8305 struct window *w;
8306 struct glyph_row *row;
8307 struct glyph_matrix *matrix;
8308 int delta, delta_bytes, dy, dvpos;
8309 {
8310 struct glyph *glyph = row->glyphs[TEXT_AREA];
8311 struct glyph *end = glyph + row->used[TEXT_AREA];
8312 int x = row->x;
8313 int pt_old = PT - delta;
8314
8315 /* Skip over glyphs not having an object at the start of the row.
8316 These are special glyphs like truncation marks on terminal
8317 frames. */
8318 if (row->displays_text_p)
8319 while (glyph < end
8320 && INTEGERP (glyph->object)
8321 && glyph->charpos < 0)
8322 {
8323 x += glyph->pixel_width;
8324 ++glyph;
8325 }
8326
8327 while (glyph < end
8328 && !INTEGERP (glyph->object)
8329 && (!BUFFERP (glyph->object)
8330 || glyph->charpos < pt_old))
8331 {
8332 x += glyph->pixel_width;
8333 ++glyph;
8334 }
8335
8336 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8337 w->cursor.x = x;
8338 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8339 w->cursor.y = row->y + dy;
8340
8341 if (w == XWINDOW (selected_window))
8342 {
8343 if (!row->continued_p
8344 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8345 && row->x == 0)
8346 {
8347 this_line_buffer = XBUFFER (w->buffer);
8348
8349 CHARPOS (this_line_start_pos)
8350 = MATRIX_ROW_START_CHARPOS (row) + delta;
8351 BYTEPOS (this_line_start_pos)
8352 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8353
8354 CHARPOS (this_line_end_pos)
8355 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8356 BYTEPOS (this_line_end_pos)
8357 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8358
8359 this_line_y = w->cursor.y;
8360 this_line_pixel_height = row->height;
8361 this_line_vpos = w->cursor.vpos;
8362 this_line_start_x = row->x;
8363 }
8364 else
8365 CHARPOS (this_line_start_pos) = 0;
8366 }
8367 }
8368
8369
8370 /* Run window scroll functions, if any, for WINDOW with new window
8371 start STARTP. Sets the window start of WINDOW to that position.
8372
8373 We assume that the window's buffer is really current. */
8374
8375 static INLINE struct text_pos
8376 run_window_scroll_functions (window, startp)
8377 Lisp_Object window;
8378 struct text_pos startp;
8379 {
8380 struct window *w = XWINDOW (window);
8381 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8382
8383 if (current_buffer != XBUFFER (w->buffer))
8384 abort ();
8385
8386 if (!NILP (Vwindow_scroll_functions))
8387 {
8388 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8389 make_number (CHARPOS (startp)));
8390 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8391 /* In case the hook functions switch buffers. */
8392 if (current_buffer != XBUFFER (w->buffer))
8393 set_buffer_internal_1 (XBUFFER (w->buffer));
8394 }
8395
8396 return startp;
8397 }
8398
8399
8400 /* Modify the desired matrix of window W and W->vscroll so that the
8401 line containing the cursor is fully visible. */
8402
8403 static void
8404 make_cursor_line_fully_visible (w)
8405 struct window *w;
8406 {
8407 struct glyph_matrix *matrix;
8408 struct glyph_row *row;
8409 int window_height, header_line_height;
8410
8411 /* It's not always possible to find the cursor, e.g, when a window
8412 is full of overlay strings. Don't do anything in that case. */
8413 if (w->cursor.vpos < 0)
8414 return;
8415
8416 matrix = w->desired_matrix;
8417 row = MATRIX_ROW (matrix, w->cursor.vpos);
8418
8419 /* If the cursor row is not partially visible, there's nothing
8420 to do. */
8421 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8422 return;
8423
8424 /* If the row the cursor is in is taller than the window's height,
8425 it's not clear what to do, so do nothing. */
8426 window_height = window_box_height (w);
8427 if (row->height >= window_height)
8428 return;
8429
8430 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8431 {
8432 int dy = row->height - row->visible_height;
8433 w->vscroll = 0;
8434 w->cursor.y += dy;
8435 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8436 }
8437 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8438 {
8439 int dy = - (row->height - row->visible_height);
8440 w->vscroll = dy;
8441 w->cursor.y += dy;
8442 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8443 }
8444
8445 /* When we change the cursor y-position of the selected window,
8446 change this_line_y as well so that the display optimization for
8447 the cursor line of the selected window in redisplay_internal uses
8448 the correct y-position. */
8449 if (w == XWINDOW (selected_window))
8450 this_line_y = w->cursor.y;
8451 }
8452
8453
8454 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8455 non-zero means only WINDOW is redisplayed in redisplay_internal.
8456 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8457 in redisplay_window to bring a partially visible line into view in
8458 the case that only the cursor has moved.
8459
8460 Value is
8461
8462 1 if scrolling succeeded
8463
8464 0 if scrolling didn't find point.
8465
8466 -1 if new fonts have been loaded so that we must interrupt
8467 redisplay, adjust glyph matrices, and try again. */
8468
8469 static int
8470 try_scrolling (window, just_this_one_p, scroll_conservatively,
8471 scroll_step, temp_scroll_step)
8472 Lisp_Object window;
8473 int just_this_one_p;
8474 int scroll_conservatively, scroll_step;
8475 int temp_scroll_step;
8476 {
8477 struct window *w = XWINDOW (window);
8478 struct frame *f = XFRAME (w->frame);
8479 struct text_pos scroll_margin_pos;
8480 struct text_pos pos;
8481 struct text_pos startp;
8482 struct it it;
8483 Lisp_Object window_end;
8484 int this_scroll_margin;
8485 int dy = 0;
8486 int scroll_max;
8487 int line_height, rc;
8488 int amount_to_scroll = 0;
8489 Lisp_Object aggressive;
8490 int height;
8491
8492 #if GLYPH_DEBUG
8493 debug_method_add (w, "try_scrolling");
8494 #endif
8495
8496 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8497
8498 /* Compute scroll margin height in pixels. We scroll when point is
8499 within this distance from the top or bottom of the window. */
8500 if (scroll_margin > 0)
8501 {
8502 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8503 this_scroll_margin *= CANON_Y_UNIT (f);
8504 }
8505 else
8506 this_scroll_margin = 0;
8507
8508 /* Compute how much we should try to scroll maximally to bring point
8509 into view. */
8510 if (scroll_step)
8511 scroll_max = scroll_step;
8512 else if (scroll_conservatively)
8513 scroll_max = scroll_conservatively;
8514 else if (temp_scroll_step)
8515 scroll_max = temp_scroll_step;
8516 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8517 || NUMBERP (current_buffer->scroll_up_aggressively))
8518 /* We're trying to scroll because of aggressive scrolling
8519 but no scroll_step is set. Choose an arbitrary one. Maybe
8520 there should be a variable for this. */
8521 scroll_max = 10;
8522 else
8523 scroll_max = 0;
8524 scroll_max *= CANON_Y_UNIT (f);
8525
8526 /* Decide whether we have to scroll down. Start at the window end
8527 and move this_scroll_margin up to find the position of the scroll
8528 margin. */
8529 window_end = Fwindow_end (window, Qt);
8530 CHARPOS (scroll_margin_pos) = XINT (window_end);
8531 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8532 if (this_scroll_margin)
8533 {
8534 start_display (&it, w, scroll_margin_pos);
8535 move_it_vertically (&it, - this_scroll_margin);
8536 scroll_margin_pos = it.current.pos;
8537 }
8538
8539 if (PT >= CHARPOS (scroll_margin_pos))
8540 {
8541 int y0;
8542
8543 /* Point is in the scroll margin at the bottom of the window, or
8544 below. Compute a new window start that makes point visible. */
8545
8546 /* Compute the distance from the scroll margin to PT.
8547 Give up if the distance is greater than scroll_max. */
8548 start_display (&it, w, scroll_margin_pos);
8549 y0 = it.current_y;
8550 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8551 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8552 line_height = (it.max_ascent + it.max_descent
8553 ? it.max_ascent + it.max_descent
8554 : last_height);
8555 dy = it.current_y + line_height - y0;
8556
8557 if (dy > scroll_max)
8558 return 0;
8559
8560 /* Move the window start down. If scrolling conservatively,
8561 move it just enough down to make point visible. If
8562 scroll_step is set, move it down by scroll_step. */
8563 start_display (&it, w, startp);
8564
8565 if (scroll_conservatively)
8566 amount_to_scroll = dy;
8567 else if (scroll_step || temp_scroll_step)
8568 amount_to_scroll = scroll_max;
8569 else
8570 {
8571 aggressive = current_buffer->scroll_down_aggressively;
8572 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8573 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8574 if (NUMBERP (aggressive))
8575 amount_to_scroll = XFLOATINT (aggressive) * height;
8576 }
8577
8578 if (amount_to_scroll <= 0)
8579 return 0;
8580
8581 move_it_vertically (&it, amount_to_scroll);
8582 startp = it.current.pos;
8583 }
8584 else
8585 {
8586 /* See if point is inside the scroll margin at the top of the
8587 window. */
8588 scroll_margin_pos = startp;
8589 if (this_scroll_margin)
8590 {
8591 start_display (&it, w, startp);
8592 move_it_vertically (&it, this_scroll_margin);
8593 scroll_margin_pos = it.current.pos;
8594 }
8595
8596 if (PT < CHARPOS (scroll_margin_pos))
8597 {
8598 /* Point is in the scroll margin at the top of the window or
8599 above what is displayed in the window. */
8600 int y0;
8601
8602 /* Compute the vertical distance from PT to the scroll
8603 margin position. Give up if distance is greater than
8604 scroll_max. */
8605 SET_TEXT_POS (pos, PT, PT_BYTE);
8606 start_display (&it, w, pos);
8607 y0 = it.current_y;
8608 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8609 it.last_visible_y, -1,
8610 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8611 dy = it.current_y - y0;
8612 if (dy > scroll_max)
8613 return 0;
8614
8615 /* Compute new window start. */
8616 start_display (&it, w, startp);
8617
8618 if (scroll_conservatively)
8619 amount_to_scroll = dy;
8620 else if (scroll_step || temp_scroll_step)
8621 amount_to_scroll = scroll_max;
8622 else
8623 {
8624 aggressive = current_buffer->scroll_up_aggressively;
8625 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8626 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8627 if (NUMBERP (aggressive))
8628 amount_to_scroll = XFLOATINT (aggressive) * height;
8629 }
8630
8631 if (amount_to_scroll <= 0)
8632 return 0;
8633
8634 move_it_vertically (&it, - amount_to_scroll);
8635 startp = it.current.pos;
8636 }
8637 }
8638
8639 /* Run window scroll functions. */
8640 startp = run_window_scroll_functions (window, startp);
8641
8642 /* Display the window. Give up if new fonts are loaded, or if point
8643 doesn't appear. */
8644 if (!try_window (window, startp))
8645 rc = -1;
8646 else if (w->cursor.vpos < 0)
8647 {
8648 clear_glyph_matrix (w->desired_matrix);
8649 rc = 0;
8650 }
8651 else
8652 {
8653 /* Maybe forget recorded base line for line number display. */
8654 if (!just_this_one_p
8655 || current_buffer->clip_changed
8656 || BEG_UNCHANGED < CHARPOS (startp))
8657 w->base_line_number = Qnil;
8658
8659 /* If cursor ends up on a partially visible line, shift display
8660 lines up or down. */
8661 make_cursor_line_fully_visible (w);
8662 rc = 1;
8663 }
8664
8665 return rc;
8666 }
8667
8668
8669 /* Compute a suitable window start for window W if display of W starts
8670 on a continuation line. Value is non-zero if a new window start
8671 was computed.
8672
8673 The new window start will be computed, based on W's width, starting
8674 from the start of the continued line. It is the start of the
8675 screen line with the minimum distance from the old start W->start. */
8676
8677 static int
8678 compute_window_start_on_continuation_line (w)
8679 struct window *w;
8680 {
8681 struct text_pos pos, start_pos;
8682 int window_start_changed_p = 0;
8683
8684 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8685
8686 /* If window start is on a continuation line... Window start may be
8687 < BEGV in case there's invisible text at the start of the
8688 buffer (M-x rmail, for example). */
8689 if (CHARPOS (start_pos) > BEGV
8690 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8691 {
8692 struct it it;
8693 struct glyph_row *row;
8694
8695 /* Handle the case that the window start is out of range. */
8696 if (CHARPOS (start_pos) < BEGV)
8697 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8698 else if (CHARPOS (start_pos) > ZV)
8699 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8700
8701 /* Find the start of the continued line. This should be fast
8702 because scan_buffer is fast (newline cache). */
8703 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8704 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8705 row, DEFAULT_FACE_ID);
8706 reseat_at_previous_visible_line_start (&it);
8707
8708 /* If the line start is "too far" away from the window start,
8709 say it takes too much time to compute a new window start. */
8710 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8711 < XFASTINT (w->height) * XFASTINT (w->width))
8712 {
8713 int min_distance, distance;
8714
8715 /* Move forward by display lines to find the new window
8716 start. If window width was enlarged, the new start can
8717 be expected to be > the old start. If window width was
8718 decreased, the new window start will be < the old start.
8719 So, we're looking for the display line start with the
8720 minimum distance from the old window start. */
8721 pos = it.current.pos;
8722 min_distance = INFINITY;
8723 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8724 distance < min_distance)
8725 {
8726 min_distance = distance;
8727 pos = it.current.pos;
8728 move_it_by_lines (&it, 1, 0);
8729 }
8730
8731 /* Set the window start there. */
8732 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8733 window_start_changed_p = 1;
8734 }
8735 }
8736
8737 return window_start_changed_p;
8738 }
8739
8740
8741 /* Try cursor movement in case text has not changes in window WINDOW,
8742 with window start STARTP. Value is
8743
8744 1 if successful
8745
8746 0 if this method cannot be used
8747
8748 -1 if we know we have to scroll the display. *SCROLL_STEP is
8749 set to 1, under certain circumstances, if we want to scroll as
8750 if scroll-step were set to 1. See the code. */
8751
8752 static int
8753 try_cursor_movement (window, startp, scroll_step)
8754 Lisp_Object window;
8755 struct text_pos startp;
8756 int *scroll_step;
8757 {
8758 struct window *w = XWINDOW (window);
8759 struct frame *f = XFRAME (w->frame);
8760 int rc = 0;
8761
8762 /* Handle case where text has not changed, only point, and it has
8763 not moved off the frame. */
8764 if (/* Point may be in this window. */
8765 PT >= CHARPOS (startp)
8766 /* If we don't check this, we are called to move the cursor in a
8767 horizontally split window with a current matrix that doesn't
8768 fit the display. */
8769 && !windows_or_buffers_changed
8770 /* Selective display hasn't changed. */
8771 && !current_buffer->clip_changed
8772 /* If force-mode-line-update was called, really redisplay;
8773 that's how redisplay is forced after e.g. changing
8774 buffer-invisibility-spec. */
8775 && NILP (w->update_mode_line)
8776 /* Can't use this case if highlighting a region. When a
8777 region exists, cursor movement has to do more than just
8778 set the cursor. */
8779 && !(!NILP (Vtransient_mark_mode)
8780 && !NILP (current_buffer->mark_active))
8781 && NILP (w->region_showing)
8782 && NILP (Vshow_trailing_whitespace)
8783 /* Right after splitting windows, last_point may be nil. */
8784 && INTEGERP (w->last_point)
8785 /* This code is not used for mini-buffer for the sake of the case
8786 of redisplaying to replace an echo area message; since in
8787 that case the mini-buffer contents per se are usually
8788 unchanged. This code is of no real use in the mini-buffer
8789 since the handling of this_line_start_pos, etc., in redisplay
8790 handles the same cases. */
8791 && !EQ (window, minibuf_window)
8792 /* When splitting windows or for new windows, it happens that
8793 redisplay is called with a nil window_end_vpos or one being
8794 larger than the window. This should really be fixed in
8795 window.c. I don't have this on my list, now, so we do
8796 approximately the same as the old redisplay code. --gerd. */
8797 && INTEGERP (w->window_end_vpos)
8798 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8799 && (FRAME_WINDOW_P (f)
8800 || !MARKERP (Voverlay_arrow_position)
8801 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8802 {
8803 int this_scroll_margin;
8804 struct glyph_row *row;
8805
8806 #if GLYPH_DEBUG
8807 debug_method_add (w, "cursor movement");
8808 #endif
8809
8810 /* Scroll if point within this distance from the top or bottom
8811 of the window. This is a pixel value. */
8812 this_scroll_margin = max (0, scroll_margin);
8813 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8814 this_scroll_margin *= CANON_Y_UNIT (f);
8815
8816 /* Start with the row the cursor was displayed during the last
8817 not paused redisplay. Give up if that row is not valid. */
8818 if (w->last_cursor.vpos < 0
8819 || w->last_cursor.vpos >= w->current_matrix->nrows)
8820 rc = -1;
8821 else
8822 {
8823 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8824 if (row->mode_line_p)
8825 ++row;
8826 if (!row->enabled_p)
8827 rc = -1;
8828 }
8829
8830 if (rc == 0)
8831 {
8832 int scroll_p = 0;
8833
8834 if (PT > XFASTINT (w->last_point))
8835 {
8836 /* Point has moved forward. */
8837 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8838
8839 while (MATRIX_ROW_END_CHARPOS (row) < PT
8840 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8841 {
8842 xassert (row->enabled_p);
8843 ++row;
8844 }
8845
8846 /* The end position of a row equals the start position
8847 of the next row. If PT is there, we would rather
8848 display it in the next line. Exceptions are when the
8849 row ends in the middle of a character, or ends in
8850 ZV. */
8851 if (MATRIX_ROW_BOTTOM_Y (row) < last_y
8852 && MATRIX_ROW_END_CHARPOS (row) == PT
8853 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
8854 && !row->ends_at_zv_p)
8855 {
8856 xassert (row->enabled_p);
8857 ++row;
8858 }
8859
8860 /* If within the scroll margin, scroll. Note that
8861 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
8862 the next line would be drawn, and that
8863 this_scroll_margin can be zero. */
8864 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8865 || PT > MATRIX_ROW_END_CHARPOS (row)
8866 /* Line is completely visible last line in window
8867 and PT is to be set in the next line. */
8868 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
8869 && PT == MATRIX_ROW_END_CHARPOS (row)
8870 && !row->ends_at_zv_p
8871 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
8872 scroll_p = 1;
8873 }
8874 else if (PT < XFASTINT (w->last_point))
8875 {
8876 /* Cursor has to be moved backward. Note that PT >=
8877 CHARPOS (startp) because of the outer
8878 if-statement. */
8879 while (!row->mode_line_p
8880 && (MATRIX_ROW_START_CHARPOS (row) > PT
8881 || (MATRIX_ROW_START_CHARPOS (row) == PT
8882 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
8883 && (row->y > this_scroll_margin
8884 || CHARPOS (startp) == BEGV))
8885 {
8886 xassert (row->enabled_p);
8887 --row;
8888 }
8889
8890 /* Consider the following case: Window starts at BEGV,
8891 there is invisible, intangible text at BEGV, so that
8892 display starts at some point START > BEGV. It can
8893 happen that we are called with PT somewhere between
8894 BEGV and START. Try to handle that case. */
8895 if (row < w->current_matrix->rows
8896 || row->mode_line_p)
8897 {
8898 row = w->current_matrix->rows;
8899 if (row->mode_line_p)
8900 ++row;
8901 }
8902
8903 /* Due to newlines in overlay strings, we may have to
8904 skip forward over overlay strings. */
8905 while (MATRIX_ROW_END_CHARPOS (row) == PT
8906 && MATRIX_ROW_ENDS_IN_OVERLAY_STRING_P (row)
8907 && !row->ends_at_zv_p)
8908 ++row;
8909
8910 /* If within the scroll margin, scroll. */
8911 if (row->y < this_scroll_margin
8912 && CHARPOS (startp) != BEGV)
8913 scroll_p = 1;
8914 }
8915
8916 if (PT < MATRIX_ROW_START_CHARPOS (row)
8917 || PT > MATRIX_ROW_END_CHARPOS (row))
8918 {
8919 /* if PT is not in the glyph row, give up. */
8920 rc = -1;
8921 }
8922 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8923 {
8924 /* If we end up in a partially visible line, let's make it
8925 fully visible, except when it's taller than the window,
8926 in which case we can't do much about it. */
8927 if (row->height > window_box_height (w))
8928 {
8929 *scroll_step = 1;
8930 rc = -1;
8931 }
8932 else
8933 {
8934 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8935 try_window (window, startp);
8936 make_cursor_line_fully_visible (w);
8937 rc = 1;
8938 }
8939 }
8940 else if (scroll_p)
8941 rc = -1;
8942 else
8943 {
8944 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8945 rc = 1;
8946 }
8947 }
8948 }
8949
8950 return rc;
8951 }
8952
8953
8954 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
8955 selected_window is redisplayed. */
8956
8957 static void
8958 redisplay_window (window, just_this_one_p)
8959 Lisp_Object window;
8960 int just_this_one_p;
8961 {
8962 struct window *w = XWINDOW (window);
8963 struct frame *f = XFRAME (w->frame);
8964 struct buffer *buffer = XBUFFER (w->buffer);
8965 struct buffer *old = current_buffer;
8966 struct text_pos lpoint, opoint, startp;
8967 int update_mode_line;
8968 int tem;
8969 struct it it;
8970 /* Record it now because it's overwritten. */
8971 int current_matrix_up_to_date_p = 0;
8972 int temp_scroll_step = 0;
8973 int count = specpdl_ptr - specpdl;
8974 int rc;
8975
8976 SET_TEXT_POS (lpoint, PT, PT_BYTE);
8977 opoint = lpoint;
8978
8979 /* W must be a leaf window here. */
8980 xassert (!NILP (w->buffer));
8981 #if GLYPH_DEBUG
8982 *w->desired_matrix->method = 0;
8983 #endif
8984
8985 specbind (Qinhibit_point_motion_hooks, Qt);
8986
8987 reconsider_clip_changes (w, buffer);
8988
8989 /* Has the mode line to be updated? */
8990 update_mode_line = (!NILP (w->update_mode_line)
8991 || update_mode_lines
8992 || buffer->clip_changed);
8993
8994 if (MINI_WINDOW_P (w))
8995 {
8996 if (w == XWINDOW (echo_area_window)
8997 && !NILP (echo_area_buffer[0]))
8998 {
8999 if (update_mode_line)
9000 /* We may have to update a tty frame's menu bar or a
9001 tool-bar. Example `M-x C-h C-h C-g'. */
9002 goto finish_menu_bars;
9003 else
9004 /* We've already displayed the echo area glyphs in this window. */
9005 goto finish_scroll_bars;
9006 }
9007 else if (w != XWINDOW (minibuf_window))
9008 {
9009 /* W is a mini-buffer window, but it's not the currently
9010 active one, so clear it. */
9011 int yb = window_text_bottom_y (w);
9012 struct glyph_row *row;
9013 int y;
9014
9015 for (y = 0, row = w->desired_matrix->rows;
9016 y < yb;
9017 y += row->height, ++row)
9018 blank_row (w, row, y);
9019 goto finish_scroll_bars;
9020 }
9021 }
9022
9023 /* Otherwise set up data on this window; select its buffer and point
9024 value. */
9025 /* Really select the buffer, for the sake of buffer-local
9026 variables. */
9027 set_buffer_internal_1 (XBUFFER (w->buffer));
9028 SET_TEXT_POS (opoint, PT, PT_BYTE);
9029
9030 current_matrix_up_to_date_p
9031 = (!NILP (w->window_end_valid)
9032 && !current_buffer->clip_changed
9033 && XFASTINT (w->last_modified) >= MODIFF
9034 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9035
9036 /* When windows_or_buffers_changed is non-zero, we can't rely on
9037 the window end being valid, so set it to nil there. */
9038 if (windows_or_buffers_changed)
9039 {
9040 /* If window starts on a continuation line, maybe adjust the
9041 window start in case the window's width changed. */
9042 if (XMARKER (w->start)->buffer == current_buffer)
9043 compute_window_start_on_continuation_line (w);
9044
9045 w->window_end_valid = Qnil;
9046 }
9047
9048 /* Some sanity checks. */
9049 CHECK_WINDOW_END (w);
9050 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9051 abort ();
9052 if (BYTEPOS (opoint) < CHARPOS (opoint))
9053 abort ();
9054
9055 /* If %c is in mode line, update it if needed. */
9056 if (!NILP (w->column_number_displayed)
9057 /* This alternative quickly identifies a common case
9058 where no change is needed. */
9059 && !(PT == XFASTINT (w->last_point)
9060 && XFASTINT (w->last_modified) >= MODIFF
9061 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9062 && XFASTINT (w->column_number_displayed) != current_column ())
9063 update_mode_line = 1;
9064
9065 /* Count number of windows showing the selected buffer. An indirect
9066 buffer counts as its base buffer. */
9067 if (!just_this_one_p)
9068 {
9069 struct buffer *current_base, *window_base;
9070 current_base = current_buffer;
9071 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9072 if (current_base->base_buffer)
9073 current_base = current_base->base_buffer;
9074 if (window_base->base_buffer)
9075 window_base = window_base->base_buffer;
9076 if (current_base == window_base)
9077 buffer_shared++;
9078 }
9079
9080 /* Point refers normally to the selected window. For any other
9081 window, set up appropriate value. */
9082 if (!EQ (window, selected_window))
9083 {
9084 int new_pt = XMARKER (w->pointm)->charpos;
9085 int new_pt_byte = marker_byte_position (w->pointm);
9086 if (new_pt < BEGV)
9087 {
9088 new_pt = BEGV;
9089 new_pt_byte = BEGV_BYTE;
9090 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9091 }
9092 else if (new_pt > (ZV - 1))
9093 {
9094 new_pt = ZV;
9095 new_pt_byte = ZV_BYTE;
9096 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9097 }
9098
9099 /* We don't use SET_PT so that the point-motion hooks don't run. */
9100 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9101 }
9102
9103 /* If any of the character widths specified in the display table
9104 have changed, invalidate the width run cache. It's true that
9105 this may be a bit late to catch such changes, but the rest of
9106 redisplay goes (non-fatally) haywire when the display table is
9107 changed, so why should we worry about doing any better? */
9108 if (current_buffer->width_run_cache)
9109 {
9110 struct Lisp_Char_Table *disptab = buffer_display_table ();
9111
9112 if (! disptab_matches_widthtab (disptab,
9113 XVECTOR (current_buffer->width_table)))
9114 {
9115 invalidate_region_cache (current_buffer,
9116 current_buffer->width_run_cache,
9117 BEG, Z);
9118 recompute_width_table (current_buffer, disptab);
9119 }
9120 }
9121
9122 /* If window-start is screwed up, choose a new one. */
9123 if (XMARKER (w->start)->buffer != current_buffer)
9124 goto recenter;
9125
9126 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9127
9128 /* If someone specified a new starting point but did not insist,
9129 check whether it can be used. */
9130 if (!NILP (w->optional_new_start)
9131 && CHARPOS (startp) >= BEGV
9132 && CHARPOS (startp) <= ZV)
9133 {
9134 w->optional_new_start = Qnil;
9135 start_display (&it, w, startp);
9136 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9137 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9138 if (IT_CHARPOS (it) == PT)
9139 w->force_start = Qt;
9140 }
9141
9142 /* Handle case where place to start displaying has been specified,
9143 unless the specified location is outside the accessible range. */
9144 if (!NILP (w->force_start)
9145 || w->frozen_window_start_p)
9146 {
9147 w->force_start = Qnil;
9148 w->vscroll = 0;
9149 w->window_end_valid = Qnil;
9150
9151 /* Forget any recorded base line for line number display. */
9152 if (!current_matrix_up_to_date_p
9153 || current_buffer->clip_changed)
9154 w->base_line_number = Qnil;
9155
9156 /* Redisplay the mode line. Select the buffer properly for that.
9157 Also, run the hook window-scroll-functions
9158 because we have scrolled. */
9159 /* Note, we do this after clearing force_start because
9160 if there's an error, it is better to forget about force_start
9161 than to get into an infinite loop calling the hook functions
9162 and having them get more errors. */
9163 if (!update_mode_line
9164 || ! NILP (Vwindow_scroll_functions))
9165 {
9166 update_mode_line = 1;
9167 w->update_mode_line = Qt;
9168 startp = run_window_scroll_functions (window, startp);
9169 }
9170
9171 XSETFASTINT (w->last_modified, 0);
9172 XSETFASTINT (w->last_overlay_modified, 0);
9173 if (CHARPOS (startp) < BEGV)
9174 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9175 else if (CHARPOS (startp) > ZV)
9176 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9177
9178 /* Redisplay, then check if cursor has been set during the
9179 redisplay. Give up if new fonts were loaded. */
9180 if (!try_window (window, startp))
9181 {
9182 w->force_start = Qt;
9183 clear_glyph_matrix (w->desired_matrix);
9184 goto restore_buffers;
9185 }
9186
9187 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9188 {
9189 /* If point does not appear, try to move point so it does
9190 appear. The desired matrix has been built above, so we
9191 can use it here. */
9192 int window_height;
9193 struct glyph_row *row;
9194
9195 window_height = window_box_height (w) / 2;
9196 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9197 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9198 ++row;
9199
9200 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9201 MATRIX_ROW_START_BYTEPOS (row));
9202
9203 if (w != XWINDOW (selected_window))
9204 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9205 else if (current_buffer == old)
9206 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9207
9208 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9209
9210 /* If we are highlighting the region, then we just changed
9211 the region, so redisplay to show it. */
9212 if (!NILP (Vtransient_mark_mode)
9213 && !NILP (current_buffer->mark_active))
9214 {
9215 clear_glyph_matrix (w->desired_matrix);
9216 if (!try_window (window, startp))
9217 goto restore_buffers;
9218 }
9219 }
9220
9221 make_cursor_line_fully_visible (w);
9222 #if GLYPH_DEBUG
9223 debug_method_add (w, "forced window start");
9224 #endif
9225 goto done;
9226 }
9227
9228 /* Handle case where text has not changed, only point, and it has
9229 not moved off the frame. */
9230 if (current_matrix_up_to_date_p
9231 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9232 rc != 0))
9233 {
9234 if (rc == -1)
9235 goto try_to_scroll;
9236 else
9237 goto done;
9238 }
9239 /* If current starting point was originally the beginning of a line
9240 but no longer is, find a new starting point. */
9241 else if (!NILP (w->start_at_line_beg)
9242 && !(CHARPOS (startp) <= BEGV
9243 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9244 {
9245 #if GLYPH_DEBUG
9246 debug_method_add (w, "recenter 1");
9247 #endif
9248 goto recenter;
9249 }
9250
9251 /* Try scrolling with try_window_id. */
9252 else if (/* Windows and buffers haven't changed. */
9253 !windows_or_buffers_changed
9254 /* Window must be either use window-based redisplay or
9255 be full width. */
9256 && (FRAME_WINDOW_P (f)
9257 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9258 && !MINI_WINDOW_P (w)
9259 /* Point is not known NOT to appear in window. */
9260 && PT >= CHARPOS (startp)
9261 && XFASTINT (w->last_modified)
9262 /* Window is not hscrolled. */
9263 && XFASTINT (w->hscroll) == 0
9264 /* Selective display has not changed. */
9265 && !current_buffer->clip_changed
9266 /* Current matrix is up to date. */
9267 && !NILP (w->window_end_valid)
9268 /* Can't use this case if highlighting a region because
9269 a cursor movement will do more than just set the cursor. */
9270 && !(!NILP (Vtransient_mark_mode)
9271 && !NILP (current_buffer->mark_active))
9272 && NILP (w->region_showing)
9273 && NILP (Vshow_trailing_whitespace)
9274 /* Overlay arrow position and string not changed. */
9275 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9276 && EQ (last_arrow_string, Voverlay_arrow_string)
9277 /* Value is > 0 if update has been done, it is -1 if we
9278 know that the same window start will not work. It is 0
9279 if unsuccessful for some other reason. */
9280 && (tem = try_window_id (w)) != 0)
9281 {
9282 #if GLYPH_DEBUG
9283 debug_method_add (w, "try_window_id %d", tem);
9284 #endif
9285
9286 if (fonts_changed_p)
9287 goto restore_buffers;
9288 if (tem > 0)
9289 goto done;
9290
9291 /* Otherwise try_window_id has returned -1 which means that we
9292 don't want the alternative below this comment to execute. */
9293 }
9294 else if (CHARPOS (startp) >= BEGV
9295 && CHARPOS (startp) <= ZV
9296 && PT >= CHARPOS (startp)
9297 && (CHARPOS (startp) < ZV
9298 /* Avoid starting at end of buffer. */
9299 || CHARPOS (startp) == BEGV
9300 || (XFASTINT (w->last_modified) >= MODIFF
9301 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9302 {
9303 #if GLYPH_DEBUG
9304 debug_method_add (w, "same window start");
9305 #endif
9306
9307 /* Try to redisplay starting at same place as before.
9308 If point has not moved off frame, accept the results. */
9309 if (!current_matrix_up_to_date_p
9310 /* Don't use try_window_reusing_current_matrix in this case
9311 because a window scroll function can have changed the
9312 buffer. */
9313 || !NILP (Vwindow_scroll_functions)
9314 || MINI_WINDOW_P (w)
9315 || !try_window_reusing_current_matrix (w))
9316 {
9317 IF_DEBUG (debug_method_add (w, "1"));
9318 try_window (window, startp);
9319 }
9320
9321 if (fonts_changed_p)
9322 goto restore_buffers;
9323
9324 if (w->cursor.vpos >= 0)
9325 {
9326 if (!just_this_one_p
9327 || current_buffer->clip_changed
9328 || BEG_UNCHANGED < CHARPOS (startp))
9329 /* Forget any recorded base line for line number display. */
9330 w->base_line_number = Qnil;
9331
9332 make_cursor_line_fully_visible (w);
9333 goto done;
9334 }
9335 else
9336 clear_glyph_matrix (w->desired_matrix);
9337 }
9338
9339 try_to_scroll:
9340
9341 XSETFASTINT (w->last_modified, 0);
9342 XSETFASTINT (w->last_overlay_modified, 0);
9343
9344 /* Redisplay the mode line. Select the buffer properly for that. */
9345 if (!update_mode_line)
9346 {
9347 update_mode_line = 1;
9348 w->update_mode_line = Qt;
9349 }
9350
9351 /* Try to scroll by specified few lines. */
9352 if ((scroll_conservatively
9353 || scroll_step
9354 || temp_scroll_step
9355 || NUMBERP (current_buffer->scroll_up_aggressively)
9356 || NUMBERP (current_buffer->scroll_down_aggressively))
9357 && !current_buffer->clip_changed
9358 && CHARPOS (startp) >= BEGV
9359 && CHARPOS (startp) <= ZV)
9360 {
9361 /* The function returns -1 if new fonts were loaded, 1 if
9362 successful, 0 if not successful. */
9363 int rc = try_scrolling (window, just_this_one_p,
9364 scroll_conservatively,
9365 scroll_step,
9366 temp_scroll_step);
9367 if (rc > 0)
9368 goto done;
9369 else if (rc < 0)
9370 goto restore_buffers;
9371 }
9372
9373 /* Finally, just choose place to start which centers point */
9374
9375 recenter:
9376
9377 #if GLYPH_DEBUG
9378 debug_method_add (w, "recenter");
9379 #endif
9380
9381 /* w->vscroll = 0; */
9382
9383 /* Forget any previously recorded base line for line number display. */
9384 if (!current_matrix_up_to_date_p
9385 || current_buffer->clip_changed)
9386 w->base_line_number = Qnil;
9387
9388 /* Move backward half the height of the window. */
9389 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9390 it.current_y = it.last_visible_y;
9391 move_it_vertically_backward (&it, it.last_visible_y / 2);
9392 xassert (IT_CHARPOS (it) >= BEGV);
9393
9394 /* The function move_it_vertically_backward may move over more
9395 than the specified y-distance. If it->w is small, e.g. a
9396 mini-buffer window, we may end up in front of the window's
9397 display area. Start displaying at the start of the line
9398 containing PT in this case. */
9399 if (it.current_y <= 0)
9400 {
9401 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9402 move_it_vertically (&it, 0);
9403 xassert (IT_CHARPOS (it) <= PT);
9404 it.current_y = 0;
9405 }
9406
9407 it.current_x = it.hpos = 0;
9408
9409 /* Set startp here explicitly in case that helps avoid an infinite loop
9410 in case the window-scroll-functions functions get errors. */
9411 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9412
9413 /* Run scroll hooks. */
9414 startp = run_window_scroll_functions (window, it.current.pos);
9415
9416 /* Redisplay the window. */
9417 if (!current_matrix_up_to_date_p
9418 || windows_or_buffers_changed
9419 /* Don't use try_window_reusing_current_matrix in this case
9420 because it can have changed the buffer. */
9421 || !NILP (Vwindow_scroll_functions)
9422 || !just_this_one_p
9423 || MINI_WINDOW_P (w)
9424 || !try_window_reusing_current_matrix (w))
9425 try_window (window, startp);
9426
9427 /* If new fonts have been loaded (due to fontsets), give up. We
9428 have to start a new redisplay since we need to re-adjust glyph
9429 matrices. */
9430 if (fonts_changed_p)
9431 goto restore_buffers;
9432
9433 /* If cursor did not appear assume that the middle of the window is
9434 in the first line of the window. Do it again with the next line.
9435 (Imagine a window of height 100, displaying two lines of height
9436 60. Moving back 50 from it->last_visible_y will end in the first
9437 line.) */
9438 if (w->cursor.vpos < 0)
9439 {
9440 if (!NILP (w->window_end_valid)
9441 && PT >= Z - XFASTINT (w->window_end_pos))
9442 {
9443 clear_glyph_matrix (w->desired_matrix);
9444 move_it_by_lines (&it, 1, 0);
9445 try_window (window, it.current.pos);
9446 }
9447 else if (PT < IT_CHARPOS (it))
9448 {
9449 clear_glyph_matrix (w->desired_matrix);
9450 move_it_by_lines (&it, -1, 0);
9451 try_window (window, it.current.pos);
9452 }
9453 else
9454 {
9455 /* Not much we can do about it. */
9456 }
9457 }
9458
9459 /* Consider the following case: Window starts at BEGV, there is
9460 invisible, intangible text at BEGV, so that display starts at
9461 some point START > BEGV. It can happen that we are called with
9462 PT somewhere between BEGV and START. Try to handle that case. */
9463 if (w->cursor.vpos < 0)
9464 {
9465 struct glyph_row *row = w->current_matrix->rows;
9466 if (row->mode_line_p)
9467 ++row;
9468 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9469 }
9470
9471 make_cursor_line_fully_visible (w);
9472
9473 done:
9474
9475 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9476 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9477 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9478 ? Qt : Qnil);
9479
9480 /* Display the mode line, if we must. */
9481 if ((update_mode_line
9482 /* If window not full width, must redo its mode line
9483 if (a) the window to its side is being redone and
9484 (b) we do a frame-based redisplay. This is a consequence
9485 of how inverted lines are drawn in frame-based redisplay. */
9486 || (!just_this_one_p
9487 && !FRAME_WINDOW_P (f)
9488 && !WINDOW_FULL_WIDTH_P (w))
9489 /* Line number to display. */
9490 || INTEGERP (w->base_line_pos)
9491 /* Column number is displayed and different from the one displayed. */
9492 || (!NILP (w->column_number_displayed)
9493 && XFASTINT (w->column_number_displayed) != current_column ()))
9494 /* This means that the window has a mode line. */
9495 && (WINDOW_WANTS_MODELINE_P (w)
9496 || WINDOW_WANTS_HEADER_LINE_P (w)))
9497 {
9498 Lisp_Object old_selected_frame;
9499
9500 old_selected_frame = selected_frame;
9501
9502 XSETFRAME (selected_frame, f);
9503 display_mode_lines (w);
9504 selected_frame = old_selected_frame;
9505
9506 /* If mode line height has changed, arrange for a thorough
9507 immediate redisplay using the correct mode line height. */
9508 if (WINDOW_WANTS_MODELINE_P (w)
9509 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9510 {
9511 fonts_changed_p = 1;
9512 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9513 = DESIRED_MODE_LINE_HEIGHT (w);
9514 }
9515
9516 /* If top line height has changed, arrange for a thorough
9517 immediate redisplay using the correct mode line height. */
9518 if (WINDOW_WANTS_HEADER_LINE_P (w)
9519 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9520 {
9521 fonts_changed_p = 1;
9522 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9523 = DESIRED_HEADER_LINE_HEIGHT (w);
9524 }
9525
9526 if (fonts_changed_p)
9527 goto restore_buffers;
9528 }
9529
9530 if (!line_number_displayed
9531 && !BUFFERP (w->base_line_pos))
9532 {
9533 w->base_line_pos = Qnil;
9534 w->base_line_number = Qnil;
9535 }
9536
9537 finish_menu_bars:
9538
9539 /* When we reach a frame's selected window, redo the frame's menu bar. */
9540 if (update_mode_line
9541 && EQ (FRAME_SELECTED_WINDOW (f), window))
9542 {
9543 int redisplay_menu_p = 0;
9544
9545 if (FRAME_WINDOW_P (f))
9546 {
9547 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
9548 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9549 #else
9550 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9551 #endif
9552 }
9553 else
9554 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9555
9556 if (redisplay_menu_p)
9557 display_menu_bar (w);
9558
9559 #ifdef HAVE_WINDOW_SYSTEM
9560 if (WINDOWP (f->tool_bar_window)
9561 && (FRAME_TOOL_BAR_LINES (f) > 0
9562 || auto_resize_tool_bars_p))
9563 redisplay_tool_bar (f);
9564 #endif
9565 }
9566
9567 finish_scroll_bars:
9568
9569 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9570 {
9571 int start, end, whole;
9572
9573 /* Calculate the start and end positions for the current window.
9574 At some point, it would be nice to choose between scrollbars
9575 which reflect the whole buffer size, with special markers
9576 indicating narrowing, and scrollbars which reflect only the
9577 visible region.
9578
9579 Note that mini-buffers sometimes aren't displaying any text. */
9580 if (!MINI_WINDOW_P (w)
9581 || (w == XWINDOW (minibuf_window)
9582 && NILP (echo_area_buffer[0])))
9583 {
9584 whole = ZV - BEGV;
9585 start = marker_position (w->start) - BEGV;
9586 /* I don't think this is guaranteed to be right. For the
9587 moment, we'll pretend it is. */
9588 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9589
9590 if (end < start)
9591 end = start;
9592 if (whole < (end - start))
9593 whole = end - start;
9594 }
9595 else
9596 start = end = whole = 0;
9597
9598 /* Indicate what this scroll bar ought to be displaying now. */
9599 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9600
9601 /* Note that we actually used the scroll bar attached to this
9602 window, so it shouldn't be deleted at the end of redisplay. */
9603 (*redeem_scroll_bar_hook) (w);
9604 }
9605
9606 restore_buffers:
9607
9608 /* Restore current_buffer and value of point in it. */
9609 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9610 set_buffer_internal_1 (old);
9611 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9612
9613 unbind_to (count, Qnil);
9614 }
9615
9616
9617 /* Build the complete desired matrix of WINDOW with a window start
9618 buffer position POS. Value is non-zero if successful. It is zero
9619 if fonts were loaded during redisplay which makes re-adjusting
9620 glyph matrices necessary. */
9621
9622 int
9623 try_window (window, pos)
9624 Lisp_Object window;
9625 struct text_pos pos;
9626 {
9627 struct window *w = XWINDOW (window);
9628 struct it it;
9629 struct glyph_row *last_text_row = NULL;
9630
9631 /* Make POS the new window start. */
9632 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9633
9634 /* Mark cursor position as unknown. No overlay arrow seen. */
9635 w->cursor.vpos = -1;
9636 overlay_arrow_seen = 0;
9637
9638 /* Initialize iterator and info to start at POS. */
9639 start_display (&it, w, pos);
9640
9641 /* Display all lines of W. */
9642 while (it.current_y < it.last_visible_y)
9643 {
9644 if (display_line (&it))
9645 last_text_row = it.glyph_row - 1;
9646 if (fonts_changed_p)
9647 return 0;
9648 }
9649
9650 /* If bottom moved off end of frame, change mode line percentage. */
9651 if (XFASTINT (w->window_end_pos) <= 0
9652 && Z != IT_CHARPOS (it))
9653 w->update_mode_line = Qt;
9654
9655 /* Set window_end_pos to the offset of the last character displayed
9656 on the window from the end of current_buffer. Set
9657 window_end_vpos to its row number. */
9658 if (last_text_row)
9659 {
9660 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9661 w->window_end_bytepos
9662 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9663 XSETFASTINT (w->window_end_pos,
9664 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9665 XSETFASTINT (w->window_end_vpos,
9666 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9667 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9668 ->displays_text_p);
9669 }
9670 else
9671 {
9672 w->window_end_bytepos = 0;
9673 XSETFASTINT (w->window_end_pos, 0);
9674 XSETFASTINT (w->window_end_vpos, 0);
9675 }
9676
9677 /* But that is not valid info until redisplay finishes. */
9678 w->window_end_valid = Qnil;
9679 return 1;
9680 }
9681
9682
9683 \f
9684 /************************************************************************
9685 Window redisplay reusing current matrix when buffer has not changed
9686 ************************************************************************/
9687
9688 /* Try redisplay of window W showing an unchanged buffer with a
9689 different window start than the last time it was displayed by
9690 reusing its current matrix. Value is non-zero if successful.
9691 W->start is the new window start. */
9692
9693 static int
9694 try_window_reusing_current_matrix (w)
9695 struct window *w;
9696 {
9697 struct frame *f = XFRAME (w->frame);
9698 struct glyph_row *row, *bottom_row;
9699 struct it it;
9700 struct run run;
9701 struct text_pos start, new_start;
9702 int nrows_scrolled, i;
9703 struct glyph_row *last_text_row;
9704 struct glyph_row *last_reused_text_row;
9705 struct glyph_row *start_row;
9706 int start_vpos, min_y, max_y;
9707
9708
9709 if (/* This function doesn't handle terminal frames. */
9710 !FRAME_WINDOW_P (f)
9711 /* Don't try to reuse the display if windows have been split
9712 or such. */
9713 || windows_or_buffers_changed)
9714 return 0;
9715
9716 /* Can't do this if region may have changed. */
9717 if ((!NILP (Vtransient_mark_mode)
9718 && !NILP (current_buffer->mark_active))
9719 || !NILP (w->region_showing)
9720 || !NILP (Vshow_trailing_whitespace))
9721 return 0;
9722
9723 /* If top-line visibility has changed, give up. */
9724 if (WINDOW_WANTS_HEADER_LINE_P (w)
9725 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9726 return 0;
9727
9728 /* Give up if old or new display is scrolled vertically. We could
9729 make this function handle this, but right now it doesn't. */
9730 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9731 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9732 return 0;
9733
9734 /* The variable new_start now holds the new window start. The old
9735 start `start' can be determined from the current matrix. */
9736 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9737 start = start_row->start.pos;
9738 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9739
9740 /* Clear the desired matrix for the display below. */
9741 clear_glyph_matrix (w->desired_matrix);
9742
9743 if (CHARPOS (new_start) <= CHARPOS (start))
9744 {
9745 int first_row_y;
9746
9747 IF_DEBUG (debug_method_add (w, "twu1"));
9748
9749 /* Display up to a row that can be reused. The variable
9750 last_text_row is set to the last row displayed that displays
9751 text. */
9752 start_display (&it, w, new_start);
9753 first_row_y = it.current_y;
9754 w->cursor.vpos = -1;
9755 last_text_row = last_reused_text_row = NULL;
9756 while (it.current_y < it.last_visible_y
9757 && IT_CHARPOS (it) < CHARPOS (start)
9758 && !fonts_changed_p)
9759 if (display_line (&it))
9760 last_text_row = it.glyph_row - 1;
9761
9762 /* A value of current_y < last_visible_y means that we stopped
9763 at the previous window start, which in turn means that we
9764 have at least one reusable row. */
9765 if (it.current_y < it.last_visible_y)
9766 {
9767 nrows_scrolled = it.vpos;
9768
9769 /* Find PT if not already found in the lines displayed. */
9770 if (w->cursor.vpos < 0)
9771 {
9772 int dy = it.current_y - first_row_y;
9773
9774 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9775 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9776 {
9777 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9778 && PT < MATRIX_ROW_END_CHARPOS (row))
9779 {
9780 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9781 dy, nrows_scrolled);
9782 break;
9783 }
9784
9785 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9786 break;
9787
9788 ++row;
9789 }
9790
9791 /* Give up if point was not found. This shouldn't
9792 happen often; not more often than with try_window
9793 itself. */
9794 if (w->cursor.vpos < 0)
9795 {
9796 clear_glyph_matrix (w->desired_matrix);
9797 return 0;
9798 }
9799 }
9800
9801 /* Scroll the display. Do it before the current matrix is
9802 changed. The problem here is that update has not yet
9803 run, i.e. part of the current matrix is not up to date.
9804 scroll_run_hook will clear the cursor, and use the
9805 current matrix to get the height of the row the cursor is
9806 in. */
9807 run.current_y = first_row_y;
9808 run.desired_y = it.current_y;
9809 run.height = it.last_visible_y - it.current_y;
9810 if (run.height > 0
9811 && run.current_y != run.desired_y)
9812 {
9813 update_begin (f);
9814 rif->update_window_begin_hook (w);
9815 rif->clear_mouse_face (w);
9816 rif->scroll_run_hook (w, &run);
9817 rif->update_window_end_hook (w, 0, 0);
9818 update_end (f);
9819 }
9820
9821 /* Shift current matrix down by nrows_scrolled lines. */
9822 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9823 rotate_matrix (w->current_matrix,
9824 start_vpos,
9825 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9826 nrows_scrolled);
9827
9828 /* Disable lines not reused. */
9829 for (i = 0; i < it.vpos; ++i)
9830 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
9831
9832 /* Re-compute Y positions. */
9833 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix) + nrows_scrolled;
9834 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9835 max_y = it.last_visible_y;
9836 while (row < bottom_row)
9837 {
9838 row->y = it.current_y;
9839
9840 if (row->y < min_y)
9841 row->visible_height = row->height - (min_y - row->y);
9842 else if (row->y + row->height > max_y)
9843 row->visible_height
9844 = row->height - (row->y + row->height - max_y);
9845 else
9846 row->visible_height = row->height;
9847
9848 it.current_y += row->height;
9849 ++it.vpos;
9850
9851 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9852 last_reused_text_row = row;
9853 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9854 break;
9855 ++row;
9856 }
9857 }
9858
9859 /* Update window_end_pos etc.; last_reused_text_row is the last
9860 reused row from the current matrix containing text, if any.
9861 The value of last_text_row is the last displayed line
9862 containing text. */
9863 if (last_reused_text_row)
9864 {
9865 w->window_end_bytepos
9866 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
9867 XSETFASTINT (w->window_end_pos,
9868 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
9869 XSETFASTINT (w->window_end_vpos,
9870 MATRIX_ROW_VPOS (last_reused_text_row,
9871 w->current_matrix));
9872 }
9873 else if (last_text_row)
9874 {
9875 w->window_end_bytepos
9876 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9877 XSETFASTINT (w->window_end_pos,
9878 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9879 XSETFASTINT (w->window_end_vpos,
9880 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9881 }
9882 else
9883 {
9884 /* This window must be completely empty. */
9885 w->window_end_bytepos = 0;
9886 XSETFASTINT (w->window_end_pos, 0);
9887 XSETFASTINT (w->window_end_vpos, 0);
9888 }
9889 w->window_end_valid = Qnil;
9890
9891 /* Update hint: don't try scrolling again in update_window. */
9892 w->desired_matrix->no_scrolling_p = 1;
9893
9894 #if GLYPH_DEBUG
9895 debug_method_add (w, "try_window_reusing_current_matrix 1");
9896 #endif
9897 return 1;
9898 }
9899 else if (CHARPOS (new_start) > CHARPOS (start))
9900 {
9901 struct glyph_row *pt_row, *row;
9902 struct glyph_row *first_reusable_row;
9903 struct glyph_row *first_row_to_display;
9904 int dy;
9905 int yb = window_text_bottom_y (w);
9906
9907 IF_DEBUG (debug_method_add (w, "twu2"));
9908
9909 /* Find the row starting at new_start, if there is one. Don't
9910 reuse a partially visible line at the end. */
9911 first_reusable_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9912 while (first_reusable_row->enabled_p
9913 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
9914 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9915 < CHARPOS (new_start)))
9916 ++first_reusable_row;
9917
9918 /* Give up if there is no row to reuse. */
9919 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
9920 || !first_reusable_row->enabled_p
9921 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
9922 != CHARPOS (new_start)))
9923 return 0;
9924
9925 /* We can reuse fully visible rows beginning with
9926 first_reusable_row to the end of the window. Set
9927 first_row_to_display to the first row that cannot be reused.
9928 Set pt_row to the row containing point, if there is any. */
9929 first_row_to_display = first_reusable_row;
9930 pt_row = NULL;
9931 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
9932 {
9933 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
9934 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
9935 pt_row = first_row_to_display;
9936
9937 ++first_row_to_display;
9938 }
9939
9940 /* Start displaying at the start of first_row_to_display. */
9941 xassert (first_row_to_display->y < yb);
9942 init_to_row_start (&it, w, first_row_to_display);
9943 nrows_scrolled = MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix);
9944 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
9945 - nrows_scrolled);
9946 it.current_y = first_row_to_display->y - first_reusable_row->y;
9947
9948 /* Display lines beginning with first_row_to_display in the
9949 desired matrix. Set last_text_row to the last row displayed
9950 that displays text. */
9951 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
9952 if (pt_row == NULL)
9953 w->cursor.vpos = -1;
9954 last_text_row = NULL;
9955 while (it.current_y < it.last_visible_y && !fonts_changed_p)
9956 if (display_line (&it))
9957 last_text_row = it.glyph_row - 1;
9958
9959 /* Give up If point isn't in a row displayed or reused. */
9960 if (w->cursor.vpos < 0)
9961 {
9962 clear_glyph_matrix (w->desired_matrix);
9963 return 0;
9964 }
9965
9966 /* If point is in a reused row, adjust y and vpos of the cursor
9967 position. */
9968 if (pt_row)
9969 {
9970 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
9971 w->current_matrix);
9972 w->cursor.y -= first_reusable_row->y;
9973 }
9974
9975 /* Scroll the display. */
9976 run.current_y = first_reusable_row->y;
9977 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9978 run.height = it.last_visible_y - run.current_y;
9979 if (run.height)
9980 {
9981 struct frame *f = XFRAME (WINDOW_FRAME (w));
9982 update_begin (f);
9983 rif->update_window_begin_hook (w);
9984 rif->clear_mouse_face (w);
9985 rif->scroll_run_hook (w, &run);
9986 rif->update_window_end_hook (w, 0, 0);
9987 update_end (f);
9988 }
9989
9990 /* Adjust Y positions of reused rows. */
9991 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9992 row = first_reusable_row;
9993 dy = first_reusable_row->y;
9994 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9995 max_y = it.last_visible_y;
9996 while (row < first_row_to_display)
9997 {
9998 row->y -= dy;
9999 if (row->y < min_y)
10000 row->visible_height = row->height - (min_y - row->y);
10001 else if (row->y + row->height > max_y)
10002 row->visible_height
10003 = row->height - (row->y + row->height - max_y);
10004 else
10005 row->visible_height = row->height;
10006 ++row;
10007 }
10008
10009 /* Disable rows not reused. */
10010 while (row < bottom_row)
10011 {
10012 row->enabled_p = 0;
10013 ++row;
10014 }
10015
10016 /* Scroll the current matrix. */
10017 xassert (nrows_scrolled > 0);
10018 rotate_matrix (w->current_matrix,
10019 start_vpos,
10020 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10021 -nrows_scrolled);
10022
10023 /* Adjust window end. A null value of last_text_row means that
10024 the window end is in reused rows which in turn means that
10025 only its vpos can have changed. */
10026 if (last_text_row)
10027 {
10028 w->window_end_bytepos
10029 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10030 XSETFASTINT (w->window_end_pos,
10031 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10032 XSETFASTINT (w->window_end_vpos,
10033 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10034 }
10035 else
10036 {
10037 XSETFASTINT (w->window_end_vpos,
10038 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10039 }
10040
10041 w->window_end_valid = Qnil;
10042 w->desired_matrix->no_scrolling_p = 1;
10043
10044 #if GLYPH_DEBUG
10045 debug_method_add (w, "try_window_reusing_current_matrix 2");
10046 #endif
10047 return 1;
10048 }
10049
10050 return 0;
10051 }
10052
10053
10054 \f
10055 /************************************************************************
10056 Window redisplay reusing current matrix when buffer has changed
10057 ************************************************************************/
10058
10059 static struct glyph_row *get_last_unchanged_at_beg_row P_ ((struct window *));
10060 static struct glyph_row *get_first_unchanged_at_end_row P_ ((struct window *,
10061 int *, int *));
10062 static struct glyph_row *
10063 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10064 struct glyph_row *));
10065
10066
10067 /* Return the last row in MATRIX displaying text. If row START is
10068 non-null, start searching with that row. IT gives the dimensions
10069 of the display. Value is null if matrix is empty; otherwise it is
10070 a pointer to the row found. */
10071
10072 static struct glyph_row *
10073 find_last_row_displaying_text (matrix, it, start)
10074 struct glyph_matrix *matrix;
10075 struct it *it;
10076 struct glyph_row *start;
10077 {
10078 struct glyph_row *row, *row_found;
10079
10080 /* Set row_found to the last row in IT->w's current matrix
10081 displaying text. The loop looks funny but think of partially
10082 visible lines. */
10083 row_found = NULL;
10084 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10085 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10086 {
10087 xassert (row->enabled_p);
10088 row_found = row;
10089 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10090 break;
10091 ++row;
10092 }
10093
10094 return row_found;
10095 }
10096
10097
10098 /* Return the last row in the current matrix of W that is not affected
10099 by changes at the start of current_buffer that occurred since the
10100 last time W was redisplayed. Value is null if no such row exists.
10101
10102 The global variable beg_unchanged has to contain the number of
10103 bytes unchanged at the start of current_buffer. BEG +
10104 beg_unchanged is the buffer position of the first changed byte in
10105 current_buffer. Characters at positions < BEG + beg_unchanged are
10106 at the same buffer positions as they were when the current matrix
10107 was built. */
10108
10109 static struct glyph_row *
10110 get_last_unchanged_at_beg_row (w)
10111 struct window *w;
10112 {
10113 int first_changed_pos = BEG + BEG_UNCHANGED;
10114 struct glyph_row *row;
10115 struct glyph_row *row_found = NULL;
10116 int yb = window_text_bottom_y (w);
10117
10118 /* Find the last row displaying unchanged text. */
10119 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10120 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10121 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10122 {
10123 if (/* If row ends before first_changed_pos, it is unchanged,
10124 except in some case. */
10125 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10126 /* When row ends in ZV and we write at ZV it is not
10127 unchanged. */
10128 && !row->ends_at_zv_p
10129 /* When first_changed_pos is the end of a continued line,
10130 row is not unchanged because it may be no longer
10131 continued. */
10132 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10133 && row->continued_p))
10134 row_found = row;
10135
10136 /* Stop if last visible row. */
10137 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10138 break;
10139
10140 ++row;
10141 }
10142
10143 return row_found;
10144 }
10145
10146
10147 /* Find the first glyph row in the current matrix of W that is not
10148 affected by changes at the end of current_buffer since the last
10149 time the window was redisplayed. Return in *DELTA the number of
10150 chars by which buffer positions in unchanged text at the end of
10151 current_buffer must be adjusted. Return in *DELTA_BYTES the
10152 corresponding number of bytes. Value is null if no such row
10153 exists, i.e. all rows are affected by changes. */
10154
10155 static struct glyph_row *
10156 get_first_unchanged_at_end_row (w, delta, delta_bytes)
10157 struct window *w;
10158 int *delta, *delta_bytes;
10159 {
10160 struct glyph_row *row;
10161 struct glyph_row *row_found = NULL;
10162
10163 *delta = *delta_bytes = 0;
10164
10165 /* A value of window_end_pos >= end_unchanged means that the window
10166 end is in the range of changed text. If so, there is no
10167 unchanged row at the end of W's current matrix. */
10168 xassert (!NILP (w->window_end_valid));
10169 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10170 return NULL;
10171
10172 /* Set row to the last row in W's current matrix displaying text. */
10173 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10174
10175 /* If matrix is entirely empty, no unchanged row exists. */
10176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10177 {
10178 /* The value of row is the last glyph row in the matrix having a
10179 meaningful buffer position in it. The end position of row
10180 corresponds to window_end_pos. This allows us to translate
10181 buffer positions in the current matrix to current buffer
10182 positions for characters not in changed text. */
10183 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10184 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10185 int last_unchanged_pos, last_unchanged_pos_old;
10186 struct glyph_row *first_text_row
10187 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10188
10189 *delta = Z - Z_old;
10190 *delta_bytes = Z_BYTE - Z_BYTE_old;
10191
10192 /* Set last_unchanged_pos to the buffer position of the last
10193 character in the buffer that has not been changed. Z is the
10194 index + 1 of the last byte in current_buffer, i.e. by
10195 subtracting end_unchanged we get the index of the last
10196 unchanged character, and we have to add BEG to get its buffer
10197 position. */
10198 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10199 last_unchanged_pos_old = last_unchanged_pos - *delta;
10200
10201 /* Search backward from ROW for a row displaying a line that
10202 starts at a minimum position >= last_unchanged_pos_old. */
10203 while (row >= first_text_row)
10204 {
10205 xassert (row->enabled_p);
10206 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (row));
10207
10208 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10209 row_found = row;
10210 --row;
10211 }
10212 }
10213
10214 xassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
10215 return row_found;
10216 }
10217
10218
10219 /* Make sure that glyph rows in the current matrix of window W
10220 reference the same glyph memory as corresponding rows in the
10221 frame's frame matrix. This function is called after scrolling W's
10222 current matrix on a terminal frame in try_window_id and
10223 try_window_reusing_current_matrix. */
10224
10225 static void
10226 sync_frame_with_window_matrix_rows (w)
10227 struct window *w;
10228 {
10229 struct frame *f = XFRAME (w->frame);
10230 struct glyph_row *window_row, *window_row_end, *frame_row;
10231
10232 /* Preconditions: W must be a leaf window and full-width. Its frame
10233 must have a frame matrix. */
10234 xassert (NILP (w->hchild) && NILP (w->vchild));
10235 xassert (WINDOW_FULL_WIDTH_P (w));
10236 xassert (!FRAME_WINDOW_P (f));
10237
10238 /* If W is a full-width window, glyph pointers in W's current matrix
10239 have, by definition, to be the same as glyph pointers in the
10240 corresponding frame matrix. */
10241 window_row = w->current_matrix->rows;
10242 window_row_end = window_row + w->current_matrix->nrows;
10243 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10244 while (window_row < window_row_end)
10245 {
10246 int area;
10247
10248 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10249 frame_row->glyphs[area] = window_row->glyphs[area];
10250
10251 /* Disable frame rows whose corresponding window rows have
10252 been disabled in try_window_id. */
10253 if (!window_row->enabled_p)
10254 frame_row->enabled_p = 0;
10255
10256 ++window_row, ++frame_row;
10257 }
10258 }
10259
10260
10261 /* Find the glyph row in window W containing CHARPOS. Consider all
10262 rows between START and END (not inclusive). END null means search
10263 all rows to the end of the display area of W. Value is the row
10264 containing CHARPOS or null. */
10265
10266 static struct glyph_row *
10267 row_containing_pos (w, charpos, start, end)
10268 struct window *w;
10269 int charpos;
10270 struct glyph_row *start, *end;
10271 {
10272 struct glyph_row *row = start;
10273 int last_y;
10274
10275 /* If we happen to start on a header-line, skip that. */
10276 if (row->mode_line_p)
10277 ++row;
10278
10279 if ((end && row >= end) || !row->enabled_p)
10280 return NULL;
10281
10282 last_y = window_text_bottom_y (w);
10283
10284 while ((end == NULL || row < end)
10285 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10286 /* The end position of a row equals the start
10287 position of the next row. If CHARPOS is there, we
10288 would rather display it in the next line, except
10289 when this line ends in ZV. */
10290 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10291 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10292 || !row->ends_at_zv_p)))
10293 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10294 ++row;
10295
10296 /* Give up if CHARPOS not found. */
10297 if ((end && row >= end)
10298 || charpos < MATRIX_ROW_START_CHARPOS (row)
10299 || charpos > MATRIX_ROW_END_CHARPOS (row))
10300 row = NULL;
10301
10302 return row;
10303 }
10304
10305
10306 /* Try to redisplay window W by reusing its existing display. W's
10307 current matrix must be up to date when this function is called,
10308 i.e. window_end_valid must not be nil.
10309
10310 Value is
10311
10312 1 if display has been updated
10313 0 if otherwise unsuccessful
10314 -1 if redisplay with same window start is known not to succeed
10315
10316 The following steps are performed:
10317
10318 1. Find the last row in the current matrix of W that is not
10319 affected by changes at the start of current_buffer. If no such row
10320 is found, give up.
10321
10322 2. Find the first row in W's current matrix that is not affected by
10323 changes at the end of current_buffer. Maybe there is no such row.
10324
10325 3. Display lines beginning with the row + 1 found in step 1 to the
10326 row found in step 2 or, if step 2 didn't find a row, to the end of
10327 the window.
10328
10329 4. If cursor is not known to appear on the window, give up.
10330
10331 5. If display stopped at the row found in step 2, scroll the
10332 display and current matrix as needed.
10333
10334 6. Maybe display some lines at the end of W, if we must. This can
10335 happen under various circumstances, like a partially visible line
10336 becoming fully visible, or because newly displayed lines are displayed
10337 in smaller font sizes.
10338
10339 7. Update W's window end information. */
10340
10341 /* Check that window end is what we expect it to be. */
10342
10343 static int
10344 try_window_id (w)
10345 struct window *w;
10346 {
10347 struct frame *f = XFRAME (w->frame);
10348 struct glyph_matrix *current_matrix = w->current_matrix;
10349 struct glyph_matrix *desired_matrix = w->desired_matrix;
10350 struct glyph_row *last_unchanged_at_beg_row;
10351 struct glyph_row *first_unchanged_at_end_row;
10352 struct glyph_row *row;
10353 struct glyph_row *bottom_row;
10354 int bottom_vpos;
10355 struct it it;
10356 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10357 struct text_pos start_pos;
10358 struct run run;
10359 int first_unchanged_at_end_vpos = 0;
10360 struct glyph_row *last_text_row, *last_text_row_at_end;
10361 struct text_pos start;
10362
10363 SET_TEXT_POS_FROM_MARKER (start, w->start);
10364
10365 /* Check pre-conditions. Window end must be valid, otherwise
10366 the current matrix would not be up to date. */
10367 xassert (!NILP (w->window_end_valid));
10368 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10369 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10370
10371 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10372 only if buffer has really changed. The reason is that the gap is
10373 initially at Z for freshly visited files. The code below would
10374 set end_unchanged to 0 in that case. */
10375 if (MODIFF > SAVE_MODIFF
10376 /* This seems to happen sometimes after saving a buffer. */
10377 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10378 {
10379 if (GPT - BEG < BEG_UNCHANGED)
10380 BEG_UNCHANGED = GPT - BEG;
10381 if (Z - GPT < END_UNCHANGED)
10382 END_UNCHANGED = Z - GPT;
10383 }
10384
10385 /* If window starts after a line end, and the last change is in
10386 front of that newline, then changes don't affect the display.
10387 This case happens with stealth-fontification. Note that although
10388 the display is unchanged, glyph positions in the matrix have to
10389 be adjusted, of course. */
10390 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10391 if (CHARPOS (start) > BEGV
10392 && Z - END_UNCHANGED < CHARPOS (start) - 1
10393 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10394 && PT < MATRIX_ROW_END_CHARPOS (row))
10395 {
10396 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10397 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10398
10399 if (delta)
10400 {
10401 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10402 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10403
10404 increment_matrix_positions (w->current_matrix,
10405 MATRIX_ROW_VPOS (r0, current_matrix),
10406 MATRIX_ROW_VPOS (r1, current_matrix),
10407 delta, delta_bytes);
10408 }
10409
10410 #if 0 /* If changes are all in front of the window start, the
10411 distance of the last displayed glyph from Z hasn't
10412 changed. */
10413 w->window_end_pos
10414 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10415 w->window_end_bytepos
10416 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10417 #endif
10418
10419 return 1;
10420 }
10421
10422 /* Return quickly if changes are all below what is displayed in the
10423 window, and if PT is in the window. */
10424 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10425 && PT < MATRIX_ROW_END_CHARPOS (row))
10426 {
10427 /* We have to update window end positions because the buffer's
10428 size has changed. */
10429 w->window_end_pos
10430 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10431 w->window_end_bytepos
10432 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10433
10434 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10435 row = row_containing_pos (w, PT, row, NULL);
10436 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10437 return 2;
10438 }
10439
10440 /* Check that window start agrees with the start of the first glyph
10441 row in its current matrix. Check this after we know the window
10442 start is not in changed text, otherwise positions would not be
10443 comparable. */
10444 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10445 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10446 return 0;
10447
10448 /* Compute the position at which we have to start displaying new
10449 lines. Some of the lines at the top of the window might be
10450 reusable because they are not displaying changed text. Find the
10451 last row in W's current matrix not affected by changes at the
10452 start of current_buffer. Value is null if changes start in the
10453 first line of window. */
10454 last_unchanged_at_beg_row = get_last_unchanged_at_beg_row (w);
10455 if (last_unchanged_at_beg_row)
10456 {
10457 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10458 start_pos = it.current.pos;
10459
10460 /* Start displaying new lines in the desired matrix at the same
10461 vpos we would use in the current matrix, i.e. below
10462 last_unchanged_at_beg_row. */
10463 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10464 current_matrix);
10465 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10466 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10467
10468 xassert (it.hpos == 0 && it.current_x == 0);
10469 }
10470 else
10471 {
10472 /* There are no reusable lines at the start of the window.
10473 Start displaying in the first line. */
10474 start_display (&it, w, start);
10475 start_pos = it.current.pos;
10476 }
10477
10478 /* Find the first row that is not affected by changes at the end of
10479 the buffer. Value will be null if there is no unchanged row, in
10480 which case we must redisplay to the end of the window. delta
10481 will be set to the value by which buffer positions beginning with
10482 first_unchanged_at_end_row have to be adjusted due to text
10483 changes. */
10484 first_unchanged_at_end_row
10485 = get_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10486 IF_DEBUG (debug_delta = delta);
10487 IF_DEBUG (debug_delta_bytes = delta_bytes);
10488
10489 /* Set stop_pos to the buffer position up to which we will have to
10490 display new lines. If first_unchanged_at_end_row != NULL, this
10491 is the buffer position of the start of the line displayed in that
10492 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10493 that we don't stop at a buffer position. */
10494 stop_pos = 0;
10495 if (first_unchanged_at_end_row)
10496 {
10497 xassert (last_unchanged_at_beg_row == NULL
10498 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10499
10500 /* If this is a continuation line, move forward to the next one
10501 that isn't. Changes in lines above affect this line.
10502 Caution: this may move first_unchanged_at_end_row to a row
10503 not displaying text. */
10504 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10505 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10506 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10507 < it.last_visible_y))
10508 ++first_unchanged_at_end_row;
10509
10510 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10511 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10512 >= it.last_visible_y))
10513 first_unchanged_at_end_row = NULL;
10514 else
10515 {
10516 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10517 + delta);
10518 first_unchanged_at_end_vpos
10519 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10520 xassert (stop_pos >= Z - END_UNCHANGED);
10521 }
10522 }
10523 else if (last_unchanged_at_beg_row == NULL)
10524 return 0;
10525
10526
10527 #if GLYPH_DEBUG
10528
10529 /* Either there is no unchanged row at the end, or the one we have
10530 now displays text. This is a necessary condition for the window
10531 end pos calculation at the end of this function. */
10532 xassert (first_unchanged_at_end_row == NULL
10533 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10534
10535 debug_last_unchanged_at_beg_vpos
10536 = (last_unchanged_at_beg_row
10537 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10538 : -1);
10539 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10540
10541 #endif /* GLYPH_DEBUG != 0 */
10542
10543
10544 /* Display new lines. Set last_text_row to the last new line
10545 displayed which has text on it, i.e. might end up as being the
10546 line where the window_end_vpos is. */
10547 w->cursor.vpos = -1;
10548 last_text_row = NULL;
10549 overlay_arrow_seen = 0;
10550 while (it.current_y < it.last_visible_y
10551 && !fonts_changed_p
10552 && (first_unchanged_at_end_row == NULL
10553 || IT_CHARPOS (it) < stop_pos))
10554 {
10555 if (display_line (&it))
10556 last_text_row = it.glyph_row - 1;
10557 }
10558
10559 if (fonts_changed_p)
10560 return -1;
10561
10562
10563 /* Compute differences in buffer positions, y-positions etc. for
10564 lines reused at the bottom of the window. Compute what we can
10565 scroll. */
10566 if (first_unchanged_at_end_row
10567 /* No lines reused because we displayed everything up to the
10568 bottom of the window. */
10569 && it.current_y < it.last_visible_y)
10570 {
10571 dvpos = (it.vpos
10572 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10573 current_matrix));
10574 dy = it.current_y - first_unchanged_at_end_row->y;
10575 run.current_y = first_unchanged_at_end_row->y;
10576 run.desired_y = run.current_y + dy;
10577 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10578 }
10579 else
10580 {
10581 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10582 first_unchanged_at_end_row = NULL;
10583 }
10584 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10585
10586
10587 /* Find the cursor if not already found. We have to decide whether
10588 PT will appear on this window (it sometimes doesn't, but this is
10589 not a very frequent case.) This decision has to be made before
10590 the current matrix is altered. A value of cursor.vpos < 0 means
10591 that PT is either in one of the lines beginning at
10592 first_unchanged_at_end_row or below the window. Don't care for
10593 lines that might be displayed later at the window end; as
10594 mentioned, this is not a frequent case. */
10595 if (w->cursor.vpos < 0)
10596 {
10597 /* Cursor in unchanged rows at the top? */
10598 if (PT < CHARPOS (start_pos)
10599 && last_unchanged_at_beg_row)
10600 {
10601 row = row_containing_pos (w, PT,
10602 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10603 last_unchanged_at_beg_row + 1);
10604 xassert (row && row <= last_unchanged_at_beg_row);
10605 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10606 }
10607
10608 /* Start from first_unchanged_at_end_row looking for PT. */
10609 else if (first_unchanged_at_end_row)
10610 {
10611 row = row_containing_pos (w, PT - delta,
10612 first_unchanged_at_end_row, NULL);
10613 if (row)
10614 set_cursor_from_row (w, row, w->current_matrix, delta,
10615 delta_bytes, dy, dvpos);
10616 }
10617
10618 /* Give up if cursor was not found. */
10619 if (w->cursor.vpos < 0)
10620 {
10621 clear_glyph_matrix (w->desired_matrix);
10622 return -1;
10623 }
10624 }
10625
10626 /* Don't let the cursor end in the scroll margins. */
10627 {
10628 int this_scroll_margin, cursor_height;
10629
10630 this_scroll_margin = max (0, scroll_margin);
10631 this_scroll_margin = min (this_scroll_margin,
10632 XFASTINT (w->height) / 4);
10633 this_scroll_margin *= CANON_Y_UNIT (it.f);
10634 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10635
10636 if ((w->cursor.y < this_scroll_margin
10637 && CHARPOS (start) > BEGV)
10638 /* Don't take scroll margin into account at the bottom because
10639 old redisplay didn't do it either. */
10640 || w->cursor.y + cursor_height > it.last_visible_y)
10641 {
10642 w->cursor.vpos = -1;
10643 clear_glyph_matrix (w->desired_matrix);
10644 return -1;
10645 }
10646 }
10647
10648 /* Scroll the display. Do it before changing the current matrix so
10649 that xterm.c doesn't get confused about where the cursor glyph is
10650 found. */
10651 if (dy && run.height)
10652 {
10653 update_begin (f);
10654
10655 if (FRAME_WINDOW_P (f))
10656 {
10657 rif->update_window_begin_hook (w);
10658 rif->clear_mouse_face (w);
10659 rif->scroll_run_hook (w, &run);
10660 rif->update_window_end_hook (w, 0, 0);
10661 }
10662 else
10663 {
10664 /* Terminal frame. In this case, dvpos gives the number of
10665 lines to scroll by; dvpos < 0 means scroll up. */
10666 int first_unchanged_at_end_vpos
10667 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10668 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10669 int end = XFASTINT (w->top) + window_internal_height (w);
10670
10671 /* Perform the operation on the screen. */
10672 if (dvpos > 0)
10673 {
10674 /* Scroll last_unchanged_at_beg_row to the end of the
10675 window down dvpos lines. */
10676 set_terminal_window (end);
10677
10678 /* On dumb terminals delete dvpos lines at the end
10679 before inserting dvpos empty lines. */
10680 if (!scroll_region_ok)
10681 ins_del_lines (end - dvpos, -dvpos);
10682
10683 /* Insert dvpos empty lines in front of
10684 last_unchanged_at_beg_row. */
10685 ins_del_lines (from, dvpos);
10686 }
10687 else if (dvpos < 0)
10688 {
10689 /* Scroll up last_unchanged_at_beg_vpos to the end of
10690 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10691 set_terminal_window (end);
10692
10693 /* Delete dvpos lines in front of
10694 last_unchanged_at_beg_vpos. ins_del_lines will set
10695 the cursor to the given vpos and emit |dvpos| delete
10696 line sequences. */
10697 ins_del_lines (from + dvpos, dvpos);
10698
10699 /* On a dumb terminal insert dvpos empty lines at the
10700 end. */
10701 if (!scroll_region_ok)
10702 ins_del_lines (end + dvpos, -dvpos);
10703 }
10704
10705 set_terminal_window (0);
10706 }
10707
10708 update_end (f);
10709 }
10710
10711 /* Shift reused rows of the current matrix to the right position.
10712 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10713 text. */
10714 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10715 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10716 if (dvpos < 0)
10717 {
10718 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10719 bottom_vpos, dvpos);
10720 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10721 bottom_vpos, 0);
10722 }
10723 else if (dvpos > 0)
10724 {
10725 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10726 bottom_vpos, dvpos);
10727 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10728 first_unchanged_at_end_vpos + dvpos, 0);
10729 }
10730
10731 /* For frame-based redisplay, make sure that current frame and window
10732 matrix are in sync with respect to glyph memory. */
10733 if (!FRAME_WINDOW_P (f))
10734 sync_frame_with_window_matrix_rows (w);
10735
10736 /* Adjust buffer positions in reused rows. */
10737 if (delta)
10738 increment_matrix_positions (current_matrix,
10739 first_unchanged_at_end_vpos + dvpos,
10740 bottom_vpos, delta, delta_bytes);
10741
10742 /* Adjust Y positions. */
10743 if (dy)
10744 shift_glyph_matrix (w, current_matrix,
10745 first_unchanged_at_end_vpos + dvpos,
10746 bottom_vpos, dy);
10747
10748 if (first_unchanged_at_end_row)
10749 first_unchanged_at_end_row += dvpos;
10750
10751 /* If scrolling up, there may be some lines to display at the end of
10752 the window. */
10753 last_text_row_at_end = NULL;
10754 if (dy < 0)
10755 {
10756 /* Set last_row to the glyph row in the current matrix where the
10757 window end line is found. It has been moved up or down in
10758 the matrix by dvpos. */
10759 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10760 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10761
10762 /* If last_row is the window end line, it should display text. */
10763 xassert (last_row->displays_text_p);
10764
10765 /* If window end line was partially visible before, begin
10766 displaying at that line. Otherwise begin displaying with the
10767 line following it. */
10768 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10769 {
10770 init_to_row_start (&it, w, last_row);
10771 it.vpos = last_vpos;
10772 it.current_y = last_row->y;
10773 }
10774 else
10775 {
10776 init_to_row_end (&it, w, last_row);
10777 it.vpos = 1 + last_vpos;
10778 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10779 ++last_row;
10780 }
10781
10782 /* We may start in a continuation line. If so, we have to get
10783 the right continuation_lines_width and current_x. */
10784 it.continuation_lines_width = last_row->continuation_lines_width;
10785 it.hpos = it.current_x = 0;
10786
10787 /* Display the rest of the lines at the window end. */
10788 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10789 while (it.current_y < it.last_visible_y
10790 && !fonts_changed_p)
10791 {
10792 /* Is it always sure that the display agrees with lines in
10793 the current matrix? I don't think so, so we mark rows
10794 displayed invalid in the current matrix by setting their
10795 enabled_p flag to zero. */
10796 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10797 if (display_line (&it))
10798 last_text_row_at_end = it.glyph_row - 1;
10799 }
10800 }
10801
10802 /* Update window_end_pos and window_end_vpos. */
10803 if (first_unchanged_at_end_row
10804 && first_unchanged_at_end_row->y < it.last_visible_y
10805 && !last_text_row_at_end)
10806 {
10807 /* Window end line if one of the preserved rows from the current
10808 matrix. Set row to the last row displaying text in current
10809 matrix starting at first_unchanged_at_end_row, after
10810 scrolling. */
10811 xassert (first_unchanged_at_end_row->displays_text_p);
10812 row = find_last_row_displaying_text (w->current_matrix, &it,
10813 first_unchanged_at_end_row);
10814 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10815
10816 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10817 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10818 XSETFASTINT (w->window_end_vpos,
10819 MATRIX_ROW_VPOS (row, w->current_matrix));
10820 }
10821 else if (last_text_row_at_end)
10822 {
10823 XSETFASTINT (w->window_end_pos,
10824 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10825 w->window_end_bytepos
10826 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10827 XSETFASTINT (w->window_end_vpos,
10828 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10829 }
10830 else if (last_text_row)
10831 {
10832 /* We have displayed either to the end of the window or at the
10833 end of the window, i.e. the last row with text is to be found
10834 in the desired matrix. */
10835 XSETFASTINT (w->window_end_pos,
10836 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10837 w->window_end_bytepos
10838 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10839 XSETFASTINT (w->window_end_vpos,
10840 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10841 }
10842 else if (first_unchanged_at_end_row == NULL
10843 && last_text_row == NULL
10844 && last_text_row_at_end == NULL)
10845 {
10846 /* Displayed to end of window, but no line containing text was
10847 displayed. Lines were deleted at the end of the window. */
10848 int vpos;
10849 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10850
10851 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10852 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10853 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10854 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10855 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10856 break;
10857
10858 w->window_end_vpos = make_number (vpos);
10859 }
10860 else
10861 abort ();
10862
10863 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
10864 debug_end_vpos = XFASTINT (w->window_end_vpos));
10865
10866 /* Record that display has not been completed. */
10867 w->window_end_valid = Qnil;
10868 w->desired_matrix->no_scrolling_p = 1;
10869 return 3;
10870 }
10871
10872
10873 \f
10874 /***********************************************************************
10875 More debugging support
10876 ***********************************************************************/
10877
10878 #if GLYPH_DEBUG
10879
10880 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
10881 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
10882
10883
10884 /* Dump the contents of glyph matrix MATRIX on stderr. If
10885 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
10886
10887 static void
10888 dump_glyph_matrix (matrix, with_glyphs_p)
10889 struct glyph_matrix *matrix;
10890 int with_glyphs_p;
10891 {
10892 int i;
10893 for (i = 0; i < matrix->nrows; ++i)
10894 dump_glyph_row (matrix, i, with_glyphs_p);
10895 }
10896
10897
10898 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
10899 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
10900
10901 void
10902 dump_glyph_row (matrix, vpos, with_glyphs_p)
10903 struct glyph_matrix *matrix;
10904 int vpos, with_glyphs_p;
10905 {
10906 struct glyph_row *row;
10907
10908 if (vpos < 0 || vpos >= matrix->nrows)
10909 return;
10910
10911 row = MATRIX_ROW (matrix, vpos);
10912
10913 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
10914 fprintf (stderr, "=======================================================================\n");
10915
10916 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1 \
10917 1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
10918 row - matrix->rows,
10919 MATRIX_ROW_START_CHARPOS (row),
10920 MATRIX_ROW_END_CHARPOS (row),
10921 row->used[TEXT_AREA],
10922 row->contains_overlapping_glyphs_p,
10923 row->enabled_p,
10924 row->inverse_p,
10925 row->truncated_on_left_p,
10926 row->truncated_on_right_p,
10927 row->overlay_arrow_p,
10928 row->continued_p,
10929 MATRIX_ROW_CONTINUATION_LINE_P (row),
10930 row->displays_text_p,
10931 row->ends_at_zv_p,
10932 row->fill_line_p,
10933 row->ends_in_middle_of_char_p,
10934 row->starts_in_middle_of_char_p,
10935 row->x,
10936 row->y,
10937 row->pixel_width,
10938 row->height,
10939 row->visible_height,
10940 row->ascent,
10941 row->phys_ascent);
10942 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
10943 row->end.overlay_string_index);
10944 fprintf (stderr, "%9d %5d\n",
10945 CHARPOS (row->start.string_pos),
10946 CHARPOS (row->end.string_pos));
10947 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
10948 row->end.dpvec_index);
10949
10950 if (with_glyphs_p)
10951 {
10952 struct glyph *glyph, *glyph_end;
10953 int prev_had_glyphs_p;
10954
10955 glyph = row->glyphs[TEXT_AREA];
10956 glyph_end = glyph + row->used[TEXT_AREA];
10957
10958 /* Glyph for a line end in text. */
10959 if (glyph == glyph_end && glyph->charpos > 0)
10960 ++glyph_end;
10961
10962 if (glyph < glyph_end)
10963 {
10964 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
10965 prev_had_glyphs_p = 1;
10966 }
10967 else
10968 prev_had_glyphs_p = 0;
10969
10970 while (glyph < glyph_end)
10971 {
10972 if (glyph->type == CHAR_GLYPH)
10973 {
10974 fprintf (stderr,
10975 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
10976 glyph - row->glyphs[TEXT_AREA],
10977 'C',
10978 glyph->charpos,
10979 (BUFFERP (glyph->object)
10980 ? 'B'
10981 : (STRINGP (glyph->object)
10982 ? 'S'
10983 : '-')),
10984 glyph->pixel_width,
10985 glyph->u.ch,
10986 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
10987 ? glyph->u.ch
10988 : '.'),
10989 glyph->face_id,
10990 glyph->left_box_line_p,
10991 glyph->right_box_line_p);
10992 }
10993 else if (glyph->type == STRETCH_GLYPH)
10994 {
10995 fprintf (stderr,
10996 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
10997 glyph - row->glyphs[TEXT_AREA],
10998 'S',
10999 glyph->charpos,
11000 (BUFFERP (glyph->object)
11001 ? 'B'
11002 : (STRINGP (glyph->object)
11003 ? 'S'
11004 : '-')),
11005 glyph->pixel_width,
11006 0,
11007 '.',
11008 glyph->face_id,
11009 glyph->left_box_line_p,
11010 glyph->right_box_line_p);
11011 }
11012 else if (glyph->type == IMAGE_GLYPH)
11013 {
11014 fprintf (stderr,
11015 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11016 glyph - row->glyphs[TEXT_AREA],
11017 'I',
11018 glyph->charpos,
11019 (BUFFERP (glyph->object)
11020 ? 'B'
11021 : (STRINGP (glyph->object)
11022 ? 'S'
11023 : '-')),
11024 glyph->pixel_width,
11025 glyph->u.img_id,
11026 '.',
11027 glyph->face_id,
11028 glyph->left_box_line_p,
11029 glyph->right_box_line_p);
11030 }
11031 ++glyph;
11032 }
11033 }
11034 }
11035
11036
11037 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11038 Sdump_glyph_matrix, 0, 1, "p",
11039 "Dump the current matrix of the selected window to stderr.\n\
11040 Shows contents of glyph row structures. With non-nil optional\n\
11041 parameter WITH-GLYPHS-P, dump glyphs as well.")
11042 (with_glyphs_p)
11043 Lisp_Object with_glyphs_p;
11044 {
11045 struct window *w = XWINDOW (selected_window);
11046 struct buffer *buffer = XBUFFER (w->buffer);
11047
11048 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11049 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11050 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11051 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11052 fprintf (stderr, "=============================================\n");
11053 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11054 return Qnil;
11055 }
11056
11057
11058 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11059 "Dump glyph row ROW to stderr.")
11060 (row)
11061 Lisp_Object row;
11062 {
11063 CHECK_NUMBER (row, 0);
11064 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11065 return Qnil;
11066 }
11067
11068
11069 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11070 0, 0, "", "")
11071 ()
11072 {
11073 struct frame *sf = SELECTED_FRAME ();
11074 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11075 ->current_matrix);
11076 dump_glyph_row (m, 0, 1);
11077 return Qnil;
11078 }
11079
11080
11081 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11082 Strace_redisplay_toggle, 0, 0, "",
11083 "Toggle tracing of redisplay.")
11084 ()
11085 {
11086 trace_redisplay_p = !trace_redisplay_p;
11087 return Qnil;
11088 }
11089
11090
11091 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11092 "Print STRING to stderr.")
11093 (string)
11094 Lisp_Object string;
11095 {
11096 CHECK_STRING (string, 0);
11097 fprintf (stderr, "%s", XSTRING (string)->data);
11098 return Qnil;
11099 }
11100
11101 #endif /* GLYPH_DEBUG */
11102
11103
11104 \f
11105 /***********************************************************************
11106 Building Desired Matrix Rows
11107 ***********************************************************************/
11108
11109 /* Return a temporary glyph row holding the glyphs of an overlay
11110 arrow. Only used for non-window-redisplay windows. */
11111
11112 static struct glyph_row *
11113 get_overlay_arrow_glyph_row (w)
11114 struct window *w;
11115 {
11116 struct frame *f = XFRAME (WINDOW_FRAME (w));
11117 struct buffer *buffer = XBUFFER (w->buffer);
11118 struct buffer *old = current_buffer;
11119 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11120 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11121 unsigned char *arrow_end = arrow_string + arrow_len;
11122 unsigned char *p;
11123 struct it it;
11124 int multibyte_p;
11125 int n_glyphs_before;
11126
11127 set_buffer_temp (buffer);
11128 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11129 it.glyph_row->used[TEXT_AREA] = 0;
11130 SET_TEXT_POS (it.position, 0, 0);
11131
11132 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11133 p = arrow_string;
11134 while (p < arrow_end)
11135 {
11136 Lisp_Object face, ilisp;
11137
11138 /* Get the next character. */
11139 if (multibyte_p)
11140 it.c = string_char_and_length (p, arrow_len, &it.len);
11141 else
11142 it.c = *p, it.len = 1;
11143 p += it.len;
11144
11145 /* Get its face. */
11146 XSETFASTINT (ilisp, p - arrow_string);
11147 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11148 it.face_id = compute_char_face (f, it.c, face);
11149
11150 /* Compute its width, get its glyphs. */
11151 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11152 SET_TEXT_POS (it.position, -1, -1);
11153 PRODUCE_GLYPHS (&it);
11154
11155 /* If this character doesn't fit any more in the line, we have
11156 to remove some glyphs. */
11157 if (it.current_x > it.last_visible_x)
11158 {
11159 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11160 break;
11161 }
11162 }
11163
11164 set_buffer_temp (old);
11165 return it.glyph_row;
11166 }
11167
11168
11169 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11170 glyphs are only inserted for terminal frames since we can't really
11171 win with truncation glyphs when partially visible glyphs are
11172 involved. Which glyphs to insert is determined by
11173 produce_special_glyphs. */
11174
11175 static void
11176 insert_left_trunc_glyphs (it)
11177 struct it *it;
11178 {
11179 struct it truncate_it;
11180 struct glyph *from, *end, *to, *toend;
11181
11182 xassert (!FRAME_WINDOW_P (it->f));
11183
11184 /* Get the truncation glyphs. */
11185 truncate_it = *it;
11186 truncate_it.current_x = 0;
11187 truncate_it.face_id = DEFAULT_FACE_ID;
11188 truncate_it.glyph_row = &scratch_glyph_row;
11189 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11190 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11191 truncate_it.object = make_number (0);
11192 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11193
11194 /* Overwrite glyphs from IT with truncation glyphs. */
11195 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11196 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11197 to = it->glyph_row->glyphs[TEXT_AREA];
11198 toend = to + it->glyph_row->used[TEXT_AREA];
11199
11200 while (from < end)
11201 *to++ = *from++;
11202
11203 /* There may be padding glyphs left over. Remove them. */
11204 from = to;
11205 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11206 ++from;
11207 while (from < toend)
11208 *to++ = *from++;
11209
11210 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11211 }
11212
11213
11214 /* Compute the pixel height and width of IT->glyph_row.
11215
11216 Most of the time, ascent and height of a display line will be equal
11217 to the max_ascent and max_height values of the display iterator
11218 structure. This is not the case if
11219
11220 1. We hit ZV without displaying anything. In this case, max_ascent
11221 and max_height will be zero.
11222
11223 2. We have some glyphs that don't contribute to the line height.
11224 (The glyph row flag contributes_to_line_height_p is for future
11225 pixmap extensions).
11226
11227 The first case is easily covered by using default values because in
11228 these cases, the line height does not really matter, except that it
11229 must not be zero. */
11230
11231 static void
11232 compute_line_metrics (it)
11233 struct it *it;
11234 {
11235 struct glyph_row *row = it->glyph_row;
11236 int area, i;
11237
11238 if (FRAME_WINDOW_P (it->f))
11239 {
11240 int i, header_line_height;
11241
11242 /* The line may consist of one space only, that was added to
11243 place the cursor on it. If so, the row's height hasn't been
11244 computed yet. */
11245 if (row->height == 0)
11246 {
11247 if (it->max_ascent + it->max_descent == 0)
11248 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11249 row->ascent = it->max_ascent;
11250 row->height = it->max_ascent + it->max_descent;
11251 row->phys_ascent = it->max_phys_ascent;
11252 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11253 }
11254
11255 /* Compute the width of this line. */
11256 row->pixel_width = row->x;
11257 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11258 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11259
11260 xassert (row->pixel_width >= 0);
11261 xassert (row->ascent >= 0 && row->height > 0);
11262
11263 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11264 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11265
11266 /* If first line's physical ascent is larger than its logical
11267 ascent, use the physical ascent, and make the row taller.
11268 This makes accented characters fully visible. */
11269 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11270 && row->phys_ascent > row->ascent)
11271 {
11272 row->height += row->phys_ascent - row->ascent;
11273 row->ascent = row->phys_ascent;
11274 }
11275
11276 /* Compute how much of the line is visible. */
11277 row->visible_height = row->height;
11278
11279 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11280 if (row->y < header_line_height)
11281 row->visible_height -= header_line_height - row->y;
11282 else
11283 {
11284 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11285 if (row->y + row->height > max_y)
11286 row->visible_height -= row->y + row->height - max_y;
11287 }
11288 }
11289 else
11290 {
11291 row->pixel_width = row->used[TEXT_AREA];
11292 row->ascent = row->phys_ascent = 0;
11293 row->height = row->phys_height = row->visible_height = 1;
11294 }
11295
11296 /* Compute a hash code for this row. */
11297 row->hash = 0;
11298 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11299 for (i = 0; i < row->used[area]; ++i)
11300 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11301 + row->glyphs[area][i].u.val
11302 + row->glyphs[area][i].face_id
11303 + row->glyphs[area][i].padding_p
11304 + (row->glyphs[area][i].type << 2));
11305
11306 it->max_ascent = it->max_descent = 0;
11307 it->max_phys_ascent = it->max_phys_descent = 0;
11308 }
11309
11310
11311 /* Append one space to the glyph row of iterator IT if doing a
11312 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11313 space have the default face, otherwise let it have the same face as
11314 IT->face_id. Value is non-zero if a space was added.
11315
11316 This function is called to make sure that there is always one glyph
11317 at the end of a glyph row that the cursor can be set on under
11318 window-systems. (If there weren't such a glyph we would not know
11319 how wide and tall a box cursor should be displayed).
11320
11321 At the same time this space let's a nicely handle clearing to the
11322 end of the line if the row ends in italic text. */
11323
11324 static int
11325 append_space (it, default_face_p)
11326 struct it *it;
11327 int default_face_p;
11328 {
11329 if (FRAME_WINDOW_P (it->f))
11330 {
11331 int n = it->glyph_row->used[TEXT_AREA];
11332
11333 if (it->glyph_row->glyphs[TEXT_AREA] + n
11334 < it->glyph_row->glyphs[1 + TEXT_AREA])
11335 {
11336 /* Save some values that must not be changed. */
11337 int saved_x = it->current_x;
11338 struct text_pos saved_pos;
11339 int saved_what = it->what;
11340 int saved_face_id = it->face_id;
11341 Lisp_Object saved_object;
11342 struct face *face;
11343
11344 saved_object = it->object;
11345 saved_pos = it->position;
11346
11347 it->what = IT_CHARACTER;
11348 bzero (&it->position, sizeof it->position);
11349 it->object = make_number (0);
11350 it->c = ' ';
11351 it->len = 1;
11352
11353 if (default_face_p)
11354 it->face_id = DEFAULT_FACE_ID;
11355 face = FACE_FROM_ID (it->f, it->face_id);
11356 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11357
11358 PRODUCE_GLYPHS (it);
11359
11360 it->current_x = saved_x;
11361 it->object = saved_object;
11362 it->position = saved_pos;
11363 it->what = saved_what;
11364 it->face_id = saved_face_id;
11365 return 1;
11366 }
11367 }
11368
11369 return 0;
11370 }
11371
11372
11373 /* Extend the face of the last glyph in the text area of IT->glyph_row
11374 to the end of the display line. Called from display_line.
11375 If the glyph row is empty, add a space glyph to it so that we
11376 know the face to draw. Set the glyph row flag fill_line_p. */
11377
11378 static void
11379 extend_face_to_end_of_line (it)
11380 struct it *it;
11381 {
11382 struct face *face;
11383 struct frame *f = it->f;
11384
11385 /* If line is already filled, do nothing. */
11386 if (it->current_x >= it->last_visible_x)
11387 return;
11388
11389 /* Face extension extends the background and box of IT->face_id
11390 to the end of the line. If the background equals the background
11391 of the frame, we haven't to do anything. */
11392 face = FACE_FROM_ID (f, it->face_id);
11393 if (FRAME_WINDOW_P (f)
11394 && face->box == FACE_NO_BOX
11395 && face->background == FRAME_BACKGROUND_PIXEL (f)
11396 && !face->stipple)
11397 return;
11398
11399 /* Set the glyph row flag indicating that the face of the last glyph
11400 in the text area has to be drawn to the end of the text area. */
11401 it->glyph_row->fill_line_p = 1;
11402
11403 /* If current character of IT is not ASCII, make sure we have the
11404 ASCII face. This will be automatically undone the next time
11405 get_next_display_element returns a multibyte character. Note
11406 that the character will always be single byte in unibyte text. */
11407 if (!SINGLE_BYTE_CHAR_P (it->c))
11408 {
11409 it->face_id = FACE_FOR_CHAR (f, face, 0);
11410 }
11411
11412 if (FRAME_WINDOW_P (f))
11413 {
11414 /* If the row is empty, add a space with the current face of IT,
11415 so that we know which face to draw. */
11416 if (it->glyph_row->used[TEXT_AREA] == 0)
11417 {
11418 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11419 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11420 it->glyph_row->used[TEXT_AREA] = 1;
11421 }
11422 }
11423 else
11424 {
11425 /* Save some values that must not be changed. */
11426 int saved_x = it->current_x;
11427 struct text_pos saved_pos;
11428 Lisp_Object saved_object;
11429 int saved_what = it->what;
11430
11431 saved_object = it->object;
11432 saved_pos = it->position;
11433
11434 it->what = IT_CHARACTER;
11435 bzero (&it->position, sizeof it->position);
11436 it->object = make_number (0);
11437 it->c = ' ';
11438 it->len = 1;
11439
11440 PRODUCE_GLYPHS (it);
11441
11442 while (it->current_x <= it->last_visible_x)
11443 PRODUCE_GLYPHS (it);
11444
11445 /* Don't count these blanks really. It would let us insert a left
11446 truncation glyph below and make us set the cursor on them, maybe. */
11447 it->current_x = saved_x;
11448 it->object = saved_object;
11449 it->position = saved_pos;
11450 it->what = saved_what;
11451 }
11452 }
11453
11454
11455 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11456 trailing whitespace. */
11457
11458 static int
11459 trailing_whitespace_p (charpos)
11460 int charpos;
11461 {
11462 int bytepos = CHAR_TO_BYTE (charpos);
11463 int c = 0;
11464
11465 while (bytepos < ZV_BYTE
11466 && (c = FETCH_CHAR (bytepos),
11467 c == ' ' || c == '\t'))
11468 ++bytepos;
11469
11470 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11471 {
11472 if (bytepos != PT_BYTE)
11473 return 1;
11474 }
11475 return 0;
11476 }
11477
11478
11479 /* Highlight trailing whitespace, if any, in ROW. */
11480
11481 void
11482 highlight_trailing_whitespace (f, row)
11483 struct frame *f;
11484 struct glyph_row *row;
11485 {
11486 int used = row->used[TEXT_AREA];
11487
11488 if (used)
11489 {
11490 struct glyph *start = row->glyphs[TEXT_AREA];
11491 struct glyph *glyph = start + used - 1;
11492
11493 /* Skip over the space glyph inserted to display the
11494 cursor at the end of a line. */
11495 if (glyph->type == CHAR_GLYPH
11496 && glyph->u.ch == ' '
11497 && INTEGERP (glyph->object))
11498 --glyph;
11499
11500 /* If last glyph is a space or stretch, and it's trailing
11501 whitespace, set the face of all trailing whitespace glyphs in
11502 IT->glyph_row to `trailing-whitespace'. */
11503 if (glyph >= start
11504 && BUFFERP (glyph->object)
11505 && (glyph->type == STRETCH_GLYPH
11506 || (glyph->type == CHAR_GLYPH
11507 && glyph->u.ch == ' '))
11508 && trailing_whitespace_p (glyph->charpos))
11509 {
11510 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11511
11512 while (glyph >= start
11513 && BUFFERP (glyph->object)
11514 && (glyph->type == STRETCH_GLYPH
11515 || (glyph->type == CHAR_GLYPH
11516 && glyph->u.ch == ' ')))
11517 (glyph--)->face_id = face_id;
11518 }
11519 }
11520 }
11521
11522
11523 /* Construct the glyph row IT->glyph_row in the desired matrix of
11524 IT->w from text at the current position of IT. See dispextern.h
11525 for an overview of struct it. Value is non-zero if
11526 IT->glyph_row displays text, as opposed to a line displaying ZV
11527 only. */
11528
11529 static int
11530 display_line (it)
11531 struct it *it;
11532 {
11533 struct glyph_row *row = it->glyph_row;
11534
11535 /* We always start displaying at hpos zero even if hscrolled. */
11536 xassert (it->hpos == 0 && it->current_x == 0);
11537
11538 /* We must not display in a row that's not a text row. */
11539 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11540 < it->w->desired_matrix->nrows);
11541
11542 /* Is IT->w showing the region? */
11543 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11544
11545 /* Clear the result glyph row and enable it. */
11546 prepare_desired_row (row);
11547
11548 row->y = it->current_y;
11549 row->start = it->current;
11550 row->continuation_lines_width = it->continuation_lines_width;
11551 row->displays_text_p = 1;
11552 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11553 it->starts_in_middle_of_char_p = 0;
11554
11555 /* Arrange the overlays nicely for our purposes. Usually, we call
11556 display_line on only one line at a time, in which case this
11557 can't really hurt too much, or we call it on lines which appear
11558 one after another in the buffer, in which case all calls to
11559 recenter_overlay_lists but the first will be pretty cheap. */
11560 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11561
11562 /* Move over display elements that are not visible because we are
11563 hscrolled. This may stop at an x-position < IT->first_visible_x
11564 if the first glyph is partially visible or if we hit a line end. */
11565 if (it->current_x < it->first_visible_x)
11566 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11567 MOVE_TO_POS | MOVE_TO_X);
11568
11569 /* Get the initial row height. This is either the height of the
11570 text hscrolled, if there is any, or zero. */
11571 row->ascent = it->max_ascent;
11572 row->height = it->max_ascent + it->max_descent;
11573 row->phys_ascent = it->max_phys_ascent;
11574 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11575
11576 /* Loop generating characters. The loop is left with IT on the next
11577 character to display. */
11578 while (1)
11579 {
11580 int n_glyphs_before, hpos_before, x_before;
11581 int x, i, nglyphs;
11582 int ascent, descent, phys_ascent, phys_descent;
11583
11584 /* Retrieve the next thing to display. Value is zero if end of
11585 buffer reached. */
11586 if (!get_next_display_element (it))
11587 {
11588 /* Maybe add a space at the end of this line that is used to
11589 display the cursor there under X. Set the charpos of the
11590 first glyph of blank lines not corresponding to any text
11591 to -1. */
11592 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11593 || row->used[TEXT_AREA] == 0)
11594 {
11595 row->glyphs[TEXT_AREA]->charpos = -1;
11596 row->displays_text_p = 0;
11597
11598 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11599 row->indicate_empty_line_p = 1;
11600 }
11601
11602 it->continuation_lines_width = 0;
11603 row->ends_at_zv_p = 1;
11604 break;
11605 }
11606
11607 /* Now, get the metrics of what we want to display. This also
11608 generates glyphs in `row' (which is IT->glyph_row). */
11609 n_glyphs_before = row->used[TEXT_AREA];
11610 x = it->current_x;
11611
11612 /* Remember the line height so far in case the next element doesn't
11613 fit on the line. */
11614 if (!it->truncate_lines_p)
11615 {
11616 ascent = it->max_ascent;
11617 descent = it->max_descent;
11618 phys_ascent = it->max_phys_ascent;
11619 phys_descent = it->max_phys_descent;
11620 }
11621
11622 PRODUCE_GLYPHS (it);
11623
11624 /* If this display element was in marginal areas, continue with
11625 the next one. */
11626 if (it->area != TEXT_AREA)
11627 {
11628 row->ascent = max (row->ascent, it->max_ascent);
11629 row->height = max (row->height, it->max_ascent + it->max_descent);
11630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11631 row->phys_height = max (row->phys_height,
11632 it->max_phys_ascent + it->max_phys_descent);
11633 set_iterator_to_next (it);
11634 continue;
11635 }
11636
11637 /* Does the display element fit on the line? If we truncate
11638 lines, we should draw past the right edge of the window. If
11639 we don't truncate, we want to stop so that we can display the
11640 continuation glyph before the right margin. If lines are
11641 continued, there are two possible strategies for characters
11642 resulting in more than 1 glyph (e.g. tabs): Display as many
11643 glyphs as possible in this line and leave the rest for the
11644 continuation line, or display the whole element in the next
11645 line. Original redisplay did the former, so we do it also. */
11646 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11647 hpos_before = it->hpos;
11648 x_before = x;
11649
11650 if (nglyphs == 1
11651 && it->current_x < it->last_visible_x)
11652 {
11653 ++it->hpos;
11654 row->ascent = max (row->ascent, it->max_ascent);
11655 row->height = max (row->height, it->max_ascent + it->max_descent);
11656 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11657 row->phys_height = max (row->phys_height,
11658 it->max_phys_ascent + it->max_phys_descent);
11659 if (it->current_x - it->pixel_width < it->first_visible_x)
11660 row->x = x - it->first_visible_x;
11661 }
11662 else
11663 {
11664 int new_x;
11665 struct glyph *glyph;
11666
11667 for (i = 0; i < nglyphs; ++i, x = new_x)
11668 {
11669 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11670 new_x = x + glyph->pixel_width;
11671
11672 if (/* Lines are continued. */
11673 !it->truncate_lines_p
11674 && (/* Glyph doesn't fit on the line. */
11675 new_x > it->last_visible_x
11676 /* Or it fits exactly on a window system frame. */
11677 || (new_x == it->last_visible_x
11678 && FRAME_WINDOW_P (it->f))))
11679 {
11680 /* End of a continued line. */
11681
11682 if (it->hpos == 0
11683 || (new_x == it->last_visible_x
11684 && FRAME_WINDOW_P (it->f)))
11685 {
11686 /* Current glyph is the only one on the line or
11687 fits exactly on the line. We must continue
11688 the line because we can't draw the cursor
11689 after the glyph. */
11690 row->continued_p = 1;
11691 it->current_x = new_x;
11692 it->continuation_lines_width += new_x;
11693 ++it->hpos;
11694 if (i == nglyphs - 1)
11695 set_iterator_to_next (it);
11696 }
11697 else if (CHAR_GLYPH_PADDING_P (*glyph)
11698 && !FRAME_WINDOW_P (it->f))
11699 {
11700 /* A padding glyph that doesn't fit on this line.
11701 This means the whole character doesn't fit
11702 on the line. */
11703 row->used[TEXT_AREA] = n_glyphs_before;
11704
11705 /* Fill the rest of the row with continuation
11706 glyphs like in 20.x. */
11707 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11708 < row->glyphs[1 + TEXT_AREA])
11709 produce_special_glyphs (it, IT_CONTINUATION);
11710
11711 row->continued_p = 1;
11712 it->current_x = x_before;
11713 it->continuation_lines_width += x_before;
11714
11715 /* Restore the height to what it was before the
11716 element not fitting on the line. */
11717 it->max_ascent = ascent;
11718 it->max_descent = descent;
11719 it->max_phys_ascent = phys_ascent;
11720 it->max_phys_descent = phys_descent;
11721 }
11722 else
11723 {
11724 /* Display element draws past the right edge of
11725 the window. Restore positions to values
11726 before the element. The next line starts
11727 with current_x before the glyph that could
11728 not be displayed, so that TAB works right. */
11729 row->used[TEXT_AREA] = n_glyphs_before + i;
11730
11731 /* Display continuation glyphs. */
11732 if (!FRAME_WINDOW_P (it->f))
11733 produce_special_glyphs (it, IT_CONTINUATION);
11734 row->continued_p = 1;
11735
11736 it->current_x = x;
11737 it->continuation_lines_width += x;
11738 if (nglyphs > 1 && i > 0)
11739 {
11740 row->ends_in_middle_of_char_p = 1;
11741 it->starts_in_middle_of_char_p = 1;
11742 }
11743
11744 /* Restore the height to what it was before the
11745 element not fitting on the line. */
11746 it->max_ascent = ascent;
11747 it->max_descent = descent;
11748 it->max_phys_ascent = phys_ascent;
11749 it->max_phys_descent = phys_descent;
11750 }
11751
11752 break;
11753 }
11754 else if (new_x > it->first_visible_x)
11755 {
11756 /* Increment number of glyphs actually displayed. */
11757 ++it->hpos;
11758
11759 if (x < it->first_visible_x)
11760 /* Glyph is partially visible, i.e. row starts at
11761 negative X position. */
11762 row->x = x - it->first_visible_x;
11763 }
11764 else
11765 {
11766 /* Glyph is completely off the left margin of the
11767 window. This should not happen because of the
11768 move_it_in_display_line at the start of
11769 this function. */
11770 abort ();
11771 }
11772 }
11773
11774 row->ascent = max (row->ascent, it->max_ascent);
11775 row->height = max (row->height, it->max_ascent + it->max_descent);
11776 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11777 row->phys_height = max (row->phys_height,
11778 it->max_phys_ascent + it->max_phys_descent);
11779
11780 /* End of this display line if row is continued. */
11781 if (row->continued_p)
11782 break;
11783 }
11784
11785 /* Is this a line end? If yes, we're also done, after making
11786 sure that a non-default face is extended up to the right
11787 margin of the window. */
11788 if (ITERATOR_AT_END_OF_LINE_P (it))
11789 {
11790 int used_before = row->used[TEXT_AREA];
11791
11792 /* Add a space at the end of the line that is used to
11793 display the cursor there. */
11794 append_space (it, 0);
11795
11796 /* Extend the face to the end of the line. */
11797 extend_face_to_end_of_line (it);
11798
11799 /* Make sure we have the position. */
11800 if (used_before == 0)
11801 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11802
11803 /* Consume the line end. This skips over invisible lines. */
11804 set_iterator_to_next (it);
11805 it->continuation_lines_width = 0;
11806 break;
11807 }
11808
11809 /* Proceed with next display element. Note that this skips
11810 over lines invisible because of selective display. */
11811 set_iterator_to_next (it);
11812
11813 /* If we truncate lines, we are done when the last displayed
11814 glyphs reach past the right margin of the window. */
11815 if (it->truncate_lines_p
11816 && (FRAME_WINDOW_P (it->f)
11817 ? (it->current_x >= it->last_visible_x)
11818 : (it->current_x > it->last_visible_x)))
11819 {
11820 /* Maybe add truncation glyphs. */
11821 if (!FRAME_WINDOW_P (it->f))
11822 {
11823 --it->glyph_row->used[TEXT_AREA];
11824 produce_special_glyphs (it, IT_TRUNCATION);
11825 }
11826
11827 row->truncated_on_right_p = 1;
11828 it->continuation_lines_width = 0;
11829 reseat_at_next_visible_line_start (it, 0);
11830 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
11831 it->hpos = hpos_before;
11832 it->current_x = x_before;
11833 break;
11834 }
11835 }
11836
11837 /* If line is not empty and hscrolled, maybe insert truncation glyphs
11838 at the left window margin. */
11839 if (it->first_visible_x
11840 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
11841 {
11842 if (!FRAME_WINDOW_P (it->f))
11843 insert_left_trunc_glyphs (it);
11844 row->truncated_on_left_p = 1;
11845 }
11846
11847 /* If the start of this line is the overlay arrow-position, then
11848 mark this glyph row as the one containing the overlay arrow.
11849 This is clearly a mess with variable size fonts. It would be
11850 better to let it be displayed like cursors under X. */
11851 if (MARKERP (Voverlay_arrow_position)
11852 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
11853 && (MATRIX_ROW_START_CHARPOS (row)
11854 == marker_position (Voverlay_arrow_position))
11855 && STRINGP (Voverlay_arrow_string)
11856 && ! overlay_arrow_seen)
11857 {
11858 /* Overlay arrow in window redisplay is a bitmap. */
11859 if (!FRAME_WINDOW_P (it->f))
11860 {
11861 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
11862 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
11863 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
11864 struct glyph *p = row->glyphs[TEXT_AREA];
11865 struct glyph *p2, *end;
11866
11867 /* Copy the arrow glyphs. */
11868 while (glyph < arrow_end)
11869 *p++ = *glyph++;
11870
11871 /* Throw away padding glyphs. */
11872 p2 = p;
11873 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
11874 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
11875 ++p2;
11876 if (p2 > p)
11877 {
11878 while (p2 < end)
11879 *p++ = *p2++;
11880 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
11881 }
11882 }
11883
11884 overlay_arrow_seen = 1;
11885 row->overlay_arrow_p = 1;
11886 }
11887
11888 /* Compute pixel dimensions of this line. */
11889 compute_line_metrics (it);
11890
11891 /* Remember the position at which this line ends. */
11892 row->end = it->current;
11893
11894 /* Maybe set the cursor. */
11895 if (it->w->cursor.vpos < 0
11896 && PT >= MATRIX_ROW_START_CHARPOS (row)
11897 && PT <= MATRIX_ROW_END_CHARPOS (row))
11898 {
11899 /* Also see redisplay_window, case cursor movement in unchanged
11900 window. */
11901 if (MATRIX_ROW_END_CHARPOS (row) == PT
11902 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11903 && !row->ends_at_zv_p)
11904 ;
11905 else
11906 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
11907 }
11908
11909 /* Highlight trailing whitespace. */
11910 if (!NILP (Vshow_trailing_whitespace))
11911 highlight_trailing_whitespace (it->f, it->glyph_row);
11912
11913 /* Prepare for the next line. This line starts horizontally at (X
11914 HPOS) = (0 0). Vertical positions are incremented. As a
11915 convenience for the caller, IT->glyph_row is set to the next
11916 row to be used. */
11917 it->current_x = it->hpos = 0;
11918 it->current_y += row->height;
11919 ++it->vpos;
11920 ++it->glyph_row;
11921 return row->displays_text_p;
11922 }
11923
11924
11925 \f
11926 /***********************************************************************
11927 Menu Bar
11928 ***********************************************************************/
11929
11930 /* Redisplay the menu bar in the frame for window W.
11931
11932 The menu bar of X frames that don't have X toolkit support is
11933 displayed in a special window W->frame->menu_bar_window.
11934
11935 The menu bar of terminal frames is treated specially as far as
11936 glyph matrices are concerned. Menu bar lines are not part of
11937 windows, so the update is done directly on the frame matrix rows
11938 for the menu bar. */
11939
11940 static void
11941 display_menu_bar (w)
11942 struct window *w;
11943 {
11944 struct frame *f = XFRAME (WINDOW_FRAME (w));
11945 struct it it;
11946 Lisp_Object items;
11947 int i;
11948
11949 /* Don't do all this for graphical frames. */
11950 #ifdef HAVE_NTGUI
11951 if (!NILP (Vwindow_system))
11952 return;
11953 #endif
11954 #ifdef USE_X_TOOLKIT
11955 if (FRAME_X_P (f))
11956 return;
11957 #endif
11958
11959 #ifdef USE_X_TOOLKIT
11960 xassert (!FRAME_WINDOW_P (f));
11961 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
11962 it.first_visible_x = 0;
11963 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11964 #else /* not USE_X_TOOLKIT */
11965 if (FRAME_WINDOW_P (f))
11966 {
11967 /* Menu bar lines are displayed in the desired matrix of the
11968 dummy window menu_bar_window. */
11969 struct window *menu_w;
11970 xassert (WINDOWP (f->menu_bar_window));
11971 menu_w = XWINDOW (f->menu_bar_window);
11972 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
11973 MENU_FACE_ID);
11974 it.first_visible_x = 0;
11975 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
11976 }
11977 else
11978 {
11979 /* This is a TTY frame, i.e. character hpos/vpos are used as
11980 pixel x/y. */
11981 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
11982 MENU_FACE_ID);
11983 it.first_visible_x = 0;
11984 it.last_visible_x = FRAME_WIDTH (f);
11985 }
11986 #endif /* not USE_X_TOOLKIT */
11987
11988 /* Clear all rows of the menu bar. */
11989 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
11990 {
11991 struct glyph_row *row = it.glyph_row + i;
11992 clear_glyph_row (row);
11993 row->enabled_p = 1;
11994 row->full_width_p = 1;
11995 }
11996
11997 /* Make the first line of the menu bar appear in reverse video. */
11998 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
11999
12000 /* Display all items of the menu bar. */
12001 items = FRAME_MENU_BAR_ITEMS (it.f);
12002 for (i = 0; i < XVECTOR (items)->size; i += 4)
12003 {
12004 Lisp_Object string;
12005
12006 /* Stop at nil string. */
12007 string = XVECTOR (items)->contents[i + 1];
12008 if (NILP (string))
12009 break;
12010
12011 /* Remember where item was displayed. */
12012 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12013
12014 /* Display the item, pad with one space. */
12015 if (it.current_x < it.last_visible_x)
12016 display_string (NULL, string, Qnil, 0, 0, &it,
12017 XSTRING (string)->size + 1, 0, 0, -1);
12018 }
12019
12020 /* Fill out the line with spaces. */
12021 if (it.current_x < it.last_visible_x)
12022 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12023
12024 /* Compute the total height of the lines. */
12025 compute_line_metrics (&it);
12026 }
12027
12028
12029 \f
12030 /***********************************************************************
12031 Mode Line
12032 ***********************************************************************/
12033
12034 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12035 FORCE is non-zero, redisplay mode lines unconditionally.
12036 Otherwise, redisplay only mode lines that are garbaged. Value is
12037 the number of windows whose mode lines were redisplayed. */
12038
12039 static int
12040 redisplay_mode_lines (window, force)
12041 Lisp_Object window;
12042 int force;
12043 {
12044 int nwindows = 0;
12045
12046 while (!NILP (window))
12047 {
12048 struct window *w = XWINDOW (window);
12049
12050 if (WINDOWP (w->hchild))
12051 nwindows += redisplay_mode_lines (w->hchild, force);
12052 else if (WINDOWP (w->vchild))
12053 nwindows += redisplay_mode_lines (w->vchild, force);
12054 else if (force
12055 || FRAME_GARBAGED_P (XFRAME (w->frame))
12056 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12057 {
12058 Lisp_Object old_selected_frame;
12059 struct text_pos lpoint;
12060 struct buffer *old = current_buffer;
12061
12062 /* Set the window's buffer for the mode line display. */
12063 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12064 set_buffer_internal_1 (XBUFFER (w->buffer));
12065
12066 /* Point refers normally to the selected window. For any
12067 other window, set up appropriate value. */
12068 if (!EQ (window, selected_window))
12069 {
12070 struct text_pos pt;
12071
12072 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12073 if (CHARPOS (pt) < BEGV)
12074 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12075 else if (CHARPOS (pt) > (ZV - 1))
12076 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12077 else
12078 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12079 }
12080
12081 /* Temporarily set up the selected frame. */
12082 old_selected_frame = selected_frame;
12083 selected_frame = w->frame;
12084
12085 /* Display mode lines. */
12086 clear_glyph_matrix (w->desired_matrix);
12087 if (display_mode_lines (w))
12088 {
12089 ++nwindows;
12090 w->must_be_updated_p = 1;
12091 }
12092
12093 /* Restore old settings. */
12094 selected_frame = old_selected_frame;
12095 set_buffer_internal_1 (old);
12096 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12097 }
12098
12099 window = w->next;
12100 }
12101
12102 return nwindows;
12103 }
12104
12105
12106 /* Display the mode and/or top line of window W. Value is the number
12107 of mode lines displayed. */
12108
12109 static int
12110 display_mode_lines (w)
12111 struct window *w;
12112 {
12113 int n = 0;
12114
12115 /* These will be set while the mode line specs are processed. */
12116 line_number_displayed = 0;
12117 w->column_number_displayed = Qnil;
12118
12119 if (WINDOW_WANTS_MODELINE_P (w))
12120 {
12121 display_mode_line (w, MODE_LINE_FACE_ID,
12122 current_buffer->mode_line_format);
12123 ++n;
12124 }
12125
12126 if (WINDOW_WANTS_HEADER_LINE_P (w))
12127 {
12128 display_mode_line (w, HEADER_LINE_FACE_ID,
12129 current_buffer->header_line_format);
12130 ++n;
12131 }
12132
12133 return n;
12134 }
12135
12136
12137 /* Display mode or top line of window W. FACE_ID specifies which line
12138 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12139 FORMAT is the mode line format to display. */
12140
12141 static void
12142 display_mode_line (w, face_id, format)
12143 struct window *w;
12144 enum face_id face_id;
12145 Lisp_Object format;
12146 {
12147 struct it it;
12148 struct face *face;
12149
12150 init_iterator (&it, w, -1, -1, NULL, face_id);
12151 prepare_desired_row (it.glyph_row);
12152
12153 /* Temporarily make frame's keyboard the current kboard so that
12154 kboard-local variables in the mode_line_format will get the right
12155 values. */
12156 push_frame_kboard (it.f);
12157 display_mode_element (&it, 0, 0, 0, format);
12158 pop_frame_kboard ();
12159
12160 /* Fill up with spaces. */
12161 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12162
12163 compute_line_metrics (&it);
12164 it.glyph_row->full_width_p = 1;
12165 it.glyph_row->mode_line_p = 1;
12166 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12167 it.glyph_row->continued_p = 0;
12168 it.glyph_row->truncated_on_left_p = 0;
12169 it.glyph_row->truncated_on_right_p = 0;
12170
12171 /* Make a 3D mode-line have a shadow at its right end. */
12172 face = FACE_FROM_ID (it.f, face_id);
12173 extend_face_to_end_of_line (&it);
12174 if (face->box != FACE_NO_BOX)
12175 {
12176 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12177 + it.glyph_row->used[TEXT_AREA] - 1);
12178 last->right_box_line_p = 1;
12179 }
12180 }
12181
12182
12183 /* Contribute ELT to the mode line for window IT->w. How it
12184 translates into text depends on its data type.
12185
12186 IT describes the display environment in which we display, as usual.
12187
12188 DEPTH is the depth in recursion. It is used to prevent
12189 infinite recursion here.
12190
12191 FIELD_WIDTH is the number of characters the display of ELT should
12192 occupy in the mode line, and PRECISION is the maximum number of
12193 characters to display from ELT's representation. See
12194 display_string for details. *
12195
12196 Returns the hpos of the end of the text generated by ELT. */
12197
12198 static int
12199 display_mode_element (it, depth, field_width, precision, elt)
12200 struct it *it;
12201 int depth;
12202 int field_width, precision;
12203 Lisp_Object elt;
12204 {
12205 int n = 0, field, prec;
12206
12207 tail_recurse:
12208 if (depth > 10)
12209 goto invalid;
12210
12211 depth++;
12212
12213 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12214 {
12215 case Lisp_String:
12216 {
12217 /* A string: output it and check for %-constructs within it. */
12218 unsigned char c;
12219 unsigned char *this = XSTRING (elt)->data;
12220 unsigned char *lisp_string = this;
12221
12222 while ((precision <= 0 || n < precision)
12223 && *this
12224 && (frame_title_ptr
12225 || it->current_x < it->last_visible_x))
12226 {
12227 unsigned char *last = this;
12228
12229 /* Advance to end of string or next format specifier. */
12230 while ((c = *this++) != '\0' && c != '%')
12231 ;
12232
12233 if (this - 1 != last)
12234 {
12235 /* Output to end of string or up to '%'. Field width
12236 is length of string. Don't output more than
12237 PRECISION allows us. */
12238 prec = --this - last;
12239 if (precision > 0 && prec > precision - n)
12240 prec = precision - n;
12241
12242 if (frame_title_ptr)
12243 n += store_frame_title (last, prec, prec);
12244 else
12245 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12246 it, 0, prec, 0, -1);
12247 }
12248 else /* c == '%' */
12249 {
12250 unsigned char *percent_position = this;
12251
12252 /* Get the specified minimum width. Zero means
12253 don't pad. */
12254 field = 0;
12255 while ((c = *this++) >= '0' && c <= '9')
12256 field = field * 10 + c - '0';
12257
12258 /* Don't pad beyond the total padding allowed. */
12259 if (field_width - n > 0 && field > field_width - n)
12260 field = field_width - n;
12261
12262 /* Note that either PRECISION <= 0 or N < PRECISION. */
12263 prec = precision - n;
12264
12265 if (c == 'M')
12266 n += display_mode_element (it, depth, field, prec,
12267 Vglobal_mode_string);
12268 else if (c != 0)
12269 {
12270 unsigned char *spec
12271 = decode_mode_spec (it->w, c, field, prec);
12272
12273 if (frame_title_ptr)
12274 n += store_frame_title (spec, field, prec);
12275 else
12276 {
12277 int nglyphs_before
12278 = it->glyph_row->used[TEXT_AREA];
12279 int charpos
12280 = percent_position - XSTRING (elt)->data;
12281 int nwritten
12282 = display_string (spec, Qnil, elt, charpos, 0, it,
12283 field, prec, 0, -1);
12284
12285 /* Assign to the glyphs written above the
12286 string where the `%x' came from, position
12287 of the `%'. */
12288 if (nwritten > 0)
12289 {
12290 struct glyph *glyph
12291 = (it->glyph_row->glyphs[TEXT_AREA]
12292 + nglyphs_before);
12293 int i;
12294
12295 for (i = 0; i < nwritten; ++i)
12296 {
12297 glyph[i].object = elt;
12298 glyph[i].charpos = charpos;
12299 }
12300
12301 n += nwritten;
12302 }
12303 }
12304 }
12305 }
12306 }
12307 }
12308 break;
12309
12310 case Lisp_Symbol:
12311 /* A symbol: process the value of the symbol recursively
12312 as if it appeared here directly. Avoid error if symbol void.
12313 Special case: if value of symbol is a string, output the string
12314 literally. */
12315 {
12316 register Lisp_Object tem;
12317 tem = Fboundp (elt);
12318 if (!NILP (tem))
12319 {
12320 tem = Fsymbol_value (elt);
12321 /* If value is a string, output that string literally:
12322 don't check for % within it. */
12323 if (STRINGP (tem))
12324 {
12325 prec = XSTRING (tem)->size;
12326 if (precision > 0 && prec > precision - n)
12327 prec = precision - n;
12328 if (frame_title_ptr)
12329 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12330 else
12331 n += display_string (NULL, tem, Qnil, 0, 0, it,
12332 0, prec, 0, -1);
12333 }
12334 else if (!EQ (tem, elt))
12335 {
12336 /* Give up right away for nil or t. */
12337 elt = tem;
12338 goto tail_recurse;
12339 }
12340 }
12341 }
12342 break;
12343
12344 case Lisp_Cons:
12345 {
12346 register Lisp_Object car, tem;
12347
12348 /* A cons cell: three distinct cases.
12349 If first element is a string or a cons, process all the elements
12350 and effectively concatenate them.
12351 If first element is a negative number, truncate displaying cdr to
12352 at most that many characters. If positive, pad (with spaces)
12353 to at least that many characters.
12354 If first element is a symbol, process the cadr or caddr recursively
12355 according to whether the symbol's value is non-nil or nil. */
12356 car = XCAR (elt);
12357 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12358 {
12359 /* An element of the form (:eval FORM) means evaluate FORM
12360 and use the result as mode line elements. */
12361 struct gcpro gcpro1;
12362 Lisp_Object spec;
12363
12364 spec = eval_form (XCAR (XCDR (elt)));
12365 GCPRO1 (spec);
12366 n += display_mode_element (it, depth, field_width - n,
12367 precision - n, spec);
12368 UNGCPRO;
12369 }
12370 else if (SYMBOLP (car))
12371 {
12372 tem = Fboundp (car);
12373 elt = XCDR (elt);
12374 if (!CONSP (elt))
12375 goto invalid;
12376 /* elt is now the cdr, and we know it is a cons cell.
12377 Use its car if CAR has a non-nil value. */
12378 if (!NILP (tem))
12379 {
12380 tem = Fsymbol_value (car);
12381 if (!NILP (tem))
12382 {
12383 elt = XCAR (elt);
12384 goto tail_recurse;
12385 }
12386 }
12387 /* Symbol's value is nil (or symbol is unbound)
12388 Get the cddr of the original list
12389 and if possible find the caddr and use that. */
12390 elt = XCDR (elt);
12391 if (NILP (elt))
12392 break;
12393 else if (!CONSP (elt))
12394 goto invalid;
12395 elt = XCAR (elt);
12396 goto tail_recurse;
12397 }
12398 else if (INTEGERP (car))
12399 {
12400 register int lim = XINT (car);
12401 elt = XCDR (elt);
12402 if (lim < 0)
12403 {
12404 /* Negative int means reduce maximum width. */
12405 if (precision <= 0)
12406 precision = -lim;
12407 else
12408 precision = min (precision, -lim);
12409 }
12410 else if (lim > 0)
12411 {
12412 /* Padding specified. Don't let it be more than
12413 current maximum. */
12414 if (precision > 0)
12415 lim = min (precision, lim);
12416
12417 /* If that's more padding than already wanted, queue it.
12418 But don't reduce padding already specified even if
12419 that is beyond the current truncation point. */
12420 field_width = max (lim, field_width);
12421 }
12422 goto tail_recurse;
12423 }
12424 else if (STRINGP (car) || CONSP (car))
12425 {
12426 register int limit = 50;
12427 /* Limit is to protect against circular lists. */
12428 while (CONSP (elt)
12429 && --limit > 0
12430 && (precision <= 0 || n < precision))
12431 {
12432 n += display_mode_element (it, depth, field_width - n,
12433 precision - n, XCAR (elt));
12434 elt = XCDR (elt);
12435 }
12436 }
12437 }
12438 break;
12439
12440 default:
12441 invalid:
12442 if (frame_title_ptr)
12443 n += store_frame_title ("*invalid*", 0, precision - n);
12444 else
12445 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12446 precision - n, 0, 0);
12447 return n;
12448 }
12449
12450 /* Pad to FIELD_WIDTH. */
12451 if (field_width > 0 && n < field_width)
12452 {
12453 if (frame_title_ptr)
12454 n += store_frame_title ("", field_width - n, 0);
12455 else
12456 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12457 0, 0, 0);
12458 }
12459
12460 return n;
12461 }
12462
12463
12464 /* Write a null-terminated, right justified decimal representation of
12465 the positive integer D to BUF using a minimal field width WIDTH. */
12466
12467 static void
12468 pint2str (buf, width, d)
12469 register char *buf;
12470 register int width;
12471 register int d;
12472 {
12473 register char *p = buf;
12474
12475 if (d <= 0)
12476 *p++ = '0';
12477 else
12478 {
12479 while (d > 0)
12480 {
12481 *p++ = d % 10 + '0';
12482 d /= 10;
12483 }
12484 }
12485
12486 for (width -= (int) (p - buf); width > 0; --width)
12487 *p++ = ' ';
12488 *p-- = '\0';
12489 while (p > buf)
12490 {
12491 d = *buf;
12492 *buf++ = *p;
12493 *p-- = d;
12494 }
12495 }
12496
12497 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12498 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12499 type of CODING_SYSTEM. Return updated pointer into BUF. */
12500
12501 static unsigned char invalid_eol_type[] = "(*invalid*)";
12502
12503 static char *
12504 decode_mode_spec_coding (coding_system, buf, eol_flag)
12505 Lisp_Object coding_system;
12506 register char *buf;
12507 int eol_flag;
12508 {
12509 Lisp_Object val;
12510 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12511 unsigned char *eol_str;
12512 int eol_str_len;
12513 /* The EOL conversion we are using. */
12514 Lisp_Object eoltype;
12515
12516 val = Fget (coding_system, Qcoding_system);
12517
12518 if (!VECTORP (val)) /* Not yet decided. */
12519 {
12520 if (multibyte)
12521 *buf++ = '-';
12522 if (eol_flag)
12523 eoltype = eol_mnemonic_undecided;
12524 /* Don't mention EOL conversion if it isn't decided. */
12525 }
12526 else
12527 {
12528 Lisp_Object eolvalue;
12529
12530 eolvalue = Fget (coding_system, Qeol_type);
12531
12532 if (multibyte)
12533 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12534
12535 if (eol_flag)
12536 {
12537 /* The EOL conversion that is normal on this system. */
12538
12539 if (NILP (eolvalue)) /* Not yet decided. */
12540 eoltype = eol_mnemonic_undecided;
12541 else if (VECTORP (eolvalue)) /* Not yet decided. */
12542 eoltype = eol_mnemonic_undecided;
12543 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12544 eoltype = (XFASTINT (eolvalue) == 0
12545 ? eol_mnemonic_unix
12546 : (XFASTINT (eolvalue) == 1
12547 ? eol_mnemonic_dos : eol_mnemonic_mac));
12548 }
12549 }
12550
12551 if (eol_flag)
12552 {
12553 /* Mention the EOL conversion if it is not the usual one. */
12554 if (STRINGP (eoltype))
12555 {
12556 eol_str = XSTRING (eoltype)->data;
12557 eol_str_len = XSTRING (eoltype)->size;
12558 }
12559 else if (INTEGERP (eoltype)
12560 && CHAR_VALID_P (XINT (eoltype), 0))
12561 {
12562 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12563 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12564 }
12565 else
12566 {
12567 eol_str = invalid_eol_type;
12568 eol_str_len = sizeof (invalid_eol_type) - 1;
12569 }
12570 bcopy (eol_str, buf, eol_str_len);
12571 buf += eol_str_len;
12572 }
12573
12574 return buf;
12575 }
12576
12577 /* Return a string for the output of a mode line %-spec for window W,
12578 generated by character C. PRECISION >= 0 means don't return a
12579 string longer than that value. FIELD_WIDTH > 0 means pad the
12580 string returned with spaces to that value. */
12581
12582 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12583
12584 static char *
12585 decode_mode_spec (w, c, field_width, precision)
12586 struct window *w;
12587 register int c;
12588 int field_width, precision;
12589 {
12590 Lisp_Object obj;
12591 struct frame *f = XFRAME (WINDOW_FRAME (w));
12592 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12593 struct buffer *b = XBUFFER (w->buffer);
12594
12595 obj = Qnil;
12596
12597 switch (c)
12598 {
12599 case '*':
12600 if (!NILP (b->read_only))
12601 return "%";
12602 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12603 return "*";
12604 return "-";
12605
12606 case '+':
12607 /* This differs from %* only for a modified read-only buffer. */
12608 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12609 return "*";
12610 if (!NILP (b->read_only))
12611 return "%";
12612 return "-";
12613
12614 case '&':
12615 /* This differs from %* in ignoring read-only-ness. */
12616 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12617 return "*";
12618 return "-";
12619
12620 case '%':
12621 return "%";
12622
12623 case '[':
12624 {
12625 int i;
12626 char *p;
12627
12628 if (command_loop_level > 5)
12629 return "[[[... ";
12630 p = decode_mode_spec_buf;
12631 for (i = 0; i < command_loop_level; i++)
12632 *p++ = '[';
12633 *p = 0;
12634 return decode_mode_spec_buf;
12635 }
12636
12637 case ']':
12638 {
12639 int i;
12640 char *p;
12641
12642 if (command_loop_level > 5)
12643 return " ...]]]";
12644 p = decode_mode_spec_buf;
12645 for (i = 0; i < command_loop_level; i++)
12646 *p++ = ']';
12647 *p = 0;
12648 return decode_mode_spec_buf;
12649 }
12650
12651 case '-':
12652 {
12653 register int i;
12654
12655 /* Let lots_of_dashes be a string of infinite length. */
12656 if (field_width <= 0
12657 || field_width > sizeof (lots_of_dashes))
12658 {
12659 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12660 decode_mode_spec_buf[i] = '-';
12661 decode_mode_spec_buf[i] = '\0';
12662 return decode_mode_spec_buf;
12663 }
12664 else
12665 return lots_of_dashes;
12666 }
12667
12668 case 'b':
12669 obj = b->name;
12670 break;
12671
12672 case 'c':
12673 {
12674 int col = current_column ();
12675 XSETFASTINT (w->column_number_displayed, col);
12676 pint2str (decode_mode_spec_buf, field_width, col);
12677 return decode_mode_spec_buf;
12678 }
12679
12680 case 'F':
12681 /* %F displays the frame name. */
12682 if (!NILP (f->title))
12683 return (char *) XSTRING (f->title)->data;
12684 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12685 return (char *) XSTRING (f->name)->data;
12686 return "Emacs";
12687
12688 case 'f':
12689 obj = b->filename;
12690 break;
12691
12692 case 'l':
12693 {
12694 int startpos = XMARKER (w->start)->charpos;
12695 int startpos_byte = marker_byte_position (w->start);
12696 int line, linepos, linepos_byte, topline;
12697 int nlines, junk;
12698 int height = XFASTINT (w->height);
12699
12700 /* If we decided that this buffer isn't suitable for line numbers,
12701 don't forget that too fast. */
12702 if (EQ (w->base_line_pos, w->buffer))
12703 goto no_value;
12704 /* But do forget it, if the window shows a different buffer now. */
12705 else if (BUFFERP (w->base_line_pos))
12706 w->base_line_pos = Qnil;
12707
12708 /* If the buffer is very big, don't waste time. */
12709 if (INTEGERP (Vline_number_display_limit)
12710 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
12711 {
12712 w->base_line_pos = Qnil;
12713 w->base_line_number = Qnil;
12714 goto no_value;
12715 }
12716
12717 if (!NILP (w->base_line_number)
12718 && !NILP (w->base_line_pos)
12719 && XFASTINT (w->base_line_pos) <= startpos)
12720 {
12721 line = XFASTINT (w->base_line_number);
12722 linepos = XFASTINT (w->base_line_pos);
12723 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12724 }
12725 else
12726 {
12727 line = 1;
12728 linepos = BUF_BEGV (b);
12729 linepos_byte = BUF_BEGV_BYTE (b);
12730 }
12731
12732 /* Count lines from base line to window start position. */
12733 nlines = display_count_lines (linepos, linepos_byte,
12734 startpos_byte,
12735 startpos, &junk);
12736
12737 topline = nlines + line;
12738
12739 /* Determine a new base line, if the old one is too close
12740 or too far away, or if we did not have one.
12741 "Too close" means it's plausible a scroll-down would
12742 go back past it. */
12743 if (startpos == BUF_BEGV (b))
12744 {
12745 XSETFASTINT (w->base_line_number, topline);
12746 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12747 }
12748 else if (nlines < height + 25 || nlines > height * 3 + 50
12749 || linepos == BUF_BEGV (b))
12750 {
12751 int limit = BUF_BEGV (b);
12752 int limit_byte = BUF_BEGV_BYTE (b);
12753 int position;
12754 int distance = (height * 2 + 30) * line_number_display_limit_width;
12755
12756 if (startpos - distance > limit)
12757 {
12758 limit = startpos - distance;
12759 limit_byte = CHAR_TO_BYTE (limit);
12760 }
12761
12762 nlines = display_count_lines (startpos, startpos_byte,
12763 limit_byte,
12764 - (height * 2 + 30),
12765 &position);
12766 /* If we couldn't find the lines we wanted within
12767 line_number_display_limit_width chars per line,
12768 give up on line numbers for this window. */
12769 if (position == limit_byte && limit == startpos - distance)
12770 {
12771 w->base_line_pos = w->buffer;
12772 w->base_line_number = Qnil;
12773 goto no_value;
12774 }
12775
12776 XSETFASTINT (w->base_line_number, topline - nlines);
12777 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12778 }
12779
12780 /* Now count lines from the start pos to point. */
12781 nlines = display_count_lines (startpos, startpos_byte,
12782 PT_BYTE, PT, &junk);
12783
12784 /* Record that we did display the line number. */
12785 line_number_displayed = 1;
12786
12787 /* Make the string to show. */
12788 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12789 return decode_mode_spec_buf;
12790 no_value:
12791 {
12792 char* p = decode_mode_spec_buf;
12793 int pad = field_width - 2;
12794 while (pad-- > 0)
12795 *p++ = ' ';
12796 *p++ = '?';
12797 *p++ = '?';
12798 *p = '\0';
12799 return decode_mode_spec_buf;
12800 }
12801 }
12802 break;
12803
12804 case 'm':
12805 obj = b->mode_name;
12806 break;
12807
12808 case 'n':
12809 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12810 return " Narrow";
12811 break;
12812
12813 case 'p':
12814 {
12815 int pos = marker_position (w->start);
12816 int total = BUF_ZV (b) - BUF_BEGV (b);
12817
12818 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12819 {
12820 if (pos <= BUF_BEGV (b))
12821 return "All";
12822 else
12823 return "Bottom";
12824 }
12825 else if (pos <= BUF_BEGV (b))
12826 return "Top";
12827 else
12828 {
12829 if (total > 1000000)
12830 /* Do it differently for a large value, to avoid overflow. */
12831 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12832 else
12833 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
12834 /* We can't normally display a 3-digit number,
12835 so get us a 2-digit number that is close. */
12836 if (total == 100)
12837 total = 99;
12838 sprintf (decode_mode_spec_buf, "%2d%%", total);
12839 return decode_mode_spec_buf;
12840 }
12841 }
12842
12843 /* Display percentage of size above the bottom of the screen. */
12844 case 'P':
12845 {
12846 int toppos = marker_position (w->start);
12847 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
12848 int total = BUF_ZV (b) - BUF_BEGV (b);
12849
12850 if (botpos >= BUF_ZV (b))
12851 {
12852 if (toppos <= BUF_BEGV (b))
12853 return "All";
12854 else
12855 return "Bottom";
12856 }
12857 else
12858 {
12859 if (total > 1000000)
12860 /* Do it differently for a large value, to avoid overflow. */
12861 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
12862 else
12863 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
12864 /* We can't normally display a 3-digit number,
12865 so get us a 2-digit number that is close. */
12866 if (total == 100)
12867 total = 99;
12868 if (toppos <= BUF_BEGV (b))
12869 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
12870 else
12871 sprintf (decode_mode_spec_buf, "%2d%%", total);
12872 return decode_mode_spec_buf;
12873 }
12874 }
12875
12876 case 's':
12877 /* status of process */
12878 obj = Fget_buffer_process (w->buffer);
12879 if (NILP (obj))
12880 return "no process";
12881 #ifdef subprocesses
12882 obj = Fsymbol_name (Fprocess_status (obj));
12883 #endif
12884 break;
12885
12886 case 't': /* indicate TEXT or BINARY */
12887 #ifdef MODE_LINE_BINARY_TEXT
12888 return MODE_LINE_BINARY_TEXT (b);
12889 #else
12890 return "T";
12891 #endif
12892
12893 case 'z':
12894 /* coding-system (not including end-of-line format) */
12895 case 'Z':
12896 /* coding-system (including end-of-line type) */
12897 {
12898 int eol_flag = (c == 'Z');
12899 char *p = decode_mode_spec_buf;
12900
12901 if (! FRAME_WINDOW_P (f))
12902 {
12903 /* No need to mention EOL here--the terminal never needs
12904 to do EOL conversion. */
12905 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
12906 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
12907 }
12908 p = decode_mode_spec_coding (b->buffer_file_coding_system,
12909 p, eol_flag);
12910
12911 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
12912 #ifdef subprocesses
12913 obj = Fget_buffer_process (Fcurrent_buffer ());
12914 if (PROCESSP (obj))
12915 {
12916 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
12917 p, eol_flag);
12918 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
12919 p, eol_flag);
12920 }
12921 #endif /* subprocesses */
12922 #endif /* 0 */
12923 *p = 0;
12924 return decode_mode_spec_buf;
12925 }
12926 }
12927
12928 if (STRINGP (obj))
12929 return (char *) XSTRING (obj)->data;
12930 else
12931 return "";
12932 }
12933
12934
12935 /* Count up to COUNT lines starting from START / START_BYTE.
12936 But don't go beyond LIMIT_BYTE.
12937 Return the number of lines thus found (always nonnegative).
12938
12939 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
12940
12941 static int
12942 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
12943 int start, start_byte, limit_byte, count;
12944 int *byte_pos_ptr;
12945 {
12946 register unsigned char *cursor;
12947 unsigned char *base;
12948
12949 register int ceiling;
12950 register unsigned char *ceiling_addr;
12951 int orig_count = count;
12952
12953 /* If we are not in selective display mode,
12954 check only for newlines. */
12955 int selective_display = (!NILP (current_buffer->selective_display)
12956 && !INTEGERP (current_buffer->selective_display));
12957
12958 if (count > 0)
12959 {
12960 while (start_byte < limit_byte)
12961 {
12962 ceiling = BUFFER_CEILING_OF (start_byte);
12963 ceiling = min (limit_byte - 1, ceiling);
12964 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
12965 base = (cursor = BYTE_POS_ADDR (start_byte));
12966 while (1)
12967 {
12968 if (selective_display)
12969 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
12970 ;
12971 else
12972 while (*cursor != '\n' && ++cursor != ceiling_addr)
12973 ;
12974
12975 if (cursor != ceiling_addr)
12976 {
12977 if (--count == 0)
12978 {
12979 start_byte += cursor - base + 1;
12980 *byte_pos_ptr = start_byte;
12981 return orig_count;
12982 }
12983 else
12984 if (++cursor == ceiling_addr)
12985 break;
12986 }
12987 else
12988 break;
12989 }
12990 start_byte += cursor - base;
12991 }
12992 }
12993 else
12994 {
12995 while (start_byte > limit_byte)
12996 {
12997 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
12998 ceiling = max (limit_byte, ceiling);
12999 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13000 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13001 while (1)
13002 {
13003 if (selective_display)
13004 while (--cursor != ceiling_addr
13005 && *cursor != '\n' && *cursor != 015)
13006 ;
13007 else
13008 while (--cursor != ceiling_addr && *cursor != '\n')
13009 ;
13010
13011 if (cursor != ceiling_addr)
13012 {
13013 if (++count == 0)
13014 {
13015 start_byte += cursor - base + 1;
13016 *byte_pos_ptr = start_byte;
13017 /* When scanning backwards, we should
13018 not count the newline posterior to which we stop. */
13019 return - orig_count - 1;
13020 }
13021 }
13022 else
13023 break;
13024 }
13025 /* Here we add 1 to compensate for the last decrement
13026 of CURSOR, which took it past the valid range. */
13027 start_byte += cursor - base + 1;
13028 }
13029 }
13030
13031 *byte_pos_ptr = limit_byte;
13032
13033 if (count < 0)
13034 return - orig_count + count;
13035 return orig_count - count;
13036
13037 }
13038
13039
13040 \f
13041 /***********************************************************************
13042 Displaying strings
13043 ***********************************************************************/
13044
13045 /* Display a NUL-terminated string, starting with index START.
13046
13047 If STRING is non-null, display that C string. Otherwise, the Lisp
13048 string LISP_STRING is displayed.
13049
13050 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13051 FACE_STRING. Display STRING or LISP_STRING with the face at
13052 FACE_STRING_POS in FACE_STRING:
13053
13054 Display the string in the environment given by IT, but use the
13055 standard display table, temporarily.
13056
13057 FIELD_WIDTH is the minimum number of output glyphs to produce.
13058 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13059 with spaces. If STRING has more characters, more than FIELD_WIDTH
13060 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13061
13062 PRECISION is the maximum number of characters to output from
13063 STRING. PRECISION < 0 means don't truncate the string.
13064
13065 This is roughly equivalent to printf format specifiers:
13066
13067 FIELD_WIDTH PRECISION PRINTF
13068 ----------------------------------------
13069 -1 -1 %s
13070 -1 10 %.10s
13071 10 -1 %10s
13072 20 10 %20.10s
13073
13074 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13075 display them, and < 0 means obey the current buffer's value of
13076 enable_multibyte_characters.
13077
13078 Value is the number of glyphs produced. */
13079
13080 static int
13081 display_string (string, lisp_string, face_string, face_string_pos,
13082 start, it, field_width, precision, max_x, multibyte)
13083 unsigned char *string;
13084 Lisp_Object lisp_string;
13085 Lisp_Object face_string;
13086 int face_string_pos;
13087 int start;
13088 struct it *it;
13089 int field_width, precision, max_x;
13090 int multibyte;
13091 {
13092 int hpos_at_start = it->hpos;
13093 int saved_face_id = it->face_id;
13094 struct glyph_row *row = it->glyph_row;
13095
13096 /* Initialize the iterator IT for iteration over STRING beginning
13097 with index START. We assume that IT may be modified here (which
13098 means that display_line has to do something when displaying a
13099 mini-buffer prompt, which it does). */
13100 reseat_to_string (it, string, lisp_string, start,
13101 precision, field_width, multibyte);
13102
13103 /* If displaying STRING, set up the face of the iterator
13104 from LISP_STRING, if that's given. */
13105 if (STRINGP (face_string))
13106 {
13107 int endptr;
13108 struct face *face;
13109
13110 it->face_id
13111 = face_at_string_position (it->w, face_string, face_string_pos,
13112 0, it->region_beg_charpos,
13113 it->region_end_charpos,
13114 &endptr, it->base_face_id);
13115 face = FACE_FROM_ID (it->f, it->face_id);
13116 it->face_box_p = face->box != FACE_NO_BOX;
13117 }
13118
13119 /* Set max_x to the maximum allowed X position. Don't let it go
13120 beyond the right edge of the window. */
13121 if (max_x <= 0)
13122 max_x = it->last_visible_x;
13123 else
13124 max_x = min (max_x, it->last_visible_x);
13125
13126 /* Skip over display elements that are not visible. because IT->w is
13127 hscrolled. */
13128 if (it->current_x < it->first_visible_x)
13129 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13130 MOVE_TO_POS | MOVE_TO_X);
13131
13132 row->ascent = it->max_ascent;
13133 row->height = it->max_ascent + it->max_descent;
13134 row->phys_ascent = it->max_phys_ascent;
13135 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13136
13137 /* This condition is for the case that we are called with current_x
13138 past last_visible_x. */
13139 while (it->current_x < max_x)
13140 {
13141 int x_before, x, n_glyphs_before, i, nglyphs;
13142
13143 /* Get the next display element. */
13144 if (!get_next_display_element (it))
13145 break;
13146
13147 /* Produce glyphs. */
13148 x_before = it->current_x;
13149 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13150 PRODUCE_GLYPHS (it);
13151
13152 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13153 i = 0;
13154 x = x_before;
13155 while (i < nglyphs)
13156 {
13157 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13158
13159 if (!it->truncate_lines_p
13160 && x + glyph->pixel_width > max_x)
13161 {
13162 /* End of continued line or max_x reached. */
13163 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13164 it->current_x = x;
13165 break;
13166 }
13167 else if (x + glyph->pixel_width > it->first_visible_x)
13168 {
13169 /* Glyph is at least partially visible. */
13170 ++it->hpos;
13171 if (x < it->first_visible_x)
13172 it->glyph_row->x = x - it->first_visible_x;
13173 }
13174 else
13175 {
13176 /* Glyph is off the left margin of the display area.
13177 Should not happen. */
13178 abort ();
13179 }
13180
13181 row->ascent = max (row->ascent, it->max_ascent);
13182 row->height = max (row->height, it->max_ascent + it->max_descent);
13183 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13184 row->phys_height = max (row->phys_height,
13185 it->max_phys_ascent + it->max_phys_descent);
13186 x += glyph->pixel_width;
13187 ++i;
13188 }
13189
13190 /* Stop if max_x reached. */
13191 if (i < nglyphs)
13192 break;
13193
13194 /* Stop at line ends. */
13195 if (ITERATOR_AT_END_OF_LINE_P (it))
13196 {
13197 it->continuation_lines_width = 0;
13198 break;
13199 }
13200
13201 set_iterator_to_next (it);
13202
13203 /* Stop if truncating at the right edge. */
13204 if (it->truncate_lines_p
13205 && it->current_x >= it->last_visible_x)
13206 {
13207 /* Add truncation mark, but don't do it if the line is
13208 truncated at a padding space. */
13209 if (IT_CHARPOS (*it) < it->string_nchars)
13210 {
13211 if (!FRAME_WINDOW_P (it->f))
13212 produce_special_glyphs (it, IT_TRUNCATION);
13213 it->glyph_row->truncated_on_right_p = 1;
13214 }
13215 break;
13216 }
13217 }
13218
13219 /* Maybe insert a truncation at the left. */
13220 if (it->first_visible_x
13221 && IT_CHARPOS (*it) > 0)
13222 {
13223 if (!FRAME_WINDOW_P (it->f))
13224 insert_left_trunc_glyphs (it);
13225 it->glyph_row->truncated_on_left_p = 1;
13226 }
13227
13228 it->face_id = saved_face_id;
13229
13230 /* Value is number of columns displayed. */
13231 return it->hpos - hpos_at_start;
13232 }
13233
13234
13235 \f
13236 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13237 appears as an element of LIST or as the car of an element of LIST.
13238 If PROPVAL is a list, compare each element against LIST in that
13239 way, and return 1 if any element of PROPVAL is found in LIST.
13240 Otherwise return 0. This function cannot quit. */
13241
13242 int
13243 invisible_p (propval, list)
13244 register Lisp_Object propval;
13245 Lisp_Object list;
13246 {
13247 register Lisp_Object tail, proptail;
13248
13249 for (tail = list; CONSP (tail); tail = XCDR (tail))
13250 {
13251 register Lisp_Object tem;
13252 tem = XCAR (tail);
13253 if (EQ (propval, tem))
13254 return 1;
13255 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13256 return 1;
13257 }
13258
13259 if (CONSP (propval))
13260 {
13261 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13262 {
13263 Lisp_Object propelt;
13264 propelt = XCAR (proptail);
13265 for (tail = list; CONSP (tail); tail = XCDR (tail))
13266 {
13267 register Lisp_Object tem;
13268 tem = XCAR (tail);
13269 if (EQ (propelt, tem))
13270 return 1;
13271 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13272 return 1;
13273 }
13274 }
13275 }
13276
13277 return 0;
13278 }
13279
13280
13281 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13282 the cdr of that element is non-nil. If PROPVAL is a list, check
13283 each element of PROPVAL in that way, and the first time some
13284 element is found, return 1 if the cdr of that element is non-nil.
13285 Otherwise return 0. This function cannot quit. */
13286
13287 int
13288 invisible_ellipsis_p (propval, list)
13289 register Lisp_Object propval;
13290 Lisp_Object list;
13291 {
13292 register Lisp_Object tail, proptail;
13293
13294 for (tail = list; CONSP (tail); tail = XCDR (tail))
13295 {
13296 register Lisp_Object tem;
13297 tem = XCAR (tail);
13298 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13299 return ! NILP (XCDR (tem));
13300 }
13301
13302 if (CONSP (propval))
13303 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13304 {
13305 Lisp_Object propelt;
13306 propelt = XCAR (proptail);
13307 for (tail = list; CONSP (tail); tail = XCDR (tail))
13308 {
13309 register Lisp_Object tem;
13310 tem = XCAR (tail);
13311 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13312 return ! NILP (XCDR (tem));
13313 }
13314 }
13315
13316 return 0;
13317 }
13318
13319
13320 \f
13321 /***********************************************************************
13322 Initialization
13323 ***********************************************************************/
13324
13325 void
13326 syms_of_xdisp ()
13327 {
13328 Vwith_echo_area_save_vector = Qnil;
13329 staticpro (&Vwith_echo_area_save_vector);
13330
13331 Vmessage_stack = Qnil;
13332 staticpro (&Vmessage_stack);
13333
13334 Qinhibit_redisplay = intern ("inhibit-redisplay");
13335 staticpro (&Qinhibit_redisplay);
13336
13337 #if GLYPH_DEBUG
13338 defsubr (&Sdump_glyph_matrix);
13339 defsubr (&Sdump_glyph_row);
13340 defsubr (&Sdump_tool_bar_row);
13341 defsubr (&Strace_redisplay_toggle);
13342 defsubr (&Strace_to_stderr);
13343 #endif
13344
13345 staticpro (&Qmenu_bar_update_hook);
13346 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13347
13348 staticpro (&Qoverriding_terminal_local_map);
13349 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13350
13351 staticpro (&Qoverriding_local_map);
13352 Qoverriding_local_map = intern ("overriding-local-map");
13353
13354 staticpro (&Qwindow_scroll_functions);
13355 Qwindow_scroll_functions = intern ("window-scroll-functions");
13356
13357 staticpro (&Qredisplay_end_trigger_functions);
13358 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13359
13360 staticpro (&Qinhibit_point_motion_hooks);
13361 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13362
13363 QCdata = intern (":data");
13364 staticpro (&QCdata);
13365 Qdisplay = intern ("display");
13366 staticpro (&Qdisplay);
13367 Qspace_width = intern ("space-width");
13368 staticpro (&Qspace_width);
13369 Qraise = intern ("raise");
13370 staticpro (&Qraise);
13371 Qspace = intern ("space");
13372 staticpro (&Qspace);
13373 Qmargin = intern ("margin");
13374 staticpro (&Qmargin);
13375 Qleft_margin = intern ("left-margin");
13376 staticpro (&Qleft_margin);
13377 Qright_margin = intern ("right-margin");
13378 staticpro (&Qright_margin);
13379 Qalign_to = intern ("align-to");
13380 staticpro (&Qalign_to);
13381 QCalign_to = intern (":align-to");
13382 staticpro (&QCalign_to);
13383 Qrelative_width = intern ("relative-width");
13384 staticpro (&Qrelative_width);
13385 QCrelative_width = intern (":relative-width");
13386 staticpro (&QCrelative_width);
13387 QCrelative_height = intern (":relative-height");
13388 staticpro (&QCrelative_height);
13389 QCeval = intern (":eval");
13390 staticpro (&QCeval);
13391 Qwhen = intern ("when");
13392 staticpro (&Qwhen);
13393 QCfile = intern (":file");
13394 staticpro (&QCfile);
13395 Qfontified = intern ("fontified");
13396 staticpro (&Qfontified);
13397 Qfontification_functions = intern ("fontification-functions");
13398 staticpro (&Qfontification_functions);
13399 Qtrailing_whitespace = intern ("trailing-whitespace");
13400 staticpro (&Qtrailing_whitespace);
13401 Qimage = intern ("image");
13402 staticpro (&Qimage);
13403 Qmessage_truncate_lines = intern ("message-truncate-lines");
13404 staticpro (&Qmessage_truncate_lines);
13405
13406 last_arrow_position = Qnil;
13407 last_arrow_string = Qnil;
13408 staticpro (&last_arrow_position);
13409 staticpro (&last_arrow_string);
13410
13411 echo_buffer[0] = echo_buffer[1] = Qnil;
13412 staticpro (&echo_buffer[0]);
13413 staticpro (&echo_buffer[1]);
13414
13415 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13416 staticpro (&echo_area_buffer[0]);
13417 staticpro (&echo_area_buffer[1]);
13418
13419 Vmessages_buffer_name = build_string ("*Messages*");
13420 staticpro (&Vmessages_buffer_name);
13421
13422 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13423 "Non-nil means highlight trailing whitespace.\n\
13424 The face used for trailing whitespace is `trailing-whitespace'.");
13425 Vshow_trailing_whitespace = Qnil;
13426
13427 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13428 "Non-nil means don't actually do any redisplay.\n\
13429 This is used for internal purposes.");
13430 Vinhibit_redisplay = Qnil;
13431
13432 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13433 "String (or mode line construct) included (normally) in `mode-line-format'.");
13434 Vglobal_mode_string = Qnil;
13435
13436 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13437 "Marker for where to display an arrow on top of the buffer text.\n\
13438 This must be the beginning of a line in order to work.\n\
13439 See also `overlay-arrow-string'.");
13440 Voverlay_arrow_position = Qnil;
13441
13442 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13443 "String to display as an arrow. See also `overlay-arrow-position'.");
13444 Voverlay_arrow_string = Qnil;
13445
13446 DEFVAR_INT ("scroll-step", &scroll_step,
13447 "*The number of lines to try scrolling a window by when point moves out.\n\
13448 If that fails to bring point back on frame, point is centered instead.\n\
13449 If this is zero, point is always centered after it moves off frame.");
13450
13451 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13452 "*Scroll up to this many lines, to bring point back on screen.\n\
13453 A value of zero means to scroll the text to center point vertically\n\
13454 in the window.");
13455 scroll_conservatively = 0;
13456
13457 DEFVAR_INT ("scroll-margin", &scroll_margin,
13458 "*Number of lines of margin at the top and bottom of a window.\n\
13459 Recenter the window whenever point gets within this many lines\n\
13460 of the top or bottom of the window.");
13461 scroll_margin = 0;
13462
13463 #if GLYPH_DEBUG
13464 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13465 #endif
13466
13467 DEFVAR_BOOL ("truncate-partial-width-windows",
13468 &truncate_partial_width_windows,
13469 "*Non-nil means truncate lines in all windows less than full frame wide.");
13470 truncate_partial_width_windows = 1;
13471
13472 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13473 "*Non-nil means use inverse video for the mode line.");
13474 mode_line_inverse_video = 1;
13475
13476 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13477 "*Maximum buffer size for which line number should be displayed.\n\
13478 If the buffer is bigger than this, the line number does not appear\n\
13479 in the mode line. A value of nil means no limit.");
13480 Vline_number_display_limit = Qnil;
13481
13482 DEFVAR_INT ("line-number-display-limit-width",
13483 &line_number_display_limit_width,
13484 "*Maximum line width (in characters) for line number display.\n\
13485 If the average length of the lines near point is bigger than this, then the\n\
13486 line number may be omitted from the mode line.");
13487 line_number_display_limit_width = 200;
13488
13489 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13490 "*Non-nil means highlight region even in nonselected windows.");
13491 highlight_nonselected_windows = 0;
13492
13493 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13494 "Non-nil if more than one frame is visible on this display.\n\
13495 Minibuffer-only frames don't count, but iconified frames do.\n\
13496 This variable is not guaranteed to be accurate except while processing\n\
13497 `frame-title-format' and `icon-title-format'.");
13498
13499 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13500 "Template for displaying the title bar of visible frames.\n\
13501 \(Assuming the window manager supports this feature.)\n\
13502 This variable has the same structure as `mode-line-format' (which see),\n\
13503 and is used only on frames for which no explicit name has been set\n\
13504 \(see `modify-frame-parameters').");
13505 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13506 "Template for displaying the title bar of an iconified frame.\n\
13507 \(Assuming the window manager supports this feature.)\n\
13508 This variable has the same structure as `mode-line-format' (which see),\n\
13509 and is used only on frames for which no explicit name has been set\n\
13510 \(see `modify-frame-parameters').");
13511 Vicon_title_format
13512 = Vframe_title_format
13513 = Fcons (intern ("multiple-frames"),
13514 Fcons (build_string ("%b"),
13515 Fcons (Fcons (build_string (""),
13516 Fcons (intern ("invocation-name"),
13517 Fcons (build_string ("@"),
13518 Fcons (intern ("system-name"),
13519 Qnil)))),
13520 Qnil)));
13521
13522 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13523 "Maximum number of lines to keep in the message log buffer.\n\
13524 If nil, disable message logging. If t, log messages but don't truncate\n\
13525 the buffer when it becomes large.");
13526 XSETFASTINT (Vmessage_log_max, 50);
13527
13528 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13529 "Functions called before redisplay, if window sizes have changed.\n\
13530 The value should be a list of functions that take one argument.\n\
13531 Just before redisplay, for each frame, if any of its windows have changed\n\
13532 size since the last redisplay, or have been split or deleted,\n\
13533 all the functions in the list are called, with the frame as argument.");
13534 Vwindow_size_change_functions = Qnil;
13535
13536 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13537 "List of Functions to call before redisplaying a window with scrolling.\n\
13538 Each function is called with two arguments, the window\n\
13539 and its new display-start position. Note that the value of `window-end'\n\
13540 is not valid when these functions are called.");
13541 Vwindow_scroll_functions = Qnil;
13542
13543 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13544 "*Non-nil means automatically resize tool-bars.\n\
13545 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13546 It decreases a tool-bar's height when it would display blank lines\n\
13547 otherwise.");
13548 auto_resize_tool_bars_p = 1;
13549
13550 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13551 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13552 auto_raise_tool_bar_buttons_p = 1;
13553
13554 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13555 "*Margin around tool-bar buttons in pixels.");
13556 tool_bar_button_margin = 1;
13557
13558 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13559 "Relief thickness of tool-bar buttons.");
13560 tool_bar_button_relief = 3;
13561
13562 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13563 "List of functions to call to fontify regions of text.\n\
13564 Each function is called with one argument POS. Functions must\n\
13565 fontify a region starting at POS in the current buffer, and give\n\
13566 fontified regions the property `fontified'.\n\
13567 This variable automatically becomes buffer-local when set.");
13568 Vfontification_functions = Qnil;
13569 Fmake_local_variable (Qfontification_functions);
13570
13571 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13572 &unibyte_display_via_language_environment,
13573 "*Non-nil means display unibyte text according to language environment.\n\
13574 Specifically this means that unibyte non-ASCII characters\n\
13575 are displayed by converting them to the equivalent multibyte characters\n\
13576 according to the current language environment. As a result, they are\n\
13577 displayed according to the current fontset.");
13578 unibyte_display_via_language_environment = 0;
13579
13580 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13581 "*Maximum height for resizing mini-windows.\n\
13582 If a float, it specifies a fraction of the mini-window frame's height.\n\
13583 If an integer, it specifies a number of lines.\n\
13584 If nil, don't resize.");
13585 Vmax_mini_window_height = make_float (0.25);
13586
13587 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13588 &cursor_in_non_selected_windows,
13589 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13590 Nil means don't display a cursor there.");
13591 cursor_in_non_selected_windows = 1;
13592
13593 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13594 "*Non-nil means scroll the display automatically to make point visible.");
13595 automatic_hscrolling_p = 1;
13596
13597 DEFVAR_LISP ("image-types", &Vimage_types,
13598 "List of supported image types.\n\
13599 Each element of the list is a symbol for a supported image type.");
13600 Vimage_types = Qnil;
13601
13602 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13603 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13604 Bind this around calls to `message' to let it take effect.");
13605 message_truncate_lines = 0;
13606 }
13607
13608
13609 /* Initialize this module when Emacs starts. */
13610
13611 void
13612 init_xdisp ()
13613 {
13614 Lisp_Object root_window;
13615 struct window *mini_w;
13616
13617 CHARPOS (this_line_start_pos) = 0;
13618
13619 mini_w = XWINDOW (minibuf_window);
13620 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13621
13622 if (!noninteractive)
13623 {
13624 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13625 int i;
13626
13627 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13628 set_window_height (root_window,
13629 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13630 0);
13631 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13632 set_window_height (minibuf_window, 1, 0);
13633
13634 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13635 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13636
13637 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13638 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13639 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13640
13641 /* The default ellipsis glyphs `...'. */
13642 for (i = 0; i < 3; ++i)
13643 XSETFASTINT (default_invis_vector[i], '.');
13644 }
13645
13646 #ifdef HAVE_WINDOW_SYSTEM
13647 {
13648 /* Allocate the buffer for frame titles. */
13649 int size = 100;
13650 frame_title_buf = (char *) xmalloc (size);
13651 frame_title_buf_end = frame_title_buf + size;
13652 frame_title_ptr = NULL;
13653 }
13654 #endif /* HAVE_WINDOW_SYSTEM */
13655 }
13656
13657