(pos_visible_p): Compute the default character height
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
200
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 int tool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Names of text properties relevant for redisplay. */
258
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
261
262 /* Symbols used in text property values. */
263
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
268
269 /* Non-nil means highlight trailing whitespace. */
270
271 Lisp_Object Vshow_trailing_whitespace;
272
273 /* Name of the face used to highlight trailing whitespace. */
274
275 Lisp_Object Qtrailing_whitespace;
276
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
279
280 Lisp_Object Qimage;
281
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
284
285 int noninteractive_need_newline;
286
287 /* Non-zero means print newline to message log before next message. */
288
289 static int message_log_need_newline;
290
291 \f
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
296
297 static struct text_pos this_line_start_pos;
298
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
301
302 static struct text_pos this_line_end_pos;
303
304 /* The vertical positions and the height of this line. */
305
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
309
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
312
313 static int this_line_start_x;
314
315 /* Buffer that this_line_.* variables are referring to. */
316
317 static struct buffer *this_line_buffer;
318
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
321
322 int truncate_partial_width_windows;
323
324 /* A flag to control how to display unibyte 8-bit character. */
325
326 int unibyte_display_via_language_environment;
327
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
331
332 int multiple_frames;
333
334 Lisp_Object Vglobal_mode_string;
335
336 /* Marker for where to display an arrow on top of the buffer text. */
337
338 Lisp_Object Voverlay_arrow_position;
339
340 /* String to display for the arrow. Only used on terminal frames. */
341
342 Lisp_Object Voverlay_arrow_string;
343
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
347
348 static Lisp_Object last_arrow_position, last_arrow_string;
349
350 /* Like mode-line-format, but for the title bar on a visible frame. */
351
352 Lisp_Object Vframe_title_format;
353
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
355
356 Lisp_Object Vicon_title_format;
357
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
361
362 static Lisp_Object Vwindow_size_change_functions;
363
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
365
366 /* Nonzero if overlay arrow has been displayed once in this window. */
367
368 static int overlay_arrow_seen;
369
370 /* Nonzero means highlight the region even in nonselected windows. */
371
372 int highlight_nonselected_windows;
373
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
376
377 static int scroll_step;
378
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
381
382 static int scroll_conservatively;
383
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
388
389 int scroll_margin;
390
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
394
395 int buffer_shared;
396
397 /* Vector containing glyphs for an ellipsis `...'. */
398
399 static Lisp_Object default_invis_vector[3];
400
401 /* Nonzero means display mode line highlighted. */
402
403 int mode_line_inverse_video;
404
405 /* Prompt to display in front of the mini-buffer contents. */
406
407 Lisp_Object minibuf_prompt;
408
409 /* Width of current mini-buffer prompt. Only set after display_line
410 of the line that contains the prompt. */
411
412 int minibuf_prompt_width;
413 int minibuf_prompt_pixel_width;
414
415 /* This is the window where the echo area message was displayed. It
416 is always a mini-buffer window, but it may not be the same window
417 currently active as a mini-buffer. */
418
419 Lisp_Object echo_area_window;
420
421 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
422 pushes the current message and the value of
423 message_enable_multibyte on the stack, the function restore_message
424 pops the stack and displays MESSAGE again. */
425
426 Lisp_Object Vmessage_stack;
427
428 /* Nonzero means multibyte characters were enabled when the echo area
429 message was specified. */
430
431 int message_enable_multibyte;
432
433 /* True if we should redraw the mode lines on the next redisplay. */
434
435 int update_mode_lines;
436
437 /* Nonzero if window sizes or contents have changed since last
438 redisplay that finished */
439
440 int windows_or_buffers_changed;
441
442 /* Nonzero after display_mode_line if %l was used and it displayed a
443 line number. */
444
445 int line_number_displayed;
446
447 /* Maximum buffer size for which to display line numbers. */
448
449 Lisp_Object Vline_number_display_limit;
450
451 /* line width to consider when repostioning for line number display */
452
453 static int line_number_display_limit_width;
454
455 /* Number of lines to keep in the message log buffer. t means
456 infinite. nil means don't log at all. */
457
458 Lisp_Object Vmessage_log_max;
459
460 /* The name of the *Messages* buffer, a string. */
461
462 static Lisp_Object Vmessages_buffer_name;
463
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
466
467 Lisp_Object echo_area_buffer[2];
468
469 /* The buffers referenced from echo_area_buffer. */
470
471 static Lisp_Object echo_buffer[2];
472
473 /* A vector saved used in with_area_buffer to reduce consing. */
474
475 static Lisp_Object Vwith_echo_area_save_vector;
476
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
479
480 static int display_last_displayed_message_p;
481
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
484
485 int message_buf_print;
486
487 /* Maximum height for resizing mini-windows. Either a float
488 specifying a fraction of the available height, or an integer
489 specifying a number of lines. */
490
491 Lisp_Object Vmax_mini_window_height;
492
493 /* Non-zero means messages should be displayed with truncated
494 lines instead of being continued. */
495
496 int message_truncate_lines;
497 Lisp_Object Qmessage_truncate_lines;
498
499 /* Non-zero means we want a hollow cursor in windows that are not
500 selected. Zero means there's no cursor in such windows. */
501
502 int cursor_in_non_selected_windows;
503
504 /* A scratch glyph row with contents used for generating truncation
505 glyphs. Also used in direct_output_for_insert. */
506
507 #define MAX_SCRATCH_GLYPHS 100
508 struct glyph_row scratch_glyph_row;
509 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
510
511 /* Ascent and height of the last line processed by move_it_to. */
512
513 static int last_max_ascent, last_height;
514
515 /* Non-zero if there's a help-echo in the echo area. */
516
517 int help_echo_showing_p;
518
519 /* If >= 0, computed, exact values of mode-line and header-line height
520 to use in the macros CURRENT_MODE_LINE_HEIGHT and
521 CURRENT_HEADER_LINE_HEIGHT. */
522
523 int current_mode_line_height, current_header_line_height;
524
525 /* The maximum distance to look ahead for text properties. Values
526 that are too small let us call compute_char_face and similar
527 functions too often which is expensive. Values that are too large
528 let us call compute_char_face and alike too often because we
529 might not be interested in text properties that far away. */
530
531 #define TEXT_PROP_DISTANCE_LIMIT 100
532
533 #if GLYPH_DEBUG
534
535 /* Non-zero means print traces of redisplay if compiled with
536 GLYPH_DEBUG != 0. */
537
538 int trace_redisplay_p;
539
540 #endif /* GLYPH_DEBUG */
541
542 #ifdef DEBUG_TRACE_MOVE
543 /* Non-zero means trace with TRACE_MOVE to stderr. */
544 int trace_move;
545
546 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
547 #else
548 #define TRACE_MOVE(x) (void) 0
549 #endif
550
551 /* Non-zero means automatically scroll windows horizontally to make
552 point visible. */
553
554 int automatic_hscrolling_p;
555
556 /* A list of symbols, one for each supported image type. */
557
558 Lisp_Object Vimage_types;
559
560 /* The variable `resize-mini-windows'. If nil, don't resize
561 mini-windows. If t, always resize them to fit the text they
562 display. If `grow-only', let mini-windows grow only until they
563 become empty. */
564
565 Lisp_Object Vresize_mini_windows;
566
567 /* Value returned from text property handlers (see below). */
568
569 enum prop_handled
570 {
571 HANDLED_NORMALLY,
572 HANDLED_RECOMPUTE_PROPS,
573 HANDLED_OVERLAY_STRING_CONSUMED,
574 HANDLED_RETURN
575 };
576
577 /* A description of text properties that redisplay is interested
578 in. */
579
580 struct props
581 {
582 /* The name of the property. */
583 Lisp_Object *name;
584
585 /* A unique index for the property. */
586 enum prop_idx idx;
587
588 /* A handler function called to set up iterator IT from the property
589 at IT's current position. Value is used to steer handle_stop. */
590 enum prop_handled (*handler) P_ ((struct it *it));
591 };
592
593 static enum prop_handled handle_face_prop P_ ((struct it *));
594 static enum prop_handled handle_invisible_prop P_ ((struct it *));
595 static enum prop_handled handle_display_prop P_ ((struct it *));
596 static enum prop_handled handle_composition_prop P_ ((struct it *));
597 static enum prop_handled handle_overlay_change P_ ((struct it *));
598 static enum prop_handled handle_fontified_prop P_ ((struct it *));
599
600 /* Properties handled by iterators. */
601
602 static struct props it_props[] =
603 {
604 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
605 /* Handle `face' before `display' because some sub-properties of
606 `display' need to know the face. */
607 {&Qface, FACE_PROP_IDX, handle_face_prop},
608 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
609 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
610 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
611 {NULL, 0, NULL}
612 };
613
614 /* Value is the position described by X. If X is a marker, value is
615 the marker_position of X. Otherwise, value is X. */
616
617 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
618
619 /* Enumeration returned by some move_it_.* functions internally. */
620
621 enum move_it_result
622 {
623 /* Not used. Undefined value. */
624 MOVE_UNDEFINED,
625
626 /* Move ended at the requested buffer position or ZV. */
627 MOVE_POS_MATCH_OR_ZV,
628
629 /* Move ended at the requested X pixel position. */
630 MOVE_X_REACHED,
631
632 /* Move within a line ended at the end of a line that must be
633 continued. */
634 MOVE_LINE_CONTINUED,
635
636 /* Move within a line ended at the end of a line that would
637 be displayed truncated. */
638 MOVE_LINE_TRUNCATED,
639
640 /* Move within a line ended at a line end. */
641 MOVE_NEWLINE_OR_CR
642 };
643
644
645 \f
646 /* Function prototypes. */
647
648 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
649 static int redisplay_mode_lines P_ ((Lisp_Object, int));
650 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
651 static int invisible_text_between_p P_ ((struct it *, int, int));
652 static int next_element_from_ellipsis P_ ((struct it *));
653 static void pint2str P_ ((char *, int, int));
654 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
655 struct text_pos));
656 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
657 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
658 static void store_frame_title_char P_ ((char));
659 static int store_frame_title P_ ((unsigned char *, int, int));
660 static void x_consider_frame_title P_ ((Lisp_Object));
661 static void handle_stop P_ ((struct it *));
662 static int tool_bar_lines_needed P_ ((struct frame *));
663 static int single_display_prop_intangible_p P_ ((Lisp_Object));
664 static void ensure_echo_area_buffers P_ ((void));
665 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
666 struct glyph_row *,
667 struct glyph_row *));
668 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
669 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
670 static int with_echo_area_buffer P_ ((struct window *, int,
671 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
672 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
673 static void clear_garbaged_frames P_ ((void));
674 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
675 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
676 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
677 static int display_echo_area P_ ((struct window *));
678 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
681 static int string_char_and_length P_ ((unsigned char *, int, int *));
682 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
683 struct text_pos));
684 static int compute_window_start_on_continuation_line P_ ((struct window *));
685 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
686 static void insert_left_trunc_glyphs P_ ((struct it *));
687 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
688 static void extend_face_to_end_of_line P_ ((struct it *));
689 static int append_space P_ ((struct it *, int));
690 static void make_cursor_line_fully_visible P_ ((struct window *));
691 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
692 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
693 static int trailing_whitespace_p P_ ((int));
694 static int message_log_check_duplicate P_ ((int, int, int, int));
695 int invisible_p P_ ((Lisp_Object, Lisp_Object));
696 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
697 static void push_it P_ ((struct it *));
698 static void pop_it P_ ((struct it *));
699 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
700 static void redisplay_internal P_ ((int));
701 static int echo_area_display P_ ((int));
702 static void redisplay_windows P_ ((Lisp_Object));
703 static void redisplay_window P_ ((Lisp_Object, int));
704 static void update_menu_bar P_ ((struct frame *, int));
705 static int try_window_reusing_current_matrix P_ ((struct window *));
706 static int try_window_id P_ ((struct window *));
707 static int display_line P_ ((struct it *));
708 static int display_mode_lines P_ ((struct window *));
709 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
710 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
711 static char *decode_mode_spec P_ ((struct window *, int, int, int));
712 static void display_menu_bar P_ ((struct window *));
713 static int display_count_lines P_ ((int, int, int, int, int *));
714 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
715 int, int, struct it *, int, int, int, int));
716 static void compute_line_metrics P_ ((struct it *));
717 static void run_redisplay_end_trigger_hook P_ ((struct it *));
718 static int get_overlay_strings P_ ((struct it *));
719 static void next_overlay_string P_ ((struct it *));
720 static void reseat P_ ((struct it *, struct text_pos, int));
721 static void reseat_1 P_ ((struct it *, struct text_pos, int));
722 static void back_to_previous_visible_line_start P_ ((struct it *));
723 static void reseat_at_previous_visible_line_start P_ ((struct it *));
724 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
725 static int next_element_from_display_vector P_ ((struct it *));
726 static int next_element_from_string P_ ((struct it *));
727 static int next_element_from_c_string P_ ((struct it *));
728 static int next_element_from_buffer P_ ((struct it *));
729 static int next_element_from_composition P_ ((struct it *));
730 static int next_element_from_image P_ ((struct it *));
731 static int next_element_from_stretch P_ ((struct it *));
732 static void load_overlay_strings P_ ((struct it *));
733 static void init_from_display_pos P_ ((struct it *, struct window *,
734 struct display_pos *));
735 static void reseat_to_string P_ ((struct it *, unsigned char *,
736 Lisp_Object, int, int, int, int));
737 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
738 int, int, int));
739 void move_it_vertically_backward P_ ((struct it *, int));
740 static void init_to_row_start P_ ((struct it *, struct window *,
741 struct glyph_row *));
742 static void init_to_row_end P_ ((struct it *, struct window *,
743 struct glyph_row *));
744 static void back_to_previous_line_start P_ ((struct it *));
745 static int forward_to_next_line_start P_ ((struct it *, int *));
746 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
747 Lisp_Object, int));
748 static struct text_pos string_pos P_ ((int, Lisp_Object));
749 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
750 static int number_of_chars P_ ((unsigned char *, int));
751 static void compute_stop_pos P_ ((struct it *));
752 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
753 Lisp_Object));
754 static int face_before_or_after_it_pos P_ ((struct it *, int));
755 static int next_overlay_change P_ ((int));
756 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
757 Lisp_Object, struct text_pos *));
758
759 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
760 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
761
762 #ifdef HAVE_WINDOW_SYSTEM
763
764 static void update_tool_bar P_ ((struct frame *, int));
765 static void build_desired_tool_bar_string P_ ((struct frame *f));
766 static int redisplay_tool_bar P_ ((struct frame *));
767 static void display_tool_bar_line P_ ((struct it *));
768
769 #endif /* HAVE_WINDOW_SYSTEM */
770
771 \f
772 /***********************************************************************
773 Window display dimensions
774 ***********************************************************************/
775
776 /* Return the window-relative maximum y + 1 for glyph rows displaying
777 text in window W. This is the height of W minus the height of a
778 mode line, if any. */
779
780 INLINE int
781 window_text_bottom_y (w)
782 struct window *w;
783 {
784 struct frame *f = XFRAME (w->frame);
785 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
786
787 if (WINDOW_WANTS_MODELINE_P (w))
788 height -= CURRENT_MODE_LINE_HEIGHT (w);
789 return height;
790 }
791
792
793 /* Return the pixel width of display area AREA of window W. AREA < 0
794 means return the total width of W, not including bitmap areas to
795 the left and right of the window. */
796
797 INLINE int
798 window_box_width (w, area)
799 struct window *w;
800 int area;
801 {
802 struct frame *f = XFRAME (w->frame);
803 int width = XFASTINT (w->width);
804
805 if (!w->pseudo_window_p)
806 {
807 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
808
809 if (area == TEXT_AREA)
810 {
811 if (INTEGERP (w->left_margin_width))
812 width -= XFASTINT (w->left_margin_width);
813 if (INTEGERP (w->right_margin_width))
814 width -= XFASTINT (w->right_margin_width);
815 }
816 else if (area == LEFT_MARGIN_AREA)
817 width = (INTEGERP (w->left_margin_width)
818 ? XFASTINT (w->left_margin_width) : 0);
819 else if (area == RIGHT_MARGIN_AREA)
820 width = (INTEGERP (w->right_margin_width)
821 ? XFASTINT (w->right_margin_width) : 0);
822 }
823
824 return width * CANON_X_UNIT (f);
825 }
826
827
828 /* Return the pixel height of the display area of window W, not
829 including mode lines of W, if any.. */
830
831 INLINE int
832 window_box_height (w)
833 struct window *w;
834 {
835 struct frame *f = XFRAME (w->frame);
836 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
837
838 xassert (height >= 0);
839
840 if (WINDOW_WANTS_MODELINE_P (w))
841 height -= CURRENT_MODE_LINE_HEIGHT (w);
842
843 if (WINDOW_WANTS_HEADER_LINE_P (w))
844 height -= CURRENT_HEADER_LINE_HEIGHT (w);
845
846 return height;
847 }
848
849
850 /* Return the frame-relative coordinate of the left edge of display
851 area AREA of window W. AREA < 0 means return the left edge of the
852 whole window, to the right of any bitmap area at the left side of
853 W. */
854
855 INLINE int
856 window_box_left (w, area)
857 struct window *w;
858 int area;
859 {
860 struct frame *f = XFRAME (w->frame);
861 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
862
863 if (!w->pseudo_window_p)
864 {
865 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
866 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
867
868 if (area == TEXT_AREA)
869 x += window_box_width (w, LEFT_MARGIN_AREA);
870 else if (area == RIGHT_MARGIN_AREA)
871 x += (window_box_width (w, LEFT_MARGIN_AREA)
872 + window_box_width (w, TEXT_AREA));
873 }
874
875 return x;
876 }
877
878
879 /* Return the frame-relative coordinate of the right edge of display
880 area AREA of window W. AREA < 0 means return the left edge of the
881 whole window, to the left of any bitmap area at the right side of
882 W. */
883
884 INLINE int
885 window_box_right (w, area)
886 struct window *w;
887 int area;
888 {
889 return window_box_left (w, area) + window_box_width (w, area);
890 }
891
892
893 /* Get the bounding box of the display area AREA of window W, without
894 mode lines, in frame-relative coordinates. AREA < 0 means the
895 whole window, not including bitmap areas to the left and right of
896 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
897 coordinates of the upper-left corner of the box. Return in
898 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
899
900 INLINE void
901 window_box (w, area, box_x, box_y, box_width, box_height)
902 struct window *w;
903 int area;
904 int *box_x, *box_y, *box_width, *box_height;
905 {
906 struct frame *f = XFRAME (w->frame);
907
908 *box_width = window_box_width (w, area);
909 *box_height = window_box_height (w);
910 *box_x = window_box_left (w, area);
911 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
912 + XFASTINT (w->top) * CANON_Y_UNIT (f));
913 if (WINDOW_WANTS_HEADER_LINE_P (w))
914 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
915 }
916
917
918 /* Get the bounding box of the display area AREA of window W, without
919 mode lines. AREA < 0 means the whole window, not including bitmap
920 areas to the left and right of the window. Return in *TOP_LEFT_X
921 and TOP_LEFT_Y the frame-relative pixel coordinates of the
922 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
923 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
924 box. */
925
926 INLINE void
927 window_box_edges (w, area, top_left_x, top_left_y,
928 bottom_right_x, bottom_right_y)
929 struct window *w;
930 int area;
931 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
932 {
933 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
934 bottom_right_y);
935 *bottom_right_x += *top_left_x;
936 *bottom_right_y += *top_left_y;
937 }
938
939
940 \f
941 /***********************************************************************
942 Utilities
943 ***********************************************************************/
944
945 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
946 1 if POS is visible and the line containing POS is fully visible.
947 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
948 and header-lines heights. */
949
950 int
951 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
952 struct window *w;
953 int charpos, *fully, exact_mode_line_heights_p;
954 {
955 struct it it;
956 struct text_pos top;
957 int visible_p;
958 struct buffer *old_buffer = NULL;
959
960 if (XBUFFER (w->buffer) != current_buffer)
961 {
962 old_buffer = current_buffer;
963 set_buffer_internal_1 (XBUFFER (w->buffer));
964 }
965
966 *fully = visible_p = 0;
967 SET_TEXT_POS_FROM_MARKER (top, w->start);
968
969 /* Compute exact mode line heights, if requested. */
970 if (exact_mode_line_heights_p)
971 {
972 if (WINDOW_WANTS_MODELINE_P (w))
973 current_mode_line_height
974 = display_mode_line (w, MODE_LINE_FACE_ID,
975 current_buffer->mode_line_format);
976
977 if (WINDOW_WANTS_HEADER_LINE_P (w))
978 current_header_line_height
979 = display_mode_line (w, HEADER_LINE_FACE_ID,
980 current_buffer->header_line_format);
981 }
982
983 start_display (&it, w, top);
984 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
985 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
986
987 if (IT_CHARPOS (it) == charpos)
988 {
989 int line_height, line_bottom_y;
990 int line_top_y = it.current_y;
991 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
992
993 line_height = it.max_ascent + it.max_descent;
994 if (line_height == 0)
995 {
996 if (last_height)
997 line_height = last_height;
998 else if (charpos < ZV)
999 {
1000 move_it_by_lines (&it, 1, 1);
1001 line_height = (it.max_ascent || it.max_descent
1002 ? it.max_ascent + it.max_descent
1003 : last_height);
1004 }
1005 else
1006 {
1007 /* Use the default character height. */
1008 it.what = IT_CHARACTER;
1009 it.c = ' ';
1010 it.len = 1;
1011 PRODUCE_GLYPHS (&it);
1012 line_height = it.ascent + it.descent;
1013 }
1014 }
1015 line_bottom_y = line_top_y + line_height;
1016
1017 if (line_top_y < window_top_y)
1018 visible_p = line_bottom_y > window_top_y;
1019 else if (line_top_y < it.last_visible_y)
1020 {
1021 visible_p = 1;
1022 *fully = line_bottom_y <= it.last_visible_y;
1023 }
1024 }
1025 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1026 {
1027 move_it_by_lines (&it, 1, 0);
1028 if (charpos < IT_CHARPOS (it))
1029 {
1030 visible_p = 1;
1031 *fully = 0;
1032 }
1033 }
1034
1035 if (old_buffer)
1036 set_buffer_internal_1 (old_buffer);
1037
1038 current_header_line_height = current_mode_line_height = -1;
1039 return visible_p;
1040 }
1041
1042
1043 /* Return the next character from STR which is MAXLEN bytes long.
1044 Return in *LEN the length of the character. This is like
1045 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1046 we find one, we return a `?', but with the length of the invalid
1047 character. */
1048
1049 static INLINE int
1050 string_char_and_length (str, maxlen, len)
1051 unsigned char *str;
1052 int maxlen, *len;
1053 {
1054 int c;
1055
1056 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1057 if (!CHAR_VALID_P (c, 1))
1058 /* We may not change the length here because other places in Emacs
1059 don't use this function, i.e. they silently accept invalid
1060 characters. */
1061 c = '?';
1062
1063 return c;
1064 }
1065
1066
1067
1068 /* Given a position POS containing a valid character and byte position
1069 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1070
1071 static struct text_pos
1072 string_pos_nchars_ahead (pos, string, nchars)
1073 struct text_pos pos;
1074 Lisp_Object string;
1075 int nchars;
1076 {
1077 xassert (STRINGP (string) && nchars >= 0);
1078
1079 if (STRING_MULTIBYTE (string))
1080 {
1081 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1082 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1083 int len;
1084
1085 while (nchars--)
1086 {
1087 string_char_and_length (p, rest, &len);
1088 p += len, rest -= len;
1089 xassert (rest >= 0);
1090 CHARPOS (pos) += 1;
1091 BYTEPOS (pos) += len;
1092 }
1093 }
1094 else
1095 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1096
1097 return pos;
1098 }
1099
1100
1101 /* Value is the text position, i.e. character and byte position,
1102 for character position CHARPOS in STRING. */
1103
1104 static INLINE struct text_pos
1105 string_pos (charpos, string)
1106 int charpos;
1107 Lisp_Object string;
1108 {
1109 struct text_pos pos;
1110 xassert (STRINGP (string));
1111 xassert (charpos >= 0);
1112 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1113 return pos;
1114 }
1115
1116
1117 /* Value is a text position, i.e. character and byte position, for
1118 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1119 means recognize multibyte characters. */
1120
1121 static struct text_pos
1122 c_string_pos (charpos, s, multibyte_p)
1123 int charpos;
1124 unsigned char *s;
1125 int multibyte_p;
1126 {
1127 struct text_pos pos;
1128
1129 xassert (s != NULL);
1130 xassert (charpos >= 0);
1131
1132 if (multibyte_p)
1133 {
1134 int rest = strlen (s), len;
1135
1136 SET_TEXT_POS (pos, 0, 0);
1137 while (charpos--)
1138 {
1139 string_char_and_length (s, rest, &len);
1140 s += len, rest -= len;
1141 xassert (rest >= 0);
1142 CHARPOS (pos) += 1;
1143 BYTEPOS (pos) += len;
1144 }
1145 }
1146 else
1147 SET_TEXT_POS (pos, charpos, charpos);
1148
1149 return pos;
1150 }
1151
1152
1153 /* Value is the number of characters in C string S. MULTIBYTE_P
1154 non-zero means recognize multibyte characters. */
1155
1156 static int
1157 number_of_chars (s, multibyte_p)
1158 unsigned char *s;
1159 int multibyte_p;
1160 {
1161 int nchars;
1162
1163 if (multibyte_p)
1164 {
1165 int rest = strlen (s), len;
1166 unsigned char *p = (unsigned char *) s;
1167
1168 for (nchars = 0; rest > 0; ++nchars)
1169 {
1170 string_char_and_length (p, rest, &len);
1171 rest -= len, p += len;
1172 }
1173 }
1174 else
1175 nchars = strlen (s);
1176
1177 return nchars;
1178 }
1179
1180
1181 /* Compute byte position NEWPOS->bytepos corresponding to
1182 NEWPOS->charpos. POS is a known position in string STRING.
1183 NEWPOS->charpos must be >= POS.charpos. */
1184
1185 static void
1186 compute_string_pos (newpos, pos, string)
1187 struct text_pos *newpos, pos;
1188 Lisp_Object string;
1189 {
1190 xassert (STRINGP (string));
1191 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1192
1193 if (STRING_MULTIBYTE (string))
1194 *newpos = string_pos_nchars_ahead (pos, string,
1195 CHARPOS (*newpos) - CHARPOS (pos));
1196 else
1197 BYTEPOS (*newpos) = CHARPOS (*newpos);
1198 }
1199
1200
1201 \f
1202 /***********************************************************************
1203 Lisp form evaluation
1204 ***********************************************************************/
1205
1206 /* Error handler for safe_eval and safe_call. */
1207
1208 static Lisp_Object
1209 safe_eval_handler (arg)
1210 Lisp_Object arg;
1211 {
1212 add_to_log ("Error during redisplay: %s", arg, Qnil);
1213 return Qnil;
1214 }
1215
1216
1217 /* Evaluate SEXPR and return the result, or nil if something went
1218 wrong. */
1219
1220 Lisp_Object
1221 safe_eval (sexpr)
1222 Lisp_Object sexpr;
1223 {
1224 int count = specpdl_ptr - specpdl;
1225 struct gcpro gcpro1;
1226 Lisp_Object val;
1227
1228 GCPRO1 (sexpr);
1229 specbind (Qinhibit_redisplay, Qt);
1230 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1231 UNGCPRO;
1232 return unbind_to (count, val);
1233 }
1234
1235
1236 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1237 Return the result, or nil if something went wrong. */
1238
1239 Lisp_Object
1240 safe_call (nargs, args)
1241 int nargs;
1242 Lisp_Object *args;
1243 {
1244 int count = specpdl_ptr - specpdl;
1245 Lisp_Object val;
1246 struct gcpro gcpro1;
1247
1248 GCPRO1 (args[0]);
1249 gcpro1.nvars = nargs;
1250 specbind (Qinhibit_redisplay, Qt);
1251 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1252 safe_eval_handler);
1253 UNGCPRO;
1254 return unbind_to (count, val);
1255 }
1256
1257
1258 /* Call function FN with one argument ARG.
1259 Return the result, or nil if something went wrong. */
1260
1261 Lisp_Object
1262 safe_call1 (fn, arg)
1263 Lisp_Object fn, arg;
1264 {
1265 Lisp_Object args[2];
1266 args[0] = fn;
1267 args[1] = arg;
1268 return safe_call (2, args);
1269 }
1270
1271
1272 \f
1273 /***********************************************************************
1274 Debugging
1275 ***********************************************************************/
1276
1277 #if 0
1278
1279 /* Define CHECK_IT to perform sanity checks on iterators.
1280 This is for debugging. It is too slow to do unconditionally. */
1281
1282 static void
1283 check_it (it)
1284 struct it *it;
1285 {
1286 if (it->method == next_element_from_string)
1287 {
1288 xassert (STRINGP (it->string));
1289 xassert (IT_STRING_CHARPOS (*it) >= 0);
1290 }
1291 else if (it->method == next_element_from_buffer)
1292 {
1293 /* Check that character and byte positions agree. */
1294 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1295 }
1296
1297 if (it->dpvec)
1298 xassert (it->current.dpvec_index >= 0);
1299 else
1300 xassert (it->current.dpvec_index < 0);
1301 }
1302
1303 #define CHECK_IT(IT) check_it ((IT))
1304
1305 #else /* not 0 */
1306
1307 #define CHECK_IT(IT) (void) 0
1308
1309 #endif /* not 0 */
1310
1311
1312 #if GLYPH_DEBUG
1313
1314 /* Check that the window end of window W is what we expect it
1315 to be---the last row in the current matrix displaying text. */
1316
1317 static void
1318 check_window_end (w)
1319 struct window *w;
1320 {
1321 if (!MINI_WINDOW_P (w)
1322 && !NILP (w->window_end_valid))
1323 {
1324 struct glyph_row *row;
1325 xassert ((row = MATRIX_ROW (w->current_matrix,
1326 XFASTINT (w->window_end_vpos)),
1327 !row->enabled_p
1328 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1329 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1330 }
1331 }
1332
1333 #define CHECK_WINDOW_END(W) check_window_end ((W))
1334
1335 #else /* not GLYPH_DEBUG */
1336
1337 #define CHECK_WINDOW_END(W) (void) 0
1338
1339 #endif /* not GLYPH_DEBUG */
1340
1341
1342 \f
1343 /***********************************************************************
1344 Iterator initialization
1345 ***********************************************************************/
1346
1347 /* Initialize IT for displaying current_buffer in window W, starting
1348 at character position CHARPOS. CHARPOS < 0 means that no buffer
1349 position is specified which is useful when the iterator is assigned
1350 a position later. BYTEPOS is the byte position corresponding to
1351 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1352
1353 If ROW is not null, calls to produce_glyphs with IT as parameter
1354 will produce glyphs in that row.
1355
1356 BASE_FACE_ID is the id of a base face to use. It must be one of
1357 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1358 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1359 displaying the tool-bar.
1360
1361 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1362 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1363 corresponding mode line glyph row of the desired matrix of W. */
1364
1365 void
1366 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1367 struct it *it;
1368 struct window *w;
1369 int charpos, bytepos;
1370 struct glyph_row *row;
1371 enum face_id base_face_id;
1372 {
1373 int highlight_region_p;
1374
1375 /* Some precondition checks. */
1376 xassert (w != NULL && it != NULL);
1377 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1378
1379 /* If face attributes have been changed since the last redisplay,
1380 free realized faces now because they depend on face definitions
1381 that might have changed. */
1382 if (face_change_count)
1383 {
1384 face_change_count = 0;
1385 free_all_realized_faces (Qnil);
1386 }
1387
1388 /* Use one of the mode line rows of W's desired matrix if
1389 appropriate. */
1390 if (row == NULL)
1391 {
1392 if (base_face_id == MODE_LINE_FACE_ID)
1393 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1394 else if (base_face_id == HEADER_LINE_FACE_ID)
1395 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1396 }
1397
1398 /* Clear IT. */
1399 bzero (it, sizeof *it);
1400 it->current.overlay_string_index = -1;
1401 it->current.dpvec_index = -1;
1402 it->base_face_id = base_face_id;
1403
1404 /* The window in which we iterate over current_buffer: */
1405 XSETWINDOW (it->window, w);
1406 it->w = w;
1407 it->f = XFRAME (w->frame);
1408
1409 /* Extra space between lines (on window systems only). */
1410 if (base_face_id == DEFAULT_FACE_ID
1411 && FRAME_WINDOW_P (it->f))
1412 {
1413 if (NATNUMP (current_buffer->extra_line_spacing))
1414 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1415 else if (it->f->extra_line_spacing > 0)
1416 it->extra_line_spacing = it->f->extra_line_spacing;
1417 }
1418
1419 /* If realized faces have been removed, e.g. because of face
1420 attribute changes of named faces, recompute them. */
1421 if (FRAME_FACE_CACHE (it->f)->used == 0)
1422 recompute_basic_faces (it->f);
1423
1424 /* Current value of the `space-width', and 'height' properties. */
1425 it->space_width = Qnil;
1426 it->font_height = Qnil;
1427
1428 /* Are control characters displayed as `^C'? */
1429 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1430
1431 /* -1 means everything between a CR and the following line end
1432 is invisible. >0 means lines indented more than this value are
1433 invisible. */
1434 it->selective = (INTEGERP (current_buffer->selective_display)
1435 ? XFASTINT (current_buffer->selective_display)
1436 : (!NILP (current_buffer->selective_display)
1437 ? -1 : 0));
1438 it->selective_display_ellipsis_p
1439 = !NILP (current_buffer->selective_display_ellipses);
1440
1441 /* Display table to use. */
1442 it->dp = window_display_table (w);
1443
1444 /* Are multibyte characters enabled in current_buffer? */
1445 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1446
1447 /* Non-zero if we should highlight the region. */
1448 highlight_region_p
1449 = (!NILP (Vtransient_mark_mode)
1450 && !NILP (current_buffer->mark_active)
1451 && XMARKER (current_buffer->mark)->buffer != 0);
1452
1453 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1454 start and end of a visible region in window IT->w. Set both to
1455 -1 to indicate no region. */
1456 if (highlight_region_p
1457 /* Maybe highlight only in selected window. */
1458 && (/* Either show region everywhere. */
1459 highlight_nonselected_windows
1460 /* Or show region in the selected window. */
1461 || w == XWINDOW (selected_window)
1462 /* Or show the region if we are in the mini-buffer and W is
1463 the window the mini-buffer refers to. */
1464 || (MINI_WINDOW_P (XWINDOW (selected_window))
1465 && w == XWINDOW (Vminibuf_scroll_window))))
1466 {
1467 int charpos = marker_position (current_buffer->mark);
1468 it->region_beg_charpos = min (PT, charpos);
1469 it->region_end_charpos = max (PT, charpos);
1470 }
1471 else
1472 it->region_beg_charpos = it->region_end_charpos = -1;
1473
1474 /* Get the position at which the redisplay_end_trigger hook should
1475 be run, if it is to be run at all. */
1476 if (MARKERP (w->redisplay_end_trigger)
1477 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1478 it->redisplay_end_trigger_charpos
1479 = marker_position (w->redisplay_end_trigger);
1480 else if (INTEGERP (w->redisplay_end_trigger))
1481 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1482
1483 /* Correct bogus values of tab_width. */
1484 it->tab_width = XINT (current_buffer->tab_width);
1485 if (it->tab_width <= 0 || it->tab_width > 1000)
1486 it->tab_width = 8;
1487
1488 /* Are lines in the display truncated? */
1489 it->truncate_lines_p
1490 = (base_face_id != DEFAULT_FACE_ID
1491 || XINT (it->w->hscroll)
1492 || (truncate_partial_width_windows
1493 && !WINDOW_FULL_WIDTH_P (it->w))
1494 || !NILP (current_buffer->truncate_lines));
1495
1496 /* Get dimensions of truncation and continuation glyphs. These are
1497 displayed as bitmaps under X, so we don't need them for such
1498 frames. */
1499 if (!FRAME_WINDOW_P (it->f))
1500 {
1501 if (it->truncate_lines_p)
1502 {
1503 /* We will need the truncation glyph. */
1504 xassert (it->glyph_row == NULL);
1505 produce_special_glyphs (it, IT_TRUNCATION);
1506 it->truncation_pixel_width = it->pixel_width;
1507 }
1508 else
1509 {
1510 /* We will need the continuation glyph. */
1511 xassert (it->glyph_row == NULL);
1512 produce_special_glyphs (it, IT_CONTINUATION);
1513 it->continuation_pixel_width = it->pixel_width;
1514 }
1515
1516 /* Reset these values to zero becaue the produce_special_glyphs
1517 above has changed them. */
1518 it->pixel_width = it->ascent = it->descent = 0;
1519 it->phys_ascent = it->phys_descent = 0;
1520 }
1521
1522 /* Set this after getting the dimensions of truncation and
1523 continuation glyphs, so that we don't produce glyphs when calling
1524 produce_special_glyphs, above. */
1525 it->glyph_row = row;
1526 it->area = TEXT_AREA;
1527
1528 /* Get the dimensions of the display area. The display area
1529 consists of the visible window area plus a horizontally scrolled
1530 part to the left of the window. All x-values are relative to the
1531 start of this total display area. */
1532 if (base_face_id != DEFAULT_FACE_ID)
1533 {
1534 /* Mode lines, menu bar in terminal frames. */
1535 it->first_visible_x = 0;
1536 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1537 }
1538 else
1539 {
1540 it->first_visible_x
1541 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1542 it->last_visible_x = (it->first_visible_x
1543 + window_box_width (w, TEXT_AREA));
1544
1545 /* If we truncate lines, leave room for the truncator glyph(s) at
1546 the right margin. Otherwise, leave room for the continuation
1547 glyph(s). Truncation and continuation glyphs are not inserted
1548 for window-based redisplay. */
1549 if (!FRAME_WINDOW_P (it->f))
1550 {
1551 if (it->truncate_lines_p)
1552 it->last_visible_x -= it->truncation_pixel_width;
1553 else
1554 it->last_visible_x -= it->continuation_pixel_width;
1555 }
1556
1557 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1558 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1559 }
1560
1561 /* Leave room for a border glyph. */
1562 if (!FRAME_WINDOW_P (it->f)
1563 && !WINDOW_RIGHTMOST_P (it->w))
1564 it->last_visible_x -= 1;
1565
1566 it->last_visible_y = window_text_bottom_y (w);
1567
1568 /* For mode lines and alike, arrange for the first glyph having a
1569 left box line if the face specifies a box. */
1570 if (base_face_id != DEFAULT_FACE_ID)
1571 {
1572 struct face *face;
1573
1574 it->face_id = base_face_id;
1575
1576 /* If we have a boxed mode line, make the first character appear
1577 with a left box line. */
1578 face = FACE_FROM_ID (it->f, base_face_id);
1579 if (face->box != FACE_NO_BOX)
1580 it->start_of_box_run_p = 1;
1581 }
1582
1583 /* If a buffer position was specified, set the iterator there,
1584 getting overlays and face properties from that position. */
1585 if (charpos > 0)
1586 {
1587 it->end_charpos = ZV;
1588 it->face_id = -1;
1589 IT_CHARPOS (*it) = charpos;
1590
1591 /* Compute byte position if not specified. */
1592 if (bytepos <= 0)
1593 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1594 else
1595 IT_BYTEPOS (*it) = bytepos;
1596
1597 /* Compute faces etc. */
1598 reseat (it, it->current.pos, 1);
1599 }
1600
1601 CHECK_IT (it);
1602 }
1603
1604
1605 /* Initialize IT for the display of window W with window start POS. */
1606
1607 void
1608 start_display (it, w, pos)
1609 struct it *it;
1610 struct window *w;
1611 struct text_pos pos;
1612 {
1613 int start_at_line_beg_p;
1614 struct glyph_row *row;
1615 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1616 int first_y;
1617
1618 row = w->desired_matrix->rows + first_vpos;
1619 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1620 first_y = it->current_y;
1621
1622 /* If window start is not at a line start, move back to the line
1623 start. This makes sure that we take continuation lines into
1624 account. */
1625 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1626 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1627 if (!start_at_line_beg_p)
1628 reseat_at_previous_visible_line_start (it);
1629
1630 /* If window start is not at a line start, skip forward to POS to
1631 get the correct continuation_lines_width and current_x. */
1632 if (!start_at_line_beg_p)
1633 {
1634 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1635
1636 /* If lines are continued, this line may end in the middle of a
1637 multi-glyph character (e.g. a control character displayed as
1638 \003, or in the middle of an overlay string). In this case
1639 move_it_to above will not have taken us to the start of
1640 the continuation line but to the end of the continued line. */
1641 if (!it->truncate_lines_p)
1642 {
1643 if (it->current_x > 0)
1644 {
1645 if (it->current.dpvec_index >= 0
1646 || it->current.overlay_string_index >= 0)
1647 {
1648 set_iterator_to_next (it, 1);
1649 move_it_in_display_line_to (it, -1, -1, 0);
1650 }
1651
1652 it->continuation_lines_width += it->current_x;
1653 }
1654
1655 /* We're starting a new display line, not affected by the
1656 height of the continued line, so clear the appropriate
1657 fields in the iterator structure. */
1658 it->max_ascent = it->max_descent = 0;
1659 it->max_phys_ascent = it->max_phys_descent = 0;
1660 }
1661
1662 it->current_y = first_y;
1663 it->vpos = 0;
1664 it->current_x = it->hpos = 0;
1665 }
1666
1667 #if 0 /* Don't assert the following because start_display is sometimes
1668 called intentionally with a window start that is not at a
1669 line start. Please leave this code in as a comment. */
1670
1671 /* Window start should be on a line start, now. */
1672 xassert (it->continuation_lines_width
1673 || IT_CHARPOS (it) == BEGV
1674 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1675 #endif /* 0 */
1676 }
1677
1678
1679 /* Initialize IT for stepping through current_buffer in window W,
1680 starting at position POS that includes overlay string and display
1681 vector/ control character translation position information. */
1682
1683 static void
1684 init_from_display_pos (it, w, pos)
1685 struct it *it;
1686 struct window *w;
1687 struct display_pos *pos;
1688 {
1689 /* Keep in mind: the call to reseat in init_iterator skips invisible
1690 text, so we might end up at a position different from POS. This
1691 is only a problem when POS is a row start after a newline and an
1692 overlay starts there with an after-string, and the overlay has an
1693 invisible property. Since we don't skip invisible text in
1694 display_line and elsewhere immediately after consuming the
1695 newline before the row start, such a POS will not be in a string,
1696 but the call to init_iterator below will move us to the
1697 after-string. */
1698 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1699 NULL, DEFAULT_FACE_ID);
1700
1701 /* If position is within an overlay string, set up IT to
1702 the right overlay string. */
1703 if (pos->overlay_string_index >= 0)
1704 {
1705 int relative_index;
1706
1707 /* We already have the first chunk of overlay strings in
1708 IT->overlay_strings. Load more until the one for
1709 pos->overlay_string_index is in IT->overlay_strings. */
1710 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1711 {
1712 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1713 it->current.overlay_string_index = 0;
1714 while (n--)
1715 {
1716 load_overlay_strings (it);
1717 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1718 }
1719 }
1720
1721 it->current.overlay_string_index = pos->overlay_string_index;
1722 relative_index = (it->current.overlay_string_index
1723 % OVERLAY_STRING_CHUNK_SIZE);
1724 it->string = it->overlay_strings[relative_index];
1725 xassert (STRINGP (it->string));
1726 it->current.string_pos = pos->string_pos;
1727 it->method = next_element_from_string;
1728 }
1729 else if (CHARPOS (pos->string_pos) >= 0)
1730 {
1731 /* Recorded position is not in an overlay string, but in another
1732 string. This can only be a string from a `display' property.
1733 IT should already be filled with that string. */
1734 it->current.string_pos = pos->string_pos;
1735 xassert (STRINGP (it->string));
1736 }
1737
1738 /* Restore position in display vector translations or control
1739 character translations. */
1740 if (pos->dpvec_index >= 0)
1741 {
1742 /* This fills IT->dpvec. */
1743 get_next_display_element (it);
1744 xassert (it->dpvec && it->current.dpvec_index == 0);
1745 it->current.dpvec_index = pos->dpvec_index;
1746 }
1747
1748 CHECK_IT (it);
1749 }
1750
1751
1752 /* Initialize IT for stepping through current_buffer in window W
1753 starting at ROW->start. */
1754
1755 static void
1756 init_to_row_start (it, w, row)
1757 struct it *it;
1758 struct window *w;
1759 struct glyph_row *row;
1760 {
1761 init_from_display_pos (it, w, &row->start);
1762 it->continuation_lines_width = row->continuation_lines_width;
1763 CHECK_IT (it);
1764 }
1765
1766
1767 /* Initialize IT for stepping through current_buffer in window W
1768 starting in the line following ROW, i.e. starting at ROW->end. */
1769
1770 static void
1771 init_to_row_end (it, w, row)
1772 struct it *it;
1773 struct window *w;
1774 struct glyph_row *row;
1775 {
1776 init_from_display_pos (it, w, &row->end);
1777
1778 if (row->continued_p)
1779 it->continuation_lines_width = (row->continuation_lines_width
1780 + row->pixel_width);
1781 CHECK_IT (it);
1782 }
1783
1784
1785
1786 \f
1787 /***********************************************************************
1788 Text properties
1789 ***********************************************************************/
1790
1791 /* Called when IT reaches IT->stop_charpos. Handle text property and
1792 overlay changes. Set IT->stop_charpos to the next position where
1793 to stop. */
1794
1795 static void
1796 handle_stop (it)
1797 struct it *it;
1798 {
1799 enum prop_handled handled;
1800 int handle_overlay_change_p = 1;
1801 struct props *p;
1802
1803 it->dpvec = NULL;
1804 it->current.dpvec_index = -1;
1805
1806 do
1807 {
1808 handled = HANDLED_NORMALLY;
1809
1810 /* Call text property handlers. */
1811 for (p = it_props; p->handler; ++p)
1812 {
1813 handled = p->handler (it);
1814
1815 if (handled == HANDLED_RECOMPUTE_PROPS)
1816 break;
1817 else if (handled == HANDLED_RETURN)
1818 return;
1819 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1820 handle_overlay_change_p = 0;
1821 }
1822
1823 if (handled != HANDLED_RECOMPUTE_PROPS)
1824 {
1825 /* Don't check for overlay strings below when set to deliver
1826 characters from a display vector. */
1827 if (it->method == next_element_from_display_vector)
1828 handle_overlay_change_p = 0;
1829
1830 /* Handle overlay changes. */
1831 if (handle_overlay_change_p)
1832 handled = handle_overlay_change (it);
1833
1834 /* Determine where to stop next. */
1835 if (handled == HANDLED_NORMALLY)
1836 compute_stop_pos (it);
1837 }
1838 }
1839 while (handled == HANDLED_RECOMPUTE_PROPS);
1840 }
1841
1842
1843 /* Compute IT->stop_charpos from text property and overlay change
1844 information for IT's current position. */
1845
1846 static void
1847 compute_stop_pos (it)
1848 struct it *it;
1849 {
1850 register INTERVAL iv, next_iv;
1851 Lisp_Object object, limit, position;
1852
1853 /* If nowhere else, stop at the end. */
1854 it->stop_charpos = it->end_charpos;
1855
1856 if (STRINGP (it->string))
1857 {
1858 /* Strings are usually short, so don't limit the search for
1859 properties. */
1860 object = it->string;
1861 limit = Qnil;
1862 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1863 }
1864 else
1865 {
1866 int charpos;
1867
1868 /* If next overlay change is in front of the current stop pos
1869 (which is IT->end_charpos), stop there. Note: value of
1870 next_overlay_change is point-max if no overlay change
1871 follows. */
1872 charpos = next_overlay_change (IT_CHARPOS (*it));
1873 if (charpos < it->stop_charpos)
1874 it->stop_charpos = charpos;
1875
1876 /* If showing the region, we have to stop at the region
1877 start or end because the face might change there. */
1878 if (it->region_beg_charpos > 0)
1879 {
1880 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1881 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1882 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1883 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1884 }
1885
1886 /* Set up variables for computing the stop position from text
1887 property changes. */
1888 XSETBUFFER (object, current_buffer);
1889 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1890 XSETFASTINT (position, IT_CHARPOS (*it));
1891
1892 }
1893
1894 /* Get the interval containing IT's position. Value is a null
1895 interval if there isn't such an interval. */
1896 iv = validate_interval_range (object, &position, &position, 0);
1897 if (!NULL_INTERVAL_P (iv))
1898 {
1899 Lisp_Object values_here[LAST_PROP_IDX];
1900 struct props *p;
1901
1902 /* Get properties here. */
1903 for (p = it_props; p->handler; ++p)
1904 values_here[p->idx] = textget (iv->plist, *p->name);
1905
1906 /* Look for an interval following iv that has different
1907 properties. */
1908 for (next_iv = next_interval (iv);
1909 (!NULL_INTERVAL_P (next_iv)
1910 && (NILP (limit)
1911 || XFASTINT (limit) > next_iv->position));
1912 next_iv = next_interval (next_iv))
1913 {
1914 for (p = it_props; p->handler; ++p)
1915 {
1916 Lisp_Object new_value;
1917
1918 new_value = textget (next_iv->plist, *p->name);
1919 if (!EQ (values_here[p->idx], new_value))
1920 break;
1921 }
1922
1923 if (p->handler)
1924 break;
1925 }
1926
1927 if (!NULL_INTERVAL_P (next_iv))
1928 {
1929 if (INTEGERP (limit)
1930 && next_iv->position >= XFASTINT (limit))
1931 /* No text property change up to limit. */
1932 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1933 else
1934 /* Text properties change in next_iv. */
1935 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1936 }
1937 }
1938
1939 xassert (STRINGP (it->string)
1940 || (it->stop_charpos >= BEGV
1941 && it->stop_charpos >= IT_CHARPOS (*it)));
1942 }
1943
1944
1945 /* Return the position of the next overlay change after POS in
1946 current_buffer. Value is point-max if no overlay change
1947 follows. This is like `next-overlay-change' but doesn't use
1948 xmalloc. */
1949
1950 static int
1951 next_overlay_change (pos)
1952 int pos;
1953 {
1954 int noverlays;
1955 int endpos;
1956 Lisp_Object *overlays;
1957 int len;
1958 int i;
1959
1960 /* Get all overlays at the given position. */
1961 len = 10;
1962 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1963 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1964 if (noverlays > len)
1965 {
1966 len = noverlays;
1967 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1968 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1969 }
1970
1971 /* If any of these overlays ends before endpos,
1972 use its ending point instead. */
1973 for (i = 0; i < noverlays; ++i)
1974 {
1975 Lisp_Object oend;
1976 int oendpos;
1977
1978 oend = OVERLAY_END (overlays[i]);
1979 oendpos = OVERLAY_POSITION (oend);
1980 endpos = min (endpos, oendpos);
1981 }
1982
1983 return endpos;
1984 }
1985
1986
1987 \f
1988 /***********************************************************************
1989 Fontification
1990 ***********************************************************************/
1991
1992 /* Handle changes in the `fontified' property of the current buffer by
1993 calling hook functions from Qfontification_functions to fontify
1994 regions of text. */
1995
1996 static enum prop_handled
1997 handle_fontified_prop (it)
1998 struct it *it;
1999 {
2000 Lisp_Object prop, pos;
2001 enum prop_handled handled = HANDLED_NORMALLY;
2002
2003 /* Get the value of the `fontified' property at IT's current buffer
2004 position. (The `fontified' property doesn't have a special
2005 meaning in strings.) If the value is nil, call functions from
2006 Qfontification_functions. */
2007 if (!STRINGP (it->string)
2008 && it->s == NULL
2009 && !NILP (Vfontification_functions)
2010 && !NILP (Vrun_hooks)
2011 && (pos = make_number (IT_CHARPOS (*it)),
2012 prop = Fget_char_property (pos, Qfontified, Qnil),
2013 NILP (prop)))
2014 {
2015 int count = specpdl_ptr - specpdl;
2016 Lisp_Object val;
2017
2018 val = Vfontification_functions;
2019 specbind (Qfontification_functions, Qnil);
2020 specbind (Qafter_change_functions, Qnil);
2021
2022 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2023 safe_call1 (val, pos);
2024 else
2025 {
2026 Lisp_Object globals, fn;
2027 struct gcpro gcpro1, gcpro2;
2028
2029 globals = Qnil;
2030 GCPRO2 (val, globals);
2031
2032 for (; CONSP (val); val = XCDR (val))
2033 {
2034 fn = XCAR (val);
2035
2036 if (EQ (fn, Qt))
2037 {
2038 /* A value of t indicates this hook has a local
2039 binding; it means to run the global binding too.
2040 In a global value, t should not occur. If it
2041 does, we must ignore it to avoid an endless
2042 loop. */
2043 for (globals = Fdefault_value (Qfontification_functions);
2044 CONSP (globals);
2045 globals = XCDR (globals))
2046 {
2047 fn = XCAR (globals);
2048 if (!EQ (fn, Qt))
2049 safe_call1 (fn, pos);
2050 }
2051 }
2052 else
2053 safe_call1 (fn, pos);
2054 }
2055
2056 UNGCPRO;
2057 }
2058
2059 unbind_to (count, Qnil);
2060
2061 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2062 something. This avoids an endless loop if they failed to
2063 fontify the text for which reason ever. */
2064 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2065 handled = HANDLED_RECOMPUTE_PROPS;
2066 }
2067
2068 return handled;
2069 }
2070
2071
2072 \f
2073 /***********************************************************************
2074 Faces
2075 ***********************************************************************/
2076
2077 /* Set up iterator IT from face properties at its current position.
2078 Called from handle_stop. */
2079
2080 static enum prop_handled
2081 handle_face_prop (it)
2082 struct it *it;
2083 {
2084 int new_face_id, next_stop;
2085
2086 if (!STRINGP (it->string))
2087 {
2088 new_face_id
2089 = face_at_buffer_position (it->w,
2090 IT_CHARPOS (*it),
2091 it->region_beg_charpos,
2092 it->region_end_charpos,
2093 &next_stop,
2094 (IT_CHARPOS (*it)
2095 + TEXT_PROP_DISTANCE_LIMIT),
2096 0);
2097
2098 /* Is this a start of a run of characters with box face?
2099 Caveat: this can be called for a freshly initialized
2100 iterator; face_id is -1 is this case. We know that the new
2101 face will not change until limit, i.e. if the new face has a
2102 box, all characters up to limit will have one. But, as
2103 usual, we don't know whether limit is really the end. */
2104 if (new_face_id != it->face_id)
2105 {
2106 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2107
2108 /* If new face has a box but old face has not, this is
2109 the start of a run of characters with box, i.e. it has
2110 a shadow on the left side. The value of face_id of the
2111 iterator will be -1 if this is the initial call that gets
2112 the face. In this case, we have to look in front of IT's
2113 position and see whether there is a face != new_face_id. */
2114 it->start_of_box_run_p
2115 = (new_face->box != FACE_NO_BOX
2116 && (it->face_id >= 0
2117 || IT_CHARPOS (*it) == BEG
2118 || new_face_id != face_before_it_pos (it)));
2119 it->face_box_p = new_face->box != FACE_NO_BOX;
2120 }
2121 }
2122 else
2123 {
2124 new_face_id
2125 = face_at_string_position (it->w,
2126 it->string,
2127 IT_STRING_CHARPOS (*it),
2128 (it->current.overlay_string_index >= 0
2129 ? IT_CHARPOS (*it)
2130 : 0),
2131 it->region_beg_charpos,
2132 it->region_end_charpos,
2133 &next_stop,
2134 it->base_face_id);
2135
2136 #if 0 /* This shouldn't be neccessary. Let's check it. */
2137 /* If IT is used to display a mode line we would really like to
2138 use the mode line face instead of the frame's default face. */
2139 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2140 && new_face_id == DEFAULT_FACE_ID)
2141 new_face_id = MODE_LINE_FACE_ID;
2142 #endif
2143
2144 /* Is this a start of a run of characters with box? Caveat:
2145 this can be called for a freshly allocated iterator; face_id
2146 is -1 is this case. We know that the new face will not
2147 change until the next check pos, i.e. if the new face has a
2148 box, all characters up to that position will have a
2149 box. But, as usual, we don't know whether that position
2150 is really the end. */
2151 if (new_face_id != it->face_id)
2152 {
2153 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2154 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2155
2156 /* If new face has a box but old face hasn't, this is the
2157 start of a run of characters with box, i.e. it has a
2158 shadow on the left side. */
2159 it->start_of_box_run_p
2160 = new_face->box && (old_face == NULL || !old_face->box);
2161 it->face_box_p = new_face->box != FACE_NO_BOX;
2162 }
2163 }
2164
2165 it->face_id = new_face_id;
2166 return HANDLED_NORMALLY;
2167 }
2168
2169
2170 /* Compute the face one character before or after the current position
2171 of IT. BEFORE_P non-zero means get the face in front of IT's
2172 position. Value is the id of the face. */
2173
2174 static int
2175 face_before_or_after_it_pos (it, before_p)
2176 struct it *it;
2177 int before_p;
2178 {
2179 int face_id, limit;
2180 int next_check_charpos;
2181 struct text_pos pos;
2182
2183 xassert (it->s == NULL);
2184
2185 if (STRINGP (it->string))
2186 {
2187 /* No face change past the end of the string (for the case
2188 we are padding with spaces). No face change before the
2189 string start. */
2190 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2191 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2192 return it->face_id;
2193
2194 /* Set pos to the position before or after IT's current position. */
2195 if (before_p)
2196 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2197 else
2198 /* For composition, we must check the character after the
2199 composition. */
2200 pos = (it->what == IT_COMPOSITION
2201 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2202 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2203
2204 /* Get the face for ASCII, or unibyte. */
2205 face_id
2206 = face_at_string_position (it->w,
2207 it->string,
2208 CHARPOS (pos),
2209 (it->current.overlay_string_index >= 0
2210 ? IT_CHARPOS (*it)
2211 : 0),
2212 it->region_beg_charpos,
2213 it->region_end_charpos,
2214 &next_check_charpos,
2215 it->base_face_id);
2216
2217 /* Correct the face for charsets different from ASCII. Do it
2218 for the multibyte case only. The face returned above is
2219 suitable for unibyte text if IT->string is unibyte. */
2220 if (STRING_MULTIBYTE (it->string))
2221 {
2222 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2223 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2224 int c, len;
2225 struct face *face = FACE_FROM_ID (it->f, face_id);
2226
2227 c = string_char_and_length (p, rest, &len);
2228 face_id = FACE_FOR_CHAR (it->f, face, c);
2229 }
2230 }
2231 else
2232 {
2233 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2234 || (IT_CHARPOS (*it) <= BEGV && before_p))
2235 return it->face_id;
2236
2237 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2238 pos = it->current.pos;
2239
2240 if (before_p)
2241 DEC_TEXT_POS (pos, it->multibyte_p);
2242 else
2243 {
2244 if (it->what == IT_COMPOSITION)
2245 /* For composition, we must check the position after the
2246 composition. */
2247 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2248 else
2249 INC_TEXT_POS (pos, it->multibyte_p);
2250 }
2251 /* Determine face for CHARSET_ASCII, or unibyte. */
2252 face_id = face_at_buffer_position (it->w,
2253 CHARPOS (pos),
2254 it->region_beg_charpos,
2255 it->region_end_charpos,
2256 &next_check_charpos,
2257 limit, 0);
2258
2259 /* Correct the face for charsets different from ASCII. Do it
2260 for the multibyte case only. The face returned above is
2261 suitable for unibyte text if current_buffer is unibyte. */
2262 if (it->multibyte_p)
2263 {
2264 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2265 struct face *face = FACE_FROM_ID (it->f, face_id);
2266 face_id = FACE_FOR_CHAR (it->f, face, c);
2267 }
2268 }
2269
2270 return face_id;
2271 }
2272
2273
2274 \f
2275 /***********************************************************************
2276 Invisible text
2277 ***********************************************************************/
2278
2279 /* Set up iterator IT from invisible properties at its current
2280 position. Called from handle_stop. */
2281
2282 static enum prop_handled
2283 handle_invisible_prop (it)
2284 struct it *it;
2285 {
2286 enum prop_handled handled = HANDLED_NORMALLY;
2287
2288 if (STRINGP (it->string))
2289 {
2290 extern Lisp_Object Qinvisible;
2291 Lisp_Object prop, end_charpos, limit, charpos;
2292
2293 /* Get the value of the invisible text property at the
2294 current position. Value will be nil if there is no such
2295 property. */
2296 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2297 prop = Fget_text_property (charpos, Qinvisible, it->string);
2298
2299 if (!NILP (prop)
2300 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2301 {
2302 handled = HANDLED_RECOMPUTE_PROPS;
2303
2304 /* Get the position at which the next change of the
2305 invisible text property can be found in IT->string.
2306 Value will be nil if the property value is the same for
2307 all the rest of IT->string. */
2308 XSETINT (limit, XSTRING (it->string)->size);
2309 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2310 it->string, limit);
2311
2312 /* Text at current position is invisible. The next
2313 change in the property is at position end_charpos.
2314 Move IT's current position to that position. */
2315 if (INTEGERP (end_charpos)
2316 && XFASTINT (end_charpos) < XFASTINT (limit))
2317 {
2318 struct text_pos old;
2319 old = it->current.string_pos;
2320 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2321 compute_string_pos (&it->current.string_pos, old, it->string);
2322 }
2323 else
2324 {
2325 /* The rest of the string is invisible. If this is an
2326 overlay string, proceed with the next overlay string
2327 or whatever comes and return a character from there. */
2328 if (it->current.overlay_string_index >= 0)
2329 {
2330 next_overlay_string (it);
2331 /* Don't check for overlay strings when we just
2332 finished processing them. */
2333 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2334 }
2335 else
2336 {
2337 struct Lisp_String *s = XSTRING (it->string);
2338 IT_STRING_CHARPOS (*it) = s->size;
2339 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2340 }
2341 }
2342 }
2343 }
2344 else
2345 {
2346 int visible_p, newpos, next_stop;
2347 Lisp_Object pos, prop;
2348
2349 /* First of all, is there invisible text at this position? */
2350 XSETFASTINT (pos, IT_CHARPOS (*it));
2351 prop = Fget_char_property (pos, Qinvisible, it->window);
2352
2353 /* If we are on invisible text, skip over it. */
2354 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2355 && IT_CHARPOS (*it) < it->end_charpos)
2356 {
2357 /* Record whether we have to display an ellipsis for the
2358 invisible text. */
2359 int display_ellipsis_p
2360 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2361
2362 handled = HANDLED_RECOMPUTE_PROPS;
2363
2364 /* Loop skipping over invisible text. The loop is left at
2365 ZV or with IT on the first char being visible again. */
2366 do
2367 {
2368 /* Try to skip some invisible text. Return value is the
2369 position reached which can be equal to IT's position
2370 if there is nothing invisible here. This skips both
2371 over invisible text properties and overlays with
2372 invisible property. */
2373 newpos = skip_invisible (IT_CHARPOS (*it),
2374 &next_stop, ZV, it->window);
2375
2376 /* If we skipped nothing at all we weren't at invisible
2377 text in the first place. If everything to the end of
2378 the buffer was skipped, end the loop. */
2379 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2380 visible_p = 1;
2381 else
2382 {
2383 /* We skipped some characters but not necessarily
2384 all there are. Check if we ended up on visible
2385 text. Fget_char_property returns the property of
2386 the char before the given position, i.e. if we
2387 get visible_p = 1, this means that the char at
2388 newpos is visible. */
2389 XSETFASTINT (pos, newpos);
2390 prop = Fget_char_property (pos, Qinvisible, it->window);
2391 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2392 }
2393
2394 /* If we ended up on invisible text, proceed to
2395 skip starting with next_stop. */
2396 if (!visible_p)
2397 IT_CHARPOS (*it) = next_stop;
2398 }
2399 while (!visible_p);
2400
2401 /* The position newpos is now either ZV or on visible text. */
2402 IT_CHARPOS (*it) = newpos;
2403 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2404
2405 /* Maybe return `...' next for the end of the invisible text. */
2406 if (display_ellipsis_p)
2407 {
2408 if (it->dp
2409 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2410 {
2411 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2412 it->dpvec = v->contents;
2413 it->dpend = v->contents + v->size;
2414 }
2415 else
2416 {
2417 /* Default `...'. */
2418 it->dpvec = default_invis_vector;
2419 it->dpend = default_invis_vector + 3;
2420 }
2421
2422 /* The ellipsis display does not replace the display of
2423 the character at the new position. Indicate this by
2424 setting IT->dpvec_char_len to zero. */
2425 it->dpvec_char_len = 0;
2426
2427 it->current.dpvec_index = 0;
2428 it->method = next_element_from_display_vector;
2429 }
2430 }
2431 }
2432
2433 return handled;
2434 }
2435
2436
2437 \f
2438 /***********************************************************************
2439 'display' property
2440 ***********************************************************************/
2441
2442 /* Set up iterator IT from `display' property at its current position.
2443 Called from handle_stop. */
2444
2445 static enum prop_handled
2446 handle_display_prop (it)
2447 struct it *it;
2448 {
2449 Lisp_Object prop, object;
2450 struct text_pos *position;
2451 int space_or_image_found_p;
2452
2453 if (STRINGP (it->string))
2454 {
2455 object = it->string;
2456 position = &it->current.string_pos;
2457 }
2458 else
2459 {
2460 object = Qnil;
2461 position = &it->current.pos;
2462 }
2463
2464 /* Reset those iterator values set from display property values. */
2465 it->font_height = Qnil;
2466 it->space_width = Qnil;
2467 it->voffset = 0;
2468
2469 /* We don't support recursive `display' properties, i.e. string
2470 values that have a string `display' property, that have a string
2471 `display' property etc. */
2472 if (!it->string_from_display_prop_p)
2473 it->area = TEXT_AREA;
2474
2475 prop = Fget_char_property (make_number (position->charpos),
2476 Qdisplay, object);
2477 if (NILP (prop))
2478 return HANDLED_NORMALLY;
2479
2480 space_or_image_found_p = 0;
2481 if (CONSP (prop)
2482 && CONSP (XCAR (prop))
2483 && !EQ (Qmargin, XCAR (XCAR (prop))))
2484 {
2485 /* A list of sub-properties. */
2486 while (CONSP (prop))
2487 {
2488 if (handle_single_display_prop (it, XCAR (prop), object, position))
2489 space_or_image_found_p = 1;
2490 prop = XCDR (prop);
2491 }
2492 }
2493 else if (VECTORP (prop))
2494 {
2495 int i;
2496 for (i = 0; i < XVECTOR (prop)->size; ++i)
2497 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2498 object, position))
2499 space_or_image_found_p = 1;
2500 }
2501 else
2502 {
2503 if (handle_single_display_prop (it, prop, object, position))
2504 space_or_image_found_p = 1;
2505 }
2506
2507 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2508 }
2509
2510
2511 /* Value is the position of the end of the `display' property starting
2512 at START_POS in OBJECT. */
2513
2514 static struct text_pos
2515 display_prop_end (it, object, start_pos)
2516 struct it *it;
2517 Lisp_Object object;
2518 struct text_pos start_pos;
2519 {
2520 Lisp_Object end;
2521 struct text_pos end_pos;
2522
2523 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2524 Qdisplay, object, Qnil);
2525 CHARPOS (end_pos) = XFASTINT (end);
2526 if (STRINGP (object))
2527 compute_string_pos (&end_pos, start_pos, it->string);
2528 else
2529 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2530
2531 return end_pos;
2532 }
2533
2534
2535 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2536 is the object in which the `display' property was found. *POSITION
2537 is the position at which it was found.
2538
2539 If PROP is a `space' or `image' sub-property, set *POSITION to the
2540 end position of the `display' property.
2541
2542 Value is non-zero if a `space' or `image' property value was found. */
2543
2544 static int
2545 handle_single_display_prop (it, prop, object, position)
2546 struct it *it;
2547 Lisp_Object prop;
2548 Lisp_Object object;
2549 struct text_pos *position;
2550 {
2551 Lisp_Object value;
2552 int space_or_image_found_p = 0;
2553 Lisp_Object form;
2554
2555 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2556 evaluated. If the result is nil, VALUE is ignored. */
2557 form = Qt;
2558 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2559 {
2560 prop = XCDR (prop);
2561 if (!CONSP (prop))
2562 return 0;
2563 form = XCAR (prop);
2564 prop = XCDR (prop);
2565 }
2566
2567 if (!NILP (form) && !EQ (form, Qt))
2568 {
2569 struct gcpro gcpro1;
2570 struct text_pos end_pos, pt;
2571
2572 GCPRO1 (form);
2573 end_pos = display_prop_end (it, object, *position);
2574
2575 /* Temporarily set point to the end position, and then evaluate
2576 the form. This makes `(eolp)' work as FORM. */
2577 if (BUFFERP (object))
2578 {
2579 CHARPOS (pt) = PT;
2580 BYTEPOS (pt) = PT_BYTE;
2581 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2582 }
2583
2584 form = safe_eval (form);
2585
2586 if (BUFFERP (object))
2587 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2588 UNGCPRO;
2589 }
2590
2591 if (NILP (form))
2592 return 0;
2593
2594 if (CONSP (prop)
2595 && EQ (XCAR (prop), Qheight)
2596 && CONSP (XCDR (prop)))
2597 {
2598 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2599 return 0;
2600
2601 /* `(height HEIGHT)'. */
2602 it->font_height = XCAR (XCDR (prop));
2603 if (!NILP (it->font_height))
2604 {
2605 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2606 int new_height = -1;
2607
2608 if (CONSP (it->font_height)
2609 && (EQ (XCAR (it->font_height), Qplus)
2610 || EQ (XCAR (it->font_height), Qminus))
2611 && CONSP (XCDR (it->font_height))
2612 && INTEGERP (XCAR (XCDR (it->font_height))))
2613 {
2614 /* `(+ N)' or `(- N)' where N is an integer. */
2615 int steps = XINT (XCAR (XCDR (it->font_height)));
2616 if (EQ (XCAR (it->font_height), Qplus))
2617 steps = - steps;
2618 it->face_id = smaller_face (it->f, it->face_id, steps);
2619 }
2620 else if (FUNCTIONP (it->font_height))
2621 {
2622 /* Call function with current height as argument.
2623 Value is the new height. */
2624 Lisp_Object height;
2625 height = safe_call1 (it->font_height,
2626 face->lface[LFACE_HEIGHT_INDEX]);
2627 if (NUMBERP (height))
2628 new_height = XFLOATINT (height);
2629 }
2630 else if (NUMBERP (it->font_height))
2631 {
2632 /* Value is a multiple of the canonical char height. */
2633 struct face *face;
2634
2635 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2636 new_height = (XFLOATINT (it->font_height)
2637 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2638 }
2639 else
2640 {
2641 /* Evaluate IT->font_height with `height' bound to the
2642 current specified height to get the new height. */
2643 Lisp_Object value;
2644 int count = specpdl_ptr - specpdl;
2645
2646 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2647 value = safe_eval (it->font_height);
2648 unbind_to (count, Qnil);
2649
2650 if (NUMBERP (value))
2651 new_height = XFLOATINT (value);
2652 }
2653
2654 if (new_height > 0)
2655 it->face_id = face_with_height (it->f, it->face_id, new_height);
2656 }
2657 }
2658 else if (CONSP (prop)
2659 && EQ (XCAR (prop), Qspace_width)
2660 && CONSP (XCDR (prop)))
2661 {
2662 /* `(space_width WIDTH)'. */
2663 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2664 return 0;
2665
2666 value = XCAR (XCDR (prop));
2667 if (NUMBERP (value) && XFLOATINT (value) > 0)
2668 it->space_width = value;
2669 }
2670 else if (CONSP (prop)
2671 && EQ (XCAR (prop), Qraise)
2672 && CONSP (XCDR (prop)))
2673 {
2674 /* `(raise FACTOR)'. */
2675 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2676 return 0;
2677
2678 #ifdef HAVE_WINDOW_SYSTEM
2679 value = XCAR (XCDR (prop));
2680 if (NUMBERP (value))
2681 {
2682 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2683 it->voffset = - (XFLOATINT (value)
2684 * (FONT_HEIGHT (face->font)));
2685 }
2686 #endif /* HAVE_WINDOW_SYSTEM */
2687 }
2688 else if (!it->string_from_display_prop_p)
2689 {
2690 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2691 VALUE) or `((margin nil) VALUE)' or VALUE. */
2692 Lisp_Object location, value;
2693 struct text_pos start_pos;
2694 int valid_p;
2695
2696 /* Characters having this form of property are not displayed, so
2697 we have to find the end of the property. */
2698 start_pos = *position;
2699 *position = display_prop_end (it, object, start_pos);
2700 value = Qnil;
2701
2702 /* Let's stop at the new position and assume that all
2703 text properties change there. */
2704 it->stop_charpos = position->charpos;
2705
2706 location = Qunbound;
2707 if (CONSP (prop) && CONSP (XCAR (prop)))
2708 {
2709 Lisp_Object tem;
2710
2711 value = XCDR (prop);
2712 if (CONSP (value))
2713 value = XCAR (value);
2714
2715 tem = XCAR (prop);
2716 if (EQ (XCAR (tem), Qmargin)
2717 && (tem = XCDR (tem),
2718 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2719 (NILP (tem)
2720 || EQ (tem, Qleft_margin)
2721 || EQ (tem, Qright_margin))))
2722 location = tem;
2723 }
2724
2725 if (EQ (location, Qunbound))
2726 {
2727 location = Qnil;
2728 value = prop;
2729 }
2730
2731 #ifdef HAVE_WINDOW_SYSTEM
2732 if (FRAME_TERMCAP_P (it->f))
2733 valid_p = STRINGP (value);
2734 else
2735 valid_p = (STRINGP (value)
2736 || (CONSP (value) && EQ (XCAR (value), Qspace))
2737 || valid_image_p (value));
2738 #else /* not HAVE_WINDOW_SYSTEM */
2739 valid_p = STRINGP (value);
2740 #endif /* not HAVE_WINDOW_SYSTEM */
2741
2742 if ((EQ (location, Qleft_margin)
2743 || EQ (location, Qright_margin)
2744 || NILP (location))
2745 && valid_p)
2746 {
2747 space_or_image_found_p = 1;
2748
2749 /* Save current settings of IT so that we can restore them
2750 when we are finished with the glyph property value. */
2751 push_it (it);
2752
2753 if (NILP (location))
2754 it->area = TEXT_AREA;
2755 else if (EQ (location, Qleft_margin))
2756 it->area = LEFT_MARGIN_AREA;
2757 else
2758 it->area = RIGHT_MARGIN_AREA;
2759
2760 if (STRINGP (value))
2761 {
2762 it->string = value;
2763 it->multibyte_p = STRING_MULTIBYTE (it->string);
2764 it->current.overlay_string_index = -1;
2765 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2766 it->end_charpos = it->string_nchars
2767 = XSTRING (it->string)->size;
2768 it->method = next_element_from_string;
2769 it->stop_charpos = 0;
2770 it->string_from_display_prop_p = 1;
2771 }
2772 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2773 {
2774 it->method = next_element_from_stretch;
2775 it->object = value;
2776 it->current.pos = it->position = start_pos;
2777 }
2778 #ifdef HAVE_WINDOW_SYSTEM
2779 else
2780 {
2781 it->what = IT_IMAGE;
2782 it->image_id = lookup_image (it->f, value);
2783 it->position = start_pos;
2784 it->object = NILP (object) ? it->w->buffer : object;
2785 it->method = next_element_from_image;
2786
2787 /* Say that we haven't consumed the characters with
2788 `display' property yet. The call to pop_it in
2789 set_iterator_to_next will clean this up. */
2790 *position = start_pos;
2791 }
2792 #endif /* HAVE_WINDOW_SYSTEM */
2793 }
2794 else
2795 /* Invalid property or property not supported. Restore
2796 the position to what it was before. */
2797 *position = start_pos;
2798 }
2799
2800 return space_or_image_found_p;
2801 }
2802
2803
2804 /* Check if PROP is a display sub-property value whose text should be
2805 treated as intangible. */
2806
2807 static int
2808 single_display_prop_intangible_p (prop)
2809 Lisp_Object prop;
2810 {
2811 /* Skip over `when FORM'. */
2812 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2813 {
2814 prop = XCDR (prop);
2815 if (!CONSP (prop))
2816 return 0;
2817 prop = XCDR (prop);
2818 }
2819
2820 if (!CONSP (prop))
2821 return 0;
2822
2823 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2824 we don't need to treat text as intangible. */
2825 if (EQ (XCAR (prop), Qmargin))
2826 {
2827 prop = XCDR (prop);
2828 if (!CONSP (prop))
2829 return 0;
2830
2831 prop = XCDR (prop);
2832 if (!CONSP (prop)
2833 || EQ (XCAR (prop), Qleft_margin)
2834 || EQ (XCAR (prop), Qright_margin))
2835 return 0;
2836 }
2837
2838 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2839 }
2840
2841
2842 /* Check if PROP is a display property value whose text should be
2843 treated as intangible. */
2844
2845 int
2846 display_prop_intangible_p (prop)
2847 Lisp_Object prop;
2848 {
2849 if (CONSP (prop)
2850 && CONSP (XCAR (prop))
2851 && !EQ (Qmargin, XCAR (XCAR (prop))))
2852 {
2853 /* A list of sub-properties. */
2854 while (CONSP (prop))
2855 {
2856 if (single_display_prop_intangible_p (XCAR (prop)))
2857 return 1;
2858 prop = XCDR (prop);
2859 }
2860 }
2861 else if (VECTORP (prop))
2862 {
2863 /* A vector of sub-properties. */
2864 int i;
2865 for (i = 0; i < XVECTOR (prop)->size; ++i)
2866 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2867 return 1;
2868 }
2869 else
2870 return single_display_prop_intangible_p (prop);
2871
2872 return 0;
2873 }
2874
2875 \f
2876 /***********************************************************************
2877 `composition' property
2878 ***********************************************************************/
2879
2880 /* Set up iterator IT from `composition' property at its current
2881 position. Called from handle_stop. */
2882
2883 static enum prop_handled
2884 handle_composition_prop (it)
2885 struct it *it;
2886 {
2887 Lisp_Object prop, string;
2888 int pos, pos_byte, end;
2889 enum prop_handled handled = HANDLED_NORMALLY;
2890
2891 if (STRINGP (it->string))
2892 {
2893 pos = IT_STRING_CHARPOS (*it);
2894 pos_byte = IT_STRING_BYTEPOS (*it);
2895 string = it->string;
2896 }
2897 else
2898 {
2899 pos = IT_CHARPOS (*it);
2900 pos_byte = IT_BYTEPOS (*it);
2901 string = Qnil;
2902 }
2903
2904 /* If there's a valid composition and point is not inside of the
2905 composition (in the case that the composition is from the current
2906 buffer), draw a glyph composed from the composition components. */
2907 if (find_composition (pos, -1, &pos, &end, &prop, string)
2908 && COMPOSITION_VALID_P (pos, end, prop)
2909 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2910 {
2911 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2912
2913 if (id >= 0)
2914 {
2915 it->method = next_element_from_composition;
2916 it->cmp_id = id;
2917 it->cmp_len = COMPOSITION_LENGTH (prop);
2918 /* For a terminal, draw only the first character of the
2919 components. */
2920 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2921 it->len = (STRINGP (it->string)
2922 ? string_char_to_byte (it->string, end)
2923 : CHAR_TO_BYTE (end)) - pos_byte;
2924 it->stop_charpos = end;
2925 handled = HANDLED_RETURN;
2926 }
2927 }
2928
2929 return handled;
2930 }
2931
2932
2933 \f
2934 /***********************************************************************
2935 Overlay strings
2936 ***********************************************************************/
2937
2938 /* The following structure is used to record overlay strings for
2939 later sorting in load_overlay_strings. */
2940
2941 struct overlay_entry
2942 {
2943 Lisp_Object overlay;
2944 Lisp_Object string;
2945 int priority;
2946 int after_string_p;
2947 };
2948
2949
2950 /* Set up iterator IT from overlay strings at its current position.
2951 Called from handle_stop. */
2952
2953 static enum prop_handled
2954 handle_overlay_change (it)
2955 struct it *it;
2956 {
2957 if (!STRINGP (it->string) && get_overlay_strings (it))
2958 return HANDLED_RECOMPUTE_PROPS;
2959 else
2960 return HANDLED_NORMALLY;
2961 }
2962
2963
2964 /* Set up the next overlay string for delivery by IT, if there is an
2965 overlay string to deliver. Called by set_iterator_to_next when the
2966 end of the current overlay string is reached. If there are more
2967 overlay strings to display, IT->string and
2968 IT->current.overlay_string_index are set appropriately here.
2969 Otherwise IT->string is set to nil. */
2970
2971 static void
2972 next_overlay_string (it)
2973 struct it *it;
2974 {
2975 ++it->current.overlay_string_index;
2976 if (it->current.overlay_string_index == it->n_overlay_strings)
2977 {
2978 /* No more overlay strings. Restore IT's settings to what
2979 they were before overlay strings were processed, and
2980 continue to deliver from current_buffer. */
2981 pop_it (it);
2982 xassert (it->stop_charpos >= BEGV
2983 && it->stop_charpos <= it->end_charpos);
2984 it->string = Qnil;
2985 it->current.overlay_string_index = -1;
2986 SET_TEXT_POS (it->current.string_pos, -1, -1);
2987 it->n_overlay_strings = 0;
2988 it->method = next_element_from_buffer;
2989
2990 /* If we're at the end of the buffer, record that we have
2991 processed the overlay strings there already, so that
2992 next_element_from_buffer doesn't try it again. */
2993 if (IT_CHARPOS (*it) >= it->end_charpos)
2994 it->overlay_strings_at_end_processed_p = 1;
2995 }
2996 else
2997 {
2998 /* There are more overlay strings to process. If
2999 IT->current.overlay_string_index has advanced to a position
3000 where we must load IT->overlay_strings with more strings, do
3001 it. */
3002 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3003
3004 if (it->current.overlay_string_index && i == 0)
3005 load_overlay_strings (it);
3006
3007 /* Initialize IT to deliver display elements from the overlay
3008 string. */
3009 it->string = it->overlay_strings[i];
3010 it->multibyte_p = STRING_MULTIBYTE (it->string);
3011 SET_TEXT_POS (it->current.string_pos, 0, 0);
3012 it->method = next_element_from_string;
3013 it->stop_charpos = 0;
3014 }
3015
3016 CHECK_IT (it);
3017 }
3018
3019
3020 /* Compare two overlay_entry structures E1 and E2. Used as a
3021 comparison function for qsort in load_overlay_strings. Overlay
3022 strings for the same position are sorted so that
3023
3024 1. All after-strings come in front of before-strings, except
3025 when they come from the same overlay.
3026
3027 2. Within after-strings, strings are sorted so that overlay strings
3028 from overlays with higher priorities come first.
3029
3030 2. Within before-strings, strings are sorted so that overlay
3031 strings from overlays with higher priorities come last.
3032
3033 Value is analogous to strcmp. */
3034
3035
3036 static int
3037 compare_overlay_entries (e1, e2)
3038 void *e1, *e2;
3039 {
3040 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3041 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3042 int result;
3043
3044 if (entry1->after_string_p != entry2->after_string_p)
3045 {
3046 /* Let after-strings appear in front of before-strings if
3047 they come from different overlays. */
3048 if (EQ (entry1->overlay, entry2->overlay))
3049 result = entry1->after_string_p ? 1 : -1;
3050 else
3051 result = entry1->after_string_p ? -1 : 1;
3052 }
3053 else if (entry1->after_string_p)
3054 /* After-strings sorted in order of decreasing priority. */
3055 result = entry2->priority - entry1->priority;
3056 else
3057 /* Before-strings sorted in order of increasing priority. */
3058 result = entry1->priority - entry2->priority;
3059
3060 return result;
3061 }
3062
3063
3064 /* Load the vector IT->overlay_strings with overlay strings from IT's
3065 current buffer position. Set IT->n_overlays to the total number of
3066 overlay strings found.
3067
3068 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3069 a time. On entry into load_overlay_strings,
3070 IT->current.overlay_string_index gives the number of overlay
3071 strings that have already been loaded by previous calls to this
3072 function.
3073
3074 IT->add_overlay_start contains an additional overlay start
3075 position to consider for taking overlay strings from, if non-zero.
3076 This position comes into play when the overlay has an `invisible'
3077 property, and both before and after-strings. When we've skipped to
3078 the end of the overlay, because of its `invisible' property, we
3079 nevertheless want its before-string to appear.
3080 IT->add_overlay_start will contain the overlay start position
3081 in this case.
3082
3083 Overlay strings are sorted so that after-string strings come in
3084 front of before-string strings. Within before and after-strings,
3085 strings are sorted by overlay priority. See also function
3086 compare_overlay_entries. */
3087
3088 static void
3089 load_overlay_strings (it)
3090 struct it *it;
3091 {
3092 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3093 Lisp_Object ov, overlay, window, str, invisible;
3094 int start, end;
3095 int size = 20;
3096 int n = 0, i, j, invis_p;
3097 struct overlay_entry *entries
3098 = (struct overlay_entry *) alloca (size * sizeof *entries);
3099 int charpos = IT_CHARPOS (*it);
3100
3101 /* Append the overlay string STRING of overlay OVERLAY to vector
3102 `entries' which has size `size' and currently contains `n'
3103 elements. AFTER_P non-zero means STRING is an after-string of
3104 OVERLAY. */
3105 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3106 do \
3107 { \
3108 Lisp_Object priority; \
3109 \
3110 if (n == size) \
3111 { \
3112 int new_size = 2 * size; \
3113 struct overlay_entry *old = entries; \
3114 entries = \
3115 (struct overlay_entry *) alloca (new_size \
3116 * sizeof *entries); \
3117 bcopy (old, entries, size * sizeof *entries); \
3118 size = new_size; \
3119 } \
3120 \
3121 entries[n].string = (STRING); \
3122 entries[n].overlay = (OVERLAY); \
3123 priority = Foverlay_get ((OVERLAY), Qpriority); \
3124 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3125 entries[n].after_string_p = (AFTER_P); \
3126 ++n; \
3127 } \
3128 while (0)
3129
3130 /* Process overlay before the overlay center. */
3131 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3132 {
3133 overlay = XCAR (ov);
3134 xassert (OVERLAYP (overlay));
3135 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3136 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3137
3138 if (end < charpos)
3139 break;
3140
3141 /* Skip this overlay if it doesn't start or end at IT's current
3142 position. */
3143 if (end != charpos && start != charpos)
3144 continue;
3145
3146 /* Skip this overlay if it doesn't apply to IT->w. */
3147 window = Foverlay_get (overlay, Qwindow);
3148 if (WINDOWP (window) && XWINDOW (window) != it->w)
3149 continue;
3150
3151 /* If the text ``under'' the overlay is invisible, both before-
3152 and after-strings from this overlay are visible; start and
3153 end position are indistinguishable. */
3154 invisible = Foverlay_get (overlay, Qinvisible);
3155 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3156
3157 /* If overlay has a non-empty before-string, record it. */
3158 if ((start == charpos || (end == charpos && invis_p))
3159 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3160 && XSTRING (str)->size)
3161 RECORD_OVERLAY_STRING (overlay, str, 0);
3162
3163 /* If overlay has a non-empty after-string, record it. */
3164 if ((end == charpos || (start == charpos && invis_p))
3165 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3166 && XSTRING (str)->size)
3167 RECORD_OVERLAY_STRING (overlay, str, 1);
3168 }
3169
3170 /* Process overlays after the overlay center. */
3171 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3172 {
3173 overlay = XCAR (ov);
3174 xassert (OVERLAYP (overlay));
3175 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3176 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3177
3178 if (start > charpos)
3179 break;
3180
3181 /* Skip this overlay if it doesn't start or end at IT's current
3182 position. */
3183 if (end != charpos && start != charpos)
3184 continue;
3185
3186 /* Skip this overlay if it doesn't apply to IT->w. */
3187 window = Foverlay_get (overlay, Qwindow);
3188 if (WINDOWP (window) && XWINDOW (window) != it->w)
3189 continue;
3190
3191 /* If the text ``under'' the overlay is invisible, it has a zero
3192 dimension, and both before- and after-strings apply. */
3193 invisible = Foverlay_get (overlay, Qinvisible);
3194 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3195
3196 /* If overlay has a non-empty before-string, record it. */
3197 if ((start == charpos || (end == charpos && invis_p))
3198 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3199 && XSTRING (str)->size)
3200 RECORD_OVERLAY_STRING (overlay, str, 0);
3201
3202 /* If overlay has a non-empty after-string, record it. */
3203 if ((end == charpos || (start == charpos && invis_p))
3204 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3205 && XSTRING (str)->size)
3206 RECORD_OVERLAY_STRING (overlay, str, 1);
3207 }
3208
3209 #undef RECORD_OVERLAY_STRING
3210
3211 /* Sort entries. */
3212 if (n > 1)
3213 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3214
3215 /* Record the total number of strings to process. */
3216 it->n_overlay_strings = n;
3217
3218 /* IT->current.overlay_string_index is the number of overlay strings
3219 that have already been consumed by IT. Copy some of the
3220 remaining overlay strings to IT->overlay_strings. */
3221 i = 0;
3222 j = it->current.overlay_string_index;
3223 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3224 it->overlay_strings[i++] = entries[j++].string;
3225
3226 CHECK_IT (it);
3227 }
3228
3229
3230 /* Get the first chunk of overlay strings at IT's current buffer
3231 position. Value is non-zero if at least one overlay string was
3232 found. */
3233
3234 static int
3235 get_overlay_strings (it)
3236 struct it *it;
3237 {
3238 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3239 process. This fills IT->overlay_strings with strings, and sets
3240 IT->n_overlay_strings to the total number of strings to process.
3241 IT->pos.overlay_string_index has to be set temporarily to zero
3242 because load_overlay_strings needs this; it must be set to -1
3243 when no overlay strings are found because a zero value would
3244 indicate a position in the first overlay string. */
3245 it->current.overlay_string_index = 0;
3246 load_overlay_strings (it);
3247
3248 /* If we found overlay strings, set up IT to deliver display
3249 elements from the first one. Otherwise set up IT to deliver
3250 from current_buffer. */
3251 if (it->n_overlay_strings)
3252 {
3253 /* Make sure we know settings in current_buffer, so that we can
3254 restore meaningful values when we're done with the overlay
3255 strings. */
3256 compute_stop_pos (it);
3257 xassert (it->face_id >= 0);
3258
3259 /* Save IT's settings. They are restored after all overlay
3260 strings have been processed. */
3261 xassert (it->sp == 0);
3262 push_it (it);
3263
3264 /* Set up IT to deliver display elements from the first overlay
3265 string. */
3266 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3267 it->stop_charpos = 0;
3268 it->string = it->overlay_strings[0];
3269 it->multibyte_p = STRING_MULTIBYTE (it->string);
3270 xassert (STRINGP (it->string));
3271 it->method = next_element_from_string;
3272 }
3273 else
3274 {
3275 it->string = Qnil;
3276 it->current.overlay_string_index = -1;
3277 it->method = next_element_from_buffer;
3278 }
3279
3280 CHECK_IT (it);
3281
3282 /* Value is non-zero if we found at least one overlay string. */
3283 return STRINGP (it->string);
3284 }
3285
3286
3287 \f
3288 /***********************************************************************
3289 Saving and restoring state
3290 ***********************************************************************/
3291
3292 /* Save current settings of IT on IT->stack. Called, for example,
3293 before setting up IT for an overlay string, to be able to restore
3294 IT's settings to what they were after the overlay string has been
3295 processed. */
3296
3297 static void
3298 push_it (it)
3299 struct it *it;
3300 {
3301 struct iterator_stack_entry *p;
3302
3303 xassert (it->sp < 2);
3304 p = it->stack + it->sp;
3305
3306 p->stop_charpos = it->stop_charpos;
3307 xassert (it->face_id >= 0);
3308 p->face_id = it->face_id;
3309 p->string = it->string;
3310 p->pos = it->current;
3311 p->end_charpos = it->end_charpos;
3312 p->string_nchars = it->string_nchars;
3313 p->area = it->area;
3314 p->multibyte_p = it->multibyte_p;
3315 p->space_width = it->space_width;
3316 p->font_height = it->font_height;
3317 p->voffset = it->voffset;
3318 p->string_from_display_prop_p = it->string_from_display_prop_p;
3319 ++it->sp;
3320 }
3321
3322
3323 /* Restore IT's settings from IT->stack. Called, for example, when no
3324 more overlay strings must be processed, and we return to delivering
3325 display elements from a buffer, or when the end of a string from a
3326 `display' property is reached and we return to delivering display
3327 elements from an overlay string, or from a buffer. */
3328
3329 static void
3330 pop_it (it)
3331 struct it *it;
3332 {
3333 struct iterator_stack_entry *p;
3334
3335 xassert (it->sp > 0);
3336 --it->sp;
3337 p = it->stack + it->sp;
3338 it->stop_charpos = p->stop_charpos;
3339 it->face_id = p->face_id;
3340 it->string = p->string;
3341 it->current = p->pos;
3342 it->end_charpos = p->end_charpos;
3343 it->string_nchars = p->string_nchars;
3344 it->area = p->area;
3345 it->multibyte_p = p->multibyte_p;
3346 it->space_width = p->space_width;
3347 it->font_height = p->font_height;
3348 it->voffset = p->voffset;
3349 it->string_from_display_prop_p = p->string_from_display_prop_p;
3350 }
3351
3352
3353 \f
3354 /***********************************************************************
3355 Moving over lines
3356 ***********************************************************************/
3357
3358 /* Set IT's current position to the previous line start. */
3359
3360 static void
3361 back_to_previous_line_start (it)
3362 struct it *it;
3363 {
3364 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3365 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3366 }
3367
3368
3369 /* Move IT to the next line start.
3370
3371 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3372 we skipped over part of the text (as opposed to moving the iterator
3373 continuously over the text). Otherwise, don't change the value
3374 of *SKIPPED_P.
3375
3376 Newlines may come from buffer text, overlay strings, or strings
3377 displayed via the `display' property. That's the reason we can't
3378 simply use find_next_newline_no_quit. */
3379
3380 static int
3381 forward_to_next_line_start (it, skipped_p)
3382 struct it *it;
3383 int *skipped_p;
3384 {
3385 int old_selective, newline_found_p, n;
3386 const int MAX_NEWLINE_DISTANCE = 500;
3387
3388 /* Don't handle selective display in the following. It's (a)
3389 unnecessary and (b) leads to an infinite recursion because
3390 next_element_from_ellipsis indirectly calls this function. */
3391 old_selective = it->selective;
3392 it->selective = 0;
3393
3394 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3395 from buffer text. */
3396 n = newline_found_p = 0;
3397 while (n < MAX_NEWLINE_DISTANCE
3398 && get_next_display_element (it)
3399 && !newline_found_p)
3400 {
3401 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3402 set_iterator_to_next (it, 0);
3403 if (!STRINGP (it->string))
3404 ++n;
3405 }
3406
3407 /* If we didn't find a newline near enough, see if we can use a
3408 short-cut. */
3409 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3410 {
3411 int start = IT_CHARPOS (*it);
3412 int limit = find_next_newline_no_quit (start, 1);
3413 Lisp_Object pos;
3414
3415 xassert (!STRINGP (it->string));
3416
3417 /* If there isn't any `display' property in sight, and no
3418 overlays, we can just use the position of the newline in
3419 buffer text. */
3420 if (it->stop_charpos >= limit
3421 || ((pos = Fnext_single_property_change (make_number (start),
3422 Qdisplay,
3423 Qnil, make_number (limit)),
3424 NILP (pos))
3425 && next_overlay_change (start) == ZV))
3426 {
3427 IT_CHARPOS (*it) = limit;
3428 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3429 *skipped_p = newline_found_p = 1;
3430 }
3431 else
3432 {
3433 while (get_next_display_element (it)
3434 && !newline_found_p)
3435 {
3436 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3437 set_iterator_to_next (it, 0);
3438 }
3439 }
3440 }
3441
3442 it->selective = old_selective;
3443 return newline_found_p;
3444 }
3445
3446
3447 /* Set IT's current position to the previous visible line start. Skip
3448 invisible text that is so either due to text properties or due to
3449 selective display. Caution: this does not change IT->current_x and
3450 IT->hpos. */
3451
3452 static void
3453 back_to_previous_visible_line_start (it)
3454 struct it *it;
3455 {
3456 int visible_p = 0;
3457
3458 /* Go back one newline if not on BEGV already. */
3459 if (IT_CHARPOS (*it) > BEGV)
3460 back_to_previous_line_start (it);
3461
3462 /* Move over lines that are invisible because of selective display
3463 or text properties. */
3464 while (IT_CHARPOS (*it) > BEGV
3465 && !visible_p)
3466 {
3467 visible_p = 1;
3468
3469 /* If selective > 0, then lines indented more than that values
3470 are invisible. */
3471 if (it->selective > 0
3472 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3473 it->selective))
3474 visible_p = 0;
3475 else
3476 {
3477 Lisp_Object prop;
3478
3479 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3480 Qinvisible, it->window);
3481 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3482 visible_p = 0;
3483 }
3484
3485 /* Back one more newline if the current one is invisible. */
3486 if (!visible_p)
3487 back_to_previous_line_start (it);
3488 }
3489
3490 xassert (IT_CHARPOS (*it) >= BEGV);
3491 xassert (IT_CHARPOS (*it) == BEGV
3492 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3493 CHECK_IT (it);
3494 }
3495
3496
3497 /* Reseat iterator IT at the previous visible line start. Skip
3498 invisible text that is so either due to text properties or due to
3499 selective display. At the end, update IT's overlay information,
3500 face information etc. */
3501
3502 static void
3503 reseat_at_previous_visible_line_start (it)
3504 struct it *it;
3505 {
3506 back_to_previous_visible_line_start (it);
3507 reseat (it, it->current.pos, 1);
3508 CHECK_IT (it);
3509 }
3510
3511
3512 /* Reseat iterator IT on the next visible line start in the current
3513 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3514 preceding the line start. Skip over invisible text that is so
3515 because of selective display. Compute faces, overlays etc at the
3516 new position. Note that this function does not skip over text that
3517 is invisible because of text properties. */
3518
3519 static void
3520 reseat_at_next_visible_line_start (it, on_newline_p)
3521 struct it *it;
3522 int on_newline_p;
3523 {
3524 int newline_found_p, skipped_p = 0;
3525
3526 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3527
3528 /* Skip over lines that are invisible because they are indented
3529 more than the value of IT->selective. */
3530 if (it->selective > 0)
3531 while (IT_CHARPOS (*it) < ZV
3532 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3533 it->selective))
3534 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3535
3536 /* Position on the newline if that's what's requested. */
3537 if (on_newline_p && newline_found_p)
3538 {
3539 if (STRINGP (it->string))
3540 {
3541 if (IT_STRING_CHARPOS (*it) > 0)
3542 {
3543 --IT_STRING_CHARPOS (*it);
3544 --IT_STRING_BYTEPOS (*it);
3545 }
3546 }
3547 else if (IT_CHARPOS (*it) > BEGV)
3548 {
3549 --IT_CHARPOS (*it);
3550 --IT_BYTEPOS (*it);
3551 reseat (it, it->current.pos, 0);
3552 }
3553 }
3554 else if (skipped_p)
3555 reseat (it, it->current.pos, 0);
3556
3557 CHECK_IT (it);
3558 }
3559
3560
3561 \f
3562 /***********************************************************************
3563 Changing an iterator's position
3564 ***********************************************************************/
3565
3566 /* Change IT's current position to POS in current_buffer. If FORCE_P
3567 is non-zero, always check for text properties at the new position.
3568 Otherwise, text properties are only looked up if POS >=
3569 IT->check_charpos of a property. */
3570
3571 static void
3572 reseat (it, pos, force_p)
3573 struct it *it;
3574 struct text_pos pos;
3575 int force_p;
3576 {
3577 int original_pos = IT_CHARPOS (*it);
3578
3579 reseat_1 (it, pos, 0);
3580
3581 /* Determine where to check text properties. Avoid doing it
3582 where possible because text property lookup is very expensive. */
3583 if (force_p
3584 || CHARPOS (pos) > it->stop_charpos
3585 || CHARPOS (pos) < original_pos)
3586 handle_stop (it);
3587
3588 CHECK_IT (it);
3589 }
3590
3591
3592 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3593 IT->stop_pos to POS, also. */
3594
3595 static void
3596 reseat_1 (it, pos, set_stop_p)
3597 struct it *it;
3598 struct text_pos pos;
3599 int set_stop_p;
3600 {
3601 /* Don't call this function when scanning a C string. */
3602 xassert (it->s == NULL);
3603
3604 /* POS must be a reasonable value. */
3605 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3606
3607 it->current.pos = it->position = pos;
3608 XSETBUFFER (it->object, current_buffer);
3609 it->dpvec = NULL;
3610 it->current.dpvec_index = -1;
3611 it->current.overlay_string_index = -1;
3612 IT_STRING_CHARPOS (*it) = -1;
3613 IT_STRING_BYTEPOS (*it) = -1;
3614 it->string = Qnil;
3615 it->method = next_element_from_buffer;
3616 it->sp = 0;
3617
3618 if (set_stop_p)
3619 it->stop_charpos = CHARPOS (pos);
3620 }
3621
3622
3623 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3624 If S is non-null, it is a C string to iterate over. Otherwise,
3625 STRING gives a Lisp string to iterate over.
3626
3627 If PRECISION > 0, don't return more then PRECISION number of
3628 characters from the string.
3629
3630 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3631 characters have been returned. FIELD_WIDTH < 0 means an infinite
3632 field width.
3633
3634 MULTIBYTE = 0 means disable processing of multibyte characters,
3635 MULTIBYTE > 0 means enable it,
3636 MULTIBYTE < 0 means use IT->multibyte_p.
3637
3638 IT must be initialized via a prior call to init_iterator before
3639 calling this function. */
3640
3641 static void
3642 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3643 struct it *it;
3644 unsigned char *s;
3645 Lisp_Object string;
3646 int charpos;
3647 int precision, field_width, multibyte;
3648 {
3649 /* No region in strings. */
3650 it->region_beg_charpos = it->region_end_charpos = -1;
3651
3652 /* No text property checks performed by default, but see below. */
3653 it->stop_charpos = -1;
3654
3655 /* Set iterator position and end position. */
3656 bzero (&it->current, sizeof it->current);
3657 it->current.overlay_string_index = -1;
3658 it->current.dpvec_index = -1;
3659 xassert (charpos >= 0);
3660
3661 /* Use the setting of MULTIBYTE if specified. */
3662 if (multibyte >= 0)
3663 it->multibyte_p = multibyte > 0;
3664
3665 if (s == NULL)
3666 {
3667 xassert (STRINGP (string));
3668 it->string = string;
3669 it->s = NULL;
3670 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3671 it->method = next_element_from_string;
3672 it->current.string_pos = string_pos (charpos, string);
3673 }
3674 else
3675 {
3676 it->s = s;
3677 it->string = Qnil;
3678
3679 /* Note that we use IT->current.pos, not it->current.string_pos,
3680 for displaying C strings. */
3681 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3682 if (it->multibyte_p)
3683 {
3684 it->current.pos = c_string_pos (charpos, s, 1);
3685 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3686 }
3687 else
3688 {
3689 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3690 it->end_charpos = it->string_nchars = strlen (s);
3691 }
3692
3693 it->method = next_element_from_c_string;
3694 }
3695
3696 /* PRECISION > 0 means don't return more than PRECISION characters
3697 from the string. */
3698 if (precision > 0 && it->end_charpos - charpos > precision)
3699 it->end_charpos = it->string_nchars = charpos + precision;
3700
3701 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3702 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3703 FIELD_WIDTH < 0 means infinite field width. This is useful for
3704 padding with `-' at the end of a mode line. */
3705 if (field_width < 0)
3706 field_width = INFINITY;
3707 if (field_width > it->end_charpos - charpos)
3708 it->end_charpos = charpos + field_width;
3709
3710 /* Use the standard display table for displaying strings. */
3711 if (DISP_TABLE_P (Vstandard_display_table))
3712 it->dp = XCHAR_TABLE (Vstandard_display_table);
3713
3714 it->stop_charpos = charpos;
3715 CHECK_IT (it);
3716 }
3717
3718
3719 \f
3720 /***********************************************************************
3721 Iteration
3722 ***********************************************************************/
3723
3724 /* Load IT's display element fields with information about the next
3725 display element from the current position of IT. Value is zero if
3726 end of buffer (or C string) is reached. */
3727
3728 int
3729 get_next_display_element (it)
3730 struct it *it;
3731 {
3732 /* Non-zero means that we found an display element. Zero means that
3733 we hit the end of what we iterate over. Performance note: the
3734 function pointer `method' used here turns out to be faster than
3735 using a sequence of if-statements. */
3736 int success_p = (*it->method) (it);
3737
3738 if (it->what == IT_CHARACTER)
3739 {
3740 /* Map via display table or translate control characters.
3741 IT->c, IT->len etc. have been set to the next character by
3742 the function call above. If we have a display table, and it
3743 contains an entry for IT->c, translate it. Don't do this if
3744 IT->c itself comes from a display table, otherwise we could
3745 end up in an infinite recursion. (An alternative could be to
3746 count the recursion depth of this function and signal an
3747 error when a certain maximum depth is reached.) Is it worth
3748 it? */
3749 if (success_p && it->dpvec == NULL)
3750 {
3751 Lisp_Object dv;
3752
3753 if (it->dp
3754 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3755 VECTORP (dv)))
3756 {
3757 struct Lisp_Vector *v = XVECTOR (dv);
3758
3759 /* Return the first character from the display table
3760 entry, if not empty. If empty, don't display the
3761 current character. */
3762 if (v->size)
3763 {
3764 it->dpvec_char_len = it->len;
3765 it->dpvec = v->contents;
3766 it->dpend = v->contents + v->size;
3767 it->current.dpvec_index = 0;
3768 it->method = next_element_from_display_vector;
3769 }
3770
3771 success_p = get_next_display_element (it);
3772 }
3773
3774 /* Translate control characters into `\003' or `^C' form.
3775 Control characters coming from a display table entry are
3776 currently not translated because we use IT->dpvec to hold
3777 the translation. This could easily be changed but I
3778 don't believe that it is worth doing.
3779
3780 Non-printable multibyte characters are also translated
3781 octal form. */
3782 else if ((it->c < ' '
3783 && (it->area != TEXT_AREA
3784 || (it->c != '\n' && it->c != '\t')))
3785 || (it->c >= 127
3786 && it->len == 1)
3787 || !CHAR_PRINTABLE_P (it->c))
3788 {
3789 /* IT->c is a control character which must be displayed
3790 either as '\003' or as `^C' where the '\\' and '^'
3791 can be defined in the display table. Fill
3792 IT->ctl_chars with glyphs for what we have to
3793 display. Then, set IT->dpvec to these glyphs. */
3794 GLYPH g;
3795
3796 if (it->c < 128 && it->ctl_arrow_p)
3797 {
3798 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3799 if (it->dp
3800 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3801 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3802 g = XINT (DISP_CTRL_GLYPH (it->dp));
3803 else
3804 g = FAST_MAKE_GLYPH ('^', 0);
3805 XSETINT (it->ctl_chars[0], g);
3806
3807 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3808 XSETINT (it->ctl_chars[1], g);
3809
3810 /* Set up IT->dpvec and return first character from it. */
3811 it->dpvec_char_len = it->len;
3812 it->dpvec = it->ctl_chars;
3813 it->dpend = it->dpvec + 2;
3814 it->current.dpvec_index = 0;
3815 it->method = next_element_from_display_vector;
3816 get_next_display_element (it);
3817 }
3818 else
3819 {
3820 unsigned char str[MAX_MULTIBYTE_LENGTH];
3821 int len;
3822 int i;
3823 GLYPH escape_glyph;
3824
3825 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3826 if (it->dp
3827 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3828 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3829 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3830 else
3831 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3832
3833 if (SINGLE_BYTE_CHAR_P (it->c))
3834 str[0] = it->c, len = 1;
3835 else
3836 len = CHAR_STRING (it->c, str);
3837
3838 for (i = 0; i < len; i++)
3839 {
3840 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3841 /* Insert three more glyphs into IT->ctl_chars for
3842 the octal display of the character. */
3843 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3844 XSETINT (it->ctl_chars[i * 4 + 1], g);
3845 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3846 XSETINT (it->ctl_chars[i * 4 + 2], g);
3847 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3848 XSETINT (it->ctl_chars[i * 4 + 3], g);
3849 }
3850
3851 /* Set up IT->dpvec and return the first character
3852 from it. */
3853 it->dpvec_char_len = it->len;
3854 it->dpvec = it->ctl_chars;
3855 it->dpend = it->dpvec + len * 4;
3856 it->current.dpvec_index = 0;
3857 it->method = next_element_from_display_vector;
3858 get_next_display_element (it);
3859 }
3860 }
3861 }
3862
3863 /* Adjust face id for a multibyte character. There are no
3864 multibyte character in unibyte text. */
3865 if (it->multibyte_p
3866 && success_p
3867 && FRAME_WINDOW_P (it->f))
3868 {
3869 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3870 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3871 }
3872 }
3873
3874 /* Is this character the last one of a run of characters with
3875 box? If yes, set IT->end_of_box_run_p to 1. */
3876 if (it->face_box_p
3877 && it->s == NULL)
3878 {
3879 int face_id;
3880 struct face *face;
3881
3882 it->end_of_box_run_p
3883 = ((face_id = face_after_it_pos (it),
3884 face_id != it->face_id)
3885 && (face = FACE_FROM_ID (it->f, face_id),
3886 face->box == FACE_NO_BOX));
3887 }
3888
3889 /* Value is 0 if end of buffer or string reached. */
3890 return success_p;
3891 }
3892
3893
3894 /* Move IT to the next display element.
3895
3896 RESEAT_P non-zero means if called on a newline in buffer text,
3897 skip to the next visible line start.
3898
3899 Functions get_next_display_element and set_iterator_to_next are
3900 separate because I find this arrangement easier to handle than a
3901 get_next_display_element function that also increments IT's
3902 position. The way it is we can first look at an iterator's current
3903 display element, decide whether it fits on a line, and if it does,
3904 increment the iterator position. The other way around we probably
3905 would either need a flag indicating whether the iterator has to be
3906 incremented the next time, or we would have to implement a
3907 decrement position function which would not be easy to write. */
3908
3909 void
3910 set_iterator_to_next (it, reseat_p)
3911 struct it *it;
3912 int reseat_p;
3913 {
3914 /* Reset flags indicating start and end of a sequence of characters
3915 with box. Reset them at the start of this function because
3916 moving the iterator to a new position might set them. */
3917 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3918
3919 if (it->method == next_element_from_buffer)
3920 {
3921 /* The current display element of IT is a character from
3922 current_buffer. Advance in the buffer, and maybe skip over
3923 invisible lines that are so because of selective display. */
3924 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3925 reseat_at_next_visible_line_start (it, 0);
3926 else
3927 {
3928 xassert (it->len != 0);
3929 IT_BYTEPOS (*it) += it->len;
3930 IT_CHARPOS (*it) += 1;
3931 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3932 }
3933 }
3934 else if (it->method == next_element_from_composition)
3935 {
3936 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3937 if (STRINGP (it->string))
3938 {
3939 IT_STRING_BYTEPOS (*it) += it->len;
3940 IT_STRING_CHARPOS (*it) += it->cmp_len;
3941 it->method = next_element_from_string;
3942 goto consider_string_end;
3943 }
3944 else
3945 {
3946 IT_BYTEPOS (*it) += it->len;
3947 IT_CHARPOS (*it) += it->cmp_len;
3948 it->method = next_element_from_buffer;
3949 }
3950 }
3951 else if (it->method == next_element_from_c_string)
3952 {
3953 /* Current display element of IT is from a C string. */
3954 IT_BYTEPOS (*it) += it->len;
3955 IT_CHARPOS (*it) += 1;
3956 }
3957 else if (it->method == next_element_from_display_vector)
3958 {
3959 /* Current display element of IT is from a display table entry.
3960 Advance in the display table definition. Reset it to null if
3961 end reached, and continue with characters from buffers/
3962 strings. */
3963 ++it->current.dpvec_index;
3964
3965 /* Restore face of the iterator to what they were before the
3966 display vector entry (these entries may contain faces). */
3967 it->face_id = it->saved_face_id;
3968
3969 if (it->dpvec + it->current.dpvec_index == it->dpend)
3970 {
3971 if (it->s)
3972 it->method = next_element_from_c_string;
3973 else if (STRINGP (it->string))
3974 it->method = next_element_from_string;
3975 else
3976 it->method = next_element_from_buffer;
3977
3978 it->dpvec = NULL;
3979 it->current.dpvec_index = -1;
3980
3981 /* Skip over characters which were displayed via IT->dpvec. */
3982 if (it->dpvec_char_len < 0)
3983 reseat_at_next_visible_line_start (it, 1);
3984 else if (it->dpvec_char_len > 0)
3985 {
3986 it->len = it->dpvec_char_len;
3987 set_iterator_to_next (it, reseat_p);
3988 }
3989 }
3990 }
3991 else if (it->method == next_element_from_string)
3992 {
3993 /* Current display element is a character from a Lisp string. */
3994 xassert (it->s == NULL && STRINGP (it->string));
3995 IT_STRING_BYTEPOS (*it) += it->len;
3996 IT_STRING_CHARPOS (*it) += 1;
3997
3998 consider_string_end:
3999
4000 if (it->current.overlay_string_index >= 0)
4001 {
4002 /* IT->string is an overlay string. Advance to the
4003 next, if there is one. */
4004 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4005 next_overlay_string (it);
4006 }
4007 else
4008 {
4009 /* IT->string is not an overlay string. If we reached
4010 its end, and there is something on IT->stack, proceed
4011 with what is on the stack. This can be either another
4012 string, this time an overlay string, or a buffer. */
4013 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4014 && it->sp > 0)
4015 {
4016 pop_it (it);
4017 if (!STRINGP (it->string))
4018 it->method = next_element_from_buffer;
4019 }
4020 }
4021 }
4022 else if (it->method == next_element_from_image
4023 || it->method == next_element_from_stretch)
4024 {
4025 /* The position etc with which we have to proceed are on
4026 the stack. The position may be at the end of a string,
4027 if the `display' property takes up the whole string. */
4028 pop_it (it);
4029 it->image_id = 0;
4030 if (STRINGP (it->string))
4031 {
4032 it->method = next_element_from_string;
4033 goto consider_string_end;
4034 }
4035 else
4036 it->method = next_element_from_buffer;
4037 }
4038 else
4039 /* There are no other methods defined, so this should be a bug. */
4040 abort ();
4041
4042 xassert (it->method != next_element_from_string
4043 || (STRINGP (it->string)
4044 && IT_STRING_CHARPOS (*it) >= 0));
4045 }
4046
4047
4048 /* Load IT's display element fields with information about the next
4049 display element which comes from a display table entry or from the
4050 result of translating a control character to one of the forms `^C'
4051 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4052
4053 static int
4054 next_element_from_display_vector (it)
4055 struct it *it;
4056 {
4057 /* Precondition. */
4058 xassert (it->dpvec && it->current.dpvec_index >= 0);
4059
4060 /* Remember the current face id in case glyphs specify faces.
4061 IT's face is restored in set_iterator_to_next. */
4062 it->saved_face_id = it->face_id;
4063
4064 if (INTEGERP (*it->dpvec)
4065 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4066 {
4067 int lface_id;
4068 GLYPH g;
4069
4070 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4071 it->c = FAST_GLYPH_CHAR (g);
4072 it->len = CHAR_BYTES (it->c);
4073
4074 /* The entry may contain a face id to use. Such a face id is
4075 the id of a Lisp face, not a realized face. A face id of
4076 zero means no face is specified. */
4077 lface_id = FAST_GLYPH_FACE (g);
4078 if (lface_id)
4079 {
4080 /* The function returns -1 if lface_id is invalid. */
4081 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4082 if (face_id >= 0)
4083 it->face_id = face_id;
4084 }
4085 }
4086 else
4087 /* Display table entry is invalid. Return a space. */
4088 it->c = ' ', it->len = 1;
4089
4090 /* Don't change position and object of the iterator here. They are
4091 still the values of the character that had this display table
4092 entry or was translated, and that's what we want. */
4093 it->what = IT_CHARACTER;
4094 return 1;
4095 }
4096
4097
4098 /* Load IT with the next display element from Lisp string IT->string.
4099 IT->current.string_pos is the current position within the string.
4100 If IT->current.overlay_string_index >= 0, the Lisp string is an
4101 overlay string. */
4102
4103 static int
4104 next_element_from_string (it)
4105 struct it *it;
4106 {
4107 struct text_pos position;
4108
4109 xassert (STRINGP (it->string));
4110 xassert (IT_STRING_CHARPOS (*it) >= 0);
4111 position = it->current.string_pos;
4112
4113 /* Time to check for invisible text? */
4114 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4115 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4116 {
4117 handle_stop (it);
4118
4119 /* Since a handler may have changed IT->method, we must
4120 recurse here. */
4121 return get_next_display_element (it);
4122 }
4123
4124 if (it->current.overlay_string_index >= 0)
4125 {
4126 /* Get the next character from an overlay string. In overlay
4127 strings, There is no field width or padding with spaces to
4128 do. */
4129 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4130 {
4131 it->what = IT_EOB;
4132 return 0;
4133 }
4134 else if (STRING_MULTIBYTE (it->string))
4135 {
4136 int remaining = (STRING_BYTES (XSTRING (it->string))
4137 - IT_STRING_BYTEPOS (*it));
4138 unsigned char *s = (XSTRING (it->string)->data
4139 + IT_STRING_BYTEPOS (*it));
4140 it->c = string_char_and_length (s, remaining, &it->len);
4141 }
4142 else
4143 {
4144 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4145 it->len = 1;
4146 }
4147 }
4148 else
4149 {
4150 /* Get the next character from a Lisp string that is not an
4151 overlay string. Such strings come from the mode line, for
4152 example. We may have to pad with spaces, or truncate the
4153 string. See also next_element_from_c_string. */
4154 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4155 {
4156 it->what = IT_EOB;
4157 return 0;
4158 }
4159 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4160 {
4161 /* Pad with spaces. */
4162 it->c = ' ', it->len = 1;
4163 CHARPOS (position) = BYTEPOS (position) = -1;
4164 }
4165 else if (STRING_MULTIBYTE (it->string))
4166 {
4167 int maxlen = (STRING_BYTES (XSTRING (it->string))
4168 - IT_STRING_BYTEPOS (*it));
4169 unsigned char *s = (XSTRING (it->string)->data
4170 + IT_STRING_BYTEPOS (*it));
4171 it->c = string_char_and_length (s, maxlen, &it->len);
4172 }
4173 else
4174 {
4175 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4176 it->len = 1;
4177 }
4178 }
4179
4180 /* Record what we have and where it came from. Note that we store a
4181 buffer position in IT->position although it could arguably be a
4182 string position. */
4183 it->what = IT_CHARACTER;
4184 it->object = it->string;
4185 it->position = position;
4186 return 1;
4187 }
4188
4189
4190 /* Load IT with next display element from C string IT->s.
4191 IT->string_nchars is the maximum number of characters to return
4192 from the string. IT->end_charpos may be greater than
4193 IT->string_nchars when this function is called, in which case we
4194 may have to return padding spaces. Value is zero if end of string
4195 reached, including padding spaces. */
4196
4197 static int
4198 next_element_from_c_string (it)
4199 struct it *it;
4200 {
4201 int success_p = 1;
4202
4203 xassert (it->s);
4204 it->what = IT_CHARACTER;
4205 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4206 it->object = Qnil;
4207
4208 /* IT's position can be greater IT->string_nchars in case a field
4209 width or precision has been specified when the iterator was
4210 initialized. */
4211 if (IT_CHARPOS (*it) >= it->end_charpos)
4212 {
4213 /* End of the game. */
4214 it->what = IT_EOB;
4215 success_p = 0;
4216 }
4217 else if (IT_CHARPOS (*it) >= it->string_nchars)
4218 {
4219 /* Pad with spaces. */
4220 it->c = ' ', it->len = 1;
4221 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4222 }
4223 else if (it->multibyte_p)
4224 {
4225 /* Implementation note: The calls to strlen apparently aren't a
4226 performance problem because there is no noticeable performance
4227 difference between Emacs running in unibyte or multibyte mode. */
4228 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4229 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4230 maxlen, &it->len);
4231 }
4232 else
4233 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4234
4235 return success_p;
4236 }
4237
4238
4239 /* Set up IT to return characters from an ellipsis, if appropriate.
4240 The definition of the ellipsis glyphs may come from a display table
4241 entry. This function Fills IT with the first glyph from the
4242 ellipsis if an ellipsis is to be displayed. */
4243
4244 static int
4245 next_element_from_ellipsis (it)
4246 struct it *it;
4247 {
4248 if (it->selective_display_ellipsis_p)
4249 {
4250 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4251 {
4252 /* Use the display table definition for `...'. Invalid glyphs
4253 will be handled by the method returning elements from dpvec. */
4254 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4255 it->dpvec_char_len = it->len;
4256 it->dpvec = v->contents;
4257 it->dpend = v->contents + v->size;
4258 it->current.dpvec_index = 0;
4259 it->method = next_element_from_display_vector;
4260 }
4261 else
4262 {
4263 /* Use default `...' which is stored in default_invis_vector. */
4264 it->dpvec_char_len = it->len;
4265 it->dpvec = default_invis_vector;
4266 it->dpend = default_invis_vector + 3;
4267 it->current.dpvec_index = 0;
4268 it->method = next_element_from_display_vector;
4269 }
4270 }
4271 else
4272 {
4273 it->method = next_element_from_buffer;
4274 reseat_at_next_visible_line_start (it, 1);
4275 }
4276
4277 return get_next_display_element (it);
4278 }
4279
4280
4281 /* Deliver an image display element. The iterator IT is already
4282 filled with image information (done in handle_display_prop). Value
4283 is always 1. */
4284
4285
4286 static int
4287 next_element_from_image (it)
4288 struct it *it;
4289 {
4290 it->what = IT_IMAGE;
4291 return 1;
4292 }
4293
4294
4295 /* Fill iterator IT with next display element from a stretch glyph
4296 property. IT->object is the value of the text property. Value is
4297 always 1. */
4298
4299 static int
4300 next_element_from_stretch (it)
4301 struct it *it;
4302 {
4303 it->what = IT_STRETCH;
4304 return 1;
4305 }
4306
4307
4308 /* Load IT with the next display element from current_buffer. Value
4309 is zero if end of buffer reached. IT->stop_charpos is the next
4310 position at which to stop and check for text properties or buffer
4311 end. */
4312
4313 static int
4314 next_element_from_buffer (it)
4315 struct it *it;
4316 {
4317 int success_p = 1;
4318
4319 /* Check this assumption, otherwise, we would never enter the
4320 if-statement, below. */
4321 xassert (IT_CHARPOS (*it) >= BEGV
4322 && IT_CHARPOS (*it) <= it->stop_charpos);
4323
4324 if (IT_CHARPOS (*it) >= it->stop_charpos)
4325 {
4326 if (IT_CHARPOS (*it) >= it->end_charpos)
4327 {
4328 int overlay_strings_follow_p;
4329
4330 /* End of the game, except when overlay strings follow that
4331 haven't been returned yet. */
4332 if (it->overlay_strings_at_end_processed_p)
4333 overlay_strings_follow_p = 0;
4334 else
4335 {
4336 it->overlay_strings_at_end_processed_p = 1;
4337 overlay_strings_follow_p = get_overlay_strings (it);
4338 }
4339
4340 if (overlay_strings_follow_p)
4341 success_p = get_next_display_element (it);
4342 else
4343 {
4344 it->what = IT_EOB;
4345 it->position = it->current.pos;
4346 success_p = 0;
4347 }
4348 }
4349 else
4350 {
4351 handle_stop (it);
4352 return get_next_display_element (it);
4353 }
4354 }
4355 else
4356 {
4357 /* No face changes, overlays etc. in sight, so just return a
4358 character from current_buffer. */
4359 unsigned char *p;
4360
4361 /* Maybe run the redisplay end trigger hook. Performance note:
4362 This doesn't seem to cost measurable time. */
4363 if (it->redisplay_end_trigger_charpos
4364 && it->glyph_row
4365 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4366 run_redisplay_end_trigger_hook (it);
4367
4368 /* Get the next character, maybe multibyte. */
4369 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4370 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4371 {
4372 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4373 - IT_BYTEPOS (*it));
4374 it->c = string_char_and_length (p, maxlen, &it->len);
4375 }
4376 else
4377 it->c = *p, it->len = 1;
4378
4379 /* Record what we have and where it came from. */
4380 it->what = IT_CHARACTER;;
4381 it->object = it->w->buffer;
4382 it->position = it->current.pos;
4383
4384 /* Normally we return the character found above, except when we
4385 really want to return an ellipsis for selective display. */
4386 if (it->selective)
4387 {
4388 if (it->c == '\n')
4389 {
4390 /* A value of selective > 0 means hide lines indented more
4391 than that number of columns. */
4392 if (it->selective > 0
4393 && IT_CHARPOS (*it) + 1 < ZV
4394 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4395 IT_BYTEPOS (*it) + 1,
4396 it->selective))
4397 {
4398 success_p = next_element_from_ellipsis (it);
4399 it->dpvec_char_len = -1;
4400 }
4401 }
4402 else if (it->c == '\r' && it->selective == -1)
4403 {
4404 /* A value of selective == -1 means that everything from the
4405 CR to the end of the line is invisible, with maybe an
4406 ellipsis displayed for it. */
4407 success_p = next_element_from_ellipsis (it);
4408 it->dpvec_char_len = -1;
4409 }
4410 }
4411 }
4412
4413 /* Value is zero if end of buffer reached. */
4414 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4415 return success_p;
4416 }
4417
4418
4419 /* Run the redisplay end trigger hook for IT. */
4420
4421 static void
4422 run_redisplay_end_trigger_hook (it)
4423 struct it *it;
4424 {
4425 Lisp_Object args[3];
4426
4427 /* IT->glyph_row should be non-null, i.e. we should be actually
4428 displaying something, or otherwise we should not run the hook. */
4429 xassert (it->glyph_row);
4430
4431 /* Set up hook arguments. */
4432 args[0] = Qredisplay_end_trigger_functions;
4433 args[1] = it->window;
4434 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4435 it->redisplay_end_trigger_charpos = 0;
4436
4437 /* Since we are *trying* to run these functions, don't try to run
4438 them again, even if they get an error. */
4439 it->w->redisplay_end_trigger = Qnil;
4440 Frun_hook_with_args (3, args);
4441
4442 /* Notice if it changed the face of the character we are on. */
4443 handle_face_prop (it);
4444 }
4445
4446
4447 /* Deliver a composition display element. The iterator IT is already
4448 filled with composition information (done in
4449 handle_composition_prop). Value is always 1. */
4450
4451 static int
4452 next_element_from_composition (it)
4453 struct it *it;
4454 {
4455 it->what = IT_COMPOSITION;
4456 it->position = (STRINGP (it->string)
4457 ? it->current.string_pos
4458 : it->current.pos);
4459 return 1;
4460 }
4461
4462
4463 \f
4464 /***********************************************************************
4465 Moving an iterator without producing glyphs
4466 ***********************************************************************/
4467
4468 /* Move iterator IT to a specified buffer or X position within one
4469 line on the display without producing glyphs.
4470
4471 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4472 whichever is reached first.
4473
4474 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4475
4476 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4477 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4478 TO_X includes the amount by which a window is horizontally
4479 scrolled.
4480
4481 Value is
4482
4483 MOVE_POS_MATCH_OR_ZV
4484 - when TO_POS or ZV was reached.
4485
4486 MOVE_X_REACHED
4487 -when TO_X was reached before TO_POS or ZV were reached.
4488
4489 MOVE_LINE_CONTINUED
4490 - when we reached the end of the display area and the line must
4491 be continued.
4492
4493 MOVE_LINE_TRUNCATED
4494 - when we reached the end of the display area and the line is
4495 truncated.
4496
4497 MOVE_NEWLINE_OR_CR
4498 - when we stopped at a line end, i.e. a newline or a CR and selective
4499 display is on. */
4500
4501 static enum move_it_result
4502 move_it_in_display_line_to (it, to_charpos, to_x, op)
4503 struct it *it;
4504 int to_charpos, to_x, op;
4505 {
4506 enum move_it_result result = MOVE_UNDEFINED;
4507 struct glyph_row *saved_glyph_row;
4508
4509 /* Don't produce glyphs in produce_glyphs. */
4510 saved_glyph_row = it->glyph_row;
4511 it->glyph_row = NULL;
4512
4513 while (1)
4514 {
4515 int x, i, ascent = 0, descent = 0;
4516
4517 /* Stop when ZV or TO_CHARPOS reached. */
4518 if (!get_next_display_element (it)
4519 || ((op & MOVE_TO_POS) != 0
4520 && BUFFERP (it->object)
4521 && IT_CHARPOS (*it) >= to_charpos))
4522 {
4523 result = MOVE_POS_MATCH_OR_ZV;
4524 break;
4525 }
4526
4527 /* The call to produce_glyphs will get the metrics of the
4528 display element IT is loaded with. We record in x the
4529 x-position before this display element in case it does not
4530 fit on the line. */
4531 x = it->current_x;
4532
4533 /* Remember the line height so far in case the next element doesn't
4534 fit on the line. */
4535 if (!it->truncate_lines_p)
4536 {
4537 ascent = it->max_ascent;
4538 descent = it->max_descent;
4539 }
4540
4541 PRODUCE_GLYPHS (it);
4542
4543 if (it->area != TEXT_AREA)
4544 {
4545 set_iterator_to_next (it, 1);
4546 continue;
4547 }
4548
4549 /* The number of glyphs we get back in IT->nglyphs will normally
4550 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4551 character on a terminal frame, or (iii) a line end. For the
4552 second case, IT->nglyphs - 1 padding glyphs will be present
4553 (on X frames, there is only one glyph produced for a
4554 composite character.
4555
4556 The behavior implemented below means, for continuation lines,
4557 that as many spaces of a TAB as fit on the current line are
4558 displayed there. For terminal frames, as many glyphs of a
4559 multi-glyph character are displayed in the current line, too.
4560 This is what the old redisplay code did, and we keep it that
4561 way. Under X, the whole shape of a complex character must
4562 fit on the line or it will be completely displayed in the
4563 next line.
4564
4565 Note that both for tabs and padding glyphs, all glyphs have
4566 the same width. */
4567 if (it->nglyphs)
4568 {
4569 /* More than one glyph or glyph doesn't fit on line. All
4570 glyphs have the same width. */
4571 int single_glyph_width = it->pixel_width / it->nglyphs;
4572 int new_x;
4573
4574 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4575 {
4576 new_x = x + single_glyph_width;
4577
4578 /* We want to leave anything reaching TO_X to the caller. */
4579 if ((op & MOVE_TO_X) && new_x > to_x)
4580 {
4581 it->current_x = x;
4582 result = MOVE_X_REACHED;
4583 break;
4584 }
4585 else if (/* Lines are continued. */
4586 !it->truncate_lines_p
4587 && (/* And glyph doesn't fit on the line. */
4588 new_x > it->last_visible_x
4589 /* Or it fits exactly and we're on a window
4590 system frame. */
4591 || (new_x == it->last_visible_x
4592 && FRAME_WINDOW_P (it->f))))
4593 {
4594 if (/* IT->hpos == 0 means the very first glyph
4595 doesn't fit on the line, e.g. a wide image. */
4596 it->hpos == 0
4597 || (new_x == it->last_visible_x
4598 && FRAME_WINDOW_P (it->f)))
4599 {
4600 ++it->hpos;
4601 it->current_x = new_x;
4602 if (i == it->nglyphs - 1)
4603 set_iterator_to_next (it, 1);
4604 }
4605 else
4606 {
4607 it->current_x = x;
4608 it->max_ascent = ascent;
4609 it->max_descent = descent;
4610 }
4611
4612 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4613 IT_CHARPOS (*it)));
4614 result = MOVE_LINE_CONTINUED;
4615 break;
4616 }
4617 else if (new_x > it->first_visible_x)
4618 {
4619 /* Glyph is visible. Increment number of glyphs that
4620 would be displayed. */
4621 ++it->hpos;
4622 }
4623 else
4624 {
4625 /* Glyph is completely off the left margin of the display
4626 area. Nothing to do. */
4627 }
4628 }
4629
4630 if (result != MOVE_UNDEFINED)
4631 break;
4632 }
4633 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4634 {
4635 /* Stop when TO_X specified and reached. This check is
4636 necessary here because of lines consisting of a line end,
4637 only. The line end will not produce any glyphs and we
4638 would never get MOVE_X_REACHED. */
4639 xassert (it->nglyphs == 0);
4640 result = MOVE_X_REACHED;
4641 break;
4642 }
4643
4644 /* Is this a line end? If yes, we're done. */
4645 if (ITERATOR_AT_END_OF_LINE_P (it))
4646 {
4647 result = MOVE_NEWLINE_OR_CR;
4648 break;
4649 }
4650
4651 /* The current display element has been consumed. Advance
4652 to the next. */
4653 set_iterator_to_next (it, 1);
4654
4655 /* Stop if lines are truncated and IT's current x-position is
4656 past the right edge of the window now. */
4657 if (it->truncate_lines_p
4658 && it->current_x >= it->last_visible_x)
4659 {
4660 result = MOVE_LINE_TRUNCATED;
4661 break;
4662 }
4663 }
4664
4665 /* Restore the iterator settings altered at the beginning of this
4666 function. */
4667 it->glyph_row = saved_glyph_row;
4668 return result;
4669 }
4670
4671
4672 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4673 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4674 the description of enum move_operation_enum.
4675
4676 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4677 screen line, this function will set IT to the next position >
4678 TO_CHARPOS. */
4679
4680 void
4681 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4682 struct it *it;
4683 int to_charpos, to_x, to_y, to_vpos;
4684 int op;
4685 {
4686 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4687 int line_height;
4688 int reached = 0;
4689
4690 for (;;)
4691 {
4692 if (op & MOVE_TO_VPOS)
4693 {
4694 /* If no TO_CHARPOS and no TO_X specified, stop at the
4695 start of the line TO_VPOS. */
4696 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4697 {
4698 if (it->vpos == to_vpos)
4699 {
4700 reached = 1;
4701 break;
4702 }
4703 else
4704 skip = move_it_in_display_line_to (it, -1, -1, 0);
4705 }
4706 else
4707 {
4708 /* TO_VPOS >= 0 means stop at TO_X in the line at
4709 TO_VPOS, or at TO_POS, whichever comes first. */
4710 if (it->vpos == to_vpos)
4711 {
4712 reached = 2;
4713 break;
4714 }
4715
4716 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4717
4718 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4719 {
4720 reached = 3;
4721 break;
4722 }
4723 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4724 {
4725 /* We have reached TO_X but not in the line we want. */
4726 skip = move_it_in_display_line_to (it, to_charpos,
4727 -1, MOVE_TO_POS);
4728 if (skip == MOVE_POS_MATCH_OR_ZV)
4729 {
4730 reached = 4;
4731 break;
4732 }
4733 }
4734 }
4735 }
4736 else if (op & MOVE_TO_Y)
4737 {
4738 struct it it_backup;
4739
4740 /* TO_Y specified means stop at TO_X in the line containing
4741 TO_Y---or at TO_CHARPOS if this is reached first. The
4742 problem is that we can't really tell whether the line
4743 contains TO_Y before we have completely scanned it, and
4744 this may skip past TO_X. What we do is to first scan to
4745 TO_X.
4746
4747 If TO_X is not specified, use a TO_X of zero. The reason
4748 is to make the outcome of this function more predictable.
4749 If we didn't use TO_X == 0, we would stop at the end of
4750 the line which is probably not what a caller would expect
4751 to happen. */
4752 skip = move_it_in_display_line_to (it, to_charpos,
4753 ((op & MOVE_TO_X)
4754 ? to_x : 0),
4755 (MOVE_TO_X
4756 | (op & MOVE_TO_POS)));
4757
4758 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4759 if (skip == MOVE_POS_MATCH_OR_ZV)
4760 {
4761 reached = 5;
4762 break;
4763 }
4764
4765 /* If TO_X was reached, we would like to know whether TO_Y
4766 is in the line. This can only be said if we know the
4767 total line height which requires us to scan the rest of
4768 the line. */
4769 if (skip == MOVE_X_REACHED)
4770 {
4771 it_backup = *it;
4772 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4773 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4774 op & MOVE_TO_POS);
4775 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4776 }
4777
4778 /* Now, decide whether TO_Y is in this line. */
4779 line_height = it->max_ascent + it->max_descent;
4780 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4781
4782 if (to_y >= it->current_y
4783 && to_y < it->current_y + line_height)
4784 {
4785 if (skip == MOVE_X_REACHED)
4786 /* If TO_Y is in this line and TO_X was reached above,
4787 we scanned too far. We have to restore IT's settings
4788 to the ones before skipping. */
4789 *it = it_backup;
4790 reached = 6;
4791 }
4792 else if (skip == MOVE_X_REACHED)
4793 {
4794 skip = skip2;
4795 if (skip == MOVE_POS_MATCH_OR_ZV)
4796 reached = 7;
4797 }
4798
4799 if (reached)
4800 break;
4801 }
4802 else
4803 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4804
4805 switch (skip)
4806 {
4807 case MOVE_POS_MATCH_OR_ZV:
4808 reached = 8;
4809 goto out;
4810
4811 case MOVE_NEWLINE_OR_CR:
4812 set_iterator_to_next (it, 1);
4813 it->continuation_lines_width = 0;
4814 break;
4815
4816 case MOVE_LINE_TRUNCATED:
4817 it->continuation_lines_width = 0;
4818 reseat_at_next_visible_line_start (it, 0);
4819 if ((op & MOVE_TO_POS) != 0
4820 && IT_CHARPOS (*it) > to_charpos)
4821 {
4822 reached = 9;
4823 goto out;
4824 }
4825 break;
4826
4827 case MOVE_LINE_CONTINUED:
4828 it->continuation_lines_width += it->current_x;
4829 break;
4830
4831 default:
4832 abort ();
4833 }
4834
4835 /* Reset/increment for the next run. */
4836 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4837 it->current_x = it->hpos = 0;
4838 it->current_y += it->max_ascent + it->max_descent;
4839 ++it->vpos;
4840 last_height = it->max_ascent + it->max_descent;
4841 last_max_ascent = it->max_ascent;
4842 it->max_ascent = it->max_descent = 0;
4843 }
4844
4845 out:
4846
4847 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4848 }
4849
4850
4851 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4852
4853 If DY > 0, move IT backward at least that many pixels. DY = 0
4854 means move IT backward to the preceding line start or BEGV. This
4855 function may move over more than DY pixels if IT->current_y - DY
4856 ends up in the middle of a line; in this case IT->current_y will be
4857 set to the top of the line moved to. */
4858
4859 void
4860 move_it_vertically_backward (it, dy)
4861 struct it *it;
4862 int dy;
4863 {
4864 int nlines, h, line_height;
4865 struct it it2;
4866 int start_pos = IT_CHARPOS (*it);
4867
4868 xassert (dy >= 0);
4869
4870 /* Estimate how many newlines we must move back. */
4871 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4872
4873 /* Set the iterator's position that many lines back. */
4874 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4875 back_to_previous_visible_line_start (it);
4876
4877 /* Reseat the iterator here. When moving backward, we don't want
4878 reseat to skip forward over invisible text, set up the iterator
4879 to deliver from overlay strings at the new position etc. So,
4880 use reseat_1 here. */
4881 reseat_1 (it, it->current.pos, 1);
4882
4883 /* We are now surely at a line start. */
4884 it->current_x = it->hpos = 0;
4885
4886 /* Move forward and see what y-distance we moved. First move to the
4887 start of the next line so that we get its height. We need this
4888 height to be able to tell whether we reached the specified
4889 y-distance. */
4890 it2 = *it;
4891 it2.max_ascent = it2.max_descent = 0;
4892 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4893 MOVE_TO_POS | MOVE_TO_VPOS);
4894 xassert (IT_CHARPOS (*it) >= BEGV);
4895 line_height = it2.max_ascent + it2.max_descent;
4896
4897 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4898 xassert (IT_CHARPOS (*it) >= BEGV);
4899 h = it2.current_y - it->current_y;
4900 nlines = it2.vpos - it->vpos;
4901
4902 /* Correct IT's y and vpos position. */
4903 it->vpos -= nlines;
4904 it->current_y -= h;
4905
4906 if (dy == 0)
4907 {
4908 /* DY == 0 means move to the start of the screen line. The
4909 value of nlines is > 0 if continuation lines were involved. */
4910 if (nlines > 0)
4911 move_it_by_lines (it, nlines, 1);
4912 xassert (IT_CHARPOS (*it) <= start_pos);
4913 }
4914 else if (nlines)
4915 {
4916 /* The y-position we try to reach. Note that h has been
4917 subtracted in front of the if-statement. */
4918 int target_y = it->current_y + h - dy;
4919
4920 /* If we did not reach target_y, try to move further backward if
4921 we can. If we moved too far backward, try to move forward. */
4922 if (target_y < it->current_y
4923 && IT_CHARPOS (*it) > BEGV)
4924 {
4925 move_it_vertically (it, target_y - it->current_y);
4926 xassert (IT_CHARPOS (*it) >= BEGV);
4927 }
4928 else if (target_y >= it->current_y + line_height
4929 && IT_CHARPOS (*it) < ZV)
4930 {
4931 move_it_vertically (it, target_y - (it->current_y + line_height));
4932 xassert (IT_CHARPOS (*it) >= BEGV);
4933 }
4934 }
4935 }
4936
4937
4938 /* Move IT by a specified amount of pixel lines DY. DY negative means
4939 move backwards. DY = 0 means move to start of screen line. At the
4940 end, IT will be on the start of a screen line. */
4941
4942 void
4943 move_it_vertically (it, dy)
4944 struct it *it;
4945 int dy;
4946 {
4947 if (dy <= 0)
4948 move_it_vertically_backward (it, -dy);
4949 else if (dy > 0)
4950 {
4951 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4952 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4953 MOVE_TO_POS | MOVE_TO_Y);
4954 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4955
4956 /* If buffer ends in ZV without a newline, move to the start of
4957 the line to satisfy the post-condition. */
4958 if (IT_CHARPOS (*it) == ZV
4959 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4960 move_it_by_lines (it, 0, 0);
4961 }
4962 }
4963
4964
4965 /* Return non-zero if some text between buffer positions START_CHARPOS
4966 and END_CHARPOS is invisible. IT->window is the window for text
4967 property lookup. */
4968
4969 static int
4970 invisible_text_between_p (it, start_charpos, end_charpos)
4971 struct it *it;
4972 int start_charpos, end_charpos;
4973 {
4974 Lisp_Object prop, limit;
4975 int invisible_found_p;
4976
4977 xassert (it != NULL && start_charpos <= end_charpos);
4978
4979 /* Is text at START invisible? */
4980 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4981 it->window);
4982 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4983 invisible_found_p = 1;
4984 else
4985 {
4986 limit = Fnext_single_char_property_change (make_number (start_charpos),
4987 Qinvisible, Qnil,
4988 make_number (end_charpos));
4989 invisible_found_p = XFASTINT (limit) < end_charpos;
4990 }
4991
4992 return invisible_found_p;
4993 }
4994
4995
4996 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4997 negative means move up. DVPOS == 0 means move to the start of the
4998 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4999 NEED_Y_P is zero, IT->current_y will be left unchanged.
5000
5001 Further optimization ideas: If we would know that IT->f doesn't use
5002 a face with proportional font, we could be faster for
5003 truncate-lines nil. */
5004
5005 void
5006 move_it_by_lines (it, dvpos, need_y_p)
5007 struct it *it;
5008 int dvpos, need_y_p;
5009 {
5010 struct position pos;
5011
5012 if (!FRAME_WINDOW_P (it->f))
5013 {
5014 struct text_pos textpos;
5015
5016 /* We can use vmotion on frames without proportional fonts. */
5017 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5018 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5019 reseat (it, textpos, 1);
5020 it->vpos += pos.vpos;
5021 it->current_y += pos.vpos;
5022 }
5023 else if (dvpos == 0)
5024 {
5025 /* DVPOS == 0 means move to the start of the screen line. */
5026 move_it_vertically_backward (it, 0);
5027 xassert (it->current_x == 0 && it->hpos == 0);
5028 }
5029 else if (dvpos > 0)
5030 {
5031 /* If there are no continuation lines, and if there is no
5032 selective display, try the simple method of moving forward
5033 DVPOS newlines, then see where we are. */
5034 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5035 {
5036 int shortage = 0, charpos;
5037
5038 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
5039 charpos = IT_CHARPOS (*it) + 1;
5040 else
5041 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5042 &shortage, 0);
5043
5044 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5045 {
5046 struct text_pos pos;
5047 CHARPOS (pos) = charpos;
5048 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5049 reseat (it, pos, 1);
5050 it->vpos += dvpos - shortage;
5051 it->hpos = it->current_x = 0;
5052 return;
5053 }
5054 }
5055
5056 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5057 }
5058 else
5059 {
5060 struct it it2;
5061 int start_charpos, i;
5062
5063 /* If there are no continuation lines, and if there is no
5064 selective display, try the simple method of moving backward
5065 -DVPOS newlines. */
5066 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5067 {
5068 int shortage;
5069 int charpos = IT_CHARPOS (*it);
5070 int bytepos = IT_BYTEPOS (*it);
5071
5072 /* If in the middle of a line, go to its start. */
5073 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5074 {
5075 charpos = find_next_newline_no_quit (charpos, -1);
5076 bytepos = CHAR_TO_BYTE (charpos);
5077 }
5078
5079 if (charpos == BEGV)
5080 {
5081 struct text_pos pos;
5082 CHARPOS (pos) = charpos;
5083 BYTEPOS (pos) = bytepos;
5084 reseat (it, pos, 1);
5085 it->hpos = it->current_x = 0;
5086 return;
5087 }
5088 else
5089 {
5090 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5091 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5092 {
5093 struct text_pos pos;
5094 CHARPOS (pos) = charpos;
5095 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5096 reseat (it, pos, 1);
5097 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5098 it->hpos = it->current_x = 0;
5099 return;
5100 }
5101 }
5102 }
5103
5104 /* Go back -DVPOS visible lines and reseat the iterator there. */
5105 start_charpos = IT_CHARPOS (*it);
5106 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5107 back_to_previous_visible_line_start (it);
5108 reseat (it, it->current.pos, 1);
5109 it->current_x = it->hpos = 0;
5110
5111 /* Above call may have moved too far if continuation lines
5112 are involved. Scan forward and see if it did. */
5113 it2 = *it;
5114 it2.vpos = it2.current_y = 0;
5115 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5116 it->vpos -= it2.vpos;
5117 it->current_y -= it2.current_y;
5118 it->current_x = it->hpos = 0;
5119
5120 /* If we moved too far, move IT some lines forward. */
5121 if (it2.vpos > -dvpos)
5122 {
5123 int delta = it2.vpos + dvpos;
5124 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5125 }
5126 }
5127 }
5128
5129
5130 \f
5131 /***********************************************************************
5132 Messages
5133 ***********************************************************************/
5134
5135
5136 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5137 to *Messages*. */
5138
5139 void
5140 add_to_log (format, arg1, arg2)
5141 char *format;
5142 Lisp_Object arg1, arg2;
5143 {
5144 Lisp_Object args[3];
5145 Lisp_Object msg, fmt;
5146 char *buffer;
5147 int len;
5148 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5149
5150 fmt = msg = Qnil;
5151 GCPRO4 (fmt, msg, arg1, arg2);
5152
5153 args[0] = fmt = build_string (format);
5154 args[1] = arg1;
5155 args[2] = arg2;
5156 msg = Fformat (3, args);
5157
5158 len = STRING_BYTES (XSTRING (msg)) + 1;
5159 buffer = (char *) alloca (len);
5160 strcpy (buffer, XSTRING (msg)->data);
5161
5162 message_dolog (buffer, len - 1, 1, 0);
5163 UNGCPRO;
5164 }
5165
5166
5167 /* Output a newline in the *Messages* buffer if "needs" one. */
5168
5169 void
5170 message_log_maybe_newline ()
5171 {
5172 if (message_log_need_newline)
5173 message_dolog ("", 0, 1, 0);
5174 }
5175
5176
5177 /* Add a string M of length LEN to the message log, optionally
5178 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5179 nonzero, means interpret the contents of M as multibyte. This
5180 function calls low-level routines in order to bypass text property
5181 hooks, etc. which might not be safe to run. */
5182
5183 void
5184 message_dolog (m, len, nlflag, multibyte)
5185 char *m;
5186 int len, nlflag, multibyte;
5187 {
5188 if (!NILP (Vmessage_log_max))
5189 {
5190 struct buffer *oldbuf;
5191 Lisp_Object oldpoint, oldbegv, oldzv;
5192 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5193 int point_at_end = 0;
5194 int zv_at_end = 0;
5195 Lisp_Object old_deactivate_mark, tem;
5196 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5197
5198 old_deactivate_mark = Vdeactivate_mark;
5199 oldbuf = current_buffer;
5200 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5201 current_buffer->undo_list = Qt;
5202
5203 oldpoint = Fpoint_marker ();
5204 oldbegv = Fpoint_min_marker ();
5205 oldzv = Fpoint_max_marker ();
5206 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5207
5208 if (PT == Z)
5209 point_at_end = 1;
5210 if (ZV == Z)
5211 zv_at_end = 1;
5212
5213 BEGV = BEG;
5214 BEGV_BYTE = BEG_BYTE;
5215 ZV = Z;
5216 ZV_BYTE = Z_BYTE;
5217 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5218
5219 /* Insert the string--maybe converting multibyte to single byte
5220 or vice versa, so that all the text fits the buffer. */
5221 if (multibyte
5222 && NILP (current_buffer->enable_multibyte_characters))
5223 {
5224 int i, c, nbytes;
5225 unsigned char work[1];
5226
5227 /* Convert a multibyte string to single-byte
5228 for the *Message* buffer. */
5229 for (i = 0; i < len; i += nbytes)
5230 {
5231 c = string_char_and_length (m + i, len - i, &nbytes);
5232 work[0] = (SINGLE_BYTE_CHAR_P (c)
5233 ? c
5234 : multibyte_char_to_unibyte (c, Qnil));
5235 insert_1_both (work, 1, 1, 1, 0, 0);
5236 }
5237 }
5238 else if (! multibyte
5239 && ! NILP (current_buffer->enable_multibyte_characters))
5240 {
5241 int i, c, nbytes;
5242 unsigned char *msg = (unsigned char *) m;
5243 unsigned char str[MAX_MULTIBYTE_LENGTH];
5244 /* Convert a single-byte string to multibyte
5245 for the *Message* buffer. */
5246 for (i = 0; i < len; i++)
5247 {
5248 c = unibyte_char_to_multibyte (msg[i]);
5249 nbytes = CHAR_STRING (c, str);
5250 insert_1_both (str, 1, nbytes, 1, 0, 0);
5251 }
5252 }
5253 else if (len)
5254 insert_1 (m, len, 1, 0, 0);
5255
5256 if (nlflag)
5257 {
5258 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5259 insert_1 ("\n", 1, 1, 0, 0);
5260
5261 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5262 this_bol = PT;
5263 this_bol_byte = PT_BYTE;
5264
5265 if (this_bol > BEG)
5266 {
5267 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5268 prev_bol = PT;
5269 prev_bol_byte = PT_BYTE;
5270
5271 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5272 this_bol, this_bol_byte);
5273 if (dup)
5274 {
5275 del_range_both (prev_bol, prev_bol_byte,
5276 this_bol, this_bol_byte, 0);
5277 if (dup > 1)
5278 {
5279 char dupstr[40];
5280 int duplen;
5281
5282 /* If you change this format, don't forget to also
5283 change message_log_check_duplicate. */
5284 sprintf (dupstr, " [%d times]", dup);
5285 duplen = strlen (dupstr);
5286 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5287 insert_1 (dupstr, duplen, 1, 0, 1);
5288 }
5289 }
5290 }
5291
5292 if (NATNUMP (Vmessage_log_max))
5293 {
5294 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5295 -XFASTINT (Vmessage_log_max) - 1, 0);
5296 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5297 }
5298 }
5299 BEGV = XMARKER (oldbegv)->charpos;
5300 BEGV_BYTE = marker_byte_position (oldbegv);
5301
5302 if (zv_at_end)
5303 {
5304 ZV = Z;
5305 ZV_BYTE = Z_BYTE;
5306 }
5307 else
5308 {
5309 ZV = XMARKER (oldzv)->charpos;
5310 ZV_BYTE = marker_byte_position (oldzv);
5311 }
5312
5313 if (point_at_end)
5314 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5315 else
5316 /* We can't do Fgoto_char (oldpoint) because it will run some
5317 Lisp code. */
5318 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5319 XMARKER (oldpoint)->bytepos);
5320
5321 UNGCPRO;
5322 free_marker (oldpoint);
5323 free_marker (oldbegv);
5324 free_marker (oldzv);
5325
5326 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5327 set_buffer_internal (oldbuf);
5328 if (NILP (tem))
5329 windows_or_buffers_changed = old_windows_or_buffers_changed;
5330 message_log_need_newline = !nlflag;
5331 Vdeactivate_mark = old_deactivate_mark;
5332 }
5333 }
5334
5335
5336 /* We are at the end of the buffer after just having inserted a newline.
5337 (Note: We depend on the fact we won't be crossing the gap.)
5338 Check to see if the most recent message looks a lot like the previous one.
5339 Return 0 if different, 1 if the new one should just replace it, or a
5340 value N > 1 if we should also append " [N times]". */
5341
5342 static int
5343 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5344 int prev_bol, this_bol;
5345 int prev_bol_byte, this_bol_byte;
5346 {
5347 int i;
5348 int len = Z_BYTE - 1 - this_bol_byte;
5349 int seen_dots = 0;
5350 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5351 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5352
5353 for (i = 0; i < len; i++)
5354 {
5355 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5356 && p1[i] != '\n')
5357 seen_dots = 1;
5358 if (p1[i] != p2[i])
5359 return seen_dots;
5360 }
5361 p1 += len;
5362 if (*p1 == '\n')
5363 return 2;
5364 if (*p1++ == ' ' && *p1++ == '[')
5365 {
5366 int n = 0;
5367 while (*p1 >= '0' && *p1 <= '9')
5368 n = n * 10 + *p1++ - '0';
5369 if (strncmp (p1, " times]\n", 8) == 0)
5370 return n+1;
5371 }
5372 return 0;
5373 }
5374
5375
5376 /* Display an echo area message M with a specified length of LEN
5377 chars. The string may include null characters. If M is 0, clear
5378 out any existing message, and let the mini-buffer text show through.
5379
5380 The buffer M must continue to exist until after the echo area gets
5381 cleared or some other message gets displayed there. This means do
5382 not pass text that is stored in a Lisp string; do not pass text in
5383 a buffer that was alloca'd. */
5384
5385 void
5386 message2 (m, len, multibyte)
5387 char *m;
5388 int len;
5389 int multibyte;
5390 {
5391 /* First flush out any partial line written with print. */
5392 message_log_maybe_newline ();
5393 if (m)
5394 message_dolog (m, len, 1, multibyte);
5395 message2_nolog (m, len, multibyte);
5396 }
5397
5398
5399 /* The non-logging counterpart of message2. */
5400
5401 void
5402 message2_nolog (m, len, multibyte)
5403 char *m;
5404 int len;
5405 {
5406 struct frame *sf = SELECTED_FRAME ();
5407 message_enable_multibyte = multibyte;
5408
5409 if (noninteractive)
5410 {
5411 if (noninteractive_need_newline)
5412 putc ('\n', stderr);
5413 noninteractive_need_newline = 0;
5414 if (m)
5415 fwrite (m, len, 1, stderr);
5416 if (cursor_in_echo_area == 0)
5417 fprintf (stderr, "\n");
5418 fflush (stderr);
5419 }
5420 /* A null message buffer means that the frame hasn't really been
5421 initialized yet. Error messages get reported properly by
5422 cmd_error, so this must be just an informative message; toss it. */
5423 else if (INTERACTIVE
5424 && sf->glyphs_initialized_p
5425 && FRAME_MESSAGE_BUF (sf))
5426 {
5427 Lisp_Object mini_window;
5428 struct frame *f;
5429
5430 /* Get the frame containing the mini-buffer
5431 that the selected frame is using. */
5432 mini_window = FRAME_MINIBUF_WINDOW (sf);
5433 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5434
5435 FRAME_SAMPLE_VISIBILITY (f);
5436 if (FRAME_VISIBLE_P (sf)
5437 && ! FRAME_VISIBLE_P (f))
5438 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5439
5440 if (m)
5441 {
5442 set_message (m, Qnil, len, multibyte);
5443 if (minibuffer_auto_raise)
5444 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5445 }
5446 else
5447 clear_message (1, 1);
5448
5449 do_pending_window_change (0);
5450 echo_area_display (1);
5451 do_pending_window_change (0);
5452 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5453 (*frame_up_to_date_hook) (f);
5454 }
5455 }
5456
5457
5458 /* Display an echo area message M with a specified length of NBYTES
5459 bytes. The string may include null characters. If M is not a
5460 string, clear out any existing message, and let the mini-buffer
5461 text show through. */
5462
5463 void
5464 message3 (m, nbytes, multibyte)
5465 Lisp_Object m;
5466 int nbytes;
5467 int multibyte;
5468 {
5469 struct gcpro gcpro1;
5470
5471 GCPRO1 (m);
5472
5473 /* First flush out any partial line written with print. */
5474 message_log_maybe_newline ();
5475 if (STRINGP (m))
5476 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5477 message3_nolog (m, nbytes, multibyte);
5478
5479 UNGCPRO;
5480 }
5481
5482
5483 /* The non-logging version of message3. */
5484
5485 void
5486 message3_nolog (m, nbytes, multibyte)
5487 Lisp_Object m;
5488 int nbytes, multibyte;
5489 {
5490 struct frame *sf = SELECTED_FRAME ();
5491 message_enable_multibyte = multibyte;
5492
5493 if (noninteractive)
5494 {
5495 if (noninteractive_need_newline)
5496 putc ('\n', stderr);
5497 noninteractive_need_newline = 0;
5498 if (STRINGP (m))
5499 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5500 if (cursor_in_echo_area == 0)
5501 fprintf (stderr, "\n");
5502 fflush (stderr);
5503 }
5504 /* A null message buffer means that the frame hasn't really been
5505 initialized yet. Error messages get reported properly by
5506 cmd_error, so this must be just an informative message; toss it. */
5507 else if (INTERACTIVE
5508 && sf->glyphs_initialized_p
5509 && FRAME_MESSAGE_BUF (sf))
5510 {
5511 Lisp_Object mini_window;
5512 Lisp_Object frame;
5513 struct frame *f;
5514
5515 /* Get the frame containing the mini-buffer
5516 that the selected frame is using. */
5517 mini_window = FRAME_MINIBUF_WINDOW (sf);
5518 frame = XWINDOW (mini_window)->frame;
5519 f = XFRAME (frame);
5520
5521 FRAME_SAMPLE_VISIBILITY (f);
5522 if (FRAME_VISIBLE_P (sf)
5523 && !FRAME_VISIBLE_P (f))
5524 Fmake_frame_visible (frame);
5525
5526 if (STRINGP (m) && XSTRING (m)->size)
5527 {
5528 set_message (NULL, m, nbytes, multibyte);
5529 if (minibuffer_auto_raise)
5530 Fraise_frame (frame);
5531 }
5532 else
5533 clear_message (1, 1);
5534
5535 do_pending_window_change (0);
5536 echo_area_display (1);
5537 do_pending_window_change (0);
5538 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5539 (*frame_up_to_date_hook) (f);
5540 }
5541 }
5542
5543
5544 /* Display a null-terminated echo area message M. If M is 0, clear
5545 out any existing message, and let the mini-buffer text show through.
5546
5547 The buffer M must continue to exist until after the echo area gets
5548 cleared or some other message gets displayed there. Do not pass
5549 text that is stored in a Lisp string. Do not pass text in a buffer
5550 that was alloca'd. */
5551
5552 void
5553 message1 (m)
5554 char *m;
5555 {
5556 message2 (m, (m ? strlen (m) : 0), 0);
5557 }
5558
5559
5560 /* The non-logging counterpart of message1. */
5561
5562 void
5563 message1_nolog (m)
5564 char *m;
5565 {
5566 message2_nolog (m, (m ? strlen (m) : 0), 0);
5567 }
5568
5569 /* Display a message M which contains a single %s
5570 which gets replaced with STRING. */
5571
5572 void
5573 message_with_string (m, string, log)
5574 char *m;
5575 Lisp_Object string;
5576 int log;
5577 {
5578 if (noninteractive)
5579 {
5580 if (m)
5581 {
5582 if (noninteractive_need_newline)
5583 putc ('\n', stderr);
5584 noninteractive_need_newline = 0;
5585 fprintf (stderr, m, XSTRING (string)->data);
5586 if (cursor_in_echo_area == 0)
5587 fprintf (stderr, "\n");
5588 fflush (stderr);
5589 }
5590 }
5591 else if (INTERACTIVE)
5592 {
5593 /* The frame whose minibuffer we're going to display the message on.
5594 It may be larger than the selected frame, so we need
5595 to use its buffer, not the selected frame's buffer. */
5596 Lisp_Object mini_window;
5597 struct frame *f, *sf = SELECTED_FRAME ();
5598
5599 /* Get the frame containing the minibuffer
5600 that the selected frame is using. */
5601 mini_window = FRAME_MINIBUF_WINDOW (sf);
5602 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5603
5604 /* A null message buffer means that the frame hasn't really been
5605 initialized yet. Error messages get reported properly by
5606 cmd_error, so this must be just an informative message; toss it. */
5607 if (FRAME_MESSAGE_BUF (f))
5608 {
5609 int len;
5610 char *a[1];
5611 a[0] = (char *) XSTRING (string)->data;
5612
5613 len = doprnt (FRAME_MESSAGE_BUF (f),
5614 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5615
5616 if (log)
5617 message2 (FRAME_MESSAGE_BUF (f), len,
5618 STRING_MULTIBYTE (string));
5619 else
5620 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5621 STRING_MULTIBYTE (string));
5622
5623 /* Print should start at the beginning of the message
5624 buffer next time. */
5625 message_buf_print = 0;
5626 }
5627 }
5628 }
5629
5630
5631 /* Dump an informative message to the minibuf. If M is 0, clear out
5632 any existing message, and let the mini-buffer text show through. */
5633
5634 /* VARARGS 1 */
5635 void
5636 message (m, a1, a2, a3)
5637 char *m;
5638 EMACS_INT a1, a2, a3;
5639 {
5640 if (noninteractive)
5641 {
5642 if (m)
5643 {
5644 if (noninteractive_need_newline)
5645 putc ('\n', stderr);
5646 noninteractive_need_newline = 0;
5647 fprintf (stderr, m, a1, a2, a3);
5648 if (cursor_in_echo_area == 0)
5649 fprintf (stderr, "\n");
5650 fflush (stderr);
5651 }
5652 }
5653 else if (INTERACTIVE)
5654 {
5655 /* The frame whose mini-buffer we're going to display the message
5656 on. It may be larger than the selected frame, so we need to
5657 use its buffer, not the selected frame's buffer. */
5658 Lisp_Object mini_window;
5659 struct frame *f, *sf = SELECTED_FRAME ();
5660
5661 /* Get the frame containing the mini-buffer
5662 that the selected frame is using. */
5663 mini_window = FRAME_MINIBUF_WINDOW (sf);
5664 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5665
5666 /* A null message buffer means that the frame hasn't really been
5667 initialized yet. Error messages get reported properly by
5668 cmd_error, so this must be just an informative message; toss
5669 it. */
5670 if (FRAME_MESSAGE_BUF (f))
5671 {
5672 if (m)
5673 {
5674 int len;
5675 #ifdef NO_ARG_ARRAY
5676 char *a[3];
5677 a[0] = (char *) a1;
5678 a[1] = (char *) a2;
5679 a[2] = (char *) a3;
5680
5681 len = doprnt (FRAME_MESSAGE_BUF (f),
5682 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5683 #else
5684 len = doprnt (FRAME_MESSAGE_BUF (f),
5685 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5686 (char **) &a1);
5687 #endif /* NO_ARG_ARRAY */
5688
5689 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5690 }
5691 else
5692 message1 (0);
5693
5694 /* Print should start at the beginning of the message
5695 buffer next time. */
5696 message_buf_print = 0;
5697 }
5698 }
5699 }
5700
5701
5702 /* The non-logging version of message. */
5703
5704 void
5705 message_nolog (m, a1, a2, a3)
5706 char *m;
5707 EMACS_INT a1, a2, a3;
5708 {
5709 Lisp_Object old_log_max;
5710 old_log_max = Vmessage_log_max;
5711 Vmessage_log_max = Qnil;
5712 message (m, a1, a2, a3);
5713 Vmessage_log_max = old_log_max;
5714 }
5715
5716
5717 /* Display the current message in the current mini-buffer. This is
5718 only called from error handlers in process.c, and is not time
5719 critical. */
5720
5721 void
5722 update_echo_area ()
5723 {
5724 if (!NILP (echo_area_buffer[0]))
5725 {
5726 Lisp_Object string;
5727 string = Fcurrent_message ();
5728 message3 (string, XSTRING (string)->size,
5729 !NILP (current_buffer->enable_multibyte_characters));
5730 }
5731 }
5732
5733
5734 /* Make sure echo area buffers in echo_buffers[] are life. If they
5735 aren't, make new ones. */
5736
5737 static void
5738 ensure_echo_area_buffers ()
5739 {
5740 int i;
5741
5742 for (i = 0; i < 2; ++i)
5743 if (!BUFFERP (echo_buffer[i])
5744 || NILP (XBUFFER (echo_buffer[i])->name))
5745 {
5746 char name[30];
5747 Lisp_Object old_buffer;
5748 int j;
5749
5750 old_buffer = echo_buffer[i];
5751 sprintf (name, " *Echo Area %d*", i);
5752 echo_buffer[i] = Fget_buffer_create (build_string (name));
5753 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5754
5755 for (j = 0; j < 2; ++j)
5756 if (EQ (old_buffer, echo_area_buffer[j]))
5757 echo_area_buffer[j] = echo_buffer[i];
5758 }
5759 }
5760
5761
5762 /* Call FN with args A1..A4 with either the current or last displayed
5763 echo_area_buffer as current buffer.
5764
5765 WHICH zero means use the current message buffer
5766 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5767 from echo_buffer[] and clear it.
5768
5769 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5770 suitable buffer from echo_buffer[] and clear it.
5771
5772 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5773 that the current message becomes the last displayed one, make
5774 choose a suitable buffer for echo_area_buffer[0], and clear it.
5775
5776 Value is what FN returns. */
5777
5778 static int
5779 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5780 struct window *w;
5781 int which;
5782 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5783 EMACS_INT a1;
5784 Lisp_Object a2;
5785 EMACS_INT a3, a4;
5786 {
5787 Lisp_Object buffer;
5788 int this_one, the_other, clear_buffer_p, rc;
5789 int count = specpdl_ptr - specpdl;
5790
5791 /* If buffers aren't life, make new ones. */
5792 ensure_echo_area_buffers ();
5793
5794 clear_buffer_p = 0;
5795
5796 if (which == 0)
5797 this_one = 0, the_other = 1;
5798 else if (which > 0)
5799 this_one = 1, the_other = 0;
5800 else
5801 {
5802 this_one = 0, the_other = 1;
5803 clear_buffer_p = 1;
5804
5805 /* We need a fresh one in case the current echo buffer equals
5806 the one containing the last displayed echo area message. */
5807 if (!NILP (echo_area_buffer[this_one])
5808 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5809 echo_area_buffer[this_one] = Qnil;
5810 }
5811
5812 /* Choose a suitable buffer from echo_buffer[] is we don't
5813 have one. */
5814 if (NILP (echo_area_buffer[this_one]))
5815 {
5816 echo_area_buffer[this_one]
5817 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5818 ? echo_buffer[the_other]
5819 : echo_buffer[this_one]);
5820 clear_buffer_p = 1;
5821 }
5822
5823 buffer = echo_area_buffer[this_one];
5824
5825 record_unwind_protect (unwind_with_echo_area_buffer,
5826 with_echo_area_buffer_unwind_data (w));
5827
5828 /* Make the echo area buffer current. Note that for display
5829 purposes, it is not necessary that the displayed window's buffer
5830 == current_buffer, except for text property lookup. So, let's
5831 only set that buffer temporarily here without doing a full
5832 Fset_window_buffer. We must also change w->pointm, though,
5833 because otherwise an assertions in unshow_buffer fails, and Emacs
5834 aborts. */
5835 set_buffer_internal_1 (XBUFFER (buffer));
5836 if (w)
5837 {
5838 w->buffer = buffer;
5839 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5840 }
5841
5842 current_buffer->undo_list = Qt;
5843 current_buffer->read_only = Qnil;
5844
5845 if (clear_buffer_p && Z > BEG)
5846 del_range (BEG, Z);
5847
5848 xassert (BEGV >= BEG);
5849 xassert (ZV <= Z && ZV >= BEGV);
5850
5851 rc = fn (a1, a2, a3, a4);
5852
5853 xassert (BEGV >= BEG);
5854 xassert (ZV <= Z && ZV >= BEGV);
5855
5856 unbind_to (count, Qnil);
5857 return rc;
5858 }
5859
5860
5861 /* Save state that should be preserved around the call to the function
5862 FN called in with_echo_area_buffer. */
5863
5864 static Lisp_Object
5865 with_echo_area_buffer_unwind_data (w)
5866 struct window *w;
5867 {
5868 int i = 0;
5869 Lisp_Object vector;
5870
5871 /* Reduce consing by keeping one vector in
5872 Vwith_echo_area_save_vector. */
5873 vector = Vwith_echo_area_save_vector;
5874 Vwith_echo_area_save_vector = Qnil;
5875
5876 if (NILP (vector))
5877 vector = Fmake_vector (make_number (7), Qnil);
5878
5879 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5880 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5881 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5882
5883 if (w)
5884 {
5885 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5886 XVECTOR (vector)->contents[i++] = w->buffer;
5887 XVECTOR (vector)->contents[i++]
5888 = make_number (XMARKER (w->pointm)->charpos);
5889 XVECTOR (vector)->contents[i++]
5890 = make_number (XMARKER (w->pointm)->bytepos);
5891 }
5892 else
5893 {
5894 int end = i + 4;
5895 while (i < end)
5896 XVECTOR (vector)->contents[i++] = Qnil;
5897 }
5898
5899 xassert (i == XVECTOR (vector)->size);
5900 return vector;
5901 }
5902
5903
5904 /* Restore global state from VECTOR which was created by
5905 with_echo_area_buffer_unwind_data. */
5906
5907 static Lisp_Object
5908 unwind_with_echo_area_buffer (vector)
5909 Lisp_Object vector;
5910 {
5911 int i = 0;
5912
5913 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5914 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5915 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5916
5917 if (WINDOWP (XVECTOR (vector)->contents[i]))
5918 {
5919 struct window *w;
5920 Lisp_Object buffer, charpos, bytepos;
5921
5922 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5923 buffer = XVECTOR (vector)->contents[i]; ++i;
5924 charpos = XVECTOR (vector)->contents[i]; ++i;
5925 bytepos = XVECTOR (vector)->contents[i]; ++i;
5926
5927 w->buffer = buffer;
5928 set_marker_both (w->pointm, buffer,
5929 XFASTINT (charpos), XFASTINT (bytepos));
5930 }
5931
5932 Vwith_echo_area_save_vector = vector;
5933 return Qnil;
5934 }
5935
5936
5937 /* Set up the echo area for use by print functions. MULTIBYTE_P
5938 non-zero means we will print multibyte. */
5939
5940 void
5941 setup_echo_area_for_printing (multibyte_p)
5942 int multibyte_p;
5943 {
5944 ensure_echo_area_buffers ();
5945
5946 if (!message_buf_print)
5947 {
5948 /* A message has been output since the last time we printed.
5949 Choose a fresh echo area buffer. */
5950 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5951 echo_area_buffer[0] = echo_buffer[1];
5952 else
5953 echo_area_buffer[0] = echo_buffer[0];
5954
5955 /* Switch to that buffer and clear it. */
5956 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5957 if (Z > BEG)
5958 del_range (BEG, Z);
5959 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5960
5961 /* Set up the buffer for the multibyteness we need. */
5962 if (multibyte_p
5963 != !NILP (current_buffer->enable_multibyte_characters))
5964 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5965
5966 /* Raise the frame containing the echo area. */
5967 if (minibuffer_auto_raise)
5968 {
5969 struct frame *sf = SELECTED_FRAME ();
5970 Lisp_Object mini_window;
5971 mini_window = FRAME_MINIBUF_WINDOW (sf);
5972 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5973 }
5974
5975 message_log_maybe_newline ();
5976 message_buf_print = 1;
5977 }
5978 else
5979 {
5980 if (NILP (echo_area_buffer[0]))
5981 {
5982 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5983 echo_area_buffer[0] = echo_buffer[1];
5984 else
5985 echo_area_buffer[0] = echo_buffer[0];
5986 }
5987
5988 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5989 /* Someone switched buffers between print requests. */
5990 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5991 }
5992 }
5993
5994
5995 /* Display an echo area message in window W. Value is non-zero if W's
5996 height is changed. If display_last_displayed_message_p is
5997 non-zero, display the message that was last displayed, otherwise
5998 display the current message. */
5999
6000 static int
6001 display_echo_area (w)
6002 struct window *w;
6003 {
6004 int i, no_message_p, window_height_changed_p, count;
6005
6006 /* Temporarily disable garbage collections while displaying the echo
6007 area. This is done because a GC can print a message itself.
6008 That message would modify the echo area buffer's contents while a
6009 redisplay of the buffer is going on, and seriously confuse
6010 redisplay. */
6011 count = inhibit_garbage_collection ();
6012
6013 /* If there is no message, we must call display_echo_area_1
6014 nevertheless because it resizes the window. But we will have to
6015 reset the echo_area_buffer in question to nil at the end because
6016 with_echo_area_buffer will sets it to an empty buffer. */
6017 i = display_last_displayed_message_p ? 1 : 0;
6018 no_message_p = NILP (echo_area_buffer[i]);
6019
6020 window_height_changed_p
6021 = with_echo_area_buffer (w, display_last_displayed_message_p,
6022 display_echo_area_1,
6023 (EMACS_INT) w, Qnil, 0, 0);
6024
6025 if (no_message_p)
6026 echo_area_buffer[i] = Qnil;
6027
6028 unbind_to (count, Qnil);
6029 return window_height_changed_p;
6030 }
6031
6032
6033 /* Helper for display_echo_area. Display the current buffer which
6034 contains the current echo area message in window W, a mini-window,
6035 a pointer to which is passed in A1. A2..A4 are currently not used.
6036 Change the height of W so that all of the message is displayed.
6037 Value is non-zero if height of W was changed. */
6038
6039 static int
6040 display_echo_area_1 (a1, a2, a3, a4)
6041 EMACS_INT a1;
6042 Lisp_Object a2;
6043 EMACS_INT a3, a4;
6044 {
6045 struct window *w = (struct window *) a1;
6046 Lisp_Object window;
6047 struct text_pos start;
6048 int window_height_changed_p = 0;
6049
6050 /* Do this before displaying, so that we have a large enough glyph
6051 matrix for the display. */
6052 window_height_changed_p = resize_mini_window (w, 0);
6053
6054 /* Display. */
6055 clear_glyph_matrix (w->desired_matrix);
6056 XSETWINDOW (window, w);
6057 SET_TEXT_POS (start, BEG, BEG_BYTE);
6058 try_window (window, start);
6059
6060 return window_height_changed_p;
6061 }
6062
6063
6064 /* Resize the echo area window to exactly the size needed for the
6065 currently displayed message, if there is one. */
6066
6067 void
6068 resize_echo_area_axactly ()
6069 {
6070 if (BUFFERP (echo_area_buffer[0])
6071 && WINDOWP (echo_area_window))
6072 {
6073 struct window *w = XWINDOW (echo_area_window);
6074 int resized_p;
6075
6076 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6077 (EMACS_INT) w, Qnil, 0, 0);
6078 if (resized_p)
6079 {
6080 ++windows_or_buffers_changed;
6081 ++update_mode_lines;
6082 redisplay_internal (0);
6083 }
6084 }
6085 }
6086
6087
6088 /* Callback function for with_echo_area_buffer, when used from
6089 resize_echo_area_axactly. A1 contains a pointer to the window to
6090 resize, A2 to A4 are not used. Value is what resize_mini_window
6091 returns. */
6092
6093 static int
6094 resize_mini_window_1 (a1, a2, a3, a4)
6095 EMACS_INT a1;
6096 Lisp_Object a2;
6097 EMACS_INT a3, a4;
6098 {
6099 return resize_mini_window ((struct window *) a1, 1);
6100 }
6101
6102
6103 /* Resize mini-window W to fit the size of its contents. EXACT:P
6104 means size the window exactly to the size needed. Otherwise, it's
6105 only enlarged until W's buffer is empty. Value is non-zero if
6106 the window height has been changed. */
6107
6108 int
6109 resize_mini_window (w, exact_p)
6110 struct window *w;
6111 int exact_p;
6112 {
6113 struct frame *f = XFRAME (w->frame);
6114 int window_height_changed_p = 0;
6115
6116 xassert (MINI_WINDOW_P (w));
6117
6118 /* Nil means don't try to resize. */
6119 if (NILP (Vresize_mini_windows)
6120 || (FRAME_X_P (f) && f->output_data.x == NULL))
6121 return 0;
6122
6123 if (!FRAME_MINIBUF_ONLY_P (f))
6124 {
6125 struct it it;
6126 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6127 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6128 int height, max_height;
6129 int unit = CANON_Y_UNIT (f);
6130 struct text_pos start;
6131
6132 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6133
6134 /* Compute the max. number of lines specified by the user. */
6135 if (FLOATP (Vmax_mini_window_height))
6136 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6137 else if (INTEGERP (Vmax_mini_window_height))
6138 max_height = XINT (Vmax_mini_window_height);
6139 else
6140 max_height = total_height / 4;
6141
6142 /* Correct that max. height if it's bogus. */
6143 max_height = max (1, max_height);
6144 max_height = min (total_height, max_height);
6145
6146 /* Find out the height of the text in the window. */
6147 if (it.truncate_lines_p)
6148 height = 1;
6149 else
6150 {
6151 last_height = 0;
6152 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6153 if (it.max_ascent == 0 && it.max_descent == 0)
6154 height = it.current_y + last_height;
6155 else
6156 height = it.current_y + it.max_ascent + it.max_descent;
6157 height -= it.extra_line_spacing;
6158 height = (height + unit - 1) / unit;
6159 }
6160
6161 /* Compute a suitable window start. */
6162 if (height > max_height)
6163 {
6164 height = max_height;
6165 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6166 move_it_vertically_backward (&it, (height - 1) * unit);
6167 start = it.current.pos;
6168 }
6169 else
6170 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6171 SET_MARKER_FROM_TEXT_POS (w->start, start);
6172
6173 if (EQ (Vresize_mini_windows, Qgrow_only))
6174 {
6175 /* Let it grow only, until we display an empty message, in which
6176 case the window shrinks again. */
6177 if (height > XFASTINT (w->height))
6178 {
6179 int old_height = XFASTINT (w->height);
6180 freeze_window_starts (f, 1);
6181 grow_mini_window (w, height - XFASTINT (w->height));
6182 window_height_changed_p = XFASTINT (w->height) != old_height;
6183 }
6184 else if (height < XFASTINT (w->height)
6185 && (exact_p || BEGV == ZV))
6186 {
6187 int old_height = XFASTINT (w->height);
6188 freeze_window_starts (f, 0);
6189 shrink_mini_window (w);
6190 window_height_changed_p = XFASTINT (w->height) != old_height;
6191 }
6192 }
6193 else
6194 {
6195 /* Always resize to exact size needed. */
6196 if (height > XFASTINT (w->height))
6197 {
6198 int old_height = XFASTINT (w->height);
6199 freeze_window_starts (f, 1);
6200 grow_mini_window (w, height - XFASTINT (w->height));
6201 window_height_changed_p = XFASTINT (w->height) != old_height;
6202 }
6203 else if (height < XFASTINT (w->height))
6204 {
6205 int old_height = XFASTINT (w->height);
6206 freeze_window_starts (f, 0);
6207 shrink_mini_window (w);
6208
6209 if (height)
6210 {
6211 freeze_window_starts (f, 1);
6212 grow_mini_window (w, height - XFASTINT (w->height));
6213 }
6214
6215 window_height_changed_p = XFASTINT (w->height) != old_height;
6216 }
6217 }
6218 }
6219
6220 return window_height_changed_p;
6221 }
6222
6223
6224 /* Value is the current message, a string, or nil if there is no
6225 current message. */
6226
6227 Lisp_Object
6228 current_message ()
6229 {
6230 Lisp_Object msg;
6231
6232 if (NILP (echo_area_buffer[0]))
6233 msg = Qnil;
6234 else
6235 {
6236 with_echo_area_buffer (0, 0, current_message_1,
6237 (EMACS_INT) &msg, Qnil, 0, 0);
6238 if (NILP (msg))
6239 echo_area_buffer[0] = Qnil;
6240 }
6241
6242 return msg;
6243 }
6244
6245
6246 static int
6247 current_message_1 (a1, a2, a3, a4)
6248 EMACS_INT a1;
6249 Lisp_Object a2;
6250 EMACS_INT a3, a4;
6251 {
6252 Lisp_Object *msg = (Lisp_Object *) a1;
6253
6254 if (Z > BEG)
6255 *msg = make_buffer_string (BEG, Z, 1);
6256 else
6257 *msg = Qnil;
6258 return 0;
6259 }
6260
6261
6262 /* Push the current message on Vmessage_stack for later restauration
6263 by restore_message. Value is non-zero if the current message isn't
6264 empty. This is a relatively infrequent operation, so it's not
6265 worth optimizing. */
6266
6267 int
6268 push_message ()
6269 {
6270 Lisp_Object msg;
6271 msg = current_message ();
6272 Vmessage_stack = Fcons (msg, Vmessage_stack);
6273 return STRINGP (msg);
6274 }
6275
6276
6277 /* Restore message display from the top of Vmessage_stack. */
6278
6279 void
6280 restore_message ()
6281 {
6282 Lisp_Object msg;
6283
6284 xassert (CONSP (Vmessage_stack));
6285 msg = XCAR (Vmessage_stack);
6286 if (STRINGP (msg))
6287 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6288 else
6289 message3_nolog (msg, 0, 0);
6290 }
6291
6292
6293 /* Pop the top-most entry off Vmessage_stack. */
6294
6295 void
6296 pop_message ()
6297 {
6298 xassert (CONSP (Vmessage_stack));
6299 Vmessage_stack = XCDR (Vmessage_stack);
6300 }
6301
6302
6303 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6304 exits. If the stack is not empty, we have a missing pop_message
6305 somewhere. */
6306
6307 void
6308 check_message_stack ()
6309 {
6310 if (!NILP (Vmessage_stack))
6311 abort ();
6312 }
6313
6314
6315 /* Truncate to NCHARS what will be displayed in the echo area the next
6316 time we display it---but don't redisplay it now. */
6317
6318 void
6319 truncate_echo_area (nchars)
6320 int nchars;
6321 {
6322 if (nchars == 0)
6323 echo_area_buffer[0] = Qnil;
6324 /* A null message buffer means that the frame hasn't really been
6325 initialized yet. Error messages get reported properly by
6326 cmd_error, so this must be just an informative message; toss it. */
6327 else if (!noninteractive
6328 && INTERACTIVE
6329 && !NILP (echo_area_buffer[0]))
6330 {
6331 struct frame *sf = SELECTED_FRAME ();
6332 if (FRAME_MESSAGE_BUF (sf))
6333 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6334 }
6335 }
6336
6337
6338 /* Helper function for truncate_echo_area. Truncate the current
6339 message to at most NCHARS characters. */
6340
6341 static int
6342 truncate_message_1 (nchars, a2, a3, a4)
6343 EMACS_INT nchars;
6344 Lisp_Object a2;
6345 EMACS_INT a3, a4;
6346 {
6347 if (BEG + nchars < Z)
6348 del_range (BEG + nchars, Z);
6349 if (Z == BEG)
6350 echo_area_buffer[0] = Qnil;
6351 return 0;
6352 }
6353
6354
6355 /* Set the current message to a substring of S or STRING.
6356
6357 If STRING is a Lisp string, set the message to the first NBYTES
6358 bytes from STRING. NBYTES zero means use the whole string. If
6359 STRING is multibyte, the message will be displayed multibyte.
6360
6361 If S is not null, set the message to the first LEN bytes of S. LEN
6362 zero means use the whole string. MULTIBYTE_P non-zero means S is
6363 multibyte. Display the message multibyte in that case. */
6364
6365 void
6366 set_message (s, string, nbytes, multibyte_p)
6367 char *s;
6368 Lisp_Object string;
6369 int nbytes;
6370 {
6371 message_enable_multibyte
6372 = ((s && multibyte_p)
6373 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6374
6375 with_echo_area_buffer (0, -1, set_message_1,
6376 (EMACS_INT) s, string, nbytes, multibyte_p);
6377 message_buf_print = 0;
6378 help_echo_showing_p = 0;
6379 }
6380
6381
6382 /* Helper function for set_message. Arguments have the same meaning
6383 as there, with A1 corresponding to S and A2 corresponding to STRING
6384 This function is called with the echo area buffer being
6385 current. */
6386
6387 static int
6388 set_message_1 (a1, a2, nbytes, multibyte_p)
6389 EMACS_INT a1;
6390 Lisp_Object a2;
6391 EMACS_INT nbytes, multibyte_p;
6392 {
6393 char *s = (char *) a1;
6394 Lisp_Object string = a2;
6395
6396 xassert (BEG == Z);
6397
6398 /* Change multibyteness of the echo buffer appropriately. */
6399 if (message_enable_multibyte
6400 != !NILP (current_buffer->enable_multibyte_characters))
6401 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6402
6403 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6404
6405 /* Insert new message at BEG. */
6406 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6407
6408 if (STRINGP (string))
6409 {
6410 int nchars;
6411
6412 if (nbytes == 0)
6413 nbytes = XSTRING (string)->size_byte;
6414 nchars = string_byte_to_char (string, nbytes);
6415
6416 /* This function takes care of single/multibyte conversion. We
6417 just have to ensure that the echo area buffer has the right
6418 setting of enable_multibyte_characters. */
6419 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6420 }
6421 else if (s)
6422 {
6423 if (nbytes == 0)
6424 nbytes = strlen (s);
6425
6426 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6427 {
6428 /* Convert from multi-byte to single-byte. */
6429 int i, c, n;
6430 unsigned char work[1];
6431
6432 /* Convert a multibyte string to single-byte. */
6433 for (i = 0; i < nbytes; i += n)
6434 {
6435 c = string_char_and_length (s + i, nbytes - i, &n);
6436 work[0] = (SINGLE_BYTE_CHAR_P (c)
6437 ? c
6438 : multibyte_char_to_unibyte (c, Qnil));
6439 insert_1_both (work, 1, 1, 1, 0, 0);
6440 }
6441 }
6442 else if (!multibyte_p
6443 && !NILP (current_buffer->enable_multibyte_characters))
6444 {
6445 /* Convert from single-byte to multi-byte. */
6446 int i, c, n;
6447 unsigned char *msg = (unsigned char *) s;
6448 unsigned char str[MAX_MULTIBYTE_LENGTH];
6449
6450 /* Convert a single-byte string to multibyte. */
6451 for (i = 0; i < nbytes; i++)
6452 {
6453 c = unibyte_char_to_multibyte (msg[i]);
6454 n = CHAR_STRING (c, str);
6455 insert_1_both (str, 1, n, 1, 0, 0);
6456 }
6457 }
6458 else
6459 insert_1 (s, nbytes, 1, 0, 0);
6460 }
6461
6462 return 0;
6463 }
6464
6465
6466 /* Clear messages. CURRENT_P non-zero means clear the current
6467 message. LAST_DISPLAYED_P non-zero means clear the message
6468 last displayed. */
6469
6470 void
6471 clear_message (current_p, last_displayed_p)
6472 int current_p, last_displayed_p;
6473 {
6474 if (current_p)
6475 echo_area_buffer[0] = Qnil;
6476
6477 if (last_displayed_p)
6478 echo_area_buffer[1] = Qnil;
6479
6480 message_buf_print = 0;
6481 }
6482
6483 /* Clear garbaged frames.
6484
6485 This function is used where the old redisplay called
6486 redraw_garbaged_frames which in turn called redraw_frame which in
6487 turn called clear_frame. The call to clear_frame was a source of
6488 flickering. I believe a clear_frame is not necessary. It should
6489 suffice in the new redisplay to invalidate all current matrices,
6490 and ensure a complete redisplay of all windows. */
6491
6492 static void
6493 clear_garbaged_frames ()
6494 {
6495 if (frame_garbaged)
6496 {
6497 Lisp_Object tail, frame;
6498
6499 FOR_EACH_FRAME (tail, frame)
6500 {
6501 struct frame *f = XFRAME (frame);
6502
6503 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6504 {
6505 clear_current_matrices (f);
6506 f->garbaged = 0;
6507 }
6508 }
6509
6510 frame_garbaged = 0;
6511 ++windows_or_buffers_changed;
6512 }
6513 }
6514
6515
6516 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6517 is non-zero update selected_frame. Value is non-zero if the
6518 mini-windows height has been changed. */
6519
6520 static int
6521 echo_area_display (update_frame_p)
6522 int update_frame_p;
6523 {
6524 Lisp_Object mini_window;
6525 struct window *w;
6526 struct frame *f;
6527 int window_height_changed_p = 0;
6528 struct frame *sf = SELECTED_FRAME ();
6529
6530 mini_window = FRAME_MINIBUF_WINDOW (sf);
6531 w = XWINDOW (mini_window);
6532 f = XFRAME (WINDOW_FRAME (w));
6533
6534 /* Don't display if frame is invisible or not yet initialized. */
6535 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6536 return 0;
6537
6538 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6539 #ifndef macintosh
6540 #ifdef HAVE_WINDOW_SYSTEM
6541 /* When Emacs starts, selected_frame may be a visible terminal
6542 frame, even if we run under a window system. If we let this
6543 through, a message would be displayed on the terminal. */
6544 if (EQ (selected_frame, Vterminal_frame)
6545 && !NILP (Vwindow_system))
6546 return 0;
6547 #endif /* HAVE_WINDOW_SYSTEM */
6548 #endif
6549
6550 /* Redraw garbaged frames. */
6551 if (frame_garbaged)
6552 clear_garbaged_frames ();
6553
6554 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6555 {
6556 echo_area_window = mini_window;
6557 window_height_changed_p = display_echo_area (w);
6558 w->must_be_updated_p = 1;
6559
6560 /* Update the display, unless called from redisplay_internal.
6561 Also don't update the screen during redisplay itself. The
6562 update will happen at the end of redisplay, and an update
6563 here could cause confusion. */
6564 if (update_frame_p && !redisplaying_p)
6565 {
6566 int n = 0;
6567
6568 /* If the display update has been interrupted by pending
6569 input, update mode lines in the frame. Due to the
6570 pending input, it might have been that redisplay hasn't
6571 been called, so that mode lines above the echo area are
6572 garbaged. This looks odd, so we prevent it here. */
6573 if (!display_completed)
6574 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6575
6576 if (window_height_changed_p)
6577 {
6578 /* Must update other windows. */
6579 windows_or_buffers_changed = 1;
6580 redisplay_internal (0);
6581 }
6582 else if (FRAME_WINDOW_P (f) && n == 0)
6583 {
6584 /* Window configuration is the same as before.
6585 Can do with a display update of the echo area,
6586 unless we displayed some mode lines. */
6587 update_single_window (w, 1);
6588 rif->flush_display (f);
6589 }
6590 else
6591 update_frame (f, 1, 1);
6592 }
6593 }
6594 else if (!EQ (mini_window, selected_window))
6595 windows_or_buffers_changed++;
6596
6597 /* Last displayed message is now the current message. */
6598 echo_area_buffer[1] = echo_area_buffer[0];
6599
6600 /* Prevent redisplay optimization in redisplay_internal by resetting
6601 this_line_start_pos. This is done because the mini-buffer now
6602 displays the message instead of its buffer text. */
6603 if (EQ (mini_window, selected_window))
6604 CHARPOS (this_line_start_pos) = 0;
6605
6606 return window_height_changed_p;
6607 }
6608
6609
6610 \f
6611 /***********************************************************************
6612 Frame Titles
6613 ***********************************************************************/
6614
6615
6616 #ifdef HAVE_WINDOW_SYSTEM
6617
6618 /* A buffer for constructing frame titles in it; allocated from the
6619 heap in init_xdisp and resized as needed in store_frame_title_char. */
6620
6621 static char *frame_title_buf;
6622
6623 /* The buffer's end, and a current output position in it. */
6624
6625 static char *frame_title_buf_end;
6626 static char *frame_title_ptr;
6627
6628
6629 /* Store a single character C for the frame title in frame_title_buf.
6630 Re-allocate frame_title_buf if necessary. */
6631
6632 static void
6633 store_frame_title_char (c)
6634 char c;
6635 {
6636 /* If output position has reached the end of the allocated buffer,
6637 double the buffer's size. */
6638 if (frame_title_ptr == frame_title_buf_end)
6639 {
6640 int len = frame_title_ptr - frame_title_buf;
6641 int new_size = 2 * len * sizeof *frame_title_buf;
6642 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6643 frame_title_buf_end = frame_title_buf + new_size;
6644 frame_title_ptr = frame_title_buf + len;
6645 }
6646
6647 *frame_title_ptr++ = c;
6648 }
6649
6650
6651 /* Store part of a frame title in frame_title_buf, beginning at
6652 frame_title_ptr. STR is the string to store. Do not copy more
6653 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6654 the whole string. Pad with spaces until FIELD_WIDTH number of
6655 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6656 Called from display_mode_element when it is used to build a frame
6657 title. */
6658
6659 static int
6660 store_frame_title (str, field_width, precision)
6661 unsigned char *str;
6662 int field_width, precision;
6663 {
6664 int n = 0;
6665
6666 /* Copy at most PRECISION chars from STR. */
6667 while ((precision <= 0 || n < precision)
6668 && *str)
6669 {
6670 store_frame_title_char (*str++);
6671 ++n;
6672 }
6673
6674 /* Fill up with spaces until FIELD_WIDTH reached. */
6675 while (field_width > 0
6676 && n < field_width)
6677 {
6678 store_frame_title_char (' ');
6679 ++n;
6680 }
6681
6682 return n;
6683 }
6684
6685
6686 /* Set the title of FRAME, if it has changed. The title format is
6687 Vicon_title_format if FRAME is iconified, otherwise it is
6688 frame_title_format. */
6689
6690 static void
6691 x_consider_frame_title (frame)
6692 Lisp_Object frame;
6693 {
6694 struct frame *f = XFRAME (frame);
6695
6696 if (FRAME_WINDOW_P (f)
6697 || FRAME_MINIBUF_ONLY_P (f)
6698 || f->explicit_name)
6699 {
6700 /* Do we have more than one visible frame on this X display? */
6701 Lisp_Object tail;
6702 Lisp_Object fmt;
6703 struct buffer *obuf;
6704 int len;
6705 struct it it;
6706
6707 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6708 {
6709 struct frame *tf = XFRAME (XCAR (tail));
6710
6711 if (tf != f
6712 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6713 && !FRAME_MINIBUF_ONLY_P (tf)
6714 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6715 break;
6716 }
6717
6718 /* Set global variable indicating that multiple frames exist. */
6719 multiple_frames = CONSP (tail);
6720
6721 /* Switch to the buffer of selected window of the frame. Set up
6722 frame_title_ptr so that display_mode_element will output into it;
6723 then display the title. */
6724 obuf = current_buffer;
6725 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6726 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6727 frame_title_ptr = frame_title_buf;
6728 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6729 NULL, DEFAULT_FACE_ID);
6730 len = display_mode_element (&it, 0, -1, -1, fmt);
6731 frame_title_ptr = NULL;
6732 set_buffer_internal (obuf);
6733
6734 /* Set the title only if it's changed. This avoids consing in
6735 the common case where it hasn't. (If it turns out that we've
6736 already wasted too much time by walking through the list with
6737 display_mode_element, then we might need to optimize at a
6738 higher level than this.) */
6739 if (! STRINGP (f->name)
6740 || STRING_BYTES (XSTRING (f->name)) != len
6741 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6742 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6743 }
6744 }
6745
6746 #else /* not HAVE_WINDOW_SYSTEM */
6747
6748 #define frame_title_ptr ((char *)0)
6749 #define store_frame_title(str, mincol, maxcol) 0
6750
6751 #endif /* not HAVE_WINDOW_SYSTEM */
6752
6753
6754
6755 \f
6756 /***********************************************************************
6757 Menu Bars
6758 ***********************************************************************/
6759
6760
6761 /* Prepare for redisplay by updating menu-bar item lists when
6762 appropriate. This can call eval. */
6763
6764 void
6765 prepare_menu_bars ()
6766 {
6767 int all_windows;
6768 struct gcpro gcpro1, gcpro2;
6769 struct frame *f;
6770 struct frame *tooltip_frame;
6771
6772 #ifdef HAVE_X_WINDOWS
6773 tooltip_frame = tip_frame;
6774 #else
6775 tooltip_frame = NULL;
6776 #endif
6777
6778 /* Update all frame titles based on their buffer names, etc. We do
6779 this before the menu bars so that the buffer-menu will show the
6780 up-to-date frame titles. */
6781 #ifdef HAVE_WINDOW_SYSTEM
6782 if (windows_or_buffers_changed || update_mode_lines)
6783 {
6784 Lisp_Object tail, frame;
6785
6786 FOR_EACH_FRAME (tail, frame)
6787 {
6788 f = XFRAME (frame);
6789 if (f != tooltip_frame
6790 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6791 x_consider_frame_title (frame);
6792 }
6793 }
6794 #endif /* HAVE_WINDOW_SYSTEM */
6795
6796 /* Update the menu bar item lists, if appropriate. This has to be
6797 done before any actual redisplay or generation of display lines. */
6798 all_windows = (update_mode_lines
6799 || buffer_shared > 1
6800 || windows_or_buffers_changed);
6801 if (all_windows)
6802 {
6803 Lisp_Object tail, frame;
6804 int count = specpdl_ptr - specpdl;
6805
6806 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6807
6808 FOR_EACH_FRAME (tail, frame)
6809 {
6810 f = XFRAME (frame);
6811
6812 /* Ignore tooltip frame. */
6813 if (f == tooltip_frame)
6814 continue;
6815
6816 /* If a window on this frame changed size, report that to
6817 the user and clear the size-change flag. */
6818 if (FRAME_WINDOW_SIZES_CHANGED (f))
6819 {
6820 Lisp_Object functions;
6821
6822 /* Clear flag first in case we get an error below. */
6823 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6824 functions = Vwindow_size_change_functions;
6825 GCPRO2 (tail, functions);
6826
6827 while (CONSP (functions))
6828 {
6829 call1 (XCAR (functions), frame);
6830 functions = XCDR (functions);
6831 }
6832 UNGCPRO;
6833 }
6834
6835 GCPRO1 (tail);
6836 update_menu_bar (f, 0);
6837 #ifdef HAVE_WINDOW_SYSTEM
6838 update_tool_bar (f, 0);
6839 #endif
6840 UNGCPRO;
6841 }
6842
6843 unbind_to (count, Qnil);
6844 }
6845 else
6846 {
6847 struct frame *sf = SELECTED_FRAME ();
6848 update_menu_bar (sf, 1);
6849 #ifdef HAVE_WINDOW_SYSTEM
6850 update_tool_bar (sf, 1);
6851 #endif
6852 }
6853
6854 /* Motif needs this. See comment in xmenu.c. Turn it off when
6855 pending_menu_activation is not defined. */
6856 #ifdef USE_X_TOOLKIT
6857 pending_menu_activation = 0;
6858 #endif
6859 }
6860
6861
6862 /* Update the menu bar item list for frame F. This has to be done
6863 before we start to fill in any display lines, because it can call
6864 eval.
6865
6866 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6867
6868 static void
6869 update_menu_bar (f, save_match_data)
6870 struct frame *f;
6871 int save_match_data;
6872 {
6873 Lisp_Object window;
6874 register struct window *w;
6875
6876 window = FRAME_SELECTED_WINDOW (f);
6877 w = XWINDOW (window);
6878
6879 if (update_mode_lines)
6880 w->update_mode_line = Qt;
6881
6882 if (FRAME_WINDOW_P (f)
6883 ?
6884 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6885 FRAME_EXTERNAL_MENU_BAR (f)
6886 #else
6887 FRAME_MENU_BAR_LINES (f) > 0
6888 #endif
6889 : FRAME_MENU_BAR_LINES (f) > 0)
6890 {
6891 /* If the user has switched buffers or windows, we need to
6892 recompute to reflect the new bindings. But we'll
6893 recompute when update_mode_lines is set too; that means
6894 that people can use force-mode-line-update to request
6895 that the menu bar be recomputed. The adverse effect on
6896 the rest of the redisplay algorithm is about the same as
6897 windows_or_buffers_changed anyway. */
6898 if (windows_or_buffers_changed
6899 || !NILP (w->update_mode_line)
6900 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6901 < BUF_MODIFF (XBUFFER (w->buffer)))
6902 != !NILP (w->last_had_star))
6903 || ((!NILP (Vtransient_mark_mode)
6904 && !NILP (XBUFFER (w->buffer)->mark_active))
6905 != !NILP (w->region_showing)))
6906 {
6907 struct buffer *prev = current_buffer;
6908 int count = specpdl_ptr - specpdl;
6909
6910 set_buffer_internal_1 (XBUFFER (w->buffer));
6911 if (save_match_data)
6912 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6913 if (NILP (Voverriding_local_map_menu_flag))
6914 {
6915 specbind (Qoverriding_terminal_local_map, Qnil);
6916 specbind (Qoverriding_local_map, Qnil);
6917 }
6918
6919 /* Run the Lucid hook. */
6920 call1 (Vrun_hooks, Qactivate_menubar_hook);
6921
6922 /* If it has changed current-menubar from previous value,
6923 really recompute the menu-bar from the value. */
6924 if (! NILP (Vlucid_menu_bar_dirty_flag))
6925 call0 (Qrecompute_lucid_menubar);
6926
6927 safe_run_hooks (Qmenu_bar_update_hook);
6928 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6929
6930 /* Redisplay the menu bar in case we changed it. */
6931 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6932 if (FRAME_WINDOW_P (f)
6933 #if defined (macintosh)
6934 /* All frames on Mac OS share the same menubar. So only the
6935 selected frame should be allowed to set it. */
6936 && f == SELECTED_FRAME ()
6937 #endif
6938 )
6939 set_frame_menubar (f, 0, 0);
6940 else
6941 /* On a terminal screen, the menu bar is an ordinary screen
6942 line, and this makes it get updated. */
6943 w->update_mode_line = Qt;
6944 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6945 /* In the non-toolkit version, the menu bar is an ordinary screen
6946 line, and this makes it get updated. */
6947 w->update_mode_line = Qt;
6948 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6949
6950 unbind_to (count, Qnil);
6951 set_buffer_internal_1 (prev);
6952 }
6953 }
6954 }
6955
6956
6957 \f
6958 /***********************************************************************
6959 Tool-bars
6960 ***********************************************************************/
6961
6962 #ifdef HAVE_WINDOW_SYSTEM
6963
6964 /* Update the tool-bar item list for frame F. This has to be done
6965 before we start to fill in any display lines. Called from
6966 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6967 and restore it here. */
6968
6969 static void
6970 update_tool_bar (f, save_match_data)
6971 struct frame *f;
6972 int save_match_data;
6973 {
6974 if (WINDOWP (f->tool_bar_window)
6975 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6976 {
6977 Lisp_Object window;
6978 struct window *w;
6979
6980 window = FRAME_SELECTED_WINDOW (f);
6981 w = XWINDOW (window);
6982
6983 /* If the user has switched buffers or windows, we need to
6984 recompute to reflect the new bindings. But we'll
6985 recompute when update_mode_lines is set too; that means
6986 that people can use force-mode-line-update to request
6987 that the menu bar be recomputed. The adverse effect on
6988 the rest of the redisplay algorithm is about the same as
6989 windows_or_buffers_changed anyway. */
6990 if (windows_or_buffers_changed
6991 || !NILP (w->update_mode_line)
6992 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6993 < BUF_MODIFF (XBUFFER (w->buffer)))
6994 != !NILP (w->last_had_star))
6995 || ((!NILP (Vtransient_mark_mode)
6996 && !NILP (XBUFFER (w->buffer)->mark_active))
6997 != !NILP (w->region_showing)))
6998 {
6999 struct buffer *prev = current_buffer;
7000 int count = specpdl_ptr - specpdl;
7001
7002 /* Set current_buffer to the buffer of the selected
7003 window of the frame, so that we get the right local
7004 keymaps. */
7005 set_buffer_internal_1 (XBUFFER (w->buffer));
7006
7007 /* Save match data, if we must. */
7008 if (save_match_data)
7009 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7010
7011 /* Make sure that we don't accidentally use bogus keymaps. */
7012 if (NILP (Voverriding_local_map_menu_flag))
7013 {
7014 specbind (Qoverriding_terminal_local_map, Qnil);
7015 specbind (Qoverriding_local_map, Qnil);
7016 }
7017
7018 /* Build desired tool-bar items from keymaps. */
7019 f->desired_tool_bar_items
7020 = tool_bar_items (f->desired_tool_bar_items,
7021 &f->n_desired_tool_bar_items);
7022
7023 /* Redisplay the tool-bar in case we changed it. */
7024 w->update_mode_line = Qt;
7025
7026 unbind_to (count, Qnil);
7027 set_buffer_internal_1 (prev);
7028 }
7029 }
7030 }
7031
7032
7033 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7034 F's desired tool-bar contents. F->desired_tool_bar_items must have
7035 been set up previously by calling prepare_menu_bars. */
7036
7037 static void
7038 build_desired_tool_bar_string (f)
7039 struct frame *f;
7040 {
7041 int i, size, size_needed, string_idx;
7042 struct gcpro gcpro1, gcpro2, gcpro3;
7043 Lisp_Object image, plist, props;
7044
7045 image = plist = props = Qnil;
7046 GCPRO3 (image, plist, props);
7047
7048 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7049 Otherwise, make a new string. */
7050
7051 /* The size of the string we might be able to reuse. */
7052 size = (STRINGP (f->desired_tool_bar_string)
7053 ? XSTRING (f->desired_tool_bar_string)->size
7054 : 0);
7055
7056 /* Each image in the string we build is preceded by a space,
7057 and there is a space at the end. */
7058 size_needed = f->n_desired_tool_bar_items + 1;
7059
7060 /* Reuse f->desired_tool_bar_string, if possible. */
7061 if (size < size_needed)
7062 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7063 make_number (' '));
7064 else
7065 {
7066 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7067 Fremove_text_properties (make_number (0), make_number (size),
7068 props, f->desired_tool_bar_string);
7069 }
7070
7071 /* Put a `display' property on the string for the images to display,
7072 put a `menu_item' property on tool-bar items with a value that
7073 is the index of the item in F's tool-bar item vector. */
7074 for (i = 0, string_idx = 0;
7075 i < f->n_desired_tool_bar_items;
7076 ++i, string_idx += 1)
7077 {
7078 #define PROP(IDX) \
7079 (XVECTOR (f->desired_tool_bar_items) \
7080 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
7081
7082 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7083 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7084 int margin, relief, idx;
7085 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7086 extern Lisp_Object Qlaplace;
7087
7088 /* If image is a vector, choose the image according to the
7089 button state. */
7090 image = PROP (TOOL_BAR_ITEM_IMAGES);
7091 if (VECTORP (image))
7092 {
7093 if (enabled_p)
7094 idx = (selected_p
7095 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7096 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7097 else
7098 idx = (selected_p
7099 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7100 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7101
7102 xassert (ASIZE (image) >= idx);
7103 image = AREF (image, idx);
7104 }
7105 else
7106 idx = -1;
7107
7108 /* Ignore invalid image specifications. */
7109 if (!valid_image_p (image))
7110 continue;
7111
7112 /* Display the tool-bar button pressed, or depressed. */
7113 plist = Fcopy_sequence (XCDR (image));
7114
7115 /* Compute margin and relief to draw. */
7116 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7117 margin = relief + max (0, tool_bar_button_margin);
7118
7119 if (auto_raise_tool_bar_buttons_p)
7120 {
7121 /* Add a `:relief' property to the image spec if the item is
7122 selected. */
7123 if (selected_p)
7124 {
7125 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7126 margin -= relief;
7127 }
7128 }
7129 else
7130 {
7131 /* If image is selected, display it pressed, i.e. with a
7132 negative relief. If it's not selected, display it with a
7133 raised relief. */
7134 plist = Fplist_put (plist, QCrelief,
7135 (selected_p
7136 ? make_number (-relief)
7137 : make_number (relief)));
7138 margin -= relief;
7139 }
7140
7141 /* Put a margin around the image. */
7142 if (margin)
7143 plist = Fplist_put (plist, QCmargin, make_number (margin));
7144
7145 /* If button is not enabled, and we don't have special images
7146 for the disabled state, make the image appear disabled by
7147 applying an appropriate algorithm to it. */
7148 if (!enabled_p && idx < 0)
7149 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7150
7151 /* Put a `display' text property on the string for the image to
7152 display. Put a `menu-item' property on the string that gives
7153 the start of this item's properties in the tool-bar items
7154 vector. */
7155 image = Fcons (Qimage, plist);
7156 props = list4 (Qdisplay, image,
7157 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7158 Fadd_text_properties (make_number (string_idx),
7159 make_number (string_idx + 1),
7160 props, f->desired_tool_bar_string);
7161 #undef PROP
7162 }
7163
7164 UNGCPRO;
7165 }
7166
7167
7168 /* Display one line of the tool-bar of frame IT->f. */
7169
7170 static void
7171 display_tool_bar_line (it)
7172 struct it *it;
7173 {
7174 struct glyph_row *row = it->glyph_row;
7175 int max_x = it->last_visible_x;
7176 struct glyph *last;
7177
7178 prepare_desired_row (row);
7179 row->y = it->current_y;
7180
7181 while (it->current_x < max_x)
7182 {
7183 int x_before, x, n_glyphs_before, i, nglyphs;
7184
7185 /* Get the next display element. */
7186 if (!get_next_display_element (it))
7187 break;
7188
7189 /* Produce glyphs. */
7190 x_before = it->current_x;
7191 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7192 PRODUCE_GLYPHS (it);
7193
7194 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7195 i = 0;
7196 x = x_before;
7197 while (i < nglyphs)
7198 {
7199 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7200
7201 if (x + glyph->pixel_width > max_x)
7202 {
7203 /* Glyph doesn't fit on line. */
7204 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7205 it->current_x = x;
7206 goto out;
7207 }
7208
7209 ++it->hpos;
7210 x += glyph->pixel_width;
7211 ++i;
7212 }
7213
7214 /* Stop at line ends. */
7215 if (ITERATOR_AT_END_OF_LINE_P (it))
7216 break;
7217
7218 set_iterator_to_next (it, 1);
7219 }
7220
7221 out:;
7222
7223 row->displays_text_p = row->used[TEXT_AREA] != 0;
7224 extend_face_to_end_of_line (it);
7225 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7226 last->right_box_line_p = 1;
7227 compute_line_metrics (it);
7228
7229 /* If line is empty, make it occupy the rest of the tool-bar. */
7230 if (!row->displays_text_p)
7231 {
7232 row->height = row->phys_height = it->last_visible_y - row->y;
7233 row->ascent = row->phys_ascent = 0;
7234 }
7235
7236 row->full_width_p = 1;
7237 row->continued_p = 0;
7238 row->truncated_on_left_p = 0;
7239 row->truncated_on_right_p = 0;
7240
7241 it->current_x = it->hpos = 0;
7242 it->current_y += row->height;
7243 ++it->vpos;
7244 ++it->glyph_row;
7245 }
7246
7247
7248 /* Value is the number of screen lines needed to make all tool-bar
7249 items of frame F visible. */
7250
7251 static int
7252 tool_bar_lines_needed (f)
7253 struct frame *f;
7254 {
7255 struct window *w = XWINDOW (f->tool_bar_window);
7256 struct it it;
7257
7258 /* Initialize an iterator for iteration over
7259 F->desired_tool_bar_string in the tool-bar window of frame F. */
7260 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7261 it.first_visible_x = 0;
7262 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7263 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7264
7265 while (!ITERATOR_AT_END_P (&it))
7266 {
7267 it.glyph_row = w->desired_matrix->rows;
7268 clear_glyph_row (it.glyph_row);
7269 display_tool_bar_line (&it);
7270 }
7271
7272 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7273 }
7274
7275
7276 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7277 height should be changed. */
7278
7279 static int
7280 redisplay_tool_bar (f)
7281 struct frame *f;
7282 {
7283 struct window *w;
7284 struct it it;
7285 struct glyph_row *row;
7286 int change_height_p = 0;
7287
7288 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7289 do anything. This means you must start with tool-bar-lines
7290 non-zero to get the auto-sizing effect. Or in other words, you
7291 can turn off tool-bars by specifying tool-bar-lines zero. */
7292 if (!WINDOWP (f->tool_bar_window)
7293 || (w = XWINDOW (f->tool_bar_window),
7294 XFASTINT (w->height) == 0))
7295 return 0;
7296
7297 /* Set up an iterator for the tool-bar window. */
7298 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7299 it.first_visible_x = 0;
7300 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7301 row = it.glyph_row;
7302
7303 /* Build a string that represents the contents of the tool-bar. */
7304 build_desired_tool_bar_string (f);
7305 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7306
7307 /* Display as many lines as needed to display all tool-bar items. */
7308 while (it.current_y < it.last_visible_y)
7309 display_tool_bar_line (&it);
7310
7311 /* It doesn't make much sense to try scrolling in the tool-bar
7312 window, so don't do it. */
7313 w->desired_matrix->no_scrolling_p = 1;
7314 w->must_be_updated_p = 1;
7315
7316 if (auto_resize_tool_bars_p)
7317 {
7318 int nlines;
7319
7320 /* If there are blank lines at the end, except for a partially
7321 visible blank line at the end that is smaller than
7322 CANON_Y_UNIT, change the tool-bar's height. */
7323 row = it.glyph_row - 1;
7324 if (!row->displays_text_p
7325 && row->height >= CANON_Y_UNIT (f))
7326 change_height_p = 1;
7327
7328 /* If row displays tool-bar items, but is partially visible,
7329 change the tool-bar's height. */
7330 if (row->displays_text_p
7331 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7332 change_height_p = 1;
7333
7334 /* Resize windows as needed by changing the `tool-bar-lines'
7335 frame parameter. */
7336 if (change_height_p
7337 && (nlines = tool_bar_lines_needed (f),
7338 nlines != XFASTINT (w->height)))
7339 {
7340 extern Lisp_Object Qtool_bar_lines;
7341 Lisp_Object frame;
7342 int old_height = XFASTINT (w->height);
7343
7344 XSETFRAME (frame, f);
7345 clear_glyph_matrix (w->desired_matrix);
7346 Fmodify_frame_parameters (frame,
7347 Fcons (Fcons (Qtool_bar_lines,
7348 make_number (nlines)),
7349 Qnil));
7350 if (XFASTINT (w->height) != old_height)
7351 fonts_changed_p = 1;
7352 }
7353 }
7354
7355 return change_height_p;
7356 }
7357
7358
7359 /* Get information about the tool-bar item which is displayed in GLYPH
7360 on frame F. Return in *PROP_IDX the index where tool-bar item
7361 properties start in F->current_tool_bar_items. Value is zero if
7362 GLYPH doesn't display a tool-bar item. */
7363
7364 int
7365 tool_bar_item_info (f, glyph, prop_idx)
7366 struct frame *f;
7367 struct glyph *glyph;
7368 int *prop_idx;
7369 {
7370 Lisp_Object prop;
7371 int success_p;
7372
7373 /* Get the text property `menu-item' at pos. The value of that
7374 property is the start index of this item's properties in
7375 F->current_tool_bar_items. */
7376 prop = Fget_text_property (make_number (glyph->charpos),
7377 Qmenu_item, f->current_tool_bar_string);
7378 if (INTEGERP (prop))
7379 {
7380 *prop_idx = XINT (prop);
7381 success_p = 1;
7382 }
7383 else
7384 success_p = 0;
7385
7386 return success_p;
7387 }
7388
7389 #endif /* HAVE_WINDOW_SYSTEM */
7390
7391
7392 \f
7393 /************************************************************************
7394 Horizontal scrolling
7395 ************************************************************************/
7396
7397 static int hscroll_window_tree P_ ((Lisp_Object));
7398 static int hscroll_windows P_ ((Lisp_Object));
7399
7400 /* For all leaf windows in the window tree rooted at WINDOW, set their
7401 hscroll value so that PT is (i) visible in the window, and (ii) so
7402 that it is not within a certain margin at the window's left and
7403 right border. Value is non-zero if any window's hscroll has been
7404 changed. */
7405
7406 static int
7407 hscroll_window_tree (window)
7408 Lisp_Object window;
7409 {
7410 int hscrolled_p = 0;
7411
7412 while (WINDOWP (window))
7413 {
7414 struct window *w = XWINDOW (window);
7415
7416 if (WINDOWP (w->hchild))
7417 hscrolled_p |= hscroll_window_tree (w->hchild);
7418 else if (WINDOWP (w->vchild))
7419 hscrolled_p |= hscroll_window_tree (w->vchild);
7420 else if (w->cursor.vpos >= 0)
7421 {
7422 int hscroll_margin, text_area_x, text_area_y;
7423 int text_area_width, text_area_height;
7424 struct glyph_row *current_cursor_row
7425 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7426 struct glyph_row *desired_cursor_row
7427 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7428 struct glyph_row *cursor_row
7429 = (desired_cursor_row->enabled_p
7430 ? desired_cursor_row
7431 : current_cursor_row);
7432
7433 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7434 &text_area_width, &text_area_height);
7435
7436 /* Scroll when cursor is inside this scroll margin. */
7437 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7438
7439 if ((XFASTINT (w->hscroll)
7440 && w->cursor.x < hscroll_margin)
7441 || (cursor_row->enabled_p
7442 && cursor_row->truncated_on_right_p
7443 && (w->cursor.x > text_area_width - hscroll_margin)))
7444 {
7445 struct it it;
7446 int hscroll;
7447 struct buffer *saved_current_buffer;
7448 int pt;
7449
7450 /* Find point in a display of infinite width. */
7451 saved_current_buffer = current_buffer;
7452 current_buffer = XBUFFER (w->buffer);
7453
7454 if (w == XWINDOW (selected_window))
7455 pt = BUF_PT (current_buffer);
7456 else
7457 {
7458 pt = marker_position (w->pointm);
7459 pt = max (BEGV, pt);
7460 pt = min (ZV, pt);
7461 }
7462
7463 /* Move iterator to pt starting at cursor_row->start in
7464 a line with infinite width. */
7465 init_to_row_start (&it, w, cursor_row);
7466 it.last_visible_x = INFINITY;
7467 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7468 current_buffer = saved_current_buffer;
7469
7470 /* Center cursor in window. */
7471 hscroll = (max (0, it.current_x - text_area_width / 2)
7472 / CANON_X_UNIT (it.f));
7473
7474 /* Don't call Fset_window_hscroll if value hasn't
7475 changed because it will prevent redisplay
7476 optimizations. */
7477 if (XFASTINT (w->hscroll) != hscroll)
7478 {
7479 Fset_window_hscroll (window, make_number (hscroll));
7480 hscrolled_p = 1;
7481 }
7482 }
7483 }
7484
7485 window = w->next;
7486 }
7487
7488 /* Value is non-zero if hscroll of any leaf window has been changed. */
7489 return hscrolled_p;
7490 }
7491
7492
7493 /* Set hscroll so that cursor is visible and not inside horizontal
7494 scroll margins for all windows in the tree rooted at WINDOW. See
7495 also hscroll_window_tree above. Value is non-zero if any window's
7496 hscroll has been changed. If it has, desired matrices on the frame
7497 of WINDOW are cleared. */
7498
7499 static int
7500 hscroll_windows (window)
7501 Lisp_Object window;
7502 {
7503 int hscrolled_p;
7504
7505 if (automatic_hscrolling_p)
7506 {
7507 hscrolled_p = hscroll_window_tree (window);
7508 if (hscrolled_p)
7509 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7510 }
7511 else
7512 hscrolled_p = 0;
7513 return hscrolled_p;
7514 }
7515
7516
7517 \f
7518 /************************************************************************
7519 Redisplay
7520 ************************************************************************/
7521
7522 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7523 to a non-zero value. This is sometimes handy to have in a debugger
7524 session. */
7525
7526 #if GLYPH_DEBUG
7527
7528 /* First and last unchanged row for try_window_id. */
7529
7530 int debug_first_unchanged_at_end_vpos;
7531 int debug_last_unchanged_at_beg_vpos;
7532
7533 /* Delta vpos and y. */
7534
7535 int debug_dvpos, debug_dy;
7536
7537 /* Delta in characters and bytes for try_window_id. */
7538
7539 int debug_delta, debug_delta_bytes;
7540
7541 /* Values of window_end_pos and window_end_vpos at the end of
7542 try_window_id. */
7543
7544 int debug_end_pos, debug_end_vpos;
7545
7546 /* Append a string to W->desired_matrix->method. FMT is a printf
7547 format string. A1...A9 are a supplement for a variable-length
7548 argument list. If trace_redisplay_p is non-zero also printf the
7549 resulting string to stderr. */
7550
7551 static void
7552 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7553 struct window *w;
7554 char *fmt;
7555 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7556 {
7557 char buffer[512];
7558 char *method = w->desired_matrix->method;
7559 int len = strlen (method);
7560 int size = sizeof w->desired_matrix->method;
7561 int remaining = size - len - 1;
7562
7563 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7564 if (len && remaining)
7565 {
7566 method[len] = '|';
7567 --remaining, ++len;
7568 }
7569
7570 strncpy (method + len, buffer, remaining);
7571
7572 if (trace_redisplay_p)
7573 fprintf (stderr, "%p (%s): %s\n",
7574 w,
7575 ((BUFFERP (w->buffer)
7576 && STRINGP (XBUFFER (w->buffer)->name))
7577 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7578 : "no buffer"),
7579 buffer);
7580 }
7581
7582 #endif /* GLYPH_DEBUG */
7583
7584
7585 /* This counter is used to clear the face cache every once in a while
7586 in redisplay_internal. It is incremented for each redisplay.
7587 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7588 cleared. */
7589
7590 #define CLEAR_FACE_CACHE_COUNT 10000
7591 static int clear_face_cache_count;
7592
7593 /* Record the previous terminal frame we displayed. */
7594
7595 static struct frame *previous_terminal_frame;
7596
7597 /* Non-zero while redisplay_internal is in progress. */
7598
7599 int redisplaying_p;
7600
7601
7602 /* Value is non-zero if all changes in window W, which displays
7603 current_buffer, are in the text between START and END. START is a
7604 buffer position, END is given as a distance from Z. Used in
7605 redisplay_internal for display optimization. */
7606
7607 static INLINE int
7608 text_outside_line_unchanged_p (w, start, end)
7609 struct window *w;
7610 int start, end;
7611 {
7612 int unchanged_p = 1;
7613
7614 /* If text or overlays have changed, see where. */
7615 if (XFASTINT (w->last_modified) < MODIFF
7616 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7617 {
7618 /* Gap in the line? */
7619 if (GPT < start || Z - GPT < end)
7620 unchanged_p = 0;
7621
7622 /* Changes start in front of the line, or end after it? */
7623 if (unchanged_p
7624 && (BEG_UNCHANGED < start - 1
7625 || END_UNCHANGED < end))
7626 unchanged_p = 0;
7627
7628 /* If selective display, can't optimize if changes start at the
7629 beginning of the line. */
7630 if (unchanged_p
7631 && INTEGERP (current_buffer->selective_display)
7632 && XINT (current_buffer->selective_display) > 0
7633 && (BEG_UNCHANGED < start || GPT <= start))
7634 unchanged_p = 0;
7635 }
7636
7637 return unchanged_p;
7638 }
7639
7640
7641 /* Do a frame update, taking possible shortcuts into account. This is
7642 the main external entry point for redisplay.
7643
7644 If the last redisplay displayed an echo area message and that message
7645 is no longer requested, we clear the echo area or bring back the
7646 mini-buffer if that is in use. */
7647
7648 void
7649 redisplay ()
7650 {
7651 redisplay_internal (0);
7652 }
7653
7654 /* Return 1 if point moved out of or into a composition. Otherwise
7655 return 0. PREV_BUF and PREV_PT are the last point buffer and
7656 position. BUF and PT are the current point buffer and position. */
7657
7658 int
7659 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7660 struct buffer *prev_buf, *buf;
7661 int prev_pt, pt;
7662 {
7663 int start, end;
7664 Lisp_Object prop;
7665 Lisp_Object buffer;
7666
7667 XSETBUFFER (buffer, buf);
7668 /* Check a composition at the last point if point moved within the
7669 same buffer. */
7670 if (prev_buf == buf)
7671 {
7672 if (prev_pt == pt)
7673 /* Point didn't move. */
7674 return 0;
7675
7676 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7677 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7678 && COMPOSITION_VALID_P (start, end, prop)
7679 && start < prev_pt && end > prev_pt)
7680 /* The last point was within the composition. Return 1 iff
7681 point moved out of the composition. */
7682 return (pt <= start || pt >= end);
7683 }
7684
7685 /* Check a composition at the current point. */
7686 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7687 && find_composition (pt, -1, &start, &end, &prop, buffer)
7688 && COMPOSITION_VALID_P (start, end, prop)
7689 && start < pt && end > pt);
7690 }
7691
7692 /* Reconsider the setting of B->clip_changed which is displayed
7693 in window W. */
7694
7695 static INLINE void
7696 reconsider_clip_changes (w, b)
7697 struct window *w;
7698 struct buffer *b;
7699 {
7700 if (b->prevent_redisplay_optimizations_p)
7701 b->clip_changed = 1;
7702 else if (b->clip_changed
7703 && !NILP (w->window_end_valid)
7704 && w->current_matrix->buffer == b
7705 && w->current_matrix->zv == BUF_ZV (b)
7706 && w->current_matrix->begv == BUF_BEGV (b))
7707 b->clip_changed = 0;
7708
7709 /* If display wasn't paused, and W is not a tool bar window, see if
7710 point has been moved into or out of a composition. In that case,
7711 we set b->clip_changed to 1 to force updating the screen. If
7712 b->clip_changed has already been set to 1, we can skip this
7713 check. */
7714 if (!b->clip_changed
7715 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7716 {
7717 int pt;
7718
7719 if (w == XWINDOW (selected_window))
7720 pt = BUF_PT (current_buffer);
7721 else
7722 pt = marker_position (w->pointm);
7723
7724 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7725 || pt != XINT (w->last_point))
7726 && check_point_in_composition (w->current_matrix->buffer,
7727 XINT (w->last_point),
7728 XBUFFER (w->buffer), pt))
7729 b->clip_changed = 1;
7730 }
7731 }
7732
7733
7734 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7735 response to any user action; therefore, we should preserve the echo
7736 area. (Actually, our caller does that job.) Perhaps in the future
7737 avoid recentering windows if it is not necessary; currently that
7738 causes some problems. */
7739
7740 static void
7741 redisplay_internal (preserve_echo_area)
7742 int preserve_echo_area;
7743 {
7744 struct window *w = XWINDOW (selected_window);
7745 struct frame *f = XFRAME (w->frame);
7746 int pause;
7747 int must_finish = 0;
7748 struct text_pos tlbufpos, tlendpos;
7749 int number_of_visible_frames;
7750 int count;
7751 struct frame *sf = SELECTED_FRAME ();
7752
7753 /* Non-zero means redisplay has to consider all windows on all
7754 frames. Zero means, only selected_window is considered. */
7755 int consider_all_windows_p;
7756
7757 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7758
7759 /* No redisplay if running in batch mode or frame is not yet fully
7760 initialized, or redisplay is explicitly turned off by setting
7761 Vinhibit_redisplay. */
7762 if (noninteractive
7763 || !NILP (Vinhibit_redisplay)
7764 || !f->glyphs_initialized_p)
7765 return;
7766
7767 /* The flag redisplay_performed_directly_p is set by
7768 direct_output_for_insert when it already did the whole screen
7769 update necessary. */
7770 if (redisplay_performed_directly_p)
7771 {
7772 redisplay_performed_directly_p = 0;
7773 if (!hscroll_windows (selected_window))
7774 return;
7775 }
7776
7777 #ifdef USE_X_TOOLKIT
7778 if (popup_activated ())
7779 return;
7780 #endif
7781
7782 /* I don't think this happens but let's be paranoid. */
7783 if (redisplaying_p)
7784 return;
7785
7786 /* Record a function that resets redisplaying_p to its old value
7787 when we leave this function. */
7788 count = specpdl_ptr - specpdl;
7789 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7790 ++redisplaying_p;
7791
7792 retry:
7793 pause = 0;
7794 reconsider_clip_changes (w, current_buffer);
7795
7796 /* If new fonts have been loaded that make a glyph matrix adjustment
7797 necessary, do it. */
7798 if (fonts_changed_p)
7799 {
7800 adjust_glyphs (NULL);
7801 ++windows_or_buffers_changed;
7802 fonts_changed_p = 0;
7803 }
7804
7805 if (! FRAME_WINDOW_P (sf)
7806 && previous_terminal_frame != sf)
7807 {
7808 /* Since frames on an ASCII terminal share the same display
7809 area, displaying a different frame means redisplay the whole
7810 thing. */
7811 windows_or_buffers_changed++;
7812 SET_FRAME_GARBAGED (sf);
7813 XSETFRAME (Vterminal_frame, sf);
7814 }
7815 previous_terminal_frame = sf;
7816
7817 /* Set the visible flags for all frames. Do this before checking
7818 for resized or garbaged frames; they want to know if their frames
7819 are visible. See the comment in frame.h for
7820 FRAME_SAMPLE_VISIBILITY. */
7821 {
7822 Lisp_Object tail, frame;
7823
7824 number_of_visible_frames = 0;
7825
7826 FOR_EACH_FRAME (tail, frame)
7827 {
7828 struct frame *f = XFRAME (frame);
7829
7830 FRAME_SAMPLE_VISIBILITY (f);
7831 if (FRAME_VISIBLE_P (f))
7832 ++number_of_visible_frames;
7833 clear_desired_matrices (f);
7834 }
7835 }
7836
7837 /* Notice any pending interrupt request to change frame size. */
7838 do_pending_window_change (1);
7839
7840 /* Clear frames marked as garbaged. */
7841 if (frame_garbaged)
7842 clear_garbaged_frames ();
7843
7844 /* Build menubar and tool-bar items. */
7845 prepare_menu_bars ();
7846
7847 if (windows_or_buffers_changed)
7848 update_mode_lines++;
7849
7850 /* Detect case that we need to write or remove a star in the mode line. */
7851 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7852 {
7853 w->update_mode_line = Qt;
7854 if (buffer_shared > 1)
7855 update_mode_lines++;
7856 }
7857
7858 /* If %c is in the mode line, update it if needed. */
7859 if (!NILP (w->column_number_displayed)
7860 /* This alternative quickly identifies a common case
7861 where no change is needed. */
7862 && !(PT == XFASTINT (w->last_point)
7863 && XFASTINT (w->last_modified) >= MODIFF
7864 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7865 && XFASTINT (w->column_number_displayed) != current_column ())
7866 w->update_mode_line = Qt;
7867
7868 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7869
7870 /* The variable buffer_shared is set in redisplay_window and
7871 indicates that we redisplay a buffer in different windows. See
7872 there. */
7873 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7874
7875 /* If specs for an arrow have changed, do thorough redisplay
7876 to ensure we remove any arrow that should no longer exist. */
7877 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7878 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7879 consider_all_windows_p = windows_or_buffers_changed = 1;
7880
7881 /* Normally the message* functions will have already displayed and
7882 updated the echo area, but the frame may have been trashed, or
7883 the update may have been preempted, so display the echo area
7884 again here. Checking both message buffers captures the case that
7885 the echo area should be cleared. */
7886 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7887 {
7888 int window_height_changed_p = echo_area_display (0);
7889 must_finish = 1;
7890
7891 if (fonts_changed_p)
7892 goto retry;
7893 else if (window_height_changed_p)
7894 {
7895 consider_all_windows_p = 1;
7896 ++update_mode_lines;
7897 ++windows_or_buffers_changed;
7898
7899 /* If window configuration was changed, frames may have been
7900 marked garbaged. Clear them or we will experience
7901 surprises wrt scrolling. */
7902 if (frame_garbaged)
7903 clear_garbaged_frames ();
7904 }
7905 }
7906 else if (EQ (selected_window, minibuf_window)
7907 && (current_buffer->clip_changed
7908 || XFASTINT (w->last_modified) < MODIFF
7909 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7910 && resize_mini_window (w, 0))
7911 {
7912 /* Resized active mini-window to fit the size of what it is
7913 showing if its contents might have changed. */
7914 must_finish = 1;
7915 consider_all_windows_p = 1;
7916 ++windows_or_buffers_changed;
7917 ++update_mode_lines;
7918
7919 /* If window configuration was changed, frames may have been
7920 marked garbaged. Clear them or we will experience
7921 surprises wrt scrolling. */
7922 if (frame_garbaged)
7923 clear_garbaged_frames ();
7924 }
7925
7926
7927 /* If showing the region, and mark has changed, we must redisplay
7928 the whole window. The assignment to this_line_start_pos prevents
7929 the optimization directly below this if-statement. */
7930 if (((!NILP (Vtransient_mark_mode)
7931 && !NILP (XBUFFER (w->buffer)->mark_active))
7932 != !NILP (w->region_showing))
7933 || (!NILP (w->region_showing)
7934 && !EQ (w->region_showing,
7935 Fmarker_position (XBUFFER (w->buffer)->mark))))
7936 CHARPOS (this_line_start_pos) = 0;
7937
7938 /* Optimize the case that only the line containing the cursor in the
7939 selected window has changed. Variables starting with this_ are
7940 set in display_line and record information about the line
7941 containing the cursor. */
7942 tlbufpos = this_line_start_pos;
7943 tlendpos = this_line_end_pos;
7944 if (!consider_all_windows_p
7945 && CHARPOS (tlbufpos) > 0
7946 && NILP (w->update_mode_line)
7947 && !current_buffer->clip_changed
7948 && FRAME_VISIBLE_P (XFRAME (w->frame))
7949 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7950 /* Make sure recorded data applies to current buffer, etc. */
7951 && this_line_buffer == current_buffer
7952 && current_buffer == XBUFFER (w->buffer)
7953 && NILP (w->force_start)
7954 /* Point must be on the line that we have info recorded about. */
7955 && PT >= CHARPOS (tlbufpos)
7956 && PT <= Z - CHARPOS (tlendpos)
7957 /* All text outside that line, including its final newline,
7958 must be unchanged */
7959 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7960 CHARPOS (tlendpos)))
7961 {
7962 if (CHARPOS (tlbufpos) > BEGV
7963 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7964 && (CHARPOS (tlbufpos) == ZV
7965 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7966 /* Former continuation line has disappeared by becoming empty */
7967 goto cancel;
7968 else if (XFASTINT (w->last_modified) < MODIFF
7969 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7970 || MINI_WINDOW_P (w))
7971 {
7972 /* We have to handle the case of continuation around a
7973 wide-column character (See the comment in indent.c around
7974 line 885).
7975
7976 For instance, in the following case:
7977
7978 -------- Insert --------
7979 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7980 J_I_ ==> J_I_ `^^' are cursors.
7981 ^^ ^^
7982 -------- --------
7983
7984 As we have to redraw the line above, we should goto cancel. */
7985
7986 struct it it;
7987 int line_height_before = this_line_pixel_height;
7988
7989 /* Note that start_display will handle the case that the
7990 line starting at tlbufpos is a continuation lines. */
7991 start_display (&it, w, tlbufpos);
7992
7993 /* Implementation note: It this still necessary? */
7994 if (it.current_x != this_line_start_x)
7995 goto cancel;
7996
7997 TRACE ((stderr, "trying display optimization 1\n"));
7998 w->cursor.vpos = -1;
7999 overlay_arrow_seen = 0;
8000 it.vpos = this_line_vpos;
8001 it.current_y = this_line_y;
8002 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8003 display_line (&it);
8004
8005 /* If line contains point, is not continued,
8006 and ends at same distance from eob as before, we win */
8007 if (w->cursor.vpos >= 0
8008 /* Line is not continued, otherwise this_line_start_pos
8009 would have been set to 0 in display_line. */
8010 && CHARPOS (this_line_start_pos)
8011 /* Line ends as before. */
8012 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8013 /* Line has same height as before. Otherwise other lines
8014 would have to be shifted up or down. */
8015 && this_line_pixel_height == line_height_before)
8016 {
8017 /* If this is not the window's last line, we must adjust
8018 the charstarts of the lines below. */
8019 if (it.current_y < it.last_visible_y)
8020 {
8021 struct glyph_row *row
8022 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8023 int delta, delta_bytes;
8024
8025 if (Z - CHARPOS (tlendpos) == ZV)
8026 {
8027 /* This line ends at end of (accessible part of)
8028 buffer. There is no newline to count. */
8029 delta = (Z
8030 - CHARPOS (tlendpos)
8031 - MATRIX_ROW_START_CHARPOS (row));
8032 delta_bytes = (Z_BYTE
8033 - BYTEPOS (tlendpos)
8034 - MATRIX_ROW_START_BYTEPOS (row));
8035 }
8036 else
8037 {
8038 /* This line ends in a newline. Must take
8039 account of the newline and the rest of the
8040 text that follows. */
8041 delta = (Z
8042 - CHARPOS (tlendpos)
8043 - MATRIX_ROW_START_CHARPOS (row));
8044 delta_bytes = (Z_BYTE
8045 - BYTEPOS (tlendpos)
8046 - MATRIX_ROW_START_BYTEPOS (row));
8047 }
8048
8049 increment_matrix_positions (w->current_matrix,
8050 this_line_vpos + 1,
8051 w->current_matrix->nrows,
8052 delta, delta_bytes);
8053 }
8054
8055 /* If this row displays text now but previously didn't,
8056 or vice versa, w->window_end_vpos may have to be
8057 adjusted. */
8058 if ((it.glyph_row - 1)->displays_text_p)
8059 {
8060 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8061 XSETINT (w->window_end_vpos, this_line_vpos);
8062 }
8063 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8064 && this_line_vpos > 0)
8065 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8066 w->window_end_valid = Qnil;
8067
8068 /* Update hint: No need to try to scroll in update_window. */
8069 w->desired_matrix->no_scrolling_p = 1;
8070
8071 #if GLYPH_DEBUG
8072 *w->desired_matrix->method = 0;
8073 debug_method_add (w, "optimization 1");
8074 #endif
8075 goto update;
8076 }
8077 else
8078 goto cancel;
8079 }
8080 else if (/* Cursor position hasn't changed. */
8081 PT == XFASTINT (w->last_point)
8082 /* Make sure the cursor was last displayed
8083 in this window. Otherwise we have to reposition it. */
8084 && 0 <= w->cursor.vpos
8085 && XINT (w->height) > w->cursor.vpos)
8086 {
8087 if (!must_finish)
8088 {
8089 do_pending_window_change (1);
8090
8091 /* We used to always goto end_of_redisplay here, but this
8092 isn't enough if we have a blinking cursor. */
8093 if (w->cursor_off_p == w->last_cursor_off_p)
8094 goto end_of_redisplay;
8095 }
8096 goto update;
8097 }
8098 /* If highlighting the region, or if the cursor is in the echo area,
8099 then we can't just move the cursor. */
8100 else if (! (!NILP (Vtransient_mark_mode)
8101 && !NILP (current_buffer->mark_active))
8102 && (EQ (selected_window, current_buffer->last_selected_window)
8103 || highlight_nonselected_windows)
8104 && NILP (w->region_showing)
8105 && NILP (Vshow_trailing_whitespace)
8106 && !cursor_in_echo_area)
8107 {
8108 struct it it;
8109 struct glyph_row *row;
8110
8111 /* Skip from tlbufpos to PT and see where it is. Note that
8112 PT may be in invisible text. If so, we will end at the
8113 next visible position. */
8114 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8115 NULL, DEFAULT_FACE_ID);
8116 it.current_x = this_line_start_x;
8117 it.current_y = this_line_y;
8118 it.vpos = this_line_vpos;
8119
8120 /* The call to move_it_to stops in front of PT, but
8121 moves over before-strings. */
8122 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8123
8124 if (it.vpos == this_line_vpos
8125 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8126 row->enabled_p))
8127 {
8128 xassert (this_line_vpos == it.vpos);
8129 xassert (this_line_y == it.current_y);
8130 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8131 goto update;
8132 }
8133 else
8134 goto cancel;
8135 }
8136
8137 cancel:
8138 /* Text changed drastically or point moved off of line. */
8139 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8140 }
8141
8142 CHARPOS (this_line_start_pos) = 0;
8143 consider_all_windows_p |= buffer_shared > 1;
8144 ++clear_face_cache_count;
8145
8146
8147 /* Build desired matrices, and update the display. If
8148 consider_all_windows_p is non-zero, do it for all windows on all
8149 frames. Otherwise do it for selected_window, only. */
8150
8151 if (consider_all_windows_p)
8152 {
8153 Lisp_Object tail, frame;
8154
8155 /* Clear the face cache eventually. */
8156 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8157 {
8158 clear_face_cache (0);
8159 clear_face_cache_count = 0;
8160 }
8161
8162 /* Recompute # windows showing selected buffer. This will be
8163 incremented each time such a window is displayed. */
8164 buffer_shared = 0;
8165
8166 FOR_EACH_FRAME (tail, frame)
8167 {
8168 struct frame *f = XFRAME (frame);
8169
8170 if (FRAME_WINDOW_P (f) || f == sf)
8171 {
8172 /* Mark all the scroll bars to be removed; we'll redeem
8173 the ones we want when we redisplay their windows. */
8174 if (condemn_scroll_bars_hook)
8175 (*condemn_scroll_bars_hook) (f);
8176
8177 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8178 redisplay_windows (FRAME_ROOT_WINDOW (f));
8179
8180 /* Any scroll bars which redisplay_windows should have
8181 nuked should now go away. */
8182 if (judge_scroll_bars_hook)
8183 (*judge_scroll_bars_hook) (f);
8184
8185 /* If fonts changed, display again. */
8186 if (fonts_changed_p)
8187 goto retry;
8188
8189 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8190 {
8191 /* See if we have to hscroll. */
8192 if (hscroll_windows (f->root_window))
8193 goto retry;
8194
8195 /* Prevent various kinds of signals during display
8196 update. stdio is not robust about handling
8197 signals, which can cause an apparent I/O
8198 error. */
8199 if (interrupt_input)
8200 unrequest_sigio ();
8201 stop_polling ();
8202
8203 /* Update the display. */
8204 set_window_update_flags (XWINDOW (f->root_window), 1);
8205 pause |= update_frame (f, 0, 0);
8206 if (pause)
8207 break;
8208
8209 mark_window_display_accurate (f->root_window, 1);
8210 if (frame_up_to_date_hook)
8211 frame_up_to_date_hook (f);
8212 }
8213 }
8214 }
8215 }
8216 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8217 {
8218 Lisp_Object mini_window;
8219 struct frame *mini_frame;
8220
8221 redisplay_window (selected_window, 1);
8222
8223 /* Compare desired and current matrices, perform output. */
8224 update:
8225
8226 /* If fonts changed, display again. */
8227 if (fonts_changed_p)
8228 goto retry;
8229
8230 /* Prevent various kinds of signals during display update.
8231 stdio is not robust about handling signals,
8232 which can cause an apparent I/O error. */
8233 if (interrupt_input)
8234 unrequest_sigio ();
8235 stop_polling ();
8236
8237 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8238 {
8239 if (hscroll_windows (selected_window))
8240 goto retry;
8241
8242 XWINDOW (selected_window)->must_be_updated_p = 1;
8243 pause = update_frame (sf, 0, 0);
8244 }
8245
8246 /* We may have called echo_area_display at the top of this
8247 function. If the echo area is on another frame, that may
8248 have put text on a frame other than the selected one, so the
8249 above call to update_frame would not have caught it. Catch
8250 it here. */
8251 mini_window = FRAME_MINIBUF_WINDOW (sf);
8252 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8253
8254 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8255 {
8256 XWINDOW (mini_window)->must_be_updated_p = 1;
8257 pause |= update_frame (mini_frame, 0, 0);
8258 if (!pause && hscroll_windows (mini_window))
8259 goto retry;
8260 }
8261 }
8262
8263 /* If display was paused because of pending input, make sure we do a
8264 thorough update the next time. */
8265 if (pause)
8266 {
8267 /* Prevent the optimization at the beginning of
8268 redisplay_internal that tries a single-line update of the
8269 line containing the cursor in the selected window. */
8270 CHARPOS (this_line_start_pos) = 0;
8271
8272 /* Let the overlay arrow be updated the next time. */
8273 if (!NILP (last_arrow_position))
8274 {
8275 last_arrow_position = Qt;
8276 last_arrow_string = Qt;
8277 }
8278
8279 /* If we pause after scrolling, some rows in the current
8280 matrices of some windows are not valid. */
8281 if (!WINDOW_FULL_WIDTH_P (w)
8282 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8283 update_mode_lines = 1;
8284 }
8285
8286 /* Now text on frame agrees with windows, so put info into the
8287 windows for partial redisplay to follow. */
8288 if (!pause)
8289 {
8290 register struct buffer *b = XBUFFER (w->buffer);
8291
8292 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8293 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8294 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8295 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8296
8297 if (consider_all_windows_p)
8298 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8299 else
8300 {
8301 XSETFASTINT (w->last_point, BUF_PT (b));
8302 w->last_cursor = w->cursor;
8303 w->last_cursor_off_p = w->cursor_off_p;
8304
8305 b->clip_changed = 0;
8306 b->prevent_redisplay_optimizations_p = 0;
8307 w->update_mode_line = Qnil;
8308 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8309 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8310 w->last_had_star
8311 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8312 ? Qt : Qnil);
8313
8314 /* Record if we are showing a region, so can make sure to
8315 update it fully at next redisplay. */
8316 w->region_showing = (!NILP (Vtransient_mark_mode)
8317 && (EQ (selected_window,
8318 current_buffer->last_selected_window)
8319 || highlight_nonselected_windows)
8320 && !NILP (XBUFFER (w->buffer)->mark_active)
8321 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8322 : Qnil);
8323
8324 w->window_end_valid = w->buffer;
8325 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8326 last_arrow_string = Voverlay_arrow_string;
8327 if (frame_up_to_date_hook != 0)
8328 (*frame_up_to_date_hook) (sf);
8329
8330 w->current_matrix->buffer = b;
8331 w->current_matrix->begv = BUF_BEGV (b);
8332 w->current_matrix->zv = BUF_ZV (b);
8333 }
8334
8335 update_mode_lines = 0;
8336 windows_or_buffers_changed = 0;
8337 }
8338
8339 /* Start SIGIO interrupts coming again. Having them off during the
8340 code above makes it less likely one will discard output, but not
8341 impossible, since there might be stuff in the system buffer here.
8342 But it is much hairier to try to do anything about that. */
8343 if (interrupt_input)
8344 request_sigio ();
8345 start_polling ();
8346
8347 /* If a frame has become visible which was not before, redisplay
8348 again, so that we display it. Expose events for such a frame
8349 (which it gets when becoming visible) don't call the parts of
8350 redisplay constructing glyphs, so simply exposing a frame won't
8351 display anything in this case. So, we have to display these
8352 frames here explicitly. */
8353 if (!pause)
8354 {
8355 Lisp_Object tail, frame;
8356 int new_count = 0;
8357
8358 FOR_EACH_FRAME (tail, frame)
8359 {
8360 int this_is_visible = 0;
8361
8362 if (XFRAME (frame)->visible)
8363 this_is_visible = 1;
8364 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8365 if (XFRAME (frame)->visible)
8366 this_is_visible = 1;
8367
8368 if (this_is_visible)
8369 new_count++;
8370 }
8371
8372 if (new_count != number_of_visible_frames)
8373 windows_or_buffers_changed++;
8374 }
8375
8376 /* Change frame size now if a change is pending. */
8377 do_pending_window_change (1);
8378
8379 /* If we just did a pending size change, or have additional
8380 visible frames, redisplay again. */
8381 if (windows_or_buffers_changed && !pause)
8382 goto retry;
8383
8384 end_of_redisplay:;
8385
8386 unbind_to (count, Qnil);
8387 }
8388
8389
8390 /* Redisplay, but leave alone any recent echo area message unless
8391 another message has been requested in its place.
8392
8393 This is useful in situations where you need to redisplay but no
8394 user action has occurred, making it inappropriate for the message
8395 area to be cleared. See tracking_off and
8396 wait_reading_process_input for examples of these situations. */
8397
8398 void
8399 redisplay_preserve_echo_area ()
8400 {
8401 if (!NILP (echo_area_buffer[1]))
8402 {
8403 /* We have a previously displayed message, but no current
8404 message. Redisplay the previous message. */
8405 display_last_displayed_message_p = 1;
8406 redisplay_internal (1);
8407 display_last_displayed_message_p = 0;
8408 }
8409 else
8410 redisplay_internal (1);
8411 }
8412
8413
8414 /* Function registered with record_unwind_protect in
8415 redisplay_internal. Clears the flag indicating that a redisplay is
8416 in progress. */
8417
8418 static Lisp_Object
8419 unwind_redisplay (old_redisplaying_p)
8420 Lisp_Object old_redisplaying_p;
8421 {
8422 redisplaying_p = XFASTINT (old_redisplaying_p);
8423 return Qnil;
8424 }
8425
8426
8427 /* Mark the display of windows in the window tree rooted at WINDOW as
8428 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8429 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8430 the next time redisplay_internal is called. */
8431
8432 void
8433 mark_window_display_accurate (window, accurate_p)
8434 Lisp_Object window;
8435 int accurate_p;
8436 {
8437 struct window *w;
8438
8439 for (; !NILP (window); window = w->next)
8440 {
8441 w = XWINDOW (window);
8442
8443 if (BUFFERP (w->buffer))
8444 {
8445 struct buffer *b = XBUFFER (w->buffer);
8446
8447 XSETFASTINT (w->last_modified,
8448 accurate_p ? BUF_MODIFF (b) : 0);
8449 XSETFASTINT (w->last_overlay_modified,
8450 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8451 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8452 ? Qt : Qnil);
8453
8454 #if 0 /* I don't think this is necessary because display_line does it.
8455 Let's check it. */
8456 /* Record if we are showing a region, so can make sure to
8457 update it fully at next redisplay. */
8458 w->region_showing
8459 = (!NILP (Vtransient_mark_mode)
8460 && (w == XWINDOW (current_buffer->last_selected_window)
8461 || highlight_nonselected_windows)
8462 && (!NILP (b->mark_active)
8463 ? Fmarker_position (b->mark)
8464 : Qnil));
8465 #endif
8466
8467 if (accurate_p)
8468 {
8469 b->clip_changed = 0;
8470 b->prevent_redisplay_optimizations_p = 0;
8471 w->current_matrix->buffer = b;
8472 w->current_matrix->begv = BUF_BEGV (b);
8473 w->current_matrix->zv = BUF_ZV (b);
8474 w->last_cursor = w->cursor;
8475 w->last_cursor_off_p = w->cursor_off_p;
8476 if (w == XWINDOW (selected_window))
8477 w->last_point = make_number (BUF_PT (b));
8478 else
8479 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8480 }
8481 }
8482
8483 w->window_end_valid = w->buffer;
8484 w->update_mode_line = Qnil;
8485
8486 if (!NILP (w->vchild))
8487 mark_window_display_accurate (w->vchild, accurate_p);
8488 if (!NILP (w->hchild))
8489 mark_window_display_accurate (w->hchild, accurate_p);
8490 }
8491
8492 if (accurate_p)
8493 {
8494 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8495 last_arrow_string = Voverlay_arrow_string;
8496 }
8497 else
8498 {
8499 /* Force a thorough redisplay the next time by setting
8500 last_arrow_position and last_arrow_string to t, which is
8501 unequal to any useful value of Voverlay_arrow_... */
8502 last_arrow_position = Qt;
8503 last_arrow_string = Qt;
8504 }
8505 }
8506
8507
8508 /* Return value in display table DP (Lisp_Char_Table *) for character
8509 C. Since a display table doesn't have any parent, we don't have to
8510 follow parent. Do not call this function directly but use the
8511 macro DISP_CHAR_VECTOR. */
8512
8513 Lisp_Object
8514 disp_char_vector (dp, c)
8515 struct Lisp_Char_Table *dp;
8516 int c;
8517 {
8518 int code[4], i;
8519 Lisp_Object val;
8520
8521 if (SINGLE_BYTE_CHAR_P (c))
8522 return (dp->contents[c]);
8523
8524 SPLIT_CHAR (c, code[0], code[1], code[2]);
8525 if (code[1] < 32)
8526 code[1] = -1;
8527 else if (code[2] < 32)
8528 code[2] = -1;
8529
8530 /* Here, the possible range of code[0] (== charset ID) is
8531 128..max_charset. Since the top level char table contains data
8532 for multibyte characters after 256th element, we must increment
8533 code[0] by 128 to get a correct index. */
8534 code[0] += 128;
8535 code[3] = -1; /* anchor */
8536
8537 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8538 {
8539 val = dp->contents[code[i]];
8540 if (!SUB_CHAR_TABLE_P (val))
8541 return (NILP (val) ? dp->defalt : val);
8542 }
8543
8544 /* Here, val is a sub char table. We return the default value of
8545 it. */
8546 return (dp->defalt);
8547 }
8548
8549
8550 \f
8551 /***********************************************************************
8552 Window Redisplay
8553 ***********************************************************************/
8554
8555 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8556
8557 static void
8558 redisplay_windows (window)
8559 Lisp_Object window;
8560 {
8561 while (!NILP (window))
8562 {
8563 struct window *w = XWINDOW (window);
8564
8565 if (!NILP (w->hchild))
8566 redisplay_windows (w->hchild);
8567 else if (!NILP (w->vchild))
8568 redisplay_windows (w->vchild);
8569 else
8570 redisplay_window (window, 0);
8571
8572 window = w->next;
8573 }
8574 }
8575
8576
8577 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8578 DELTA is the number of bytes by which positions recorded in ROW
8579 differ from current buffer positions. */
8580
8581 void
8582 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8583 struct window *w;
8584 struct glyph_row *row;
8585 struct glyph_matrix *matrix;
8586 int delta, delta_bytes, dy, dvpos;
8587 {
8588 struct glyph *glyph = row->glyphs[TEXT_AREA];
8589 struct glyph *end = glyph + row->used[TEXT_AREA];
8590 int x = row->x;
8591 int pt_old = PT - delta;
8592
8593 /* Skip over glyphs not having an object at the start of the row.
8594 These are special glyphs like truncation marks on terminal
8595 frames. */
8596 if (row->displays_text_p)
8597 while (glyph < end
8598 && INTEGERP (glyph->object)
8599 && glyph->charpos < 0)
8600 {
8601 x += glyph->pixel_width;
8602 ++glyph;
8603 }
8604
8605 while (glyph < end
8606 && !INTEGERP (glyph->object)
8607 && (!BUFFERP (glyph->object)
8608 || glyph->charpos < pt_old))
8609 {
8610 x += glyph->pixel_width;
8611 ++glyph;
8612 }
8613
8614 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8615 w->cursor.x = x;
8616 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8617 w->cursor.y = row->y + dy;
8618
8619 if (w == XWINDOW (selected_window))
8620 {
8621 if (!row->continued_p
8622 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8623 && row->x == 0)
8624 {
8625 this_line_buffer = XBUFFER (w->buffer);
8626
8627 CHARPOS (this_line_start_pos)
8628 = MATRIX_ROW_START_CHARPOS (row) + delta;
8629 BYTEPOS (this_line_start_pos)
8630 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8631
8632 CHARPOS (this_line_end_pos)
8633 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8634 BYTEPOS (this_line_end_pos)
8635 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8636
8637 this_line_y = w->cursor.y;
8638 this_line_pixel_height = row->height;
8639 this_line_vpos = w->cursor.vpos;
8640 this_line_start_x = row->x;
8641 }
8642 else
8643 CHARPOS (this_line_start_pos) = 0;
8644 }
8645 }
8646
8647
8648 /* Run window scroll functions, if any, for WINDOW with new window
8649 start STARTP. Sets the window start of WINDOW to that position.
8650
8651 We assume that the window's buffer is really current. */
8652
8653 static INLINE struct text_pos
8654 run_window_scroll_functions (window, startp)
8655 Lisp_Object window;
8656 struct text_pos startp;
8657 {
8658 struct window *w = XWINDOW (window);
8659 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8660
8661 if (current_buffer != XBUFFER (w->buffer))
8662 abort ();
8663
8664 if (!NILP (Vwindow_scroll_functions))
8665 {
8666 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8667 make_number (CHARPOS (startp)));
8668 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8669 /* In case the hook functions switch buffers. */
8670 if (current_buffer != XBUFFER (w->buffer))
8671 set_buffer_internal_1 (XBUFFER (w->buffer));
8672 }
8673
8674 return startp;
8675 }
8676
8677
8678 /* Modify the desired matrix of window W and W->vscroll so that the
8679 line containing the cursor is fully visible. */
8680
8681 static void
8682 make_cursor_line_fully_visible (w)
8683 struct window *w;
8684 {
8685 struct glyph_matrix *matrix;
8686 struct glyph_row *row;
8687 int window_height, header_line_height;
8688
8689 /* It's not always possible to find the cursor, e.g, when a window
8690 is full of overlay strings. Don't do anything in that case. */
8691 if (w->cursor.vpos < 0)
8692 return;
8693
8694 matrix = w->desired_matrix;
8695 row = MATRIX_ROW (matrix, w->cursor.vpos);
8696
8697 /* If the cursor row is not partially visible, there's nothing
8698 to do. */
8699 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8700 return;
8701
8702 /* If the row the cursor is in is taller than the window's height,
8703 it's not clear what to do, so do nothing. */
8704 window_height = window_box_height (w);
8705 if (row->height >= window_height)
8706 return;
8707
8708 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8709 {
8710 int dy = row->height - row->visible_height;
8711 w->vscroll = 0;
8712 w->cursor.y += dy;
8713 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8714 }
8715 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8716 {
8717 int dy = - (row->height - row->visible_height);
8718 w->vscroll = dy;
8719 w->cursor.y += dy;
8720 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8721 }
8722
8723 /* When we change the cursor y-position of the selected window,
8724 change this_line_y as well so that the display optimization for
8725 the cursor line of the selected window in redisplay_internal uses
8726 the correct y-position. */
8727 if (w == XWINDOW (selected_window))
8728 this_line_y = w->cursor.y;
8729 }
8730
8731
8732 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8733 non-zero means only WINDOW is redisplayed in redisplay_internal.
8734 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8735 in redisplay_window to bring a partially visible line into view in
8736 the case that only the cursor has moved.
8737
8738 Value is
8739
8740 1 if scrolling succeeded
8741
8742 0 if scrolling didn't find point.
8743
8744 -1 if new fonts have been loaded so that we must interrupt
8745 redisplay, adjust glyph matrices, and try again. */
8746
8747 static int
8748 try_scrolling (window, just_this_one_p, scroll_conservatively,
8749 scroll_step, temp_scroll_step)
8750 Lisp_Object window;
8751 int just_this_one_p;
8752 int scroll_conservatively, scroll_step;
8753 int temp_scroll_step;
8754 {
8755 struct window *w = XWINDOW (window);
8756 struct frame *f = XFRAME (w->frame);
8757 struct text_pos scroll_margin_pos;
8758 struct text_pos pos;
8759 struct text_pos startp;
8760 struct it it;
8761 Lisp_Object window_end;
8762 int this_scroll_margin;
8763 int dy = 0;
8764 int scroll_max;
8765 int rc;
8766 int amount_to_scroll = 0;
8767 Lisp_Object aggressive;
8768 int height;
8769
8770 #if GLYPH_DEBUG
8771 debug_method_add (w, "try_scrolling");
8772 #endif
8773
8774 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8775
8776 /* Compute scroll margin height in pixels. We scroll when point is
8777 within this distance from the top or bottom of the window. */
8778 if (scroll_margin > 0)
8779 {
8780 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8781 this_scroll_margin *= CANON_Y_UNIT (f);
8782 }
8783 else
8784 this_scroll_margin = 0;
8785
8786 /* Compute how much we should try to scroll maximally to bring point
8787 into view. */
8788 if (scroll_step || scroll_conservatively || temp_scroll_step)
8789 scroll_max = max (scroll_step,
8790 max (scroll_conservatively, temp_scroll_step));
8791 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8792 || NUMBERP (current_buffer->scroll_up_aggressively))
8793 /* We're trying to scroll because of aggressive scrolling
8794 but no scroll_step is set. Choose an arbitrary one. Maybe
8795 there should be a variable for this. */
8796 scroll_max = 10;
8797 else
8798 scroll_max = 0;
8799 scroll_max *= CANON_Y_UNIT (f);
8800
8801 /* Decide whether we have to scroll down. Start at the window end
8802 and move this_scroll_margin up to find the position of the scroll
8803 margin. */
8804 window_end = Fwindow_end (window, Qt);
8805 CHARPOS (scroll_margin_pos) = XINT (window_end);
8806 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8807 if (this_scroll_margin)
8808 {
8809 start_display (&it, w, scroll_margin_pos);
8810 move_it_vertically (&it, - this_scroll_margin);
8811 scroll_margin_pos = it.current.pos;
8812 }
8813
8814 if (PT >= CHARPOS (scroll_margin_pos))
8815 {
8816 int y0;
8817 #if 0
8818 int line_height;
8819 #endif
8820
8821 /* Point is in the scroll margin at the bottom of the window, or
8822 below. Compute a new window start that makes point visible. */
8823
8824 /* Compute the distance from the scroll margin to PT.
8825 Give up if the distance is greater than scroll_max. */
8826 start_display (&it, w, scroll_margin_pos);
8827 y0 = it.current_y;
8828 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8829 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8830 #if 0 /* Taking the line's height into account here looks wrong. */
8831 line_height = (it.max_ascent + it.max_descent
8832 ? it.max_ascent + it.max_descent
8833 : last_height);
8834 dy = it.current_y + line_height - y0;
8835 #else
8836 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8837 end, which is one line below the window. The iterator's
8838 current_y will be same as y0 in that case, but we have to
8839 scroll a line to make PT visible. That's the reason why 1 is
8840 added below. */
8841 dy = 1 + it.current_y - y0;
8842 #endif
8843
8844 if (dy > scroll_max)
8845 return 0;
8846
8847 /* Move the window start down. If scrolling conservatively,
8848 move it just enough down to make point visible. If
8849 scroll_step is set, move it down by scroll_step. */
8850 start_display (&it, w, startp);
8851
8852 if (scroll_conservatively)
8853 amount_to_scroll =
8854 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8855 else if (scroll_step || temp_scroll_step)
8856 amount_to_scroll = scroll_max;
8857 else
8858 {
8859 aggressive = current_buffer->scroll_down_aggressively;
8860 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8861 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8862 if (NUMBERP (aggressive))
8863 amount_to_scroll = XFLOATINT (aggressive) * height;
8864 }
8865
8866 if (amount_to_scroll <= 0)
8867 return 0;
8868
8869 move_it_vertically (&it, amount_to_scroll);
8870 startp = it.current.pos;
8871 }
8872 else
8873 {
8874 /* See if point is inside the scroll margin at the top of the
8875 window. */
8876 scroll_margin_pos = startp;
8877 if (this_scroll_margin)
8878 {
8879 start_display (&it, w, startp);
8880 move_it_vertically (&it, this_scroll_margin);
8881 scroll_margin_pos = it.current.pos;
8882 }
8883
8884 if (PT < CHARPOS (scroll_margin_pos))
8885 {
8886 /* Point is in the scroll margin at the top of the window or
8887 above what is displayed in the window. */
8888 int y0;
8889
8890 /* Compute the vertical distance from PT to the scroll
8891 margin position. Give up if distance is greater than
8892 scroll_max. */
8893 SET_TEXT_POS (pos, PT, PT_BYTE);
8894 start_display (&it, w, pos);
8895 y0 = it.current_y;
8896 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8897 it.last_visible_y, -1,
8898 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8899 dy = it.current_y - y0;
8900 if (dy > scroll_max)
8901 return 0;
8902
8903 /* Compute new window start. */
8904 start_display (&it, w, startp);
8905
8906 if (scroll_conservatively)
8907 amount_to_scroll =
8908 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8909 else if (scroll_step || temp_scroll_step)
8910 amount_to_scroll = scroll_max;
8911 else
8912 {
8913 aggressive = current_buffer->scroll_up_aggressively;
8914 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8915 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8916 if (NUMBERP (aggressive))
8917 amount_to_scroll = XFLOATINT (aggressive) * height;
8918 }
8919
8920 if (amount_to_scroll <= 0)
8921 return 0;
8922
8923 move_it_vertically (&it, - amount_to_scroll);
8924 startp = it.current.pos;
8925 }
8926 }
8927
8928 /* Run window scroll functions. */
8929 startp = run_window_scroll_functions (window, startp);
8930
8931 /* Display the window. Give up if new fonts are loaded, or if point
8932 doesn't appear. */
8933 if (!try_window (window, startp))
8934 rc = -1;
8935 else if (w->cursor.vpos < 0)
8936 {
8937 clear_glyph_matrix (w->desired_matrix);
8938 rc = 0;
8939 }
8940 else
8941 {
8942 /* Maybe forget recorded base line for line number display. */
8943 if (!just_this_one_p
8944 || current_buffer->clip_changed
8945 || BEG_UNCHANGED < CHARPOS (startp))
8946 w->base_line_number = Qnil;
8947
8948 /* If cursor ends up on a partially visible line, shift display
8949 lines up or down. */
8950 make_cursor_line_fully_visible (w);
8951 rc = 1;
8952 }
8953
8954 return rc;
8955 }
8956
8957
8958 /* Compute a suitable window start for window W if display of W starts
8959 on a continuation line. Value is non-zero if a new window start
8960 was computed.
8961
8962 The new window start will be computed, based on W's width, starting
8963 from the start of the continued line. It is the start of the
8964 screen line with the minimum distance from the old start W->start. */
8965
8966 static int
8967 compute_window_start_on_continuation_line (w)
8968 struct window *w;
8969 {
8970 struct text_pos pos, start_pos;
8971 int window_start_changed_p = 0;
8972
8973 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8974
8975 /* If window start is on a continuation line... Window start may be
8976 < BEGV in case there's invisible text at the start of the
8977 buffer (M-x rmail, for example). */
8978 if (CHARPOS (start_pos) > BEGV
8979 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8980 {
8981 struct it it;
8982 struct glyph_row *row;
8983
8984 /* Handle the case that the window start is out of range. */
8985 if (CHARPOS (start_pos) < BEGV)
8986 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8987 else if (CHARPOS (start_pos) > ZV)
8988 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8989
8990 /* Find the start of the continued line. This should be fast
8991 because scan_buffer is fast (newline cache). */
8992 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8993 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8994 row, DEFAULT_FACE_ID);
8995 reseat_at_previous_visible_line_start (&it);
8996
8997 /* If the line start is "too far" away from the window start,
8998 say it takes too much time to compute a new window start. */
8999 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9000 < XFASTINT (w->height) * XFASTINT (w->width))
9001 {
9002 int min_distance, distance;
9003
9004 /* Move forward by display lines to find the new window
9005 start. If window width was enlarged, the new start can
9006 be expected to be > the old start. If window width was
9007 decreased, the new window start will be < the old start.
9008 So, we're looking for the display line start with the
9009 minimum distance from the old window start. */
9010 pos = it.current.pos;
9011 min_distance = INFINITY;
9012 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9013 distance < min_distance)
9014 {
9015 min_distance = distance;
9016 pos = it.current.pos;
9017 move_it_by_lines (&it, 1, 0);
9018 }
9019
9020 /* Set the window start there. */
9021 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9022 window_start_changed_p = 1;
9023 }
9024 }
9025
9026 return window_start_changed_p;
9027 }
9028
9029
9030 /* Try cursor movement in case text has not changes in window WINDOW,
9031 with window start STARTP. Value is
9032
9033 1 if successful
9034
9035 0 if this method cannot be used
9036
9037 -1 if we know we have to scroll the display. *SCROLL_STEP is
9038 set to 1, under certain circumstances, if we want to scroll as
9039 if scroll-step were set to 1. See the code. */
9040
9041 static int
9042 try_cursor_movement (window, startp, scroll_step)
9043 Lisp_Object window;
9044 struct text_pos startp;
9045 int *scroll_step;
9046 {
9047 struct window *w = XWINDOW (window);
9048 struct frame *f = XFRAME (w->frame);
9049 int rc = 0;
9050
9051 /* Handle case where text has not changed, only point, and it has
9052 not moved off the frame. */
9053 if (/* Point may be in this window. */
9054 PT >= CHARPOS (startp)
9055 /* If we don't check this, we are called to move the cursor in a
9056 horizontally split window with a current matrix that doesn't
9057 fit the display. */
9058 && !windows_or_buffers_changed
9059 /* Selective display hasn't changed. */
9060 && !current_buffer->clip_changed
9061 /* If force-mode-line-update was called, really redisplay;
9062 that's how redisplay is forced after e.g. changing
9063 buffer-invisibility-spec. */
9064 && NILP (w->update_mode_line)
9065 /* Can't use this case if highlighting a region. When a
9066 region exists, cursor movement has to do more than just
9067 set the cursor. */
9068 && !(!NILP (Vtransient_mark_mode)
9069 && !NILP (current_buffer->mark_active))
9070 && NILP (w->region_showing)
9071 && NILP (Vshow_trailing_whitespace)
9072 /* Right after splitting windows, last_point may be nil. */
9073 && INTEGERP (w->last_point)
9074 /* This code is not used for mini-buffer for the sake of the case
9075 of redisplaying to replace an echo area message; since in
9076 that case the mini-buffer contents per se are usually
9077 unchanged. This code is of no real use in the mini-buffer
9078 since the handling of this_line_start_pos, etc., in redisplay
9079 handles the same cases. */
9080 && !EQ (window, minibuf_window)
9081 /* When splitting windows or for new windows, it happens that
9082 redisplay is called with a nil window_end_vpos or one being
9083 larger than the window. This should really be fixed in
9084 window.c. I don't have this on my list, now, so we do
9085 approximately the same as the old redisplay code. --gerd. */
9086 && INTEGERP (w->window_end_vpos)
9087 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9088 && (FRAME_WINDOW_P (f)
9089 || !MARKERP (Voverlay_arrow_position)
9090 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9091 {
9092 int this_scroll_margin;
9093 struct glyph_row *row;
9094
9095 #if GLYPH_DEBUG
9096 debug_method_add (w, "cursor movement");
9097 #endif
9098
9099 /* Scroll if point within this distance from the top or bottom
9100 of the window. This is a pixel value. */
9101 this_scroll_margin = max (0, scroll_margin);
9102 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9103 this_scroll_margin *= CANON_Y_UNIT (f);
9104
9105 /* Start with the row the cursor was displayed during the last
9106 not paused redisplay. Give up if that row is not valid. */
9107 if (w->last_cursor.vpos < 0
9108 || w->last_cursor.vpos >= w->current_matrix->nrows)
9109 rc = -1;
9110 else
9111 {
9112 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9113 if (row->mode_line_p)
9114 ++row;
9115 if (!row->enabled_p)
9116 rc = -1;
9117 }
9118
9119 if (rc == 0)
9120 {
9121 int scroll_p = 0;
9122 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9123
9124
9125 if (PT > XFASTINT (w->last_point))
9126 {
9127 /* Point has moved forward. */
9128 while (MATRIX_ROW_END_CHARPOS (row) < PT
9129 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9130 {
9131 xassert (row->enabled_p);
9132 ++row;
9133 }
9134
9135 /* The end position of a row equals the start position
9136 of the next row. If PT is there, we would rather
9137 display it in the next line. */
9138 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9139 && MATRIX_ROW_END_CHARPOS (row) == PT
9140 && !cursor_row_p (w, row))
9141 ++row;
9142
9143 /* If within the scroll margin, scroll. Note that
9144 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9145 the next line would be drawn, and that
9146 this_scroll_margin can be zero. */
9147 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9148 || PT > MATRIX_ROW_END_CHARPOS (row)
9149 /* Line is completely visible last line in window
9150 and PT is to be set in the next line. */
9151 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9152 && PT == MATRIX_ROW_END_CHARPOS (row)
9153 && !row->ends_at_zv_p
9154 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9155 scroll_p = 1;
9156 }
9157 else if (PT < XFASTINT (w->last_point))
9158 {
9159 /* Cursor has to be moved backward. Note that PT >=
9160 CHARPOS (startp) because of the outer
9161 if-statement. */
9162 while (!row->mode_line_p
9163 && (MATRIX_ROW_START_CHARPOS (row) > PT
9164 || (MATRIX_ROW_START_CHARPOS (row) == PT
9165 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9166 && (row->y > this_scroll_margin
9167 || CHARPOS (startp) == BEGV))
9168 {
9169 xassert (row->enabled_p);
9170 --row;
9171 }
9172
9173 /* Consider the following case: Window starts at BEGV,
9174 there is invisible, intangible text at BEGV, so that
9175 display starts at some point START > BEGV. It can
9176 happen that we are called with PT somewhere between
9177 BEGV and START. Try to handle that case. */
9178 if (row < w->current_matrix->rows
9179 || row->mode_line_p)
9180 {
9181 row = w->current_matrix->rows;
9182 if (row->mode_line_p)
9183 ++row;
9184 }
9185
9186 /* Due to newlines in overlay strings, we may have to
9187 skip forward over overlay strings. */
9188 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9189 && MATRIX_ROW_END_CHARPOS (row) == PT
9190 && !cursor_row_p (w, row))
9191 ++row;
9192
9193 /* If within the scroll margin, scroll. */
9194 if (row->y < this_scroll_margin
9195 && CHARPOS (startp) != BEGV)
9196 scroll_p = 1;
9197 }
9198
9199 if (PT < MATRIX_ROW_START_CHARPOS (row)
9200 || PT > MATRIX_ROW_END_CHARPOS (row))
9201 {
9202 /* if PT is not in the glyph row, give up. */
9203 rc = -1;
9204 }
9205 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9206 {
9207 /* If we end up in a partially visible line, let's make it
9208 fully visible, except when it's taller than the window,
9209 in which case we can't do much about it. */
9210 if (row->height > window_box_height (w))
9211 {
9212 *scroll_step = 1;
9213 rc = -1;
9214 }
9215 else
9216 {
9217 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9218 try_window (window, startp);
9219 make_cursor_line_fully_visible (w);
9220 rc = 1;
9221 }
9222 }
9223 else if (scroll_p)
9224 rc = -1;
9225 else
9226 {
9227 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9228 rc = 1;
9229 }
9230 }
9231 }
9232
9233 return rc;
9234 }
9235
9236
9237 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9238 selected_window is redisplayed. */
9239
9240 static void
9241 redisplay_window (window, just_this_one_p)
9242 Lisp_Object window;
9243 int just_this_one_p;
9244 {
9245 struct window *w = XWINDOW (window);
9246 struct frame *f = XFRAME (w->frame);
9247 struct buffer *buffer = XBUFFER (w->buffer);
9248 struct buffer *old = current_buffer;
9249 struct text_pos lpoint, opoint, startp;
9250 int update_mode_line;
9251 int tem;
9252 struct it it;
9253 /* Record it now because it's overwritten. */
9254 int current_matrix_up_to_date_p = 0;
9255 int temp_scroll_step = 0;
9256 int count = specpdl_ptr - specpdl;
9257 int rc;
9258
9259 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9260 opoint = lpoint;
9261
9262 /* W must be a leaf window here. */
9263 xassert (!NILP (w->buffer));
9264 #if GLYPH_DEBUG
9265 *w->desired_matrix->method = 0;
9266 #endif
9267
9268 specbind (Qinhibit_point_motion_hooks, Qt);
9269
9270 reconsider_clip_changes (w, buffer);
9271
9272 /* Has the mode line to be updated? */
9273 update_mode_line = (!NILP (w->update_mode_line)
9274 || update_mode_lines
9275 || buffer->clip_changed);
9276
9277 if (MINI_WINDOW_P (w))
9278 {
9279 if (w == XWINDOW (echo_area_window)
9280 && !NILP (echo_area_buffer[0]))
9281 {
9282 if (update_mode_line)
9283 /* We may have to update a tty frame's menu bar or a
9284 tool-bar. Example `M-x C-h C-h C-g'. */
9285 goto finish_menu_bars;
9286 else
9287 /* We've already displayed the echo area glyphs in this window. */
9288 goto finish_scroll_bars;
9289 }
9290 else if (w != XWINDOW (minibuf_window))
9291 {
9292 /* W is a mini-buffer window, but it's not the currently
9293 active one, so clear it. */
9294 int yb = window_text_bottom_y (w);
9295 struct glyph_row *row;
9296 int y;
9297
9298 for (y = 0, row = w->desired_matrix->rows;
9299 y < yb;
9300 y += row->height, ++row)
9301 blank_row (w, row, y);
9302 goto finish_scroll_bars;
9303 }
9304 }
9305
9306 /* Otherwise set up data on this window; select its buffer and point
9307 value. */
9308 /* Really select the buffer, for the sake of buffer-local
9309 variables. */
9310 set_buffer_internal_1 (XBUFFER (w->buffer));
9311 SET_TEXT_POS (opoint, PT, PT_BYTE);
9312
9313 current_matrix_up_to_date_p
9314 = (!NILP (w->window_end_valid)
9315 && !current_buffer->clip_changed
9316 && XFASTINT (w->last_modified) >= MODIFF
9317 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9318
9319 /* When windows_or_buffers_changed is non-zero, we can't rely on
9320 the window end being valid, so set it to nil there. */
9321 if (windows_or_buffers_changed)
9322 {
9323 /* If window starts on a continuation line, maybe adjust the
9324 window start in case the window's width changed. */
9325 if (XMARKER (w->start)->buffer == current_buffer)
9326 compute_window_start_on_continuation_line (w);
9327
9328 w->window_end_valid = Qnil;
9329 }
9330
9331 /* Some sanity checks. */
9332 CHECK_WINDOW_END (w);
9333 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9334 abort ();
9335 if (BYTEPOS (opoint) < CHARPOS (opoint))
9336 abort ();
9337
9338 /* If %c is in mode line, update it if needed. */
9339 if (!NILP (w->column_number_displayed)
9340 /* This alternative quickly identifies a common case
9341 where no change is needed. */
9342 && !(PT == XFASTINT (w->last_point)
9343 && XFASTINT (w->last_modified) >= MODIFF
9344 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9345 && XFASTINT (w->column_number_displayed) != current_column ())
9346 update_mode_line = 1;
9347
9348 /* Count number of windows showing the selected buffer. An indirect
9349 buffer counts as its base buffer. */
9350 if (!just_this_one_p)
9351 {
9352 struct buffer *current_base, *window_base;
9353 current_base = current_buffer;
9354 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9355 if (current_base->base_buffer)
9356 current_base = current_base->base_buffer;
9357 if (window_base->base_buffer)
9358 window_base = window_base->base_buffer;
9359 if (current_base == window_base)
9360 buffer_shared++;
9361 }
9362
9363 /* Point refers normally to the selected window. For any other
9364 window, set up appropriate value. */
9365 if (!EQ (window, selected_window))
9366 {
9367 int new_pt = XMARKER (w->pointm)->charpos;
9368 int new_pt_byte = marker_byte_position (w->pointm);
9369 if (new_pt < BEGV)
9370 {
9371 new_pt = BEGV;
9372 new_pt_byte = BEGV_BYTE;
9373 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9374 }
9375 else if (new_pt > (ZV - 1))
9376 {
9377 new_pt = ZV;
9378 new_pt_byte = ZV_BYTE;
9379 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9380 }
9381
9382 /* We don't use SET_PT so that the point-motion hooks don't run. */
9383 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9384 }
9385
9386 /* If any of the character widths specified in the display table
9387 have changed, invalidate the width run cache. It's true that
9388 this may be a bit late to catch such changes, but the rest of
9389 redisplay goes (non-fatally) haywire when the display table is
9390 changed, so why should we worry about doing any better? */
9391 if (current_buffer->width_run_cache)
9392 {
9393 struct Lisp_Char_Table *disptab = buffer_display_table ();
9394
9395 if (! disptab_matches_widthtab (disptab,
9396 XVECTOR (current_buffer->width_table)))
9397 {
9398 invalidate_region_cache (current_buffer,
9399 current_buffer->width_run_cache,
9400 BEG, Z);
9401 recompute_width_table (current_buffer, disptab);
9402 }
9403 }
9404
9405 /* If window-start is screwed up, choose a new one. */
9406 if (XMARKER (w->start)->buffer != current_buffer)
9407 goto recenter;
9408
9409 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9410
9411 /* If someone specified a new starting point but did not insist,
9412 check whether it can be used. */
9413 if (!NILP (w->optional_new_start)
9414 && CHARPOS (startp) >= BEGV
9415 && CHARPOS (startp) <= ZV)
9416 {
9417 w->optional_new_start = Qnil;
9418 start_display (&it, w, startp);
9419 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9420 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9421 if (IT_CHARPOS (it) == PT)
9422 w->force_start = Qt;
9423 }
9424
9425 /* Handle case where place to start displaying has been specified,
9426 unless the specified location is outside the accessible range. */
9427 if (!NILP (w->force_start)
9428 || w->frozen_window_start_p)
9429 {
9430 w->force_start = Qnil;
9431 w->vscroll = 0;
9432 w->window_end_valid = Qnil;
9433
9434 /* Forget any recorded base line for line number display. */
9435 if (!current_matrix_up_to_date_p
9436 || current_buffer->clip_changed)
9437 w->base_line_number = Qnil;
9438
9439 /* Redisplay the mode line. Select the buffer properly for that.
9440 Also, run the hook window-scroll-functions
9441 because we have scrolled. */
9442 /* Note, we do this after clearing force_start because
9443 if there's an error, it is better to forget about force_start
9444 than to get into an infinite loop calling the hook functions
9445 and having them get more errors. */
9446 if (!update_mode_line
9447 || ! NILP (Vwindow_scroll_functions))
9448 {
9449 update_mode_line = 1;
9450 w->update_mode_line = Qt;
9451 startp = run_window_scroll_functions (window, startp);
9452 }
9453
9454 XSETFASTINT (w->last_modified, 0);
9455 XSETFASTINT (w->last_overlay_modified, 0);
9456 if (CHARPOS (startp) < BEGV)
9457 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9458 else if (CHARPOS (startp) > ZV)
9459 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9460
9461 /* Redisplay, then check if cursor has been set during the
9462 redisplay. Give up if new fonts were loaded. */
9463 if (!try_window (window, startp))
9464 {
9465 w->force_start = Qt;
9466 clear_glyph_matrix (w->desired_matrix);
9467 goto restore_buffers;
9468 }
9469
9470 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9471 {
9472 /* If point does not appear, try to move point so it does
9473 appear. The desired matrix has been built above, so we
9474 can use it here. */
9475 int window_height;
9476 struct glyph_row *row;
9477
9478 window_height = window_box_height (w) / 2;
9479 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9480 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9481 ++row;
9482
9483 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9484 MATRIX_ROW_START_BYTEPOS (row));
9485
9486 if (w != XWINDOW (selected_window))
9487 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9488 else if (current_buffer == old)
9489 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9490
9491 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9492
9493 /* If we are highlighting the region, then we just changed
9494 the region, so redisplay to show it. */
9495 if (!NILP (Vtransient_mark_mode)
9496 && !NILP (current_buffer->mark_active))
9497 {
9498 clear_glyph_matrix (w->desired_matrix);
9499 if (!try_window (window, startp))
9500 goto restore_buffers;
9501 }
9502 }
9503
9504 make_cursor_line_fully_visible (w);
9505 #if GLYPH_DEBUG
9506 debug_method_add (w, "forced window start");
9507 #endif
9508 goto done;
9509 }
9510
9511 /* Handle case where text has not changed, only point, and it has
9512 not moved off the frame. */
9513 if (current_matrix_up_to_date_p
9514 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9515 rc != 0))
9516 {
9517 if (rc == -1)
9518 goto try_to_scroll;
9519 else
9520 goto done;
9521 }
9522 /* If current starting point was originally the beginning of a line
9523 but no longer is, find a new starting point. */
9524 else if (!NILP (w->start_at_line_beg)
9525 && !(CHARPOS (startp) <= BEGV
9526 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9527 {
9528 #if GLYPH_DEBUG
9529 debug_method_add (w, "recenter 1");
9530 #endif
9531 goto recenter;
9532 }
9533
9534 /* Try scrolling with try_window_id. */
9535 else if (/* Windows and buffers haven't changed. */
9536 !windows_or_buffers_changed
9537 /* Window must be either use window-based redisplay or
9538 be full width. */
9539 && (FRAME_WINDOW_P (f)
9540 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9541 && !MINI_WINDOW_P (w)
9542 /* Point is not known NOT to appear in window. */
9543 && PT >= CHARPOS (startp)
9544 && XFASTINT (w->last_modified)
9545 /* Window is not hscrolled. */
9546 && XFASTINT (w->hscroll) == 0
9547 /* Selective display has not changed. */
9548 && !current_buffer->clip_changed
9549 /* Current matrix is up to date. */
9550 && !NILP (w->window_end_valid)
9551 /* Can't use this case if highlighting a region because
9552 a cursor movement will do more than just set the cursor. */
9553 && !(!NILP (Vtransient_mark_mode)
9554 && !NILP (current_buffer->mark_active))
9555 && NILP (w->region_showing)
9556 && NILP (Vshow_trailing_whitespace)
9557 /* Overlay arrow position and string not changed. */
9558 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9559 && EQ (last_arrow_string, Voverlay_arrow_string)
9560 /* Value is > 0 if update has been done, it is -1 if we
9561 know that the same window start will not work. It is 0
9562 if unsuccessful for some other reason. */
9563 && (tem = try_window_id (w)) != 0)
9564 {
9565 #if GLYPH_DEBUG
9566 debug_method_add (w, "try_window_id %d", tem);
9567 #endif
9568
9569 if (fonts_changed_p)
9570 goto restore_buffers;
9571 if (tem > 0)
9572 goto done;
9573
9574 /* Otherwise try_window_id has returned -1 which means that we
9575 don't want the alternative below this comment to execute. */
9576 }
9577 else if (CHARPOS (startp) >= BEGV
9578 && CHARPOS (startp) <= ZV
9579 && PT >= CHARPOS (startp)
9580 && (CHARPOS (startp) < ZV
9581 /* Avoid starting at end of buffer. */
9582 || CHARPOS (startp) == BEGV
9583 || (XFASTINT (w->last_modified) >= MODIFF
9584 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9585 {
9586 #if GLYPH_DEBUG
9587 debug_method_add (w, "same window start");
9588 #endif
9589
9590 /* Try to redisplay starting at same place as before.
9591 If point has not moved off frame, accept the results. */
9592 if (!current_matrix_up_to_date_p
9593 /* Don't use try_window_reusing_current_matrix in this case
9594 because a window scroll function can have changed the
9595 buffer. */
9596 || !NILP (Vwindow_scroll_functions)
9597 || MINI_WINDOW_P (w)
9598 || !try_window_reusing_current_matrix (w))
9599 {
9600 IF_DEBUG (debug_method_add (w, "1"));
9601 try_window (window, startp);
9602 }
9603
9604 if (fonts_changed_p)
9605 goto restore_buffers;
9606
9607 if (w->cursor.vpos >= 0)
9608 {
9609 if (!just_this_one_p
9610 || current_buffer->clip_changed
9611 || BEG_UNCHANGED < CHARPOS (startp))
9612 /* Forget any recorded base line for line number display. */
9613 w->base_line_number = Qnil;
9614
9615 make_cursor_line_fully_visible (w);
9616 goto done;
9617 }
9618 else
9619 clear_glyph_matrix (w->desired_matrix);
9620 }
9621
9622 try_to_scroll:
9623
9624 XSETFASTINT (w->last_modified, 0);
9625 XSETFASTINT (w->last_overlay_modified, 0);
9626
9627 /* Redisplay the mode line. Select the buffer properly for that. */
9628 if (!update_mode_line)
9629 {
9630 update_mode_line = 1;
9631 w->update_mode_line = Qt;
9632 }
9633
9634 /* Try to scroll by specified few lines. */
9635 if ((scroll_conservatively
9636 || scroll_step
9637 || temp_scroll_step
9638 || NUMBERP (current_buffer->scroll_up_aggressively)
9639 || NUMBERP (current_buffer->scroll_down_aggressively))
9640 && !current_buffer->clip_changed
9641 && CHARPOS (startp) >= BEGV
9642 && CHARPOS (startp) <= ZV)
9643 {
9644 /* The function returns -1 if new fonts were loaded, 1 if
9645 successful, 0 if not successful. */
9646 int rc = try_scrolling (window, just_this_one_p,
9647 scroll_conservatively,
9648 scroll_step,
9649 temp_scroll_step);
9650 if (rc > 0)
9651 goto done;
9652 else if (rc < 0)
9653 goto restore_buffers;
9654 }
9655
9656 /* Finally, just choose place to start which centers point */
9657
9658 recenter:
9659
9660 #if GLYPH_DEBUG
9661 debug_method_add (w, "recenter");
9662 #endif
9663
9664 /* w->vscroll = 0; */
9665
9666 /* Forget any previously recorded base line for line number display. */
9667 if (!current_matrix_up_to_date_p
9668 || current_buffer->clip_changed)
9669 w->base_line_number = Qnil;
9670
9671 /* Move backward half the height of the window. */
9672 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9673 it.current_y = it.last_visible_y;
9674 move_it_vertically_backward (&it, it.last_visible_y / 2);
9675 xassert (IT_CHARPOS (it) >= BEGV);
9676
9677 /* The function move_it_vertically_backward may move over more
9678 than the specified y-distance. If it->w is small, e.g. a
9679 mini-buffer window, we may end up in front of the window's
9680 display area. Start displaying at the start of the line
9681 containing PT in this case. */
9682 if (it.current_y <= 0)
9683 {
9684 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9685 move_it_vertically (&it, 0);
9686 xassert (IT_CHARPOS (it) <= PT);
9687 it.current_y = 0;
9688 }
9689
9690 it.current_x = it.hpos = 0;
9691
9692 /* Set startp here explicitly in case that helps avoid an infinite loop
9693 in case the window-scroll-functions functions get errors. */
9694 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9695
9696 /* Run scroll hooks. */
9697 startp = run_window_scroll_functions (window, it.current.pos);
9698
9699 /* Redisplay the window. */
9700 if (!current_matrix_up_to_date_p
9701 || windows_or_buffers_changed
9702 /* Don't use try_window_reusing_current_matrix in this case
9703 because it can have changed the buffer. */
9704 || !NILP (Vwindow_scroll_functions)
9705 || !just_this_one_p
9706 || MINI_WINDOW_P (w)
9707 || !try_window_reusing_current_matrix (w))
9708 try_window (window, startp);
9709
9710 /* If new fonts have been loaded (due to fontsets), give up. We
9711 have to start a new redisplay since we need to re-adjust glyph
9712 matrices. */
9713 if (fonts_changed_p)
9714 goto restore_buffers;
9715
9716 /* If cursor did not appear assume that the middle of the window is
9717 in the first line of the window. Do it again with the next line.
9718 (Imagine a window of height 100, displaying two lines of height
9719 60. Moving back 50 from it->last_visible_y will end in the first
9720 line.) */
9721 if (w->cursor.vpos < 0)
9722 {
9723 if (!NILP (w->window_end_valid)
9724 && PT >= Z - XFASTINT (w->window_end_pos))
9725 {
9726 clear_glyph_matrix (w->desired_matrix);
9727 move_it_by_lines (&it, 1, 0);
9728 try_window (window, it.current.pos);
9729 }
9730 else if (PT < IT_CHARPOS (it))
9731 {
9732 clear_glyph_matrix (w->desired_matrix);
9733 move_it_by_lines (&it, -1, 0);
9734 try_window (window, it.current.pos);
9735 }
9736 else
9737 {
9738 /* Not much we can do about it. */
9739 }
9740 }
9741
9742 /* Consider the following case: Window starts at BEGV, there is
9743 invisible, intangible text at BEGV, so that display starts at
9744 some point START > BEGV. It can happen that we are called with
9745 PT somewhere between BEGV and START. Try to handle that case. */
9746 if (w->cursor.vpos < 0)
9747 {
9748 struct glyph_row *row = w->current_matrix->rows;
9749 if (row->mode_line_p)
9750 ++row;
9751 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9752 }
9753
9754 make_cursor_line_fully_visible (w);
9755
9756 done:
9757
9758 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9759 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9760 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9761 ? Qt : Qnil);
9762
9763 /* Display the mode line, if we must. */
9764 if ((update_mode_line
9765 /* If window not full width, must redo its mode line
9766 if (a) the window to its side is being redone and
9767 (b) we do a frame-based redisplay. This is a consequence
9768 of how inverted lines are drawn in frame-based redisplay. */
9769 || (!just_this_one_p
9770 && !FRAME_WINDOW_P (f)
9771 && !WINDOW_FULL_WIDTH_P (w))
9772 /* Line number to display. */
9773 || INTEGERP (w->base_line_pos)
9774 /* Column number is displayed and different from the one displayed. */
9775 || (!NILP (w->column_number_displayed)
9776 && XFASTINT (w->column_number_displayed) != current_column ()))
9777 /* This means that the window has a mode line. */
9778 && (WINDOW_WANTS_MODELINE_P (w)
9779 || WINDOW_WANTS_HEADER_LINE_P (w)))
9780 {
9781 Lisp_Object old_selected_frame;
9782
9783 old_selected_frame = selected_frame;
9784
9785 XSETFRAME (selected_frame, f);
9786 display_mode_lines (w);
9787 selected_frame = old_selected_frame;
9788
9789 /* If mode line height has changed, arrange for a thorough
9790 immediate redisplay using the correct mode line height. */
9791 if (WINDOW_WANTS_MODELINE_P (w)
9792 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9793 {
9794 fonts_changed_p = 1;
9795 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9796 = DESIRED_MODE_LINE_HEIGHT (w);
9797 }
9798
9799 /* If top line height has changed, arrange for a thorough
9800 immediate redisplay using the correct mode line height. */
9801 if (WINDOW_WANTS_HEADER_LINE_P (w)
9802 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9803 {
9804 fonts_changed_p = 1;
9805 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9806 = DESIRED_HEADER_LINE_HEIGHT (w);
9807 }
9808
9809 if (fonts_changed_p)
9810 goto restore_buffers;
9811 }
9812
9813 if (!line_number_displayed
9814 && !BUFFERP (w->base_line_pos))
9815 {
9816 w->base_line_pos = Qnil;
9817 w->base_line_number = Qnil;
9818 }
9819
9820 finish_menu_bars:
9821
9822 /* When we reach a frame's selected window, redo the frame's menu bar. */
9823 if (update_mode_line
9824 && EQ (FRAME_SELECTED_WINDOW (f), window))
9825 {
9826 int redisplay_menu_p = 0;
9827
9828 if (FRAME_WINDOW_P (f))
9829 {
9830 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9831 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9832 #else
9833 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9834 #endif
9835 }
9836 else
9837 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9838
9839 if (redisplay_menu_p)
9840 display_menu_bar (w);
9841
9842 #ifdef HAVE_WINDOW_SYSTEM
9843 if (WINDOWP (f->tool_bar_window)
9844 && (FRAME_TOOL_BAR_LINES (f) > 0
9845 || auto_resize_tool_bars_p))
9846 redisplay_tool_bar (f);
9847 #endif
9848 }
9849
9850 finish_scroll_bars:
9851
9852 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9853 {
9854 int start, end, whole;
9855
9856 /* Calculate the start and end positions for the current window.
9857 At some point, it would be nice to choose between scrollbars
9858 which reflect the whole buffer size, with special markers
9859 indicating narrowing, and scrollbars which reflect only the
9860 visible region.
9861
9862 Note that mini-buffers sometimes aren't displaying any text. */
9863 if (!MINI_WINDOW_P (w)
9864 || (w == XWINDOW (minibuf_window)
9865 && NILP (echo_area_buffer[0])))
9866 {
9867 whole = ZV - BEGV;
9868 start = marker_position (w->start) - BEGV;
9869 /* I don't think this is guaranteed to be right. For the
9870 moment, we'll pretend it is. */
9871 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9872
9873 if (end < start)
9874 end = start;
9875 if (whole < (end - start))
9876 whole = end - start;
9877 }
9878 else
9879 start = end = whole = 0;
9880
9881 /* Indicate what this scroll bar ought to be displaying now. */
9882 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9883
9884 /* Note that we actually used the scroll bar attached to this
9885 window, so it shouldn't be deleted at the end of redisplay. */
9886 (*redeem_scroll_bar_hook) (w);
9887 }
9888
9889 restore_buffers:
9890
9891 /* Restore current_buffer and value of point in it. */
9892 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9893 set_buffer_internal_1 (old);
9894 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9895
9896 unbind_to (count, Qnil);
9897 }
9898
9899
9900 /* Build the complete desired matrix of WINDOW with a window start
9901 buffer position POS. Value is non-zero if successful. It is zero
9902 if fonts were loaded during redisplay which makes re-adjusting
9903 glyph matrices necessary. */
9904
9905 int
9906 try_window (window, pos)
9907 Lisp_Object window;
9908 struct text_pos pos;
9909 {
9910 struct window *w = XWINDOW (window);
9911 struct it it;
9912 struct glyph_row *last_text_row = NULL;
9913
9914 /* Make POS the new window start. */
9915 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9916
9917 /* Mark cursor position as unknown. No overlay arrow seen. */
9918 w->cursor.vpos = -1;
9919 overlay_arrow_seen = 0;
9920
9921 /* Initialize iterator and info to start at POS. */
9922 start_display (&it, w, pos);
9923
9924 /* Display all lines of W. */
9925 while (it.current_y < it.last_visible_y)
9926 {
9927 if (display_line (&it))
9928 last_text_row = it.glyph_row - 1;
9929 if (fonts_changed_p)
9930 return 0;
9931 }
9932
9933 /* If bottom moved off end of frame, change mode line percentage. */
9934 if (XFASTINT (w->window_end_pos) <= 0
9935 && Z != IT_CHARPOS (it))
9936 w->update_mode_line = Qt;
9937
9938 /* Set window_end_pos to the offset of the last character displayed
9939 on the window from the end of current_buffer. Set
9940 window_end_vpos to its row number. */
9941 if (last_text_row)
9942 {
9943 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9944 w->window_end_bytepos
9945 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9946 XSETFASTINT (w->window_end_pos,
9947 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9948 XSETFASTINT (w->window_end_vpos,
9949 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9950 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9951 ->displays_text_p);
9952 }
9953 else
9954 {
9955 w->window_end_bytepos = 0;
9956 XSETFASTINT (w->window_end_pos, 0);
9957 XSETFASTINT (w->window_end_vpos, 0);
9958 }
9959
9960 /* But that is not valid info until redisplay finishes. */
9961 w->window_end_valid = Qnil;
9962 return 1;
9963 }
9964
9965
9966 \f
9967 /************************************************************************
9968 Window redisplay reusing current matrix when buffer has not changed
9969 ************************************************************************/
9970
9971 /* Try redisplay of window W showing an unchanged buffer with a
9972 different window start than the last time it was displayed by
9973 reusing its current matrix. Value is non-zero if successful.
9974 W->start is the new window start. */
9975
9976 static int
9977 try_window_reusing_current_matrix (w)
9978 struct window *w;
9979 {
9980 struct frame *f = XFRAME (w->frame);
9981 struct glyph_row *row, *bottom_row;
9982 struct it it;
9983 struct run run;
9984 struct text_pos start, new_start;
9985 int nrows_scrolled, i;
9986 struct glyph_row *last_text_row;
9987 struct glyph_row *last_reused_text_row;
9988 struct glyph_row *start_row;
9989 int start_vpos, min_y, max_y;
9990
9991 if (/* This function doesn't handle terminal frames. */
9992 !FRAME_WINDOW_P (f)
9993 /* Don't try to reuse the display if windows have been split
9994 or such. */
9995 || windows_or_buffers_changed)
9996 return 0;
9997
9998 /* Can't do this if region may have changed. */
9999 if ((!NILP (Vtransient_mark_mode)
10000 && !NILP (current_buffer->mark_active))
10001 || !NILP (w->region_showing)
10002 || !NILP (Vshow_trailing_whitespace))
10003 return 0;
10004
10005 /* If top-line visibility has changed, give up. */
10006 if (WINDOW_WANTS_HEADER_LINE_P (w)
10007 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10008 return 0;
10009
10010 /* Give up if old or new display is scrolled vertically. We could
10011 make this function handle this, but right now it doesn't. */
10012 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10013 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10014 return 0;
10015
10016 /* The variable new_start now holds the new window start. The old
10017 start `start' can be determined from the current matrix. */
10018 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10019 start = start_row->start.pos;
10020 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10021
10022 /* Clear the desired matrix for the display below. */
10023 clear_glyph_matrix (w->desired_matrix);
10024
10025 if (CHARPOS (new_start) <= CHARPOS (start))
10026 {
10027 int first_row_y;
10028
10029 IF_DEBUG (debug_method_add (w, "twu1"));
10030
10031 /* Display up to a row that can be reused. The variable
10032 last_text_row is set to the last row displayed that displays
10033 text. Note that it.vpos == 0 if or if not there is a
10034 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10035 start_display (&it, w, new_start);
10036 first_row_y = it.current_y;
10037 w->cursor.vpos = -1;
10038 last_text_row = last_reused_text_row = NULL;
10039
10040 while (it.current_y < it.last_visible_y
10041 && IT_CHARPOS (it) < CHARPOS (start)
10042 && !fonts_changed_p)
10043 if (display_line (&it))
10044 last_text_row = it.glyph_row - 1;
10045
10046 /* A value of current_y < last_visible_y means that we stopped
10047 at the previous window start, which in turn means that we
10048 have at least one reusable row. */
10049 if (it.current_y < it.last_visible_y)
10050 {
10051 /* IT.vpos always starts from 0; it counts text lines. */
10052 nrows_scrolled = it.vpos;
10053
10054 /* Find PT if not already found in the lines displayed. */
10055 if (w->cursor.vpos < 0)
10056 {
10057 int dy = it.current_y - first_row_y;
10058
10059 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10060 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10061 {
10062 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10063 && PT < MATRIX_ROW_END_CHARPOS (row))
10064 {
10065 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10066 dy, nrows_scrolled);
10067 break;
10068 }
10069
10070 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10071 break;
10072
10073 ++row;
10074 }
10075
10076 /* Give up if point was not found. This shouldn't
10077 happen often; not more often than with try_window
10078 itself. */
10079 if (w->cursor.vpos < 0)
10080 {
10081 clear_glyph_matrix (w->desired_matrix);
10082 return 0;
10083 }
10084 }
10085
10086 /* Scroll the display. Do it before the current matrix is
10087 changed. The problem here is that update has not yet
10088 run, i.e. part of the current matrix is not up to date.
10089 scroll_run_hook will clear the cursor, and use the
10090 current matrix to get the height of the row the cursor is
10091 in. */
10092 run.current_y = first_row_y;
10093 run.desired_y = it.current_y;
10094 run.height = it.last_visible_y - it.current_y;
10095
10096 if (run.height > 0 && run.current_y != run.desired_y)
10097 {
10098 update_begin (f);
10099 rif->update_window_begin_hook (w);
10100 rif->clear_mouse_face (w);
10101 rif->scroll_run_hook (w, &run);
10102 rif->update_window_end_hook (w, 0, 0);
10103 update_end (f);
10104 }
10105
10106 /* Shift current matrix down by nrows_scrolled lines. */
10107 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10108 rotate_matrix (w->current_matrix,
10109 start_vpos,
10110 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10111 nrows_scrolled);
10112
10113 /* Disable lines not reused. */
10114 for (i = 0; i < it.vpos; ++i)
10115 (start_row + i)->enabled_p = 0;
10116
10117 /* Re-compute Y positions. */
10118 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10119 max_y = it.last_visible_y;
10120 for (row = start_row + nrows_scrolled;
10121 row < bottom_row;
10122 ++row)
10123 {
10124 row->y = it.current_y;
10125
10126 if (row->y < min_y)
10127 row->visible_height = row->height - (min_y - row->y);
10128 else if (row->y + row->height > max_y)
10129 row->visible_height
10130 = row->height - (row->y + row->height - max_y);
10131 else
10132 row->visible_height = row->height;
10133
10134 it.current_y += row->height;
10135
10136 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10137 last_reused_text_row = row;
10138 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10139 break;
10140 }
10141 }
10142
10143 /* Update window_end_pos etc.; last_reused_text_row is the last
10144 reused row from the current matrix containing text, if any.
10145 The value of last_text_row is the last displayed line
10146 containing text. */
10147 if (last_reused_text_row)
10148 {
10149 w->window_end_bytepos
10150 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10151 XSETFASTINT (w->window_end_pos,
10152 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10153 XSETFASTINT (w->window_end_vpos,
10154 MATRIX_ROW_VPOS (last_reused_text_row,
10155 w->current_matrix));
10156 }
10157 else if (last_text_row)
10158 {
10159 w->window_end_bytepos
10160 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10161 XSETFASTINT (w->window_end_pos,
10162 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10163 XSETFASTINT (w->window_end_vpos,
10164 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10165 }
10166 else
10167 {
10168 /* This window must be completely empty. */
10169 w->window_end_bytepos = 0;
10170 XSETFASTINT (w->window_end_pos, 0);
10171 XSETFASTINT (w->window_end_vpos, 0);
10172 }
10173 w->window_end_valid = Qnil;
10174
10175 /* Update hint: don't try scrolling again in update_window. */
10176 w->desired_matrix->no_scrolling_p = 1;
10177
10178 #if GLYPH_DEBUG
10179 debug_method_add (w, "try_window_reusing_current_matrix 1");
10180 #endif
10181 return 1;
10182 }
10183 else if (CHARPOS (new_start) > CHARPOS (start))
10184 {
10185 struct glyph_row *pt_row, *row;
10186 struct glyph_row *first_reusable_row;
10187 struct glyph_row *first_row_to_display;
10188 int dy;
10189 int yb = window_text_bottom_y (w);
10190
10191 IF_DEBUG (debug_method_add (w, "twu2"));
10192
10193 /* Find the row starting at new_start, if there is one. Don't
10194 reuse a partially visible line at the end. */
10195 first_reusable_row = start_row;
10196 while (first_reusable_row->enabled_p
10197 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10198 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10199 < CHARPOS (new_start)))
10200 ++first_reusable_row;
10201
10202 /* Give up if there is no row to reuse. */
10203 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10204 || !first_reusable_row->enabled_p
10205 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10206 != CHARPOS (new_start)))
10207 return 0;
10208
10209 /* We can reuse fully visible rows beginning with
10210 first_reusable_row to the end of the window. Set
10211 first_row_to_display to the first row that cannot be reused.
10212 Set pt_row to the row containing point, if there is any. */
10213 first_row_to_display = first_reusable_row;
10214 pt_row = NULL;
10215 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10216 {
10217 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10218 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10219 pt_row = first_row_to_display;
10220
10221 ++first_row_to_display;
10222 }
10223
10224 /* Start displaying at the start of first_row_to_display. */
10225 xassert (first_row_to_display->y < yb);
10226 init_to_row_start (&it, w, first_row_to_display);
10227 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10228 - start_vpos);
10229 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10230 - nrows_scrolled);
10231 it.current_y = (first_row_to_display->y - first_reusable_row->y
10232 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10233
10234 /* Display lines beginning with first_row_to_display in the
10235 desired matrix. Set last_text_row to the last row displayed
10236 that displays text. */
10237 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10238 if (pt_row == NULL)
10239 w->cursor.vpos = -1;
10240 last_text_row = NULL;
10241 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10242 if (display_line (&it))
10243 last_text_row = it.glyph_row - 1;
10244
10245 /* Give up If point isn't in a row displayed or reused. */
10246 if (w->cursor.vpos < 0)
10247 {
10248 clear_glyph_matrix (w->desired_matrix);
10249 return 0;
10250 }
10251
10252 /* If point is in a reused row, adjust y and vpos of the cursor
10253 position. */
10254 if (pt_row)
10255 {
10256 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10257 w->current_matrix);
10258 w->cursor.y -= first_reusable_row->y;
10259 }
10260
10261 /* Scroll the display. */
10262 run.current_y = first_reusable_row->y;
10263 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10264 run.height = it.last_visible_y - run.current_y;
10265 dy = run.current_y - run.desired_y;
10266
10267 if (run.height)
10268 {
10269 struct frame *f = XFRAME (WINDOW_FRAME (w));
10270 update_begin (f);
10271 rif->update_window_begin_hook (w);
10272 rif->clear_mouse_face (w);
10273 rif->scroll_run_hook (w, &run);
10274 rif->update_window_end_hook (w, 0, 0);
10275 update_end (f);
10276 }
10277
10278 /* Adjust Y positions of reused rows. */
10279 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10280 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10281 max_y = it.last_visible_y;
10282 for (row = first_reusable_row; row < first_row_to_display; ++row)
10283 {
10284 row->y -= dy;
10285 if (row->y < min_y)
10286 row->visible_height = row->height - (min_y - row->y);
10287 else if (row->y + row->height > max_y)
10288 row->visible_height
10289 = row->height - (row->y + row->height - max_y);
10290 else
10291 row->visible_height = row->height;
10292 }
10293
10294 /* Disable rows not reused. */
10295 while (row < bottom_row)
10296 {
10297 row->enabled_p = 0;
10298 ++row;
10299 }
10300
10301 /* Scroll the current matrix. */
10302 xassert (nrows_scrolled > 0);
10303 rotate_matrix (w->current_matrix,
10304 start_vpos,
10305 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10306 -nrows_scrolled);
10307
10308 /* Adjust window end. A null value of last_text_row means that
10309 the window end is in reused rows which in turn means that
10310 only its vpos can have changed. */
10311 if (last_text_row)
10312 {
10313 w->window_end_bytepos
10314 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10315 XSETFASTINT (w->window_end_pos,
10316 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10317 XSETFASTINT (w->window_end_vpos,
10318 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10319 }
10320 else
10321 {
10322 XSETFASTINT (w->window_end_vpos,
10323 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10324 }
10325
10326 w->window_end_valid = Qnil;
10327 w->desired_matrix->no_scrolling_p = 1;
10328
10329 #if GLYPH_DEBUG
10330 debug_method_add (w, "try_window_reusing_current_matrix 2");
10331 #endif
10332 return 1;
10333 }
10334
10335 return 0;
10336 }
10337
10338
10339 \f
10340 /************************************************************************
10341 Window redisplay reusing current matrix when buffer has changed
10342 ************************************************************************/
10343
10344 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10345 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10346 int *, int *));
10347 static struct glyph_row *
10348 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10349 struct glyph_row *));
10350
10351
10352 /* Return the last row in MATRIX displaying text. If row START is
10353 non-null, start searching with that row. IT gives the dimensions
10354 of the display. Value is null if matrix is empty; otherwise it is
10355 a pointer to the row found. */
10356
10357 static struct glyph_row *
10358 find_last_row_displaying_text (matrix, it, start)
10359 struct glyph_matrix *matrix;
10360 struct it *it;
10361 struct glyph_row *start;
10362 {
10363 struct glyph_row *row, *row_found;
10364
10365 /* Set row_found to the last row in IT->w's current matrix
10366 displaying text. The loop looks funny but think of partially
10367 visible lines. */
10368 row_found = NULL;
10369 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10370 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10371 {
10372 xassert (row->enabled_p);
10373 row_found = row;
10374 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10375 break;
10376 ++row;
10377 }
10378
10379 return row_found;
10380 }
10381
10382
10383 /* Return the last row in the current matrix of W that is not affected
10384 by changes at the start of current_buffer that occurred since the
10385 last time W was redisplayed. Value is null if no such row exists.
10386
10387 The global variable beg_unchanged has to contain the number of
10388 bytes unchanged at the start of current_buffer. BEG +
10389 beg_unchanged is the buffer position of the first changed byte in
10390 current_buffer. Characters at positions < BEG + beg_unchanged are
10391 at the same buffer positions as they were when the current matrix
10392 was built. */
10393
10394 static struct glyph_row *
10395 find_last_unchanged_at_beg_row (w)
10396 struct window *w;
10397 {
10398 int first_changed_pos = BEG + BEG_UNCHANGED;
10399 struct glyph_row *row;
10400 struct glyph_row *row_found = NULL;
10401 int yb = window_text_bottom_y (w);
10402
10403 /* Find the last row displaying unchanged text. */
10404 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10405 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10406 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10407 {
10408 if (/* If row ends before first_changed_pos, it is unchanged,
10409 except in some case. */
10410 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10411 /* When row ends in ZV and we write at ZV it is not
10412 unchanged. */
10413 && !row->ends_at_zv_p
10414 /* When first_changed_pos is the end of a continued line,
10415 row is not unchanged because it may be no longer
10416 continued. */
10417 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10418 && row->continued_p))
10419 row_found = row;
10420
10421 /* Stop if last visible row. */
10422 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10423 break;
10424
10425 ++row;
10426 }
10427
10428 return row_found;
10429 }
10430
10431
10432 /* Find the first glyph row in the current matrix of W that is not
10433 affected by changes at the end of current_buffer since the last
10434 time the window was redisplayed. Return in *DELTA the number of
10435 chars by which buffer positions in unchanged text at the end of
10436 current_buffer must be adjusted. Return in *DELTA_BYTES the
10437 corresponding number of bytes. Value is null if no such row
10438 exists, i.e. all rows are affected by changes. */
10439
10440 static struct glyph_row *
10441 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10442 struct window *w;
10443 int *delta, *delta_bytes;
10444 {
10445 struct glyph_row *row;
10446 struct glyph_row *row_found = NULL;
10447
10448 *delta = *delta_bytes = 0;
10449
10450 /* Display must not have been paused, otherwise the current matrix
10451 is not up to date. */
10452 if (NILP (w->window_end_valid))
10453 abort ();
10454
10455 /* A value of window_end_pos >= END_UNCHANGED means that the window
10456 end is in the range of changed text. If so, there is no
10457 unchanged row at the end of W's current matrix. */
10458 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10459 return NULL;
10460
10461 /* Set row to the last row in W's current matrix displaying text. */
10462 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10463
10464 /* If matrix is entirely empty, no unchanged row exists. */
10465 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10466 {
10467 /* The value of row is the last glyph row in the matrix having a
10468 meaningful buffer position in it. The end position of row
10469 corresponds to window_end_pos. This allows us to translate
10470 buffer positions in the current matrix to current buffer
10471 positions for characters not in changed text. */
10472 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10473 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10474 int last_unchanged_pos, last_unchanged_pos_old;
10475 struct glyph_row *first_text_row
10476 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10477
10478 *delta = Z - Z_old;
10479 *delta_bytes = Z_BYTE - Z_BYTE_old;
10480
10481 /* Set last_unchanged_pos to the buffer position of the last
10482 character in the buffer that has not been changed. Z is the
10483 index + 1 of the last byte in current_buffer, i.e. by
10484 subtracting end_unchanged we get the index of the last
10485 unchanged character, and we have to add BEG to get its buffer
10486 position. */
10487 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10488 last_unchanged_pos_old = last_unchanged_pos - *delta;
10489
10490 /* Search backward from ROW for a row displaying a line that
10491 starts at a minimum position >= last_unchanged_pos_old. */
10492 for (; row > first_text_row; --row)
10493 {
10494 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10495 abort ();
10496
10497 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10498 row_found = row;
10499 }
10500 }
10501
10502 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10503 abort ();
10504
10505 return row_found;
10506 }
10507
10508
10509 /* Make sure that glyph rows in the current matrix of window W
10510 reference the same glyph memory as corresponding rows in the
10511 frame's frame matrix. This function is called after scrolling W's
10512 current matrix on a terminal frame in try_window_id and
10513 try_window_reusing_current_matrix. */
10514
10515 static void
10516 sync_frame_with_window_matrix_rows (w)
10517 struct window *w;
10518 {
10519 struct frame *f = XFRAME (w->frame);
10520 struct glyph_row *window_row, *window_row_end, *frame_row;
10521
10522 /* Preconditions: W must be a leaf window and full-width. Its frame
10523 must have a frame matrix. */
10524 xassert (NILP (w->hchild) && NILP (w->vchild));
10525 xassert (WINDOW_FULL_WIDTH_P (w));
10526 xassert (!FRAME_WINDOW_P (f));
10527
10528 /* If W is a full-width window, glyph pointers in W's current matrix
10529 have, by definition, to be the same as glyph pointers in the
10530 corresponding frame matrix. */
10531 window_row = w->current_matrix->rows;
10532 window_row_end = window_row + w->current_matrix->nrows;
10533 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10534 while (window_row < window_row_end)
10535 {
10536 int area;
10537
10538 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10539 frame_row->glyphs[area] = window_row->glyphs[area];
10540
10541 /* Disable frame rows whose corresponding window rows have
10542 been disabled in try_window_id. */
10543 if (!window_row->enabled_p)
10544 frame_row->enabled_p = 0;
10545
10546 ++window_row, ++frame_row;
10547 }
10548 }
10549
10550
10551 /* Find the glyph row in window W containing CHARPOS. Consider all
10552 rows between START and END (not inclusive). END null means search
10553 all rows to the end of the display area of W. Value is the row
10554 containing CHARPOS or null. */
10555
10556 static struct glyph_row *
10557 row_containing_pos (w, charpos, start, end)
10558 struct window *w;
10559 int charpos;
10560 struct glyph_row *start, *end;
10561 {
10562 struct glyph_row *row = start;
10563 int last_y;
10564
10565 /* If we happen to start on a header-line, skip that. */
10566 if (row->mode_line_p)
10567 ++row;
10568
10569 if ((end && row >= end) || !row->enabled_p)
10570 return NULL;
10571
10572 last_y = window_text_bottom_y (w);
10573
10574 while ((end == NULL || row < end)
10575 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10576 /* The end position of a row equals the start
10577 position of the next row. If CHARPOS is there, we
10578 would rather display it in the next line, except
10579 when this line ends in ZV. */
10580 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10581 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10582 || !row->ends_at_zv_p)))
10583 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10584 ++row;
10585
10586 /* Give up if CHARPOS not found. */
10587 if ((end && row >= end)
10588 || charpos < MATRIX_ROW_START_CHARPOS (row)
10589 || charpos > MATRIX_ROW_END_CHARPOS (row))
10590 row = NULL;
10591
10592 return row;
10593 }
10594
10595
10596 /* Try to redisplay window W by reusing its existing display. W's
10597 current matrix must be up to date when this function is called,
10598 i.e. window_end_valid must not be nil.
10599
10600 Value is
10601
10602 1 if display has been updated
10603 0 if otherwise unsuccessful
10604 -1 if redisplay with same window start is known not to succeed
10605
10606 The following steps are performed:
10607
10608 1. Find the last row in the current matrix of W that is not
10609 affected by changes at the start of current_buffer. If no such row
10610 is found, give up.
10611
10612 2. Find the first row in W's current matrix that is not affected by
10613 changes at the end of current_buffer. Maybe there is no such row.
10614
10615 3. Display lines beginning with the row + 1 found in step 1 to the
10616 row found in step 2 or, if step 2 didn't find a row, to the end of
10617 the window.
10618
10619 4. If cursor is not known to appear on the window, give up.
10620
10621 5. If display stopped at the row found in step 2, scroll the
10622 display and current matrix as needed.
10623
10624 6. Maybe display some lines at the end of W, if we must. This can
10625 happen under various circumstances, like a partially visible line
10626 becoming fully visible, or because newly displayed lines are displayed
10627 in smaller font sizes.
10628
10629 7. Update W's window end information. */
10630
10631 /* Check that window end is what we expect it to be. */
10632
10633 static int
10634 try_window_id (w)
10635 struct window *w;
10636 {
10637 struct frame *f = XFRAME (w->frame);
10638 struct glyph_matrix *current_matrix = w->current_matrix;
10639 struct glyph_matrix *desired_matrix = w->desired_matrix;
10640 struct glyph_row *last_unchanged_at_beg_row;
10641 struct glyph_row *first_unchanged_at_end_row;
10642 struct glyph_row *row;
10643 struct glyph_row *bottom_row;
10644 int bottom_vpos;
10645 struct it it;
10646 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10647 struct text_pos start_pos;
10648 struct run run;
10649 int first_unchanged_at_end_vpos = 0;
10650 struct glyph_row *last_text_row, *last_text_row_at_end;
10651 struct text_pos start;
10652
10653 SET_TEXT_POS_FROM_MARKER (start, w->start);
10654
10655 /* Check pre-conditions. Window end must be valid, otherwise
10656 the current matrix would not be up to date. */
10657 xassert (!NILP (w->window_end_valid));
10658 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10659 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10660
10661 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10662 only if buffer has really changed. The reason is that the gap is
10663 initially at Z for freshly visited files. The code below would
10664 set end_unchanged to 0 in that case. */
10665 if (MODIFF > SAVE_MODIFF
10666 /* This seems to happen sometimes after saving a buffer. */
10667 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10668 {
10669 if (GPT - BEG < BEG_UNCHANGED)
10670 BEG_UNCHANGED = GPT - BEG;
10671 if (Z - GPT < END_UNCHANGED)
10672 END_UNCHANGED = Z - GPT;
10673 }
10674
10675 /* If window starts after a line end, and the last change is in
10676 front of that newline, then changes don't affect the display.
10677 This case happens with stealth-fontification. Note that although
10678 the display is unchanged, glyph positions in the matrix have to
10679 be adjusted, of course. */
10680 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10681 if (CHARPOS (start) > BEGV
10682 && Z - END_UNCHANGED < CHARPOS (start) - 1
10683 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10684 && PT < MATRIX_ROW_END_CHARPOS (row))
10685 {
10686 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10687 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10688
10689 if (delta)
10690 {
10691 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10692 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10693
10694 increment_matrix_positions (w->current_matrix,
10695 MATRIX_ROW_VPOS (r0, current_matrix),
10696 MATRIX_ROW_VPOS (r1, current_matrix),
10697 delta, delta_bytes);
10698 }
10699
10700 #if 0 /* If changes are all in front of the window start, the
10701 distance of the last displayed glyph from Z hasn't
10702 changed. */
10703 w->window_end_pos
10704 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10705 w->window_end_bytepos
10706 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10707 #endif
10708
10709 return 1;
10710 }
10711
10712 /* Return quickly if changes are all below what is displayed in the
10713 window, and if PT is in the window. */
10714 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10715 && PT < MATRIX_ROW_END_CHARPOS (row))
10716 {
10717 /* We have to update window end positions because the buffer's
10718 size has changed. */
10719 w->window_end_pos
10720 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10721 w->window_end_bytepos
10722 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10723
10724 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10725 row = row_containing_pos (w, PT, row, NULL);
10726 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10727 return 2;
10728 }
10729
10730 /* Check that window start agrees with the start of the first glyph
10731 row in its current matrix. Check this after we know the window
10732 start is not in changed text, otherwise positions would not be
10733 comparable. */
10734 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10735 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10736 return 0;
10737
10738 /* Compute the position at which we have to start displaying new
10739 lines. Some of the lines at the top of the window might be
10740 reusable because they are not displaying changed text. Find the
10741 last row in W's current matrix not affected by changes at the
10742 start of current_buffer. Value is null if changes start in the
10743 first line of window. */
10744 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10745 if (last_unchanged_at_beg_row)
10746 {
10747 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10748 start_pos = it.current.pos;
10749
10750 /* Start displaying new lines in the desired matrix at the same
10751 vpos we would use in the current matrix, i.e. below
10752 last_unchanged_at_beg_row. */
10753 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10754 current_matrix);
10755 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10756 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10757
10758 xassert (it.hpos == 0 && it.current_x == 0);
10759 }
10760 else
10761 {
10762 /* There are no reusable lines at the start of the window.
10763 Start displaying in the first line. */
10764 start_display (&it, w, start);
10765 start_pos = it.current.pos;
10766 }
10767
10768 /* Find the first row that is not affected by changes at the end of
10769 the buffer. Value will be null if there is no unchanged row, in
10770 which case we must redisplay to the end of the window. delta
10771 will be set to the value by which buffer positions beginning with
10772 first_unchanged_at_end_row have to be adjusted due to text
10773 changes. */
10774 first_unchanged_at_end_row
10775 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10776 IF_DEBUG (debug_delta = delta);
10777 IF_DEBUG (debug_delta_bytes = delta_bytes);
10778
10779 /* Set stop_pos to the buffer position up to which we will have to
10780 display new lines. If first_unchanged_at_end_row != NULL, this
10781 is the buffer position of the start of the line displayed in that
10782 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10783 that we don't stop at a buffer position. */
10784 stop_pos = 0;
10785 if (first_unchanged_at_end_row)
10786 {
10787 xassert (last_unchanged_at_beg_row == NULL
10788 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10789
10790 /* If this is a continuation line, move forward to the next one
10791 that isn't. Changes in lines above affect this line.
10792 Caution: this may move first_unchanged_at_end_row to a row
10793 not displaying text. */
10794 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10795 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10796 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10797 < it.last_visible_y))
10798 ++first_unchanged_at_end_row;
10799
10800 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10801 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10802 >= it.last_visible_y))
10803 first_unchanged_at_end_row = NULL;
10804 else
10805 {
10806 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10807 + delta);
10808 first_unchanged_at_end_vpos
10809 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10810 xassert (stop_pos >= Z - END_UNCHANGED);
10811 }
10812 }
10813 else if (last_unchanged_at_beg_row == NULL)
10814 return 0;
10815
10816
10817 #if GLYPH_DEBUG
10818
10819 /* Either there is no unchanged row at the end, or the one we have
10820 now displays text. This is a necessary condition for the window
10821 end pos calculation at the end of this function. */
10822 xassert (first_unchanged_at_end_row == NULL
10823 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10824
10825 debug_last_unchanged_at_beg_vpos
10826 = (last_unchanged_at_beg_row
10827 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10828 : -1);
10829 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10830
10831 #endif /* GLYPH_DEBUG != 0 */
10832
10833
10834 /* Display new lines. Set last_text_row to the last new line
10835 displayed which has text on it, i.e. might end up as being the
10836 line where the window_end_vpos is. */
10837 w->cursor.vpos = -1;
10838 last_text_row = NULL;
10839 overlay_arrow_seen = 0;
10840 while (it.current_y < it.last_visible_y
10841 && !fonts_changed_p
10842 && (first_unchanged_at_end_row == NULL
10843 || IT_CHARPOS (it) < stop_pos))
10844 {
10845 if (display_line (&it))
10846 last_text_row = it.glyph_row - 1;
10847 }
10848
10849 if (fonts_changed_p)
10850 return -1;
10851
10852
10853 /* Compute differences in buffer positions, y-positions etc. for
10854 lines reused at the bottom of the window. Compute what we can
10855 scroll. */
10856 if (first_unchanged_at_end_row
10857 /* No lines reused because we displayed everything up to the
10858 bottom of the window. */
10859 && it.current_y < it.last_visible_y)
10860 {
10861 dvpos = (it.vpos
10862 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10863 current_matrix));
10864 dy = it.current_y - first_unchanged_at_end_row->y;
10865 run.current_y = first_unchanged_at_end_row->y;
10866 run.desired_y = run.current_y + dy;
10867 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10868 }
10869 else
10870 {
10871 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10872 first_unchanged_at_end_row = NULL;
10873 }
10874 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10875
10876
10877 /* Find the cursor if not already found. We have to decide whether
10878 PT will appear on this window (it sometimes doesn't, but this is
10879 not a very frequent case.) This decision has to be made before
10880 the current matrix is altered. A value of cursor.vpos < 0 means
10881 that PT is either in one of the lines beginning at
10882 first_unchanged_at_end_row or below the window. Don't care for
10883 lines that might be displayed later at the window end; as
10884 mentioned, this is not a frequent case. */
10885 if (w->cursor.vpos < 0)
10886 {
10887 /* Cursor in unchanged rows at the top? */
10888 if (PT < CHARPOS (start_pos)
10889 && last_unchanged_at_beg_row)
10890 {
10891 row = row_containing_pos (w, PT,
10892 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10893 last_unchanged_at_beg_row + 1);
10894 if (row)
10895 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10896 }
10897
10898 /* Start from first_unchanged_at_end_row looking for PT. */
10899 else if (first_unchanged_at_end_row)
10900 {
10901 row = row_containing_pos (w, PT - delta,
10902 first_unchanged_at_end_row, NULL);
10903 if (row)
10904 set_cursor_from_row (w, row, w->current_matrix, delta,
10905 delta_bytes, dy, dvpos);
10906 }
10907
10908 /* Give up if cursor was not found. */
10909 if (w->cursor.vpos < 0)
10910 {
10911 clear_glyph_matrix (w->desired_matrix);
10912 return -1;
10913 }
10914 }
10915
10916 /* Don't let the cursor end in the scroll margins. */
10917 {
10918 int this_scroll_margin, cursor_height;
10919
10920 this_scroll_margin = max (0, scroll_margin);
10921 this_scroll_margin = min (this_scroll_margin,
10922 XFASTINT (w->height) / 4);
10923 this_scroll_margin *= CANON_Y_UNIT (it.f);
10924 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10925
10926 if ((w->cursor.y < this_scroll_margin
10927 && CHARPOS (start) > BEGV)
10928 /* Don't take scroll margin into account at the bottom because
10929 old redisplay didn't do it either. */
10930 || w->cursor.y + cursor_height > it.last_visible_y)
10931 {
10932 w->cursor.vpos = -1;
10933 clear_glyph_matrix (w->desired_matrix);
10934 return -1;
10935 }
10936 }
10937
10938 /* Scroll the display. Do it before changing the current matrix so
10939 that xterm.c doesn't get confused about where the cursor glyph is
10940 found. */
10941 if (dy && run.height)
10942 {
10943 update_begin (f);
10944
10945 if (FRAME_WINDOW_P (f))
10946 {
10947 rif->update_window_begin_hook (w);
10948 rif->clear_mouse_face (w);
10949 rif->scroll_run_hook (w, &run);
10950 rif->update_window_end_hook (w, 0, 0);
10951 }
10952 else
10953 {
10954 /* Terminal frame. In this case, dvpos gives the number of
10955 lines to scroll by; dvpos < 0 means scroll up. */
10956 int first_unchanged_at_end_vpos
10957 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10958 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10959 int end = XFASTINT (w->top) + window_internal_height (w);
10960
10961 /* Perform the operation on the screen. */
10962 if (dvpos > 0)
10963 {
10964 /* Scroll last_unchanged_at_beg_row to the end of the
10965 window down dvpos lines. */
10966 set_terminal_window (end);
10967
10968 /* On dumb terminals delete dvpos lines at the end
10969 before inserting dvpos empty lines. */
10970 if (!scroll_region_ok)
10971 ins_del_lines (end - dvpos, -dvpos);
10972
10973 /* Insert dvpos empty lines in front of
10974 last_unchanged_at_beg_row. */
10975 ins_del_lines (from, dvpos);
10976 }
10977 else if (dvpos < 0)
10978 {
10979 /* Scroll up last_unchanged_at_beg_vpos to the end of
10980 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10981 set_terminal_window (end);
10982
10983 /* Delete dvpos lines in front of
10984 last_unchanged_at_beg_vpos. ins_del_lines will set
10985 the cursor to the given vpos and emit |dvpos| delete
10986 line sequences. */
10987 ins_del_lines (from + dvpos, dvpos);
10988
10989 /* On a dumb terminal insert dvpos empty lines at the
10990 end. */
10991 if (!scroll_region_ok)
10992 ins_del_lines (end + dvpos, -dvpos);
10993 }
10994
10995 set_terminal_window (0);
10996 }
10997
10998 update_end (f);
10999 }
11000
11001 /* Shift reused rows of the current matrix to the right position.
11002 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11003 text. */
11004 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11005 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11006 if (dvpos < 0)
11007 {
11008 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11009 bottom_vpos, dvpos);
11010 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11011 bottom_vpos, 0);
11012 }
11013 else if (dvpos > 0)
11014 {
11015 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11016 bottom_vpos, dvpos);
11017 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11018 first_unchanged_at_end_vpos + dvpos, 0);
11019 }
11020
11021 /* For frame-based redisplay, make sure that current frame and window
11022 matrix are in sync with respect to glyph memory. */
11023 if (!FRAME_WINDOW_P (f))
11024 sync_frame_with_window_matrix_rows (w);
11025
11026 /* Adjust buffer positions in reused rows. */
11027 if (delta)
11028 increment_matrix_positions (current_matrix,
11029 first_unchanged_at_end_vpos + dvpos,
11030 bottom_vpos, delta, delta_bytes);
11031
11032 /* Adjust Y positions. */
11033 if (dy)
11034 shift_glyph_matrix (w, current_matrix,
11035 first_unchanged_at_end_vpos + dvpos,
11036 bottom_vpos, dy);
11037
11038 if (first_unchanged_at_end_row)
11039 first_unchanged_at_end_row += dvpos;
11040
11041 /* If scrolling up, there may be some lines to display at the end of
11042 the window. */
11043 last_text_row_at_end = NULL;
11044 if (dy < 0)
11045 {
11046 /* Set last_row to the glyph row in the current matrix where the
11047 window end line is found. It has been moved up or down in
11048 the matrix by dvpos. */
11049 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11050 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11051
11052 /* If last_row is the window end line, it should display text. */
11053 xassert (last_row->displays_text_p);
11054
11055 /* If window end line was partially visible before, begin
11056 displaying at that line. Otherwise begin displaying with the
11057 line following it. */
11058 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11059 {
11060 init_to_row_start (&it, w, last_row);
11061 it.vpos = last_vpos;
11062 it.current_y = last_row->y;
11063 }
11064 else
11065 {
11066 init_to_row_end (&it, w, last_row);
11067 it.vpos = 1 + last_vpos;
11068 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11069 ++last_row;
11070 }
11071
11072 /* We may start in a continuation line. If so, we have to get
11073 the right continuation_lines_width and current_x. */
11074 it.continuation_lines_width = last_row->continuation_lines_width;
11075 it.hpos = it.current_x = 0;
11076
11077 /* Display the rest of the lines at the window end. */
11078 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11079 while (it.current_y < it.last_visible_y
11080 && !fonts_changed_p)
11081 {
11082 /* Is it always sure that the display agrees with lines in
11083 the current matrix? I don't think so, so we mark rows
11084 displayed invalid in the current matrix by setting their
11085 enabled_p flag to zero. */
11086 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11087 if (display_line (&it))
11088 last_text_row_at_end = it.glyph_row - 1;
11089 }
11090 }
11091
11092 /* Update window_end_pos and window_end_vpos. */
11093 if (first_unchanged_at_end_row
11094 && first_unchanged_at_end_row->y < it.last_visible_y
11095 && !last_text_row_at_end)
11096 {
11097 /* Window end line if one of the preserved rows from the current
11098 matrix. Set row to the last row displaying text in current
11099 matrix starting at first_unchanged_at_end_row, after
11100 scrolling. */
11101 xassert (first_unchanged_at_end_row->displays_text_p);
11102 row = find_last_row_displaying_text (w->current_matrix, &it,
11103 first_unchanged_at_end_row);
11104 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11105
11106 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11107 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11108 XSETFASTINT (w->window_end_vpos,
11109 MATRIX_ROW_VPOS (row, w->current_matrix));
11110 }
11111 else if (last_text_row_at_end)
11112 {
11113 XSETFASTINT (w->window_end_pos,
11114 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11115 w->window_end_bytepos
11116 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11117 XSETFASTINT (w->window_end_vpos,
11118 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11119 }
11120 else if (last_text_row)
11121 {
11122 /* We have displayed either to the end of the window or at the
11123 end of the window, i.e. the last row with text is to be found
11124 in the desired matrix. */
11125 XSETFASTINT (w->window_end_pos,
11126 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11127 w->window_end_bytepos
11128 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11129 XSETFASTINT (w->window_end_vpos,
11130 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11131 }
11132 else if (first_unchanged_at_end_row == NULL
11133 && last_text_row == NULL
11134 && last_text_row_at_end == NULL)
11135 {
11136 /* Displayed to end of window, but no line containing text was
11137 displayed. Lines were deleted at the end of the window. */
11138 int vpos;
11139 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11140
11141 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11142 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11143 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11144 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11145 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11146 break;
11147
11148 w->window_end_vpos = make_number (vpos);
11149 }
11150 else
11151 abort ();
11152
11153 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11154 debug_end_vpos = XFASTINT (w->window_end_vpos));
11155
11156 /* Record that display has not been completed. */
11157 w->window_end_valid = Qnil;
11158 w->desired_matrix->no_scrolling_p = 1;
11159 return 3;
11160 }
11161
11162
11163 \f
11164 /***********************************************************************
11165 More debugging support
11166 ***********************************************************************/
11167
11168 #if GLYPH_DEBUG
11169
11170 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11171 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11172
11173
11174 /* Dump the contents of glyph matrix MATRIX on stderr. If
11175 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11176
11177 static void
11178 dump_glyph_matrix (matrix, with_glyphs_p)
11179 struct glyph_matrix *matrix;
11180 int with_glyphs_p;
11181 {
11182 int i;
11183 for (i = 0; i < matrix->nrows; ++i)
11184 dump_glyph_row (matrix, i, with_glyphs_p);
11185 }
11186
11187
11188 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11189 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11190
11191 void
11192 dump_glyph_row (matrix, vpos, with_glyphs_p)
11193 struct glyph_matrix *matrix;
11194 int vpos, with_glyphs_p;
11195 {
11196 struct glyph_row *row;
11197
11198 if (vpos < 0 || vpos >= matrix->nrows)
11199 return;
11200
11201 row = MATRIX_ROW (matrix, vpos);
11202
11203 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11204 fprintf (stderr, "=======================================================================\n");
11205
11206 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11207 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11208 row - matrix->rows,
11209 MATRIX_ROW_START_CHARPOS (row),
11210 MATRIX_ROW_END_CHARPOS (row),
11211 row->used[TEXT_AREA],
11212 row->contains_overlapping_glyphs_p,
11213 row->enabled_p,
11214 row->inverse_p,
11215 row->truncated_on_left_p,
11216 row->truncated_on_right_p,
11217 row->overlay_arrow_p,
11218 row->continued_p,
11219 MATRIX_ROW_CONTINUATION_LINE_P (row),
11220 row->displays_text_p,
11221 row->ends_at_zv_p,
11222 row->fill_line_p,
11223 row->ends_in_middle_of_char_p,
11224 row->starts_in_middle_of_char_p,
11225 row->x,
11226 row->y,
11227 row->pixel_width,
11228 row->height,
11229 row->visible_height,
11230 row->ascent,
11231 row->phys_ascent);
11232 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11233 row->end.overlay_string_index);
11234 fprintf (stderr, "%9d %5d\n",
11235 CHARPOS (row->start.string_pos),
11236 CHARPOS (row->end.string_pos));
11237 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11238 row->end.dpvec_index);
11239
11240 if (with_glyphs_p)
11241 {
11242 struct glyph *glyph, *glyph_end;
11243 int prev_had_glyphs_p;
11244
11245 glyph = row->glyphs[TEXT_AREA];
11246 glyph_end = glyph + row->used[TEXT_AREA];
11247
11248 /* Glyph for a line end in text. */
11249 if (glyph == glyph_end && glyph->charpos > 0)
11250 ++glyph_end;
11251
11252 if (glyph < glyph_end)
11253 {
11254 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11255 prev_had_glyphs_p = 1;
11256 }
11257 else
11258 prev_had_glyphs_p = 0;
11259
11260 while (glyph < glyph_end)
11261 {
11262 if (glyph->type == CHAR_GLYPH)
11263 {
11264 fprintf (stderr,
11265 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11266 glyph - row->glyphs[TEXT_AREA],
11267 'C',
11268 glyph->charpos,
11269 (BUFFERP (glyph->object)
11270 ? 'B'
11271 : (STRINGP (glyph->object)
11272 ? 'S'
11273 : '-')),
11274 glyph->pixel_width,
11275 glyph->u.ch,
11276 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11277 ? glyph->u.ch
11278 : '.'),
11279 glyph->face_id,
11280 glyph->left_box_line_p,
11281 glyph->right_box_line_p);
11282 }
11283 else if (glyph->type == STRETCH_GLYPH)
11284 {
11285 fprintf (stderr,
11286 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11287 glyph - row->glyphs[TEXT_AREA],
11288 'S',
11289 glyph->charpos,
11290 (BUFFERP (glyph->object)
11291 ? 'B'
11292 : (STRINGP (glyph->object)
11293 ? 'S'
11294 : '-')),
11295 glyph->pixel_width,
11296 0,
11297 '.',
11298 glyph->face_id,
11299 glyph->left_box_line_p,
11300 glyph->right_box_line_p);
11301 }
11302 else if (glyph->type == IMAGE_GLYPH)
11303 {
11304 fprintf (stderr,
11305 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11306 glyph - row->glyphs[TEXT_AREA],
11307 'I',
11308 glyph->charpos,
11309 (BUFFERP (glyph->object)
11310 ? 'B'
11311 : (STRINGP (glyph->object)
11312 ? 'S'
11313 : '-')),
11314 glyph->pixel_width,
11315 glyph->u.img_id,
11316 '.',
11317 glyph->face_id,
11318 glyph->left_box_line_p,
11319 glyph->right_box_line_p);
11320 }
11321 ++glyph;
11322 }
11323 }
11324 }
11325
11326
11327 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11328 Sdump_glyph_matrix, 0, 1, "p",
11329 "Dump the current matrix of the selected window to stderr.\n\
11330 Shows contents of glyph row structures. With non-nil optional\n\
11331 parameter WITH-GLYPHS-P, dump glyphs as well.")
11332 (with_glyphs_p)
11333 Lisp_Object with_glyphs_p;
11334 {
11335 struct window *w = XWINDOW (selected_window);
11336 struct buffer *buffer = XBUFFER (w->buffer);
11337
11338 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11339 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11340 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11341 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11342 fprintf (stderr, "=============================================\n");
11343 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11344 return Qnil;
11345 }
11346
11347
11348 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11349 "Dump glyph row ROW to stderr.")
11350 (row)
11351 Lisp_Object row;
11352 {
11353 CHECK_NUMBER (row, 0);
11354 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11355 return Qnil;
11356 }
11357
11358
11359 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11360 0, 0, "", "")
11361 ()
11362 {
11363 struct frame *sf = SELECTED_FRAME ();
11364 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11365 ->current_matrix);
11366 dump_glyph_row (m, 0, 1);
11367 return Qnil;
11368 }
11369
11370
11371 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11372 Strace_redisplay_toggle, 0, 0, "",
11373 "Toggle tracing of redisplay.")
11374 ()
11375 {
11376 trace_redisplay_p = !trace_redisplay_p;
11377 return Qnil;
11378 }
11379
11380
11381 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11382 "Print STRING to stderr.")
11383 (string)
11384 Lisp_Object string;
11385 {
11386 CHECK_STRING (string, 0);
11387 fprintf (stderr, "%s", XSTRING (string)->data);
11388 return Qnil;
11389 }
11390
11391 #endif /* GLYPH_DEBUG */
11392
11393
11394 \f
11395 /***********************************************************************
11396 Building Desired Matrix Rows
11397 ***********************************************************************/
11398
11399 /* Return a temporary glyph row holding the glyphs of an overlay
11400 arrow. Only used for non-window-redisplay windows. */
11401
11402 static struct glyph_row *
11403 get_overlay_arrow_glyph_row (w)
11404 struct window *w;
11405 {
11406 struct frame *f = XFRAME (WINDOW_FRAME (w));
11407 struct buffer *buffer = XBUFFER (w->buffer);
11408 struct buffer *old = current_buffer;
11409 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11410 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11411 unsigned char *arrow_end = arrow_string + arrow_len;
11412 unsigned char *p;
11413 struct it it;
11414 int multibyte_p;
11415 int n_glyphs_before;
11416
11417 set_buffer_temp (buffer);
11418 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11419 it.glyph_row->used[TEXT_AREA] = 0;
11420 SET_TEXT_POS (it.position, 0, 0);
11421
11422 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11423 p = arrow_string;
11424 while (p < arrow_end)
11425 {
11426 Lisp_Object face, ilisp;
11427
11428 /* Get the next character. */
11429 if (multibyte_p)
11430 it.c = string_char_and_length (p, arrow_len, &it.len);
11431 else
11432 it.c = *p, it.len = 1;
11433 p += it.len;
11434
11435 /* Get its face. */
11436 XSETFASTINT (ilisp, p - arrow_string);
11437 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11438 it.face_id = compute_char_face (f, it.c, face);
11439
11440 /* Compute its width, get its glyphs. */
11441 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11442 SET_TEXT_POS (it.position, -1, -1);
11443 PRODUCE_GLYPHS (&it);
11444
11445 /* If this character doesn't fit any more in the line, we have
11446 to remove some glyphs. */
11447 if (it.current_x > it.last_visible_x)
11448 {
11449 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11450 break;
11451 }
11452 }
11453
11454 set_buffer_temp (old);
11455 return it.glyph_row;
11456 }
11457
11458
11459 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11460 glyphs are only inserted for terminal frames since we can't really
11461 win with truncation glyphs when partially visible glyphs are
11462 involved. Which glyphs to insert is determined by
11463 produce_special_glyphs. */
11464
11465 static void
11466 insert_left_trunc_glyphs (it)
11467 struct it *it;
11468 {
11469 struct it truncate_it;
11470 struct glyph *from, *end, *to, *toend;
11471
11472 xassert (!FRAME_WINDOW_P (it->f));
11473
11474 /* Get the truncation glyphs. */
11475 truncate_it = *it;
11476 truncate_it.current_x = 0;
11477 truncate_it.face_id = DEFAULT_FACE_ID;
11478 truncate_it.glyph_row = &scratch_glyph_row;
11479 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11480 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11481 truncate_it.object = make_number (0);
11482 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11483
11484 /* Overwrite glyphs from IT with truncation glyphs. */
11485 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11486 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11487 to = it->glyph_row->glyphs[TEXT_AREA];
11488 toend = to + it->glyph_row->used[TEXT_AREA];
11489
11490 while (from < end)
11491 *to++ = *from++;
11492
11493 /* There may be padding glyphs left over. Remove them. */
11494 from = to;
11495 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11496 ++from;
11497 while (from < toend)
11498 *to++ = *from++;
11499
11500 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11501 }
11502
11503
11504 /* Compute the pixel height and width of IT->glyph_row.
11505
11506 Most of the time, ascent and height of a display line will be equal
11507 to the max_ascent and max_height values of the display iterator
11508 structure. This is not the case if
11509
11510 1. We hit ZV without displaying anything. In this case, max_ascent
11511 and max_height will be zero.
11512
11513 2. We have some glyphs that don't contribute to the line height.
11514 (The glyph row flag contributes_to_line_height_p is for future
11515 pixmap extensions).
11516
11517 The first case is easily covered by using default values because in
11518 these cases, the line height does not really matter, except that it
11519 must not be zero. */
11520
11521 static void
11522 compute_line_metrics (it)
11523 struct it *it;
11524 {
11525 struct glyph_row *row = it->glyph_row;
11526 int area, i;
11527
11528 if (FRAME_WINDOW_P (it->f))
11529 {
11530 int i, header_line_height;
11531
11532 /* The line may consist of one space only, that was added to
11533 place the cursor on it. If so, the row's height hasn't been
11534 computed yet. */
11535 if (row->height == 0)
11536 {
11537 if (it->max_ascent + it->max_descent == 0)
11538 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11539 row->ascent = it->max_ascent;
11540 row->height = it->max_ascent + it->max_descent;
11541 row->phys_ascent = it->max_phys_ascent;
11542 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11543 }
11544
11545 /* Compute the width of this line. */
11546 row->pixel_width = row->x;
11547 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11548 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11549
11550 xassert (row->pixel_width >= 0);
11551 xassert (row->ascent >= 0 && row->height > 0);
11552
11553 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11554 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11555
11556 /* If first line's physical ascent is larger than its logical
11557 ascent, use the physical ascent, and make the row taller.
11558 This makes accented characters fully visible. */
11559 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11560 && row->phys_ascent > row->ascent)
11561 {
11562 row->height += row->phys_ascent - row->ascent;
11563 row->ascent = row->phys_ascent;
11564 }
11565
11566 /* Compute how much of the line is visible. */
11567 row->visible_height = row->height;
11568
11569 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11570 if (row->y < header_line_height)
11571 row->visible_height -= header_line_height - row->y;
11572 else
11573 {
11574 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11575 if (row->y + row->height > max_y)
11576 row->visible_height -= row->y + row->height - max_y;
11577 }
11578 }
11579 else
11580 {
11581 row->pixel_width = row->used[TEXT_AREA];
11582 row->ascent = row->phys_ascent = 0;
11583 row->height = row->phys_height = row->visible_height = 1;
11584 }
11585
11586 /* Compute a hash code for this row. */
11587 row->hash = 0;
11588 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11589 for (i = 0; i < row->used[area]; ++i)
11590 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11591 + row->glyphs[area][i].u.val
11592 + row->glyphs[area][i].face_id
11593 + row->glyphs[area][i].padding_p
11594 + (row->glyphs[area][i].type << 2));
11595
11596 it->max_ascent = it->max_descent = 0;
11597 it->max_phys_ascent = it->max_phys_descent = 0;
11598 }
11599
11600
11601 /* Append one space to the glyph row of iterator IT if doing a
11602 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11603 space have the default face, otherwise let it have the same face as
11604 IT->face_id. Value is non-zero if a space was added.
11605
11606 This function is called to make sure that there is always one glyph
11607 at the end of a glyph row that the cursor can be set on under
11608 window-systems. (If there weren't such a glyph we would not know
11609 how wide and tall a box cursor should be displayed).
11610
11611 At the same time this space let's a nicely handle clearing to the
11612 end of the line if the row ends in italic text. */
11613
11614 static int
11615 append_space (it, default_face_p)
11616 struct it *it;
11617 int default_face_p;
11618 {
11619 if (FRAME_WINDOW_P (it->f))
11620 {
11621 int n = it->glyph_row->used[TEXT_AREA];
11622
11623 if (it->glyph_row->glyphs[TEXT_AREA] + n
11624 < it->glyph_row->glyphs[1 + TEXT_AREA])
11625 {
11626 /* Save some values that must not be changed.
11627 Must save IT->c and IT->len because otherwise
11628 ITERATOR_AT_END_P wouldn't work anymore after
11629 append_space has been called. */
11630 int saved_what = it->what;
11631 int saved_c = it->c, saved_len = it->len;
11632 int saved_x = it->current_x;
11633 int saved_face_id = it->face_id;
11634 struct text_pos saved_pos;
11635 Lisp_Object saved_object;
11636 struct face *face;
11637
11638 saved_object = it->object;
11639 saved_pos = it->position;
11640
11641 it->what = IT_CHARACTER;
11642 bzero (&it->position, sizeof it->position);
11643 it->object = make_number (0);
11644 it->c = ' ';
11645 it->len = 1;
11646
11647 if (default_face_p)
11648 it->face_id = DEFAULT_FACE_ID;
11649 face = FACE_FROM_ID (it->f, it->face_id);
11650 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11651
11652 PRODUCE_GLYPHS (it);
11653
11654 it->current_x = saved_x;
11655 it->object = saved_object;
11656 it->position = saved_pos;
11657 it->what = saved_what;
11658 it->face_id = saved_face_id;
11659 it->len = saved_len;
11660 it->c = saved_c;
11661 return 1;
11662 }
11663 }
11664
11665 return 0;
11666 }
11667
11668
11669 /* Extend the face of the last glyph in the text area of IT->glyph_row
11670 to the end of the display line. Called from display_line.
11671 If the glyph row is empty, add a space glyph to it so that we
11672 know the face to draw. Set the glyph row flag fill_line_p. */
11673
11674 static void
11675 extend_face_to_end_of_line (it)
11676 struct it *it;
11677 {
11678 struct face *face;
11679 struct frame *f = it->f;
11680
11681 /* If line is already filled, do nothing. */
11682 if (it->current_x >= it->last_visible_x)
11683 return;
11684
11685 /* Face extension extends the background and box of IT->face_id
11686 to the end of the line. If the background equals the background
11687 of the frame, we haven't to do anything. */
11688 face = FACE_FROM_ID (f, it->face_id);
11689 if (FRAME_WINDOW_P (f)
11690 && face->box == FACE_NO_BOX
11691 && face->background == FRAME_BACKGROUND_PIXEL (f)
11692 && !face->stipple)
11693 return;
11694
11695 /* Set the glyph row flag indicating that the face of the last glyph
11696 in the text area has to be drawn to the end of the text area. */
11697 it->glyph_row->fill_line_p = 1;
11698
11699 /* If current character of IT is not ASCII, make sure we have the
11700 ASCII face. This will be automatically undone the next time
11701 get_next_display_element returns a multibyte character. Note
11702 that the character will always be single byte in unibyte text. */
11703 if (!SINGLE_BYTE_CHAR_P (it->c))
11704 {
11705 it->face_id = FACE_FOR_CHAR (f, face, 0);
11706 }
11707
11708 if (FRAME_WINDOW_P (f))
11709 {
11710 /* If the row is empty, add a space with the current face of IT,
11711 so that we know which face to draw. */
11712 if (it->glyph_row->used[TEXT_AREA] == 0)
11713 {
11714 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11715 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11716 it->glyph_row->used[TEXT_AREA] = 1;
11717 }
11718 }
11719 else
11720 {
11721 /* Save some values that must not be changed. */
11722 int saved_x = it->current_x;
11723 struct text_pos saved_pos;
11724 Lisp_Object saved_object;
11725 int saved_what = it->what;
11726
11727 saved_object = it->object;
11728 saved_pos = it->position;
11729
11730 it->what = IT_CHARACTER;
11731 bzero (&it->position, sizeof it->position);
11732 it->object = make_number (0);
11733 it->c = ' ';
11734 it->len = 1;
11735
11736 PRODUCE_GLYPHS (it);
11737
11738 while (it->current_x <= it->last_visible_x)
11739 PRODUCE_GLYPHS (it);
11740
11741 /* Don't count these blanks really. It would let us insert a left
11742 truncation glyph below and make us set the cursor on them, maybe. */
11743 it->current_x = saved_x;
11744 it->object = saved_object;
11745 it->position = saved_pos;
11746 it->what = saved_what;
11747 }
11748 }
11749
11750
11751 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11752 trailing whitespace. */
11753
11754 static int
11755 trailing_whitespace_p (charpos)
11756 int charpos;
11757 {
11758 int bytepos = CHAR_TO_BYTE (charpos);
11759 int c = 0;
11760
11761 while (bytepos < ZV_BYTE
11762 && (c = FETCH_CHAR (bytepos),
11763 c == ' ' || c == '\t'))
11764 ++bytepos;
11765
11766 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11767 {
11768 if (bytepos != PT_BYTE)
11769 return 1;
11770 }
11771 return 0;
11772 }
11773
11774
11775 /* Highlight trailing whitespace, if any, in ROW. */
11776
11777 void
11778 highlight_trailing_whitespace (f, row)
11779 struct frame *f;
11780 struct glyph_row *row;
11781 {
11782 int used = row->used[TEXT_AREA];
11783
11784 if (used)
11785 {
11786 struct glyph *start = row->glyphs[TEXT_AREA];
11787 struct glyph *glyph = start + used - 1;
11788
11789 /* Skip over the space glyph inserted to display the
11790 cursor at the end of a line. */
11791 if (glyph->type == CHAR_GLYPH
11792 && glyph->u.ch == ' '
11793 && INTEGERP (glyph->object))
11794 --glyph;
11795
11796 /* If last glyph is a space or stretch, and it's trailing
11797 whitespace, set the face of all trailing whitespace glyphs in
11798 IT->glyph_row to `trailing-whitespace'. */
11799 if (glyph >= start
11800 && BUFFERP (glyph->object)
11801 && (glyph->type == STRETCH_GLYPH
11802 || (glyph->type == CHAR_GLYPH
11803 && glyph->u.ch == ' '))
11804 && trailing_whitespace_p (glyph->charpos))
11805 {
11806 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11807
11808 while (glyph >= start
11809 && BUFFERP (glyph->object)
11810 && (glyph->type == STRETCH_GLYPH
11811 || (glyph->type == CHAR_GLYPH
11812 && glyph->u.ch == ' ')))
11813 (glyph--)->face_id = face_id;
11814 }
11815 }
11816 }
11817
11818
11819 /* Value is non-zero if glyph row ROW in window W should be
11820 used to put the cursor on. */
11821
11822 static int
11823 cursor_row_p (w, row)
11824 struct window *w;
11825 struct glyph_row *row;
11826 {
11827 int cursor_row_p = 1;
11828
11829 if (PT == MATRIX_ROW_END_CHARPOS (row))
11830 {
11831 /* If the row ends with a newline from a string, we don't want
11832 the cursor there (if the row is continued it doesn't end in a
11833 newline). */
11834 if (CHARPOS (row->end.string_pos) >= 0
11835 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11836 cursor_row_p = row->continued_p;
11837
11838 /* If the row ends at ZV, display the cursor at the end of that
11839 row instead of at the start of the row below. */
11840 else if (row->ends_at_zv_p)
11841 cursor_row_p = 1;
11842 else
11843 cursor_row_p = 0;
11844 }
11845
11846 return cursor_row_p;
11847 }
11848
11849
11850 /* Construct the glyph row IT->glyph_row in the desired matrix of
11851 IT->w from text at the current position of IT. See dispextern.h
11852 for an overview of struct it. Value is non-zero if
11853 IT->glyph_row displays text, as opposed to a line displaying ZV
11854 only. */
11855
11856 static int
11857 display_line (it)
11858 struct it *it;
11859 {
11860 struct glyph_row *row = it->glyph_row;
11861
11862 /* We always start displaying at hpos zero even if hscrolled. */
11863 xassert (it->hpos == 0 && it->current_x == 0);
11864
11865 /* We must not display in a row that's not a text row. */
11866 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11867 < it->w->desired_matrix->nrows);
11868
11869 /* Is IT->w showing the region? */
11870 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11871
11872 /* Clear the result glyph row and enable it. */
11873 prepare_desired_row (row);
11874
11875 row->y = it->current_y;
11876 row->start = it->current;
11877 row->continuation_lines_width = it->continuation_lines_width;
11878 row->displays_text_p = 1;
11879 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11880 it->starts_in_middle_of_char_p = 0;
11881
11882 /* Arrange the overlays nicely for our purposes. Usually, we call
11883 display_line on only one line at a time, in which case this
11884 can't really hurt too much, or we call it on lines which appear
11885 one after another in the buffer, in which case all calls to
11886 recenter_overlay_lists but the first will be pretty cheap. */
11887 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11888
11889 /* Move over display elements that are not visible because we are
11890 hscrolled. This may stop at an x-position < IT->first_visible_x
11891 if the first glyph is partially visible or if we hit a line end. */
11892 if (it->current_x < it->first_visible_x)
11893 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11894 MOVE_TO_POS | MOVE_TO_X);
11895
11896 /* Get the initial row height. This is either the height of the
11897 text hscrolled, if there is any, or zero. */
11898 row->ascent = it->max_ascent;
11899 row->height = it->max_ascent + it->max_descent;
11900 row->phys_ascent = it->max_phys_ascent;
11901 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11902
11903 /* Loop generating characters. The loop is left with IT on the next
11904 character to display. */
11905 while (1)
11906 {
11907 int n_glyphs_before, hpos_before, x_before;
11908 int x, i, nglyphs;
11909 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11910
11911 /* Retrieve the next thing to display. Value is zero if end of
11912 buffer reached. */
11913 if (!get_next_display_element (it))
11914 {
11915 /* Maybe add a space at the end of this line that is used to
11916 display the cursor there under X. Set the charpos of the
11917 first glyph of blank lines not corresponding to any text
11918 to -1. */
11919 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11920 || row->used[TEXT_AREA] == 0)
11921 {
11922 row->glyphs[TEXT_AREA]->charpos = -1;
11923 row->displays_text_p = 0;
11924
11925 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11926 row->indicate_empty_line_p = 1;
11927 }
11928
11929 it->continuation_lines_width = 0;
11930 row->ends_at_zv_p = 1;
11931 break;
11932 }
11933
11934 /* Now, get the metrics of what we want to display. This also
11935 generates glyphs in `row' (which is IT->glyph_row). */
11936 n_glyphs_before = row->used[TEXT_AREA];
11937 x = it->current_x;
11938
11939 /* Remember the line height so far in case the next element doesn't
11940 fit on the line. */
11941 if (!it->truncate_lines_p)
11942 {
11943 ascent = it->max_ascent;
11944 descent = it->max_descent;
11945 phys_ascent = it->max_phys_ascent;
11946 phys_descent = it->max_phys_descent;
11947 }
11948
11949 PRODUCE_GLYPHS (it);
11950
11951 /* If this display element was in marginal areas, continue with
11952 the next one. */
11953 if (it->area != TEXT_AREA)
11954 {
11955 row->ascent = max (row->ascent, it->max_ascent);
11956 row->height = max (row->height, it->max_ascent + it->max_descent);
11957 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11958 row->phys_height = max (row->phys_height,
11959 it->max_phys_ascent + it->max_phys_descent);
11960 set_iterator_to_next (it, 1);
11961 continue;
11962 }
11963
11964 /* Does the display element fit on the line? If we truncate
11965 lines, we should draw past the right edge of the window. If
11966 we don't truncate, we want to stop so that we can display the
11967 continuation glyph before the right margin. If lines are
11968 continued, there are two possible strategies for characters
11969 resulting in more than 1 glyph (e.g. tabs): Display as many
11970 glyphs as possible in this line and leave the rest for the
11971 continuation line, or display the whole element in the next
11972 line. Original redisplay did the former, so we do it also. */
11973 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11974 hpos_before = it->hpos;
11975 x_before = x;
11976
11977 if (nglyphs == 1
11978 && it->current_x < it->last_visible_x)
11979 {
11980 ++it->hpos;
11981 row->ascent = max (row->ascent, it->max_ascent);
11982 row->height = max (row->height, it->max_ascent + it->max_descent);
11983 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11984 row->phys_height = max (row->phys_height,
11985 it->max_phys_ascent + it->max_phys_descent);
11986 if (it->current_x - it->pixel_width < it->first_visible_x)
11987 row->x = x - it->first_visible_x;
11988 }
11989 else
11990 {
11991 int new_x;
11992 struct glyph *glyph;
11993
11994 for (i = 0; i < nglyphs; ++i, x = new_x)
11995 {
11996 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11997 new_x = x + glyph->pixel_width;
11998
11999 if (/* Lines are continued. */
12000 !it->truncate_lines_p
12001 && (/* Glyph doesn't fit on the line. */
12002 new_x > it->last_visible_x
12003 /* Or it fits exactly on a window system frame. */
12004 || (new_x == it->last_visible_x
12005 && FRAME_WINDOW_P (it->f))))
12006 {
12007 /* End of a continued line. */
12008
12009 if (it->hpos == 0
12010 || (new_x == it->last_visible_x
12011 && FRAME_WINDOW_P (it->f)))
12012 {
12013 /* Current glyph is the only one on the line or
12014 fits exactly on the line. We must continue
12015 the line because we can't draw the cursor
12016 after the glyph. */
12017 row->continued_p = 1;
12018 it->current_x = new_x;
12019 it->continuation_lines_width += new_x;
12020 ++it->hpos;
12021 if (i == nglyphs - 1)
12022 set_iterator_to_next (it, 1);
12023 }
12024 else if (CHAR_GLYPH_PADDING_P (*glyph)
12025 && !FRAME_WINDOW_P (it->f))
12026 {
12027 /* A padding glyph that doesn't fit on this line.
12028 This means the whole character doesn't fit
12029 on the line. */
12030 row->used[TEXT_AREA] = n_glyphs_before;
12031
12032 /* Fill the rest of the row with continuation
12033 glyphs like in 20.x. */
12034 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12035 < row->glyphs[1 + TEXT_AREA])
12036 produce_special_glyphs (it, IT_CONTINUATION);
12037
12038 row->continued_p = 1;
12039 it->current_x = x_before;
12040 it->continuation_lines_width += x_before;
12041
12042 /* Restore the height to what it was before the
12043 element not fitting on the line. */
12044 it->max_ascent = ascent;
12045 it->max_descent = descent;
12046 it->max_phys_ascent = phys_ascent;
12047 it->max_phys_descent = phys_descent;
12048 }
12049 else
12050 {
12051 /* Display element draws past the right edge of
12052 the window. Restore positions to values
12053 before the element. The next line starts
12054 with current_x before the glyph that could
12055 not be displayed, so that TAB works right. */
12056 row->used[TEXT_AREA] = n_glyphs_before + i;
12057
12058 /* Display continuation glyphs. */
12059 if (!FRAME_WINDOW_P (it->f))
12060 produce_special_glyphs (it, IT_CONTINUATION);
12061 row->continued_p = 1;
12062
12063 it->current_x = x;
12064 it->continuation_lines_width += x;
12065 if (nglyphs > 1 && i > 0)
12066 {
12067 row->ends_in_middle_of_char_p = 1;
12068 it->starts_in_middle_of_char_p = 1;
12069 }
12070
12071 /* Restore the height to what it was before the
12072 element not fitting on the line. */
12073 it->max_ascent = ascent;
12074 it->max_descent = descent;
12075 it->max_phys_ascent = phys_ascent;
12076 it->max_phys_descent = phys_descent;
12077 }
12078
12079 break;
12080 }
12081 else if (new_x > it->first_visible_x)
12082 {
12083 /* Increment number of glyphs actually displayed. */
12084 ++it->hpos;
12085
12086 if (x < it->first_visible_x)
12087 /* Glyph is partially visible, i.e. row starts at
12088 negative X position. */
12089 row->x = x - it->first_visible_x;
12090 }
12091 else
12092 {
12093 /* Glyph is completely off the left margin of the
12094 window. This should not happen because of the
12095 move_it_in_display_line at the start of
12096 this function. */
12097 abort ();
12098 }
12099 }
12100
12101 row->ascent = max (row->ascent, it->max_ascent);
12102 row->height = max (row->height, it->max_ascent + it->max_descent);
12103 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12104 row->phys_height = max (row->phys_height,
12105 it->max_phys_ascent + it->max_phys_descent);
12106
12107 /* End of this display line if row is continued. */
12108 if (row->continued_p)
12109 break;
12110 }
12111
12112 /* Is this a line end? If yes, we're also done, after making
12113 sure that a non-default face is extended up to the right
12114 margin of the window. */
12115 if (ITERATOR_AT_END_OF_LINE_P (it))
12116 {
12117 int used_before = row->used[TEXT_AREA];
12118
12119 /* Add a space at the end of the line that is used to
12120 display the cursor there. */
12121 append_space (it, 0);
12122
12123 /* Extend the face to the end of the line. */
12124 extend_face_to_end_of_line (it);
12125
12126 /* Make sure we have the position. */
12127 if (used_before == 0)
12128 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12129
12130 /* Consume the line end. This skips over invisible lines. */
12131 set_iterator_to_next (it, 1);
12132 it->continuation_lines_width = 0;
12133 break;
12134 }
12135
12136 /* Proceed with next display element. Note that this skips
12137 over lines invisible because of selective display. */
12138 set_iterator_to_next (it, 1);
12139
12140 /* If we truncate lines, we are done when the last displayed
12141 glyphs reach past the right margin of the window. */
12142 if (it->truncate_lines_p
12143 && (FRAME_WINDOW_P (it->f)
12144 ? (it->current_x >= it->last_visible_x)
12145 : (it->current_x > it->last_visible_x)))
12146 {
12147 /* Maybe add truncation glyphs. */
12148 if (!FRAME_WINDOW_P (it->f))
12149 {
12150 --it->glyph_row->used[TEXT_AREA];
12151 produce_special_glyphs (it, IT_TRUNCATION);
12152 }
12153
12154 row->truncated_on_right_p = 1;
12155 it->continuation_lines_width = 0;
12156 reseat_at_next_visible_line_start (it, 0);
12157 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12158 it->hpos = hpos_before;
12159 it->current_x = x_before;
12160 break;
12161 }
12162 }
12163
12164 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12165 at the left window margin. */
12166 if (it->first_visible_x
12167 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12168 {
12169 if (!FRAME_WINDOW_P (it->f))
12170 insert_left_trunc_glyphs (it);
12171 row->truncated_on_left_p = 1;
12172 }
12173
12174 /* If the start of this line is the overlay arrow-position, then
12175 mark this glyph row as the one containing the overlay arrow.
12176 This is clearly a mess with variable size fonts. It would be
12177 better to let it be displayed like cursors under X. */
12178 if (MARKERP (Voverlay_arrow_position)
12179 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12180 && (MATRIX_ROW_START_CHARPOS (row)
12181 == marker_position (Voverlay_arrow_position))
12182 && STRINGP (Voverlay_arrow_string)
12183 && ! overlay_arrow_seen)
12184 {
12185 /* Overlay arrow in window redisplay is a bitmap. */
12186 if (!FRAME_WINDOW_P (it->f))
12187 {
12188 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12189 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12190 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12191 struct glyph *p = row->glyphs[TEXT_AREA];
12192 struct glyph *p2, *end;
12193
12194 /* Copy the arrow glyphs. */
12195 while (glyph < arrow_end)
12196 *p++ = *glyph++;
12197
12198 /* Throw away padding glyphs. */
12199 p2 = p;
12200 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12201 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12202 ++p2;
12203 if (p2 > p)
12204 {
12205 while (p2 < end)
12206 *p++ = *p2++;
12207 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12208 }
12209 }
12210
12211 overlay_arrow_seen = 1;
12212 row->overlay_arrow_p = 1;
12213 }
12214
12215 /* Compute pixel dimensions of this line. */
12216 compute_line_metrics (it);
12217
12218 /* Remember the position at which this line ends. */
12219 row->end = it->current;
12220
12221 /* Maybe set the cursor. */
12222 if (it->w->cursor.vpos < 0
12223 && PT >= MATRIX_ROW_START_CHARPOS (row)
12224 && PT <= MATRIX_ROW_END_CHARPOS (row)
12225 && cursor_row_p (it->w, row))
12226 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12227
12228 /* Highlight trailing whitespace. */
12229 if (!NILP (Vshow_trailing_whitespace))
12230 highlight_trailing_whitespace (it->f, it->glyph_row);
12231
12232 /* Prepare for the next line. This line starts horizontally at (X
12233 HPOS) = (0 0). Vertical positions are incremented. As a
12234 convenience for the caller, IT->glyph_row is set to the next
12235 row to be used. */
12236 it->current_x = it->hpos = 0;
12237 it->current_y += row->height;
12238 ++it->vpos;
12239 ++it->glyph_row;
12240 return row->displays_text_p;
12241 }
12242
12243
12244 \f
12245 /***********************************************************************
12246 Menu Bar
12247 ***********************************************************************/
12248
12249 /* Redisplay the menu bar in the frame for window W.
12250
12251 The menu bar of X frames that don't have X toolkit support is
12252 displayed in a special window W->frame->menu_bar_window.
12253
12254 The menu bar of terminal frames is treated specially as far as
12255 glyph matrices are concerned. Menu bar lines are not part of
12256 windows, so the update is done directly on the frame matrix rows
12257 for the menu bar. */
12258
12259 static void
12260 display_menu_bar (w)
12261 struct window *w;
12262 {
12263 struct frame *f = XFRAME (WINDOW_FRAME (w));
12264 struct it it;
12265 Lisp_Object items;
12266 int i;
12267
12268 /* Don't do all this for graphical frames. */
12269 #ifdef HAVE_NTGUI
12270 if (!NILP (Vwindow_system))
12271 return;
12272 #endif
12273 #ifdef USE_X_TOOLKIT
12274 if (FRAME_X_P (f))
12275 return;
12276 #endif
12277 #ifdef macintosh
12278 if (FRAME_MAC_P (f))
12279 return;
12280 #endif
12281
12282 #ifdef USE_X_TOOLKIT
12283 xassert (!FRAME_WINDOW_P (f));
12284 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12285 it.first_visible_x = 0;
12286 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12287 #else /* not USE_X_TOOLKIT */
12288 if (FRAME_WINDOW_P (f))
12289 {
12290 /* Menu bar lines are displayed in the desired matrix of the
12291 dummy window menu_bar_window. */
12292 struct window *menu_w;
12293 xassert (WINDOWP (f->menu_bar_window));
12294 menu_w = XWINDOW (f->menu_bar_window);
12295 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12296 MENU_FACE_ID);
12297 it.first_visible_x = 0;
12298 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12299 }
12300 else
12301 {
12302 /* This is a TTY frame, i.e. character hpos/vpos are used as
12303 pixel x/y. */
12304 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12305 MENU_FACE_ID);
12306 it.first_visible_x = 0;
12307 it.last_visible_x = FRAME_WIDTH (f);
12308 }
12309 #endif /* not USE_X_TOOLKIT */
12310
12311 /* Clear all rows of the menu bar. */
12312 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12313 {
12314 struct glyph_row *row = it.glyph_row + i;
12315 clear_glyph_row (row);
12316 row->enabled_p = 1;
12317 row->full_width_p = 1;
12318 }
12319
12320 /* Make the first line of the menu bar appear in reverse video. */
12321 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12322
12323 /* Display all items of the menu bar. */
12324 items = FRAME_MENU_BAR_ITEMS (it.f);
12325 for (i = 0; i < XVECTOR (items)->size; i += 4)
12326 {
12327 Lisp_Object string;
12328
12329 /* Stop at nil string. */
12330 string = XVECTOR (items)->contents[i + 1];
12331 if (NILP (string))
12332 break;
12333
12334 /* Remember where item was displayed. */
12335 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12336
12337 /* Display the item, pad with one space. */
12338 if (it.current_x < it.last_visible_x)
12339 display_string (NULL, string, Qnil, 0, 0, &it,
12340 XSTRING (string)->size + 1, 0, 0, -1);
12341 }
12342
12343 /* Fill out the line with spaces. */
12344 if (it.current_x < it.last_visible_x)
12345 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12346
12347 /* Compute the total height of the lines. */
12348 compute_line_metrics (&it);
12349 }
12350
12351
12352 \f
12353 /***********************************************************************
12354 Mode Line
12355 ***********************************************************************/
12356
12357 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12358 FORCE is non-zero, redisplay mode lines unconditionally.
12359 Otherwise, redisplay only mode lines that are garbaged. Value is
12360 the number of windows whose mode lines were redisplayed. */
12361
12362 static int
12363 redisplay_mode_lines (window, force)
12364 Lisp_Object window;
12365 int force;
12366 {
12367 int nwindows = 0;
12368
12369 while (!NILP (window))
12370 {
12371 struct window *w = XWINDOW (window);
12372
12373 if (WINDOWP (w->hchild))
12374 nwindows += redisplay_mode_lines (w->hchild, force);
12375 else if (WINDOWP (w->vchild))
12376 nwindows += redisplay_mode_lines (w->vchild, force);
12377 else if (force
12378 || FRAME_GARBAGED_P (XFRAME (w->frame))
12379 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12380 {
12381 Lisp_Object old_selected_frame;
12382 struct text_pos lpoint;
12383 struct buffer *old = current_buffer;
12384
12385 /* Set the window's buffer for the mode line display. */
12386 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12387 set_buffer_internal_1 (XBUFFER (w->buffer));
12388
12389 /* Point refers normally to the selected window. For any
12390 other window, set up appropriate value. */
12391 if (!EQ (window, selected_window))
12392 {
12393 struct text_pos pt;
12394
12395 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12396 if (CHARPOS (pt) < BEGV)
12397 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12398 else if (CHARPOS (pt) > (ZV - 1))
12399 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12400 else
12401 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12402 }
12403
12404 /* Temporarily set up the selected frame. */
12405 old_selected_frame = selected_frame;
12406 selected_frame = w->frame;
12407
12408 /* Display mode lines. */
12409 clear_glyph_matrix (w->desired_matrix);
12410 if (display_mode_lines (w))
12411 {
12412 ++nwindows;
12413 w->must_be_updated_p = 1;
12414 }
12415
12416 /* Restore old settings. */
12417 selected_frame = old_selected_frame;
12418 set_buffer_internal_1 (old);
12419 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12420 }
12421
12422 window = w->next;
12423 }
12424
12425 return nwindows;
12426 }
12427
12428
12429 /* Display the mode and/or top line of window W. Value is the number
12430 of mode lines displayed. */
12431
12432 static int
12433 display_mode_lines (w)
12434 struct window *w;
12435 {
12436 int n = 0;
12437
12438 /* These will be set while the mode line specs are processed. */
12439 line_number_displayed = 0;
12440 w->column_number_displayed = Qnil;
12441
12442 if (WINDOW_WANTS_MODELINE_P (w))
12443 {
12444 display_mode_line (w, MODE_LINE_FACE_ID,
12445 current_buffer->mode_line_format);
12446 ++n;
12447 }
12448
12449 if (WINDOW_WANTS_HEADER_LINE_P (w))
12450 {
12451 display_mode_line (w, HEADER_LINE_FACE_ID,
12452 current_buffer->header_line_format);
12453 ++n;
12454 }
12455
12456 return n;
12457 }
12458
12459
12460 /* Display mode or top line of window W. FACE_ID specifies which line
12461 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12462 FORMAT is the mode line format to display. Value is the pixel
12463 height of the mode line displayed. */
12464
12465 static int
12466 display_mode_line (w, face_id, format)
12467 struct window *w;
12468 enum face_id face_id;
12469 Lisp_Object format;
12470 {
12471 struct it it;
12472 struct face *face;
12473
12474 init_iterator (&it, w, -1, -1, NULL, face_id);
12475 prepare_desired_row (it.glyph_row);
12476
12477 /* Temporarily make frame's keyboard the current kboard so that
12478 kboard-local variables in the mode_line_format will get the right
12479 values. */
12480 push_frame_kboard (it.f);
12481 display_mode_element (&it, 0, 0, 0, format);
12482 pop_frame_kboard ();
12483
12484 /* Fill up with spaces. */
12485 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12486
12487 compute_line_metrics (&it);
12488 it.glyph_row->full_width_p = 1;
12489 it.glyph_row->mode_line_p = 1;
12490 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12491 it.glyph_row->continued_p = 0;
12492 it.glyph_row->truncated_on_left_p = 0;
12493 it.glyph_row->truncated_on_right_p = 0;
12494
12495 /* Make a 3D mode-line have a shadow at its right end. */
12496 face = FACE_FROM_ID (it.f, face_id);
12497 extend_face_to_end_of_line (&it);
12498 if (face->box != FACE_NO_BOX)
12499 {
12500 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12501 + it.glyph_row->used[TEXT_AREA] - 1);
12502 last->right_box_line_p = 1;
12503 }
12504
12505 return it.glyph_row->height;
12506 }
12507
12508
12509 /* Contribute ELT to the mode line for window IT->w. How it
12510 translates into text depends on its data type.
12511
12512 IT describes the display environment in which we display, as usual.
12513
12514 DEPTH is the depth in recursion. It is used to prevent
12515 infinite recursion here.
12516
12517 FIELD_WIDTH is the number of characters the display of ELT should
12518 occupy in the mode line, and PRECISION is the maximum number of
12519 characters to display from ELT's representation. See
12520 display_string for details. *
12521
12522 Returns the hpos of the end of the text generated by ELT. */
12523
12524 static int
12525 display_mode_element (it, depth, field_width, precision, elt)
12526 struct it *it;
12527 int depth;
12528 int field_width, precision;
12529 Lisp_Object elt;
12530 {
12531 int n = 0, field, prec;
12532
12533 tail_recurse:
12534 if (depth > 10)
12535 goto invalid;
12536
12537 depth++;
12538
12539 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12540 {
12541 case Lisp_String:
12542 {
12543 /* A string: output it and check for %-constructs within it. */
12544 unsigned char c;
12545 unsigned char *this = XSTRING (elt)->data;
12546 unsigned char *lisp_string = this;
12547
12548 while ((precision <= 0 || n < precision)
12549 && *this
12550 && (frame_title_ptr
12551 || it->current_x < it->last_visible_x))
12552 {
12553 unsigned char *last = this;
12554
12555 /* Advance to end of string or next format specifier. */
12556 while ((c = *this++) != '\0' && c != '%')
12557 ;
12558
12559 if (this - 1 != last)
12560 {
12561 /* Output to end of string or up to '%'. Field width
12562 is length of string. Don't output more than
12563 PRECISION allows us. */
12564 prec = --this - last;
12565 if (precision > 0 && prec > precision - n)
12566 prec = precision - n;
12567
12568 if (frame_title_ptr)
12569 n += store_frame_title (last, prec, prec);
12570 else
12571 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12572 it, 0, prec, 0, -1);
12573 }
12574 else /* c == '%' */
12575 {
12576 unsigned char *percent_position = this;
12577
12578 /* Get the specified minimum width. Zero means
12579 don't pad. */
12580 field = 0;
12581 while ((c = *this++) >= '0' && c <= '9')
12582 field = field * 10 + c - '0';
12583
12584 /* Don't pad beyond the total padding allowed. */
12585 if (field_width - n > 0 && field > field_width - n)
12586 field = field_width - n;
12587
12588 /* Note that either PRECISION <= 0 or N < PRECISION. */
12589 prec = precision - n;
12590
12591 if (c == 'M')
12592 n += display_mode_element (it, depth, field, prec,
12593 Vglobal_mode_string);
12594 else if (c != 0)
12595 {
12596 unsigned char *spec
12597 = decode_mode_spec (it->w, c, field, prec);
12598
12599 if (frame_title_ptr)
12600 n += store_frame_title (spec, field, prec);
12601 else
12602 {
12603 int nglyphs_before
12604 = it->glyph_row->used[TEXT_AREA];
12605 int charpos
12606 = percent_position - XSTRING (elt)->data;
12607 int nwritten
12608 = display_string (spec, Qnil, elt, charpos, 0, it,
12609 field, prec, 0, -1);
12610
12611 /* Assign to the glyphs written above the
12612 string where the `%x' came from, position
12613 of the `%'. */
12614 if (nwritten > 0)
12615 {
12616 struct glyph *glyph
12617 = (it->glyph_row->glyphs[TEXT_AREA]
12618 + nglyphs_before);
12619 int i;
12620
12621 for (i = 0; i < nwritten; ++i)
12622 {
12623 glyph[i].object = elt;
12624 glyph[i].charpos = charpos;
12625 }
12626
12627 n += nwritten;
12628 }
12629 }
12630 }
12631 }
12632 }
12633 }
12634 break;
12635
12636 case Lisp_Symbol:
12637 /* A symbol: process the value of the symbol recursively
12638 as if it appeared here directly. Avoid error if symbol void.
12639 Special case: if value of symbol is a string, output the string
12640 literally. */
12641 {
12642 register Lisp_Object tem;
12643 tem = Fboundp (elt);
12644 if (!NILP (tem))
12645 {
12646 tem = Fsymbol_value (elt);
12647 /* If value is a string, output that string literally:
12648 don't check for % within it. */
12649 if (STRINGP (tem))
12650 {
12651 prec = XSTRING (tem)->size;
12652 if (precision > 0 && prec > precision - n)
12653 prec = precision - n;
12654 if (frame_title_ptr)
12655 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12656 else
12657 n += display_string (NULL, tem, Qnil, 0, 0, it,
12658 0, prec, 0, -1);
12659 }
12660 else if (!EQ (tem, elt))
12661 {
12662 /* Give up right away for nil or t. */
12663 elt = tem;
12664 goto tail_recurse;
12665 }
12666 }
12667 }
12668 break;
12669
12670 case Lisp_Cons:
12671 {
12672 register Lisp_Object car, tem;
12673
12674 /* A cons cell: three distinct cases.
12675 If first element is a string or a cons, process all the elements
12676 and effectively concatenate them.
12677 If first element is a negative number, truncate displaying cdr to
12678 at most that many characters. If positive, pad (with spaces)
12679 to at least that many characters.
12680 If first element is a symbol, process the cadr or caddr recursively
12681 according to whether the symbol's value is non-nil or nil. */
12682 car = XCAR (elt);
12683 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12684 {
12685 /* An element of the form (:eval FORM) means evaluate FORM
12686 and use the result as mode line elements. */
12687 struct gcpro gcpro1;
12688 Lisp_Object spec;
12689
12690 spec = safe_eval (XCAR (XCDR (elt)));
12691 GCPRO1 (spec);
12692 n += display_mode_element (it, depth, field_width - n,
12693 precision - n, spec);
12694 UNGCPRO;
12695 }
12696 else if (SYMBOLP (car))
12697 {
12698 tem = Fboundp (car);
12699 elt = XCDR (elt);
12700 if (!CONSP (elt))
12701 goto invalid;
12702 /* elt is now the cdr, and we know it is a cons cell.
12703 Use its car if CAR has a non-nil value. */
12704 if (!NILP (tem))
12705 {
12706 tem = Fsymbol_value (car);
12707 if (!NILP (tem))
12708 {
12709 elt = XCAR (elt);
12710 goto tail_recurse;
12711 }
12712 }
12713 /* Symbol's value is nil (or symbol is unbound)
12714 Get the cddr of the original list
12715 and if possible find the caddr and use that. */
12716 elt = XCDR (elt);
12717 if (NILP (elt))
12718 break;
12719 else if (!CONSP (elt))
12720 goto invalid;
12721 elt = XCAR (elt);
12722 goto tail_recurse;
12723 }
12724 else if (INTEGERP (car))
12725 {
12726 register int lim = XINT (car);
12727 elt = XCDR (elt);
12728 if (lim < 0)
12729 {
12730 /* Negative int means reduce maximum width. */
12731 if (precision <= 0)
12732 precision = -lim;
12733 else
12734 precision = min (precision, -lim);
12735 }
12736 else if (lim > 0)
12737 {
12738 /* Padding specified. Don't let it be more than
12739 current maximum. */
12740 if (precision > 0)
12741 lim = min (precision, lim);
12742
12743 /* If that's more padding than already wanted, queue it.
12744 But don't reduce padding already specified even if
12745 that is beyond the current truncation point. */
12746 field_width = max (lim, field_width);
12747 }
12748 goto tail_recurse;
12749 }
12750 else if (STRINGP (car) || CONSP (car))
12751 {
12752 register int limit = 50;
12753 /* Limit is to protect against circular lists. */
12754 while (CONSP (elt)
12755 && --limit > 0
12756 && (precision <= 0 || n < precision))
12757 {
12758 n += display_mode_element (it, depth, field_width - n,
12759 precision - n, XCAR (elt));
12760 elt = XCDR (elt);
12761 }
12762 }
12763 }
12764 break;
12765
12766 default:
12767 invalid:
12768 if (frame_title_ptr)
12769 n += store_frame_title ("*invalid*", 0, precision - n);
12770 else
12771 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12772 precision - n, 0, 0);
12773 return n;
12774 }
12775
12776 /* Pad to FIELD_WIDTH. */
12777 if (field_width > 0 && n < field_width)
12778 {
12779 if (frame_title_ptr)
12780 n += store_frame_title ("", field_width - n, 0);
12781 else
12782 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12783 0, 0, 0);
12784 }
12785
12786 return n;
12787 }
12788
12789
12790 /* Write a null-terminated, right justified decimal representation of
12791 the positive integer D to BUF using a minimal field width WIDTH. */
12792
12793 static void
12794 pint2str (buf, width, d)
12795 register char *buf;
12796 register int width;
12797 register int d;
12798 {
12799 register char *p = buf;
12800
12801 if (d <= 0)
12802 *p++ = '0';
12803 else
12804 {
12805 while (d > 0)
12806 {
12807 *p++ = d % 10 + '0';
12808 d /= 10;
12809 }
12810 }
12811
12812 for (width -= (int) (p - buf); width > 0; --width)
12813 *p++ = ' ';
12814 *p-- = '\0';
12815 while (p > buf)
12816 {
12817 d = *buf;
12818 *buf++ = *p;
12819 *p-- = d;
12820 }
12821 }
12822
12823 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12824 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12825 type of CODING_SYSTEM. Return updated pointer into BUF. */
12826
12827 static unsigned char invalid_eol_type[] = "(*invalid*)";
12828
12829 static char *
12830 decode_mode_spec_coding (coding_system, buf, eol_flag)
12831 Lisp_Object coding_system;
12832 register char *buf;
12833 int eol_flag;
12834 {
12835 Lisp_Object val;
12836 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12837 unsigned char *eol_str;
12838 int eol_str_len;
12839 /* The EOL conversion we are using. */
12840 Lisp_Object eoltype;
12841
12842 val = Fget (coding_system, Qcoding_system);
12843 eoltype = Qnil;
12844
12845 if (!VECTORP (val)) /* Not yet decided. */
12846 {
12847 if (multibyte)
12848 *buf++ = '-';
12849 if (eol_flag)
12850 eoltype = eol_mnemonic_undecided;
12851 /* Don't mention EOL conversion if it isn't decided. */
12852 }
12853 else
12854 {
12855 Lisp_Object eolvalue;
12856
12857 eolvalue = Fget (coding_system, Qeol_type);
12858
12859 if (multibyte)
12860 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12861
12862 if (eol_flag)
12863 {
12864 /* The EOL conversion that is normal on this system. */
12865
12866 if (NILP (eolvalue)) /* Not yet decided. */
12867 eoltype = eol_mnemonic_undecided;
12868 else if (VECTORP (eolvalue)) /* Not yet decided. */
12869 eoltype = eol_mnemonic_undecided;
12870 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12871 eoltype = (XFASTINT (eolvalue) == 0
12872 ? eol_mnemonic_unix
12873 : (XFASTINT (eolvalue) == 1
12874 ? eol_mnemonic_dos : eol_mnemonic_mac));
12875 }
12876 }
12877
12878 if (eol_flag)
12879 {
12880 /* Mention the EOL conversion if it is not the usual one. */
12881 if (STRINGP (eoltype))
12882 {
12883 eol_str = XSTRING (eoltype)->data;
12884 eol_str_len = XSTRING (eoltype)->size;
12885 }
12886 else if (INTEGERP (eoltype)
12887 && CHAR_VALID_P (XINT (eoltype), 0))
12888 {
12889 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12890 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12891 }
12892 else
12893 {
12894 eol_str = invalid_eol_type;
12895 eol_str_len = sizeof (invalid_eol_type) - 1;
12896 }
12897 bcopy (eol_str, buf, eol_str_len);
12898 buf += eol_str_len;
12899 }
12900
12901 return buf;
12902 }
12903
12904 /* Return a string for the output of a mode line %-spec for window W,
12905 generated by character C. PRECISION >= 0 means don't return a
12906 string longer than that value. FIELD_WIDTH > 0 means pad the
12907 string returned with spaces to that value. */
12908
12909 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12910
12911 static char *
12912 decode_mode_spec (w, c, field_width, precision)
12913 struct window *w;
12914 register int c;
12915 int field_width, precision;
12916 {
12917 Lisp_Object obj;
12918 struct frame *f = XFRAME (WINDOW_FRAME (w));
12919 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12920 struct buffer *b = XBUFFER (w->buffer);
12921
12922 obj = Qnil;
12923
12924 switch (c)
12925 {
12926 case '*':
12927 if (!NILP (b->read_only))
12928 return "%";
12929 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12930 return "*";
12931 return "-";
12932
12933 case '+':
12934 /* This differs from %* only for a modified read-only buffer. */
12935 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12936 return "*";
12937 if (!NILP (b->read_only))
12938 return "%";
12939 return "-";
12940
12941 case '&':
12942 /* This differs from %* in ignoring read-only-ness. */
12943 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12944 return "*";
12945 return "-";
12946
12947 case '%':
12948 return "%";
12949
12950 case '[':
12951 {
12952 int i;
12953 char *p;
12954
12955 if (command_loop_level > 5)
12956 return "[[[... ";
12957 p = decode_mode_spec_buf;
12958 for (i = 0; i < command_loop_level; i++)
12959 *p++ = '[';
12960 *p = 0;
12961 return decode_mode_spec_buf;
12962 }
12963
12964 case ']':
12965 {
12966 int i;
12967 char *p;
12968
12969 if (command_loop_level > 5)
12970 return " ...]]]";
12971 p = decode_mode_spec_buf;
12972 for (i = 0; i < command_loop_level; i++)
12973 *p++ = ']';
12974 *p = 0;
12975 return decode_mode_spec_buf;
12976 }
12977
12978 case '-':
12979 {
12980 register int i;
12981
12982 /* Let lots_of_dashes be a string of infinite length. */
12983 if (field_width <= 0
12984 || field_width > sizeof (lots_of_dashes))
12985 {
12986 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12987 decode_mode_spec_buf[i] = '-';
12988 decode_mode_spec_buf[i] = '\0';
12989 return decode_mode_spec_buf;
12990 }
12991 else
12992 return lots_of_dashes;
12993 }
12994
12995 case 'b':
12996 obj = b->name;
12997 break;
12998
12999 case 'c':
13000 {
13001 int col = current_column ();
13002 XSETFASTINT (w->column_number_displayed, col);
13003 pint2str (decode_mode_spec_buf, field_width, col);
13004 return decode_mode_spec_buf;
13005 }
13006
13007 case 'F':
13008 /* %F displays the frame name. */
13009 if (!NILP (f->title))
13010 return (char *) XSTRING (f->title)->data;
13011 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13012 return (char *) XSTRING (f->name)->data;
13013 return "Emacs";
13014
13015 case 'f':
13016 obj = b->filename;
13017 break;
13018
13019 case 'l':
13020 {
13021 int startpos = XMARKER (w->start)->charpos;
13022 int startpos_byte = marker_byte_position (w->start);
13023 int line, linepos, linepos_byte, topline;
13024 int nlines, junk;
13025 int height = XFASTINT (w->height);
13026
13027 /* If we decided that this buffer isn't suitable for line numbers,
13028 don't forget that too fast. */
13029 if (EQ (w->base_line_pos, w->buffer))
13030 goto no_value;
13031 /* But do forget it, if the window shows a different buffer now. */
13032 else if (BUFFERP (w->base_line_pos))
13033 w->base_line_pos = Qnil;
13034
13035 /* If the buffer is very big, don't waste time. */
13036 if (INTEGERP (Vline_number_display_limit)
13037 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13038 {
13039 w->base_line_pos = Qnil;
13040 w->base_line_number = Qnil;
13041 goto no_value;
13042 }
13043
13044 if (!NILP (w->base_line_number)
13045 && !NILP (w->base_line_pos)
13046 && XFASTINT (w->base_line_pos) <= startpos)
13047 {
13048 line = XFASTINT (w->base_line_number);
13049 linepos = XFASTINT (w->base_line_pos);
13050 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13051 }
13052 else
13053 {
13054 line = 1;
13055 linepos = BUF_BEGV (b);
13056 linepos_byte = BUF_BEGV_BYTE (b);
13057 }
13058
13059 /* Count lines from base line to window start position. */
13060 nlines = display_count_lines (linepos, linepos_byte,
13061 startpos_byte,
13062 startpos, &junk);
13063
13064 topline = nlines + line;
13065
13066 /* Determine a new base line, if the old one is too close
13067 or too far away, or if we did not have one.
13068 "Too close" means it's plausible a scroll-down would
13069 go back past it. */
13070 if (startpos == BUF_BEGV (b))
13071 {
13072 XSETFASTINT (w->base_line_number, topline);
13073 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13074 }
13075 else if (nlines < height + 25 || nlines > height * 3 + 50
13076 || linepos == BUF_BEGV (b))
13077 {
13078 int limit = BUF_BEGV (b);
13079 int limit_byte = BUF_BEGV_BYTE (b);
13080 int position;
13081 int distance = (height * 2 + 30) * line_number_display_limit_width;
13082
13083 if (startpos - distance > limit)
13084 {
13085 limit = startpos - distance;
13086 limit_byte = CHAR_TO_BYTE (limit);
13087 }
13088
13089 nlines = display_count_lines (startpos, startpos_byte,
13090 limit_byte,
13091 - (height * 2 + 30),
13092 &position);
13093 /* If we couldn't find the lines we wanted within
13094 line_number_display_limit_width chars per line,
13095 give up on line numbers for this window. */
13096 if (position == limit_byte && limit == startpos - distance)
13097 {
13098 w->base_line_pos = w->buffer;
13099 w->base_line_number = Qnil;
13100 goto no_value;
13101 }
13102
13103 XSETFASTINT (w->base_line_number, topline - nlines);
13104 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13105 }
13106
13107 /* Now count lines from the start pos to point. */
13108 nlines = display_count_lines (startpos, startpos_byte,
13109 PT_BYTE, PT, &junk);
13110
13111 /* Record that we did display the line number. */
13112 line_number_displayed = 1;
13113
13114 /* Make the string to show. */
13115 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13116 return decode_mode_spec_buf;
13117 no_value:
13118 {
13119 char* p = decode_mode_spec_buf;
13120 int pad = field_width - 2;
13121 while (pad-- > 0)
13122 *p++ = ' ';
13123 *p++ = '?';
13124 *p++ = '?';
13125 *p = '\0';
13126 return decode_mode_spec_buf;
13127 }
13128 }
13129 break;
13130
13131 case 'm':
13132 obj = b->mode_name;
13133 break;
13134
13135 case 'n':
13136 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13137 return " Narrow";
13138 break;
13139
13140 case 'p':
13141 {
13142 int pos = marker_position (w->start);
13143 int total = BUF_ZV (b) - BUF_BEGV (b);
13144
13145 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13146 {
13147 if (pos <= BUF_BEGV (b))
13148 return "All";
13149 else
13150 return "Bottom";
13151 }
13152 else if (pos <= BUF_BEGV (b))
13153 return "Top";
13154 else
13155 {
13156 if (total > 1000000)
13157 /* Do it differently for a large value, to avoid overflow. */
13158 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13159 else
13160 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13161 /* We can't normally display a 3-digit number,
13162 so get us a 2-digit number that is close. */
13163 if (total == 100)
13164 total = 99;
13165 sprintf (decode_mode_spec_buf, "%2d%%", total);
13166 return decode_mode_spec_buf;
13167 }
13168 }
13169
13170 /* Display percentage of size above the bottom of the screen. */
13171 case 'P':
13172 {
13173 int toppos = marker_position (w->start);
13174 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13175 int total = BUF_ZV (b) - BUF_BEGV (b);
13176
13177 if (botpos >= BUF_ZV (b))
13178 {
13179 if (toppos <= BUF_BEGV (b))
13180 return "All";
13181 else
13182 return "Bottom";
13183 }
13184 else
13185 {
13186 if (total > 1000000)
13187 /* Do it differently for a large value, to avoid overflow. */
13188 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13189 else
13190 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13191 /* We can't normally display a 3-digit number,
13192 so get us a 2-digit number that is close. */
13193 if (total == 100)
13194 total = 99;
13195 if (toppos <= BUF_BEGV (b))
13196 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13197 else
13198 sprintf (decode_mode_spec_buf, "%2d%%", total);
13199 return decode_mode_spec_buf;
13200 }
13201 }
13202
13203 case 's':
13204 /* status of process */
13205 obj = Fget_buffer_process (w->buffer);
13206 if (NILP (obj))
13207 return "no process";
13208 #ifdef subprocesses
13209 obj = Fsymbol_name (Fprocess_status (obj));
13210 #endif
13211 break;
13212
13213 case 't': /* indicate TEXT or BINARY */
13214 #ifdef MODE_LINE_BINARY_TEXT
13215 return MODE_LINE_BINARY_TEXT (b);
13216 #else
13217 return "T";
13218 #endif
13219
13220 case 'z':
13221 /* coding-system (not including end-of-line format) */
13222 case 'Z':
13223 /* coding-system (including end-of-line type) */
13224 {
13225 int eol_flag = (c == 'Z');
13226 char *p = decode_mode_spec_buf;
13227
13228 if (! FRAME_WINDOW_P (f))
13229 {
13230 /* No need to mention EOL here--the terminal never needs
13231 to do EOL conversion. */
13232 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13233 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13234 }
13235 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13236 p, eol_flag);
13237
13238 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13239 #ifdef subprocesses
13240 obj = Fget_buffer_process (Fcurrent_buffer ());
13241 if (PROCESSP (obj))
13242 {
13243 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13244 p, eol_flag);
13245 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13246 p, eol_flag);
13247 }
13248 #endif /* subprocesses */
13249 #endif /* 0 */
13250 *p = 0;
13251 return decode_mode_spec_buf;
13252 }
13253 }
13254
13255 if (STRINGP (obj))
13256 return (char *) XSTRING (obj)->data;
13257 else
13258 return "";
13259 }
13260
13261
13262 /* Count up to COUNT lines starting from START / START_BYTE.
13263 But don't go beyond LIMIT_BYTE.
13264 Return the number of lines thus found (always nonnegative).
13265
13266 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13267
13268 static int
13269 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13270 int start, start_byte, limit_byte, count;
13271 int *byte_pos_ptr;
13272 {
13273 register unsigned char *cursor;
13274 unsigned char *base;
13275
13276 register int ceiling;
13277 register unsigned char *ceiling_addr;
13278 int orig_count = count;
13279
13280 /* If we are not in selective display mode,
13281 check only for newlines. */
13282 int selective_display = (!NILP (current_buffer->selective_display)
13283 && !INTEGERP (current_buffer->selective_display));
13284
13285 if (count > 0)
13286 {
13287 while (start_byte < limit_byte)
13288 {
13289 ceiling = BUFFER_CEILING_OF (start_byte);
13290 ceiling = min (limit_byte - 1, ceiling);
13291 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13292 base = (cursor = BYTE_POS_ADDR (start_byte));
13293 while (1)
13294 {
13295 if (selective_display)
13296 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13297 ;
13298 else
13299 while (*cursor != '\n' && ++cursor != ceiling_addr)
13300 ;
13301
13302 if (cursor != ceiling_addr)
13303 {
13304 if (--count == 0)
13305 {
13306 start_byte += cursor - base + 1;
13307 *byte_pos_ptr = start_byte;
13308 return orig_count;
13309 }
13310 else
13311 if (++cursor == ceiling_addr)
13312 break;
13313 }
13314 else
13315 break;
13316 }
13317 start_byte += cursor - base;
13318 }
13319 }
13320 else
13321 {
13322 while (start_byte > limit_byte)
13323 {
13324 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13325 ceiling = max (limit_byte, ceiling);
13326 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13327 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13328 while (1)
13329 {
13330 if (selective_display)
13331 while (--cursor != ceiling_addr
13332 && *cursor != '\n' && *cursor != 015)
13333 ;
13334 else
13335 while (--cursor != ceiling_addr && *cursor != '\n')
13336 ;
13337
13338 if (cursor != ceiling_addr)
13339 {
13340 if (++count == 0)
13341 {
13342 start_byte += cursor - base + 1;
13343 *byte_pos_ptr = start_byte;
13344 /* When scanning backwards, we should
13345 not count the newline posterior to which we stop. */
13346 return - orig_count - 1;
13347 }
13348 }
13349 else
13350 break;
13351 }
13352 /* Here we add 1 to compensate for the last decrement
13353 of CURSOR, which took it past the valid range. */
13354 start_byte += cursor - base + 1;
13355 }
13356 }
13357
13358 *byte_pos_ptr = limit_byte;
13359
13360 if (count < 0)
13361 return - orig_count + count;
13362 return orig_count - count;
13363
13364 }
13365
13366
13367 \f
13368 /***********************************************************************
13369 Displaying strings
13370 ***********************************************************************/
13371
13372 /* Display a NUL-terminated string, starting with index START.
13373
13374 If STRING is non-null, display that C string. Otherwise, the Lisp
13375 string LISP_STRING is displayed.
13376
13377 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13378 FACE_STRING. Display STRING or LISP_STRING with the face at
13379 FACE_STRING_POS in FACE_STRING:
13380
13381 Display the string in the environment given by IT, but use the
13382 standard display table, temporarily.
13383
13384 FIELD_WIDTH is the minimum number of output glyphs to produce.
13385 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13386 with spaces. If STRING has more characters, more than FIELD_WIDTH
13387 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13388
13389 PRECISION is the maximum number of characters to output from
13390 STRING. PRECISION < 0 means don't truncate the string.
13391
13392 This is roughly equivalent to printf format specifiers:
13393
13394 FIELD_WIDTH PRECISION PRINTF
13395 ----------------------------------------
13396 -1 -1 %s
13397 -1 10 %.10s
13398 10 -1 %10s
13399 20 10 %20.10s
13400
13401 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13402 display them, and < 0 means obey the current buffer's value of
13403 enable_multibyte_characters.
13404
13405 Value is the number of glyphs produced. */
13406
13407 static int
13408 display_string (string, lisp_string, face_string, face_string_pos,
13409 start, it, field_width, precision, max_x, multibyte)
13410 unsigned char *string;
13411 Lisp_Object lisp_string;
13412 Lisp_Object face_string;
13413 int face_string_pos;
13414 int start;
13415 struct it *it;
13416 int field_width, precision, max_x;
13417 int multibyte;
13418 {
13419 int hpos_at_start = it->hpos;
13420 int saved_face_id = it->face_id;
13421 struct glyph_row *row = it->glyph_row;
13422
13423 /* Initialize the iterator IT for iteration over STRING beginning
13424 with index START. We assume that IT may be modified here (which
13425 means that display_line has to do something when displaying a
13426 mini-buffer prompt, which it does). */
13427 reseat_to_string (it, string, lisp_string, start,
13428 precision, field_width, multibyte);
13429
13430 /* If displaying STRING, set up the face of the iterator
13431 from LISP_STRING, if that's given. */
13432 if (STRINGP (face_string))
13433 {
13434 int endptr;
13435 struct face *face;
13436
13437 it->face_id
13438 = face_at_string_position (it->w, face_string, face_string_pos,
13439 0, it->region_beg_charpos,
13440 it->region_end_charpos,
13441 &endptr, it->base_face_id);
13442 face = FACE_FROM_ID (it->f, it->face_id);
13443 it->face_box_p = face->box != FACE_NO_BOX;
13444 }
13445
13446 /* Set max_x to the maximum allowed X position. Don't let it go
13447 beyond the right edge of the window. */
13448 if (max_x <= 0)
13449 max_x = it->last_visible_x;
13450 else
13451 max_x = min (max_x, it->last_visible_x);
13452
13453 /* Skip over display elements that are not visible. because IT->w is
13454 hscrolled. */
13455 if (it->current_x < it->first_visible_x)
13456 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13457 MOVE_TO_POS | MOVE_TO_X);
13458
13459 row->ascent = it->max_ascent;
13460 row->height = it->max_ascent + it->max_descent;
13461 row->phys_ascent = it->max_phys_ascent;
13462 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13463
13464 /* This condition is for the case that we are called with current_x
13465 past last_visible_x. */
13466 while (it->current_x < max_x)
13467 {
13468 int x_before, x, n_glyphs_before, i, nglyphs;
13469
13470 /* Get the next display element. */
13471 if (!get_next_display_element (it))
13472 break;
13473
13474 /* Produce glyphs. */
13475 x_before = it->current_x;
13476 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13477 PRODUCE_GLYPHS (it);
13478
13479 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13480 i = 0;
13481 x = x_before;
13482 while (i < nglyphs)
13483 {
13484 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13485
13486 if (!it->truncate_lines_p
13487 && x + glyph->pixel_width > max_x)
13488 {
13489 /* End of continued line or max_x reached. */
13490 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13491 it->current_x = x;
13492 break;
13493 }
13494 else if (x + glyph->pixel_width > it->first_visible_x)
13495 {
13496 /* Glyph is at least partially visible. */
13497 ++it->hpos;
13498 if (x < it->first_visible_x)
13499 it->glyph_row->x = x - it->first_visible_x;
13500 }
13501 else
13502 {
13503 /* Glyph is off the left margin of the display area.
13504 Should not happen. */
13505 abort ();
13506 }
13507
13508 row->ascent = max (row->ascent, it->max_ascent);
13509 row->height = max (row->height, it->max_ascent + it->max_descent);
13510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13511 row->phys_height = max (row->phys_height,
13512 it->max_phys_ascent + it->max_phys_descent);
13513 x += glyph->pixel_width;
13514 ++i;
13515 }
13516
13517 /* Stop if max_x reached. */
13518 if (i < nglyphs)
13519 break;
13520
13521 /* Stop at line ends. */
13522 if (ITERATOR_AT_END_OF_LINE_P (it))
13523 {
13524 it->continuation_lines_width = 0;
13525 break;
13526 }
13527
13528 set_iterator_to_next (it, 1);
13529
13530 /* Stop if truncating at the right edge. */
13531 if (it->truncate_lines_p
13532 && it->current_x >= it->last_visible_x)
13533 {
13534 /* Add truncation mark, but don't do it if the line is
13535 truncated at a padding space. */
13536 if (IT_CHARPOS (*it) < it->string_nchars)
13537 {
13538 if (!FRAME_WINDOW_P (it->f))
13539 produce_special_glyphs (it, IT_TRUNCATION);
13540 it->glyph_row->truncated_on_right_p = 1;
13541 }
13542 break;
13543 }
13544 }
13545
13546 /* Maybe insert a truncation at the left. */
13547 if (it->first_visible_x
13548 && IT_CHARPOS (*it) > 0)
13549 {
13550 if (!FRAME_WINDOW_P (it->f))
13551 insert_left_trunc_glyphs (it);
13552 it->glyph_row->truncated_on_left_p = 1;
13553 }
13554
13555 it->face_id = saved_face_id;
13556
13557 /* Value is number of columns displayed. */
13558 return it->hpos - hpos_at_start;
13559 }
13560
13561
13562 \f
13563 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13564 appears as an element of LIST or as the car of an element of LIST.
13565 If PROPVAL is a list, compare each element against LIST in that
13566 way, and return 1 if any element of PROPVAL is found in LIST.
13567 Otherwise return 0. This function cannot quit. */
13568
13569 int
13570 invisible_p (propval, list)
13571 register Lisp_Object propval;
13572 Lisp_Object list;
13573 {
13574 register Lisp_Object tail, proptail;
13575
13576 for (tail = list; CONSP (tail); tail = XCDR (tail))
13577 {
13578 register Lisp_Object tem;
13579 tem = XCAR (tail);
13580 if (EQ (propval, tem))
13581 return 1;
13582 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13583 return 1;
13584 }
13585
13586 if (CONSP (propval))
13587 {
13588 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13589 {
13590 Lisp_Object propelt;
13591 propelt = XCAR (proptail);
13592 for (tail = list; CONSP (tail); tail = XCDR (tail))
13593 {
13594 register Lisp_Object tem;
13595 tem = XCAR (tail);
13596 if (EQ (propelt, tem))
13597 return 1;
13598 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13599 return 1;
13600 }
13601 }
13602 }
13603
13604 return 0;
13605 }
13606
13607
13608 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13609 the cdr of that element is non-nil. If PROPVAL is a list, check
13610 each element of PROPVAL in that way, and the first time some
13611 element is found, return 1 if the cdr of that element is non-nil.
13612 Otherwise return 0. This function cannot quit. */
13613
13614 int
13615 invisible_ellipsis_p (propval, list)
13616 register Lisp_Object propval;
13617 Lisp_Object list;
13618 {
13619 register Lisp_Object tail, proptail;
13620
13621 for (tail = list; CONSP (tail); tail = XCDR (tail))
13622 {
13623 register Lisp_Object tem;
13624 tem = XCAR (tail);
13625 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13626 return ! NILP (XCDR (tem));
13627 }
13628
13629 if (CONSP (propval))
13630 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13631 {
13632 Lisp_Object propelt;
13633 propelt = XCAR (proptail);
13634 for (tail = list; CONSP (tail); tail = XCDR (tail))
13635 {
13636 register Lisp_Object tem;
13637 tem = XCAR (tail);
13638 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13639 return ! NILP (XCDR (tem));
13640 }
13641 }
13642
13643 return 0;
13644 }
13645
13646
13647 \f
13648 /***********************************************************************
13649 Initialization
13650 ***********************************************************************/
13651
13652 void
13653 syms_of_xdisp ()
13654 {
13655 Vwith_echo_area_save_vector = Qnil;
13656 staticpro (&Vwith_echo_area_save_vector);
13657
13658 Vmessage_stack = Qnil;
13659 staticpro (&Vmessage_stack);
13660
13661 Qinhibit_redisplay = intern ("inhibit-redisplay");
13662 staticpro (&Qinhibit_redisplay);
13663
13664 #if GLYPH_DEBUG
13665 defsubr (&Sdump_glyph_matrix);
13666 defsubr (&Sdump_glyph_row);
13667 defsubr (&Sdump_tool_bar_row);
13668 defsubr (&Strace_redisplay_toggle);
13669 defsubr (&Strace_to_stderr);
13670 #endif
13671
13672 staticpro (&Qmenu_bar_update_hook);
13673 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13674
13675 staticpro (&Qoverriding_terminal_local_map);
13676 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13677
13678 staticpro (&Qoverriding_local_map);
13679 Qoverriding_local_map = intern ("overriding-local-map");
13680
13681 staticpro (&Qwindow_scroll_functions);
13682 Qwindow_scroll_functions = intern ("window-scroll-functions");
13683
13684 staticpro (&Qredisplay_end_trigger_functions);
13685 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13686
13687 staticpro (&Qinhibit_point_motion_hooks);
13688 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13689
13690 QCdata = intern (":data");
13691 staticpro (&QCdata);
13692 Qdisplay = intern ("display");
13693 staticpro (&Qdisplay);
13694 Qspace_width = intern ("space-width");
13695 staticpro (&Qspace_width);
13696 Qraise = intern ("raise");
13697 staticpro (&Qraise);
13698 Qspace = intern ("space");
13699 staticpro (&Qspace);
13700 Qmargin = intern ("margin");
13701 staticpro (&Qmargin);
13702 Qleft_margin = intern ("left-margin");
13703 staticpro (&Qleft_margin);
13704 Qright_margin = intern ("right-margin");
13705 staticpro (&Qright_margin);
13706 Qalign_to = intern ("align-to");
13707 staticpro (&Qalign_to);
13708 QCalign_to = intern (":align-to");
13709 staticpro (&QCalign_to);
13710 Qrelative_width = intern ("relative-width");
13711 staticpro (&Qrelative_width);
13712 QCrelative_width = intern (":relative-width");
13713 staticpro (&QCrelative_width);
13714 QCrelative_height = intern (":relative-height");
13715 staticpro (&QCrelative_height);
13716 QCeval = intern (":eval");
13717 staticpro (&QCeval);
13718 Qwhen = intern ("when");
13719 staticpro (&Qwhen);
13720 QCfile = intern (":file");
13721 staticpro (&QCfile);
13722 Qfontified = intern ("fontified");
13723 staticpro (&Qfontified);
13724 Qfontification_functions = intern ("fontification-functions");
13725 staticpro (&Qfontification_functions);
13726 Qtrailing_whitespace = intern ("trailing-whitespace");
13727 staticpro (&Qtrailing_whitespace);
13728 Qimage = intern ("image");
13729 staticpro (&Qimage);
13730 Qmessage_truncate_lines = intern ("message-truncate-lines");
13731 staticpro (&Qmessage_truncate_lines);
13732 Qgrow_only = intern ("grow-only");
13733 staticpro (&Qgrow_only);
13734
13735 last_arrow_position = Qnil;
13736 last_arrow_string = Qnil;
13737 staticpro (&last_arrow_position);
13738 staticpro (&last_arrow_string);
13739
13740 echo_buffer[0] = echo_buffer[1] = Qnil;
13741 staticpro (&echo_buffer[0]);
13742 staticpro (&echo_buffer[1]);
13743
13744 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13745 staticpro (&echo_area_buffer[0]);
13746 staticpro (&echo_area_buffer[1]);
13747
13748 Vmessages_buffer_name = build_string ("*Messages*");
13749 staticpro (&Vmessages_buffer_name);
13750
13751 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13752 "Non-nil means highlight trailing whitespace.\n\
13753 The face used for trailing whitespace is `trailing-whitespace'.");
13754 Vshow_trailing_whitespace = Qnil;
13755
13756 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13757 "Non-nil means don't actually do any redisplay.\n\
13758 This is used for internal purposes.");
13759 Vinhibit_redisplay = Qnil;
13760
13761 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13762 "String (or mode line construct) included (normally) in `mode-line-format'.");
13763 Vglobal_mode_string = Qnil;
13764
13765 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13766 "Marker for where to display an arrow on top of the buffer text.\n\
13767 This must be the beginning of a line in order to work.\n\
13768 See also `overlay-arrow-string'.");
13769 Voverlay_arrow_position = Qnil;
13770
13771 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13772 "String to display as an arrow. See also `overlay-arrow-position'.");
13773 Voverlay_arrow_string = Qnil;
13774
13775 DEFVAR_INT ("scroll-step", &scroll_step,
13776 "*The number of lines to try scrolling a window by when point moves out.\n\
13777 If that fails to bring point back on frame, point is centered instead.\n\
13778 If this is zero, point is always centered after it moves off frame.\n\
13779 If you want scrolling to always be a line at a time, you should set\n\
13780 `scroll-conservatively' to a large value rather than set this to 1.");
13781
13782 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13783 "*Scroll up to this many lines, to bring point back on screen.\n\
13784 A value of zero means to scroll the text to center point vertically\n\
13785 in the window.");
13786 scroll_conservatively = 0;
13787
13788 DEFVAR_INT ("scroll-margin", &scroll_margin,
13789 "*Number of lines of margin at the top and bottom of a window.\n\
13790 Recenter the window whenever point gets within this many lines\n\
13791 of the top or bottom of the window.");
13792 scroll_margin = 0;
13793
13794 #if GLYPH_DEBUG
13795 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13796 #endif
13797
13798 DEFVAR_BOOL ("truncate-partial-width-windows",
13799 &truncate_partial_width_windows,
13800 "*Non-nil means truncate lines in all windows less than full frame wide.");
13801 truncate_partial_width_windows = 1;
13802
13803 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13804 "*Non-nil means use inverse video for the mode line.");
13805 mode_line_inverse_video = 1;
13806
13807 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13808 "*Maximum buffer size for which line number should be displayed.\n\
13809 If the buffer is bigger than this, the line number does not appear\n\
13810 in the mode line. A value of nil means no limit.");
13811 Vline_number_display_limit = Qnil;
13812
13813 DEFVAR_INT ("line-number-display-limit-width",
13814 &line_number_display_limit_width,
13815 "*Maximum line width (in characters) for line number display.\n\
13816 If the average length of the lines near point is bigger than this, then the\n\
13817 line number may be omitted from the mode line.");
13818 line_number_display_limit_width = 200;
13819
13820 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13821 "*Non-nil means highlight region even in nonselected windows.");
13822 highlight_nonselected_windows = 0;
13823
13824 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13825 "Non-nil if more than one frame is visible on this display.\n\
13826 Minibuffer-only frames don't count, but iconified frames do.\n\
13827 This variable is not guaranteed to be accurate except while processing\n\
13828 `frame-title-format' and `icon-title-format'.");
13829
13830 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13831 "Template for displaying the title bar of visible frames.\n\
13832 \(Assuming the window manager supports this feature.)\n\
13833 This variable has the same structure as `mode-line-format' (which see),\n\
13834 and is used only on frames for which no explicit name has been set\n\
13835 \(see `modify-frame-parameters').");
13836 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13837 "Template for displaying the title bar of an iconified frame.\n\
13838 \(Assuming the window manager supports this feature.)\n\
13839 This variable has the same structure as `mode-line-format' (which see),\n\
13840 and is used only on frames for which no explicit name has been set\n\
13841 \(see `modify-frame-parameters').");
13842 Vicon_title_format
13843 = Vframe_title_format
13844 = Fcons (intern ("multiple-frames"),
13845 Fcons (build_string ("%b"),
13846 Fcons (Fcons (build_string (""),
13847 Fcons (intern ("invocation-name"),
13848 Fcons (build_string ("@"),
13849 Fcons (intern ("system-name"),
13850 Qnil)))),
13851 Qnil)));
13852
13853 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13854 "Maximum number of lines to keep in the message log buffer.\n\
13855 If nil, disable message logging. If t, log messages but don't truncate\n\
13856 the buffer when it becomes large.");
13857 XSETFASTINT (Vmessage_log_max, 50);
13858
13859 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13860 "Functions called before redisplay, if window sizes have changed.\n\
13861 The value should be a list of functions that take one argument.\n\
13862 Just before redisplay, for each frame, if any of its windows have changed\n\
13863 size since the last redisplay, or have been split or deleted,\n\
13864 all the functions in the list are called, with the frame as argument.");
13865 Vwindow_size_change_functions = Qnil;
13866
13867 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13868 "List of Functions to call before redisplaying a window with scrolling.\n\
13869 Each function is called with two arguments, the window\n\
13870 and its new display-start position. Note that the value of `window-end'\n\
13871 is not valid when these functions are called.");
13872 Vwindow_scroll_functions = Qnil;
13873
13874 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13875 "*Non-nil means automatically resize tool-bars.\n\
13876 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13877 It decreases a tool-bar's height when it would display blank lines\n\
13878 otherwise.");
13879 auto_resize_tool_bars_p = 1;
13880
13881 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13882 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13883 auto_raise_tool_bar_buttons_p = 1;
13884
13885 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13886 "*Margin around tool-bar buttons in pixels.");
13887 tool_bar_button_margin = 1;
13888
13889 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13890 "Relief thickness of tool-bar buttons.");
13891 tool_bar_button_relief = 3;
13892
13893 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13894 "List of functions to call to fontify regions of text.\n\
13895 Each function is called with one argument POS. Functions must\n\
13896 fontify a region starting at POS in the current buffer, and give\n\
13897 fontified regions the property `fontified'.\n\
13898 This variable automatically becomes buffer-local when set.");
13899 Vfontification_functions = Qnil;
13900 Fmake_local_variable (Qfontification_functions);
13901
13902 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13903 &unibyte_display_via_language_environment,
13904 "*Non-nil means display unibyte text according to language environment.\n\
13905 Specifically this means that unibyte non-ASCII characters\n\
13906 are displayed by converting them to the equivalent multibyte characters\n\
13907 according to the current language environment. As a result, they are\n\
13908 displayed according to the current fontset.");
13909 unibyte_display_via_language_environment = 0;
13910
13911 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13912 "*Maximum height for resizing mini-windows.\n\
13913 If a float, it specifies a fraction of the mini-window frame's height.\n\
13914 If an integer, it specifies a number of lines.");
13915 Vmax_mini_window_height = make_float (0.25);
13916
13917 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
13918 "*How to resize the mini-window.\n\
13919 A value of nil means don't automatically resize mini-windows.\n\
13920 A value of t means resize it to fit the text displayed in it.\n\
13921 A value of `grow-only', the default, means let mini-windows grow\n\
13922 only, until the its display becomes empty, at which point the mini-window\n\
13923 goes back to its normal size.");
13924 Vresize_mini_windows = Qgrow_only;
13925
13926 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13927 &cursor_in_non_selected_windows,
13928 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13929 Nil means don't display a cursor there.");
13930 cursor_in_non_selected_windows = 1;
13931
13932 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13933 "*Non-nil means scroll the display automatically to make point visible.");
13934 automatic_hscrolling_p = 1;
13935
13936 DEFVAR_LISP ("image-types", &Vimage_types,
13937 "List of supported image types.\n\
13938 Each element of the list is a symbol for a supported image type.");
13939 Vimage_types = Qnil;
13940
13941 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13942 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13943 Bind this around calls to `message' to let it take effect.");
13944 message_truncate_lines = 0;
13945
13946 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13947 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13948 Can be used to update submenus whose contents should vary.");
13949 Vmenu_bar_update_hook = Qnil;
13950 }
13951
13952
13953 /* Initialize this module when Emacs starts. */
13954
13955 void
13956 init_xdisp ()
13957 {
13958 Lisp_Object root_window;
13959 struct window *mini_w;
13960
13961 current_header_line_height = current_mode_line_height = -1;
13962
13963 CHARPOS (this_line_start_pos) = 0;
13964
13965 mini_w = XWINDOW (minibuf_window);
13966 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13967
13968 if (!noninteractive)
13969 {
13970 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13971 int i;
13972
13973 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13974 set_window_height (root_window,
13975 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13976 0);
13977 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13978 set_window_height (minibuf_window, 1, 0);
13979
13980 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13981 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13982
13983 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13984 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13985 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13986
13987 /* The default ellipsis glyphs `...'. */
13988 for (i = 0; i < 3; ++i)
13989 XSETFASTINT (default_invis_vector[i], '.');
13990 }
13991
13992 #ifdef HAVE_WINDOW_SYSTEM
13993 {
13994 /* Allocate the buffer for frame titles. */
13995 int size = 100;
13996 frame_title_buf = (char *) xmalloc (size);
13997 frame_title_buf_end = frame_title_buf + size;
13998 frame_title_ptr = NULL;
13999 }
14000 #endif /* HAVE_WINDOW_SYSTEM */
14001
14002 help_echo_showing_p = 0;
14003 }
14004
14005