Initial check-in: changes for building Emacs under Mac OS.
[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
229 /* Functions called to fontify regions of text. */
230
231 Lisp_Object Vfontification_functions;
232 Lisp_Object Qfontification_functions;
233
234 /* Non-zero means draw tool bar buttons raised when the mouse moves
235 over them. */
236
237 int auto_raise_tool_bar_buttons_p;
238
239 /* Margin around tool bar buttons in pixels. */
240
241 int tool_bar_button_margin;
242
243 /* Thickness of shadow to draw around tool bar buttons. */
244
245 int tool_bar_button_relief;
246
247 /* Non-zero means automatically resize tool-bars so that all tool-bar
248 items are visible, and no blank lines remain. */
249
250 int auto_resize_tool_bars_p;
251
252 /* Non-nil means don't actually do any redisplay. */
253
254 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
255
256 /* Names of text properties relevant for redisplay. */
257
258 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
259 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
260
261 /* Symbols used in text property values. */
262
263 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
264 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
265 Lisp_Object Qmargin;
266 extern Lisp_Object Qheight;
267
268 /* Non-nil means highlight trailing whitespace. */
269
270 Lisp_Object Vshow_trailing_whitespace;
271
272 /* Name of the face used to highlight trailing whitespace. */
273
274 Lisp_Object Qtrailing_whitespace;
275
276 /* The symbol `image' which is the car of the lists used to represent
277 images in Lisp. */
278
279 Lisp_Object Qimage;
280
281 /* Non-zero means print newline to stdout before next mini-buffer
282 message. */
283
284 int noninteractive_need_newline;
285
286 /* Non-zero means print newline to message log before next message. */
287
288 static int message_log_need_newline;
289
290 \f
291 /* The buffer position of the first character appearing entirely or
292 partially on the line of the selected window which contains the
293 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
294 redisplay optimization in redisplay_internal. */
295
296 static struct text_pos this_line_start_pos;
297
298 /* Number of characters past the end of the line above, including the
299 terminating newline. */
300
301 static struct text_pos this_line_end_pos;
302
303 /* The vertical positions and the height of this line. */
304
305 static int this_line_vpos;
306 static int this_line_y;
307 static int this_line_pixel_height;
308
309 /* X position at which this display line starts. Usually zero;
310 negative if first character is partially visible. */
311
312 static int this_line_start_x;
313
314 /* Buffer that this_line_.* variables are referring to. */
315
316 static struct buffer *this_line_buffer;
317
318 /* Nonzero means truncate lines in all windows less wide than the
319 frame. */
320
321 int truncate_partial_width_windows;
322
323 /* A flag to control how to display unibyte 8-bit character. */
324
325 int unibyte_display_via_language_environment;
326
327 /* Nonzero means we have more than one non-mini-buffer-only frame.
328 Not guaranteed to be accurate except while parsing
329 frame-title-format. */
330
331 int multiple_frames;
332
333 Lisp_Object Vglobal_mode_string;
334
335 /* Marker for where to display an arrow on top of the buffer text. */
336
337 Lisp_Object Voverlay_arrow_position;
338
339 /* String to display for the arrow. Only used on terminal frames. */
340
341 Lisp_Object Voverlay_arrow_string;
342
343 /* Values of those variables at last redisplay. However, if
344 Voverlay_arrow_position is a marker, last_arrow_position is its
345 numerical position. */
346
347 static Lisp_Object last_arrow_position, last_arrow_string;
348
349 /* Like mode-line-format, but for the title bar on a visible frame. */
350
351 Lisp_Object Vframe_title_format;
352
353 /* Like mode-line-format, but for the title bar on an iconified frame. */
354
355 Lisp_Object Vicon_title_format;
356
357 /* List of functions to call when a window's size changes. These
358 functions get one arg, a frame on which one or more windows' sizes
359 have changed. */
360
361 static Lisp_Object Vwindow_size_change_functions;
362
363 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
364
365 /* Nonzero if overlay arrow has been displayed once in this window. */
366
367 static int overlay_arrow_seen;
368
369 /* Nonzero means highlight the region even in nonselected windows. */
370
371 int highlight_nonselected_windows;
372
373 /* If cursor motion alone moves point off frame, try scrolling this
374 many lines up or down if that will bring it back. */
375
376 static int scroll_step;
377
378 /* Non-0 means scroll just far enough to bring point back on the
379 screen, when appropriate. */
380
381 static int scroll_conservatively;
382
383 /* Recenter the window whenever point gets within this many lines of
384 the top or bottom of the window. This value is translated into a
385 pixel value by multiplying it with CANON_Y_UNIT, which means that
386 there is really a fixed pixel height scroll margin. */
387
388 int scroll_margin;
389
390 /* Number of windows showing the buffer of the selected window (or
391 another buffer with the same base buffer). keyboard.c refers to
392 this. */
393
394 int buffer_shared;
395
396 /* Vector containing glyphs for an ellipsis `...'. */
397
398 static Lisp_Object default_invis_vector[3];
399
400 /* Nonzero means display mode line highlighted. */
401
402 int mode_line_inverse_video;
403
404 /* Prompt to display in front of the mini-buffer contents. */
405
406 Lisp_Object minibuf_prompt;
407
408 /* Width of current mini-buffer prompt. Only set after display_line
409 of the line that contains the prompt. */
410
411 int minibuf_prompt_width;
412 int minibuf_prompt_pixel_width;
413
414 /* This is the window where the echo area message was displayed. It
415 is always a mini-buffer window, but it may not be the same window
416 currently active as a mini-buffer. */
417
418 Lisp_Object echo_area_window;
419
420 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
421 pushes the current message and the value of
422 message_enable_multibyte on the stack, the function restore_message
423 pops the stack and displays MESSAGE again. */
424
425 Lisp_Object Vmessage_stack;
426
427 /* Nonzero means multibyte characters were enabled when the echo area
428 message was specified. */
429
430 int message_enable_multibyte;
431
432 /* True if we should redraw the mode lines on the next redisplay. */
433
434 int update_mode_lines;
435
436 /* Nonzero if window sizes or contents have changed since last
437 redisplay that finished */
438
439 int windows_or_buffers_changed;
440
441 /* Nonzero after display_mode_line if %l was used and it displayed a
442 line number. */
443
444 int line_number_displayed;
445
446 /* Maximum buffer size for which to display line numbers. */
447
448 Lisp_Object Vline_number_display_limit;
449
450 /* line width to consider when repostioning for line number display */
451
452 static int line_number_display_limit_width;
453
454 /* Number of lines to keep in the message log buffer. t means
455 infinite. nil means don't log at all. */
456
457 Lisp_Object Vmessage_log_max;
458
459 /* The name of the *Messages* buffer, a string. */
460
461 static Lisp_Object Vmessages_buffer_name;
462
463 /* Current, index 0, and last displayed echo area message. Either
464 buffers from echo_buffers, or nil to indicate no message. */
465
466 Lisp_Object echo_area_buffer[2];
467
468 /* The buffers referenced from echo_area_buffer. */
469
470 static Lisp_Object echo_buffer[2];
471
472 /* A vector saved used in with_area_buffer to reduce consing. */
473
474 static Lisp_Object Vwith_echo_area_save_vector;
475
476 /* Non-zero means display_echo_area should display the last echo area
477 message again. Set by redisplay_preserve_echo_area. */
478
479 static int display_last_displayed_message_p;
480
481 /* Nonzero if echo area is being used by print; zero if being used by
482 message. */
483
484 int message_buf_print;
485
486 /* Maximum height for resizing mini-windows. Either a float
487 specifying a fraction of the available height, or an integer
488 specifying a number of lines. */
489
490 Lisp_Object Vmax_mini_window_height;
491
492 /* Non-zero means messages should be displayed with truncated
493 lines instead of being continued. */
494
495 int message_truncate_lines;
496 Lisp_Object Qmessage_truncate_lines;
497
498 /* Non-zero means we want a hollow cursor in windows that are not
499 selected. Zero means there's no cursor in such windows. */
500
501 int cursor_in_non_selected_windows;
502
503 /* A scratch glyph row with contents used for generating truncation
504 glyphs. Also used in direct_output_for_insert. */
505
506 #define MAX_SCRATCH_GLYPHS 100
507 struct glyph_row scratch_glyph_row;
508 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
509
510 /* Ascent and height of the last line processed by move_it_to. */
511
512 static int last_max_ascent, last_height;
513
514 /* Non-zero if there's a help-echo in the echo area. */
515
516 int help_echo_showing_p;
517
518 /* The maximum distance to look ahead for text properties. Values
519 that are too small let us call compute_char_face and similar
520 functions too often which is expensive. Values that are too large
521 let us call compute_char_face and alike too often because we
522 might not be interested in text properties that far away. */
523
524 #define TEXT_PROP_DISTANCE_LIMIT 100
525
526 #if GLYPH_DEBUG
527
528 /* Non-zero means print traces of redisplay if compiled with
529 GLYPH_DEBUG != 0. */
530
531 int trace_redisplay_p;
532
533 #endif /* GLYPH_DEBUG */
534
535 #ifdef DEBUG_TRACE_MOVE
536 /* Non-zero means trace with TRACE_MOVE to stderr. */
537 int trace_move;
538
539 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
540 #else
541 #define TRACE_MOVE(x) (void) 0
542 #endif
543
544 /* Non-zero means automatically scroll windows horizontally to make
545 point visible. */
546
547 int automatic_hscrolling_p;
548
549 /* A list of symbols, one for each supported image type. */
550
551 Lisp_Object Vimage_types;
552
553 /* Value returned from text property handlers (see below). */
554
555 enum prop_handled
556 {
557 HANDLED_NORMALLY,
558 HANDLED_RECOMPUTE_PROPS,
559 HANDLED_OVERLAY_STRING_CONSUMED,
560 HANDLED_RETURN
561 };
562
563 /* A description of text properties that redisplay is interested
564 in. */
565
566 struct props
567 {
568 /* The name of the property. */
569 Lisp_Object *name;
570
571 /* A unique index for the property. */
572 enum prop_idx idx;
573
574 /* A handler function called to set up iterator IT from the property
575 at IT's current position. Value is used to steer handle_stop. */
576 enum prop_handled (*handler) P_ ((struct it *it));
577 };
578
579 static enum prop_handled handle_face_prop P_ ((struct it *));
580 static enum prop_handled handle_invisible_prop P_ ((struct it *));
581 static enum prop_handled handle_display_prop P_ ((struct it *));
582 static enum prop_handled handle_composition_prop P_ ((struct it *));
583 static enum prop_handled handle_overlay_change P_ ((struct it *));
584 static enum prop_handled handle_fontified_prop P_ ((struct it *));
585
586 /* Properties handled by iterators. */
587
588 static struct props it_props[] =
589 {
590 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
591 /* Handle `face' before `display' because some sub-properties of
592 `display' need to know the face. */
593 {&Qface, FACE_PROP_IDX, handle_face_prop},
594 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
595 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
596 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
597 {NULL, 0, NULL}
598 };
599
600 /* Value is the position described by X. If X is a marker, value is
601 the marker_position of X. Otherwise, value is X. */
602
603 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
604
605 /* Enumeration returned by some move_it_.* functions internally. */
606
607 enum move_it_result
608 {
609 /* Not used. Undefined value. */
610 MOVE_UNDEFINED,
611
612 /* Move ended at the requested buffer position or ZV. */
613 MOVE_POS_MATCH_OR_ZV,
614
615 /* Move ended at the requested X pixel position. */
616 MOVE_X_REACHED,
617
618 /* Move within a line ended at the end of a line that must be
619 continued. */
620 MOVE_LINE_CONTINUED,
621
622 /* Move within a line ended at the end of a line that would
623 be displayed truncated. */
624 MOVE_LINE_TRUNCATED,
625
626 /* Move within a line ended at a line end. */
627 MOVE_NEWLINE_OR_CR
628 };
629
630
631 \f
632 /* Function prototypes. */
633
634 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
635 static int redisplay_mode_lines P_ ((Lisp_Object, int));
636 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
637 static int invisible_text_between_p P_ ((struct it *, int, int));
638 static int next_element_from_ellipsis P_ ((struct it *));
639 static void pint2str P_ ((char *, int, int));
640 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
641 struct text_pos));
642 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
643 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
644 static void store_frame_title_char P_ ((char));
645 static int store_frame_title P_ ((unsigned char *, int, int));
646 static void x_consider_frame_title P_ ((Lisp_Object));
647 static void handle_stop P_ ((struct it *));
648 static int tool_bar_lines_needed P_ ((struct frame *));
649 static int single_display_prop_intangible_p P_ ((Lisp_Object));
650 static void ensure_echo_area_buffers P_ ((void));
651 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
652 struct glyph_row *,
653 struct glyph_row *));
654 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
655 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
656 static int with_echo_area_buffer P_ ((struct window *, int,
657 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
658 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
659 static void clear_garbaged_frames P_ ((void));
660 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
661 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
662 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
663 static int display_echo_area P_ ((struct window *));
664 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
665 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
666 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
667 static int string_char_and_length P_ ((unsigned char *, int, int *));
668 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
669 struct text_pos));
670 static int compute_window_start_on_continuation_line P_ ((struct window *));
671 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
672 static void insert_left_trunc_glyphs P_ ((struct it *));
673 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
674 static void extend_face_to_end_of_line P_ ((struct it *));
675 static int append_space P_ ((struct it *, int));
676 static void make_cursor_line_fully_visible P_ ((struct window *));
677 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
678 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
679 static int trailing_whitespace_p P_ ((int));
680 static int message_log_check_duplicate P_ ((int, int, int, int));
681 int invisible_p P_ ((Lisp_Object, Lisp_Object));
682 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
683 static void push_it P_ ((struct it *));
684 static void pop_it P_ ((struct it *));
685 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
686 static void redisplay_internal P_ ((int));
687 static int echo_area_display P_ ((int));
688 static void redisplay_windows P_ ((Lisp_Object));
689 static void redisplay_window P_ ((Lisp_Object, int));
690 static void update_menu_bar P_ ((struct frame *, int));
691 static int try_window_reusing_current_matrix P_ ((struct window *));
692 static int try_window_id P_ ((struct window *));
693 static int display_line P_ ((struct it *));
694 static int display_mode_lines P_ ((struct window *));
695 static void display_mode_line P_ ((struct window *, enum face_id,
696 Lisp_Object));
697 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
698 static char *decode_mode_spec P_ ((struct window *, int, int, int));
699 static void display_menu_bar P_ ((struct window *));
700 static int display_count_lines P_ ((int, int, int, int, int *));
701 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
702 int, int, struct it *, int, int, int, int));
703 static void compute_line_metrics P_ ((struct it *));
704 static void run_redisplay_end_trigger_hook P_ ((struct it *));
705 static int get_overlay_strings P_ ((struct it *));
706 static void next_overlay_string P_ ((struct it *));
707 static void reseat P_ ((struct it *, struct text_pos, int));
708 static void reseat_1 P_ ((struct it *, struct text_pos, int));
709 static void back_to_previous_visible_line_start P_ ((struct it *));
710 static void reseat_at_previous_visible_line_start P_ ((struct it *));
711 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
712 static int next_element_from_display_vector P_ ((struct it *));
713 static int next_element_from_string P_ ((struct it *));
714 static int next_element_from_c_string P_ ((struct it *));
715 static int next_element_from_buffer P_ ((struct it *));
716 static int next_element_from_composition P_ ((struct it *));
717 static int next_element_from_image P_ ((struct it *));
718 static int next_element_from_stretch P_ ((struct it *));
719 static void load_overlay_strings P_ ((struct it *));
720 static void init_from_display_pos P_ ((struct it *, struct window *,
721 struct display_pos *));
722 static void reseat_to_string P_ ((struct it *, unsigned char *,
723 Lisp_Object, int, int, int, int));
724 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
725 int, int, int));
726 void move_it_vertically_backward P_ ((struct it *, int));
727 static void init_to_row_start P_ ((struct it *, struct window *,
728 struct glyph_row *));
729 static void init_to_row_end P_ ((struct it *, struct window *,
730 struct glyph_row *));
731 static void back_to_previous_line_start P_ ((struct it *));
732 static int forward_to_next_line_start P_ ((struct it *, int *));
733 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
734 Lisp_Object, int));
735 static struct text_pos string_pos P_ ((int, Lisp_Object));
736 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
737 static int number_of_chars P_ ((unsigned char *, int));
738 static void compute_stop_pos P_ ((struct it *));
739 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
740 Lisp_Object));
741 static int face_before_or_after_it_pos P_ ((struct it *, int));
742 static int next_overlay_change P_ ((int));
743 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
744 Lisp_Object, struct text_pos *));
745
746 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
747 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
748
749 #ifdef HAVE_WINDOW_SYSTEM
750
751 static void update_tool_bar P_ ((struct frame *, int));
752 static void build_desired_tool_bar_string P_ ((struct frame *f));
753 static int redisplay_tool_bar P_ ((struct frame *));
754 static void display_tool_bar_line P_ ((struct it *));
755
756 #endif /* HAVE_WINDOW_SYSTEM */
757
758 \f
759 /***********************************************************************
760 Window display dimensions
761 ***********************************************************************/
762
763 /* Return the window-relative maximum y + 1 for glyph rows displaying
764 text in window W. This is the height of W minus the height of a
765 mode line, if any. */
766
767 INLINE int
768 window_text_bottom_y (w)
769 struct window *w;
770 {
771 struct frame *f = XFRAME (w->frame);
772 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
773
774 if (WINDOW_WANTS_MODELINE_P (w))
775 height -= CURRENT_MODE_LINE_HEIGHT (w);
776 return height;
777 }
778
779
780 /* Return the pixel width of display area AREA of window W. AREA < 0
781 means return the total width of W, not including bitmap areas to
782 the left and right of the window. */
783
784 INLINE int
785 window_box_width (w, area)
786 struct window *w;
787 int area;
788 {
789 struct frame *f = XFRAME (w->frame);
790 int width = XFASTINT (w->width);
791
792 if (!w->pseudo_window_p)
793 {
794 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
795
796 if (area == TEXT_AREA)
797 {
798 if (INTEGERP (w->left_margin_width))
799 width -= XFASTINT (w->left_margin_width);
800 if (INTEGERP (w->right_margin_width))
801 width -= XFASTINT (w->right_margin_width);
802 }
803 else if (area == LEFT_MARGIN_AREA)
804 width = (INTEGERP (w->left_margin_width)
805 ? XFASTINT (w->left_margin_width) : 0);
806 else if (area == RIGHT_MARGIN_AREA)
807 width = (INTEGERP (w->right_margin_width)
808 ? XFASTINT (w->right_margin_width) : 0);
809 }
810
811 return width * CANON_X_UNIT (f);
812 }
813
814
815 /* Return the pixel height of the display area of window W, not
816 including mode lines of W, if any.. */
817
818 INLINE int
819 window_box_height (w)
820 struct window *w;
821 {
822 struct frame *f = XFRAME (w->frame);
823 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
824
825 xassert (height >= 0);
826
827 if (WINDOW_WANTS_MODELINE_P (w))
828 height -= CURRENT_MODE_LINE_HEIGHT (w);
829
830 if (WINDOW_WANTS_HEADER_LINE_P (w))
831 height -= CURRENT_HEADER_LINE_HEIGHT (w);
832
833 return height;
834 }
835
836
837 /* Return the frame-relative coordinate of the left edge of display
838 area AREA of window W. AREA < 0 means return the left edge of the
839 whole window, to the right of any bitmap area at the left side of
840 W. */
841
842 INLINE int
843 window_box_left (w, area)
844 struct window *w;
845 int area;
846 {
847 struct frame *f = XFRAME (w->frame);
848 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
849
850 if (!w->pseudo_window_p)
851 {
852 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
853 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
854
855 if (area == TEXT_AREA)
856 x += window_box_width (w, LEFT_MARGIN_AREA);
857 else if (area == RIGHT_MARGIN_AREA)
858 x += (window_box_width (w, LEFT_MARGIN_AREA)
859 + window_box_width (w, TEXT_AREA));
860 }
861
862 return x;
863 }
864
865
866 /* Return the frame-relative coordinate of the right edge of display
867 area AREA of window W. AREA < 0 means return the left edge of the
868 whole window, to the left of any bitmap area at the right side of
869 W. */
870
871 INLINE int
872 window_box_right (w, area)
873 struct window *w;
874 int area;
875 {
876 return window_box_left (w, area) + window_box_width (w, area);
877 }
878
879
880 /* Get the bounding box of the display area AREA of window W, without
881 mode lines, in frame-relative coordinates. AREA < 0 means the
882 whole window, not including bitmap areas to the left and right of
883 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
884 coordinates of the upper-left corner of the box. Return in
885 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
886
887 INLINE void
888 window_box (w, area, box_x, box_y, box_width, box_height)
889 struct window *w;
890 int area;
891 int *box_x, *box_y, *box_width, *box_height;
892 {
893 struct frame *f = XFRAME (w->frame);
894
895 *box_width = window_box_width (w, area);
896 *box_height = window_box_height (w);
897 *box_x = window_box_left (w, area);
898 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
899 + XFASTINT (w->top) * CANON_Y_UNIT (f));
900 if (WINDOW_WANTS_HEADER_LINE_P (w))
901 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
902 }
903
904
905 /* Get the bounding box of the display area AREA of window W, without
906 mode lines. AREA < 0 means the whole window, not including bitmap
907 areas to the left and right of the window. Return in *TOP_LEFT_X
908 and TOP_LEFT_Y the frame-relative pixel coordinates of the
909 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
910 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
911 box. */
912
913 INLINE void
914 window_box_edges (w, area, top_left_x, top_left_y,
915 bottom_right_x, bottom_right_y)
916 struct window *w;
917 int area;
918 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
919 {
920 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
921 bottom_right_y);
922 *bottom_right_x += *top_left_x;
923 *bottom_right_y += *top_left_y;
924 }
925
926
927 \f
928 /***********************************************************************
929 Utilities
930 ***********************************************************************/
931
932 /* Return the next character from STR which is MAXLEN bytes long.
933 Return in *LEN the length of the character. This is like
934 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
935 we find one, we return a `?', but with the length of the invalid
936 character. */
937
938 static INLINE int
939 string_char_and_length (str, maxlen, len)
940 unsigned char *str;
941 int maxlen, *len;
942 {
943 int c;
944
945 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
946 if (!CHAR_VALID_P (c, 1))
947 /* We may not change the length here because other places in Emacs
948 don't use this function, i.e. they silently accept invalid
949 characters. */
950 c = '?';
951
952 return c;
953 }
954
955
956
957 /* Given a position POS containing a valid character and byte position
958 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
959
960 static struct text_pos
961 string_pos_nchars_ahead (pos, string, nchars)
962 struct text_pos pos;
963 Lisp_Object string;
964 int nchars;
965 {
966 xassert (STRINGP (string) && nchars >= 0);
967
968 if (STRING_MULTIBYTE (string))
969 {
970 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
971 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
972 int len;
973
974 while (nchars--)
975 {
976 string_char_and_length (p, rest, &len);
977 p += len, rest -= len;
978 xassert (rest >= 0);
979 CHARPOS (pos) += 1;
980 BYTEPOS (pos) += len;
981 }
982 }
983 else
984 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
985
986 return pos;
987 }
988
989
990 /* Value is the text position, i.e. character and byte position,
991 for character position CHARPOS in STRING. */
992
993 static INLINE struct text_pos
994 string_pos (charpos, string)
995 int charpos;
996 Lisp_Object string;
997 {
998 struct text_pos pos;
999 xassert (STRINGP (string));
1000 xassert (charpos >= 0);
1001 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1002 return pos;
1003 }
1004
1005
1006 /* Value is a text position, i.e. character and byte position, for
1007 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1008 means recognize multibyte characters. */
1009
1010 static struct text_pos
1011 c_string_pos (charpos, s, multibyte_p)
1012 int charpos;
1013 unsigned char *s;
1014 int multibyte_p;
1015 {
1016 struct text_pos pos;
1017
1018 xassert (s != NULL);
1019 xassert (charpos >= 0);
1020
1021 if (multibyte_p)
1022 {
1023 int rest = strlen (s), len;
1024
1025 SET_TEXT_POS (pos, 0, 0);
1026 while (charpos--)
1027 {
1028 string_char_and_length (s, rest, &len);
1029 s += len, rest -= len;
1030 xassert (rest >= 0);
1031 CHARPOS (pos) += 1;
1032 BYTEPOS (pos) += len;
1033 }
1034 }
1035 else
1036 SET_TEXT_POS (pos, charpos, charpos);
1037
1038 return pos;
1039 }
1040
1041
1042 /* Value is the number of characters in C string S. MULTIBYTE_P
1043 non-zero means recognize multibyte characters. */
1044
1045 static int
1046 number_of_chars (s, multibyte_p)
1047 unsigned char *s;
1048 int multibyte_p;
1049 {
1050 int nchars;
1051
1052 if (multibyte_p)
1053 {
1054 int rest = strlen (s), len;
1055 unsigned char *p = (unsigned char *) s;
1056
1057 for (nchars = 0; rest > 0; ++nchars)
1058 {
1059 string_char_and_length (p, rest, &len);
1060 rest -= len, p += len;
1061 }
1062 }
1063 else
1064 nchars = strlen (s);
1065
1066 return nchars;
1067 }
1068
1069
1070 /* Compute byte position NEWPOS->bytepos corresponding to
1071 NEWPOS->charpos. POS is a known position in string STRING.
1072 NEWPOS->charpos must be >= POS.charpos. */
1073
1074 static void
1075 compute_string_pos (newpos, pos, string)
1076 struct text_pos *newpos, pos;
1077 Lisp_Object string;
1078 {
1079 xassert (STRINGP (string));
1080 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1081
1082 if (STRING_MULTIBYTE (string))
1083 *newpos = string_pos_nchars_ahead (pos, string,
1084 CHARPOS (*newpos) - CHARPOS (pos));
1085 else
1086 BYTEPOS (*newpos) = CHARPOS (*newpos);
1087 }
1088
1089
1090 \f
1091 /***********************************************************************
1092 Lisp form evaluation
1093 ***********************************************************************/
1094
1095 /* Error handler for safe_eval and safe_call. */
1096
1097 static Lisp_Object
1098 safe_eval_handler (arg)
1099 Lisp_Object arg;
1100 {
1101 return Qnil;
1102 }
1103
1104
1105 /* Evaluate SEXPR and return the result, or nil if something went
1106 wrong. */
1107
1108 Lisp_Object
1109 safe_eval (sexpr)
1110 Lisp_Object sexpr;
1111 {
1112 int count = specpdl_ptr - specpdl;
1113 struct gcpro gcpro1;
1114 Lisp_Object val;
1115
1116 GCPRO1 (sexpr);
1117 specbind (Qinhibit_redisplay, Qt);
1118 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1119 UNGCPRO;
1120 return unbind_to (count, val);
1121 }
1122
1123
1124 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1125 Return the result, or nil if something went wrong. */
1126
1127 Lisp_Object
1128 safe_call (nargs, args)
1129 int nargs;
1130 Lisp_Object *args;
1131 {
1132 int count = specpdl_ptr - specpdl;
1133 Lisp_Object val;
1134 struct gcpro gcpro1;
1135
1136 GCPRO1 (args[0]);
1137 gcpro1.nvars = nargs;
1138 specbind (Qinhibit_redisplay, Qt);
1139 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1140 safe_eval_handler);
1141 UNGCPRO;
1142 return unbind_to (count, val);
1143 }
1144
1145
1146 /* Call function FN with one argument ARG.
1147 Return the result, or nil if something went wrong. */
1148
1149 Lisp_Object
1150 safe_call1 (fn, arg)
1151 Lisp_Object fn, arg;
1152 {
1153 Lisp_Object args[2];
1154 args[0] = fn;
1155 args[1] = arg;
1156 return safe_call (2, args);
1157 }
1158
1159
1160 \f
1161 /***********************************************************************
1162 Debugging
1163 ***********************************************************************/
1164
1165 #if 0
1166
1167 /* Define CHECK_IT to perform sanity checks on iterators.
1168 This is for debugging. It is too slow to do unconditionally. */
1169
1170 static void
1171 check_it (it)
1172 struct it *it;
1173 {
1174 if (it->method == next_element_from_string)
1175 {
1176 xassert (STRINGP (it->string));
1177 xassert (IT_STRING_CHARPOS (*it) >= 0);
1178 }
1179 else if (it->method == next_element_from_buffer)
1180 {
1181 /* Check that character and byte positions agree. */
1182 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1183 }
1184
1185 if (it->dpvec)
1186 xassert (it->current.dpvec_index >= 0);
1187 else
1188 xassert (it->current.dpvec_index < 0);
1189 }
1190
1191 #define CHECK_IT(IT) check_it ((IT))
1192
1193 #else /* not 0 */
1194
1195 #define CHECK_IT(IT) (void) 0
1196
1197 #endif /* not 0 */
1198
1199
1200 #if GLYPH_DEBUG
1201
1202 /* Check that the window end of window W is what we expect it
1203 to be---the last row in the current matrix displaying text. */
1204
1205 static void
1206 check_window_end (w)
1207 struct window *w;
1208 {
1209 if (!MINI_WINDOW_P (w)
1210 && !NILP (w->window_end_valid))
1211 {
1212 struct glyph_row *row;
1213 xassert ((row = MATRIX_ROW (w->current_matrix,
1214 XFASTINT (w->window_end_vpos)),
1215 !row->enabled_p
1216 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1217 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1218 }
1219 }
1220
1221 #define CHECK_WINDOW_END(W) check_window_end ((W))
1222
1223 #else /* not GLYPH_DEBUG */
1224
1225 #define CHECK_WINDOW_END(W) (void) 0
1226
1227 #endif /* not GLYPH_DEBUG */
1228
1229
1230 \f
1231 /***********************************************************************
1232 Iterator initialization
1233 ***********************************************************************/
1234
1235 /* Initialize IT for displaying current_buffer in window W, starting
1236 at character position CHARPOS. CHARPOS < 0 means that no buffer
1237 position is specified which is useful when the iterator is assigned
1238 a position later. BYTEPOS is the byte position corresponding to
1239 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1240
1241 If ROW is not null, calls to produce_glyphs with IT as parameter
1242 will produce glyphs in that row.
1243
1244 BASE_FACE_ID is the id of a base face to use. It must be one of
1245 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1246 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1247 displaying the tool-bar.
1248
1249 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1250 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1251 corresponding mode line glyph row of the desired matrix of W. */
1252
1253 void
1254 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1255 struct it *it;
1256 struct window *w;
1257 int charpos, bytepos;
1258 struct glyph_row *row;
1259 enum face_id base_face_id;
1260 {
1261 int highlight_region_p;
1262
1263 /* Some precondition checks. */
1264 xassert (w != NULL && it != NULL);
1265 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1266
1267 /* If face attributes have been changed since the last redisplay,
1268 free realized faces now because they depend on face definitions
1269 that might have changed. */
1270 if (face_change_count)
1271 {
1272 face_change_count = 0;
1273 free_all_realized_faces (Qnil);
1274 }
1275
1276 /* Use one of the mode line rows of W's desired matrix if
1277 appropriate. */
1278 if (row == NULL)
1279 {
1280 if (base_face_id == MODE_LINE_FACE_ID)
1281 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1282 else if (base_face_id == HEADER_LINE_FACE_ID)
1283 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1284 }
1285
1286 /* Clear IT. */
1287 bzero (it, sizeof *it);
1288 it->current.overlay_string_index = -1;
1289 it->current.dpvec_index = -1;
1290 it->base_face_id = base_face_id;
1291
1292 /* The window in which we iterate over current_buffer: */
1293 XSETWINDOW (it->window, w);
1294 it->w = w;
1295 it->f = XFRAME (w->frame);
1296
1297 /* Extra space between lines (on window systems only). */
1298 if (base_face_id == DEFAULT_FACE_ID
1299 && FRAME_WINDOW_P (it->f))
1300 {
1301 if (NATNUMP (current_buffer->extra_line_spacing))
1302 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1303 else if (it->f->extra_line_spacing > 0)
1304 it->extra_line_spacing = it->f->extra_line_spacing;
1305 }
1306
1307 /* If realized faces have been removed, e.g. because of face
1308 attribute changes of named faces, recompute them. */
1309 if (FRAME_FACE_CACHE (it->f)->used == 0)
1310 recompute_basic_faces (it->f);
1311
1312 /* Current value of the `space-width', and 'height' properties. */
1313 it->space_width = Qnil;
1314 it->font_height = Qnil;
1315
1316 /* Are control characters displayed as `^C'? */
1317 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1318
1319 /* -1 means everything between a CR and the following line end
1320 is invisible. >0 means lines indented more than this value are
1321 invisible. */
1322 it->selective = (INTEGERP (current_buffer->selective_display)
1323 ? XFASTINT (current_buffer->selective_display)
1324 : (!NILP (current_buffer->selective_display)
1325 ? -1 : 0));
1326 it->selective_display_ellipsis_p
1327 = !NILP (current_buffer->selective_display_ellipses);
1328
1329 /* Display table to use. */
1330 it->dp = window_display_table (w);
1331
1332 /* Are multibyte characters enabled in current_buffer? */
1333 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1334
1335 /* Non-zero if we should highlight the region. */
1336 highlight_region_p
1337 = (!NILP (Vtransient_mark_mode)
1338 && !NILP (current_buffer->mark_active)
1339 && XMARKER (current_buffer->mark)->buffer != 0);
1340
1341 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1342 start and end of a visible region in window IT->w. Set both to
1343 -1 to indicate no region. */
1344 if (highlight_region_p
1345 /* Maybe highlight only in selected window. */
1346 && (/* Either show region everywhere. */
1347 highlight_nonselected_windows
1348 /* Or show region in the selected window. */
1349 || w == XWINDOW (selected_window)
1350 /* Or show the region if we are in the mini-buffer and W is
1351 the window the mini-buffer refers to. */
1352 || (MINI_WINDOW_P (XWINDOW (selected_window))
1353 && w == XWINDOW (Vminibuf_scroll_window))))
1354 {
1355 int charpos = marker_position (current_buffer->mark);
1356 it->region_beg_charpos = min (PT, charpos);
1357 it->region_end_charpos = max (PT, charpos);
1358 }
1359 else
1360 it->region_beg_charpos = it->region_end_charpos = -1;
1361
1362 /* Get the position at which the redisplay_end_trigger hook should
1363 be run, if it is to be run at all. */
1364 if (MARKERP (w->redisplay_end_trigger)
1365 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1366 it->redisplay_end_trigger_charpos
1367 = marker_position (w->redisplay_end_trigger);
1368 else if (INTEGERP (w->redisplay_end_trigger))
1369 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1370
1371 /* Correct bogus values of tab_width. */
1372 it->tab_width = XINT (current_buffer->tab_width);
1373 if (it->tab_width <= 0 || it->tab_width > 1000)
1374 it->tab_width = 8;
1375
1376 /* Are lines in the display truncated? */
1377 it->truncate_lines_p
1378 = (base_face_id != DEFAULT_FACE_ID
1379 || XINT (it->w->hscroll)
1380 || (truncate_partial_width_windows
1381 && !WINDOW_FULL_WIDTH_P (it->w))
1382 || !NILP (current_buffer->truncate_lines));
1383
1384 /* Get dimensions of truncation and continuation glyphs. These are
1385 displayed as bitmaps under X, so we don't need them for such
1386 frames. */
1387 if (!FRAME_WINDOW_P (it->f))
1388 {
1389 if (it->truncate_lines_p)
1390 {
1391 /* We will need the truncation glyph. */
1392 xassert (it->glyph_row == NULL);
1393 produce_special_glyphs (it, IT_TRUNCATION);
1394 it->truncation_pixel_width = it->pixel_width;
1395 }
1396 else
1397 {
1398 /* We will need the continuation glyph. */
1399 xassert (it->glyph_row == NULL);
1400 produce_special_glyphs (it, IT_CONTINUATION);
1401 it->continuation_pixel_width = it->pixel_width;
1402 }
1403
1404 /* Reset these values to zero becaue the produce_special_glyphs
1405 above has changed them. */
1406 it->pixel_width = it->ascent = it->descent = 0;
1407 it->phys_ascent = it->phys_descent = 0;
1408 }
1409
1410 /* Set this after getting the dimensions of truncation and
1411 continuation glyphs, so that we don't produce glyphs when calling
1412 produce_special_glyphs, above. */
1413 it->glyph_row = row;
1414 it->area = TEXT_AREA;
1415
1416 /* Get the dimensions of the display area. The display area
1417 consists of the visible window area plus a horizontally scrolled
1418 part to the left of the window. All x-values are relative to the
1419 start of this total display area. */
1420 if (base_face_id != DEFAULT_FACE_ID)
1421 {
1422 /* Mode lines, menu bar in terminal frames. */
1423 it->first_visible_x = 0;
1424 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1425 }
1426 else
1427 {
1428 it->first_visible_x
1429 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1430 it->last_visible_x = (it->first_visible_x
1431 + window_box_width (w, TEXT_AREA));
1432
1433 /* If we truncate lines, leave room for the truncator glyph(s) at
1434 the right margin. Otherwise, leave room for the continuation
1435 glyph(s). Truncation and continuation glyphs are not inserted
1436 for window-based redisplay. */
1437 if (!FRAME_WINDOW_P (it->f))
1438 {
1439 if (it->truncate_lines_p)
1440 it->last_visible_x -= it->truncation_pixel_width;
1441 else
1442 it->last_visible_x -= it->continuation_pixel_width;
1443 }
1444
1445 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1446 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1447 }
1448
1449 /* Leave room for a border glyph. */
1450 if (!FRAME_WINDOW_P (it->f)
1451 && !WINDOW_RIGHTMOST_P (it->w))
1452 it->last_visible_x -= 1;
1453
1454 it->last_visible_y = window_text_bottom_y (w);
1455
1456 /* For mode lines and alike, arrange for the first glyph having a
1457 left box line if the face specifies a box. */
1458 if (base_face_id != DEFAULT_FACE_ID)
1459 {
1460 struct face *face;
1461
1462 it->face_id = base_face_id;
1463
1464 /* If we have a boxed mode line, make the first character appear
1465 with a left box line. */
1466 face = FACE_FROM_ID (it->f, base_face_id);
1467 if (face->box != FACE_NO_BOX)
1468 it->start_of_box_run_p = 1;
1469 }
1470
1471 /* If a buffer position was specified, set the iterator there,
1472 getting overlays and face properties from that position. */
1473 if (charpos > 0)
1474 {
1475 it->end_charpos = ZV;
1476 it->face_id = -1;
1477 IT_CHARPOS (*it) = charpos;
1478
1479 /* Compute byte position if not specified. */
1480 if (bytepos <= 0)
1481 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1482 else
1483 IT_BYTEPOS (*it) = bytepos;
1484
1485 /* Compute faces etc. */
1486 reseat (it, it->current.pos, 1);
1487 }
1488
1489 CHECK_IT (it);
1490 }
1491
1492
1493 /* Initialize IT for the display of window W with window start POS. */
1494
1495 void
1496 start_display (it, w, pos)
1497 struct it *it;
1498 struct window *w;
1499 struct text_pos pos;
1500 {
1501 int start_at_line_beg_p;
1502 struct glyph_row *row;
1503 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1504 int first_y;
1505
1506 row = w->desired_matrix->rows + first_vpos;
1507 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1508 first_y = it->current_y;
1509
1510 /* If window start is not at a line start, move back to the line
1511 start. This makes sure that we take continuation lines into
1512 account. */
1513 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1514 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1515 if (!start_at_line_beg_p)
1516 reseat_at_previous_visible_line_start (it);
1517
1518 /* If window start is not at a line start, skip forward to POS to
1519 get the correct continuation_lines_width and current_x. */
1520 if (!start_at_line_beg_p)
1521 {
1522 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1523
1524 /* If lines are continued, this line may end in the middle of a
1525 multi-glyph character (e.g. a control character displayed as
1526 \003, or in the middle of an overlay string). In this case
1527 move_it_to above will not have taken us to the start of
1528 the continuation line but to the end of the continued line. */
1529 if (!it->truncate_lines_p)
1530 {
1531 if (it->current_x > 0)
1532 {
1533 if (it->current.dpvec_index >= 0
1534 || it->current.overlay_string_index >= 0)
1535 {
1536 set_iterator_to_next (it, 1);
1537 move_it_in_display_line_to (it, -1, -1, 0);
1538 }
1539
1540 it->continuation_lines_width += it->current_x;
1541 }
1542
1543 /* We're starting a new display line, not affected by the
1544 height of the continued line, so clear the appropriate
1545 fields in the iterator structure. */
1546 it->max_ascent = it->max_descent = 0;
1547 it->max_phys_ascent = it->max_phys_descent = 0;
1548 }
1549
1550 it->current_y = first_y;
1551 it->vpos = 0;
1552 it->current_x = it->hpos = 0;
1553 }
1554
1555 #if 0 /* Don't assert the following because start_display is sometimes
1556 called intentionally with a window start that is not at a
1557 line start. Please leave this code in as a comment. */
1558
1559 /* Window start should be on a line start, now. */
1560 xassert (it->continuation_lines_width
1561 || IT_CHARPOS (it) == BEGV
1562 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1563 #endif /* 0 */
1564 }
1565
1566
1567 /* Initialize IT for stepping through current_buffer in window W,
1568 starting at position POS that includes overlay string and display
1569 vector/ control character translation position information. */
1570
1571 static void
1572 init_from_display_pos (it, w, pos)
1573 struct it *it;
1574 struct window *w;
1575 struct display_pos *pos;
1576 {
1577 /* Keep in mind: the call to reseat in init_iterator skips invisible
1578 text, so we might end up at a position different from POS. This
1579 is only a problem when POS is a row start after a newline and an
1580 overlay starts there with an after-string, and the overlay has an
1581 invisible property. Since we don't skip invisible text in
1582 display_line and elsewhere immediately after consuming the
1583 newline before the row start, such a POS will not be in a string,
1584 but the call to init_iterator below will move us to the
1585 after-string. */
1586 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1587 NULL, DEFAULT_FACE_ID);
1588
1589 /* If position is within an overlay string, set up IT to
1590 the right overlay string. */
1591 if (pos->overlay_string_index >= 0)
1592 {
1593 int relative_index;
1594
1595 /* We already have the first chunk of overlay strings in
1596 IT->overlay_strings. Load more until the one for
1597 pos->overlay_string_index is in IT->overlay_strings. */
1598 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1599 {
1600 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1601 it->current.overlay_string_index = 0;
1602 while (n--)
1603 {
1604 load_overlay_strings (it);
1605 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1606 }
1607 }
1608
1609 it->current.overlay_string_index = pos->overlay_string_index;
1610 relative_index = (it->current.overlay_string_index
1611 % OVERLAY_STRING_CHUNK_SIZE);
1612 it->string = it->overlay_strings[relative_index];
1613 xassert (STRINGP (it->string));
1614 it->current.string_pos = pos->string_pos;
1615 it->method = next_element_from_string;
1616 }
1617 else if (CHARPOS (pos->string_pos) >= 0)
1618 {
1619 /* Recorded position is not in an overlay string, but in another
1620 string. This can only be a string from a `display' property.
1621 IT should already be filled with that string. */
1622 it->current.string_pos = pos->string_pos;
1623 xassert (STRINGP (it->string));
1624 }
1625
1626 /* Restore position in display vector translations or control
1627 character translations. */
1628 if (pos->dpvec_index >= 0)
1629 {
1630 /* This fills IT->dpvec. */
1631 get_next_display_element (it);
1632 xassert (it->dpvec && it->current.dpvec_index == 0);
1633 it->current.dpvec_index = pos->dpvec_index;
1634 }
1635
1636 CHECK_IT (it);
1637 }
1638
1639
1640 /* Initialize IT for stepping through current_buffer in window W
1641 starting at ROW->start. */
1642
1643 static void
1644 init_to_row_start (it, w, row)
1645 struct it *it;
1646 struct window *w;
1647 struct glyph_row *row;
1648 {
1649 init_from_display_pos (it, w, &row->start);
1650 it->continuation_lines_width = row->continuation_lines_width;
1651 CHECK_IT (it);
1652 }
1653
1654
1655 /* Initialize IT for stepping through current_buffer in window W
1656 starting in the line following ROW, i.e. starting at ROW->end. */
1657
1658 static void
1659 init_to_row_end (it, w, row)
1660 struct it *it;
1661 struct window *w;
1662 struct glyph_row *row;
1663 {
1664 init_from_display_pos (it, w, &row->end);
1665
1666 if (row->continued_p)
1667 it->continuation_lines_width = (row->continuation_lines_width
1668 + row->pixel_width);
1669 CHECK_IT (it);
1670 }
1671
1672
1673
1674 \f
1675 /***********************************************************************
1676 Text properties
1677 ***********************************************************************/
1678
1679 /* Called when IT reaches IT->stop_charpos. Handle text property and
1680 overlay changes. Set IT->stop_charpos to the next position where
1681 to stop. */
1682
1683 static void
1684 handle_stop (it)
1685 struct it *it;
1686 {
1687 enum prop_handled handled;
1688 int handle_overlay_change_p = 1;
1689 struct props *p;
1690
1691 it->dpvec = NULL;
1692 it->current.dpvec_index = -1;
1693
1694 do
1695 {
1696 handled = HANDLED_NORMALLY;
1697
1698 /* Call text property handlers. */
1699 for (p = it_props; p->handler; ++p)
1700 {
1701 handled = p->handler (it);
1702
1703 if (handled == HANDLED_RECOMPUTE_PROPS)
1704 break;
1705 else if (handled == HANDLED_RETURN)
1706 return;
1707 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1708 handle_overlay_change_p = 0;
1709 }
1710
1711 if (handled != HANDLED_RECOMPUTE_PROPS)
1712 {
1713 /* Don't check for overlay strings below when set to deliver
1714 characters from a display vector. */
1715 if (it->method == next_element_from_display_vector)
1716 handle_overlay_change_p = 0;
1717
1718 /* Handle overlay changes. */
1719 if (handle_overlay_change_p)
1720 handled = handle_overlay_change (it);
1721
1722 /* Determine where to stop next. */
1723 if (handled == HANDLED_NORMALLY)
1724 compute_stop_pos (it);
1725 }
1726 }
1727 while (handled == HANDLED_RECOMPUTE_PROPS);
1728 }
1729
1730
1731 /* Compute IT->stop_charpos from text property and overlay change
1732 information for IT's current position. */
1733
1734 static void
1735 compute_stop_pos (it)
1736 struct it *it;
1737 {
1738 register INTERVAL iv, next_iv;
1739 Lisp_Object object, limit, position;
1740
1741 /* If nowhere else, stop at the end. */
1742 it->stop_charpos = it->end_charpos;
1743
1744 if (STRINGP (it->string))
1745 {
1746 /* Strings are usually short, so don't limit the search for
1747 properties. */
1748 object = it->string;
1749 limit = Qnil;
1750 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1751 }
1752 else
1753 {
1754 int charpos;
1755
1756 /* If next overlay change is in front of the current stop pos
1757 (which is IT->end_charpos), stop there. Note: value of
1758 next_overlay_change is point-max if no overlay change
1759 follows. */
1760 charpos = next_overlay_change (IT_CHARPOS (*it));
1761 if (charpos < it->stop_charpos)
1762 it->stop_charpos = charpos;
1763
1764 /* If showing the region, we have to stop at the region
1765 start or end because the face might change there. */
1766 if (it->region_beg_charpos > 0)
1767 {
1768 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1769 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1770 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1771 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1772 }
1773
1774 /* Set up variables for computing the stop position from text
1775 property changes. */
1776 XSETBUFFER (object, current_buffer);
1777 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1778 XSETFASTINT (position, IT_CHARPOS (*it));
1779
1780 }
1781
1782 /* Get the interval containing IT's position. Value is a null
1783 interval if there isn't such an interval. */
1784 iv = validate_interval_range (object, &position, &position, 0);
1785 if (!NULL_INTERVAL_P (iv))
1786 {
1787 Lisp_Object values_here[LAST_PROP_IDX];
1788 struct props *p;
1789
1790 /* Get properties here. */
1791 for (p = it_props; p->handler; ++p)
1792 values_here[p->idx] = textget (iv->plist, *p->name);
1793
1794 /* Look for an interval following iv that has different
1795 properties. */
1796 for (next_iv = next_interval (iv);
1797 (!NULL_INTERVAL_P (next_iv)
1798 && (NILP (limit)
1799 || XFASTINT (limit) > next_iv->position));
1800 next_iv = next_interval (next_iv))
1801 {
1802 for (p = it_props; p->handler; ++p)
1803 {
1804 Lisp_Object new_value;
1805
1806 new_value = textget (next_iv->plist, *p->name);
1807 if (!EQ (values_here[p->idx], new_value))
1808 break;
1809 }
1810
1811 if (p->handler)
1812 break;
1813 }
1814
1815 if (!NULL_INTERVAL_P (next_iv))
1816 {
1817 if (INTEGERP (limit)
1818 && next_iv->position >= XFASTINT (limit))
1819 /* No text property change up to limit. */
1820 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1821 else
1822 /* Text properties change in next_iv. */
1823 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1824 }
1825 }
1826
1827 xassert (STRINGP (it->string)
1828 || (it->stop_charpos >= BEGV
1829 && it->stop_charpos >= IT_CHARPOS (*it)));
1830 }
1831
1832
1833 /* Return the position of the next overlay change after POS in
1834 current_buffer. Value is point-max if no overlay change
1835 follows. This is like `next-overlay-change' but doesn't use
1836 xmalloc. */
1837
1838 static int
1839 next_overlay_change (pos)
1840 int pos;
1841 {
1842 int noverlays;
1843 int endpos;
1844 Lisp_Object *overlays;
1845 int len;
1846 int i;
1847
1848 /* Get all overlays at the given position. */
1849 len = 10;
1850 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1851 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1852 if (noverlays > len)
1853 {
1854 len = noverlays;
1855 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1856 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1857 }
1858
1859 /* If any of these overlays ends before endpos,
1860 use its ending point instead. */
1861 for (i = 0; i < noverlays; ++i)
1862 {
1863 Lisp_Object oend;
1864 int oendpos;
1865
1866 oend = OVERLAY_END (overlays[i]);
1867 oendpos = OVERLAY_POSITION (oend);
1868 endpos = min (endpos, oendpos);
1869 }
1870
1871 return endpos;
1872 }
1873
1874
1875 \f
1876 /***********************************************************************
1877 Fontification
1878 ***********************************************************************/
1879
1880 /* Handle changes in the `fontified' property of the current buffer by
1881 calling hook functions from Qfontification_functions to fontify
1882 regions of text. */
1883
1884 static enum prop_handled
1885 handle_fontified_prop (it)
1886 struct it *it;
1887 {
1888 Lisp_Object prop, pos;
1889 enum prop_handled handled = HANDLED_NORMALLY;
1890
1891 /* Get the value of the `fontified' property at IT's current buffer
1892 position. (The `fontified' property doesn't have a special
1893 meaning in strings.) If the value is nil, call functions from
1894 Qfontification_functions. */
1895 if (!STRINGP (it->string)
1896 && it->s == NULL
1897 && !NILP (Vfontification_functions)
1898 && !NILP (Vrun_hooks)
1899 && (pos = make_number (IT_CHARPOS (*it)),
1900 prop = Fget_char_property (pos, Qfontified, Qnil),
1901 NILP (prop)))
1902 {
1903 int count = specpdl_ptr - specpdl;
1904 Lisp_Object val;
1905
1906 val = Vfontification_functions;
1907 specbind (Qfontification_functions, Qnil);
1908 specbind (Qafter_change_functions, Qnil);
1909
1910 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
1911 safe_call1 (val, pos);
1912 else
1913 {
1914 Lisp_Object globals, fn;
1915 struct gcpro gcpro1, gcpro2;
1916
1917 globals = Qnil;
1918 GCPRO2 (val, globals);
1919
1920 for (; CONSP (val); val = XCDR (val))
1921 {
1922 fn = XCAR (val);
1923
1924 if (EQ (fn, Qt))
1925 {
1926 /* A value of t indicates this hook has a local
1927 binding; it means to run the global binding too.
1928 In a global value, t should not occur. If it
1929 does, we must ignore it to avoid an endless
1930 loop. */
1931 for (globals = Fdefault_value (Qfontification_functions);
1932 CONSP (globals);
1933 globals = XCDR (globals))
1934 {
1935 fn = XCAR (globals);
1936 if (!EQ (fn, Qt))
1937 safe_call1 (fn, pos);
1938 }
1939 }
1940 else
1941 safe_call1 (fn, pos);
1942 }
1943
1944 UNGCPRO;
1945 }
1946
1947 unbind_to (count, Qnil);
1948
1949 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
1950 something. This avoids an endless loop if they failed to
1951 fontify the text for which reason ever. */
1952 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
1953 handled = HANDLED_RECOMPUTE_PROPS;
1954 }
1955
1956 return handled;
1957 }
1958
1959
1960 \f
1961 /***********************************************************************
1962 Faces
1963 ***********************************************************************/
1964
1965 /* Set up iterator IT from face properties at its current position.
1966 Called from handle_stop. */
1967
1968 static enum prop_handled
1969 handle_face_prop (it)
1970 struct it *it;
1971 {
1972 int new_face_id, next_stop;
1973
1974 if (!STRINGP (it->string))
1975 {
1976 new_face_id
1977 = face_at_buffer_position (it->w,
1978 IT_CHARPOS (*it),
1979 it->region_beg_charpos,
1980 it->region_end_charpos,
1981 &next_stop,
1982 (IT_CHARPOS (*it)
1983 + TEXT_PROP_DISTANCE_LIMIT),
1984 0);
1985
1986 /* Is this a start of a run of characters with box face?
1987 Caveat: this can be called for a freshly initialized
1988 iterator; face_id is -1 is this case. We know that the new
1989 face will not change until limit, i.e. if the new face has a
1990 box, all characters up to limit will have one. But, as
1991 usual, we don't know whether limit is really the end. */
1992 if (new_face_id != it->face_id)
1993 {
1994 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
1995
1996 /* If new face has a box but old face has not, this is
1997 the start of a run of characters with box, i.e. it has
1998 a shadow on the left side. The value of face_id of the
1999 iterator will be -1 if this is the initial call that gets
2000 the face. In this case, we have to look in front of IT's
2001 position and see whether there is a face != new_face_id. */
2002 it->start_of_box_run_p
2003 = (new_face->box != FACE_NO_BOX
2004 && (it->face_id >= 0
2005 || IT_CHARPOS (*it) == BEG
2006 || new_face_id != face_before_it_pos (it)));
2007 it->face_box_p = new_face->box != FACE_NO_BOX;
2008 }
2009 }
2010 else
2011 {
2012 new_face_id
2013 = face_at_string_position (it->w,
2014 it->string,
2015 IT_STRING_CHARPOS (*it),
2016 (it->current.overlay_string_index >= 0
2017 ? IT_CHARPOS (*it)
2018 : 0),
2019 it->region_beg_charpos,
2020 it->region_end_charpos,
2021 &next_stop,
2022 it->base_face_id);
2023
2024 #if 0 /* This shouldn't be neccessary. Let's check it. */
2025 /* If IT is used to display a mode line we would really like to
2026 use the mode line face instead of the frame's default face. */
2027 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2028 && new_face_id == DEFAULT_FACE_ID)
2029 new_face_id = MODE_LINE_FACE_ID;
2030 #endif
2031
2032 /* Is this a start of a run of characters with box? Caveat:
2033 this can be called for a freshly allocated iterator; face_id
2034 is -1 is this case. We know that the new face will not
2035 change until the next check pos, i.e. if the new face has a
2036 box, all characters up to that position will have a
2037 box. But, as usual, we don't know whether that position
2038 is really the end. */
2039 if (new_face_id != it->face_id)
2040 {
2041 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2042 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2043
2044 /* If new face has a box but old face hasn't, this is the
2045 start of a run of characters with box, i.e. it has a
2046 shadow on the left side. */
2047 it->start_of_box_run_p
2048 = new_face->box && (old_face == NULL || !old_face->box);
2049 it->face_box_p = new_face->box != FACE_NO_BOX;
2050 }
2051 }
2052
2053 it->face_id = new_face_id;
2054 return HANDLED_NORMALLY;
2055 }
2056
2057
2058 /* Compute the face one character before or after the current position
2059 of IT. BEFORE_P non-zero means get the face in front of IT's
2060 position. Value is the id of the face. */
2061
2062 static int
2063 face_before_or_after_it_pos (it, before_p)
2064 struct it *it;
2065 int before_p;
2066 {
2067 int face_id, limit;
2068 int next_check_charpos;
2069 struct text_pos pos;
2070
2071 xassert (it->s == NULL);
2072
2073 if (STRINGP (it->string))
2074 {
2075 /* No face change past the end of the string (for the case
2076 we are padding with spaces). No face change before the
2077 string start. */
2078 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2079 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2080 return it->face_id;
2081
2082 /* Set pos to the position before or after IT's current position. */
2083 if (before_p)
2084 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2085 else
2086 /* For composition, we must check the character after the
2087 composition. */
2088 pos = (it->what == IT_COMPOSITION
2089 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2090 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2091
2092 /* Get the face for ASCII, or unibyte. */
2093 face_id
2094 = face_at_string_position (it->w,
2095 it->string,
2096 CHARPOS (pos),
2097 (it->current.overlay_string_index >= 0
2098 ? IT_CHARPOS (*it)
2099 : 0),
2100 it->region_beg_charpos,
2101 it->region_end_charpos,
2102 &next_check_charpos,
2103 it->base_face_id);
2104
2105 /* Correct the face for charsets different from ASCII. Do it
2106 for the multibyte case only. The face returned above is
2107 suitable for unibyte text if IT->string is unibyte. */
2108 if (STRING_MULTIBYTE (it->string))
2109 {
2110 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2111 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2112 int c, len;
2113 struct face *face = FACE_FROM_ID (it->f, face_id);
2114
2115 c = string_char_and_length (p, rest, &len);
2116 face_id = FACE_FOR_CHAR (it->f, face, c);
2117 }
2118 }
2119 else
2120 {
2121 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2122 || (IT_CHARPOS (*it) <= BEGV && before_p))
2123 return it->face_id;
2124
2125 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2126 pos = it->current.pos;
2127
2128 if (before_p)
2129 DEC_TEXT_POS (pos, it->multibyte_p);
2130 else
2131 {
2132 if (it->what == IT_COMPOSITION)
2133 /* For composition, we must check the position after the
2134 composition. */
2135 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2136 else
2137 INC_TEXT_POS (pos, it->multibyte_p);
2138 }
2139 /* Determine face for CHARSET_ASCII, or unibyte. */
2140 face_id = face_at_buffer_position (it->w,
2141 CHARPOS (pos),
2142 it->region_beg_charpos,
2143 it->region_end_charpos,
2144 &next_check_charpos,
2145 limit, 0);
2146
2147 /* Correct the face for charsets different from ASCII. Do it
2148 for the multibyte case only. The face returned above is
2149 suitable for unibyte text if current_buffer is unibyte. */
2150 if (it->multibyte_p)
2151 {
2152 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2153 struct face *face = FACE_FROM_ID (it->f, face_id);
2154 face_id = FACE_FOR_CHAR (it->f, face, c);
2155 }
2156 }
2157
2158 return face_id;
2159 }
2160
2161
2162 \f
2163 /***********************************************************************
2164 Invisible text
2165 ***********************************************************************/
2166
2167 /* Set up iterator IT from invisible properties at its current
2168 position. Called from handle_stop. */
2169
2170 static enum prop_handled
2171 handle_invisible_prop (it)
2172 struct it *it;
2173 {
2174 enum prop_handled handled = HANDLED_NORMALLY;
2175
2176 if (STRINGP (it->string))
2177 {
2178 extern Lisp_Object Qinvisible;
2179 Lisp_Object prop, end_charpos, limit, charpos;
2180
2181 /* Get the value of the invisible text property at the
2182 current position. Value will be nil if there is no such
2183 property. */
2184 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2185 prop = Fget_text_property (charpos, Qinvisible, it->string);
2186
2187 if (!NILP (prop)
2188 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2189 {
2190 handled = HANDLED_RECOMPUTE_PROPS;
2191
2192 /* Get the position at which the next change of the
2193 invisible text property can be found in IT->string.
2194 Value will be nil if the property value is the same for
2195 all the rest of IT->string. */
2196 XSETINT (limit, XSTRING (it->string)->size);
2197 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2198 it->string, limit);
2199
2200 /* Text at current position is invisible. The next
2201 change in the property is at position end_charpos.
2202 Move IT's current position to that position. */
2203 if (INTEGERP (end_charpos)
2204 && XFASTINT (end_charpos) < XFASTINT (limit))
2205 {
2206 struct text_pos old;
2207 old = it->current.string_pos;
2208 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2209 compute_string_pos (&it->current.string_pos, old, it->string);
2210 }
2211 else
2212 {
2213 /* The rest of the string is invisible. If this is an
2214 overlay string, proceed with the next overlay string
2215 or whatever comes and return a character from there. */
2216 if (it->current.overlay_string_index >= 0)
2217 {
2218 next_overlay_string (it);
2219 /* Don't check for overlay strings when we just
2220 finished processing them. */
2221 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2222 }
2223 else
2224 {
2225 struct Lisp_String *s = XSTRING (it->string);
2226 IT_STRING_CHARPOS (*it) = s->size;
2227 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2228 }
2229 }
2230 }
2231 }
2232 else
2233 {
2234 int visible_p, newpos, next_stop;
2235 Lisp_Object pos, prop;
2236
2237 /* First of all, is there invisible text at this position? */
2238 XSETFASTINT (pos, IT_CHARPOS (*it));
2239 prop = Fget_char_property (pos, Qinvisible, it->window);
2240
2241 /* If we are on invisible text, skip over it. */
2242 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2243 && IT_CHARPOS (*it) < it->end_charpos)
2244 {
2245 /* Record whether we have to display an ellipsis for the
2246 invisible text. */
2247 int display_ellipsis_p
2248 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2249
2250 handled = HANDLED_RECOMPUTE_PROPS;
2251
2252 /* Loop skipping over invisible text. The loop is left at
2253 ZV or with IT on the first char being visible again. */
2254 do
2255 {
2256 /* Try to skip some invisible text. Return value is the
2257 position reached which can be equal to IT's position
2258 if there is nothing invisible here. This skips both
2259 over invisible text properties and overlays with
2260 invisible property. */
2261 newpos = skip_invisible (IT_CHARPOS (*it),
2262 &next_stop, ZV, it->window);
2263
2264 /* If we skipped nothing at all we weren't at invisible
2265 text in the first place. If everything to the end of
2266 the buffer was skipped, end the loop. */
2267 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2268 visible_p = 1;
2269 else
2270 {
2271 /* We skipped some characters but not necessarily
2272 all there are. Check if we ended up on visible
2273 text. Fget_char_property returns the property of
2274 the char before the given position, i.e. if we
2275 get visible_p = 1, this means that the char at
2276 newpos is visible. */
2277 XSETFASTINT (pos, newpos);
2278 prop = Fget_char_property (pos, Qinvisible, it->window);
2279 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2280 }
2281
2282 /* If we ended up on invisible text, proceed to
2283 skip starting with next_stop. */
2284 if (!visible_p)
2285 IT_CHARPOS (*it) = next_stop;
2286 }
2287 while (!visible_p);
2288
2289 /* The position newpos is now either ZV or on visible text. */
2290 IT_CHARPOS (*it) = newpos;
2291 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2292
2293 /* Maybe return `...' next for the end of the invisible text. */
2294 if (display_ellipsis_p)
2295 {
2296 if (it->dp
2297 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2298 {
2299 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2300 it->dpvec = v->contents;
2301 it->dpend = v->contents + v->size;
2302 }
2303 else
2304 {
2305 /* Default `...'. */
2306 it->dpvec = default_invis_vector;
2307 it->dpend = default_invis_vector + 3;
2308 }
2309
2310 /* The ellipsis display does not replace the display of
2311 the character at the new position. Indicate this by
2312 setting IT->dpvec_char_len to zero. */
2313 it->dpvec_char_len = 0;
2314
2315 it->current.dpvec_index = 0;
2316 it->method = next_element_from_display_vector;
2317 }
2318 }
2319 }
2320
2321 return handled;
2322 }
2323
2324
2325 \f
2326 /***********************************************************************
2327 'display' property
2328 ***********************************************************************/
2329
2330 /* Set up iterator IT from `display' property at its current position.
2331 Called from handle_stop. */
2332
2333 static enum prop_handled
2334 handle_display_prop (it)
2335 struct it *it;
2336 {
2337 Lisp_Object prop, object;
2338 struct text_pos *position;
2339 int space_or_image_found_p;
2340
2341 if (STRINGP (it->string))
2342 {
2343 object = it->string;
2344 position = &it->current.string_pos;
2345 }
2346 else
2347 {
2348 object = Qnil;
2349 position = &it->current.pos;
2350 }
2351
2352 /* Reset those iterator values set from display property values. */
2353 it->font_height = Qnil;
2354 it->space_width = Qnil;
2355 it->voffset = 0;
2356
2357 /* We don't support recursive `display' properties, i.e. string
2358 values that have a string `display' property, that have a string
2359 `display' property etc. */
2360 if (!it->string_from_display_prop_p)
2361 it->area = TEXT_AREA;
2362
2363 prop = Fget_char_property (make_number (position->charpos),
2364 Qdisplay, object);
2365 if (NILP (prop))
2366 return HANDLED_NORMALLY;
2367
2368 space_or_image_found_p = 0;
2369 if (CONSP (prop)
2370 && CONSP (XCAR (prop))
2371 && !EQ (Qmargin, XCAR (XCAR (prop))))
2372 {
2373 /* A list of sub-properties. */
2374 while (CONSP (prop))
2375 {
2376 if (handle_single_display_prop (it, XCAR (prop), object, position))
2377 space_or_image_found_p = 1;
2378 prop = XCDR (prop);
2379 }
2380 }
2381 else if (VECTORP (prop))
2382 {
2383 int i;
2384 for (i = 0; i < XVECTOR (prop)->size; ++i)
2385 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2386 object, position))
2387 space_or_image_found_p = 1;
2388 }
2389 else
2390 {
2391 if (handle_single_display_prop (it, prop, object, position))
2392 space_or_image_found_p = 1;
2393 }
2394
2395 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2396 }
2397
2398
2399 /* Value is the position of the end of the `display' property starting
2400 at START_POS in OBJECT. */
2401
2402 static struct text_pos
2403 display_prop_end (it, object, start_pos)
2404 struct it *it;
2405 Lisp_Object object;
2406 struct text_pos start_pos;
2407 {
2408 Lisp_Object end;
2409 struct text_pos end_pos;
2410
2411 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2412 Qdisplay, object, Qnil);
2413 CHARPOS (end_pos) = XFASTINT (end);
2414 if (STRINGP (object))
2415 compute_string_pos (&end_pos, start_pos, it->string);
2416 else
2417 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2418
2419 return end_pos;
2420 }
2421
2422
2423 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2424 is the object in which the `display' property was found. *POSITION
2425 is the position at which it was found.
2426
2427 If PROP is a `space' or `image' sub-property, set *POSITION to the
2428 end position of the `display' property.
2429
2430 Value is non-zero if a `space' or `image' property value was found. */
2431
2432 static int
2433 handle_single_display_prop (it, prop, object, position)
2434 struct it *it;
2435 Lisp_Object prop;
2436 Lisp_Object object;
2437 struct text_pos *position;
2438 {
2439 Lisp_Object value;
2440 int space_or_image_found_p = 0;
2441 Lisp_Object form;
2442
2443 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2444 evaluated. If the result is nil, VALUE is ignored. */
2445 form = Qt;
2446 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2447 {
2448 prop = XCDR (prop);
2449 if (!CONSP (prop))
2450 return 0;
2451 form = XCAR (prop);
2452 prop = XCDR (prop);
2453 }
2454
2455 if (!NILP (form) && !EQ (form, Qt))
2456 {
2457 struct gcpro gcpro1;
2458 struct text_pos end_pos, pt;
2459
2460 GCPRO1 (form);
2461 end_pos = display_prop_end (it, object, *position);
2462
2463 /* Temporarily set point to the end position, and then evaluate
2464 the form. This makes `(eolp)' work as FORM. */
2465 if (BUFFERP (object))
2466 {
2467 CHARPOS (pt) = PT;
2468 BYTEPOS (pt) = PT_BYTE;
2469 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2470 }
2471
2472 form = safe_eval (form);
2473
2474 if (BUFFERP (object))
2475 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2476 UNGCPRO;
2477 }
2478
2479 if (NILP (form))
2480 return 0;
2481
2482 if (CONSP (prop)
2483 && EQ (XCAR (prop), Qheight)
2484 && CONSP (XCDR (prop)))
2485 {
2486 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2487 return 0;
2488
2489 /* `(height HEIGHT)'. */
2490 it->font_height = XCAR (XCDR (prop));
2491 if (!NILP (it->font_height))
2492 {
2493 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2494 int new_height = -1;
2495
2496 if (CONSP (it->font_height)
2497 && (EQ (XCAR (it->font_height), Qplus)
2498 || EQ (XCAR (it->font_height), Qminus))
2499 && CONSP (XCDR (it->font_height))
2500 && INTEGERP (XCAR (XCDR (it->font_height))))
2501 {
2502 /* `(+ N)' or `(- N)' where N is an integer. */
2503 int steps = XINT (XCAR (XCDR (it->font_height)));
2504 if (EQ (XCAR (it->font_height), Qplus))
2505 steps = - steps;
2506 it->face_id = smaller_face (it->f, it->face_id, steps);
2507 }
2508 else if (FUNCTIONP (it->font_height))
2509 {
2510 /* Call function with current height as argument.
2511 Value is the new height. */
2512 Lisp_Object height;
2513 height = safe_call1 (it->font_height,
2514 face->lface[LFACE_HEIGHT_INDEX]);
2515 if (NUMBERP (height))
2516 new_height = XFLOATINT (height);
2517 }
2518 else if (NUMBERP (it->font_height))
2519 {
2520 /* Value is a multiple of the canonical char height. */
2521 struct face *face;
2522
2523 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2524 new_height = (XFLOATINT (it->font_height)
2525 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2526 }
2527 else
2528 {
2529 /* Evaluate IT->font_height with `height' bound to the
2530 current specified height to get the new height. */
2531 Lisp_Object value;
2532 int count = specpdl_ptr - specpdl;
2533
2534 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2535 value = safe_eval (it->font_height);
2536 unbind_to (count, Qnil);
2537
2538 if (NUMBERP (value))
2539 new_height = XFLOATINT (value);
2540 }
2541
2542 if (new_height > 0)
2543 it->face_id = face_with_height (it->f, it->face_id, new_height);
2544 }
2545 }
2546 else if (CONSP (prop)
2547 && EQ (XCAR (prop), Qspace_width)
2548 && CONSP (XCDR (prop)))
2549 {
2550 /* `(space_width WIDTH)'. */
2551 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2552 return 0;
2553
2554 value = XCAR (XCDR (prop));
2555 if (NUMBERP (value) && XFLOATINT (value) > 0)
2556 it->space_width = value;
2557 }
2558 else if (CONSP (prop)
2559 && EQ (XCAR (prop), Qraise)
2560 && CONSP (XCDR (prop)))
2561 {
2562 /* `(raise FACTOR)'. */
2563 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2564 return 0;
2565
2566 #ifdef HAVE_WINDOW_SYSTEM
2567 value = XCAR (XCDR (prop));
2568 if (NUMBERP (value))
2569 {
2570 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2571 it->voffset = - (XFLOATINT (value)
2572 * (FONT_HEIGHT (face->font)));
2573 }
2574 #endif /* HAVE_WINDOW_SYSTEM */
2575 }
2576 else if (!it->string_from_display_prop_p)
2577 {
2578 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2579 VALUE) or `((margin nil) VALUE)' or VALUE. */
2580 Lisp_Object location, value;
2581 struct text_pos start_pos;
2582 int valid_p;
2583
2584 /* Characters having this form of property are not displayed, so
2585 we have to find the end of the property. */
2586 start_pos = *position;
2587 *position = display_prop_end (it, object, start_pos);
2588 value = Qnil;
2589
2590 /* Let's stop at the new position and assume that all
2591 text properties change there. */
2592 it->stop_charpos = position->charpos;
2593
2594 location = Qunbound;
2595 if (CONSP (prop) && CONSP (XCAR (prop)))
2596 {
2597 Lisp_Object tem;
2598
2599 value = XCDR (prop);
2600 if (CONSP (value))
2601 value = XCAR (value);
2602
2603 tem = XCAR (prop);
2604 if (EQ (XCAR (tem), Qmargin)
2605 && (tem = XCDR (tem),
2606 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2607 (NILP (tem)
2608 || EQ (tem, Qleft_margin)
2609 || EQ (tem, Qright_margin))))
2610 location = tem;
2611 }
2612
2613 if (EQ (location, Qunbound))
2614 {
2615 location = Qnil;
2616 value = prop;
2617 }
2618
2619 #ifdef HAVE_WINDOW_SYSTEM
2620 if (FRAME_TERMCAP_P (it->f))
2621 valid_p = STRINGP (value);
2622 else
2623 valid_p = (STRINGP (value)
2624 || (CONSP (value) && EQ (XCAR (value), Qspace))
2625 || valid_image_p (value));
2626 #else /* not HAVE_WINDOW_SYSTEM */
2627 valid_p = STRINGP (value);
2628 #endif /* not HAVE_WINDOW_SYSTEM */
2629
2630 if ((EQ (location, Qleft_margin)
2631 || EQ (location, Qright_margin)
2632 || NILP (location))
2633 && valid_p)
2634 {
2635 space_or_image_found_p = 1;
2636
2637 /* Save current settings of IT so that we can restore them
2638 when we are finished with the glyph property value. */
2639 push_it (it);
2640
2641 if (NILP (location))
2642 it->area = TEXT_AREA;
2643 else if (EQ (location, Qleft_margin))
2644 it->area = LEFT_MARGIN_AREA;
2645 else
2646 it->area = RIGHT_MARGIN_AREA;
2647
2648 if (STRINGP (value))
2649 {
2650 it->string = value;
2651 it->multibyte_p = STRING_MULTIBYTE (it->string);
2652 it->current.overlay_string_index = -1;
2653 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2654 it->end_charpos = it->string_nchars
2655 = XSTRING (it->string)->size;
2656 it->method = next_element_from_string;
2657 it->stop_charpos = 0;
2658 it->string_from_display_prop_p = 1;
2659 }
2660 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2661 {
2662 it->method = next_element_from_stretch;
2663 it->object = value;
2664 it->current.pos = it->position = start_pos;
2665 }
2666 #ifdef HAVE_WINDOW_SYSTEM
2667 else
2668 {
2669 it->what = IT_IMAGE;
2670 it->image_id = lookup_image (it->f, value);
2671 it->position = start_pos;
2672 it->object = NILP (object) ? it->w->buffer : object;
2673 it->method = next_element_from_image;
2674
2675 /* Say that we haven't consumed the characters with
2676 `display' property yet. The call to pop_it in
2677 set_iterator_to_next will clean this up. */
2678 *position = start_pos;
2679 }
2680 #endif /* HAVE_WINDOW_SYSTEM */
2681 }
2682 else
2683 /* Invalid property or property not supported. Restore
2684 the position to what it was before. */
2685 *position = start_pos;
2686 }
2687
2688 return space_or_image_found_p;
2689 }
2690
2691
2692 /* Check if PROP is a display sub-property value whose text should be
2693 treated as intangible. */
2694
2695 static int
2696 single_display_prop_intangible_p (prop)
2697 Lisp_Object prop;
2698 {
2699 /* Skip over `when FORM'. */
2700 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2701 {
2702 prop = XCDR (prop);
2703 if (!CONSP (prop))
2704 return 0;
2705 prop = XCDR (prop);
2706 }
2707
2708 if (!CONSP (prop))
2709 return 0;
2710
2711 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2712 we don't need to treat text as intangible. */
2713 if (EQ (XCAR (prop), Qmargin))
2714 {
2715 prop = XCDR (prop);
2716 if (!CONSP (prop))
2717 return 0;
2718
2719 prop = XCDR (prop);
2720 if (!CONSP (prop)
2721 || EQ (XCAR (prop), Qleft_margin)
2722 || EQ (XCAR (prop), Qright_margin))
2723 return 0;
2724 }
2725
2726 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2727 }
2728
2729
2730 /* Check if PROP is a display property value whose text should be
2731 treated as intangible. */
2732
2733 int
2734 display_prop_intangible_p (prop)
2735 Lisp_Object prop;
2736 {
2737 if (CONSP (prop)
2738 && CONSP (XCAR (prop))
2739 && !EQ (Qmargin, XCAR (XCAR (prop))))
2740 {
2741 /* A list of sub-properties. */
2742 while (CONSP (prop))
2743 {
2744 if (single_display_prop_intangible_p (XCAR (prop)))
2745 return 1;
2746 prop = XCDR (prop);
2747 }
2748 }
2749 else if (VECTORP (prop))
2750 {
2751 /* A vector of sub-properties. */
2752 int i;
2753 for (i = 0; i < XVECTOR (prop)->size; ++i)
2754 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2755 return 1;
2756 }
2757 else
2758 return single_display_prop_intangible_p (prop);
2759
2760 return 0;
2761 }
2762
2763 \f
2764 /***********************************************************************
2765 `composition' property
2766 ***********************************************************************/
2767
2768 /* Set up iterator IT from `composition' property at its current
2769 position. Called from handle_stop. */
2770
2771 static enum prop_handled
2772 handle_composition_prop (it)
2773 struct it *it;
2774 {
2775 Lisp_Object prop, string;
2776 int pos, pos_byte, end;
2777 enum prop_handled handled = HANDLED_NORMALLY;
2778
2779 if (STRINGP (it->string))
2780 {
2781 pos = IT_STRING_CHARPOS (*it);
2782 pos_byte = IT_STRING_BYTEPOS (*it);
2783 string = it->string;
2784 }
2785 else
2786 {
2787 pos = IT_CHARPOS (*it);
2788 pos_byte = IT_BYTEPOS (*it);
2789 string = Qnil;
2790 }
2791
2792 /* If there's a valid composition and point is not inside of the
2793 composition (in the case that the composition is from the current
2794 buffer), draw a glyph composed from the composition components. */
2795 if (find_composition (pos, -1, &pos, &end, &prop, string)
2796 && COMPOSITION_VALID_P (pos, end, prop)
2797 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2798 {
2799 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2800
2801 if (id >= 0)
2802 {
2803 it->method = next_element_from_composition;
2804 it->cmp_id = id;
2805 it->cmp_len = COMPOSITION_LENGTH (prop);
2806 /* For a terminal, draw only the first character of the
2807 components. */
2808 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2809 it->len = (STRINGP (it->string)
2810 ? string_char_to_byte (it->string, end)
2811 : CHAR_TO_BYTE (end)) - pos_byte;
2812 it->stop_charpos = end;
2813 handled = HANDLED_RETURN;
2814 }
2815 }
2816
2817 return handled;
2818 }
2819
2820
2821 \f
2822 /***********************************************************************
2823 Overlay strings
2824 ***********************************************************************/
2825
2826 /* The following structure is used to record overlay strings for
2827 later sorting in load_overlay_strings. */
2828
2829 struct overlay_entry
2830 {
2831 Lisp_Object overlay;
2832 Lisp_Object string;
2833 int priority;
2834 int after_string_p;
2835 };
2836
2837
2838 /* Set up iterator IT from overlay strings at its current position.
2839 Called from handle_stop. */
2840
2841 static enum prop_handled
2842 handle_overlay_change (it)
2843 struct it *it;
2844 {
2845 if (!STRINGP (it->string) && get_overlay_strings (it))
2846 return HANDLED_RECOMPUTE_PROPS;
2847 else
2848 return HANDLED_NORMALLY;
2849 }
2850
2851
2852 /* Set up the next overlay string for delivery by IT, if there is an
2853 overlay string to deliver. Called by set_iterator_to_next when the
2854 end of the current overlay string is reached. If there are more
2855 overlay strings to display, IT->string and
2856 IT->current.overlay_string_index are set appropriately here.
2857 Otherwise IT->string is set to nil. */
2858
2859 static void
2860 next_overlay_string (it)
2861 struct it *it;
2862 {
2863 ++it->current.overlay_string_index;
2864 if (it->current.overlay_string_index == it->n_overlay_strings)
2865 {
2866 /* No more overlay strings. Restore IT's settings to what
2867 they were before overlay strings were processed, and
2868 continue to deliver from current_buffer. */
2869 pop_it (it);
2870 xassert (it->stop_charpos >= BEGV
2871 && it->stop_charpos <= it->end_charpos);
2872 it->string = Qnil;
2873 it->current.overlay_string_index = -1;
2874 SET_TEXT_POS (it->current.string_pos, -1, -1);
2875 it->n_overlay_strings = 0;
2876 it->method = next_element_from_buffer;
2877
2878 /* If we're at the end of the buffer, record that we have
2879 processed the overlay strings there already, so that
2880 next_element_from_buffer doesn't try it again. */
2881 if (IT_CHARPOS (*it) >= it->end_charpos)
2882 it->overlay_strings_at_end_processed_p = 1;
2883 }
2884 else
2885 {
2886 /* There are more overlay strings to process. If
2887 IT->current.overlay_string_index has advanced to a position
2888 where we must load IT->overlay_strings with more strings, do
2889 it. */
2890 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
2891
2892 if (it->current.overlay_string_index && i == 0)
2893 load_overlay_strings (it);
2894
2895 /* Initialize IT to deliver display elements from the overlay
2896 string. */
2897 it->string = it->overlay_strings[i];
2898 it->multibyte_p = STRING_MULTIBYTE (it->string);
2899 SET_TEXT_POS (it->current.string_pos, 0, 0);
2900 it->method = next_element_from_string;
2901 it->stop_charpos = 0;
2902 }
2903
2904 CHECK_IT (it);
2905 }
2906
2907
2908 /* Compare two overlay_entry structures E1 and E2. Used as a
2909 comparison function for qsort in load_overlay_strings. Overlay
2910 strings for the same position are sorted so that
2911
2912 1. All after-strings come in front of before-strings, except
2913 when they come from the same overlay.
2914
2915 2. Within after-strings, strings are sorted so that overlay strings
2916 from overlays with higher priorities come first.
2917
2918 2. Within before-strings, strings are sorted so that overlay
2919 strings from overlays with higher priorities come last.
2920
2921 Value is analogous to strcmp. */
2922
2923
2924 static int
2925 compare_overlay_entries (e1, e2)
2926 void *e1, *e2;
2927 {
2928 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
2929 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
2930 int result;
2931
2932 if (entry1->after_string_p != entry2->after_string_p)
2933 {
2934 /* Let after-strings appear in front of before-strings if
2935 they come from different overlays. */
2936 if (EQ (entry1->overlay, entry2->overlay))
2937 result = entry1->after_string_p ? 1 : -1;
2938 else
2939 result = entry1->after_string_p ? -1 : 1;
2940 }
2941 else if (entry1->after_string_p)
2942 /* After-strings sorted in order of decreasing priority. */
2943 result = entry2->priority - entry1->priority;
2944 else
2945 /* Before-strings sorted in order of increasing priority. */
2946 result = entry1->priority - entry2->priority;
2947
2948 return result;
2949 }
2950
2951
2952 /* Load the vector IT->overlay_strings with overlay strings from IT's
2953 current buffer position. Set IT->n_overlays to the total number of
2954 overlay strings found.
2955
2956 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
2957 a time. On entry into load_overlay_strings,
2958 IT->current.overlay_string_index gives the number of overlay
2959 strings that have already been loaded by previous calls to this
2960 function.
2961
2962 IT->add_overlay_start contains an additional overlay start
2963 position to consider for taking overlay strings from, if non-zero.
2964 This position comes into play when the overlay has an `invisible'
2965 property, and both before and after-strings. When we've skipped to
2966 the end of the overlay, because of its `invisible' property, we
2967 nevertheless want its before-string to appear.
2968 IT->add_overlay_start will contain the overlay start position
2969 in this case.
2970
2971 Overlay strings are sorted so that after-string strings come in
2972 front of before-string strings. Within before and after-strings,
2973 strings are sorted by overlay priority. See also function
2974 compare_overlay_entries. */
2975
2976 static void
2977 load_overlay_strings (it)
2978 struct it *it;
2979 {
2980 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
2981 Lisp_Object ov, overlay, window, str, invisible;
2982 int start, end;
2983 int size = 20;
2984 int n = 0, i, j, invis_p;
2985 struct overlay_entry *entries
2986 = (struct overlay_entry *) alloca (size * sizeof *entries);
2987 int charpos = IT_CHARPOS (*it);
2988
2989 /* Append the overlay string STRING of overlay OVERLAY to vector
2990 `entries' which has size `size' and currently contains `n'
2991 elements. AFTER_P non-zero means STRING is an after-string of
2992 OVERLAY. */
2993 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
2994 do \
2995 { \
2996 Lisp_Object priority; \
2997 \
2998 if (n == size) \
2999 { \
3000 int new_size = 2 * size; \
3001 struct overlay_entry *old = entries; \
3002 entries = \
3003 (struct overlay_entry *) alloca (new_size \
3004 * sizeof *entries); \
3005 bcopy (old, entries, size * sizeof *entries); \
3006 size = new_size; \
3007 } \
3008 \
3009 entries[n].string = (STRING); \
3010 entries[n].overlay = (OVERLAY); \
3011 priority = Foverlay_get ((OVERLAY), Qpriority); \
3012 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3013 entries[n].after_string_p = (AFTER_P); \
3014 ++n; \
3015 } \
3016 while (0)
3017
3018 /* Process overlay before the overlay center. */
3019 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3020 {
3021 overlay = XCAR (ov);
3022 xassert (OVERLAYP (overlay));
3023 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3024 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3025
3026 if (end < charpos)
3027 break;
3028
3029 /* Skip this overlay if it doesn't start or end at IT's current
3030 position. */
3031 if (end != charpos && start != charpos)
3032 continue;
3033
3034 /* Skip this overlay if it doesn't apply to IT->w. */
3035 window = Foverlay_get (overlay, Qwindow);
3036 if (WINDOWP (window) && XWINDOW (window) != it->w)
3037 continue;
3038
3039 /* If the text ``under'' the overlay is invisible, both before-
3040 and after-strings from this overlay are visible; start and
3041 end position are indistinguishable. */
3042 invisible = Foverlay_get (overlay, Qinvisible);
3043 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3044
3045 /* If overlay has a non-empty before-string, record it. */
3046 if ((start == charpos || (end == charpos && invis_p))
3047 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3048 && XSTRING (str)->size)
3049 RECORD_OVERLAY_STRING (overlay, str, 0);
3050
3051 /* If overlay has a non-empty after-string, record it. */
3052 if ((end == charpos || (start == charpos && invis_p))
3053 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3054 && XSTRING (str)->size)
3055 RECORD_OVERLAY_STRING (overlay, str, 1);
3056 }
3057
3058 /* Process overlays after the overlay center. */
3059 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3060 {
3061 overlay = XCAR (ov);
3062 xassert (OVERLAYP (overlay));
3063 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3064 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3065
3066 if (start > charpos)
3067 break;
3068
3069 /* Skip this overlay if it doesn't start or end at IT's current
3070 position. */
3071 if (end != charpos && start != charpos)
3072 continue;
3073
3074 /* Skip this overlay if it doesn't apply to IT->w. */
3075 window = Foverlay_get (overlay, Qwindow);
3076 if (WINDOWP (window) && XWINDOW (window) != it->w)
3077 continue;
3078
3079 /* If the text ``under'' the overlay is invisible, it has a zero
3080 dimension, and both before- and after-strings apply. */
3081 invisible = Foverlay_get (overlay, Qinvisible);
3082 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3083
3084 /* If overlay has a non-empty before-string, record it. */
3085 if ((start == charpos || (end == charpos && invis_p))
3086 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3087 && XSTRING (str)->size)
3088 RECORD_OVERLAY_STRING (overlay, str, 0);
3089
3090 /* If overlay has a non-empty after-string, record it. */
3091 if ((end == charpos || (start == charpos && invis_p))
3092 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3093 && XSTRING (str)->size)
3094 RECORD_OVERLAY_STRING (overlay, str, 1);
3095 }
3096
3097 #undef RECORD_OVERLAY_STRING
3098
3099 /* Sort entries. */
3100 if (n > 1)
3101 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3102
3103 /* Record the total number of strings to process. */
3104 it->n_overlay_strings = n;
3105
3106 /* IT->current.overlay_string_index is the number of overlay strings
3107 that have already been consumed by IT. Copy some of the
3108 remaining overlay strings to IT->overlay_strings. */
3109 i = 0;
3110 j = it->current.overlay_string_index;
3111 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3112 it->overlay_strings[i++] = entries[j++].string;
3113
3114 CHECK_IT (it);
3115 }
3116
3117
3118 /* Get the first chunk of overlay strings at IT's current buffer
3119 position. Value is non-zero if at least one overlay string was
3120 found. */
3121
3122 static int
3123 get_overlay_strings (it)
3124 struct it *it;
3125 {
3126 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3127 process. This fills IT->overlay_strings with strings, and sets
3128 IT->n_overlay_strings to the total number of strings to process.
3129 IT->pos.overlay_string_index has to be set temporarily to zero
3130 because load_overlay_strings needs this; it must be set to -1
3131 when no overlay strings are found because a zero value would
3132 indicate a position in the first overlay string. */
3133 it->current.overlay_string_index = 0;
3134 load_overlay_strings (it);
3135
3136 /* If we found overlay strings, set up IT to deliver display
3137 elements from the first one. Otherwise set up IT to deliver
3138 from current_buffer. */
3139 if (it->n_overlay_strings)
3140 {
3141 /* Make sure we know settings in current_buffer, so that we can
3142 restore meaningful values when we're done with the overlay
3143 strings. */
3144 compute_stop_pos (it);
3145 xassert (it->face_id >= 0);
3146
3147 /* Save IT's settings. They are restored after all overlay
3148 strings have been processed. */
3149 xassert (it->sp == 0);
3150 push_it (it);
3151
3152 /* Set up IT to deliver display elements from the first overlay
3153 string. */
3154 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3155 it->stop_charpos = 0;
3156 it->string = it->overlay_strings[0];
3157 it->multibyte_p = STRING_MULTIBYTE (it->string);
3158 xassert (STRINGP (it->string));
3159 it->method = next_element_from_string;
3160 }
3161 else
3162 {
3163 it->string = Qnil;
3164 it->current.overlay_string_index = -1;
3165 it->method = next_element_from_buffer;
3166 }
3167
3168 CHECK_IT (it);
3169
3170 /* Value is non-zero if we found at least one overlay string. */
3171 return STRINGP (it->string);
3172 }
3173
3174
3175 \f
3176 /***********************************************************************
3177 Saving and restoring state
3178 ***********************************************************************/
3179
3180 /* Save current settings of IT on IT->stack. Called, for example,
3181 before setting up IT for an overlay string, to be able to restore
3182 IT's settings to what they were after the overlay string has been
3183 processed. */
3184
3185 static void
3186 push_it (it)
3187 struct it *it;
3188 {
3189 struct iterator_stack_entry *p;
3190
3191 xassert (it->sp < 2);
3192 p = it->stack + it->sp;
3193
3194 p->stop_charpos = it->stop_charpos;
3195 xassert (it->face_id >= 0);
3196 p->face_id = it->face_id;
3197 p->string = it->string;
3198 p->pos = it->current;
3199 p->end_charpos = it->end_charpos;
3200 p->string_nchars = it->string_nchars;
3201 p->area = it->area;
3202 p->multibyte_p = it->multibyte_p;
3203 p->space_width = it->space_width;
3204 p->font_height = it->font_height;
3205 p->voffset = it->voffset;
3206 p->string_from_display_prop_p = it->string_from_display_prop_p;
3207 ++it->sp;
3208 }
3209
3210
3211 /* Restore IT's settings from IT->stack. Called, for example, when no
3212 more overlay strings must be processed, and we return to delivering
3213 display elements from a buffer, or when the end of a string from a
3214 `display' property is reached and we return to delivering display
3215 elements from an overlay string, or from a buffer. */
3216
3217 static void
3218 pop_it (it)
3219 struct it *it;
3220 {
3221 struct iterator_stack_entry *p;
3222
3223 xassert (it->sp > 0);
3224 --it->sp;
3225 p = it->stack + it->sp;
3226 it->stop_charpos = p->stop_charpos;
3227 it->face_id = p->face_id;
3228 it->string = p->string;
3229 it->current = p->pos;
3230 it->end_charpos = p->end_charpos;
3231 it->string_nchars = p->string_nchars;
3232 it->area = p->area;
3233 it->multibyte_p = p->multibyte_p;
3234 it->space_width = p->space_width;
3235 it->font_height = p->font_height;
3236 it->voffset = p->voffset;
3237 it->string_from_display_prop_p = p->string_from_display_prop_p;
3238 }
3239
3240
3241 \f
3242 /***********************************************************************
3243 Moving over lines
3244 ***********************************************************************/
3245
3246 /* Set IT's current position to the previous line start. */
3247
3248 static void
3249 back_to_previous_line_start (it)
3250 struct it *it;
3251 {
3252 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3253 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3254 }
3255
3256
3257 /* Move IT to the next line start.
3258
3259 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3260 we skipped over part of the text (as opposed to moving the iterator
3261 continuously over the text). Otherwise, don't change the value
3262 of *SKIPPED_P.
3263
3264 Newlines may come from buffer text, overlay strings, or strings
3265 displayed via the `display' property. That's the reason we can't
3266 simply use find_next_newline_no_quit. */
3267
3268 static int
3269 forward_to_next_line_start (it, skipped_p)
3270 struct it *it;
3271 int *skipped_p;
3272 {
3273 int old_selective, newline_found_p, n;
3274 const int MAX_NEWLINE_DISTANCE = 500;
3275
3276 /* Don't handle selective display in the following. It's (a)
3277 unnecessary and (b) leads to an infinite recursion because
3278 next_element_from_ellipsis indirectly calls this function. */
3279 old_selective = it->selective;
3280 it->selective = 0;
3281
3282 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3283 from buffer text. */
3284 n = newline_found_p = 0;
3285 while (n < MAX_NEWLINE_DISTANCE
3286 && get_next_display_element (it)
3287 && !newline_found_p)
3288 {
3289 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3290 set_iterator_to_next (it, 0);
3291 if (!STRINGP (it->string))
3292 ++n;
3293 }
3294
3295 /* If we didn't find a newline near enough, see if we can use a
3296 short-cut. */
3297 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3298 {
3299 int start = IT_CHARPOS (*it);
3300 int limit = find_next_newline_no_quit (start, 1);
3301 Lisp_Object pos;
3302
3303 xassert (!STRINGP (it->string));
3304
3305 /* If there isn't any `display' property in sight, and no
3306 overlays, we can just use the position of the newline in
3307 buffer text. */
3308 if (it->stop_charpos >= limit
3309 || ((pos = Fnext_single_property_change (make_number (start),
3310 Qdisplay,
3311 Qnil, make_number (limit)),
3312 NILP (pos))
3313 && next_overlay_change (start) == ZV))
3314 {
3315 IT_CHARPOS (*it) = limit;
3316 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3317 *skipped_p = newline_found_p = 1;
3318 }
3319 else
3320 {
3321 while (get_next_display_element (it)
3322 && !newline_found_p)
3323 {
3324 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3325 set_iterator_to_next (it, 0);
3326 }
3327 }
3328 }
3329
3330 it->selective = old_selective;
3331 return newline_found_p;
3332 }
3333
3334
3335 /* Set IT's current position to the previous visible line start. Skip
3336 invisible text that is so either due to text properties or due to
3337 selective display. Caution: this does not change IT->current_x and
3338 IT->hpos. */
3339
3340 static void
3341 back_to_previous_visible_line_start (it)
3342 struct it *it;
3343 {
3344 int visible_p = 0;
3345
3346 /* Go back one newline if not on BEGV already. */
3347 if (IT_CHARPOS (*it) > BEGV)
3348 back_to_previous_line_start (it);
3349
3350 /* Move over lines that are invisible because of selective display
3351 or text properties. */
3352 while (IT_CHARPOS (*it) > BEGV
3353 && !visible_p)
3354 {
3355 visible_p = 1;
3356
3357 /* If selective > 0, then lines indented more than that values
3358 are invisible. */
3359 if (it->selective > 0
3360 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3361 it->selective))
3362 visible_p = 0;
3363 else
3364 {
3365 Lisp_Object prop;
3366
3367 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3368 Qinvisible, it->window);
3369 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3370 visible_p = 0;
3371 }
3372
3373 /* Back one more newline if the current one is invisible. */
3374 if (!visible_p)
3375 back_to_previous_line_start (it);
3376 }
3377
3378 xassert (IT_CHARPOS (*it) >= BEGV);
3379 xassert (IT_CHARPOS (*it) == BEGV
3380 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3381 CHECK_IT (it);
3382 }
3383
3384
3385 /* Reseat iterator IT at the previous visible line start. Skip
3386 invisible text that is so either due to text properties or due to
3387 selective display. At the end, update IT's overlay information,
3388 face information etc. */
3389
3390 static void
3391 reseat_at_previous_visible_line_start (it)
3392 struct it *it;
3393 {
3394 back_to_previous_visible_line_start (it);
3395 reseat (it, it->current.pos, 1);
3396 CHECK_IT (it);
3397 }
3398
3399
3400 /* Reseat iterator IT on the next visible line start in the current
3401 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3402 preceding the line start. Skip over invisible text that is so
3403 because of selective display. Compute faces, overlays etc at the
3404 new position. Note that this function does not skip over text that
3405 is invisible because of text properties. */
3406
3407 static void
3408 reseat_at_next_visible_line_start (it, on_newline_p)
3409 struct it *it;
3410 int on_newline_p;
3411 {
3412 int newline_found_p, skipped_p = 0;
3413
3414 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3415
3416 /* Skip over lines that are invisible because they are indented
3417 more than the value of IT->selective. */
3418 if (it->selective > 0)
3419 while (IT_CHARPOS (*it) < ZV
3420 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3421 it->selective))
3422 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3423
3424 /* Position on the newline if that's what's requested. */
3425 if (on_newline_p && newline_found_p)
3426 {
3427 if (STRINGP (it->string))
3428 {
3429 if (IT_STRING_CHARPOS (*it) > 0)
3430 {
3431 --IT_STRING_CHARPOS (*it);
3432 --IT_STRING_BYTEPOS (*it);
3433 }
3434 }
3435 else if (IT_CHARPOS (*it) > BEGV)
3436 {
3437 --IT_CHARPOS (*it);
3438 --IT_BYTEPOS (*it);
3439 reseat (it, it->current.pos, 0);
3440 }
3441 }
3442 else if (skipped_p)
3443 reseat (it, it->current.pos, 0);
3444
3445 CHECK_IT (it);
3446 }
3447
3448
3449 \f
3450 /***********************************************************************
3451 Changing an iterator's position
3452 ***********************************************************************/
3453
3454 /* Change IT's current position to POS in current_buffer. If FORCE_P
3455 is non-zero, always check for text properties at the new position.
3456 Otherwise, text properties are only looked up if POS >=
3457 IT->check_charpos of a property. */
3458
3459 static void
3460 reseat (it, pos, force_p)
3461 struct it *it;
3462 struct text_pos pos;
3463 int force_p;
3464 {
3465 int original_pos = IT_CHARPOS (*it);
3466
3467 reseat_1 (it, pos, 0);
3468
3469 /* Determine where to check text properties. Avoid doing it
3470 where possible because text property lookup is very expensive. */
3471 if (force_p
3472 || CHARPOS (pos) > it->stop_charpos
3473 || CHARPOS (pos) < original_pos)
3474 handle_stop (it);
3475
3476 CHECK_IT (it);
3477 }
3478
3479
3480 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3481 IT->stop_pos to POS, also. */
3482
3483 static void
3484 reseat_1 (it, pos, set_stop_p)
3485 struct it *it;
3486 struct text_pos pos;
3487 int set_stop_p;
3488 {
3489 /* Don't call this function when scanning a C string. */
3490 xassert (it->s == NULL);
3491
3492 /* POS must be a reasonable value. */
3493 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3494
3495 it->current.pos = it->position = pos;
3496 XSETBUFFER (it->object, current_buffer);
3497 it->dpvec = NULL;
3498 it->current.dpvec_index = -1;
3499 it->current.overlay_string_index = -1;
3500 IT_STRING_CHARPOS (*it) = -1;
3501 IT_STRING_BYTEPOS (*it) = -1;
3502 it->string = Qnil;
3503 it->method = next_element_from_buffer;
3504 it->sp = 0;
3505
3506 if (set_stop_p)
3507 it->stop_charpos = CHARPOS (pos);
3508 }
3509
3510
3511 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3512 If S is non-null, it is a C string to iterate over. Otherwise,
3513 STRING gives a Lisp string to iterate over.
3514
3515 If PRECISION > 0, don't return more then PRECISION number of
3516 characters from the string.
3517
3518 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3519 characters have been returned. FIELD_WIDTH < 0 means an infinite
3520 field width.
3521
3522 MULTIBYTE = 0 means disable processing of multibyte characters,
3523 MULTIBYTE > 0 means enable it,
3524 MULTIBYTE < 0 means use IT->multibyte_p.
3525
3526 IT must be initialized via a prior call to init_iterator before
3527 calling this function. */
3528
3529 static void
3530 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3531 struct it *it;
3532 unsigned char *s;
3533 Lisp_Object string;
3534 int charpos;
3535 int precision, field_width, multibyte;
3536 {
3537 /* No region in strings. */
3538 it->region_beg_charpos = it->region_end_charpos = -1;
3539
3540 /* No text property checks performed by default, but see below. */
3541 it->stop_charpos = -1;
3542
3543 /* Set iterator position and end position. */
3544 bzero (&it->current, sizeof it->current);
3545 it->current.overlay_string_index = -1;
3546 it->current.dpvec_index = -1;
3547 xassert (charpos >= 0);
3548
3549 /* Use the setting of MULTIBYTE if specified. */
3550 if (multibyte >= 0)
3551 it->multibyte_p = multibyte > 0;
3552
3553 if (s == NULL)
3554 {
3555 xassert (STRINGP (string));
3556 it->string = string;
3557 it->s = NULL;
3558 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3559 it->method = next_element_from_string;
3560 it->current.string_pos = string_pos (charpos, string);
3561 }
3562 else
3563 {
3564 it->s = s;
3565 it->string = Qnil;
3566
3567 /* Note that we use IT->current.pos, not it->current.string_pos,
3568 for displaying C strings. */
3569 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3570 if (it->multibyte_p)
3571 {
3572 it->current.pos = c_string_pos (charpos, s, 1);
3573 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3574 }
3575 else
3576 {
3577 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3578 it->end_charpos = it->string_nchars = strlen (s);
3579 }
3580
3581 it->method = next_element_from_c_string;
3582 }
3583
3584 /* PRECISION > 0 means don't return more than PRECISION characters
3585 from the string. */
3586 if (precision > 0 && it->end_charpos - charpos > precision)
3587 it->end_charpos = it->string_nchars = charpos + precision;
3588
3589 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3590 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3591 FIELD_WIDTH < 0 means infinite field width. This is useful for
3592 padding with `-' at the end of a mode line. */
3593 if (field_width < 0)
3594 field_width = INFINITY;
3595 if (field_width > it->end_charpos - charpos)
3596 it->end_charpos = charpos + field_width;
3597
3598 /* Use the standard display table for displaying strings. */
3599 if (DISP_TABLE_P (Vstandard_display_table))
3600 it->dp = XCHAR_TABLE (Vstandard_display_table);
3601
3602 it->stop_charpos = charpos;
3603 CHECK_IT (it);
3604 }
3605
3606
3607 \f
3608 /***********************************************************************
3609 Iteration
3610 ***********************************************************************/
3611
3612 /* Load IT's display element fields with information about the next
3613 display element from the current position of IT. Value is zero if
3614 end of buffer (or C string) is reached. */
3615
3616 int
3617 get_next_display_element (it)
3618 struct it *it;
3619 {
3620 /* Non-zero means that we found an display element. Zero means that
3621 we hit the end of what we iterate over. Performance note: the
3622 function pointer `method' used here turns out to be faster than
3623 using a sequence of if-statements. */
3624 int success_p = (*it->method) (it);
3625
3626 if (it->what == IT_CHARACTER)
3627 {
3628 /* Map via display table or translate control characters.
3629 IT->c, IT->len etc. have been set to the next character by
3630 the function call above. If we have a display table, and it
3631 contains an entry for IT->c, translate it. Don't do this if
3632 IT->c itself comes from a display table, otherwise we could
3633 end up in an infinite recursion. (An alternative could be to
3634 count the recursion depth of this function and signal an
3635 error when a certain maximum depth is reached.) Is it worth
3636 it? */
3637 if (success_p && it->dpvec == NULL)
3638 {
3639 Lisp_Object dv;
3640
3641 if (it->dp
3642 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3643 VECTORP (dv)))
3644 {
3645 struct Lisp_Vector *v = XVECTOR (dv);
3646
3647 /* Return the first character from the display table
3648 entry, if not empty. If empty, don't display the
3649 current character. */
3650 if (v->size)
3651 {
3652 it->dpvec_char_len = it->len;
3653 it->dpvec = v->contents;
3654 it->dpend = v->contents + v->size;
3655 it->current.dpvec_index = 0;
3656 it->method = next_element_from_display_vector;
3657 }
3658
3659 success_p = get_next_display_element (it);
3660 }
3661
3662 /* Translate control characters into `\003' or `^C' form.
3663 Control characters coming from a display table entry are
3664 currently not translated because we use IT->dpvec to hold
3665 the translation. This could easily be changed but I
3666 don't believe that it is worth doing.
3667
3668 Non-printable multibyte characters are also translated
3669 octal form. */
3670 else if ((it->c < ' '
3671 && (it->area != TEXT_AREA
3672 || (it->c != '\n' && it->c != '\t')))
3673 || (it->c >= 127
3674 && it->len == 1)
3675 || !CHAR_PRINTABLE_P (it->c))
3676 {
3677 /* IT->c is a control character which must be displayed
3678 either as '\003' or as `^C' where the '\\' and '^'
3679 can be defined in the display table. Fill
3680 IT->ctl_chars with glyphs for what we have to
3681 display. Then, set IT->dpvec to these glyphs. */
3682 GLYPH g;
3683
3684 if (it->c < 128 && it->ctl_arrow_p)
3685 {
3686 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3687 if (it->dp
3688 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3689 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3690 g = XINT (DISP_CTRL_GLYPH (it->dp));
3691 else
3692 g = FAST_MAKE_GLYPH ('^', 0);
3693 XSETINT (it->ctl_chars[0], g);
3694
3695 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3696 XSETINT (it->ctl_chars[1], g);
3697
3698 /* Set up IT->dpvec and return first character from it. */
3699 it->dpvec_char_len = it->len;
3700 it->dpvec = it->ctl_chars;
3701 it->dpend = it->dpvec + 2;
3702 it->current.dpvec_index = 0;
3703 it->method = next_element_from_display_vector;
3704 get_next_display_element (it);
3705 }
3706 else
3707 {
3708 unsigned char str[MAX_MULTIBYTE_LENGTH];
3709 int len;
3710 int i;
3711 GLYPH escape_glyph;
3712
3713 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3714 if (it->dp
3715 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3716 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3717 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3718 else
3719 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3720
3721 if (SINGLE_BYTE_CHAR_P (it->c))
3722 str[0] = it->c, len = 1;
3723 else
3724 len = CHAR_STRING (it->c, str);
3725
3726 for (i = 0; i < len; i++)
3727 {
3728 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3729 /* Insert three more glyphs into IT->ctl_chars for
3730 the octal display of the character. */
3731 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3732 XSETINT (it->ctl_chars[i * 4 + 1], g);
3733 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3734 XSETINT (it->ctl_chars[i * 4 + 2], g);
3735 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3736 XSETINT (it->ctl_chars[i * 4 + 3], g);
3737 }
3738
3739 /* Set up IT->dpvec and return the first character
3740 from it. */
3741 it->dpvec_char_len = it->len;
3742 it->dpvec = it->ctl_chars;
3743 it->dpend = it->dpvec + len * 4;
3744 it->current.dpvec_index = 0;
3745 it->method = next_element_from_display_vector;
3746 get_next_display_element (it);
3747 }
3748 }
3749 }
3750
3751 /* Adjust face id for a multibyte character. There are no
3752 multibyte character in unibyte text. */
3753 if (it->multibyte_p
3754 && success_p
3755 && FRAME_WINDOW_P (it->f))
3756 {
3757 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3758 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3759 }
3760 }
3761
3762 /* Is this character the last one of a run of characters with
3763 box? If yes, set IT->end_of_box_run_p to 1. */
3764 if (it->face_box_p
3765 && it->s == NULL)
3766 {
3767 int face_id;
3768 struct face *face;
3769
3770 it->end_of_box_run_p
3771 = ((face_id = face_after_it_pos (it),
3772 face_id != it->face_id)
3773 && (face = FACE_FROM_ID (it->f, face_id),
3774 face->box == FACE_NO_BOX));
3775 }
3776
3777 /* Value is 0 if end of buffer or string reached. */
3778 return success_p;
3779 }
3780
3781
3782 /* Move IT to the next display element.
3783
3784 RESEAT_P non-zero means if called on a newline in buffer text,
3785 skip to the next visible line start.
3786
3787 Functions get_next_display_element and set_iterator_to_next are
3788 separate because I find this arrangement easier to handle than a
3789 get_next_display_element function that also increments IT's
3790 position. The way it is we can first look at an iterator's current
3791 display element, decide whether it fits on a line, and if it does,
3792 increment the iterator position. The other way around we probably
3793 would either need a flag indicating whether the iterator has to be
3794 incremented the next time, or we would have to implement a
3795 decrement position function which would not be easy to write. */
3796
3797 void
3798 set_iterator_to_next (it, reseat_p)
3799 struct it *it;
3800 int reseat_p;
3801 {
3802 /* Reset flags indicating start and end of a sequence of characters
3803 with box. Reset them at the start of this function because
3804 moving the iterator to a new position might set them. */
3805 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3806
3807 if (it->method == next_element_from_buffer)
3808 {
3809 /* The current display element of IT is a character from
3810 current_buffer. Advance in the buffer, and maybe skip over
3811 invisible lines that are so because of selective display. */
3812 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3813 reseat_at_next_visible_line_start (it, 0);
3814 else
3815 {
3816 xassert (it->len != 0);
3817 IT_BYTEPOS (*it) += it->len;
3818 IT_CHARPOS (*it) += 1;
3819 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
3820 }
3821 }
3822 else if (it->method == next_element_from_composition)
3823 {
3824 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
3825 if (STRINGP (it->string))
3826 {
3827 IT_STRING_BYTEPOS (*it) += it->len;
3828 IT_STRING_CHARPOS (*it) += it->cmp_len;
3829 it->method = next_element_from_string;
3830 goto consider_string_end;
3831 }
3832 else
3833 {
3834 IT_BYTEPOS (*it) += it->len;
3835 IT_CHARPOS (*it) += it->cmp_len;
3836 it->method = next_element_from_buffer;
3837 }
3838 }
3839 else if (it->method == next_element_from_c_string)
3840 {
3841 /* Current display element of IT is from a C string. */
3842 IT_BYTEPOS (*it) += it->len;
3843 IT_CHARPOS (*it) += 1;
3844 }
3845 else if (it->method == next_element_from_display_vector)
3846 {
3847 /* Current display element of IT is from a display table entry.
3848 Advance in the display table definition. Reset it to null if
3849 end reached, and continue with characters from buffers/
3850 strings. */
3851 ++it->current.dpvec_index;
3852
3853 /* Restore face of the iterator to what they were before the
3854 display vector entry (these entries may contain faces). */
3855 it->face_id = it->saved_face_id;
3856
3857 if (it->dpvec + it->current.dpvec_index == it->dpend)
3858 {
3859 if (it->s)
3860 it->method = next_element_from_c_string;
3861 else if (STRINGP (it->string))
3862 it->method = next_element_from_string;
3863 else
3864 it->method = next_element_from_buffer;
3865
3866 it->dpvec = NULL;
3867 it->current.dpvec_index = -1;
3868
3869 /* Skip over characters which were displayed via IT->dpvec. */
3870 if (it->dpvec_char_len < 0)
3871 reseat_at_next_visible_line_start (it, 1);
3872 else if (it->dpvec_char_len > 0)
3873 {
3874 it->len = it->dpvec_char_len;
3875 set_iterator_to_next (it, reseat_p);
3876 }
3877 }
3878 }
3879 else if (it->method == next_element_from_string)
3880 {
3881 /* Current display element is a character from a Lisp string. */
3882 xassert (it->s == NULL && STRINGP (it->string));
3883 IT_STRING_BYTEPOS (*it) += it->len;
3884 IT_STRING_CHARPOS (*it) += 1;
3885
3886 consider_string_end:
3887
3888 if (it->current.overlay_string_index >= 0)
3889 {
3890 /* IT->string is an overlay string. Advance to the
3891 next, if there is one. */
3892 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
3893 next_overlay_string (it);
3894 }
3895 else
3896 {
3897 /* IT->string is not an overlay string. If we reached
3898 its end, and there is something on IT->stack, proceed
3899 with what is on the stack. This can be either another
3900 string, this time an overlay string, or a buffer. */
3901 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
3902 && it->sp > 0)
3903 {
3904 pop_it (it);
3905 if (!STRINGP (it->string))
3906 it->method = next_element_from_buffer;
3907 }
3908 }
3909 }
3910 else if (it->method == next_element_from_image
3911 || it->method == next_element_from_stretch)
3912 {
3913 /* The position etc with which we have to proceed are on
3914 the stack. The position may be at the end of a string,
3915 if the `display' property takes up the whole string. */
3916 pop_it (it);
3917 it->image_id = 0;
3918 if (STRINGP (it->string))
3919 {
3920 it->method = next_element_from_string;
3921 goto consider_string_end;
3922 }
3923 else
3924 it->method = next_element_from_buffer;
3925 }
3926 else
3927 /* There are no other methods defined, so this should be a bug. */
3928 abort ();
3929
3930 xassert (it->method != next_element_from_string
3931 || (STRINGP (it->string)
3932 && IT_STRING_CHARPOS (*it) >= 0));
3933 }
3934
3935
3936 /* Load IT's display element fields with information about the next
3937 display element which comes from a display table entry or from the
3938 result of translating a control character to one of the forms `^C'
3939 or `\003'. IT->dpvec holds the glyphs to return as characters. */
3940
3941 static int
3942 next_element_from_display_vector (it)
3943 struct it *it;
3944 {
3945 /* Precondition. */
3946 xassert (it->dpvec && it->current.dpvec_index >= 0);
3947
3948 /* Remember the current face id in case glyphs specify faces.
3949 IT's face is restored in set_iterator_to_next. */
3950 it->saved_face_id = it->face_id;
3951
3952 if (INTEGERP (*it->dpvec)
3953 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
3954 {
3955 int lface_id;
3956 GLYPH g;
3957
3958 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
3959 it->c = FAST_GLYPH_CHAR (g);
3960 it->len = CHAR_BYTES (it->c);
3961
3962 /* The entry may contain a face id to use. Such a face id is
3963 the id of a Lisp face, not a realized face. A face id of
3964 zero means no face is specified. */
3965 lface_id = FAST_GLYPH_FACE (g);
3966 if (lface_id)
3967 {
3968 /* The function returns -1 if lface_id is invalid. */
3969 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
3970 if (face_id >= 0)
3971 it->face_id = face_id;
3972 }
3973 }
3974 else
3975 /* Display table entry is invalid. Return a space. */
3976 it->c = ' ', it->len = 1;
3977
3978 /* Don't change position and object of the iterator here. They are
3979 still the values of the character that had this display table
3980 entry or was translated, and that's what we want. */
3981 it->what = IT_CHARACTER;
3982 return 1;
3983 }
3984
3985
3986 /* Load IT with the next display element from Lisp string IT->string.
3987 IT->current.string_pos is the current position within the string.
3988 If IT->current.overlay_string_index >= 0, the Lisp string is an
3989 overlay string. */
3990
3991 static int
3992 next_element_from_string (it)
3993 struct it *it;
3994 {
3995 struct text_pos position;
3996
3997 xassert (STRINGP (it->string));
3998 xassert (IT_STRING_CHARPOS (*it) >= 0);
3999 position = it->current.string_pos;
4000
4001 /* Time to check for invisible text? */
4002 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4003 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4004 {
4005 handle_stop (it);
4006
4007 /* Since a handler may have changed IT->method, we must
4008 recurse here. */
4009 return get_next_display_element (it);
4010 }
4011
4012 if (it->current.overlay_string_index >= 0)
4013 {
4014 /* Get the next character from an overlay string. In overlay
4015 strings, There is no field width or padding with spaces to
4016 do. */
4017 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4018 {
4019 it->what = IT_EOB;
4020 return 0;
4021 }
4022 else if (STRING_MULTIBYTE (it->string))
4023 {
4024 int remaining = (STRING_BYTES (XSTRING (it->string))
4025 - IT_STRING_BYTEPOS (*it));
4026 unsigned char *s = (XSTRING (it->string)->data
4027 + IT_STRING_BYTEPOS (*it));
4028 it->c = string_char_and_length (s, remaining, &it->len);
4029 }
4030 else
4031 {
4032 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4033 it->len = 1;
4034 }
4035 }
4036 else
4037 {
4038 /* Get the next character from a Lisp string that is not an
4039 overlay string. Such strings come from the mode line, for
4040 example. We may have to pad with spaces, or truncate the
4041 string. See also next_element_from_c_string. */
4042 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4043 {
4044 it->what = IT_EOB;
4045 return 0;
4046 }
4047 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4048 {
4049 /* Pad with spaces. */
4050 it->c = ' ', it->len = 1;
4051 CHARPOS (position) = BYTEPOS (position) = -1;
4052 }
4053 else if (STRING_MULTIBYTE (it->string))
4054 {
4055 int maxlen = (STRING_BYTES (XSTRING (it->string))
4056 - IT_STRING_BYTEPOS (*it));
4057 unsigned char *s = (XSTRING (it->string)->data
4058 + IT_STRING_BYTEPOS (*it));
4059 it->c = string_char_and_length (s, maxlen, &it->len);
4060 }
4061 else
4062 {
4063 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4064 it->len = 1;
4065 }
4066 }
4067
4068 /* Record what we have and where it came from. Note that we store a
4069 buffer position in IT->position although it could arguably be a
4070 string position. */
4071 it->what = IT_CHARACTER;
4072 it->object = it->string;
4073 it->position = position;
4074 return 1;
4075 }
4076
4077
4078 /* Load IT with next display element from C string IT->s.
4079 IT->string_nchars is the maximum number of characters to return
4080 from the string. IT->end_charpos may be greater than
4081 IT->string_nchars when this function is called, in which case we
4082 may have to return padding spaces. Value is zero if end of string
4083 reached, including padding spaces. */
4084
4085 static int
4086 next_element_from_c_string (it)
4087 struct it *it;
4088 {
4089 int success_p = 1;
4090
4091 xassert (it->s);
4092 it->what = IT_CHARACTER;
4093 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4094 it->object = Qnil;
4095
4096 /* IT's position can be greater IT->string_nchars in case a field
4097 width or precision has been specified when the iterator was
4098 initialized. */
4099 if (IT_CHARPOS (*it) >= it->end_charpos)
4100 {
4101 /* End of the game. */
4102 it->what = IT_EOB;
4103 success_p = 0;
4104 }
4105 else if (IT_CHARPOS (*it) >= it->string_nchars)
4106 {
4107 /* Pad with spaces. */
4108 it->c = ' ', it->len = 1;
4109 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4110 }
4111 else if (it->multibyte_p)
4112 {
4113 /* Implementation note: The calls to strlen apparently aren't a
4114 performance problem because there is no noticeable performance
4115 difference between Emacs running in unibyte or multibyte mode. */
4116 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4117 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4118 maxlen, &it->len);
4119 }
4120 else
4121 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4122
4123 return success_p;
4124 }
4125
4126
4127 /* Set up IT to return characters from an ellipsis, if appropriate.
4128 The definition of the ellipsis glyphs may come from a display table
4129 entry. This function Fills IT with the first glyph from the
4130 ellipsis if an ellipsis is to be displayed. */
4131
4132 static int
4133 next_element_from_ellipsis (it)
4134 struct it *it;
4135 {
4136 if (it->selective_display_ellipsis_p)
4137 {
4138 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4139 {
4140 /* Use the display table definition for `...'. Invalid glyphs
4141 will be handled by the method returning elements from dpvec. */
4142 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4143 it->dpvec_char_len = it->len;
4144 it->dpvec = v->contents;
4145 it->dpend = v->contents + v->size;
4146 it->current.dpvec_index = 0;
4147 it->method = next_element_from_display_vector;
4148 }
4149 else
4150 {
4151 /* Use default `...' which is stored in default_invis_vector. */
4152 it->dpvec_char_len = it->len;
4153 it->dpvec = default_invis_vector;
4154 it->dpend = default_invis_vector + 3;
4155 it->current.dpvec_index = 0;
4156 it->method = next_element_from_display_vector;
4157 }
4158 }
4159 else
4160 {
4161 it->method = next_element_from_buffer;
4162 reseat_at_next_visible_line_start (it, 1);
4163 }
4164
4165 return get_next_display_element (it);
4166 }
4167
4168
4169 /* Deliver an image display element. The iterator IT is already
4170 filled with image information (done in handle_display_prop). Value
4171 is always 1. */
4172
4173
4174 static int
4175 next_element_from_image (it)
4176 struct it *it;
4177 {
4178 it->what = IT_IMAGE;
4179 return 1;
4180 }
4181
4182
4183 /* Fill iterator IT with next display element from a stretch glyph
4184 property. IT->object is the value of the text property. Value is
4185 always 1. */
4186
4187 static int
4188 next_element_from_stretch (it)
4189 struct it *it;
4190 {
4191 it->what = IT_STRETCH;
4192 return 1;
4193 }
4194
4195
4196 /* Load IT with the next display element from current_buffer. Value
4197 is zero if end of buffer reached. IT->stop_charpos is the next
4198 position at which to stop and check for text properties or buffer
4199 end. */
4200
4201 static int
4202 next_element_from_buffer (it)
4203 struct it *it;
4204 {
4205 int success_p = 1;
4206
4207 /* Check this assumption, otherwise, we would never enter the
4208 if-statement, below. */
4209 xassert (IT_CHARPOS (*it) >= BEGV
4210 && IT_CHARPOS (*it) <= it->stop_charpos);
4211
4212 if (IT_CHARPOS (*it) >= it->stop_charpos)
4213 {
4214 if (IT_CHARPOS (*it) >= it->end_charpos)
4215 {
4216 int overlay_strings_follow_p;
4217
4218 /* End of the game, except when overlay strings follow that
4219 haven't been returned yet. */
4220 if (it->overlay_strings_at_end_processed_p)
4221 overlay_strings_follow_p = 0;
4222 else
4223 {
4224 it->overlay_strings_at_end_processed_p = 1;
4225 overlay_strings_follow_p = get_overlay_strings (it);
4226 }
4227
4228 if (overlay_strings_follow_p)
4229 success_p = get_next_display_element (it);
4230 else
4231 {
4232 it->what = IT_EOB;
4233 it->position = it->current.pos;
4234 success_p = 0;
4235 }
4236 }
4237 else
4238 {
4239 handle_stop (it);
4240 return get_next_display_element (it);
4241 }
4242 }
4243 else
4244 {
4245 /* No face changes, overlays etc. in sight, so just return a
4246 character from current_buffer. */
4247 unsigned char *p;
4248
4249 /* Maybe run the redisplay end trigger hook. Performance note:
4250 This doesn't seem to cost measurable time. */
4251 if (it->redisplay_end_trigger_charpos
4252 && it->glyph_row
4253 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4254 run_redisplay_end_trigger_hook (it);
4255
4256 /* Get the next character, maybe multibyte. */
4257 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4258 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4259 {
4260 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4261 - IT_BYTEPOS (*it));
4262 it->c = string_char_and_length (p, maxlen, &it->len);
4263 }
4264 else
4265 it->c = *p, it->len = 1;
4266
4267 /* Record what we have and where it came from. */
4268 it->what = IT_CHARACTER;;
4269 it->object = it->w->buffer;
4270 it->position = it->current.pos;
4271
4272 /* Normally we return the character found above, except when we
4273 really want to return an ellipsis for selective display. */
4274 if (it->selective)
4275 {
4276 if (it->c == '\n')
4277 {
4278 /* A value of selective > 0 means hide lines indented more
4279 than that number of columns. */
4280 if (it->selective > 0
4281 && IT_CHARPOS (*it) + 1 < ZV
4282 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4283 IT_BYTEPOS (*it) + 1,
4284 it->selective))
4285 {
4286 success_p = next_element_from_ellipsis (it);
4287 it->dpvec_char_len = -1;
4288 }
4289 }
4290 else if (it->c == '\r' && it->selective == -1)
4291 {
4292 /* A value of selective == -1 means that everything from the
4293 CR to the end of the line is invisible, with maybe an
4294 ellipsis displayed for it. */
4295 success_p = next_element_from_ellipsis (it);
4296 it->dpvec_char_len = -1;
4297 }
4298 }
4299 }
4300
4301 /* Value is zero if end of buffer reached. */
4302 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4303 return success_p;
4304 }
4305
4306
4307 /* Run the redisplay end trigger hook for IT. */
4308
4309 static void
4310 run_redisplay_end_trigger_hook (it)
4311 struct it *it;
4312 {
4313 Lisp_Object args[3];
4314
4315 /* IT->glyph_row should be non-null, i.e. we should be actually
4316 displaying something, or otherwise we should not run the hook. */
4317 xassert (it->glyph_row);
4318
4319 /* Set up hook arguments. */
4320 args[0] = Qredisplay_end_trigger_functions;
4321 args[1] = it->window;
4322 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4323 it->redisplay_end_trigger_charpos = 0;
4324
4325 /* Since we are *trying* to run these functions, don't try to run
4326 them again, even if they get an error. */
4327 it->w->redisplay_end_trigger = Qnil;
4328 Frun_hook_with_args (3, args);
4329
4330 /* Notice if it changed the face of the character we are on. */
4331 handle_face_prop (it);
4332 }
4333
4334
4335 /* Deliver a composition display element. The iterator IT is already
4336 filled with composition information (done in
4337 handle_composition_prop). Value is always 1. */
4338
4339 static int
4340 next_element_from_composition (it)
4341 struct it *it;
4342 {
4343 it->what = IT_COMPOSITION;
4344 it->position = (STRINGP (it->string)
4345 ? it->current.string_pos
4346 : it->current.pos);
4347 return 1;
4348 }
4349
4350
4351 \f
4352 /***********************************************************************
4353 Moving an iterator without producing glyphs
4354 ***********************************************************************/
4355
4356 /* Move iterator IT to a specified buffer or X position within one
4357 line on the display without producing glyphs.
4358
4359 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4360 whichever is reached first.
4361
4362 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4363
4364 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4365 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4366 TO_X includes the amount by which a window is horizontally
4367 scrolled.
4368
4369 Value is
4370
4371 MOVE_POS_MATCH_OR_ZV
4372 - when TO_POS or ZV was reached.
4373
4374 MOVE_X_REACHED
4375 -when TO_X was reached before TO_POS or ZV were reached.
4376
4377 MOVE_LINE_CONTINUED
4378 - when we reached the end of the display area and the line must
4379 be continued.
4380
4381 MOVE_LINE_TRUNCATED
4382 - when we reached the end of the display area and the line is
4383 truncated.
4384
4385 MOVE_NEWLINE_OR_CR
4386 - when we stopped at a line end, i.e. a newline or a CR and selective
4387 display is on. */
4388
4389 static enum move_it_result
4390 move_it_in_display_line_to (it, to_charpos, to_x, op)
4391 struct it *it;
4392 int to_charpos, to_x, op;
4393 {
4394 enum move_it_result result = MOVE_UNDEFINED;
4395 struct glyph_row *saved_glyph_row;
4396
4397 /* Don't produce glyphs in produce_glyphs. */
4398 saved_glyph_row = it->glyph_row;
4399 it->glyph_row = NULL;
4400
4401 while (1)
4402 {
4403 int x, i, ascent = 0, descent = 0;
4404
4405 /* Stop when ZV or TO_CHARPOS reached. */
4406 if (!get_next_display_element (it)
4407 || ((op & MOVE_TO_POS) != 0
4408 && BUFFERP (it->object)
4409 && IT_CHARPOS (*it) >= to_charpos))
4410 {
4411 result = MOVE_POS_MATCH_OR_ZV;
4412 break;
4413 }
4414
4415 /* The call to produce_glyphs will get the metrics of the
4416 display element IT is loaded with. We record in x the
4417 x-position before this display element in case it does not
4418 fit on the line. */
4419 x = it->current_x;
4420
4421 /* Remember the line height so far in case the next element doesn't
4422 fit on the line. */
4423 if (!it->truncate_lines_p)
4424 {
4425 ascent = it->max_ascent;
4426 descent = it->max_descent;
4427 }
4428
4429 PRODUCE_GLYPHS (it);
4430
4431 if (it->area != TEXT_AREA)
4432 {
4433 set_iterator_to_next (it, 1);
4434 continue;
4435 }
4436
4437 /* The number of glyphs we get back in IT->nglyphs will normally
4438 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4439 character on a terminal frame, or (iii) a line end. For the
4440 second case, IT->nglyphs - 1 padding glyphs will be present
4441 (on X frames, there is only one glyph produced for a
4442 composite character.
4443
4444 The behavior implemented below means, for continuation lines,
4445 that as many spaces of a TAB as fit on the current line are
4446 displayed there. For terminal frames, as many glyphs of a
4447 multi-glyph character are displayed in the current line, too.
4448 This is what the old redisplay code did, and we keep it that
4449 way. Under X, the whole shape of a complex character must
4450 fit on the line or it will be completely displayed in the
4451 next line.
4452
4453 Note that both for tabs and padding glyphs, all glyphs have
4454 the same width. */
4455 if (it->nglyphs)
4456 {
4457 /* More than one glyph or glyph doesn't fit on line. All
4458 glyphs have the same width. */
4459 int single_glyph_width = it->pixel_width / it->nglyphs;
4460 int new_x;
4461
4462 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4463 {
4464 new_x = x + single_glyph_width;
4465
4466 /* We want to leave anything reaching TO_X to the caller. */
4467 if ((op & MOVE_TO_X) && new_x > to_x)
4468 {
4469 it->current_x = x;
4470 result = MOVE_X_REACHED;
4471 break;
4472 }
4473 else if (/* Lines are continued. */
4474 !it->truncate_lines_p
4475 && (/* And glyph doesn't fit on the line. */
4476 new_x > it->last_visible_x
4477 /* Or it fits exactly and we're on a window
4478 system frame. */
4479 || (new_x == it->last_visible_x
4480 && FRAME_WINDOW_P (it->f))))
4481 {
4482 if (/* IT->hpos == 0 means the very first glyph
4483 doesn't fit on the line, e.g. a wide image. */
4484 it->hpos == 0
4485 || (new_x == it->last_visible_x
4486 && FRAME_WINDOW_P (it->f)))
4487 {
4488 ++it->hpos;
4489 it->current_x = new_x;
4490 if (i == it->nglyphs - 1)
4491 set_iterator_to_next (it, 1);
4492 }
4493 else
4494 {
4495 it->current_x = x;
4496 it->max_ascent = ascent;
4497 it->max_descent = descent;
4498 }
4499
4500 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4501 IT_CHARPOS (*it)));
4502 result = MOVE_LINE_CONTINUED;
4503 break;
4504 }
4505 else if (new_x > it->first_visible_x)
4506 {
4507 /* Glyph is visible. Increment number of glyphs that
4508 would be displayed. */
4509 ++it->hpos;
4510 }
4511 else
4512 {
4513 /* Glyph is completely off the left margin of the display
4514 area. Nothing to do. */
4515 }
4516 }
4517
4518 if (result != MOVE_UNDEFINED)
4519 break;
4520 }
4521 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4522 {
4523 /* Stop when TO_X specified and reached. This check is
4524 necessary here because of lines consisting of a line end,
4525 only. The line end will not produce any glyphs and we
4526 would never get MOVE_X_REACHED. */
4527 xassert (it->nglyphs == 0);
4528 result = MOVE_X_REACHED;
4529 break;
4530 }
4531
4532 /* Is this a line end? If yes, we're done. */
4533 if (ITERATOR_AT_END_OF_LINE_P (it))
4534 {
4535 result = MOVE_NEWLINE_OR_CR;
4536 break;
4537 }
4538
4539 /* The current display element has been consumed. Advance
4540 to the next. */
4541 set_iterator_to_next (it, 1);
4542
4543 /* Stop if lines are truncated and IT's current x-position is
4544 past the right edge of the window now. */
4545 if (it->truncate_lines_p
4546 && it->current_x >= it->last_visible_x)
4547 {
4548 result = MOVE_LINE_TRUNCATED;
4549 break;
4550 }
4551 }
4552
4553 /* Restore the iterator settings altered at the beginning of this
4554 function. */
4555 it->glyph_row = saved_glyph_row;
4556 return result;
4557 }
4558
4559
4560 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4561 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4562 the description of enum move_operation_enum.
4563
4564 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4565 screen line, this function will set IT to the next position >
4566 TO_CHARPOS. */
4567
4568 void
4569 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4570 struct it *it;
4571 int to_charpos, to_x, to_y, to_vpos;
4572 int op;
4573 {
4574 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4575 int line_height;
4576 int reached = 0;
4577
4578 for (;;)
4579 {
4580 if (op & MOVE_TO_VPOS)
4581 {
4582 /* If no TO_CHARPOS and no TO_X specified, stop at the
4583 start of the line TO_VPOS. */
4584 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4585 {
4586 if (it->vpos == to_vpos)
4587 {
4588 reached = 1;
4589 break;
4590 }
4591 else
4592 skip = move_it_in_display_line_to (it, -1, -1, 0);
4593 }
4594 else
4595 {
4596 /* TO_VPOS >= 0 means stop at TO_X in the line at
4597 TO_VPOS, or at TO_POS, whichever comes first. */
4598 if (it->vpos == to_vpos)
4599 {
4600 reached = 2;
4601 break;
4602 }
4603
4604 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4605
4606 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4607 {
4608 reached = 3;
4609 break;
4610 }
4611 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4612 {
4613 /* We have reached TO_X but not in the line we want. */
4614 skip = move_it_in_display_line_to (it, to_charpos,
4615 -1, MOVE_TO_POS);
4616 if (skip == MOVE_POS_MATCH_OR_ZV)
4617 {
4618 reached = 4;
4619 break;
4620 }
4621 }
4622 }
4623 }
4624 else if (op & MOVE_TO_Y)
4625 {
4626 struct it it_backup;
4627
4628 /* TO_Y specified means stop at TO_X in the line containing
4629 TO_Y---or at TO_CHARPOS if this is reached first. The
4630 problem is that we can't really tell whether the line
4631 contains TO_Y before we have completely scanned it, and
4632 this may skip past TO_X. What we do is to first scan to
4633 TO_X.
4634
4635 If TO_X is not specified, use a TO_X of zero. The reason
4636 is to make the outcome of this function more predictable.
4637 If we didn't use TO_X == 0, we would stop at the end of
4638 the line which is probably not what a caller would expect
4639 to happen. */
4640 skip = move_it_in_display_line_to (it, to_charpos,
4641 ((op & MOVE_TO_X)
4642 ? to_x : 0),
4643 (MOVE_TO_X
4644 | (op & MOVE_TO_POS)));
4645
4646 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4647 if (skip == MOVE_POS_MATCH_OR_ZV)
4648 {
4649 reached = 5;
4650 break;
4651 }
4652
4653 /* If TO_X was reached, we would like to know whether TO_Y
4654 is in the line. This can only be said if we know the
4655 total line height which requires us to scan the rest of
4656 the line. */
4657 if (skip == MOVE_X_REACHED)
4658 {
4659 it_backup = *it;
4660 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4661 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4662 op & MOVE_TO_POS);
4663 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4664 }
4665
4666 /* Now, decide whether TO_Y is in this line. */
4667 line_height = it->max_ascent + it->max_descent;
4668 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4669
4670 if (to_y >= it->current_y
4671 && to_y < it->current_y + line_height)
4672 {
4673 if (skip == MOVE_X_REACHED)
4674 /* If TO_Y is in this line and TO_X was reached above,
4675 we scanned too far. We have to restore IT's settings
4676 to the ones before skipping. */
4677 *it = it_backup;
4678 reached = 6;
4679 }
4680 else if (skip == MOVE_X_REACHED)
4681 {
4682 skip = skip2;
4683 if (skip == MOVE_POS_MATCH_OR_ZV)
4684 reached = 7;
4685 }
4686
4687 if (reached)
4688 break;
4689 }
4690 else
4691 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4692
4693 switch (skip)
4694 {
4695 case MOVE_POS_MATCH_OR_ZV:
4696 reached = 8;
4697 goto out;
4698
4699 case MOVE_NEWLINE_OR_CR:
4700 set_iterator_to_next (it, 1);
4701 it->continuation_lines_width = 0;
4702 break;
4703
4704 case MOVE_LINE_TRUNCATED:
4705 it->continuation_lines_width = 0;
4706 reseat_at_next_visible_line_start (it, 0);
4707 if ((op & MOVE_TO_POS) != 0
4708 && IT_CHARPOS (*it) > to_charpos)
4709 {
4710 reached = 9;
4711 goto out;
4712 }
4713 break;
4714
4715 case MOVE_LINE_CONTINUED:
4716 it->continuation_lines_width += it->current_x;
4717 break;
4718
4719 default:
4720 abort ();
4721 }
4722
4723 /* Reset/increment for the next run. */
4724 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4725 it->current_x = it->hpos = 0;
4726 it->current_y += it->max_ascent + it->max_descent;
4727 ++it->vpos;
4728 last_height = it->max_ascent + it->max_descent;
4729 last_max_ascent = it->max_ascent;
4730 it->max_ascent = it->max_descent = 0;
4731 }
4732
4733 out:
4734
4735 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4736 }
4737
4738
4739 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4740
4741 If DY > 0, move IT backward at least that many pixels. DY = 0
4742 means move IT backward to the preceding line start or BEGV. This
4743 function may move over more than DY pixels if IT->current_y - DY
4744 ends up in the middle of a line; in this case IT->current_y will be
4745 set to the top of the line moved to. */
4746
4747 void
4748 move_it_vertically_backward (it, dy)
4749 struct it *it;
4750 int dy;
4751 {
4752 int nlines, h, line_height;
4753 struct it it2;
4754 int start_pos = IT_CHARPOS (*it);
4755
4756 xassert (dy >= 0);
4757
4758 /* Estimate how many newlines we must move back. */
4759 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4760
4761 /* Set the iterator's position that many lines back. */
4762 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4763 back_to_previous_visible_line_start (it);
4764
4765 /* Reseat the iterator here. When moving backward, we don't want
4766 reseat to skip forward over invisible text, set up the iterator
4767 to deliver from overlay strings at the new position etc. So,
4768 use reseat_1 here. */
4769 reseat_1 (it, it->current.pos, 1);
4770
4771 /* We are now surely at a line start. */
4772 it->current_x = it->hpos = 0;
4773
4774 /* Move forward and see what y-distance we moved. First move to the
4775 start of the next line so that we get its height. We need this
4776 height to be able to tell whether we reached the specified
4777 y-distance. */
4778 it2 = *it;
4779 it2.max_ascent = it2.max_descent = 0;
4780 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4781 MOVE_TO_POS | MOVE_TO_VPOS);
4782 xassert (IT_CHARPOS (*it) >= BEGV);
4783 line_height = it2.max_ascent + it2.max_descent;
4784
4785 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4786 xassert (IT_CHARPOS (*it) >= BEGV);
4787 h = it2.current_y - it->current_y;
4788 nlines = it2.vpos - it->vpos;
4789
4790 /* Correct IT's y and vpos position. */
4791 it->vpos -= nlines;
4792 it->current_y -= h;
4793
4794 if (dy == 0)
4795 {
4796 /* DY == 0 means move to the start of the screen line. The
4797 value of nlines is > 0 if continuation lines were involved. */
4798 if (nlines > 0)
4799 move_it_by_lines (it, nlines, 1);
4800 xassert (IT_CHARPOS (*it) <= start_pos);
4801 }
4802 else if (nlines)
4803 {
4804 /* The y-position we try to reach. Note that h has been
4805 subtracted in front of the if-statement. */
4806 int target_y = it->current_y + h - dy;
4807
4808 /* If we did not reach target_y, try to move further backward if
4809 we can. If we moved too far backward, try to move forward. */
4810 if (target_y < it->current_y
4811 && IT_CHARPOS (*it) > BEGV)
4812 {
4813 move_it_vertically (it, target_y - it->current_y);
4814 xassert (IT_CHARPOS (*it) >= BEGV);
4815 }
4816 else if (target_y >= it->current_y + line_height
4817 && IT_CHARPOS (*it) < ZV)
4818 {
4819 move_it_vertically (it, target_y - (it->current_y + line_height));
4820 xassert (IT_CHARPOS (*it) >= BEGV);
4821 }
4822 }
4823 }
4824
4825
4826 /* Move IT by a specified amount of pixel lines DY. DY negative means
4827 move backwards. DY = 0 means move to start of screen line. At the
4828 end, IT will be on the start of a screen line. */
4829
4830 void
4831 move_it_vertically (it, dy)
4832 struct it *it;
4833 int dy;
4834 {
4835 if (dy <= 0)
4836 move_it_vertically_backward (it, -dy);
4837 else if (dy > 0)
4838 {
4839 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
4840 move_it_to (it, ZV, -1, it->current_y + dy, -1,
4841 MOVE_TO_POS | MOVE_TO_Y);
4842 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
4843
4844 /* If buffer ends in ZV without a newline, move to the start of
4845 the line to satisfy the post-condition. */
4846 if (IT_CHARPOS (*it) == ZV
4847 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
4848 move_it_by_lines (it, 0, 0);
4849 }
4850 }
4851
4852
4853 /* Return non-zero if some text between buffer positions START_CHARPOS
4854 and END_CHARPOS is invisible. IT->window is the window for text
4855 property lookup. */
4856
4857 static int
4858 invisible_text_between_p (it, start_charpos, end_charpos)
4859 struct it *it;
4860 int start_charpos, end_charpos;
4861 {
4862 Lisp_Object prop, limit;
4863 int invisible_found_p;
4864
4865 xassert (it != NULL && start_charpos <= end_charpos);
4866
4867 /* Is text at START invisible? */
4868 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
4869 it->window);
4870 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4871 invisible_found_p = 1;
4872 else
4873 {
4874 limit = Fnext_single_char_property_change (make_number (start_charpos),
4875 Qinvisible, Qnil,
4876 make_number (end_charpos));
4877 invisible_found_p = XFASTINT (limit) < end_charpos;
4878 }
4879
4880 return invisible_found_p;
4881 }
4882
4883
4884 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
4885 negative means move up. DVPOS == 0 means move to the start of the
4886 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
4887 NEED_Y_P is zero, IT->current_y will be left unchanged.
4888
4889 Further optimization ideas: If we would know that IT->f doesn't use
4890 a face with proportional font, we could be faster for
4891 truncate-lines nil. */
4892
4893 void
4894 move_it_by_lines (it, dvpos, need_y_p)
4895 struct it *it;
4896 int dvpos, need_y_p;
4897 {
4898 struct position pos;
4899
4900 if (!FRAME_WINDOW_P (it->f))
4901 {
4902 struct text_pos textpos;
4903
4904 /* We can use vmotion on frames without proportional fonts. */
4905 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
4906 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
4907 reseat (it, textpos, 1);
4908 it->vpos += pos.vpos;
4909 it->current_y += pos.vpos;
4910 }
4911 else if (dvpos == 0)
4912 {
4913 /* DVPOS == 0 means move to the start of the screen line. */
4914 move_it_vertically_backward (it, 0);
4915 xassert (it->current_x == 0 && it->hpos == 0);
4916 }
4917 else if (dvpos > 0)
4918 {
4919 /* If there are no continuation lines, and if there is no
4920 selective display, try the simple method of moving forward
4921 DVPOS newlines, then see where we are. */
4922 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4923 {
4924 int shortage = 0, charpos;
4925
4926 if (FETCH_BYTE (IT_BYTEPOS (*it) == '\n'))
4927 charpos = IT_CHARPOS (*it) + 1;
4928 else
4929 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
4930 &shortage, 0);
4931
4932 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
4933 {
4934 struct text_pos pos;
4935 CHARPOS (pos) = charpos;
4936 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4937 reseat (it, pos, 1);
4938 it->vpos += dvpos - shortage;
4939 it->hpos = it->current_x = 0;
4940 return;
4941 }
4942 }
4943
4944 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
4945 }
4946 else
4947 {
4948 struct it it2;
4949 int start_charpos, i;
4950
4951 /* If there are no continuation lines, and if there is no
4952 selective display, try the simple method of moving backward
4953 -DVPOS newlines. */
4954 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
4955 {
4956 int shortage;
4957 int charpos = IT_CHARPOS (*it);
4958 int bytepos = IT_BYTEPOS (*it);
4959
4960 /* If in the middle of a line, go to its start. */
4961 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
4962 {
4963 charpos = find_next_newline_no_quit (charpos, -1);
4964 bytepos = CHAR_TO_BYTE (charpos);
4965 }
4966
4967 if (charpos == BEGV)
4968 {
4969 struct text_pos pos;
4970 CHARPOS (pos) = charpos;
4971 BYTEPOS (pos) = bytepos;
4972 reseat (it, pos, 1);
4973 it->hpos = it->current_x = 0;
4974 return;
4975 }
4976 else
4977 {
4978 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
4979 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
4980 {
4981 struct text_pos pos;
4982 CHARPOS (pos) = charpos;
4983 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
4984 reseat (it, pos, 1);
4985 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
4986 it->hpos = it->current_x = 0;
4987 return;
4988 }
4989 }
4990 }
4991
4992 /* Go back -DVPOS visible lines and reseat the iterator there. */
4993 start_charpos = IT_CHARPOS (*it);
4994 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
4995 back_to_previous_visible_line_start (it);
4996 reseat (it, it->current.pos, 1);
4997 it->current_x = it->hpos = 0;
4998
4999 /* Above call may have moved too far if continuation lines
5000 are involved. Scan forward and see if it did. */
5001 it2 = *it;
5002 it2.vpos = it2.current_y = 0;
5003 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5004 it->vpos -= it2.vpos;
5005 it->current_y -= it2.current_y;
5006 it->current_x = it->hpos = 0;
5007
5008 /* If we moved too far, move IT some lines forward. */
5009 if (it2.vpos > -dvpos)
5010 {
5011 int delta = it2.vpos + dvpos;
5012 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5013 }
5014 }
5015 }
5016
5017
5018 \f
5019 /***********************************************************************
5020 Messages
5021 ***********************************************************************/
5022
5023
5024 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5025 to *Messages*. */
5026
5027 void
5028 add_to_log (format, arg1, arg2)
5029 char *format;
5030 Lisp_Object arg1, arg2;
5031 {
5032 Lisp_Object args[3];
5033 Lisp_Object msg, fmt;
5034 char *buffer;
5035 int len;
5036 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5037
5038 fmt = msg = Qnil;
5039 GCPRO4 (fmt, msg, arg1, arg2);
5040
5041 args[0] = fmt = build_string (format);
5042 args[1] = arg1;
5043 args[2] = arg2;
5044 msg = Fformat (3, args);
5045
5046 len = STRING_BYTES (XSTRING (msg)) + 1;
5047 buffer = (char *) alloca (len);
5048 strcpy (buffer, XSTRING (msg)->data);
5049
5050 message_dolog (buffer, len - 1, 1, 0);
5051 UNGCPRO;
5052 }
5053
5054
5055 /* Output a newline in the *Messages* buffer if "needs" one. */
5056
5057 void
5058 message_log_maybe_newline ()
5059 {
5060 if (message_log_need_newline)
5061 message_dolog ("", 0, 1, 0);
5062 }
5063
5064
5065 /* Add a string M of length LEN to the message log, optionally
5066 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5067 nonzero, means interpret the contents of M as multibyte. This
5068 function calls low-level routines in order to bypass text property
5069 hooks, etc. which might not be safe to run. */
5070
5071 void
5072 message_dolog (m, len, nlflag, multibyte)
5073 char *m;
5074 int len, nlflag, multibyte;
5075 {
5076 if (!NILP (Vmessage_log_max))
5077 {
5078 struct buffer *oldbuf;
5079 Lisp_Object oldpoint, oldbegv, oldzv;
5080 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5081 int point_at_end = 0;
5082 int zv_at_end = 0;
5083 Lisp_Object old_deactivate_mark, tem;
5084 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5085
5086 old_deactivate_mark = Vdeactivate_mark;
5087 oldbuf = current_buffer;
5088 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5089 current_buffer->undo_list = Qt;
5090
5091 oldpoint = Fpoint_marker ();
5092 oldbegv = Fpoint_min_marker ();
5093 oldzv = Fpoint_max_marker ();
5094 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5095
5096 if (PT == Z)
5097 point_at_end = 1;
5098 if (ZV == Z)
5099 zv_at_end = 1;
5100
5101 BEGV = BEG;
5102 BEGV_BYTE = BEG_BYTE;
5103 ZV = Z;
5104 ZV_BYTE = Z_BYTE;
5105 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5106
5107 /* Insert the string--maybe converting multibyte to single byte
5108 or vice versa, so that all the text fits the buffer. */
5109 if (multibyte
5110 && NILP (current_buffer->enable_multibyte_characters))
5111 {
5112 int i, c, nbytes;
5113 unsigned char work[1];
5114
5115 /* Convert a multibyte string to single-byte
5116 for the *Message* buffer. */
5117 for (i = 0; i < len; i += nbytes)
5118 {
5119 c = string_char_and_length (m + i, len - i, &nbytes);
5120 work[0] = (SINGLE_BYTE_CHAR_P (c)
5121 ? c
5122 : multibyte_char_to_unibyte (c, Qnil));
5123 insert_1_both (work, 1, 1, 1, 0, 0);
5124 }
5125 }
5126 else if (! multibyte
5127 && ! NILP (current_buffer->enable_multibyte_characters))
5128 {
5129 int i, c, nbytes;
5130 unsigned char *msg = (unsigned char *) m;
5131 unsigned char str[MAX_MULTIBYTE_LENGTH];
5132 /* Convert a single-byte string to multibyte
5133 for the *Message* buffer. */
5134 for (i = 0; i < len; i++)
5135 {
5136 c = unibyte_char_to_multibyte (msg[i]);
5137 nbytes = CHAR_STRING (c, str);
5138 insert_1_both (str, 1, nbytes, 1, 0, 0);
5139 }
5140 }
5141 else if (len)
5142 insert_1 (m, len, 1, 0, 0);
5143
5144 if (nlflag)
5145 {
5146 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5147 insert_1 ("\n", 1, 1, 0, 0);
5148
5149 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5150 this_bol = PT;
5151 this_bol_byte = PT_BYTE;
5152
5153 if (this_bol > BEG)
5154 {
5155 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5156 prev_bol = PT;
5157 prev_bol_byte = PT_BYTE;
5158
5159 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5160 this_bol, this_bol_byte);
5161 if (dup)
5162 {
5163 del_range_both (prev_bol, prev_bol_byte,
5164 this_bol, this_bol_byte, 0);
5165 if (dup > 1)
5166 {
5167 char dupstr[40];
5168 int duplen;
5169
5170 /* If you change this format, don't forget to also
5171 change message_log_check_duplicate. */
5172 sprintf (dupstr, " [%d times]", dup);
5173 duplen = strlen (dupstr);
5174 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5175 insert_1 (dupstr, duplen, 1, 0, 1);
5176 }
5177 }
5178 }
5179
5180 if (NATNUMP (Vmessage_log_max))
5181 {
5182 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5183 -XFASTINT (Vmessage_log_max) - 1, 0);
5184 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5185 }
5186 }
5187 BEGV = XMARKER (oldbegv)->charpos;
5188 BEGV_BYTE = marker_byte_position (oldbegv);
5189
5190 if (zv_at_end)
5191 {
5192 ZV = Z;
5193 ZV_BYTE = Z_BYTE;
5194 }
5195 else
5196 {
5197 ZV = XMARKER (oldzv)->charpos;
5198 ZV_BYTE = marker_byte_position (oldzv);
5199 }
5200
5201 if (point_at_end)
5202 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5203 else
5204 /* We can't do Fgoto_char (oldpoint) because it will run some
5205 Lisp code. */
5206 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5207 XMARKER (oldpoint)->bytepos);
5208
5209 UNGCPRO;
5210 free_marker (oldpoint);
5211 free_marker (oldbegv);
5212 free_marker (oldzv);
5213
5214 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5215 set_buffer_internal (oldbuf);
5216 if (NILP (tem))
5217 windows_or_buffers_changed = old_windows_or_buffers_changed;
5218 message_log_need_newline = !nlflag;
5219 Vdeactivate_mark = old_deactivate_mark;
5220 }
5221 }
5222
5223
5224 /* We are at the end of the buffer after just having inserted a newline.
5225 (Note: We depend on the fact we won't be crossing the gap.)
5226 Check to see if the most recent message looks a lot like the previous one.
5227 Return 0 if different, 1 if the new one should just replace it, or a
5228 value N > 1 if we should also append " [N times]". */
5229
5230 static int
5231 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5232 int prev_bol, this_bol;
5233 int prev_bol_byte, this_bol_byte;
5234 {
5235 int i;
5236 int len = Z_BYTE - 1 - this_bol_byte;
5237 int seen_dots = 0;
5238 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5239 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5240
5241 for (i = 0; i < len; i++)
5242 {
5243 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.'
5244 && p1[i] != '\n')
5245 seen_dots = 1;
5246 if (p1[i] != p2[i])
5247 return seen_dots;
5248 }
5249 p1 += len;
5250 if (*p1 == '\n')
5251 return 2;
5252 if (*p1++ == ' ' && *p1++ == '[')
5253 {
5254 int n = 0;
5255 while (*p1 >= '0' && *p1 <= '9')
5256 n = n * 10 + *p1++ - '0';
5257 if (strncmp (p1, " times]\n", 8) == 0)
5258 return n+1;
5259 }
5260 return 0;
5261 }
5262
5263
5264 /* Display an echo area message M with a specified length of LEN
5265 chars. The string may include null characters. If M is 0, clear
5266 out any existing message, and let the mini-buffer text show through.
5267
5268 The buffer M must continue to exist until after the echo area gets
5269 cleared or some other message gets displayed there. This means do
5270 not pass text that is stored in a Lisp string; do not pass text in
5271 a buffer that was alloca'd. */
5272
5273 void
5274 message2 (m, len, multibyte)
5275 char *m;
5276 int len;
5277 int multibyte;
5278 {
5279 /* First flush out any partial line written with print. */
5280 message_log_maybe_newline ();
5281 if (m)
5282 message_dolog (m, len, 1, multibyte);
5283 message2_nolog (m, len, multibyte);
5284 }
5285
5286
5287 /* The non-logging counterpart of message2. */
5288
5289 void
5290 message2_nolog (m, len, multibyte)
5291 char *m;
5292 int len;
5293 {
5294 struct frame *sf = SELECTED_FRAME ();
5295 message_enable_multibyte = multibyte;
5296
5297 if (noninteractive)
5298 {
5299 if (noninteractive_need_newline)
5300 putc ('\n', stderr);
5301 noninteractive_need_newline = 0;
5302 if (m)
5303 fwrite (m, len, 1, stderr);
5304 if (cursor_in_echo_area == 0)
5305 fprintf (stderr, "\n");
5306 fflush (stderr);
5307 }
5308 /* A null message buffer means that the frame hasn't really been
5309 initialized yet. Error messages get reported properly by
5310 cmd_error, so this must be just an informative message; toss it. */
5311 else if (INTERACTIVE
5312 && sf->glyphs_initialized_p
5313 && FRAME_MESSAGE_BUF (sf))
5314 {
5315 Lisp_Object mini_window;
5316 struct frame *f;
5317
5318 /* Get the frame containing the mini-buffer
5319 that the selected frame is using. */
5320 mini_window = FRAME_MINIBUF_WINDOW (sf);
5321 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5322
5323 FRAME_SAMPLE_VISIBILITY (f);
5324 if (FRAME_VISIBLE_P (sf)
5325 && ! FRAME_VISIBLE_P (f))
5326 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5327
5328 if (m)
5329 {
5330 set_message (m, Qnil, len, multibyte);
5331 if (minibuffer_auto_raise)
5332 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5333 }
5334 else
5335 clear_message (1, 1);
5336
5337 do_pending_window_change (0);
5338 echo_area_display (1);
5339 do_pending_window_change (0);
5340 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5341 (*frame_up_to_date_hook) (f);
5342 }
5343 }
5344
5345
5346 /* Display an echo area message M with a specified length of NBYTES
5347 bytes. The string may include null characters. If M is not a
5348 string, clear out any existing message, and let the mini-buffer
5349 text show through. */
5350
5351 void
5352 message3 (m, nbytes, multibyte)
5353 Lisp_Object m;
5354 int nbytes;
5355 int multibyte;
5356 {
5357 struct gcpro gcpro1;
5358
5359 GCPRO1 (m);
5360
5361 /* First flush out any partial line written with print. */
5362 message_log_maybe_newline ();
5363 if (STRINGP (m))
5364 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5365 message3_nolog (m, nbytes, multibyte);
5366
5367 UNGCPRO;
5368 }
5369
5370
5371 /* The non-logging version of message3. */
5372
5373 void
5374 message3_nolog (m, nbytes, multibyte)
5375 Lisp_Object m;
5376 int nbytes, multibyte;
5377 {
5378 struct frame *sf = SELECTED_FRAME ();
5379 message_enable_multibyte = multibyte;
5380
5381 if (noninteractive)
5382 {
5383 if (noninteractive_need_newline)
5384 putc ('\n', stderr);
5385 noninteractive_need_newline = 0;
5386 if (STRINGP (m))
5387 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5388 if (cursor_in_echo_area == 0)
5389 fprintf (stderr, "\n");
5390 fflush (stderr);
5391 }
5392 /* A null message buffer means that the frame hasn't really been
5393 initialized yet. Error messages get reported properly by
5394 cmd_error, so this must be just an informative message; toss it. */
5395 else if (INTERACTIVE
5396 && sf->glyphs_initialized_p
5397 && FRAME_MESSAGE_BUF (sf))
5398 {
5399 Lisp_Object mini_window;
5400 Lisp_Object frame;
5401 struct frame *f;
5402
5403 /* Get the frame containing the mini-buffer
5404 that the selected frame is using. */
5405 mini_window = FRAME_MINIBUF_WINDOW (sf);
5406 frame = XWINDOW (mini_window)->frame;
5407 f = XFRAME (frame);
5408
5409 FRAME_SAMPLE_VISIBILITY (f);
5410 if (FRAME_VISIBLE_P (sf)
5411 && !FRAME_VISIBLE_P (f))
5412 Fmake_frame_visible (frame);
5413
5414 if (STRINGP (m) && XSTRING (m)->size)
5415 {
5416 set_message (NULL, m, nbytes, multibyte);
5417 if (minibuffer_auto_raise)
5418 Fraise_frame (frame);
5419 }
5420 else
5421 clear_message (1, 1);
5422
5423 do_pending_window_change (0);
5424 echo_area_display (1);
5425 do_pending_window_change (0);
5426 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5427 (*frame_up_to_date_hook) (f);
5428 }
5429 }
5430
5431
5432 /* Display a null-terminated echo area message M. If M is 0, clear
5433 out any existing message, and let the mini-buffer text show through.
5434
5435 The buffer M must continue to exist until after the echo area gets
5436 cleared or some other message gets displayed there. Do not pass
5437 text that is stored in a Lisp string. Do not pass text in a buffer
5438 that was alloca'd. */
5439
5440 void
5441 message1 (m)
5442 char *m;
5443 {
5444 message2 (m, (m ? strlen (m) : 0), 0);
5445 }
5446
5447
5448 /* The non-logging counterpart of message1. */
5449
5450 void
5451 message1_nolog (m)
5452 char *m;
5453 {
5454 message2_nolog (m, (m ? strlen (m) : 0), 0);
5455 }
5456
5457 /* Display a message M which contains a single %s
5458 which gets replaced with STRING. */
5459
5460 void
5461 message_with_string (m, string, log)
5462 char *m;
5463 Lisp_Object string;
5464 int log;
5465 {
5466 if (noninteractive)
5467 {
5468 if (m)
5469 {
5470 if (noninteractive_need_newline)
5471 putc ('\n', stderr);
5472 noninteractive_need_newline = 0;
5473 fprintf (stderr, m, XSTRING (string)->data);
5474 if (cursor_in_echo_area == 0)
5475 fprintf (stderr, "\n");
5476 fflush (stderr);
5477 }
5478 }
5479 else if (INTERACTIVE)
5480 {
5481 /* The frame whose minibuffer we're going to display the message on.
5482 It may be larger than the selected frame, so we need
5483 to use its buffer, not the selected frame's buffer. */
5484 Lisp_Object mini_window;
5485 struct frame *f, *sf = SELECTED_FRAME ();
5486
5487 /* Get the frame containing the minibuffer
5488 that the selected frame is using. */
5489 mini_window = FRAME_MINIBUF_WINDOW (sf);
5490 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5491
5492 /* A null message buffer means that the frame hasn't really been
5493 initialized yet. Error messages get reported properly by
5494 cmd_error, so this must be just an informative message; toss it. */
5495 if (FRAME_MESSAGE_BUF (f))
5496 {
5497 int len;
5498 char *a[1];
5499 a[0] = (char *) XSTRING (string)->data;
5500
5501 len = doprnt (FRAME_MESSAGE_BUF (f),
5502 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5503
5504 if (log)
5505 message2 (FRAME_MESSAGE_BUF (f), len,
5506 STRING_MULTIBYTE (string));
5507 else
5508 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5509 STRING_MULTIBYTE (string));
5510
5511 /* Print should start at the beginning of the message
5512 buffer next time. */
5513 message_buf_print = 0;
5514 }
5515 }
5516 }
5517
5518
5519 /* Dump an informative message to the minibuf. If M is 0, clear out
5520 any existing message, and let the mini-buffer text show through. */
5521
5522 /* VARARGS 1 */
5523 void
5524 message (m, a1, a2, a3)
5525 char *m;
5526 EMACS_INT a1, a2, a3;
5527 {
5528 if (noninteractive)
5529 {
5530 if (m)
5531 {
5532 if (noninteractive_need_newline)
5533 putc ('\n', stderr);
5534 noninteractive_need_newline = 0;
5535 fprintf (stderr, m, a1, a2, a3);
5536 if (cursor_in_echo_area == 0)
5537 fprintf (stderr, "\n");
5538 fflush (stderr);
5539 }
5540 }
5541 else if (INTERACTIVE)
5542 {
5543 /* The frame whose mini-buffer we're going to display the message
5544 on. It may be larger than the selected frame, so we need to
5545 use its buffer, not the selected frame's buffer. */
5546 Lisp_Object mini_window;
5547 struct frame *f, *sf = SELECTED_FRAME ();
5548
5549 /* Get the frame containing the mini-buffer
5550 that the selected frame is using. */
5551 mini_window = FRAME_MINIBUF_WINDOW (sf);
5552 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5553
5554 /* A null message buffer means that the frame hasn't really been
5555 initialized yet. Error messages get reported properly by
5556 cmd_error, so this must be just an informative message; toss
5557 it. */
5558 if (FRAME_MESSAGE_BUF (f))
5559 {
5560 if (m)
5561 {
5562 int len;
5563 #ifdef NO_ARG_ARRAY
5564 char *a[3];
5565 a[0] = (char *) a1;
5566 a[1] = (char *) a2;
5567 a[2] = (char *) a3;
5568
5569 len = doprnt (FRAME_MESSAGE_BUF (f),
5570 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5571 #else
5572 len = doprnt (FRAME_MESSAGE_BUF (f),
5573 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5574 (char **) &a1);
5575 #endif /* NO_ARG_ARRAY */
5576
5577 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5578 }
5579 else
5580 message1 (0);
5581
5582 /* Print should start at the beginning of the message
5583 buffer next time. */
5584 message_buf_print = 0;
5585 }
5586 }
5587 }
5588
5589
5590 /* The non-logging version of message. */
5591
5592 void
5593 message_nolog (m, a1, a2, a3)
5594 char *m;
5595 EMACS_INT a1, a2, a3;
5596 {
5597 Lisp_Object old_log_max;
5598 old_log_max = Vmessage_log_max;
5599 Vmessage_log_max = Qnil;
5600 message (m, a1, a2, a3);
5601 Vmessage_log_max = old_log_max;
5602 }
5603
5604
5605 /* Display the current message in the current mini-buffer. This is
5606 only called from error handlers in process.c, and is not time
5607 critical. */
5608
5609 void
5610 update_echo_area ()
5611 {
5612 if (!NILP (echo_area_buffer[0]))
5613 {
5614 Lisp_Object string;
5615 string = Fcurrent_message ();
5616 message3 (string, XSTRING (string)->size,
5617 !NILP (current_buffer->enable_multibyte_characters));
5618 }
5619 }
5620
5621
5622 /* Make sure echo area buffers in echo_buffers[] are life. If they
5623 aren't, make new ones. */
5624
5625 static void
5626 ensure_echo_area_buffers ()
5627 {
5628 int i;
5629
5630 for (i = 0; i < 2; ++i)
5631 if (!BUFFERP (echo_buffer[i])
5632 || NILP (XBUFFER (echo_buffer[i])->name))
5633 {
5634 char name[30];
5635 Lisp_Object old_buffer;
5636 int j;
5637
5638 old_buffer = echo_buffer[i];
5639 sprintf (name, " *Echo Area %d*", i);
5640 echo_buffer[i] = Fget_buffer_create (build_string (name));
5641 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5642
5643 for (j = 0; j < 2; ++j)
5644 if (EQ (old_buffer, echo_area_buffer[j]))
5645 echo_area_buffer[j] = echo_buffer[i];
5646 }
5647 }
5648
5649
5650 /* Call FN with args A1..A4 with either the current or last displayed
5651 echo_area_buffer as current buffer.
5652
5653 WHICH zero means use the current message buffer
5654 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5655 from echo_buffer[] and clear it.
5656
5657 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5658 suitable buffer from echo_buffer[] and clear it.
5659
5660 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5661 that the current message becomes the last displayed one, make
5662 choose a suitable buffer for echo_area_buffer[0], and clear it.
5663
5664 Value is what FN returns. */
5665
5666 static int
5667 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5668 struct window *w;
5669 int which;
5670 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5671 EMACS_INT a1;
5672 Lisp_Object a2;
5673 EMACS_INT a3, a4;
5674 {
5675 Lisp_Object buffer;
5676 int this_one, the_other, clear_buffer_p, rc;
5677 int count = specpdl_ptr - specpdl;
5678
5679 /* If buffers aren't life, make new ones. */
5680 ensure_echo_area_buffers ();
5681
5682 clear_buffer_p = 0;
5683
5684 if (which == 0)
5685 this_one = 0, the_other = 1;
5686 else if (which > 0)
5687 this_one = 1, the_other = 0;
5688 else
5689 {
5690 this_one = 0, the_other = 1;
5691 clear_buffer_p = 1;
5692
5693 /* We need a fresh one in case the current echo buffer equals
5694 the one containing the last displayed echo area message. */
5695 if (!NILP (echo_area_buffer[this_one])
5696 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5697 echo_area_buffer[this_one] = Qnil;
5698 }
5699
5700 /* Choose a suitable buffer from echo_buffer[] is we don't
5701 have one. */
5702 if (NILP (echo_area_buffer[this_one]))
5703 {
5704 echo_area_buffer[this_one]
5705 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5706 ? echo_buffer[the_other]
5707 : echo_buffer[this_one]);
5708 clear_buffer_p = 1;
5709 }
5710
5711 buffer = echo_area_buffer[this_one];
5712
5713 record_unwind_protect (unwind_with_echo_area_buffer,
5714 with_echo_area_buffer_unwind_data (w));
5715
5716 /* Make the echo area buffer current. Note that for display
5717 purposes, it is not necessary that the displayed window's buffer
5718 == current_buffer, except for text property lookup. So, let's
5719 only set that buffer temporarily here without doing a full
5720 Fset_window_buffer. We must also change w->pointm, though,
5721 because otherwise an assertions in unshow_buffer fails, and Emacs
5722 aborts. */
5723 set_buffer_internal_1 (XBUFFER (buffer));
5724 if (w)
5725 {
5726 w->buffer = buffer;
5727 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5728 }
5729
5730 current_buffer->undo_list = Qt;
5731 current_buffer->read_only = Qnil;
5732
5733 if (clear_buffer_p && Z > BEG)
5734 del_range (BEG, Z);
5735
5736 xassert (BEGV >= BEG);
5737 xassert (ZV <= Z && ZV >= BEGV);
5738
5739 rc = fn (a1, a2, a3, a4);
5740
5741 xassert (BEGV >= BEG);
5742 xassert (ZV <= Z && ZV >= BEGV);
5743
5744 unbind_to (count, Qnil);
5745 return rc;
5746 }
5747
5748
5749 /* Save state that should be preserved around the call to the function
5750 FN called in with_echo_area_buffer. */
5751
5752 static Lisp_Object
5753 with_echo_area_buffer_unwind_data (w)
5754 struct window *w;
5755 {
5756 int i = 0;
5757 Lisp_Object vector;
5758
5759 /* Reduce consing by keeping one vector in
5760 Vwith_echo_area_save_vector. */
5761 vector = Vwith_echo_area_save_vector;
5762 Vwith_echo_area_save_vector = Qnil;
5763
5764 if (NILP (vector))
5765 vector = Fmake_vector (make_number (7), Qnil);
5766
5767 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5768 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5769 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5770
5771 if (w)
5772 {
5773 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5774 XVECTOR (vector)->contents[i++] = w->buffer;
5775 XVECTOR (vector)->contents[i++]
5776 = make_number (XMARKER (w->pointm)->charpos);
5777 XVECTOR (vector)->contents[i++]
5778 = make_number (XMARKER (w->pointm)->bytepos);
5779 }
5780 else
5781 {
5782 int end = i + 4;
5783 while (i < end)
5784 XVECTOR (vector)->contents[i++] = Qnil;
5785 }
5786
5787 xassert (i == XVECTOR (vector)->size);
5788 return vector;
5789 }
5790
5791
5792 /* Restore global state from VECTOR which was created by
5793 with_echo_area_buffer_unwind_data. */
5794
5795 static Lisp_Object
5796 unwind_with_echo_area_buffer (vector)
5797 Lisp_Object vector;
5798 {
5799 int i = 0;
5800
5801 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5802 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5803 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5804
5805 if (WINDOWP (XVECTOR (vector)->contents[i]))
5806 {
5807 struct window *w;
5808 Lisp_Object buffer, charpos, bytepos;
5809
5810 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
5811 buffer = XVECTOR (vector)->contents[i]; ++i;
5812 charpos = XVECTOR (vector)->contents[i]; ++i;
5813 bytepos = XVECTOR (vector)->contents[i]; ++i;
5814
5815 w->buffer = buffer;
5816 set_marker_both (w->pointm, buffer,
5817 XFASTINT (charpos), XFASTINT (bytepos));
5818 }
5819
5820 Vwith_echo_area_save_vector = vector;
5821 return Qnil;
5822 }
5823
5824
5825 /* Set up the echo area for use by print functions. MULTIBYTE_P
5826 non-zero means we will print multibyte. */
5827
5828 void
5829 setup_echo_area_for_printing (multibyte_p)
5830 int multibyte_p;
5831 {
5832 ensure_echo_area_buffers ();
5833
5834 if (!message_buf_print)
5835 {
5836 /* A message has been output since the last time we printed.
5837 Choose a fresh echo area buffer. */
5838 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5839 echo_area_buffer[0] = echo_buffer[1];
5840 else
5841 echo_area_buffer[0] = echo_buffer[0];
5842
5843 /* Switch to that buffer and clear it. */
5844 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5845 if (Z > BEG)
5846 del_range (BEG, Z);
5847 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5848
5849 /* Set up the buffer for the multibyteness we need. */
5850 if (multibyte_p
5851 != !NILP (current_buffer->enable_multibyte_characters))
5852 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
5853
5854 /* Raise the frame containing the echo area. */
5855 if (minibuffer_auto_raise)
5856 {
5857 struct frame *sf = SELECTED_FRAME ();
5858 Lisp_Object mini_window;
5859 mini_window = FRAME_MINIBUF_WINDOW (sf);
5860 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5861 }
5862
5863 message_log_maybe_newline ();
5864 message_buf_print = 1;
5865 }
5866 else
5867 {
5868 if (NILP (echo_area_buffer[0]))
5869 {
5870 if (EQ (echo_area_buffer[1], echo_buffer[0]))
5871 echo_area_buffer[0] = echo_buffer[1];
5872 else
5873 echo_area_buffer[0] = echo_buffer[0];
5874 }
5875
5876 if (current_buffer != XBUFFER (echo_area_buffer[0]))
5877 /* Someone switched buffers between print requests. */
5878 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
5879 }
5880 }
5881
5882
5883 /* Display an echo area message in window W. Value is non-zero if W's
5884 height is changed. If display_last_displayed_message_p is
5885 non-zero, display the message that was last displayed, otherwise
5886 display the current message. */
5887
5888 static int
5889 display_echo_area (w)
5890 struct window *w;
5891 {
5892 int i, no_message_p, window_height_changed_p, count;
5893
5894 /* Temporarily disable garbage collections while displaying the echo
5895 area. This is done because a GC can print a message itself.
5896 That message would modify the echo area buffer's contents while a
5897 redisplay of the buffer is going on, and seriously confuse
5898 redisplay. */
5899 count = inhibit_garbage_collection ();
5900
5901 /* If there is no message, we must call display_echo_area_1
5902 nevertheless because it resizes the window. But we will have to
5903 reset the echo_area_buffer in question to nil at the end because
5904 with_echo_area_buffer will sets it to an empty buffer. */
5905 i = display_last_displayed_message_p ? 1 : 0;
5906 no_message_p = NILP (echo_area_buffer[i]);
5907
5908 window_height_changed_p
5909 = with_echo_area_buffer (w, display_last_displayed_message_p,
5910 display_echo_area_1,
5911 (EMACS_INT) w, Qnil, 0, 0);
5912
5913 if (no_message_p)
5914 echo_area_buffer[i] = Qnil;
5915
5916 unbind_to (count, Qnil);
5917 return window_height_changed_p;
5918 }
5919
5920
5921 /* Helper for display_echo_area. Display the current buffer which
5922 contains the current echo area message in window W, a mini-window,
5923 a pointer to which is passed in A1. A2..A4 are currently not used.
5924 Change the height of W so that all of the message is displayed.
5925 Value is non-zero if height of W was changed. */
5926
5927 static int
5928 display_echo_area_1 (a1, a2, a3, a4)
5929 EMACS_INT a1;
5930 Lisp_Object a2;
5931 EMACS_INT a3, a4;
5932 {
5933 struct window *w = (struct window *) a1;
5934 Lisp_Object window;
5935 struct text_pos start;
5936 int window_height_changed_p = 0;
5937
5938 /* Do this before displaying, so that we have a large enough glyph
5939 matrix for the display. */
5940 window_height_changed_p = resize_mini_window (w, 0);
5941
5942 /* Display. */
5943 clear_glyph_matrix (w->desired_matrix);
5944 XSETWINDOW (window, w);
5945 SET_TEXT_POS (start, BEG, BEG_BYTE);
5946 try_window (window, start);
5947
5948 return window_height_changed_p;
5949 }
5950
5951
5952 /* Resize the echo area window to exactly the size needed for the
5953 currently displayed message, if there is one. */
5954
5955 void
5956 resize_echo_area_axactly ()
5957 {
5958 if (BUFFERP (echo_area_buffer[0])
5959 && WINDOWP (echo_area_window))
5960 {
5961 struct window *w = XWINDOW (echo_area_window);
5962 int resized_p;
5963
5964 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
5965 (EMACS_INT) w, Qnil, 0, 0);
5966 if (resized_p)
5967 {
5968 ++windows_or_buffers_changed;
5969 ++update_mode_lines;
5970 redisplay_internal (0);
5971 }
5972 }
5973 }
5974
5975
5976 /* Callback function for with_echo_area_buffer, when used from
5977 resize_echo_area_axactly. A1 contains a pointer to the window to
5978 resize, A2 to A4 are not used. Value is what resize_mini_window
5979 returns. */
5980
5981 static int
5982 resize_mini_window_1 (a1, a2, a3, a4)
5983 EMACS_INT a1;
5984 Lisp_Object a2;
5985 EMACS_INT a3, a4;
5986 {
5987 return resize_mini_window ((struct window *) a1, 1);
5988 }
5989
5990
5991 /* Resize mini-window W to fit the size of its contents. EXACT:P
5992 means size the window exactly to the size needed. Otherwise, it's
5993 only enlarged until W's buffer is empty. Value is non-zero if
5994 the window height has been changed. */
5995
5996 int
5997 resize_mini_window (w, exact_p)
5998 struct window *w;
5999 int exact_p;
6000 {
6001 struct frame *f = XFRAME (w->frame);
6002 int window_height_changed_p = 0;
6003
6004 xassert (MINI_WINDOW_P (w));
6005
6006 /* Nil means don't try to resize. */
6007 if (NILP (Vmax_mini_window_height)
6008 || (FRAME_X_P (f) && f->output_data.x == NULL))
6009 return 0;
6010
6011 if (!FRAME_MINIBUF_ONLY_P (f))
6012 {
6013 struct it it;
6014 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6015 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6016 int height, max_height;
6017 int unit = CANON_Y_UNIT (f);
6018 struct text_pos start;
6019
6020 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6021
6022 /* Compute the max. number of lines specified by the user. */
6023 if (FLOATP (Vmax_mini_window_height))
6024 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6025 else if (INTEGERP (Vmax_mini_window_height))
6026 max_height = XINT (Vmax_mini_window_height);
6027 else
6028 max_height = total_height / 4;
6029
6030 /* Correct that max. height if it's bogus. */
6031 max_height = max (1, max_height);
6032 max_height = min (total_height, max_height);
6033
6034 /* Find out the height of the text in the window. */
6035 if (it.truncate_lines_p)
6036 height = 1;
6037 else
6038 {
6039 last_height = 0;
6040 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6041 if (it.max_ascent == 0 && it.max_descent == 0)
6042 height = it.current_y + last_height;
6043 else
6044 height = it.current_y + it.max_ascent + it.max_descent;
6045 height -= it.extra_line_spacing;
6046 height = (height + unit - 1) / unit;
6047 }
6048
6049 /* Compute a suitable window start. */
6050 if (height > max_height)
6051 {
6052 height = max_height;
6053 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6054 move_it_vertically_backward (&it, (height - 1) * unit);
6055 start = it.current.pos;
6056 }
6057 else
6058 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6059 SET_MARKER_FROM_TEXT_POS (w->start, start);
6060
6061 /* Let it grow only, until we display an empty message, in which
6062 case the window shrinks again. */
6063 if (height > XFASTINT (w->height))
6064 {
6065 int old_height = XFASTINT (w->height);
6066 freeze_window_starts (f, 1);
6067 grow_mini_window (w, height - XFASTINT (w->height));
6068 window_height_changed_p = XFASTINT (w->height) != old_height;
6069 }
6070 else if (height < XFASTINT (w->height)
6071 && (exact_p || BEGV == ZV))
6072 {
6073 int old_height = XFASTINT (w->height);
6074 freeze_window_starts (f, 0);
6075 shrink_mini_window (w);
6076 window_height_changed_p = XFASTINT (w->height) != old_height;
6077 }
6078 }
6079
6080 return window_height_changed_p;
6081 }
6082
6083
6084 /* Value is the current message, a string, or nil if there is no
6085 current message. */
6086
6087 Lisp_Object
6088 current_message ()
6089 {
6090 Lisp_Object msg;
6091
6092 if (NILP (echo_area_buffer[0]))
6093 msg = Qnil;
6094 else
6095 {
6096 with_echo_area_buffer (0, 0, current_message_1,
6097 (EMACS_INT) &msg, Qnil, 0, 0);
6098 if (NILP (msg))
6099 echo_area_buffer[0] = Qnil;
6100 }
6101
6102 return msg;
6103 }
6104
6105
6106 static int
6107 current_message_1 (a1, a2, a3, a4)
6108 EMACS_INT a1;
6109 Lisp_Object a2;
6110 EMACS_INT a3, a4;
6111 {
6112 Lisp_Object *msg = (Lisp_Object *) a1;
6113
6114 if (Z > BEG)
6115 *msg = make_buffer_string (BEG, Z, 1);
6116 else
6117 *msg = Qnil;
6118 return 0;
6119 }
6120
6121
6122 /* Push the current message on Vmessage_stack for later restauration
6123 by restore_message. Value is non-zero if the current message isn't
6124 empty. This is a relatively infrequent operation, so it's not
6125 worth optimizing. */
6126
6127 int
6128 push_message ()
6129 {
6130 Lisp_Object msg;
6131 msg = current_message ();
6132 Vmessage_stack = Fcons (msg, Vmessage_stack);
6133 return STRINGP (msg);
6134 }
6135
6136
6137 /* Restore message display from the top of Vmessage_stack. */
6138
6139 void
6140 restore_message ()
6141 {
6142 Lisp_Object msg;
6143
6144 xassert (CONSP (Vmessage_stack));
6145 msg = XCAR (Vmessage_stack);
6146 if (STRINGP (msg))
6147 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6148 else
6149 message3_nolog (msg, 0, 0);
6150 }
6151
6152
6153 /* Pop the top-most entry off Vmessage_stack. */
6154
6155 void
6156 pop_message ()
6157 {
6158 xassert (CONSP (Vmessage_stack));
6159 Vmessage_stack = XCDR (Vmessage_stack);
6160 }
6161
6162
6163 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6164 exits. If the stack is not empty, we have a missing pop_message
6165 somewhere. */
6166
6167 void
6168 check_message_stack ()
6169 {
6170 if (!NILP (Vmessage_stack))
6171 abort ();
6172 }
6173
6174
6175 /* Truncate to NCHARS what will be displayed in the echo area the next
6176 time we display it---but don't redisplay it now. */
6177
6178 void
6179 truncate_echo_area (nchars)
6180 int nchars;
6181 {
6182 if (nchars == 0)
6183 echo_area_buffer[0] = Qnil;
6184 /* A null message buffer means that the frame hasn't really been
6185 initialized yet. Error messages get reported properly by
6186 cmd_error, so this must be just an informative message; toss it. */
6187 else if (!noninteractive
6188 && INTERACTIVE
6189 && !NILP (echo_area_buffer[0]))
6190 {
6191 struct frame *sf = SELECTED_FRAME ();
6192 if (FRAME_MESSAGE_BUF (sf))
6193 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6194 }
6195 }
6196
6197
6198 /* Helper function for truncate_echo_area. Truncate the current
6199 message to at most NCHARS characters. */
6200
6201 static int
6202 truncate_message_1 (nchars, a2, a3, a4)
6203 EMACS_INT nchars;
6204 Lisp_Object a2;
6205 EMACS_INT a3, a4;
6206 {
6207 if (BEG + nchars < Z)
6208 del_range (BEG + nchars, Z);
6209 if (Z == BEG)
6210 echo_area_buffer[0] = Qnil;
6211 return 0;
6212 }
6213
6214
6215 /* Set the current message to a substring of S or STRING.
6216
6217 If STRING is a Lisp string, set the message to the first NBYTES
6218 bytes from STRING. NBYTES zero means use the whole string. If
6219 STRING is multibyte, the message will be displayed multibyte.
6220
6221 If S is not null, set the message to the first LEN bytes of S. LEN
6222 zero means use the whole string. MULTIBYTE_P non-zero means S is
6223 multibyte. Display the message multibyte in that case. */
6224
6225 void
6226 set_message (s, string, nbytes, multibyte_p)
6227 char *s;
6228 Lisp_Object string;
6229 int nbytes;
6230 {
6231 message_enable_multibyte
6232 = ((s && multibyte_p)
6233 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6234
6235 with_echo_area_buffer (0, -1, set_message_1,
6236 (EMACS_INT) s, string, nbytes, multibyte_p);
6237 message_buf_print = 0;
6238 help_echo_showing_p = 0;
6239 }
6240
6241
6242 /* Helper function for set_message. Arguments have the same meaning
6243 as there, with A1 corresponding to S and A2 corresponding to STRING
6244 This function is called with the echo area buffer being
6245 current. */
6246
6247 static int
6248 set_message_1 (a1, a2, nbytes, multibyte_p)
6249 EMACS_INT a1;
6250 Lisp_Object a2;
6251 EMACS_INT nbytes, multibyte_p;
6252 {
6253 char *s = (char *) a1;
6254 Lisp_Object string = a2;
6255
6256 xassert (BEG == Z);
6257
6258 /* Change multibyteness of the echo buffer appropriately. */
6259 if (message_enable_multibyte
6260 != !NILP (current_buffer->enable_multibyte_characters))
6261 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6262
6263 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6264
6265 /* Insert new message at BEG. */
6266 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6267
6268 if (STRINGP (string))
6269 {
6270 int nchars;
6271
6272 if (nbytes == 0)
6273 nbytes = XSTRING (string)->size_byte;
6274 nchars = string_byte_to_char (string, nbytes);
6275
6276 /* This function takes care of single/multibyte conversion. We
6277 just have to ensure that the echo area buffer has the right
6278 setting of enable_multibyte_characters. */
6279 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6280 }
6281 else if (s)
6282 {
6283 if (nbytes == 0)
6284 nbytes = strlen (s);
6285
6286 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6287 {
6288 /* Convert from multi-byte to single-byte. */
6289 int i, c, n;
6290 unsigned char work[1];
6291
6292 /* Convert a multibyte string to single-byte. */
6293 for (i = 0; i < nbytes; i += n)
6294 {
6295 c = string_char_and_length (s + i, nbytes - i, &n);
6296 work[0] = (SINGLE_BYTE_CHAR_P (c)
6297 ? c
6298 : multibyte_char_to_unibyte (c, Qnil));
6299 insert_1_both (work, 1, 1, 1, 0, 0);
6300 }
6301 }
6302 else if (!multibyte_p
6303 && !NILP (current_buffer->enable_multibyte_characters))
6304 {
6305 /* Convert from single-byte to multi-byte. */
6306 int i, c, n;
6307 unsigned char *msg = (unsigned char *) s;
6308 unsigned char str[MAX_MULTIBYTE_LENGTH];
6309
6310 /* Convert a single-byte string to multibyte. */
6311 for (i = 0; i < nbytes; i++)
6312 {
6313 c = unibyte_char_to_multibyte (msg[i]);
6314 n = CHAR_STRING (c, str);
6315 insert_1_both (str, 1, n, 1, 0, 0);
6316 }
6317 }
6318 else
6319 insert_1 (s, nbytes, 1, 0, 0);
6320 }
6321
6322 return 0;
6323 }
6324
6325
6326 /* Clear messages. CURRENT_P non-zero means clear the current
6327 message. LAST_DISPLAYED_P non-zero means clear the message
6328 last displayed. */
6329
6330 void
6331 clear_message (current_p, last_displayed_p)
6332 int current_p, last_displayed_p;
6333 {
6334 if (current_p)
6335 echo_area_buffer[0] = Qnil;
6336
6337 if (last_displayed_p)
6338 echo_area_buffer[1] = Qnil;
6339
6340 message_buf_print = 0;
6341 }
6342
6343 /* Clear garbaged frames.
6344
6345 This function is used where the old redisplay called
6346 redraw_garbaged_frames which in turn called redraw_frame which in
6347 turn called clear_frame. The call to clear_frame was a source of
6348 flickering. I believe a clear_frame is not necessary. It should
6349 suffice in the new redisplay to invalidate all current matrices,
6350 and ensure a complete redisplay of all windows. */
6351
6352 static void
6353 clear_garbaged_frames ()
6354 {
6355 if (frame_garbaged)
6356 {
6357 Lisp_Object tail, frame;
6358
6359 FOR_EACH_FRAME (tail, frame)
6360 {
6361 struct frame *f = XFRAME (frame);
6362
6363 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6364 {
6365 clear_current_matrices (f);
6366 f->garbaged = 0;
6367 }
6368 }
6369
6370 frame_garbaged = 0;
6371 ++windows_or_buffers_changed;
6372 }
6373 }
6374
6375
6376 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6377 is non-zero update selected_frame. Value is non-zero if the
6378 mini-windows height has been changed. */
6379
6380 static int
6381 echo_area_display (update_frame_p)
6382 int update_frame_p;
6383 {
6384 Lisp_Object mini_window;
6385 struct window *w;
6386 struct frame *f;
6387 int window_height_changed_p = 0;
6388 struct frame *sf = SELECTED_FRAME ();
6389
6390 mini_window = FRAME_MINIBUF_WINDOW (sf);
6391 w = XWINDOW (mini_window);
6392 f = XFRAME (WINDOW_FRAME (w));
6393
6394 /* Don't display if frame is invisible or not yet initialized. */
6395 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6396 return 0;
6397
6398 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6399 #ifndef macintosh
6400 #ifdef HAVE_WINDOW_SYSTEM
6401 /* When Emacs starts, selected_frame may be a visible terminal
6402 frame, even if we run under a window system. If we let this
6403 through, a message would be displayed on the terminal. */
6404 if (EQ (selected_frame, Vterminal_frame)
6405 && !NILP (Vwindow_system))
6406 return 0;
6407 #endif /* HAVE_WINDOW_SYSTEM */
6408 #endif
6409
6410 /* Redraw garbaged frames. */
6411 if (frame_garbaged)
6412 clear_garbaged_frames ();
6413
6414 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6415 {
6416 echo_area_window = mini_window;
6417 window_height_changed_p = display_echo_area (w);
6418 w->must_be_updated_p = 1;
6419
6420 /* Update the display, unless called from redisplay_internal. */
6421 if (update_frame_p)
6422 {
6423 int n = 0;
6424
6425 /* If the display update has been interrupted by pending
6426 input, update mode lines in the frame. Due to the
6427 pending input, it might have been that redisplay hasn't
6428 been called, so that mode lines above the echo area are
6429 garbaged. This looks odd, so we prevent it here. */
6430 if (!display_completed)
6431 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6432
6433 if (window_height_changed_p)
6434 {
6435 /* Must update other windows. */
6436 windows_or_buffers_changed = 1;
6437 redisplay_internal (0);
6438 }
6439 else if (FRAME_WINDOW_P (f) && n == 0)
6440 {
6441 /* Window configuration is the same as before.
6442 Can do with a display update of the echo area,
6443 unless we displayed some mode lines. */
6444 update_single_window (w, 1);
6445 rif->flush_display (f);
6446 }
6447 else
6448 update_frame (f, 1, 1);
6449 }
6450 }
6451 else if (!EQ (mini_window, selected_window))
6452 windows_or_buffers_changed++;
6453
6454 /* Last displayed message is now the current message. */
6455 echo_area_buffer[1] = echo_area_buffer[0];
6456
6457 /* Prevent redisplay optimization in redisplay_internal by resetting
6458 this_line_start_pos. This is done because the mini-buffer now
6459 displays the message instead of its buffer text. */
6460 if (EQ (mini_window, selected_window))
6461 CHARPOS (this_line_start_pos) = 0;
6462
6463 return window_height_changed_p;
6464 }
6465
6466
6467 \f
6468 /***********************************************************************
6469 Frame Titles
6470 ***********************************************************************/
6471
6472
6473 #ifdef HAVE_WINDOW_SYSTEM
6474
6475 /* A buffer for constructing frame titles in it; allocated from the
6476 heap in init_xdisp and resized as needed in store_frame_title_char. */
6477
6478 static char *frame_title_buf;
6479
6480 /* The buffer's end, and a current output position in it. */
6481
6482 static char *frame_title_buf_end;
6483 static char *frame_title_ptr;
6484
6485
6486 /* Store a single character C for the frame title in frame_title_buf.
6487 Re-allocate frame_title_buf if necessary. */
6488
6489 static void
6490 store_frame_title_char (c)
6491 char c;
6492 {
6493 /* If output position has reached the end of the allocated buffer,
6494 double the buffer's size. */
6495 if (frame_title_ptr == frame_title_buf_end)
6496 {
6497 int len = frame_title_ptr - frame_title_buf;
6498 int new_size = 2 * len * sizeof *frame_title_buf;
6499 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6500 frame_title_buf_end = frame_title_buf + new_size;
6501 frame_title_ptr = frame_title_buf + len;
6502 }
6503
6504 *frame_title_ptr++ = c;
6505 }
6506
6507
6508 /* Store part of a frame title in frame_title_buf, beginning at
6509 frame_title_ptr. STR is the string to store. Do not copy more
6510 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6511 the whole string. Pad with spaces until FIELD_WIDTH number of
6512 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6513 Called from display_mode_element when it is used to build a frame
6514 title. */
6515
6516 static int
6517 store_frame_title (str, field_width, precision)
6518 unsigned char *str;
6519 int field_width, precision;
6520 {
6521 int n = 0;
6522
6523 /* Copy at most PRECISION chars from STR. */
6524 while ((precision <= 0 || n < precision)
6525 && *str)
6526 {
6527 store_frame_title_char (*str++);
6528 ++n;
6529 }
6530
6531 /* Fill up with spaces until FIELD_WIDTH reached. */
6532 while (field_width > 0
6533 && n < field_width)
6534 {
6535 store_frame_title_char (' ');
6536 ++n;
6537 }
6538
6539 return n;
6540 }
6541
6542
6543 /* Set the title of FRAME, if it has changed. The title format is
6544 Vicon_title_format if FRAME is iconified, otherwise it is
6545 frame_title_format. */
6546
6547 static void
6548 x_consider_frame_title (frame)
6549 Lisp_Object frame;
6550 {
6551 struct frame *f = XFRAME (frame);
6552
6553 if (FRAME_WINDOW_P (f)
6554 || FRAME_MINIBUF_ONLY_P (f)
6555 || f->explicit_name)
6556 {
6557 /* Do we have more than one visible frame on this X display? */
6558 Lisp_Object tail;
6559 Lisp_Object fmt;
6560 struct buffer *obuf;
6561 int len;
6562 struct it it;
6563
6564 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6565 {
6566 struct frame *tf = XFRAME (XCAR (tail));
6567
6568 if (tf != f
6569 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6570 && !FRAME_MINIBUF_ONLY_P (tf)
6571 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6572 break;
6573 }
6574
6575 /* Set global variable indicating that multiple frames exist. */
6576 multiple_frames = CONSP (tail);
6577
6578 /* Switch to the buffer of selected window of the frame. Set up
6579 frame_title_ptr so that display_mode_element will output into it;
6580 then display the title. */
6581 obuf = current_buffer;
6582 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6583 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6584 frame_title_ptr = frame_title_buf;
6585 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6586 NULL, DEFAULT_FACE_ID);
6587 len = display_mode_element (&it, 0, -1, -1, fmt);
6588 frame_title_ptr = NULL;
6589 set_buffer_internal (obuf);
6590
6591 /* Set the title only if it's changed. This avoids consing in
6592 the common case where it hasn't. (If it turns out that we've
6593 already wasted too much time by walking through the list with
6594 display_mode_element, then we might need to optimize at a
6595 higher level than this.) */
6596 if (! STRINGP (f->name)
6597 || STRING_BYTES (XSTRING (f->name)) != len
6598 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6599 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6600 }
6601 }
6602
6603 #else /* not HAVE_WINDOW_SYSTEM */
6604
6605 #define frame_title_ptr ((char *)0)
6606 #define store_frame_title(str, mincol, maxcol) 0
6607
6608 #endif /* not HAVE_WINDOW_SYSTEM */
6609
6610
6611
6612 \f
6613 /***********************************************************************
6614 Menu Bars
6615 ***********************************************************************/
6616
6617
6618 /* Prepare for redisplay by updating menu-bar item lists when
6619 appropriate. This can call eval. */
6620
6621 void
6622 prepare_menu_bars ()
6623 {
6624 int all_windows;
6625 struct gcpro gcpro1, gcpro2;
6626 struct frame *f;
6627 struct frame *tooltip_frame;
6628
6629 #ifdef HAVE_X_WINDOWS
6630 tooltip_frame = tip_frame;
6631 #else
6632 tooltip_frame = NULL;
6633 #endif
6634
6635 /* Update all frame titles based on their buffer names, etc. We do
6636 this before the menu bars so that the buffer-menu will show the
6637 up-to-date frame titles. */
6638 #ifdef HAVE_WINDOW_SYSTEM
6639 if (windows_or_buffers_changed || update_mode_lines)
6640 {
6641 Lisp_Object tail, frame;
6642
6643 FOR_EACH_FRAME (tail, frame)
6644 {
6645 f = XFRAME (frame);
6646 if (f != tooltip_frame
6647 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6648 x_consider_frame_title (frame);
6649 }
6650 }
6651 #endif /* HAVE_WINDOW_SYSTEM */
6652
6653 /* Update the menu bar item lists, if appropriate. This has to be
6654 done before any actual redisplay or generation of display lines. */
6655 all_windows = (update_mode_lines
6656 || buffer_shared > 1
6657 || windows_or_buffers_changed);
6658 if (all_windows)
6659 {
6660 Lisp_Object tail, frame;
6661 int count = specpdl_ptr - specpdl;
6662
6663 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6664
6665 FOR_EACH_FRAME (tail, frame)
6666 {
6667 f = XFRAME (frame);
6668
6669 /* Ignore tooltip frame. */
6670 if (f == tooltip_frame)
6671 continue;
6672
6673 /* If a window on this frame changed size, report that to
6674 the user and clear the size-change flag. */
6675 if (FRAME_WINDOW_SIZES_CHANGED (f))
6676 {
6677 Lisp_Object functions;
6678
6679 /* Clear flag first in case we get an error below. */
6680 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6681 functions = Vwindow_size_change_functions;
6682 GCPRO2 (tail, functions);
6683
6684 while (CONSP (functions))
6685 {
6686 call1 (XCAR (functions), frame);
6687 functions = XCDR (functions);
6688 }
6689 UNGCPRO;
6690 }
6691
6692 GCPRO1 (tail);
6693 update_menu_bar (f, 0);
6694 #ifdef HAVE_WINDOW_SYSTEM
6695 update_tool_bar (f, 0);
6696 #endif
6697 UNGCPRO;
6698 }
6699
6700 unbind_to (count, Qnil);
6701 }
6702 else
6703 {
6704 struct frame *sf = SELECTED_FRAME ();
6705 update_menu_bar (sf, 1);
6706 #ifdef HAVE_WINDOW_SYSTEM
6707 update_tool_bar (sf, 1);
6708 #endif
6709 }
6710
6711 /* Motif needs this. See comment in xmenu.c. Turn it off when
6712 pending_menu_activation is not defined. */
6713 #ifdef USE_X_TOOLKIT
6714 pending_menu_activation = 0;
6715 #endif
6716 }
6717
6718
6719 /* Update the menu bar item list for frame F. This has to be done
6720 before we start to fill in any display lines, because it can call
6721 eval.
6722
6723 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6724
6725 static void
6726 update_menu_bar (f, save_match_data)
6727 struct frame *f;
6728 int save_match_data;
6729 {
6730 Lisp_Object window;
6731 register struct window *w;
6732
6733 window = FRAME_SELECTED_WINDOW (f);
6734 w = XWINDOW (window);
6735
6736 if (update_mode_lines)
6737 w->update_mode_line = Qt;
6738
6739 if (FRAME_WINDOW_P (f)
6740 ?
6741 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6742 FRAME_EXTERNAL_MENU_BAR (f)
6743 #else
6744 FRAME_MENU_BAR_LINES (f) > 0
6745 #endif
6746 : FRAME_MENU_BAR_LINES (f) > 0)
6747 {
6748 /* If the user has switched buffers or windows, we need to
6749 recompute to reflect the new bindings. But we'll
6750 recompute when update_mode_lines is set too; that means
6751 that people can use force-mode-line-update to request
6752 that the menu bar be recomputed. The adverse effect on
6753 the rest of the redisplay algorithm is about the same as
6754 windows_or_buffers_changed anyway. */
6755 if (windows_or_buffers_changed
6756 || !NILP (w->update_mode_line)
6757 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6758 < BUF_MODIFF (XBUFFER (w->buffer)))
6759 != !NILP (w->last_had_star))
6760 || ((!NILP (Vtransient_mark_mode)
6761 && !NILP (XBUFFER (w->buffer)->mark_active))
6762 != !NILP (w->region_showing)))
6763 {
6764 struct buffer *prev = current_buffer;
6765 int count = specpdl_ptr - specpdl;
6766
6767 set_buffer_internal_1 (XBUFFER (w->buffer));
6768 if (save_match_data)
6769 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6770 if (NILP (Voverriding_local_map_menu_flag))
6771 {
6772 specbind (Qoverriding_terminal_local_map, Qnil);
6773 specbind (Qoverriding_local_map, Qnil);
6774 }
6775
6776 /* Run the Lucid hook. */
6777 call1 (Vrun_hooks, Qactivate_menubar_hook);
6778
6779 /* If it has changed current-menubar from previous value,
6780 really recompute the menu-bar from the value. */
6781 if (! NILP (Vlucid_menu_bar_dirty_flag))
6782 call0 (Qrecompute_lucid_menubar);
6783
6784 safe_run_hooks (Qmenu_bar_update_hook);
6785 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
6786
6787 /* Redisplay the menu bar in case we changed it. */
6788 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6789 if (FRAME_WINDOW_P (f)
6790 #if defined (macintosh)
6791 /* All frames on Mac OS share the same menubar. So only the
6792 selected frame should be allowed to set it. */
6793 && f == SELECTED_FRAME ()
6794 #endif
6795 )
6796 set_frame_menubar (f, 0, 0);
6797 else
6798 /* On a terminal screen, the menu bar is an ordinary screen
6799 line, and this makes it get updated. */
6800 w->update_mode_line = Qt;
6801 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6802 /* In the non-toolkit version, the menu bar is an ordinary screen
6803 line, and this makes it get updated. */
6804 w->update_mode_line = Qt;
6805 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
6806
6807 unbind_to (count, Qnil);
6808 set_buffer_internal_1 (prev);
6809 }
6810 }
6811 }
6812
6813
6814 \f
6815 /***********************************************************************
6816 Tool-bars
6817 ***********************************************************************/
6818
6819 #ifdef HAVE_WINDOW_SYSTEM
6820
6821 /* Update the tool-bar item list for frame F. This has to be done
6822 before we start to fill in any display lines. Called from
6823 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
6824 and restore it here. */
6825
6826 static void
6827 update_tool_bar (f, save_match_data)
6828 struct frame *f;
6829 int save_match_data;
6830 {
6831 if (WINDOWP (f->tool_bar_window)
6832 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
6833 {
6834 Lisp_Object window;
6835 struct window *w;
6836
6837 window = FRAME_SELECTED_WINDOW (f);
6838 w = XWINDOW (window);
6839
6840 /* If the user has switched buffers or windows, we need to
6841 recompute to reflect the new bindings. But we'll
6842 recompute when update_mode_lines is set too; that means
6843 that people can use force-mode-line-update to request
6844 that the menu bar be recomputed. The adverse effect on
6845 the rest of the redisplay algorithm is about the same as
6846 windows_or_buffers_changed anyway. */
6847 if (windows_or_buffers_changed
6848 || !NILP (w->update_mode_line)
6849 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6850 < BUF_MODIFF (XBUFFER (w->buffer)))
6851 != !NILP (w->last_had_star))
6852 || ((!NILP (Vtransient_mark_mode)
6853 && !NILP (XBUFFER (w->buffer)->mark_active))
6854 != !NILP (w->region_showing)))
6855 {
6856 struct buffer *prev = current_buffer;
6857 int count = specpdl_ptr - specpdl;
6858
6859 /* Set current_buffer to the buffer of the selected
6860 window of the frame, so that we get the right local
6861 keymaps. */
6862 set_buffer_internal_1 (XBUFFER (w->buffer));
6863
6864 /* Save match data, if we must. */
6865 if (save_match_data)
6866 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6867
6868 /* Make sure that we don't accidentally use bogus keymaps. */
6869 if (NILP (Voverriding_local_map_menu_flag))
6870 {
6871 specbind (Qoverriding_terminal_local_map, Qnil);
6872 specbind (Qoverriding_local_map, Qnil);
6873 }
6874
6875 /* Build desired tool-bar items from keymaps. */
6876 f->desired_tool_bar_items
6877 = tool_bar_items (f->desired_tool_bar_items,
6878 &f->n_desired_tool_bar_items);
6879
6880 /* Redisplay the tool-bar in case we changed it. */
6881 w->update_mode_line = Qt;
6882
6883 unbind_to (count, Qnil);
6884 set_buffer_internal_1 (prev);
6885 }
6886 }
6887 }
6888
6889
6890 /* Set F->desired_tool_bar_string to a Lisp string representing frame
6891 F's desired tool-bar contents. F->desired_tool_bar_items must have
6892 been set up previously by calling prepare_menu_bars. */
6893
6894 static void
6895 build_desired_tool_bar_string (f)
6896 struct frame *f;
6897 {
6898 int i, size, size_needed, string_idx;
6899 struct gcpro gcpro1, gcpro2, gcpro3;
6900 Lisp_Object image, plist, props;
6901
6902 image = plist = props = Qnil;
6903 GCPRO3 (image, plist, props);
6904
6905 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
6906 Otherwise, make a new string. */
6907
6908 /* The size of the string we might be able to reuse. */
6909 size = (STRINGP (f->desired_tool_bar_string)
6910 ? XSTRING (f->desired_tool_bar_string)->size
6911 : 0);
6912
6913 /* Each image in the string we build is preceded by a space,
6914 and there is a space at the end. */
6915 size_needed = f->n_desired_tool_bar_items + 1;
6916
6917 /* Reuse f->desired_tool_bar_string, if possible. */
6918 if (size < size_needed)
6919 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
6920 make_number (' '));
6921 else
6922 {
6923 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
6924 Fremove_text_properties (make_number (0), make_number (size),
6925 props, f->desired_tool_bar_string);
6926 }
6927
6928 /* Put a `display' property on the string for the images to display,
6929 put a `menu_item' property on tool-bar items with a value that
6930 is the index of the item in F's tool-bar item vector. */
6931 for (i = 0, string_idx = 0;
6932 i < f->n_desired_tool_bar_items;
6933 ++i, string_idx += 1)
6934 {
6935 #define PROP(IDX) \
6936 (XVECTOR (f->desired_tool_bar_items) \
6937 ->contents[i * TOOL_BAR_ITEM_NSLOTS + (IDX)])
6938
6939 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
6940 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
6941 int margin, relief, idx;
6942 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
6943 extern Lisp_Object Qlaplace;
6944
6945 /* If image is a vector, choose the image according to the
6946 button state. */
6947 image = PROP (TOOL_BAR_ITEM_IMAGES);
6948 if (VECTORP (image))
6949 {
6950 if (enabled_p)
6951 idx = (selected_p
6952 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
6953 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
6954 else
6955 idx = (selected_p
6956 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
6957 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
6958
6959 xassert (ASIZE (image) >= idx);
6960 image = AREF (image, idx);
6961 }
6962 else
6963 idx = -1;
6964
6965 /* Ignore invalid image specifications. */
6966 if (!valid_image_p (image))
6967 continue;
6968
6969 /* Display the tool-bar button pressed, or depressed. */
6970 plist = Fcopy_sequence (XCDR (image));
6971
6972 /* Compute margin and relief to draw. */
6973 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
6974 margin = relief + max (0, tool_bar_button_margin);
6975
6976 if (auto_raise_tool_bar_buttons_p)
6977 {
6978 /* Add a `:relief' property to the image spec if the item is
6979 selected. */
6980 if (selected_p)
6981 {
6982 plist = Fplist_put (plist, QCrelief, make_number (-relief));
6983 margin -= relief;
6984 }
6985 }
6986 else
6987 {
6988 /* If image is selected, display it pressed, i.e. with a
6989 negative relief. If it's not selected, display it with a
6990 raised relief. */
6991 plist = Fplist_put (plist, QCrelief,
6992 (selected_p
6993 ? make_number (-relief)
6994 : make_number (relief)));
6995 margin -= relief;
6996 }
6997
6998 /* Put a margin around the image. */
6999 if (margin)
7000 plist = Fplist_put (plist, QCmargin, make_number (margin));
7001
7002 /* If button is not enabled, and we don't have special images
7003 for the disabled state, make the image appear disabled by
7004 applying an appropriate algorithm to it. */
7005 if (!enabled_p && idx < 0)
7006 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7007
7008 /* Put a `display' text property on the string for the image to
7009 display. Put a `menu-item' property on the string that gives
7010 the start of this item's properties in the tool-bar items
7011 vector. */
7012 image = Fcons (Qimage, plist);
7013 props = list4 (Qdisplay, image,
7014 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7015 Fadd_text_properties (make_number (string_idx),
7016 make_number (string_idx + 1),
7017 props, f->desired_tool_bar_string);
7018 #undef PROP
7019 }
7020
7021 UNGCPRO;
7022 }
7023
7024
7025 /* Display one line of the tool-bar of frame IT->f. */
7026
7027 static void
7028 display_tool_bar_line (it)
7029 struct it *it;
7030 {
7031 struct glyph_row *row = it->glyph_row;
7032 int max_x = it->last_visible_x;
7033 struct glyph *last;
7034
7035 prepare_desired_row (row);
7036 row->y = it->current_y;
7037
7038 while (it->current_x < max_x)
7039 {
7040 int x_before, x, n_glyphs_before, i, nglyphs;
7041
7042 /* Get the next display element. */
7043 if (!get_next_display_element (it))
7044 break;
7045
7046 /* Produce glyphs. */
7047 x_before = it->current_x;
7048 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7049 PRODUCE_GLYPHS (it);
7050
7051 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7052 i = 0;
7053 x = x_before;
7054 while (i < nglyphs)
7055 {
7056 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7057
7058 if (x + glyph->pixel_width > max_x)
7059 {
7060 /* Glyph doesn't fit on line. */
7061 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7062 it->current_x = x;
7063 goto out;
7064 }
7065
7066 ++it->hpos;
7067 x += glyph->pixel_width;
7068 ++i;
7069 }
7070
7071 /* Stop at line ends. */
7072 if (ITERATOR_AT_END_OF_LINE_P (it))
7073 break;
7074
7075 set_iterator_to_next (it, 1);
7076 }
7077
7078 out:;
7079
7080 row->displays_text_p = row->used[TEXT_AREA] != 0;
7081 extend_face_to_end_of_line (it);
7082 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7083 last->right_box_line_p = 1;
7084 compute_line_metrics (it);
7085
7086 /* If line is empty, make it occupy the rest of the tool-bar. */
7087 if (!row->displays_text_p)
7088 {
7089 row->height = row->phys_height = it->last_visible_y - row->y;
7090 row->ascent = row->phys_ascent = 0;
7091 }
7092
7093 row->full_width_p = 1;
7094 row->continued_p = 0;
7095 row->truncated_on_left_p = 0;
7096 row->truncated_on_right_p = 0;
7097
7098 it->current_x = it->hpos = 0;
7099 it->current_y += row->height;
7100 ++it->vpos;
7101 ++it->glyph_row;
7102 }
7103
7104
7105 /* Value is the number of screen lines needed to make all tool-bar
7106 items of frame F visible. */
7107
7108 static int
7109 tool_bar_lines_needed (f)
7110 struct frame *f;
7111 {
7112 struct window *w = XWINDOW (f->tool_bar_window);
7113 struct it it;
7114
7115 /* Initialize an iterator for iteration over
7116 F->desired_tool_bar_string in the tool-bar window of frame F. */
7117 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7118 it.first_visible_x = 0;
7119 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7120 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7121
7122 while (!ITERATOR_AT_END_P (&it))
7123 {
7124 it.glyph_row = w->desired_matrix->rows;
7125 clear_glyph_row (it.glyph_row);
7126 display_tool_bar_line (&it);
7127 }
7128
7129 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7130 }
7131
7132
7133 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7134 height should be changed. */
7135
7136 static int
7137 redisplay_tool_bar (f)
7138 struct frame *f;
7139 {
7140 struct window *w;
7141 struct it it;
7142 struct glyph_row *row;
7143 int change_height_p = 0;
7144
7145 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7146 do anything. This means you must start with tool-bar-lines
7147 non-zero to get the auto-sizing effect. Or in other words, you
7148 can turn off tool-bars by specifying tool-bar-lines zero. */
7149 if (!WINDOWP (f->tool_bar_window)
7150 || (w = XWINDOW (f->tool_bar_window),
7151 XFASTINT (w->height) == 0))
7152 return 0;
7153
7154 /* Set up an iterator for the tool-bar window. */
7155 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7156 it.first_visible_x = 0;
7157 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7158 row = it.glyph_row;
7159
7160 /* Build a string that represents the contents of the tool-bar. */
7161 build_desired_tool_bar_string (f);
7162 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7163
7164 /* Display as many lines as needed to display all tool-bar items. */
7165 while (it.current_y < it.last_visible_y)
7166 display_tool_bar_line (&it);
7167
7168 /* It doesn't make much sense to try scrolling in the tool-bar
7169 window, so don't do it. */
7170 w->desired_matrix->no_scrolling_p = 1;
7171 w->must_be_updated_p = 1;
7172
7173 if (auto_resize_tool_bars_p)
7174 {
7175 int nlines;
7176
7177 /* If there are blank lines at the end, except for a partially
7178 visible blank line at the end that is smaller than
7179 CANON_Y_UNIT, change the tool-bar's height. */
7180 row = it.glyph_row - 1;
7181 if (!row->displays_text_p
7182 && row->height >= CANON_Y_UNIT (f))
7183 change_height_p = 1;
7184
7185 /* If row displays tool-bar items, but is partially visible,
7186 change the tool-bar's height. */
7187 if (row->displays_text_p
7188 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7189 change_height_p = 1;
7190
7191 /* Resize windows as needed by changing the `tool-bar-lines'
7192 frame parameter. */
7193 if (change_height_p
7194 && (nlines = tool_bar_lines_needed (f),
7195 nlines != XFASTINT (w->height)))
7196 {
7197 extern Lisp_Object Qtool_bar_lines;
7198 Lisp_Object frame;
7199
7200 XSETFRAME (frame, f);
7201 clear_glyph_matrix (w->desired_matrix);
7202 Fmodify_frame_parameters (frame,
7203 Fcons (Fcons (Qtool_bar_lines,
7204 make_number (nlines)),
7205 Qnil));
7206 fonts_changed_p = 1;
7207 }
7208 }
7209
7210 return change_height_p;
7211 }
7212
7213
7214 /* Get information about the tool-bar item which is displayed in GLYPH
7215 on frame F. Return in *PROP_IDX the index where tool-bar item
7216 properties start in F->current_tool_bar_items. Value is zero if
7217 GLYPH doesn't display a tool-bar item. */
7218
7219 int
7220 tool_bar_item_info (f, glyph, prop_idx)
7221 struct frame *f;
7222 struct glyph *glyph;
7223 int *prop_idx;
7224 {
7225 Lisp_Object prop;
7226 int success_p;
7227
7228 /* Get the text property `menu-item' at pos. The value of that
7229 property is the start index of this item's properties in
7230 F->current_tool_bar_items. */
7231 prop = Fget_text_property (make_number (glyph->charpos),
7232 Qmenu_item, f->current_tool_bar_string);
7233 if (INTEGERP (prop))
7234 {
7235 *prop_idx = XINT (prop);
7236 success_p = 1;
7237 }
7238 else
7239 success_p = 0;
7240
7241 return success_p;
7242 }
7243
7244 #endif /* HAVE_WINDOW_SYSTEM */
7245
7246
7247 \f
7248 /************************************************************************
7249 Horizontal scrolling
7250 ************************************************************************/
7251
7252 static int hscroll_window_tree P_ ((Lisp_Object));
7253 static int hscroll_windows P_ ((Lisp_Object));
7254
7255 /* For all leaf windows in the window tree rooted at WINDOW, set their
7256 hscroll value so that PT is (i) visible in the window, and (ii) so
7257 that it is not within a certain margin at the window's left and
7258 right border. Value is non-zero if any window's hscroll has been
7259 changed. */
7260
7261 static int
7262 hscroll_window_tree (window)
7263 Lisp_Object window;
7264 {
7265 int hscrolled_p = 0;
7266
7267 while (WINDOWP (window))
7268 {
7269 struct window *w = XWINDOW (window);
7270
7271 if (WINDOWP (w->hchild))
7272 hscrolled_p |= hscroll_window_tree (w->hchild);
7273 else if (WINDOWP (w->vchild))
7274 hscrolled_p |= hscroll_window_tree (w->vchild);
7275 else if (w->cursor.vpos >= 0)
7276 {
7277 int hscroll_margin, text_area_x, text_area_y;
7278 int text_area_width, text_area_height;
7279 struct glyph_row *current_cursor_row
7280 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7281 struct glyph_row *desired_cursor_row
7282 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7283 struct glyph_row *cursor_row
7284 = (desired_cursor_row->enabled_p
7285 ? desired_cursor_row
7286 : current_cursor_row);
7287
7288 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7289 &text_area_width, &text_area_height);
7290
7291 /* Scroll when cursor is inside this scroll margin. */
7292 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7293
7294 if ((XFASTINT (w->hscroll)
7295 && w->cursor.x < hscroll_margin)
7296 || (cursor_row->enabled_p
7297 && cursor_row->truncated_on_right_p
7298 && (w->cursor.x > text_area_width - hscroll_margin)))
7299 {
7300 struct it it;
7301 int hscroll;
7302 struct buffer *saved_current_buffer;
7303 int pt;
7304
7305 /* Find point in a display of infinite width. */
7306 saved_current_buffer = current_buffer;
7307 current_buffer = XBUFFER (w->buffer);
7308
7309 if (w == XWINDOW (selected_window))
7310 pt = BUF_PT (current_buffer);
7311 else
7312 {
7313 pt = marker_position (w->pointm);
7314 pt = max (BEGV, pt);
7315 pt = min (ZV, pt);
7316 }
7317
7318 /* Move iterator to pt starting at cursor_row->start in
7319 a line with infinite width. */
7320 init_to_row_start (&it, w, cursor_row);
7321 it.last_visible_x = INFINITY;
7322 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7323 current_buffer = saved_current_buffer;
7324
7325 /* Center cursor in window. */
7326 hscroll = (max (0, it.current_x - text_area_width / 2)
7327 / CANON_X_UNIT (it.f));
7328
7329 /* Don't call Fset_window_hscroll if value hasn't
7330 changed because it will prevent redisplay
7331 optimizations. */
7332 if (XFASTINT (w->hscroll) != hscroll)
7333 {
7334 Fset_window_hscroll (window, make_number (hscroll));
7335 hscrolled_p = 1;
7336 }
7337 }
7338 }
7339
7340 window = w->next;
7341 }
7342
7343 /* Value is non-zero if hscroll of any leaf window has been changed. */
7344 return hscrolled_p;
7345 }
7346
7347
7348 /* Set hscroll so that cursor is visible and not inside horizontal
7349 scroll margins for all windows in the tree rooted at WINDOW. See
7350 also hscroll_window_tree above. Value is non-zero if any window's
7351 hscroll has been changed. If it has, desired matrices on the frame
7352 of WINDOW are cleared. */
7353
7354 static int
7355 hscroll_windows (window)
7356 Lisp_Object window;
7357 {
7358 int hscrolled_p;
7359
7360 if (automatic_hscrolling_p)
7361 {
7362 hscrolled_p = hscroll_window_tree (window);
7363 if (hscrolled_p)
7364 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7365 }
7366 else
7367 hscrolled_p = 0;
7368 return hscrolled_p;
7369 }
7370
7371
7372 \f
7373 /************************************************************************
7374 Redisplay
7375 ************************************************************************/
7376
7377 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7378 to a non-zero value. This is sometimes handy to have in a debugger
7379 session. */
7380
7381 #if GLYPH_DEBUG
7382
7383 /* First and last unchanged row for try_window_id. */
7384
7385 int debug_first_unchanged_at_end_vpos;
7386 int debug_last_unchanged_at_beg_vpos;
7387
7388 /* Delta vpos and y. */
7389
7390 int debug_dvpos, debug_dy;
7391
7392 /* Delta in characters and bytes for try_window_id. */
7393
7394 int debug_delta, debug_delta_bytes;
7395
7396 /* Values of window_end_pos and window_end_vpos at the end of
7397 try_window_id. */
7398
7399 int debug_end_pos, debug_end_vpos;
7400
7401 /* Append a string to W->desired_matrix->method. FMT is a printf
7402 format string. A1...A9 are a supplement for a variable-length
7403 argument list. If trace_redisplay_p is non-zero also printf the
7404 resulting string to stderr. */
7405
7406 static void
7407 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7408 struct window *w;
7409 char *fmt;
7410 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7411 {
7412 char buffer[512];
7413 char *method = w->desired_matrix->method;
7414 int len = strlen (method);
7415 int size = sizeof w->desired_matrix->method;
7416 int remaining = size - len - 1;
7417
7418 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7419 if (len && remaining)
7420 {
7421 method[len] = '|';
7422 --remaining, ++len;
7423 }
7424
7425 strncpy (method + len, buffer, remaining);
7426
7427 if (trace_redisplay_p)
7428 fprintf (stderr, "%p (%s): %s\n",
7429 w,
7430 ((BUFFERP (w->buffer)
7431 && STRINGP (XBUFFER (w->buffer)->name))
7432 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7433 : "no buffer"),
7434 buffer);
7435 }
7436
7437 #endif /* GLYPH_DEBUG */
7438
7439
7440 /* This counter is used to clear the face cache every once in a while
7441 in redisplay_internal. It is incremented for each redisplay.
7442 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7443 cleared. */
7444
7445 #define CLEAR_FACE_CACHE_COUNT 10000
7446 static int clear_face_cache_count;
7447
7448 /* Record the previous terminal frame we displayed. */
7449
7450 static struct frame *previous_terminal_frame;
7451
7452 /* Non-zero while redisplay_internal is in progress. */
7453
7454 int redisplaying_p;
7455
7456
7457 /* Value is non-zero if all changes in window W, which displays
7458 current_buffer, are in the text between START and END. START is a
7459 buffer position, END is given as a distance from Z. Used in
7460 redisplay_internal for display optimization. */
7461
7462 static INLINE int
7463 text_outside_line_unchanged_p (w, start, end)
7464 struct window *w;
7465 int start, end;
7466 {
7467 int unchanged_p = 1;
7468
7469 /* If text or overlays have changed, see where. */
7470 if (XFASTINT (w->last_modified) < MODIFF
7471 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7472 {
7473 /* Gap in the line? */
7474 if (GPT < start || Z - GPT < end)
7475 unchanged_p = 0;
7476
7477 /* Changes start in front of the line, or end after it? */
7478 if (unchanged_p
7479 && (BEG_UNCHANGED < start - 1
7480 || END_UNCHANGED < end))
7481 unchanged_p = 0;
7482
7483 /* If selective display, can't optimize if changes start at the
7484 beginning of the line. */
7485 if (unchanged_p
7486 && INTEGERP (current_buffer->selective_display)
7487 && XINT (current_buffer->selective_display) > 0
7488 && (BEG_UNCHANGED < start || GPT <= start))
7489 unchanged_p = 0;
7490 }
7491
7492 return unchanged_p;
7493 }
7494
7495
7496 /* Do a frame update, taking possible shortcuts into account. This is
7497 the main external entry point for redisplay.
7498
7499 If the last redisplay displayed an echo area message and that message
7500 is no longer requested, we clear the echo area or bring back the
7501 mini-buffer if that is in use. */
7502
7503 void
7504 redisplay ()
7505 {
7506 redisplay_internal (0);
7507 }
7508
7509 /* Return 1 if point moved out of or into a composition. Otherwise
7510 return 0. PREV_BUF and PREV_PT are the last point buffer and
7511 position. BUF and PT are the current point buffer and position. */
7512
7513 int
7514 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7515 struct buffer *prev_buf, *buf;
7516 int prev_pt, pt;
7517 {
7518 int start, end;
7519 Lisp_Object prop;
7520 Lisp_Object buffer;
7521
7522 XSETBUFFER (buffer, buf);
7523 /* Check a composition at the last point if point moved within the
7524 same buffer. */
7525 if (prev_buf == buf)
7526 {
7527 if (prev_pt == pt)
7528 /* Point didn't move. */
7529 return 0;
7530
7531 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7532 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7533 && COMPOSITION_VALID_P (start, end, prop)
7534 && start < prev_pt && end > prev_pt)
7535 /* The last point was within the composition. Return 1 iff
7536 point moved out of the composition. */
7537 return (pt <= start || pt >= end);
7538 }
7539
7540 /* Check a composition at the current point. */
7541 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7542 && find_composition (pt, -1, &start, &end, &prop, buffer)
7543 && COMPOSITION_VALID_P (start, end, prop)
7544 && start < pt && end > pt);
7545 }
7546
7547 /* Reconsider the setting of B->clip_changed which is displayed
7548 in window W. */
7549
7550 static INLINE void
7551 reconsider_clip_changes (w, b)
7552 struct window *w;
7553 struct buffer *b;
7554 {
7555 if (b->prevent_redisplay_optimizations_p)
7556 b->clip_changed = 1;
7557 else if (b->clip_changed
7558 && !NILP (w->window_end_valid)
7559 && w->current_matrix->buffer == b
7560 && w->current_matrix->zv == BUF_ZV (b)
7561 && w->current_matrix->begv == BUF_BEGV (b))
7562 b->clip_changed = 0;
7563
7564 /* If display wasn't paused, and W is not a tool bar window, see if
7565 point has been moved into or out of a composition. In that case,
7566 we set b->clip_changed to 1 to force updating the screen. If
7567 b->clip_changed has already been set to 1, we can skip this
7568 check. */
7569 if (!b->clip_changed
7570 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7571 {
7572 int pt;
7573
7574 if (w == XWINDOW (selected_window))
7575 pt = BUF_PT (current_buffer);
7576 else
7577 pt = marker_position (w->pointm);
7578
7579 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7580 || pt != XINT (w->last_point))
7581 && check_point_in_composition (w->current_matrix->buffer,
7582 XINT (w->last_point),
7583 XBUFFER (w->buffer), pt))
7584 b->clip_changed = 1;
7585 }
7586 }
7587
7588
7589 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7590 response to any user action; therefore, we should preserve the echo
7591 area. (Actually, our caller does that job.) Perhaps in the future
7592 avoid recentering windows if it is not necessary; currently that
7593 causes some problems. */
7594
7595 static void
7596 redisplay_internal (preserve_echo_area)
7597 int preserve_echo_area;
7598 {
7599 struct window *w = XWINDOW (selected_window);
7600 struct frame *f = XFRAME (w->frame);
7601 int pause;
7602 int must_finish = 0;
7603 struct text_pos tlbufpos, tlendpos;
7604 int number_of_visible_frames;
7605 int count;
7606 struct frame *sf = SELECTED_FRAME ();
7607
7608 /* Non-zero means redisplay has to consider all windows on all
7609 frames. Zero means, only selected_window is considered. */
7610 int consider_all_windows_p;
7611
7612 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7613
7614 /* No redisplay if running in batch mode or frame is not yet fully
7615 initialized, or redisplay is explicitly turned off by setting
7616 Vinhibit_redisplay. */
7617 if (noninteractive
7618 || !NILP (Vinhibit_redisplay)
7619 || !f->glyphs_initialized_p)
7620 return;
7621
7622 /* The flag redisplay_performed_directly_p is set by
7623 direct_output_for_insert when it already did the whole screen
7624 update necessary. */
7625 if (redisplay_performed_directly_p)
7626 {
7627 redisplay_performed_directly_p = 0;
7628 if (!hscroll_windows (selected_window))
7629 return;
7630 }
7631
7632 #ifdef USE_X_TOOLKIT
7633 if (popup_activated ())
7634 return;
7635 #endif
7636
7637 /* I don't think this happens but let's be paranoid. */
7638 if (redisplaying_p)
7639 return;
7640
7641 /* Record a function that resets redisplaying_p to its old value
7642 when we leave this function. */
7643 count = specpdl_ptr - specpdl;
7644 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7645 ++redisplaying_p;
7646
7647 retry:
7648 pause = 0;
7649 reconsider_clip_changes (w, current_buffer);
7650
7651 /* If new fonts have been loaded that make a glyph matrix adjustment
7652 necessary, do it. */
7653 if (fonts_changed_p)
7654 {
7655 adjust_glyphs (NULL);
7656 ++windows_or_buffers_changed;
7657 fonts_changed_p = 0;
7658 }
7659
7660 if (! FRAME_WINDOW_P (sf)
7661 && previous_terminal_frame != sf)
7662 {
7663 /* Since frames on an ASCII terminal share the same display
7664 area, displaying a different frame means redisplay the whole
7665 thing. */
7666 windows_or_buffers_changed++;
7667 SET_FRAME_GARBAGED (sf);
7668 XSETFRAME (Vterminal_frame, sf);
7669 }
7670 previous_terminal_frame = sf;
7671
7672 /* Set the visible flags for all frames. Do this before checking
7673 for resized or garbaged frames; they want to know if their frames
7674 are visible. See the comment in frame.h for
7675 FRAME_SAMPLE_VISIBILITY. */
7676 {
7677 Lisp_Object tail, frame;
7678
7679 number_of_visible_frames = 0;
7680
7681 FOR_EACH_FRAME (tail, frame)
7682 {
7683 struct frame *f = XFRAME (frame);
7684
7685 FRAME_SAMPLE_VISIBILITY (f);
7686 if (FRAME_VISIBLE_P (f))
7687 ++number_of_visible_frames;
7688 clear_desired_matrices (f);
7689 }
7690 }
7691
7692 /* Notice any pending interrupt request to change frame size. */
7693 do_pending_window_change (1);
7694
7695 /* Clear frames marked as garbaged. */
7696 if (frame_garbaged)
7697 clear_garbaged_frames ();
7698
7699 /* Build menubar and tool-bar items. */
7700 prepare_menu_bars ();
7701
7702 if (windows_or_buffers_changed)
7703 update_mode_lines++;
7704
7705 /* Detect case that we need to write or remove a star in the mode line. */
7706 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7707 {
7708 w->update_mode_line = Qt;
7709 if (buffer_shared > 1)
7710 update_mode_lines++;
7711 }
7712
7713 /* If %c is in the mode line, update it if needed. */
7714 if (!NILP (w->column_number_displayed)
7715 /* This alternative quickly identifies a common case
7716 where no change is needed. */
7717 && !(PT == XFASTINT (w->last_point)
7718 && XFASTINT (w->last_modified) >= MODIFF
7719 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7720 && XFASTINT (w->column_number_displayed) != current_column ())
7721 w->update_mode_line = Qt;
7722
7723 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7724
7725 /* The variable buffer_shared is set in redisplay_window and
7726 indicates that we redisplay a buffer in different windows. See
7727 there. */
7728 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7729
7730 /* If specs for an arrow have changed, do thorough redisplay
7731 to ensure we remove any arrow that should no longer exist. */
7732 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7733 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7734 consider_all_windows_p = windows_or_buffers_changed = 1;
7735
7736 /* Normally the message* functions will have already displayed and
7737 updated the echo area, but the frame may have been trashed, or
7738 the update may have been preempted, so display the echo area
7739 again here. Checking both message buffers captures the case that
7740 the echo area should be cleared. */
7741 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7742 {
7743 int window_height_changed_p = echo_area_display (0);
7744 must_finish = 1;
7745
7746 if (fonts_changed_p)
7747 goto retry;
7748 else if (window_height_changed_p)
7749 {
7750 consider_all_windows_p = 1;
7751 ++update_mode_lines;
7752 ++windows_or_buffers_changed;
7753
7754 /* If window configuration was changed, frames may have been
7755 marked garbaged. Clear them or we will experience
7756 surprises wrt scrolling. */
7757 if (frame_garbaged)
7758 clear_garbaged_frames ();
7759 }
7760 }
7761 else if (EQ (selected_window, minibuf_window)
7762 && (current_buffer->clip_changed
7763 || XFASTINT (w->last_modified) < MODIFF
7764 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7765 && resize_mini_window (w, 0))
7766 {
7767 /* Resized active mini-window to fit the size of what it is
7768 showing if its contents might have changed. */
7769 must_finish = 1;
7770 consider_all_windows_p = 1;
7771 ++windows_or_buffers_changed;
7772 ++update_mode_lines;
7773
7774 /* If window configuration was changed, frames may have been
7775 marked garbaged. Clear them or we will experience
7776 surprises wrt scrolling. */
7777 if (frame_garbaged)
7778 clear_garbaged_frames ();
7779 }
7780
7781
7782 /* If showing the region, and mark has changed, we must redisplay
7783 the whole window. The assignment to this_line_start_pos prevents
7784 the optimization directly below this if-statement. */
7785 if (((!NILP (Vtransient_mark_mode)
7786 && !NILP (XBUFFER (w->buffer)->mark_active))
7787 != !NILP (w->region_showing))
7788 || (!NILP (w->region_showing)
7789 && !EQ (w->region_showing,
7790 Fmarker_position (XBUFFER (w->buffer)->mark))))
7791 CHARPOS (this_line_start_pos) = 0;
7792
7793 /* Optimize the case that only the line containing the cursor in the
7794 selected window has changed. Variables starting with this_ are
7795 set in display_line and record information about the line
7796 containing the cursor. */
7797 tlbufpos = this_line_start_pos;
7798 tlendpos = this_line_end_pos;
7799 if (!consider_all_windows_p
7800 && CHARPOS (tlbufpos) > 0
7801 && NILP (w->update_mode_line)
7802 && !current_buffer->clip_changed
7803 && FRAME_VISIBLE_P (XFRAME (w->frame))
7804 && !FRAME_OBSCURED_P (XFRAME (w->frame))
7805 /* Make sure recorded data applies to current buffer, etc. */
7806 && this_line_buffer == current_buffer
7807 && current_buffer == XBUFFER (w->buffer)
7808 && NILP (w->force_start)
7809 /* Point must be on the line that we have info recorded about. */
7810 && PT >= CHARPOS (tlbufpos)
7811 && PT <= Z - CHARPOS (tlendpos)
7812 /* All text outside that line, including its final newline,
7813 must be unchanged */
7814 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
7815 CHARPOS (tlendpos)))
7816 {
7817 if (CHARPOS (tlbufpos) > BEGV
7818 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
7819 && (CHARPOS (tlbufpos) == ZV
7820 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
7821 /* Former continuation line has disappeared by becoming empty */
7822 goto cancel;
7823 else if (XFASTINT (w->last_modified) < MODIFF
7824 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
7825 || MINI_WINDOW_P (w))
7826 {
7827 /* We have to handle the case of continuation around a
7828 wide-column character (See the comment in indent.c around
7829 line 885).
7830
7831 For instance, in the following case:
7832
7833 -------- Insert --------
7834 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
7835 J_I_ ==> J_I_ `^^' are cursors.
7836 ^^ ^^
7837 -------- --------
7838
7839 As we have to redraw the line above, we should goto cancel. */
7840
7841 struct it it;
7842 int line_height_before = this_line_pixel_height;
7843
7844 /* Note that start_display will handle the case that the
7845 line starting at tlbufpos is a continuation lines. */
7846 start_display (&it, w, tlbufpos);
7847
7848 /* Implementation note: It this still necessary? */
7849 if (it.current_x != this_line_start_x)
7850 goto cancel;
7851
7852 TRACE ((stderr, "trying display optimization 1\n"));
7853 w->cursor.vpos = -1;
7854 overlay_arrow_seen = 0;
7855 it.vpos = this_line_vpos;
7856 it.current_y = this_line_y;
7857 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
7858 display_line (&it);
7859
7860 /* If line contains point, is not continued,
7861 and ends at same distance from eob as before, we win */
7862 if (w->cursor.vpos >= 0
7863 /* Line is not continued, otherwise this_line_start_pos
7864 would have been set to 0 in display_line. */
7865 && CHARPOS (this_line_start_pos)
7866 /* Line ends as before. */
7867 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
7868 /* Line has same height as before. Otherwise other lines
7869 would have to be shifted up or down. */
7870 && this_line_pixel_height == line_height_before)
7871 {
7872 /* If this is not the window's last line, we must adjust
7873 the charstarts of the lines below. */
7874 if (it.current_y < it.last_visible_y)
7875 {
7876 struct glyph_row *row
7877 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
7878 int delta, delta_bytes;
7879
7880 if (Z - CHARPOS (tlendpos) == ZV)
7881 {
7882 /* This line ends at end of (accessible part of)
7883 buffer. There is no newline to count. */
7884 delta = (Z
7885 - CHARPOS (tlendpos)
7886 - MATRIX_ROW_START_CHARPOS (row));
7887 delta_bytes = (Z_BYTE
7888 - BYTEPOS (tlendpos)
7889 - MATRIX_ROW_START_BYTEPOS (row));
7890 }
7891 else
7892 {
7893 /* This line ends in a newline. Must take
7894 account of the newline and the rest of the
7895 text that follows. */
7896 delta = (Z
7897 - CHARPOS (tlendpos)
7898 - MATRIX_ROW_START_CHARPOS (row));
7899 delta_bytes = (Z_BYTE
7900 - BYTEPOS (tlendpos)
7901 - MATRIX_ROW_START_BYTEPOS (row));
7902 }
7903
7904 increment_matrix_positions (w->current_matrix,
7905 this_line_vpos + 1,
7906 w->current_matrix->nrows,
7907 delta, delta_bytes);
7908 }
7909
7910 /* If this row displays text now but previously didn't,
7911 or vice versa, w->window_end_vpos may have to be
7912 adjusted. */
7913 if ((it.glyph_row - 1)->displays_text_p)
7914 {
7915 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
7916 XSETINT (w->window_end_vpos, this_line_vpos);
7917 }
7918 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
7919 && this_line_vpos > 0)
7920 XSETINT (w->window_end_vpos, this_line_vpos - 1);
7921 w->window_end_valid = Qnil;
7922
7923 /* Update hint: No need to try to scroll in update_window. */
7924 w->desired_matrix->no_scrolling_p = 1;
7925
7926 #if GLYPH_DEBUG
7927 *w->desired_matrix->method = 0;
7928 debug_method_add (w, "optimization 1");
7929 #endif
7930 goto update;
7931 }
7932 else
7933 goto cancel;
7934 }
7935 else if (/* Cursor position hasn't changed. */
7936 PT == XFASTINT (w->last_point)
7937 /* Make sure the cursor was last displayed
7938 in this window. Otherwise we have to reposition it. */
7939 && 0 <= w->cursor.vpos
7940 && XINT (w->height) > w->cursor.vpos)
7941 {
7942 if (!must_finish)
7943 {
7944 do_pending_window_change (1);
7945
7946 /* We used to always goto end_of_redisplay here, but this
7947 isn't enough if we have a blinking cursor. */
7948 if (w->cursor_off_p == w->last_cursor_off_p)
7949 goto end_of_redisplay;
7950 }
7951 goto update;
7952 }
7953 /* If highlighting the region, or if the cursor is in the echo area,
7954 then we can't just move the cursor. */
7955 else if (! (!NILP (Vtransient_mark_mode)
7956 && !NILP (current_buffer->mark_active))
7957 && (EQ (selected_window, current_buffer->last_selected_window)
7958 || highlight_nonselected_windows)
7959 && NILP (w->region_showing)
7960 && NILP (Vshow_trailing_whitespace)
7961 && !cursor_in_echo_area)
7962 {
7963 struct it it;
7964 struct glyph_row *row;
7965
7966 /* Skip from tlbufpos to PT and see where it is. Note that
7967 PT may be in invisible text. If so, we will end at the
7968 next visible position. */
7969 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
7970 NULL, DEFAULT_FACE_ID);
7971 it.current_x = this_line_start_x;
7972 it.current_y = this_line_y;
7973 it.vpos = this_line_vpos;
7974
7975 /* The call to move_it_to stops in front of PT, but
7976 moves over before-strings. */
7977 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
7978
7979 if (it.vpos == this_line_vpos
7980 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
7981 row->enabled_p))
7982 {
7983 xassert (this_line_vpos == it.vpos);
7984 xassert (this_line_y == it.current_y);
7985 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
7986 goto update;
7987 }
7988 else
7989 goto cancel;
7990 }
7991
7992 cancel:
7993 /* Text changed drastically or point moved off of line. */
7994 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
7995 }
7996
7997 CHARPOS (this_line_start_pos) = 0;
7998 consider_all_windows_p |= buffer_shared > 1;
7999 ++clear_face_cache_count;
8000
8001
8002 /* Build desired matrices, and update the display. If
8003 consider_all_windows_p is non-zero, do it for all windows on all
8004 frames. Otherwise do it for selected_window, only. */
8005
8006 if (consider_all_windows_p)
8007 {
8008 Lisp_Object tail, frame;
8009
8010 /* Clear the face cache eventually. */
8011 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8012 {
8013 clear_face_cache (0);
8014 clear_face_cache_count = 0;
8015 }
8016
8017 /* Recompute # windows showing selected buffer. This will be
8018 incremented each time such a window is displayed. */
8019 buffer_shared = 0;
8020
8021 FOR_EACH_FRAME (tail, frame)
8022 {
8023 struct frame *f = XFRAME (frame);
8024
8025 if (FRAME_WINDOW_P (f) || f == sf)
8026 {
8027 /* Mark all the scroll bars to be removed; we'll redeem
8028 the ones we want when we redisplay their windows. */
8029 if (condemn_scroll_bars_hook)
8030 (*condemn_scroll_bars_hook) (f);
8031
8032 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8033 redisplay_windows (FRAME_ROOT_WINDOW (f));
8034
8035 /* Any scroll bars which redisplay_windows should have
8036 nuked should now go away. */
8037 if (judge_scroll_bars_hook)
8038 (*judge_scroll_bars_hook) (f);
8039
8040 /* If fonts changed, display again. */
8041 if (fonts_changed_p)
8042 goto retry;
8043
8044 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8045 {
8046 /* See if we have to hscroll. */
8047 if (hscroll_windows (f->root_window))
8048 goto retry;
8049
8050 /* Prevent various kinds of signals during display
8051 update. stdio is not robust about handling
8052 signals, which can cause an apparent I/O
8053 error. */
8054 if (interrupt_input)
8055 unrequest_sigio ();
8056 stop_polling ();
8057
8058 /* Update the display. */
8059 set_window_update_flags (XWINDOW (f->root_window), 1);
8060 pause |= update_frame (f, 0, 0);
8061 if (pause)
8062 break;
8063
8064 mark_window_display_accurate (f->root_window, 1);
8065 if (frame_up_to_date_hook)
8066 frame_up_to_date_hook (f);
8067 }
8068 }
8069 }
8070 }
8071 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8072 {
8073 Lisp_Object mini_window;
8074 struct frame *mini_frame;
8075
8076 redisplay_window (selected_window, 1);
8077
8078 /* Compare desired and current matrices, perform output. */
8079 update:
8080
8081 /* If fonts changed, display again. */
8082 if (fonts_changed_p)
8083 goto retry;
8084
8085 /* Prevent various kinds of signals during display update.
8086 stdio is not robust about handling signals,
8087 which can cause an apparent I/O error. */
8088 if (interrupt_input)
8089 unrequest_sigio ();
8090 stop_polling ();
8091
8092 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8093 {
8094 if (hscroll_windows (selected_window))
8095 goto retry;
8096
8097 XWINDOW (selected_window)->must_be_updated_p = 1;
8098 pause = update_frame (sf, 0, 0);
8099 }
8100
8101 /* We may have called echo_area_display at the top of this
8102 function. If the echo area is on another frame, that may
8103 have put text on a frame other than the selected one, so the
8104 above call to update_frame would not have caught it. Catch
8105 it here. */
8106 mini_window = FRAME_MINIBUF_WINDOW (sf);
8107 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8108
8109 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8110 {
8111 XWINDOW (mini_window)->must_be_updated_p = 1;
8112 pause |= update_frame (mini_frame, 0, 0);
8113 if (!pause && hscroll_windows (mini_window))
8114 goto retry;
8115 }
8116 }
8117
8118 /* If display was paused because of pending input, make sure we do a
8119 thorough update the next time. */
8120 if (pause)
8121 {
8122 /* Prevent the optimization at the beginning of
8123 redisplay_internal that tries a single-line update of the
8124 line containing the cursor in the selected window. */
8125 CHARPOS (this_line_start_pos) = 0;
8126
8127 /* Let the overlay arrow be updated the next time. */
8128 if (!NILP (last_arrow_position))
8129 {
8130 last_arrow_position = Qt;
8131 last_arrow_string = Qt;
8132 }
8133
8134 /* If we pause after scrolling, some rows in the current
8135 matrices of some windows are not valid. */
8136 if (!WINDOW_FULL_WIDTH_P (w)
8137 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8138 update_mode_lines = 1;
8139 }
8140
8141 /* Now text on frame agrees with windows, so put info into the
8142 windows for partial redisplay to follow. */
8143 if (!pause)
8144 {
8145 register struct buffer *b = XBUFFER (w->buffer);
8146
8147 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8148 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8149 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8150 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8151
8152 if (consider_all_windows_p)
8153 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8154 else
8155 {
8156 XSETFASTINT (w->last_point, BUF_PT (b));
8157 w->last_cursor = w->cursor;
8158 w->last_cursor_off_p = w->cursor_off_p;
8159
8160 b->clip_changed = 0;
8161 b->prevent_redisplay_optimizations_p = 0;
8162 w->update_mode_line = Qnil;
8163 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8164 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8165 w->last_had_star
8166 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8167 ? Qt : Qnil);
8168
8169 /* Record if we are showing a region, so can make sure to
8170 update it fully at next redisplay. */
8171 w->region_showing = (!NILP (Vtransient_mark_mode)
8172 && (EQ (selected_window,
8173 current_buffer->last_selected_window)
8174 || highlight_nonselected_windows)
8175 && !NILP (XBUFFER (w->buffer)->mark_active)
8176 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8177 : Qnil);
8178
8179 w->window_end_valid = w->buffer;
8180 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8181 last_arrow_string = Voverlay_arrow_string;
8182 if (frame_up_to_date_hook != 0)
8183 (*frame_up_to_date_hook) (sf);
8184
8185 w->current_matrix->buffer = b;
8186 w->current_matrix->begv = BUF_BEGV (b);
8187 w->current_matrix->zv = BUF_ZV (b);
8188 }
8189
8190 update_mode_lines = 0;
8191 windows_or_buffers_changed = 0;
8192 }
8193
8194 /* Start SIGIO interrupts coming again. Having them off during the
8195 code above makes it less likely one will discard output, but not
8196 impossible, since there might be stuff in the system buffer here.
8197 But it is much hairier to try to do anything about that. */
8198 if (interrupt_input)
8199 request_sigio ();
8200 start_polling ();
8201
8202 /* If a frame has become visible which was not before, redisplay
8203 again, so that we display it. Expose events for such a frame
8204 (which it gets when becoming visible) don't call the parts of
8205 redisplay constructing glyphs, so simply exposing a frame won't
8206 display anything in this case. So, we have to display these
8207 frames here explicitly. */
8208 if (!pause)
8209 {
8210 Lisp_Object tail, frame;
8211 int new_count = 0;
8212
8213 FOR_EACH_FRAME (tail, frame)
8214 {
8215 int this_is_visible = 0;
8216
8217 if (XFRAME (frame)->visible)
8218 this_is_visible = 1;
8219 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8220 if (XFRAME (frame)->visible)
8221 this_is_visible = 1;
8222
8223 if (this_is_visible)
8224 new_count++;
8225 }
8226
8227 if (new_count != number_of_visible_frames)
8228 windows_or_buffers_changed++;
8229 }
8230
8231 /* Change frame size now if a change is pending. */
8232 do_pending_window_change (1);
8233
8234 /* If we just did a pending size change, or have additional
8235 visible frames, redisplay again. */
8236 if (windows_or_buffers_changed && !pause)
8237 goto retry;
8238
8239 end_of_redisplay:;
8240
8241 unbind_to (count, Qnil);
8242 }
8243
8244
8245 /* Redisplay, but leave alone any recent echo area message unless
8246 another message has been requested in its place.
8247
8248 This is useful in situations where you need to redisplay but no
8249 user action has occurred, making it inappropriate for the message
8250 area to be cleared. See tracking_off and
8251 wait_reading_process_input for examples of these situations. */
8252
8253 void
8254 redisplay_preserve_echo_area ()
8255 {
8256 if (!NILP (echo_area_buffer[1]))
8257 {
8258 /* We have a previously displayed message, but no current
8259 message. Redisplay the previous message. */
8260 display_last_displayed_message_p = 1;
8261 redisplay_internal (1);
8262 display_last_displayed_message_p = 0;
8263 }
8264 else
8265 redisplay_internal (1);
8266 }
8267
8268
8269 /* Function registered with record_unwind_protect in
8270 redisplay_internal. Clears the flag indicating that a redisplay is
8271 in progress. */
8272
8273 static Lisp_Object
8274 unwind_redisplay (old_redisplaying_p)
8275 Lisp_Object old_redisplaying_p;
8276 {
8277 redisplaying_p = XFASTINT (old_redisplaying_p);
8278 return Qnil;
8279 }
8280
8281
8282 /* Mark the display of windows in the window tree rooted at WINDOW as
8283 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8284 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8285 the next time redisplay_internal is called. */
8286
8287 void
8288 mark_window_display_accurate (window, accurate_p)
8289 Lisp_Object window;
8290 int accurate_p;
8291 {
8292 struct window *w;
8293
8294 for (; !NILP (window); window = w->next)
8295 {
8296 w = XWINDOW (window);
8297
8298 if (BUFFERP (w->buffer))
8299 {
8300 struct buffer *b = XBUFFER (w->buffer);
8301
8302 XSETFASTINT (w->last_modified,
8303 accurate_p ? BUF_MODIFF (b) : 0);
8304 XSETFASTINT (w->last_overlay_modified,
8305 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8306 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8307 ? Qt : Qnil);
8308
8309 #if 0 /* I don't think this is necessary because display_line does it.
8310 Let's check it. */
8311 /* Record if we are showing a region, so can make sure to
8312 update it fully at next redisplay. */
8313 w->region_showing
8314 = (!NILP (Vtransient_mark_mode)
8315 && (w == XWINDOW (current_buffer->last_selected_window)
8316 || highlight_nonselected_windows)
8317 && (!NILP (b->mark_active)
8318 ? Fmarker_position (b->mark)
8319 : Qnil));
8320 #endif
8321
8322 if (accurate_p)
8323 {
8324 b->clip_changed = 0;
8325 b->prevent_redisplay_optimizations_p = 0;
8326 w->current_matrix->buffer = b;
8327 w->current_matrix->begv = BUF_BEGV (b);
8328 w->current_matrix->zv = BUF_ZV (b);
8329 w->last_cursor = w->cursor;
8330 w->last_cursor_off_p = w->cursor_off_p;
8331 if (w == XWINDOW (selected_window))
8332 w->last_point = make_number (BUF_PT (b));
8333 else
8334 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8335 }
8336 }
8337
8338 w->window_end_valid = w->buffer;
8339 w->update_mode_line = Qnil;
8340
8341 if (!NILP (w->vchild))
8342 mark_window_display_accurate (w->vchild, accurate_p);
8343 if (!NILP (w->hchild))
8344 mark_window_display_accurate (w->hchild, accurate_p);
8345 }
8346
8347 if (accurate_p)
8348 {
8349 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8350 last_arrow_string = Voverlay_arrow_string;
8351 }
8352 else
8353 {
8354 /* Force a thorough redisplay the next time by setting
8355 last_arrow_position and last_arrow_string to t, which is
8356 unequal to any useful value of Voverlay_arrow_... */
8357 last_arrow_position = Qt;
8358 last_arrow_string = Qt;
8359 }
8360 }
8361
8362
8363 /* Return value in display table DP (Lisp_Char_Table *) for character
8364 C. Since a display table doesn't have any parent, we don't have to
8365 follow parent. Do not call this function directly but use the
8366 macro DISP_CHAR_VECTOR. */
8367
8368 Lisp_Object
8369 disp_char_vector (dp, c)
8370 struct Lisp_Char_Table *dp;
8371 int c;
8372 {
8373 int code[4], i;
8374 Lisp_Object val;
8375
8376 if (SINGLE_BYTE_CHAR_P (c))
8377 return (dp->contents[c]);
8378
8379 SPLIT_CHAR (c, code[0], code[1], code[2]);
8380 if (code[1] < 32)
8381 code[1] = -1;
8382 else if (code[2] < 32)
8383 code[2] = -1;
8384
8385 /* Here, the possible range of code[0] (== charset ID) is
8386 128..max_charset. Since the top level char table contains data
8387 for multibyte characters after 256th element, we must increment
8388 code[0] by 128 to get a correct index. */
8389 code[0] += 128;
8390 code[3] = -1; /* anchor */
8391
8392 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8393 {
8394 val = dp->contents[code[i]];
8395 if (!SUB_CHAR_TABLE_P (val))
8396 return (NILP (val) ? dp->defalt : val);
8397 }
8398
8399 /* Here, val is a sub char table. We return the default value of
8400 it. */
8401 return (dp->defalt);
8402 }
8403
8404
8405 \f
8406 /***********************************************************************
8407 Window Redisplay
8408 ***********************************************************************/
8409
8410 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8411
8412 static void
8413 redisplay_windows (window)
8414 Lisp_Object window;
8415 {
8416 while (!NILP (window))
8417 {
8418 struct window *w = XWINDOW (window);
8419
8420 if (!NILP (w->hchild))
8421 redisplay_windows (w->hchild);
8422 else if (!NILP (w->vchild))
8423 redisplay_windows (w->vchild);
8424 else
8425 redisplay_window (window, 0);
8426
8427 window = w->next;
8428 }
8429 }
8430
8431
8432 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8433 DELTA is the number of bytes by which positions recorded in ROW
8434 differ from current buffer positions. */
8435
8436 void
8437 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8438 struct window *w;
8439 struct glyph_row *row;
8440 struct glyph_matrix *matrix;
8441 int delta, delta_bytes, dy, dvpos;
8442 {
8443 struct glyph *glyph = row->glyphs[TEXT_AREA];
8444 struct glyph *end = glyph + row->used[TEXT_AREA];
8445 int x = row->x;
8446 int pt_old = PT - delta;
8447
8448 /* Skip over glyphs not having an object at the start of the row.
8449 These are special glyphs like truncation marks on terminal
8450 frames. */
8451 if (row->displays_text_p)
8452 while (glyph < end
8453 && INTEGERP (glyph->object)
8454 && glyph->charpos < 0)
8455 {
8456 x += glyph->pixel_width;
8457 ++glyph;
8458 }
8459
8460 while (glyph < end
8461 && !INTEGERP (glyph->object)
8462 && (!BUFFERP (glyph->object)
8463 || glyph->charpos < pt_old))
8464 {
8465 x += glyph->pixel_width;
8466 ++glyph;
8467 }
8468
8469 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8470 w->cursor.x = x;
8471 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8472 w->cursor.y = row->y + dy;
8473
8474 if (w == XWINDOW (selected_window))
8475 {
8476 if (!row->continued_p
8477 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8478 && row->x == 0)
8479 {
8480 this_line_buffer = XBUFFER (w->buffer);
8481
8482 CHARPOS (this_line_start_pos)
8483 = MATRIX_ROW_START_CHARPOS (row) + delta;
8484 BYTEPOS (this_line_start_pos)
8485 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8486
8487 CHARPOS (this_line_end_pos)
8488 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8489 BYTEPOS (this_line_end_pos)
8490 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8491
8492 this_line_y = w->cursor.y;
8493 this_line_pixel_height = row->height;
8494 this_line_vpos = w->cursor.vpos;
8495 this_line_start_x = row->x;
8496 }
8497 else
8498 CHARPOS (this_line_start_pos) = 0;
8499 }
8500 }
8501
8502
8503 /* Run window scroll functions, if any, for WINDOW with new window
8504 start STARTP. Sets the window start of WINDOW to that position.
8505
8506 We assume that the window's buffer is really current. */
8507
8508 static INLINE struct text_pos
8509 run_window_scroll_functions (window, startp)
8510 Lisp_Object window;
8511 struct text_pos startp;
8512 {
8513 struct window *w = XWINDOW (window);
8514 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8515
8516 if (current_buffer != XBUFFER (w->buffer))
8517 abort ();
8518
8519 if (!NILP (Vwindow_scroll_functions))
8520 {
8521 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8522 make_number (CHARPOS (startp)));
8523 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8524 /* In case the hook functions switch buffers. */
8525 if (current_buffer != XBUFFER (w->buffer))
8526 set_buffer_internal_1 (XBUFFER (w->buffer));
8527 }
8528
8529 return startp;
8530 }
8531
8532
8533 /* Modify the desired matrix of window W and W->vscroll so that the
8534 line containing the cursor is fully visible. */
8535
8536 static void
8537 make_cursor_line_fully_visible (w)
8538 struct window *w;
8539 {
8540 struct glyph_matrix *matrix;
8541 struct glyph_row *row;
8542 int window_height, header_line_height;
8543
8544 /* It's not always possible to find the cursor, e.g, when a window
8545 is full of overlay strings. Don't do anything in that case. */
8546 if (w->cursor.vpos < 0)
8547 return;
8548
8549 matrix = w->desired_matrix;
8550 row = MATRIX_ROW (matrix, w->cursor.vpos);
8551
8552 /* If the cursor row is not partially visible, there's nothing
8553 to do. */
8554 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8555 return;
8556
8557 /* If the row the cursor is in is taller than the window's height,
8558 it's not clear what to do, so do nothing. */
8559 window_height = window_box_height (w);
8560 if (row->height >= window_height)
8561 return;
8562
8563 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8564 {
8565 int dy = row->height - row->visible_height;
8566 w->vscroll = 0;
8567 w->cursor.y += dy;
8568 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8569 }
8570 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8571 {
8572 int dy = - (row->height - row->visible_height);
8573 w->vscroll = dy;
8574 w->cursor.y += dy;
8575 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8576 }
8577
8578 /* When we change the cursor y-position of the selected window,
8579 change this_line_y as well so that the display optimization for
8580 the cursor line of the selected window in redisplay_internal uses
8581 the correct y-position. */
8582 if (w == XWINDOW (selected_window))
8583 this_line_y = w->cursor.y;
8584 }
8585
8586
8587 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8588 non-zero means only WINDOW is redisplayed in redisplay_internal.
8589 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8590 in redisplay_window to bring a partially visible line into view in
8591 the case that only the cursor has moved.
8592
8593 Value is
8594
8595 1 if scrolling succeeded
8596
8597 0 if scrolling didn't find point.
8598
8599 -1 if new fonts have been loaded so that we must interrupt
8600 redisplay, adjust glyph matrices, and try again. */
8601
8602 static int
8603 try_scrolling (window, just_this_one_p, scroll_conservatively,
8604 scroll_step, temp_scroll_step)
8605 Lisp_Object window;
8606 int just_this_one_p;
8607 int scroll_conservatively, scroll_step;
8608 int temp_scroll_step;
8609 {
8610 struct window *w = XWINDOW (window);
8611 struct frame *f = XFRAME (w->frame);
8612 struct text_pos scroll_margin_pos;
8613 struct text_pos pos;
8614 struct text_pos startp;
8615 struct it it;
8616 Lisp_Object window_end;
8617 int this_scroll_margin;
8618 int dy = 0;
8619 int scroll_max;
8620 int rc;
8621 int amount_to_scroll = 0;
8622 Lisp_Object aggressive;
8623 int height;
8624
8625 #if GLYPH_DEBUG
8626 debug_method_add (w, "try_scrolling");
8627 #endif
8628
8629 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8630
8631 /* Compute scroll margin height in pixels. We scroll when point is
8632 within this distance from the top or bottom of the window. */
8633 if (scroll_margin > 0)
8634 {
8635 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8636 this_scroll_margin *= CANON_Y_UNIT (f);
8637 }
8638 else
8639 this_scroll_margin = 0;
8640
8641 /* Compute how much we should try to scroll maximally to bring point
8642 into view. */
8643 if (scroll_step)
8644 scroll_max = scroll_step;
8645 else if (scroll_conservatively)
8646 scroll_max = scroll_conservatively;
8647 else if (temp_scroll_step)
8648 scroll_max = temp_scroll_step;
8649 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8650 || NUMBERP (current_buffer->scroll_up_aggressively))
8651 /* We're trying to scroll because of aggressive scrolling
8652 but no scroll_step is set. Choose an arbitrary one. Maybe
8653 there should be a variable for this. */
8654 scroll_max = 10;
8655 else
8656 scroll_max = 0;
8657 scroll_max *= CANON_Y_UNIT (f);
8658
8659 /* Decide whether we have to scroll down. Start at the window end
8660 and move this_scroll_margin up to find the position of the scroll
8661 margin. */
8662 window_end = Fwindow_end (window, Qt);
8663 CHARPOS (scroll_margin_pos) = XINT (window_end);
8664 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8665 if (this_scroll_margin)
8666 {
8667 start_display (&it, w, scroll_margin_pos);
8668 move_it_vertically (&it, - this_scroll_margin);
8669 scroll_margin_pos = it.current.pos;
8670 }
8671
8672 if (PT >= CHARPOS (scroll_margin_pos))
8673 {
8674 int y0;
8675 #if 0
8676 int line_height;
8677 #endif
8678
8679 /* Point is in the scroll margin at the bottom of the window, or
8680 below. Compute a new window start that makes point visible. */
8681
8682 /* Compute the distance from the scroll margin to PT.
8683 Give up if the distance is greater than scroll_max. */
8684 start_display (&it, w, scroll_margin_pos);
8685 y0 = it.current_y;
8686 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8687 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8688 #if 0 /* Taking the line's height into account here looks wrong. */
8689 line_height = (it.max_ascent + it.max_descent
8690 ? it.max_ascent + it.max_descent
8691 : last_height);
8692 dy = it.current_y + line_height - y0;
8693 #else
8694 dy = it.current_y - y0;
8695 #endif
8696
8697 if (dy > scroll_max)
8698 return 0;
8699
8700 /* Move the window start down. If scrolling conservatively,
8701 move it just enough down to make point visible. If
8702 scroll_step is set, move it down by scroll_step. */
8703 start_display (&it, w, startp);
8704
8705 if (scroll_conservatively)
8706 amount_to_scroll = dy;
8707 else if (scroll_step || temp_scroll_step)
8708 amount_to_scroll = scroll_max;
8709 else
8710 {
8711 aggressive = current_buffer->scroll_down_aggressively;
8712 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8713 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8714 if (NUMBERP (aggressive))
8715 amount_to_scroll = XFLOATINT (aggressive) * height;
8716 }
8717
8718 if (amount_to_scroll <= 0)
8719 return 0;
8720
8721 move_it_vertically (&it, amount_to_scroll);
8722 startp = it.current.pos;
8723 }
8724 else
8725 {
8726 /* See if point is inside the scroll margin at the top of the
8727 window. */
8728 scroll_margin_pos = startp;
8729 if (this_scroll_margin)
8730 {
8731 start_display (&it, w, startp);
8732 move_it_vertically (&it, this_scroll_margin);
8733 scroll_margin_pos = it.current.pos;
8734 }
8735
8736 if (PT < CHARPOS (scroll_margin_pos))
8737 {
8738 /* Point is in the scroll margin at the top of the window or
8739 above what is displayed in the window. */
8740 int y0;
8741
8742 /* Compute the vertical distance from PT to the scroll
8743 margin position. Give up if distance is greater than
8744 scroll_max. */
8745 SET_TEXT_POS (pos, PT, PT_BYTE);
8746 start_display (&it, w, pos);
8747 y0 = it.current_y;
8748 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8749 it.last_visible_y, -1,
8750 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8751 dy = it.current_y - y0;
8752 if (dy > scroll_max)
8753 return 0;
8754
8755 /* Compute new window start. */
8756 start_display (&it, w, startp);
8757
8758 if (scroll_conservatively)
8759 amount_to_scroll = dy;
8760 else if (scroll_step || temp_scroll_step)
8761 amount_to_scroll = scroll_max;
8762 else
8763 {
8764 aggressive = current_buffer->scroll_up_aggressively;
8765 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8766 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8767 if (NUMBERP (aggressive))
8768 amount_to_scroll = XFLOATINT (aggressive) * height;
8769 }
8770
8771 if (amount_to_scroll <= 0)
8772 return 0;
8773
8774 move_it_vertically (&it, - amount_to_scroll);
8775 startp = it.current.pos;
8776 }
8777 }
8778
8779 /* Run window scroll functions. */
8780 startp = run_window_scroll_functions (window, startp);
8781
8782 /* Display the window. Give up if new fonts are loaded, or if point
8783 doesn't appear. */
8784 if (!try_window (window, startp))
8785 rc = -1;
8786 else if (w->cursor.vpos < 0)
8787 {
8788 clear_glyph_matrix (w->desired_matrix);
8789 rc = 0;
8790 }
8791 else
8792 {
8793 /* Maybe forget recorded base line for line number display. */
8794 if (!just_this_one_p
8795 || current_buffer->clip_changed
8796 || BEG_UNCHANGED < CHARPOS (startp))
8797 w->base_line_number = Qnil;
8798
8799 /* If cursor ends up on a partially visible line, shift display
8800 lines up or down. */
8801 make_cursor_line_fully_visible (w);
8802 rc = 1;
8803 }
8804
8805 return rc;
8806 }
8807
8808
8809 /* Compute a suitable window start for window W if display of W starts
8810 on a continuation line. Value is non-zero if a new window start
8811 was computed.
8812
8813 The new window start will be computed, based on W's width, starting
8814 from the start of the continued line. It is the start of the
8815 screen line with the minimum distance from the old start W->start. */
8816
8817 static int
8818 compute_window_start_on_continuation_line (w)
8819 struct window *w;
8820 {
8821 struct text_pos pos, start_pos;
8822 int window_start_changed_p = 0;
8823
8824 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
8825
8826 /* If window start is on a continuation line... Window start may be
8827 < BEGV in case there's invisible text at the start of the
8828 buffer (M-x rmail, for example). */
8829 if (CHARPOS (start_pos) > BEGV
8830 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
8831 {
8832 struct it it;
8833 struct glyph_row *row;
8834
8835 /* Handle the case that the window start is out of range. */
8836 if (CHARPOS (start_pos) < BEGV)
8837 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
8838 else if (CHARPOS (start_pos) > ZV)
8839 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
8840
8841 /* Find the start of the continued line. This should be fast
8842 because scan_buffer is fast (newline cache). */
8843 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
8844 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
8845 row, DEFAULT_FACE_ID);
8846 reseat_at_previous_visible_line_start (&it);
8847
8848 /* If the line start is "too far" away from the window start,
8849 say it takes too much time to compute a new window start. */
8850 if (CHARPOS (start_pos) - IT_CHARPOS (it)
8851 < XFASTINT (w->height) * XFASTINT (w->width))
8852 {
8853 int min_distance, distance;
8854
8855 /* Move forward by display lines to find the new window
8856 start. If window width was enlarged, the new start can
8857 be expected to be > the old start. If window width was
8858 decreased, the new window start will be < the old start.
8859 So, we're looking for the display line start with the
8860 minimum distance from the old window start. */
8861 pos = it.current.pos;
8862 min_distance = INFINITY;
8863 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
8864 distance < min_distance)
8865 {
8866 min_distance = distance;
8867 pos = it.current.pos;
8868 move_it_by_lines (&it, 1, 0);
8869 }
8870
8871 /* Set the window start there. */
8872 SET_MARKER_FROM_TEXT_POS (w->start, pos);
8873 window_start_changed_p = 1;
8874 }
8875 }
8876
8877 return window_start_changed_p;
8878 }
8879
8880
8881 /* Try cursor movement in case text has not changes in window WINDOW,
8882 with window start STARTP. Value is
8883
8884 1 if successful
8885
8886 0 if this method cannot be used
8887
8888 -1 if we know we have to scroll the display. *SCROLL_STEP is
8889 set to 1, under certain circumstances, if we want to scroll as
8890 if scroll-step were set to 1. See the code. */
8891
8892 static int
8893 try_cursor_movement (window, startp, scroll_step)
8894 Lisp_Object window;
8895 struct text_pos startp;
8896 int *scroll_step;
8897 {
8898 struct window *w = XWINDOW (window);
8899 struct frame *f = XFRAME (w->frame);
8900 int rc = 0;
8901
8902 /* Handle case where text has not changed, only point, and it has
8903 not moved off the frame. */
8904 if (/* Point may be in this window. */
8905 PT >= CHARPOS (startp)
8906 /* If we don't check this, we are called to move the cursor in a
8907 horizontally split window with a current matrix that doesn't
8908 fit the display. */
8909 && !windows_or_buffers_changed
8910 /* Selective display hasn't changed. */
8911 && !current_buffer->clip_changed
8912 /* If force-mode-line-update was called, really redisplay;
8913 that's how redisplay is forced after e.g. changing
8914 buffer-invisibility-spec. */
8915 && NILP (w->update_mode_line)
8916 /* Can't use this case if highlighting a region. When a
8917 region exists, cursor movement has to do more than just
8918 set the cursor. */
8919 && !(!NILP (Vtransient_mark_mode)
8920 && !NILP (current_buffer->mark_active))
8921 && NILP (w->region_showing)
8922 && NILP (Vshow_trailing_whitespace)
8923 /* Right after splitting windows, last_point may be nil. */
8924 && INTEGERP (w->last_point)
8925 /* This code is not used for mini-buffer for the sake of the case
8926 of redisplaying to replace an echo area message; since in
8927 that case the mini-buffer contents per se are usually
8928 unchanged. This code is of no real use in the mini-buffer
8929 since the handling of this_line_start_pos, etc., in redisplay
8930 handles the same cases. */
8931 && !EQ (window, minibuf_window)
8932 /* When splitting windows or for new windows, it happens that
8933 redisplay is called with a nil window_end_vpos or one being
8934 larger than the window. This should really be fixed in
8935 window.c. I don't have this on my list, now, so we do
8936 approximately the same as the old redisplay code. --gerd. */
8937 && INTEGERP (w->window_end_vpos)
8938 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
8939 && (FRAME_WINDOW_P (f)
8940 || !MARKERP (Voverlay_arrow_position)
8941 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
8942 {
8943 int this_scroll_margin;
8944 struct glyph_row *row;
8945
8946 #if GLYPH_DEBUG
8947 debug_method_add (w, "cursor movement");
8948 #endif
8949
8950 /* Scroll if point within this distance from the top or bottom
8951 of the window. This is a pixel value. */
8952 this_scroll_margin = max (0, scroll_margin);
8953 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
8954 this_scroll_margin *= CANON_Y_UNIT (f);
8955
8956 /* Start with the row the cursor was displayed during the last
8957 not paused redisplay. Give up if that row is not valid. */
8958 if (w->last_cursor.vpos < 0
8959 || w->last_cursor.vpos >= w->current_matrix->nrows)
8960 rc = -1;
8961 else
8962 {
8963 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
8964 if (row->mode_line_p)
8965 ++row;
8966 if (!row->enabled_p)
8967 rc = -1;
8968 }
8969
8970 if (rc == 0)
8971 {
8972 int scroll_p = 0;
8973 int last_y = window_text_bottom_y (w) - this_scroll_margin;
8974
8975
8976 if (PT > XFASTINT (w->last_point))
8977 {
8978 /* Point has moved forward. */
8979 while (MATRIX_ROW_END_CHARPOS (row) < PT
8980 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
8981 {
8982 xassert (row->enabled_p);
8983 ++row;
8984 }
8985
8986 /* The end position of a row equals the start position
8987 of the next row. If PT is there, we would rather
8988 display it in the next line. */
8989 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
8990 && MATRIX_ROW_END_CHARPOS (row) == PT
8991 && !cursor_row_p (w, row))
8992 ++row;
8993
8994 /* If within the scroll margin, scroll. Note that
8995 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
8996 the next line would be drawn, and that
8997 this_scroll_margin can be zero. */
8998 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
8999 || PT > MATRIX_ROW_END_CHARPOS (row)
9000 /* Line is completely visible last line in window
9001 and PT is to be set in the next line. */
9002 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9003 && PT == MATRIX_ROW_END_CHARPOS (row)
9004 && !row->ends_at_zv_p
9005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9006 scroll_p = 1;
9007 }
9008 else if (PT < XFASTINT (w->last_point))
9009 {
9010 /* Cursor has to be moved backward. Note that PT >=
9011 CHARPOS (startp) because of the outer
9012 if-statement. */
9013 while (!row->mode_line_p
9014 && (MATRIX_ROW_START_CHARPOS (row) > PT
9015 || (MATRIX_ROW_START_CHARPOS (row) == PT
9016 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9017 && (row->y > this_scroll_margin
9018 || CHARPOS (startp) == BEGV))
9019 {
9020 xassert (row->enabled_p);
9021 --row;
9022 }
9023
9024 /* Consider the following case: Window starts at BEGV,
9025 there is invisible, intangible text at BEGV, so that
9026 display starts at some point START > BEGV. It can
9027 happen that we are called with PT somewhere between
9028 BEGV and START. Try to handle that case. */
9029 if (row < w->current_matrix->rows
9030 || row->mode_line_p)
9031 {
9032 row = w->current_matrix->rows;
9033 if (row->mode_line_p)
9034 ++row;
9035 }
9036
9037 /* Due to newlines in overlay strings, we may have to
9038 skip forward over overlay strings. */
9039 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9040 && MATRIX_ROW_END_CHARPOS (row) == PT
9041 && !cursor_row_p (w, row))
9042 ++row;
9043
9044 /* If within the scroll margin, scroll. */
9045 if (row->y < this_scroll_margin
9046 && CHARPOS (startp) != BEGV)
9047 scroll_p = 1;
9048 }
9049
9050 if (PT < MATRIX_ROW_START_CHARPOS (row)
9051 || PT > MATRIX_ROW_END_CHARPOS (row))
9052 {
9053 /* if PT is not in the glyph row, give up. */
9054 rc = -1;
9055 }
9056 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9057 {
9058 /* If we end up in a partially visible line, let's make it
9059 fully visible, except when it's taller than the window,
9060 in which case we can't do much about it. */
9061 if (row->height > window_box_height (w))
9062 {
9063 *scroll_step = 1;
9064 rc = -1;
9065 }
9066 else
9067 {
9068 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9069 try_window (window, startp);
9070 make_cursor_line_fully_visible (w);
9071 rc = 1;
9072 }
9073 }
9074 else if (scroll_p)
9075 rc = -1;
9076 else
9077 {
9078 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9079 rc = 1;
9080 }
9081 }
9082 }
9083
9084 return rc;
9085 }
9086
9087
9088 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9089 selected_window is redisplayed. */
9090
9091 static void
9092 redisplay_window (window, just_this_one_p)
9093 Lisp_Object window;
9094 int just_this_one_p;
9095 {
9096 struct window *w = XWINDOW (window);
9097 struct frame *f = XFRAME (w->frame);
9098 struct buffer *buffer = XBUFFER (w->buffer);
9099 struct buffer *old = current_buffer;
9100 struct text_pos lpoint, opoint, startp;
9101 int update_mode_line;
9102 int tem;
9103 struct it it;
9104 /* Record it now because it's overwritten. */
9105 int current_matrix_up_to_date_p = 0;
9106 int temp_scroll_step = 0;
9107 int count = specpdl_ptr - specpdl;
9108 int rc;
9109
9110 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9111 opoint = lpoint;
9112
9113 /* W must be a leaf window here. */
9114 xassert (!NILP (w->buffer));
9115 #if GLYPH_DEBUG
9116 *w->desired_matrix->method = 0;
9117 #endif
9118
9119 specbind (Qinhibit_point_motion_hooks, Qt);
9120
9121 reconsider_clip_changes (w, buffer);
9122
9123 /* Has the mode line to be updated? */
9124 update_mode_line = (!NILP (w->update_mode_line)
9125 || update_mode_lines
9126 || buffer->clip_changed);
9127
9128 if (MINI_WINDOW_P (w))
9129 {
9130 if (w == XWINDOW (echo_area_window)
9131 && !NILP (echo_area_buffer[0]))
9132 {
9133 if (update_mode_line)
9134 /* We may have to update a tty frame's menu bar or a
9135 tool-bar. Example `M-x C-h C-h C-g'. */
9136 goto finish_menu_bars;
9137 else
9138 /* We've already displayed the echo area glyphs in this window. */
9139 goto finish_scroll_bars;
9140 }
9141 else if (w != XWINDOW (minibuf_window))
9142 {
9143 /* W is a mini-buffer window, but it's not the currently
9144 active one, so clear it. */
9145 int yb = window_text_bottom_y (w);
9146 struct glyph_row *row;
9147 int y;
9148
9149 for (y = 0, row = w->desired_matrix->rows;
9150 y < yb;
9151 y += row->height, ++row)
9152 blank_row (w, row, y);
9153 goto finish_scroll_bars;
9154 }
9155 }
9156
9157 /* Otherwise set up data on this window; select its buffer and point
9158 value. */
9159 /* Really select the buffer, for the sake of buffer-local
9160 variables. */
9161 set_buffer_internal_1 (XBUFFER (w->buffer));
9162 SET_TEXT_POS (opoint, PT, PT_BYTE);
9163
9164 current_matrix_up_to_date_p
9165 = (!NILP (w->window_end_valid)
9166 && !current_buffer->clip_changed
9167 && XFASTINT (w->last_modified) >= MODIFF
9168 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9169
9170 /* When windows_or_buffers_changed is non-zero, we can't rely on
9171 the window end being valid, so set it to nil there. */
9172 if (windows_or_buffers_changed)
9173 {
9174 /* If window starts on a continuation line, maybe adjust the
9175 window start in case the window's width changed. */
9176 if (XMARKER (w->start)->buffer == current_buffer)
9177 compute_window_start_on_continuation_line (w);
9178
9179 w->window_end_valid = Qnil;
9180 }
9181
9182 /* Some sanity checks. */
9183 CHECK_WINDOW_END (w);
9184 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9185 abort ();
9186 if (BYTEPOS (opoint) < CHARPOS (opoint))
9187 abort ();
9188
9189 /* If %c is in mode line, update it if needed. */
9190 if (!NILP (w->column_number_displayed)
9191 /* This alternative quickly identifies a common case
9192 where no change is needed. */
9193 && !(PT == XFASTINT (w->last_point)
9194 && XFASTINT (w->last_modified) >= MODIFF
9195 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9196 && XFASTINT (w->column_number_displayed) != current_column ())
9197 update_mode_line = 1;
9198
9199 /* Count number of windows showing the selected buffer. An indirect
9200 buffer counts as its base buffer. */
9201 if (!just_this_one_p)
9202 {
9203 struct buffer *current_base, *window_base;
9204 current_base = current_buffer;
9205 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9206 if (current_base->base_buffer)
9207 current_base = current_base->base_buffer;
9208 if (window_base->base_buffer)
9209 window_base = window_base->base_buffer;
9210 if (current_base == window_base)
9211 buffer_shared++;
9212 }
9213
9214 /* Point refers normally to the selected window. For any other
9215 window, set up appropriate value. */
9216 if (!EQ (window, selected_window))
9217 {
9218 int new_pt = XMARKER (w->pointm)->charpos;
9219 int new_pt_byte = marker_byte_position (w->pointm);
9220 if (new_pt < BEGV)
9221 {
9222 new_pt = BEGV;
9223 new_pt_byte = BEGV_BYTE;
9224 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9225 }
9226 else if (new_pt > (ZV - 1))
9227 {
9228 new_pt = ZV;
9229 new_pt_byte = ZV_BYTE;
9230 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9231 }
9232
9233 /* We don't use SET_PT so that the point-motion hooks don't run. */
9234 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9235 }
9236
9237 /* If any of the character widths specified in the display table
9238 have changed, invalidate the width run cache. It's true that
9239 this may be a bit late to catch such changes, but the rest of
9240 redisplay goes (non-fatally) haywire when the display table is
9241 changed, so why should we worry about doing any better? */
9242 if (current_buffer->width_run_cache)
9243 {
9244 struct Lisp_Char_Table *disptab = buffer_display_table ();
9245
9246 if (! disptab_matches_widthtab (disptab,
9247 XVECTOR (current_buffer->width_table)))
9248 {
9249 invalidate_region_cache (current_buffer,
9250 current_buffer->width_run_cache,
9251 BEG, Z);
9252 recompute_width_table (current_buffer, disptab);
9253 }
9254 }
9255
9256 /* If window-start is screwed up, choose a new one. */
9257 if (XMARKER (w->start)->buffer != current_buffer)
9258 goto recenter;
9259
9260 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9261
9262 /* If someone specified a new starting point but did not insist,
9263 check whether it can be used. */
9264 if (!NILP (w->optional_new_start)
9265 && CHARPOS (startp) >= BEGV
9266 && CHARPOS (startp) <= ZV)
9267 {
9268 w->optional_new_start = Qnil;
9269 start_display (&it, w, startp);
9270 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9271 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9272 if (IT_CHARPOS (it) == PT)
9273 w->force_start = Qt;
9274 }
9275
9276 /* Handle case where place to start displaying has been specified,
9277 unless the specified location is outside the accessible range. */
9278 if (!NILP (w->force_start)
9279 || w->frozen_window_start_p)
9280 {
9281 w->force_start = Qnil;
9282 w->vscroll = 0;
9283 w->window_end_valid = Qnil;
9284
9285 /* Forget any recorded base line for line number display. */
9286 if (!current_matrix_up_to_date_p
9287 || current_buffer->clip_changed)
9288 w->base_line_number = Qnil;
9289
9290 /* Redisplay the mode line. Select the buffer properly for that.
9291 Also, run the hook window-scroll-functions
9292 because we have scrolled. */
9293 /* Note, we do this after clearing force_start because
9294 if there's an error, it is better to forget about force_start
9295 than to get into an infinite loop calling the hook functions
9296 and having them get more errors. */
9297 if (!update_mode_line
9298 || ! NILP (Vwindow_scroll_functions))
9299 {
9300 update_mode_line = 1;
9301 w->update_mode_line = Qt;
9302 startp = run_window_scroll_functions (window, startp);
9303 }
9304
9305 XSETFASTINT (w->last_modified, 0);
9306 XSETFASTINT (w->last_overlay_modified, 0);
9307 if (CHARPOS (startp) < BEGV)
9308 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9309 else if (CHARPOS (startp) > ZV)
9310 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9311
9312 /* Redisplay, then check if cursor has been set during the
9313 redisplay. Give up if new fonts were loaded. */
9314 if (!try_window (window, startp))
9315 {
9316 w->force_start = Qt;
9317 clear_glyph_matrix (w->desired_matrix);
9318 goto restore_buffers;
9319 }
9320
9321 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9322 {
9323 /* If point does not appear, try to move point so it does
9324 appear. The desired matrix has been built above, so we
9325 can use it here. */
9326 int window_height;
9327 struct glyph_row *row;
9328
9329 window_height = window_box_height (w) / 2;
9330 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9331 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9332 ++row;
9333
9334 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9335 MATRIX_ROW_START_BYTEPOS (row));
9336
9337 if (w != XWINDOW (selected_window))
9338 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9339 else if (current_buffer == old)
9340 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9341
9342 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9343
9344 /* If we are highlighting the region, then we just changed
9345 the region, so redisplay to show it. */
9346 if (!NILP (Vtransient_mark_mode)
9347 && !NILP (current_buffer->mark_active))
9348 {
9349 clear_glyph_matrix (w->desired_matrix);
9350 if (!try_window (window, startp))
9351 goto restore_buffers;
9352 }
9353 }
9354
9355 make_cursor_line_fully_visible (w);
9356 #if GLYPH_DEBUG
9357 debug_method_add (w, "forced window start");
9358 #endif
9359 goto done;
9360 }
9361
9362 /* Handle case where text has not changed, only point, and it has
9363 not moved off the frame. */
9364 if (current_matrix_up_to_date_p
9365 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9366 rc != 0))
9367 {
9368 if (rc == -1)
9369 goto try_to_scroll;
9370 else
9371 goto done;
9372 }
9373 /* If current starting point was originally the beginning of a line
9374 but no longer is, find a new starting point. */
9375 else if (!NILP (w->start_at_line_beg)
9376 && !(CHARPOS (startp) <= BEGV
9377 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9378 {
9379 #if GLYPH_DEBUG
9380 debug_method_add (w, "recenter 1");
9381 #endif
9382 goto recenter;
9383 }
9384
9385 /* Try scrolling with try_window_id. */
9386 else if (/* Windows and buffers haven't changed. */
9387 !windows_or_buffers_changed
9388 /* Window must be either use window-based redisplay or
9389 be full width. */
9390 && (FRAME_WINDOW_P (f)
9391 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9392 && !MINI_WINDOW_P (w)
9393 /* Point is not known NOT to appear in window. */
9394 && PT >= CHARPOS (startp)
9395 && XFASTINT (w->last_modified)
9396 /* Window is not hscrolled. */
9397 && XFASTINT (w->hscroll) == 0
9398 /* Selective display has not changed. */
9399 && !current_buffer->clip_changed
9400 /* Current matrix is up to date. */
9401 && !NILP (w->window_end_valid)
9402 /* Can't use this case if highlighting a region because
9403 a cursor movement will do more than just set the cursor. */
9404 && !(!NILP (Vtransient_mark_mode)
9405 && !NILP (current_buffer->mark_active))
9406 && NILP (w->region_showing)
9407 && NILP (Vshow_trailing_whitespace)
9408 /* Overlay arrow position and string not changed. */
9409 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9410 && EQ (last_arrow_string, Voverlay_arrow_string)
9411 /* Value is > 0 if update has been done, it is -1 if we
9412 know that the same window start will not work. It is 0
9413 if unsuccessful for some other reason. */
9414 && (tem = try_window_id (w)) != 0)
9415 {
9416 #if GLYPH_DEBUG
9417 debug_method_add (w, "try_window_id %d", tem);
9418 #endif
9419
9420 if (fonts_changed_p)
9421 goto restore_buffers;
9422 if (tem > 0)
9423 goto done;
9424
9425 /* Otherwise try_window_id has returned -1 which means that we
9426 don't want the alternative below this comment to execute. */
9427 }
9428 else if (CHARPOS (startp) >= BEGV
9429 && CHARPOS (startp) <= ZV
9430 && PT >= CHARPOS (startp)
9431 && (CHARPOS (startp) < ZV
9432 /* Avoid starting at end of buffer. */
9433 || CHARPOS (startp) == BEGV
9434 || (XFASTINT (w->last_modified) >= MODIFF
9435 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9436 {
9437 #if GLYPH_DEBUG
9438 debug_method_add (w, "same window start");
9439 #endif
9440
9441 /* Try to redisplay starting at same place as before.
9442 If point has not moved off frame, accept the results. */
9443 if (!current_matrix_up_to_date_p
9444 /* Don't use try_window_reusing_current_matrix in this case
9445 because a window scroll function can have changed the
9446 buffer. */
9447 || !NILP (Vwindow_scroll_functions)
9448 || MINI_WINDOW_P (w)
9449 || !try_window_reusing_current_matrix (w))
9450 {
9451 IF_DEBUG (debug_method_add (w, "1"));
9452 try_window (window, startp);
9453 }
9454
9455 if (fonts_changed_p)
9456 goto restore_buffers;
9457
9458 if (w->cursor.vpos >= 0)
9459 {
9460 if (!just_this_one_p
9461 || current_buffer->clip_changed
9462 || BEG_UNCHANGED < CHARPOS (startp))
9463 /* Forget any recorded base line for line number display. */
9464 w->base_line_number = Qnil;
9465
9466 make_cursor_line_fully_visible (w);
9467 goto done;
9468 }
9469 else
9470 clear_glyph_matrix (w->desired_matrix);
9471 }
9472
9473 try_to_scroll:
9474
9475 XSETFASTINT (w->last_modified, 0);
9476 XSETFASTINT (w->last_overlay_modified, 0);
9477
9478 /* Redisplay the mode line. Select the buffer properly for that. */
9479 if (!update_mode_line)
9480 {
9481 update_mode_line = 1;
9482 w->update_mode_line = Qt;
9483 }
9484
9485 /* Try to scroll by specified few lines. */
9486 if ((scroll_conservatively
9487 || scroll_step
9488 || temp_scroll_step
9489 || NUMBERP (current_buffer->scroll_up_aggressively)
9490 || NUMBERP (current_buffer->scroll_down_aggressively))
9491 && !current_buffer->clip_changed
9492 && CHARPOS (startp) >= BEGV
9493 && CHARPOS (startp) <= ZV)
9494 {
9495 /* The function returns -1 if new fonts were loaded, 1 if
9496 successful, 0 if not successful. */
9497 int rc = try_scrolling (window, just_this_one_p,
9498 scroll_conservatively,
9499 scroll_step,
9500 temp_scroll_step);
9501 if (rc > 0)
9502 goto done;
9503 else if (rc < 0)
9504 goto restore_buffers;
9505 }
9506
9507 /* Finally, just choose place to start which centers point */
9508
9509 recenter:
9510
9511 #if GLYPH_DEBUG
9512 debug_method_add (w, "recenter");
9513 #endif
9514
9515 /* w->vscroll = 0; */
9516
9517 /* Forget any previously recorded base line for line number display. */
9518 if (!current_matrix_up_to_date_p
9519 || current_buffer->clip_changed)
9520 w->base_line_number = Qnil;
9521
9522 /* Move backward half the height of the window. */
9523 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9524 it.current_y = it.last_visible_y;
9525 move_it_vertically_backward (&it, it.last_visible_y / 2);
9526 xassert (IT_CHARPOS (it) >= BEGV);
9527
9528 /* The function move_it_vertically_backward may move over more
9529 than the specified y-distance. If it->w is small, e.g. a
9530 mini-buffer window, we may end up in front of the window's
9531 display area. Start displaying at the start of the line
9532 containing PT in this case. */
9533 if (it.current_y <= 0)
9534 {
9535 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9536 move_it_vertically (&it, 0);
9537 xassert (IT_CHARPOS (it) <= PT);
9538 it.current_y = 0;
9539 }
9540
9541 it.current_x = it.hpos = 0;
9542
9543 /* Set startp here explicitly in case that helps avoid an infinite loop
9544 in case the window-scroll-functions functions get errors. */
9545 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9546
9547 /* Run scroll hooks. */
9548 startp = run_window_scroll_functions (window, it.current.pos);
9549
9550 /* Redisplay the window. */
9551 if (!current_matrix_up_to_date_p
9552 || windows_or_buffers_changed
9553 /* Don't use try_window_reusing_current_matrix in this case
9554 because it can have changed the buffer. */
9555 || !NILP (Vwindow_scroll_functions)
9556 || !just_this_one_p
9557 || MINI_WINDOW_P (w)
9558 || !try_window_reusing_current_matrix (w))
9559 try_window (window, startp);
9560
9561 /* If new fonts have been loaded (due to fontsets), give up. We
9562 have to start a new redisplay since we need to re-adjust glyph
9563 matrices. */
9564 if (fonts_changed_p)
9565 goto restore_buffers;
9566
9567 /* If cursor did not appear assume that the middle of the window is
9568 in the first line of the window. Do it again with the next line.
9569 (Imagine a window of height 100, displaying two lines of height
9570 60. Moving back 50 from it->last_visible_y will end in the first
9571 line.) */
9572 if (w->cursor.vpos < 0)
9573 {
9574 if (!NILP (w->window_end_valid)
9575 && PT >= Z - XFASTINT (w->window_end_pos))
9576 {
9577 clear_glyph_matrix (w->desired_matrix);
9578 move_it_by_lines (&it, 1, 0);
9579 try_window (window, it.current.pos);
9580 }
9581 else if (PT < IT_CHARPOS (it))
9582 {
9583 clear_glyph_matrix (w->desired_matrix);
9584 move_it_by_lines (&it, -1, 0);
9585 try_window (window, it.current.pos);
9586 }
9587 else
9588 {
9589 /* Not much we can do about it. */
9590 }
9591 }
9592
9593 /* Consider the following case: Window starts at BEGV, there is
9594 invisible, intangible text at BEGV, so that display starts at
9595 some point START > BEGV. It can happen that we are called with
9596 PT somewhere between BEGV and START. Try to handle that case. */
9597 if (w->cursor.vpos < 0)
9598 {
9599 struct glyph_row *row = w->current_matrix->rows;
9600 if (row->mode_line_p)
9601 ++row;
9602 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9603 }
9604
9605 make_cursor_line_fully_visible (w);
9606
9607 done:
9608
9609 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9610 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9611 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9612 ? Qt : Qnil);
9613
9614 /* Display the mode line, if we must. */
9615 if ((update_mode_line
9616 /* If window not full width, must redo its mode line
9617 if (a) the window to its side is being redone and
9618 (b) we do a frame-based redisplay. This is a consequence
9619 of how inverted lines are drawn in frame-based redisplay. */
9620 || (!just_this_one_p
9621 && !FRAME_WINDOW_P (f)
9622 && !WINDOW_FULL_WIDTH_P (w))
9623 /* Line number to display. */
9624 || INTEGERP (w->base_line_pos)
9625 /* Column number is displayed and different from the one displayed. */
9626 || (!NILP (w->column_number_displayed)
9627 && XFASTINT (w->column_number_displayed) != current_column ()))
9628 /* This means that the window has a mode line. */
9629 && (WINDOW_WANTS_MODELINE_P (w)
9630 || WINDOW_WANTS_HEADER_LINE_P (w)))
9631 {
9632 Lisp_Object old_selected_frame;
9633
9634 old_selected_frame = selected_frame;
9635
9636 XSETFRAME (selected_frame, f);
9637 display_mode_lines (w);
9638 selected_frame = old_selected_frame;
9639
9640 /* If mode line height has changed, arrange for a thorough
9641 immediate redisplay using the correct mode line height. */
9642 if (WINDOW_WANTS_MODELINE_P (w)
9643 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9644 {
9645 fonts_changed_p = 1;
9646 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9647 = DESIRED_MODE_LINE_HEIGHT (w);
9648 }
9649
9650 /* If top line height has changed, arrange for a thorough
9651 immediate redisplay using the correct mode line height. */
9652 if (WINDOW_WANTS_HEADER_LINE_P (w)
9653 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9654 {
9655 fonts_changed_p = 1;
9656 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9657 = DESIRED_HEADER_LINE_HEIGHT (w);
9658 }
9659
9660 if (fonts_changed_p)
9661 goto restore_buffers;
9662 }
9663
9664 if (!line_number_displayed
9665 && !BUFFERP (w->base_line_pos))
9666 {
9667 w->base_line_pos = Qnil;
9668 w->base_line_number = Qnil;
9669 }
9670
9671 finish_menu_bars:
9672
9673 /* When we reach a frame's selected window, redo the frame's menu bar. */
9674 if (update_mode_line
9675 && EQ (FRAME_SELECTED_WINDOW (f), window))
9676 {
9677 int redisplay_menu_p = 0;
9678
9679 if (FRAME_WINDOW_P (f))
9680 {
9681 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9682 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9683 #else
9684 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9685 #endif
9686 }
9687 else
9688 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9689
9690 if (redisplay_menu_p)
9691 display_menu_bar (w);
9692
9693 #ifdef HAVE_WINDOW_SYSTEM
9694 if (WINDOWP (f->tool_bar_window)
9695 && (FRAME_TOOL_BAR_LINES (f) > 0
9696 || auto_resize_tool_bars_p))
9697 redisplay_tool_bar (f);
9698 #endif
9699 }
9700
9701 finish_scroll_bars:
9702
9703 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9704 {
9705 int start, end, whole;
9706
9707 /* Calculate the start and end positions for the current window.
9708 At some point, it would be nice to choose between scrollbars
9709 which reflect the whole buffer size, with special markers
9710 indicating narrowing, and scrollbars which reflect only the
9711 visible region.
9712
9713 Note that mini-buffers sometimes aren't displaying any text. */
9714 if (!MINI_WINDOW_P (w)
9715 || (w == XWINDOW (minibuf_window)
9716 && NILP (echo_area_buffer[0])))
9717 {
9718 whole = ZV - BEGV;
9719 start = marker_position (w->start) - BEGV;
9720 /* I don't think this is guaranteed to be right. For the
9721 moment, we'll pretend it is. */
9722 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9723
9724 if (end < start)
9725 end = start;
9726 if (whole < (end - start))
9727 whole = end - start;
9728 }
9729 else
9730 start = end = whole = 0;
9731
9732 /* Indicate what this scroll bar ought to be displaying now. */
9733 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9734
9735 /* Note that we actually used the scroll bar attached to this
9736 window, so it shouldn't be deleted at the end of redisplay. */
9737 (*redeem_scroll_bar_hook) (w);
9738 }
9739
9740 restore_buffers:
9741
9742 /* Restore current_buffer and value of point in it. */
9743 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9744 set_buffer_internal_1 (old);
9745 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9746
9747 unbind_to (count, Qnil);
9748 }
9749
9750
9751 /* Build the complete desired matrix of WINDOW with a window start
9752 buffer position POS. Value is non-zero if successful. It is zero
9753 if fonts were loaded during redisplay which makes re-adjusting
9754 glyph matrices necessary. */
9755
9756 int
9757 try_window (window, pos)
9758 Lisp_Object window;
9759 struct text_pos pos;
9760 {
9761 struct window *w = XWINDOW (window);
9762 struct it it;
9763 struct glyph_row *last_text_row = NULL;
9764
9765 /* Make POS the new window start. */
9766 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
9767
9768 /* Mark cursor position as unknown. No overlay arrow seen. */
9769 w->cursor.vpos = -1;
9770 overlay_arrow_seen = 0;
9771
9772 /* Initialize iterator and info to start at POS. */
9773 start_display (&it, w, pos);
9774
9775 /* Display all lines of W. */
9776 while (it.current_y < it.last_visible_y)
9777 {
9778 if (display_line (&it))
9779 last_text_row = it.glyph_row - 1;
9780 if (fonts_changed_p)
9781 return 0;
9782 }
9783
9784 /* If bottom moved off end of frame, change mode line percentage. */
9785 if (XFASTINT (w->window_end_pos) <= 0
9786 && Z != IT_CHARPOS (it))
9787 w->update_mode_line = Qt;
9788
9789 /* Set window_end_pos to the offset of the last character displayed
9790 on the window from the end of current_buffer. Set
9791 window_end_vpos to its row number. */
9792 if (last_text_row)
9793 {
9794 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
9795 w->window_end_bytepos
9796 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
9797 XSETFASTINT (w->window_end_pos,
9798 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
9799 XSETFASTINT (w->window_end_vpos,
9800 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
9801 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
9802 ->displays_text_p);
9803 }
9804 else
9805 {
9806 w->window_end_bytepos = 0;
9807 XSETFASTINT (w->window_end_pos, 0);
9808 XSETFASTINT (w->window_end_vpos, 0);
9809 }
9810
9811 /* But that is not valid info until redisplay finishes. */
9812 w->window_end_valid = Qnil;
9813 return 1;
9814 }
9815
9816
9817 \f
9818 /************************************************************************
9819 Window redisplay reusing current matrix when buffer has not changed
9820 ************************************************************************/
9821
9822 /* Try redisplay of window W showing an unchanged buffer with a
9823 different window start than the last time it was displayed by
9824 reusing its current matrix. Value is non-zero if successful.
9825 W->start is the new window start. */
9826
9827 static int
9828 try_window_reusing_current_matrix (w)
9829 struct window *w;
9830 {
9831 struct frame *f = XFRAME (w->frame);
9832 struct glyph_row *row, *bottom_row;
9833 struct it it;
9834 struct run run;
9835 struct text_pos start, new_start;
9836 int nrows_scrolled, i;
9837 struct glyph_row *last_text_row;
9838 struct glyph_row *last_reused_text_row;
9839 struct glyph_row *start_row;
9840 int start_vpos, min_y, max_y;
9841
9842 if (/* This function doesn't handle terminal frames. */
9843 !FRAME_WINDOW_P (f)
9844 /* Don't try to reuse the display if windows have been split
9845 or such. */
9846 || windows_or_buffers_changed)
9847 return 0;
9848
9849 /* Can't do this if region may have changed. */
9850 if ((!NILP (Vtransient_mark_mode)
9851 && !NILP (current_buffer->mark_active))
9852 || !NILP (w->region_showing)
9853 || !NILP (Vshow_trailing_whitespace))
9854 return 0;
9855
9856 /* If top-line visibility has changed, give up. */
9857 if (WINDOW_WANTS_HEADER_LINE_P (w)
9858 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
9859 return 0;
9860
9861 /* Give up if old or new display is scrolled vertically. We could
9862 make this function handle this, but right now it doesn't. */
9863 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9864 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
9865 return 0;
9866
9867 /* The variable new_start now holds the new window start. The old
9868 start `start' can be determined from the current matrix. */
9869 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
9870 start = start_row->start.pos;
9871 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
9872
9873 /* Clear the desired matrix for the display below. */
9874 clear_glyph_matrix (w->desired_matrix);
9875
9876 if (CHARPOS (new_start) <= CHARPOS (start))
9877 {
9878 int first_row_y;
9879
9880 IF_DEBUG (debug_method_add (w, "twu1"));
9881
9882 /* Display up to a row that can be reused. The variable
9883 last_text_row is set to the last row displayed that displays
9884 text. Note that it.vpos == 0 if or if not there is a
9885 header-line; it's not the same as the MATRIX_ROW_VPOS! */
9886 start_display (&it, w, new_start);
9887 first_row_y = it.current_y;
9888 w->cursor.vpos = -1;
9889 last_text_row = last_reused_text_row = NULL;
9890
9891 while (it.current_y < it.last_visible_y
9892 && IT_CHARPOS (it) < CHARPOS (start)
9893 && !fonts_changed_p)
9894 if (display_line (&it))
9895 last_text_row = it.glyph_row - 1;
9896
9897 /* A value of current_y < last_visible_y means that we stopped
9898 at the previous window start, which in turn means that we
9899 have at least one reusable row. */
9900 if (it.current_y < it.last_visible_y)
9901 {
9902 /* IT.vpos always starts from 0; it counts text lines. */
9903 nrows_scrolled = it.vpos;
9904
9905 /* Find PT if not already found in the lines displayed. */
9906 if (w->cursor.vpos < 0)
9907 {
9908 int dy = it.current_y - first_row_y;
9909
9910 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
9911 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9912 {
9913 if (PT >= MATRIX_ROW_START_CHARPOS (row)
9914 && PT < MATRIX_ROW_END_CHARPOS (row))
9915 {
9916 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
9917 dy, nrows_scrolled);
9918 break;
9919 }
9920
9921 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
9922 break;
9923
9924 ++row;
9925 }
9926
9927 /* Give up if point was not found. This shouldn't
9928 happen often; not more often than with try_window
9929 itself. */
9930 if (w->cursor.vpos < 0)
9931 {
9932 clear_glyph_matrix (w->desired_matrix);
9933 return 0;
9934 }
9935 }
9936
9937 /* Scroll the display. Do it before the current matrix is
9938 changed. The problem here is that update has not yet
9939 run, i.e. part of the current matrix is not up to date.
9940 scroll_run_hook will clear the cursor, and use the
9941 current matrix to get the height of the row the cursor is
9942 in. */
9943 run.current_y = first_row_y;
9944 run.desired_y = it.current_y;
9945 run.height = it.last_visible_y - it.current_y;
9946
9947 if (run.height > 0 && run.current_y != run.desired_y)
9948 {
9949 update_begin (f);
9950 rif->update_window_begin_hook (w);
9951 rif->clear_mouse_face (w);
9952 rif->scroll_run_hook (w, &run);
9953 rif->update_window_end_hook (w, 0, 0);
9954 update_end (f);
9955 }
9956
9957 /* Shift current matrix down by nrows_scrolled lines. */
9958 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
9959 rotate_matrix (w->current_matrix,
9960 start_vpos,
9961 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
9962 nrows_scrolled);
9963
9964 /* Disable lines not reused. */
9965 for (i = 0; i < it.vpos; ++i)
9966 (start_row + i)->enabled_p = 0;
9967
9968 /* Re-compute Y positions. */
9969 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
9970 max_y = it.last_visible_y;
9971 for (row = start_row + nrows_scrolled;
9972 row < bottom_row;
9973 ++row)
9974 {
9975 row->y = it.current_y;
9976
9977 if (row->y < min_y)
9978 row->visible_height = row->height - (min_y - row->y);
9979 else if (row->y + row->height > max_y)
9980 row->visible_height
9981 = row->height - (row->y + row->height - max_y);
9982 else
9983 row->visible_height = row->height;
9984
9985 it.current_y += row->height;
9986
9987 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
9988 last_reused_text_row = row;
9989 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
9990 break;
9991 }
9992 }
9993
9994 /* Update window_end_pos etc.; last_reused_text_row is the last
9995 reused row from the current matrix containing text, if any.
9996 The value of last_text_row is the last displayed line
9997 containing text. */
9998 if (last_reused_text_row)
9999 {
10000 w->window_end_bytepos
10001 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10002 XSETFASTINT (w->window_end_pos,
10003 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10004 XSETFASTINT (w->window_end_vpos,
10005 MATRIX_ROW_VPOS (last_reused_text_row,
10006 w->current_matrix));
10007 }
10008 else if (last_text_row)
10009 {
10010 w->window_end_bytepos
10011 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10012 XSETFASTINT (w->window_end_pos,
10013 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10014 XSETFASTINT (w->window_end_vpos,
10015 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10016 }
10017 else
10018 {
10019 /* This window must be completely empty. */
10020 w->window_end_bytepos = 0;
10021 XSETFASTINT (w->window_end_pos, 0);
10022 XSETFASTINT (w->window_end_vpos, 0);
10023 }
10024 w->window_end_valid = Qnil;
10025
10026 /* Update hint: don't try scrolling again in update_window. */
10027 w->desired_matrix->no_scrolling_p = 1;
10028
10029 #if GLYPH_DEBUG
10030 debug_method_add (w, "try_window_reusing_current_matrix 1");
10031 #endif
10032 return 1;
10033 }
10034 else if (CHARPOS (new_start) > CHARPOS (start))
10035 {
10036 struct glyph_row *pt_row, *row;
10037 struct glyph_row *first_reusable_row;
10038 struct glyph_row *first_row_to_display;
10039 int dy;
10040 int yb = window_text_bottom_y (w);
10041
10042 IF_DEBUG (debug_method_add (w, "twu2"));
10043
10044 /* Find the row starting at new_start, if there is one. Don't
10045 reuse a partially visible line at the end. */
10046 first_reusable_row = start_row;
10047 while (first_reusable_row->enabled_p
10048 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10049 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10050 < CHARPOS (new_start)))
10051 ++first_reusable_row;
10052
10053 /* Give up if there is no row to reuse. */
10054 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10055 || !first_reusable_row->enabled_p
10056 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10057 != CHARPOS (new_start)))
10058 return 0;
10059
10060 /* We can reuse fully visible rows beginning with
10061 first_reusable_row to the end of the window. Set
10062 first_row_to_display to the first row that cannot be reused.
10063 Set pt_row to the row containing point, if there is any. */
10064 first_row_to_display = first_reusable_row;
10065 pt_row = NULL;
10066 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10067 {
10068 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10069 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10070 pt_row = first_row_to_display;
10071
10072 ++first_row_to_display;
10073 }
10074
10075 /* Start displaying at the start of first_row_to_display. */
10076 xassert (first_row_to_display->y < yb);
10077 init_to_row_start (&it, w, first_row_to_display);
10078 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10079 - start_vpos);
10080 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10081 - nrows_scrolled);
10082 it.current_y = (first_row_to_display->y - first_reusable_row->y
10083 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10084
10085 /* Display lines beginning with first_row_to_display in the
10086 desired matrix. Set last_text_row to the last row displayed
10087 that displays text. */
10088 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10089 if (pt_row == NULL)
10090 w->cursor.vpos = -1;
10091 last_text_row = NULL;
10092 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10093 if (display_line (&it))
10094 last_text_row = it.glyph_row - 1;
10095
10096 /* Give up If point isn't in a row displayed or reused. */
10097 if (w->cursor.vpos < 0)
10098 {
10099 clear_glyph_matrix (w->desired_matrix);
10100 return 0;
10101 }
10102
10103 /* If point is in a reused row, adjust y and vpos of the cursor
10104 position. */
10105 if (pt_row)
10106 {
10107 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10108 w->current_matrix);
10109 w->cursor.y -= first_reusable_row->y;
10110 }
10111
10112 /* Scroll the display. */
10113 run.current_y = first_reusable_row->y;
10114 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10115 run.height = it.last_visible_y - run.current_y;
10116 dy = run.current_y - run.desired_y;
10117
10118 if (run.height)
10119 {
10120 struct frame *f = XFRAME (WINDOW_FRAME (w));
10121 update_begin (f);
10122 rif->update_window_begin_hook (w);
10123 rif->clear_mouse_face (w);
10124 rif->scroll_run_hook (w, &run);
10125 rif->update_window_end_hook (w, 0, 0);
10126 update_end (f);
10127 }
10128
10129 /* Adjust Y positions of reused rows. */
10130 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10131 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10132 max_y = it.last_visible_y;
10133 for (row = first_reusable_row; row < first_row_to_display; ++row)
10134 {
10135 row->y -= dy;
10136 if (row->y < min_y)
10137 row->visible_height = row->height - (min_y - row->y);
10138 else if (row->y + row->height > max_y)
10139 row->visible_height
10140 = row->height - (row->y + row->height - max_y);
10141 else
10142 row->visible_height = row->height;
10143 }
10144
10145 /* Disable rows not reused. */
10146 while (row < bottom_row)
10147 {
10148 row->enabled_p = 0;
10149 ++row;
10150 }
10151
10152 /* Scroll the current matrix. */
10153 xassert (nrows_scrolled > 0);
10154 rotate_matrix (w->current_matrix,
10155 start_vpos,
10156 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10157 -nrows_scrolled);
10158
10159 /* Adjust window end. A null value of last_text_row means that
10160 the window end is in reused rows which in turn means that
10161 only its vpos can have changed. */
10162 if (last_text_row)
10163 {
10164 w->window_end_bytepos
10165 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10166 XSETFASTINT (w->window_end_pos,
10167 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10168 XSETFASTINT (w->window_end_vpos,
10169 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10170 }
10171 else
10172 {
10173 XSETFASTINT (w->window_end_vpos,
10174 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10175 }
10176
10177 w->window_end_valid = Qnil;
10178 w->desired_matrix->no_scrolling_p = 1;
10179
10180 #if GLYPH_DEBUG
10181 debug_method_add (w, "try_window_reusing_current_matrix 2");
10182 #endif
10183 return 1;
10184 }
10185
10186 return 0;
10187 }
10188
10189
10190 \f
10191 /************************************************************************
10192 Window redisplay reusing current matrix when buffer has changed
10193 ************************************************************************/
10194
10195 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10196 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10197 int *, int *));
10198 static struct glyph_row *
10199 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10200 struct glyph_row *));
10201
10202
10203 /* Return the last row in MATRIX displaying text. If row START is
10204 non-null, start searching with that row. IT gives the dimensions
10205 of the display. Value is null if matrix is empty; otherwise it is
10206 a pointer to the row found. */
10207
10208 static struct glyph_row *
10209 find_last_row_displaying_text (matrix, it, start)
10210 struct glyph_matrix *matrix;
10211 struct it *it;
10212 struct glyph_row *start;
10213 {
10214 struct glyph_row *row, *row_found;
10215
10216 /* Set row_found to the last row in IT->w's current matrix
10217 displaying text. The loop looks funny but think of partially
10218 visible lines. */
10219 row_found = NULL;
10220 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10221 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10222 {
10223 xassert (row->enabled_p);
10224 row_found = row;
10225 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10226 break;
10227 ++row;
10228 }
10229
10230 return row_found;
10231 }
10232
10233
10234 /* Return the last row in the current matrix of W that is not affected
10235 by changes at the start of current_buffer that occurred since the
10236 last time W was redisplayed. Value is null if no such row exists.
10237
10238 The global variable beg_unchanged has to contain the number of
10239 bytes unchanged at the start of current_buffer. BEG +
10240 beg_unchanged is the buffer position of the first changed byte in
10241 current_buffer. Characters at positions < BEG + beg_unchanged are
10242 at the same buffer positions as they were when the current matrix
10243 was built. */
10244
10245 static struct glyph_row *
10246 find_last_unchanged_at_beg_row (w)
10247 struct window *w;
10248 {
10249 int first_changed_pos = BEG + BEG_UNCHANGED;
10250 struct glyph_row *row;
10251 struct glyph_row *row_found = NULL;
10252 int yb = window_text_bottom_y (w);
10253
10254 /* Find the last row displaying unchanged text. */
10255 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10256 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10257 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10258 {
10259 if (/* If row ends before first_changed_pos, it is unchanged,
10260 except in some case. */
10261 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10262 /* When row ends in ZV and we write at ZV it is not
10263 unchanged. */
10264 && !row->ends_at_zv_p
10265 /* When first_changed_pos is the end of a continued line,
10266 row is not unchanged because it may be no longer
10267 continued. */
10268 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10269 && row->continued_p))
10270 row_found = row;
10271
10272 /* Stop if last visible row. */
10273 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10274 break;
10275
10276 ++row;
10277 }
10278
10279 return row_found;
10280 }
10281
10282
10283 /* Find the first glyph row in the current matrix of W that is not
10284 affected by changes at the end of current_buffer since the last
10285 time the window was redisplayed. Return in *DELTA the number of
10286 chars by which buffer positions in unchanged text at the end of
10287 current_buffer must be adjusted. Return in *DELTA_BYTES the
10288 corresponding number of bytes. Value is null if no such row
10289 exists, i.e. all rows are affected by changes. */
10290
10291 static struct glyph_row *
10292 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10293 struct window *w;
10294 int *delta, *delta_bytes;
10295 {
10296 struct glyph_row *row;
10297 struct glyph_row *row_found = NULL;
10298
10299 *delta = *delta_bytes = 0;
10300
10301 /* Display must not have been paused, otherwise the current matrix
10302 is not up to date. */
10303 if (NILP (w->window_end_valid))
10304 abort ();
10305
10306 /* A value of window_end_pos >= END_UNCHANGED means that the window
10307 end is in the range of changed text. If so, there is no
10308 unchanged row at the end of W's current matrix. */
10309 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10310 return NULL;
10311
10312 /* Set row to the last row in W's current matrix displaying text. */
10313 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10314
10315 /* If matrix is entirely empty, no unchanged row exists. */
10316 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10317 {
10318 /* The value of row is the last glyph row in the matrix having a
10319 meaningful buffer position in it. The end position of row
10320 corresponds to window_end_pos. This allows us to translate
10321 buffer positions in the current matrix to current buffer
10322 positions for characters not in changed text. */
10323 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10324 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10325 int last_unchanged_pos, last_unchanged_pos_old;
10326 struct glyph_row *first_text_row
10327 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10328
10329 *delta = Z - Z_old;
10330 *delta_bytes = Z_BYTE - Z_BYTE_old;
10331
10332 /* Set last_unchanged_pos to the buffer position of the last
10333 character in the buffer that has not been changed. Z is the
10334 index + 1 of the last byte in current_buffer, i.e. by
10335 subtracting end_unchanged we get the index of the last
10336 unchanged character, and we have to add BEG to get its buffer
10337 position. */
10338 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10339 last_unchanged_pos_old = last_unchanged_pos - *delta;
10340
10341 /* Search backward from ROW for a row displaying a line that
10342 starts at a minimum position >= last_unchanged_pos_old. */
10343 for (; row > first_text_row; --row)
10344 {
10345 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10346 abort ();
10347
10348 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10349 row_found = row;
10350 }
10351 }
10352
10353 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10354 abort ();
10355
10356 return row_found;
10357 }
10358
10359
10360 /* Make sure that glyph rows in the current matrix of window W
10361 reference the same glyph memory as corresponding rows in the
10362 frame's frame matrix. This function is called after scrolling W's
10363 current matrix on a terminal frame in try_window_id and
10364 try_window_reusing_current_matrix. */
10365
10366 static void
10367 sync_frame_with_window_matrix_rows (w)
10368 struct window *w;
10369 {
10370 struct frame *f = XFRAME (w->frame);
10371 struct glyph_row *window_row, *window_row_end, *frame_row;
10372
10373 /* Preconditions: W must be a leaf window and full-width. Its frame
10374 must have a frame matrix. */
10375 xassert (NILP (w->hchild) && NILP (w->vchild));
10376 xassert (WINDOW_FULL_WIDTH_P (w));
10377 xassert (!FRAME_WINDOW_P (f));
10378
10379 /* If W is a full-width window, glyph pointers in W's current matrix
10380 have, by definition, to be the same as glyph pointers in the
10381 corresponding frame matrix. */
10382 window_row = w->current_matrix->rows;
10383 window_row_end = window_row + w->current_matrix->nrows;
10384 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10385 while (window_row < window_row_end)
10386 {
10387 int area;
10388
10389 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10390 frame_row->glyphs[area] = window_row->glyphs[area];
10391
10392 /* Disable frame rows whose corresponding window rows have
10393 been disabled in try_window_id. */
10394 if (!window_row->enabled_p)
10395 frame_row->enabled_p = 0;
10396
10397 ++window_row, ++frame_row;
10398 }
10399 }
10400
10401
10402 /* Find the glyph row in window W containing CHARPOS. Consider all
10403 rows between START and END (not inclusive). END null means search
10404 all rows to the end of the display area of W. Value is the row
10405 containing CHARPOS or null. */
10406
10407 static struct glyph_row *
10408 row_containing_pos (w, charpos, start, end)
10409 struct window *w;
10410 int charpos;
10411 struct glyph_row *start, *end;
10412 {
10413 struct glyph_row *row = start;
10414 int last_y;
10415
10416 /* If we happen to start on a header-line, skip that. */
10417 if (row->mode_line_p)
10418 ++row;
10419
10420 if ((end && row >= end) || !row->enabled_p)
10421 return NULL;
10422
10423 last_y = window_text_bottom_y (w);
10424
10425 while ((end == NULL || row < end)
10426 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10427 /* The end position of a row equals the start
10428 position of the next row. If CHARPOS is there, we
10429 would rather display it in the next line, except
10430 when this line ends in ZV. */
10431 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10432 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10433 || !row->ends_at_zv_p)))
10434 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10435 ++row;
10436
10437 /* Give up if CHARPOS not found. */
10438 if ((end && row >= end)
10439 || charpos < MATRIX_ROW_START_CHARPOS (row)
10440 || charpos > MATRIX_ROW_END_CHARPOS (row))
10441 row = NULL;
10442
10443 return row;
10444 }
10445
10446
10447 /* Try to redisplay window W by reusing its existing display. W's
10448 current matrix must be up to date when this function is called,
10449 i.e. window_end_valid must not be nil.
10450
10451 Value is
10452
10453 1 if display has been updated
10454 0 if otherwise unsuccessful
10455 -1 if redisplay with same window start is known not to succeed
10456
10457 The following steps are performed:
10458
10459 1. Find the last row in the current matrix of W that is not
10460 affected by changes at the start of current_buffer. If no such row
10461 is found, give up.
10462
10463 2. Find the first row in W's current matrix that is not affected by
10464 changes at the end of current_buffer. Maybe there is no such row.
10465
10466 3. Display lines beginning with the row + 1 found in step 1 to the
10467 row found in step 2 or, if step 2 didn't find a row, to the end of
10468 the window.
10469
10470 4. If cursor is not known to appear on the window, give up.
10471
10472 5. If display stopped at the row found in step 2, scroll the
10473 display and current matrix as needed.
10474
10475 6. Maybe display some lines at the end of W, if we must. This can
10476 happen under various circumstances, like a partially visible line
10477 becoming fully visible, or because newly displayed lines are displayed
10478 in smaller font sizes.
10479
10480 7. Update W's window end information. */
10481
10482 /* Check that window end is what we expect it to be. */
10483
10484 static int
10485 try_window_id (w)
10486 struct window *w;
10487 {
10488 struct frame *f = XFRAME (w->frame);
10489 struct glyph_matrix *current_matrix = w->current_matrix;
10490 struct glyph_matrix *desired_matrix = w->desired_matrix;
10491 struct glyph_row *last_unchanged_at_beg_row;
10492 struct glyph_row *first_unchanged_at_end_row;
10493 struct glyph_row *row;
10494 struct glyph_row *bottom_row;
10495 int bottom_vpos;
10496 struct it it;
10497 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10498 struct text_pos start_pos;
10499 struct run run;
10500 int first_unchanged_at_end_vpos = 0;
10501 struct glyph_row *last_text_row, *last_text_row_at_end;
10502 struct text_pos start;
10503
10504 SET_TEXT_POS_FROM_MARKER (start, w->start);
10505
10506 /* Check pre-conditions. Window end must be valid, otherwise
10507 the current matrix would not be up to date. */
10508 xassert (!NILP (w->window_end_valid));
10509 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10510 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10511
10512 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10513 only if buffer has really changed. The reason is that the gap is
10514 initially at Z for freshly visited files. The code below would
10515 set end_unchanged to 0 in that case. */
10516 if (MODIFF > SAVE_MODIFF
10517 /* This seems to happen sometimes after saving a buffer. */
10518 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10519 {
10520 if (GPT - BEG < BEG_UNCHANGED)
10521 BEG_UNCHANGED = GPT - BEG;
10522 if (Z - GPT < END_UNCHANGED)
10523 END_UNCHANGED = Z - GPT;
10524 }
10525
10526 /* If window starts after a line end, and the last change is in
10527 front of that newline, then changes don't affect the display.
10528 This case happens with stealth-fontification. Note that although
10529 the display is unchanged, glyph positions in the matrix have to
10530 be adjusted, of course. */
10531 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10532 if (CHARPOS (start) > BEGV
10533 && Z - END_UNCHANGED < CHARPOS (start) - 1
10534 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10535 && PT < MATRIX_ROW_END_CHARPOS (row))
10536 {
10537 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10538 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10539
10540 if (delta)
10541 {
10542 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10543 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10544
10545 increment_matrix_positions (w->current_matrix,
10546 MATRIX_ROW_VPOS (r0, current_matrix),
10547 MATRIX_ROW_VPOS (r1, current_matrix),
10548 delta, delta_bytes);
10549 }
10550
10551 #if 0 /* If changes are all in front of the window start, the
10552 distance of the last displayed glyph from Z hasn't
10553 changed. */
10554 w->window_end_pos
10555 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10556 w->window_end_bytepos
10557 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10558 #endif
10559
10560 return 1;
10561 }
10562
10563 /* Return quickly if changes are all below what is displayed in the
10564 window, and if PT is in the window. */
10565 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10566 && PT < MATRIX_ROW_END_CHARPOS (row))
10567 {
10568 /* We have to update window end positions because the buffer's
10569 size has changed. */
10570 w->window_end_pos
10571 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10572 w->window_end_bytepos
10573 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10574
10575 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10576 row = row_containing_pos (w, PT, row, NULL);
10577 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10578 return 2;
10579 }
10580
10581 /* Check that window start agrees with the start of the first glyph
10582 row in its current matrix. Check this after we know the window
10583 start is not in changed text, otherwise positions would not be
10584 comparable. */
10585 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10586 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10587 return 0;
10588
10589 /* Compute the position at which we have to start displaying new
10590 lines. Some of the lines at the top of the window might be
10591 reusable because they are not displaying changed text. Find the
10592 last row in W's current matrix not affected by changes at the
10593 start of current_buffer. Value is null if changes start in the
10594 first line of window. */
10595 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10596 if (last_unchanged_at_beg_row)
10597 {
10598 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10599 start_pos = it.current.pos;
10600
10601 /* Start displaying new lines in the desired matrix at the same
10602 vpos we would use in the current matrix, i.e. below
10603 last_unchanged_at_beg_row. */
10604 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10605 current_matrix);
10606 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10607 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10608
10609 xassert (it.hpos == 0 && it.current_x == 0);
10610 }
10611 else
10612 {
10613 /* There are no reusable lines at the start of the window.
10614 Start displaying in the first line. */
10615 start_display (&it, w, start);
10616 start_pos = it.current.pos;
10617 }
10618
10619 /* Find the first row that is not affected by changes at the end of
10620 the buffer. Value will be null if there is no unchanged row, in
10621 which case we must redisplay to the end of the window. delta
10622 will be set to the value by which buffer positions beginning with
10623 first_unchanged_at_end_row have to be adjusted due to text
10624 changes. */
10625 first_unchanged_at_end_row
10626 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10627 IF_DEBUG (debug_delta = delta);
10628 IF_DEBUG (debug_delta_bytes = delta_bytes);
10629
10630 /* Set stop_pos to the buffer position up to which we will have to
10631 display new lines. If first_unchanged_at_end_row != NULL, this
10632 is the buffer position of the start of the line displayed in that
10633 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10634 that we don't stop at a buffer position. */
10635 stop_pos = 0;
10636 if (first_unchanged_at_end_row)
10637 {
10638 xassert (last_unchanged_at_beg_row == NULL
10639 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10640
10641 /* If this is a continuation line, move forward to the next one
10642 that isn't. Changes in lines above affect this line.
10643 Caution: this may move first_unchanged_at_end_row to a row
10644 not displaying text. */
10645 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10646 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10647 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10648 < it.last_visible_y))
10649 ++first_unchanged_at_end_row;
10650
10651 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10652 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10653 >= it.last_visible_y))
10654 first_unchanged_at_end_row = NULL;
10655 else
10656 {
10657 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10658 + delta);
10659 first_unchanged_at_end_vpos
10660 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10661 xassert (stop_pos >= Z - END_UNCHANGED);
10662 }
10663 }
10664 else if (last_unchanged_at_beg_row == NULL)
10665 return 0;
10666
10667
10668 #if GLYPH_DEBUG
10669
10670 /* Either there is no unchanged row at the end, or the one we have
10671 now displays text. This is a necessary condition for the window
10672 end pos calculation at the end of this function. */
10673 xassert (first_unchanged_at_end_row == NULL
10674 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10675
10676 debug_last_unchanged_at_beg_vpos
10677 = (last_unchanged_at_beg_row
10678 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10679 : -1);
10680 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10681
10682 #endif /* GLYPH_DEBUG != 0 */
10683
10684
10685 /* Display new lines. Set last_text_row to the last new line
10686 displayed which has text on it, i.e. might end up as being the
10687 line where the window_end_vpos is. */
10688 w->cursor.vpos = -1;
10689 last_text_row = NULL;
10690 overlay_arrow_seen = 0;
10691 while (it.current_y < it.last_visible_y
10692 && !fonts_changed_p
10693 && (first_unchanged_at_end_row == NULL
10694 || IT_CHARPOS (it) < stop_pos))
10695 {
10696 if (display_line (&it))
10697 last_text_row = it.glyph_row - 1;
10698 }
10699
10700 if (fonts_changed_p)
10701 return -1;
10702
10703
10704 /* Compute differences in buffer positions, y-positions etc. for
10705 lines reused at the bottom of the window. Compute what we can
10706 scroll. */
10707 if (first_unchanged_at_end_row
10708 /* No lines reused because we displayed everything up to the
10709 bottom of the window. */
10710 && it.current_y < it.last_visible_y)
10711 {
10712 dvpos = (it.vpos
10713 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10714 current_matrix));
10715 dy = it.current_y - first_unchanged_at_end_row->y;
10716 run.current_y = first_unchanged_at_end_row->y;
10717 run.desired_y = run.current_y + dy;
10718 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10719 }
10720 else
10721 {
10722 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10723 first_unchanged_at_end_row = NULL;
10724 }
10725 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10726
10727
10728 /* Find the cursor if not already found. We have to decide whether
10729 PT will appear on this window (it sometimes doesn't, but this is
10730 not a very frequent case.) This decision has to be made before
10731 the current matrix is altered. A value of cursor.vpos < 0 means
10732 that PT is either in one of the lines beginning at
10733 first_unchanged_at_end_row or below the window. Don't care for
10734 lines that might be displayed later at the window end; as
10735 mentioned, this is not a frequent case. */
10736 if (w->cursor.vpos < 0)
10737 {
10738 /* Cursor in unchanged rows at the top? */
10739 if (PT < CHARPOS (start_pos)
10740 && last_unchanged_at_beg_row)
10741 {
10742 row = row_containing_pos (w, PT,
10743 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10744 last_unchanged_at_beg_row + 1);
10745 if (row)
10746 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10747 }
10748
10749 /* Start from first_unchanged_at_end_row looking for PT. */
10750 else if (first_unchanged_at_end_row)
10751 {
10752 row = row_containing_pos (w, PT - delta,
10753 first_unchanged_at_end_row, NULL);
10754 if (row)
10755 set_cursor_from_row (w, row, w->current_matrix, delta,
10756 delta_bytes, dy, dvpos);
10757 }
10758
10759 /* Give up if cursor was not found. */
10760 if (w->cursor.vpos < 0)
10761 {
10762 clear_glyph_matrix (w->desired_matrix);
10763 return -1;
10764 }
10765 }
10766
10767 /* Don't let the cursor end in the scroll margins. */
10768 {
10769 int this_scroll_margin, cursor_height;
10770
10771 this_scroll_margin = max (0, scroll_margin);
10772 this_scroll_margin = min (this_scroll_margin,
10773 XFASTINT (w->height) / 4);
10774 this_scroll_margin *= CANON_Y_UNIT (it.f);
10775 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
10776
10777 if ((w->cursor.y < this_scroll_margin
10778 && CHARPOS (start) > BEGV)
10779 /* Don't take scroll margin into account at the bottom because
10780 old redisplay didn't do it either. */
10781 || w->cursor.y + cursor_height > it.last_visible_y)
10782 {
10783 w->cursor.vpos = -1;
10784 clear_glyph_matrix (w->desired_matrix);
10785 return -1;
10786 }
10787 }
10788
10789 /* Scroll the display. Do it before changing the current matrix so
10790 that xterm.c doesn't get confused about where the cursor glyph is
10791 found. */
10792 if (dy && run.height)
10793 {
10794 update_begin (f);
10795
10796 if (FRAME_WINDOW_P (f))
10797 {
10798 rif->update_window_begin_hook (w);
10799 rif->clear_mouse_face (w);
10800 rif->scroll_run_hook (w, &run);
10801 rif->update_window_end_hook (w, 0, 0);
10802 }
10803 else
10804 {
10805 /* Terminal frame. In this case, dvpos gives the number of
10806 lines to scroll by; dvpos < 0 means scroll up. */
10807 int first_unchanged_at_end_vpos
10808 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
10809 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
10810 int end = XFASTINT (w->top) + window_internal_height (w);
10811
10812 /* Perform the operation on the screen. */
10813 if (dvpos > 0)
10814 {
10815 /* Scroll last_unchanged_at_beg_row to the end of the
10816 window down dvpos lines. */
10817 set_terminal_window (end);
10818
10819 /* On dumb terminals delete dvpos lines at the end
10820 before inserting dvpos empty lines. */
10821 if (!scroll_region_ok)
10822 ins_del_lines (end - dvpos, -dvpos);
10823
10824 /* Insert dvpos empty lines in front of
10825 last_unchanged_at_beg_row. */
10826 ins_del_lines (from, dvpos);
10827 }
10828 else if (dvpos < 0)
10829 {
10830 /* Scroll up last_unchanged_at_beg_vpos to the end of
10831 the window to last_unchanged_at_beg_vpos - |dvpos|. */
10832 set_terminal_window (end);
10833
10834 /* Delete dvpos lines in front of
10835 last_unchanged_at_beg_vpos. ins_del_lines will set
10836 the cursor to the given vpos and emit |dvpos| delete
10837 line sequences. */
10838 ins_del_lines (from + dvpos, dvpos);
10839
10840 /* On a dumb terminal insert dvpos empty lines at the
10841 end. */
10842 if (!scroll_region_ok)
10843 ins_del_lines (end + dvpos, -dvpos);
10844 }
10845
10846 set_terminal_window (0);
10847 }
10848
10849 update_end (f);
10850 }
10851
10852 /* Shift reused rows of the current matrix to the right position.
10853 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
10854 text. */
10855 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10856 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
10857 if (dvpos < 0)
10858 {
10859 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
10860 bottom_vpos, dvpos);
10861 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
10862 bottom_vpos, 0);
10863 }
10864 else if (dvpos > 0)
10865 {
10866 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
10867 bottom_vpos, dvpos);
10868 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
10869 first_unchanged_at_end_vpos + dvpos, 0);
10870 }
10871
10872 /* For frame-based redisplay, make sure that current frame and window
10873 matrix are in sync with respect to glyph memory. */
10874 if (!FRAME_WINDOW_P (f))
10875 sync_frame_with_window_matrix_rows (w);
10876
10877 /* Adjust buffer positions in reused rows. */
10878 if (delta)
10879 increment_matrix_positions (current_matrix,
10880 first_unchanged_at_end_vpos + dvpos,
10881 bottom_vpos, delta, delta_bytes);
10882
10883 /* Adjust Y positions. */
10884 if (dy)
10885 shift_glyph_matrix (w, current_matrix,
10886 first_unchanged_at_end_vpos + dvpos,
10887 bottom_vpos, dy);
10888
10889 if (first_unchanged_at_end_row)
10890 first_unchanged_at_end_row += dvpos;
10891
10892 /* If scrolling up, there may be some lines to display at the end of
10893 the window. */
10894 last_text_row_at_end = NULL;
10895 if (dy < 0)
10896 {
10897 /* Set last_row to the glyph row in the current matrix where the
10898 window end line is found. It has been moved up or down in
10899 the matrix by dvpos. */
10900 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
10901 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
10902
10903 /* If last_row is the window end line, it should display text. */
10904 xassert (last_row->displays_text_p);
10905
10906 /* If window end line was partially visible before, begin
10907 displaying at that line. Otherwise begin displaying with the
10908 line following it. */
10909 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
10910 {
10911 init_to_row_start (&it, w, last_row);
10912 it.vpos = last_vpos;
10913 it.current_y = last_row->y;
10914 }
10915 else
10916 {
10917 init_to_row_end (&it, w, last_row);
10918 it.vpos = 1 + last_vpos;
10919 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
10920 ++last_row;
10921 }
10922
10923 /* We may start in a continuation line. If so, we have to get
10924 the right continuation_lines_width and current_x. */
10925 it.continuation_lines_width = last_row->continuation_lines_width;
10926 it.hpos = it.current_x = 0;
10927
10928 /* Display the rest of the lines at the window end. */
10929 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10930 while (it.current_y < it.last_visible_y
10931 && !fonts_changed_p)
10932 {
10933 /* Is it always sure that the display agrees with lines in
10934 the current matrix? I don't think so, so we mark rows
10935 displayed invalid in the current matrix by setting their
10936 enabled_p flag to zero. */
10937 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
10938 if (display_line (&it))
10939 last_text_row_at_end = it.glyph_row - 1;
10940 }
10941 }
10942
10943 /* Update window_end_pos and window_end_vpos. */
10944 if (first_unchanged_at_end_row
10945 && first_unchanged_at_end_row->y < it.last_visible_y
10946 && !last_text_row_at_end)
10947 {
10948 /* Window end line if one of the preserved rows from the current
10949 matrix. Set row to the last row displaying text in current
10950 matrix starting at first_unchanged_at_end_row, after
10951 scrolling. */
10952 xassert (first_unchanged_at_end_row->displays_text_p);
10953 row = find_last_row_displaying_text (w->current_matrix, &it,
10954 first_unchanged_at_end_row);
10955 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
10956
10957 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
10958 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10959 XSETFASTINT (w->window_end_vpos,
10960 MATRIX_ROW_VPOS (row, w->current_matrix));
10961 }
10962 else if (last_text_row_at_end)
10963 {
10964 XSETFASTINT (w->window_end_pos,
10965 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
10966 w->window_end_bytepos
10967 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
10968 XSETFASTINT (w->window_end_vpos,
10969 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
10970 }
10971 else if (last_text_row)
10972 {
10973 /* We have displayed either to the end of the window or at the
10974 end of the window, i.e. the last row with text is to be found
10975 in the desired matrix. */
10976 XSETFASTINT (w->window_end_pos,
10977 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10978 w->window_end_bytepos
10979 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10980 XSETFASTINT (w->window_end_vpos,
10981 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
10982 }
10983 else if (first_unchanged_at_end_row == NULL
10984 && last_text_row == NULL
10985 && last_text_row_at_end == NULL)
10986 {
10987 /* Displayed to end of window, but no line containing text was
10988 displayed. Lines were deleted at the end of the window. */
10989 int vpos;
10990 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
10991
10992 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
10993 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
10994 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
10995 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
10996 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
10997 break;
10998
10999 w->window_end_vpos = make_number (vpos);
11000 }
11001 else
11002 abort ();
11003
11004 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11005 debug_end_vpos = XFASTINT (w->window_end_vpos));
11006
11007 /* Record that display has not been completed. */
11008 w->window_end_valid = Qnil;
11009 w->desired_matrix->no_scrolling_p = 1;
11010 return 3;
11011 }
11012
11013
11014 \f
11015 /***********************************************************************
11016 More debugging support
11017 ***********************************************************************/
11018
11019 #if GLYPH_DEBUG
11020
11021 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11022 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11023
11024
11025 /* Dump the contents of glyph matrix MATRIX on stderr. If
11026 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11027
11028 static void
11029 dump_glyph_matrix (matrix, with_glyphs_p)
11030 struct glyph_matrix *matrix;
11031 int with_glyphs_p;
11032 {
11033 int i;
11034 for (i = 0; i < matrix->nrows; ++i)
11035 dump_glyph_row (matrix, i, with_glyphs_p);
11036 }
11037
11038
11039 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11040 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11041
11042 void
11043 dump_glyph_row (matrix, vpos, with_glyphs_p)
11044 struct glyph_matrix *matrix;
11045 int vpos, with_glyphs_p;
11046 {
11047 struct glyph_row *row;
11048
11049 if (vpos < 0 || vpos >= matrix->nrows)
11050 return;
11051
11052 row = MATRIX_ROW (matrix, vpos);
11053
11054 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11055 fprintf (stderr, "=======================================================================\n");
11056
11057 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11058 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11059 row - matrix->rows,
11060 MATRIX_ROW_START_CHARPOS (row),
11061 MATRIX_ROW_END_CHARPOS (row),
11062 row->used[TEXT_AREA],
11063 row->contains_overlapping_glyphs_p,
11064 row->enabled_p,
11065 row->inverse_p,
11066 row->truncated_on_left_p,
11067 row->truncated_on_right_p,
11068 row->overlay_arrow_p,
11069 row->continued_p,
11070 MATRIX_ROW_CONTINUATION_LINE_P (row),
11071 row->displays_text_p,
11072 row->ends_at_zv_p,
11073 row->fill_line_p,
11074 row->ends_in_middle_of_char_p,
11075 row->starts_in_middle_of_char_p,
11076 row->x,
11077 row->y,
11078 row->pixel_width,
11079 row->height,
11080 row->visible_height,
11081 row->ascent,
11082 row->phys_ascent);
11083 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11084 row->end.overlay_string_index);
11085 fprintf (stderr, "%9d %5d\n",
11086 CHARPOS (row->start.string_pos),
11087 CHARPOS (row->end.string_pos));
11088 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11089 row->end.dpvec_index);
11090
11091 if (with_glyphs_p)
11092 {
11093 struct glyph *glyph, *glyph_end;
11094 int prev_had_glyphs_p;
11095
11096 glyph = row->glyphs[TEXT_AREA];
11097 glyph_end = glyph + row->used[TEXT_AREA];
11098
11099 /* Glyph for a line end in text. */
11100 if (glyph == glyph_end && glyph->charpos > 0)
11101 ++glyph_end;
11102
11103 if (glyph < glyph_end)
11104 {
11105 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11106 prev_had_glyphs_p = 1;
11107 }
11108 else
11109 prev_had_glyphs_p = 0;
11110
11111 while (glyph < glyph_end)
11112 {
11113 if (glyph->type == CHAR_GLYPH)
11114 {
11115 fprintf (stderr,
11116 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11117 glyph - row->glyphs[TEXT_AREA],
11118 'C',
11119 glyph->charpos,
11120 (BUFFERP (glyph->object)
11121 ? 'B'
11122 : (STRINGP (glyph->object)
11123 ? 'S'
11124 : '-')),
11125 glyph->pixel_width,
11126 glyph->u.ch,
11127 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11128 ? glyph->u.ch
11129 : '.'),
11130 glyph->face_id,
11131 glyph->left_box_line_p,
11132 glyph->right_box_line_p);
11133 }
11134 else if (glyph->type == STRETCH_GLYPH)
11135 {
11136 fprintf (stderr,
11137 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11138 glyph - row->glyphs[TEXT_AREA],
11139 'S',
11140 glyph->charpos,
11141 (BUFFERP (glyph->object)
11142 ? 'B'
11143 : (STRINGP (glyph->object)
11144 ? 'S'
11145 : '-')),
11146 glyph->pixel_width,
11147 0,
11148 '.',
11149 glyph->face_id,
11150 glyph->left_box_line_p,
11151 glyph->right_box_line_p);
11152 }
11153 else if (glyph->type == IMAGE_GLYPH)
11154 {
11155 fprintf (stderr,
11156 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11157 glyph - row->glyphs[TEXT_AREA],
11158 'I',
11159 glyph->charpos,
11160 (BUFFERP (glyph->object)
11161 ? 'B'
11162 : (STRINGP (glyph->object)
11163 ? 'S'
11164 : '-')),
11165 glyph->pixel_width,
11166 glyph->u.img_id,
11167 '.',
11168 glyph->face_id,
11169 glyph->left_box_line_p,
11170 glyph->right_box_line_p);
11171 }
11172 ++glyph;
11173 }
11174 }
11175 }
11176
11177
11178 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11179 Sdump_glyph_matrix, 0, 1, "p",
11180 "Dump the current matrix of the selected window to stderr.\n\
11181 Shows contents of glyph row structures. With non-nil optional\n\
11182 parameter WITH-GLYPHS-P, dump glyphs as well.")
11183 (with_glyphs_p)
11184 Lisp_Object with_glyphs_p;
11185 {
11186 struct window *w = XWINDOW (selected_window);
11187 struct buffer *buffer = XBUFFER (w->buffer);
11188
11189 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11190 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11191 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11192 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11193 fprintf (stderr, "=============================================\n");
11194 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11195 return Qnil;
11196 }
11197
11198
11199 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11200 "Dump glyph row ROW to stderr.")
11201 (row)
11202 Lisp_Object row;
11203 {
11204 CHECK_NUMBER (row, 0);
11205 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11206 return Qnil;
11207 }
11208
11209
11210 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11211 0, 0, "", "")
11212 ()
11213 {
11214 struct frame *sf = SELECTED_FRAME ();
11215 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11216 ->current_matrix);
11217 dump_glyph_row (m, 0, 1);
11218 return Qnil;
11219 }
11220
11221
11222 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11223 Strace_redisplay_toggle, 0, 0, "",
11224 "Toggle tracing of redisplay.")
11225 ()
11226 {
11227 trace_redisplay_p = !trace_redisplay_p;
11228 return Qnil;
11229 }
11230
11231
11232 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11233 "Print STRING to stderr.")
11234 (string)
11235 Lisp_Object string;
11236 {
11237 CHECK_STRING (string, 0);
11238 fprintf (stderr, "%s", XSTRING (string)->data);
11239 return Qnil;
11240 }
11241
11242 #endif /* GLYPH_DEBUG */
11243
11244
11245 \f
11246 /***********************************************************************
11247 Building Desired Matrix Rows
11248 ***********************************************************************/
11249
11250 /* Return a temporary glyph row holding the glyphs of an overlay
11251 arrow. Only used for non-window-redisplay windows. */
11252
11253 static struct glyph_row *
11254 get_overlay_arrow_glyph_row (w)
11255 struct window *w;
11256 {
11257 struct frame *f = XFRAME (WINDOW_FRAME (w));
11258 struct buffer *buffer = XBUFFER (w->buffer);
11259 struct buffer *old = current_buffer;
11260 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11261 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11262 unsigned char *arrow_end = arrow_string + arrow_len;
11263 unsigned char *p;
11264 struct it it;
11265 int multibyte_p;
11266 int n_glyphs_before;
11267
11268 set_buffer_temp (buffer);
11269 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11270 it.glyph_row->used[TEXT_AREA] = 0;
11271 SET_TEXT_POS (it.position, 0, 0);
11272
11273 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11274 p = arrow_string;
11275 while (p < arrow_end)
11276 {
11277 Lisp_Object face, ilisp;
11278
11279 /* Get the next character. */
11280 if (multibyte_p)
11281 it.c = string_char_and_length (p, arrow_len, &it.len);
11282 else
11283 it.c = *p, it.len = 1;
11284 p += it.len;
11285
11286 /* Get its face. */
11287 XSETFASTINT (ilisp, p - arrow_string);
11288 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11289 it.face_id = compute_char_face (f, it.c, face);
11290
11291 /* Compute its width, get its glyphs. */
11292 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11293 SET_TEXT_POS (it.position, -1, -1);
11294 PRODUCE_GLYPHS (&it);
11295
11296 /* If this character doesn't fit any more in the line, we have
11297 to remove some glyphs. */
11298 if (it.current_x > it.last_visible_x)
11299 {
11300 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11301 break;
11302 }
11303 }
11304
11305 set_buffer_temp (old);
11306 return it.glyph_row;
11307 }
11308
11309
11310 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11311 glyphs are only inserted for terminal frames since we can't really
11312 win with truncation glyphs when partially visible glyphs are
11313 involved. Which glyphs to insert is determined by
11314 produce_special_glyphs. */
11315
11316 static void
11317 insert_left_trunc_glyphs (it)
11318 struct it *it;
11319 {
11320 struct it truncate_it;
11321 struct glyph *from, *end, *to, *toend;
11322
11323 xassert (!FRAME_WINDOW_P (it->f));
11324
11325 /* Get the truncation glyphs. */
11326 truncate_it = *it;
11327 truncate_it.current_x = 0;
11328 truncate_it.face_id = DEFAULT_FACE_ID;
11329 truncate_it.glyph_row = &scratch_glyph_row;
11330 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11331 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11332 truncate_it.object = make_number (0);
11333 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11334
11335 /* Overwrite glyphs from IT with truncation glyphs. */
11336 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11337 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11338 to = it->glyph_row->glyphs[TEXT_AREA];
11339 toend = to + it->glyph_row->used[TEXT_AREA];
11340
11341 while (from < end)
11342 *to++ = *from++;
11343
11344 /* There may be padding glyphs left over. Remove them. */
11345 from = to;
11346 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11347 ++from;
11348 while (from < toend)
11349 *to++ = *from++;
11350
11351 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11352 }
11353
11354
11355 /* Compute the pixel height and width of IT->glyph_row.
11356
11357 Most of the time, ascent and height of a display line will be equal
11358 to the max_ascent and max_height values of the display iterator
11359 structure. This is not the case if
11360
11361 1. We hit ZV without displaying anything. In this case, max_ascent
11362 and max_height will be zero.
11363
11364 2. We have some glyphs that don't contribute to the line height.
11365 (The glyph row flag contributes_to_line_height_p is for future
11366 pixmap extensions).
11367
11368 The first case is easily covered by using default values because in
11369 these cases, the line height does not really matter, except that it
11370 must not be zero. */
11371
11372 static void
11373 compute_line_metrics (it)
11374 struct it *it;
11375 {
11376 struct glyph_row *row = it->glyph_row;
11377 int area, i;
11378
11379 if (FRAME_WINDOW_P (it->f))
11380 {
11381 int i, header_line_height;
11382
11383 /* The line may consist of one space only, that was added to
11384 place the cursor on it. If so, the row's height hasn't been
11385 computed yet. */
11386 if (row->height == 0)
11387 {
11388 if (it->max_ascent + it->max_descent == 0)
11389 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11390 row->ascent = it->max_ascent;
11391 row->height = it->max_ascent + it->max_descent;
11392 row->phys_ascent = it->max_phys_ascent;
11393 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11394 }
11395
11396 /* Compute the width of this line. */
11397 row->pixel_width = row->x;
11398 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11399 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11400
11401 xassert (row->pixel_width >= 0);
11402 xassert (row->ascent >= 0 && row->height > 0);
11403
11404 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11405 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11406
11407 /* If first line's physical ascent is larger than its logical
11408 ascent, use the physical ascent, and make the row taller.
11409 This makes accented characters fully visible. */
11410 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11411 && row->phys_ascent > row->ascent)
11412 {
11413 row->height += row->phys_ascent - row->ascent;
11414 row->ascent = row->phys_ascent;
11415 }
11416
11417 /* Compute how much of the line is visible. */
11418 row->visible_height = row->height;
11419
11420 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11421 if (row->y < header_line_height)
11422 row->visible_height -= header_line_height - row->y;
11423 else
11424 {
11425 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11426 if (row->y + row->height > max_y)
11427 row->visible_height -= row->y + row->height - max_y;
11428 }
11429 }
11430 else
11431 {
11432 row->pixel_width = row->used[TEXT_AREA];
11433 row->ascent = row->phys_ascent = 0;
11434 row->height = row->phys_height = row->visible_height = 1;
11435 }
11436
11437 /* Compute a hash code for this row. */
11438 row->hash = 0;
11439 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11440 for (i = 0; i < row->used[area]; ++i)
11441 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11442 + row->glyphs[area][i].u.val
11443 + row->glyphs[area][i].face_id
11444 + row->glyphs[area][i].padding_p
11445 + (row->glyphs[area][i].type << 2));
11446
11447 it->max_ascent = it->max_descent = 0;
11448 it->max_phys_ascent = it->max_phys_descent = 0;
11449 }
11450
11451
11452 /* Append one space to the glyph row of iterator IT if doing a
11453 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11454 space have the default face, otherwise let it have the same face as
11455 IT->face_id. Value is non-zero if a space was added.
11456
11457 This function is called to make sure that there is always one glyph
11458 at the end of a glyph row that the cursor can be set on under
11459 window-systems. (If there weren't such a glyph we would not know
11460 how wide and tall a box cursor should be displayed).
11461
11462 At the same time this space let's a nicely handle clearing to the
11463 end of the line if the row ends in italic text. */
11464
11465 static int
11466 append_space (it, default_face_p)
11467 struct it *it;
11468 int default_face_p;
11469 {
11470 if (FRAME_WINDOW_P (it->f))
11471 {
11472 int n = it->glyph_row->used[TEXT_AREA];
11473
11474 if (it->glyph_row->glyphs[TEXT_AREA] + n
11475 < it->glyph_row->glyphs[1 + TEXT_AREA])
11476 {
11477 /* Save some values that must not be changed.
11478 Must save IT->c and IT->len because otherwise
11479 ITERATOR_AT_END_P wouldn't work anymore after
11480 append_space has been called. */
11481 int saved_what = it->what;
11482 int saved_c = it->c, saved_len = it->len;
11483 int saved_x = it->current_x;
11484 int saved_face_id = it->face_id;
11485 struct text_pos saved_pos;
11486 Lisp_Object saved_object;
11487 struct face *face;
11488
11489 saved_object = it->object;
11490 saved_pos = it->position;
11491
11492 it->what = IT_CHARACTER;
11493 bzero (&it->position, sizeof it->position);
11494 it->object = make_number (0);
11495 it->c = ' ';
11496 it->len = 1;
11497
11498 if (default_face_p)
11499 it->face_id = DEFAULT_FACE_ID;
11500 face = FACE_FROM_ID (it->f, it->face_id);
11501 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11502
11503 PRODUCE_GLYPHS (it);
11504
11505 it->current_x = saved_x;
11506 it->object = saved_object;
11507 it->position = saved_pos;
11508 it->what = saved_what;
11509 it->face_id = saved_face_id;
11510 it->len = saved_len;
11511 it->c = saved_c;
11512 return 1;
11513 }
11514 }
11515
11516 return 0;
11517 }
11518
11519
11520 /* Extend the face of the last glyph in the text area of IT->glyph_row
11521 to the end of the display line. Called from display_line.
11522 If the glyph row is empty, add a space glyph to it so that we
11523 know the face to draw. Set the glyph row flag fill_line_p. */
11524
11525 static void
11526 extend_face_to_end_of_line (it)
11527 struct it *it;
11528 {
11529 struct face *face;
11530 struct frame *f = it->f;
11531
11532 /* If line is already filled, do nothing. */
11533 if (it->current_x >= it->last_visible_x)
11534 return;
11535
11536 /* Face extension extends the background and box of IT->face_id
11537 to the end of the line. If the background equals the background
11538 of the frame, we haven't to do anything. */
11539 face = FACE_FROM_ID (f, it->face_id);
11540 if (FRAME_WINDOW_P (f)
11541 && face->box == FACE_NO_BOX
11542 && face->background == FRAME_BACKGROUND_PIXEL (f)
11543 && !face->stipple)
11544 return;
11545
11546 /* Set the glyph row flag indicating that the face of the last glyph
11547 in the text area has to be drawn to the end of the text area. */
11548 it->glyph_row->fill_line_p = 1;
11549
11550 /* If current character of IT is not ASCII, make sure we have the
11551 ASCII face. This will be automatically undone the next time
11552 get_next_display_element returns a multibyte character. Note
11553 that the character will always be single byte in unibyte text. */
11554 if (!SINGLE_BYTE_CHAR_P (it->c))
11555 {
11556 it->face_id = FACE_FOR_CHAR (f, face, 0);
11557 }
11558
11559 if (FRAME_WINDOW_P (f))
11560 {
11561 /* If the row is empty, add a space with the current face of IT,
11562 so that we know which face to draw. */
11563 if (it->glyph_row->used[TEXT_AREA] == 0)
11564 {
11565 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11566 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11567 it->glyph_row->used[TEXT_AREA] = 1;
11568 }
11569 }
11570 else
11571 {
11572 /* Save some values that must not be changed. */
11573 int saved_x = it->current_x;
11574 struct text_pos saved_pos;
11575 Lisp_Object saved_object;
11576 int saved_what = it->what;
11577
11578 saved_object = it->object;
11579 saved_pos = it->position;
11580
11581 it->what = IT_CHARACTER;
11582 bzero (&it->position, sizeof it->position);
11583 it->object = make_number (0);
11584 it->c = ' ';
11585 it->len = 1;
11586
11587 PRODUCE_GLYPHS (it);
11588
11589 while (it->current_x <= it->last_visible_x)
11590 PRODUCE_GLYPHS (it);
11591
11592 /* Don't count these blanks really. It would let us insert a left
11593 truncation glyph below and make us set the cursor on them, maybe. */
11594 it->current_x = saved_x;
11595 it->object = saved_object;
11596 it->position = saved_pos;
11597 it->what = saved_what;
11598 }
11599 }
11600
11601
11602 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11603 trailing whitespace. */
11604
11605 static int
11606 trailing_whitespace_p (charpos)
11607 int charpos;
11608 {
11609 int bytepos = CHAR_TO_BYTE (charpos);
11610 int c = 0;
11611
11612 while (bytepos < ZV_BYTE
11613 && (c = FETCH_CHAR (bytepos),
11614 c == ' ' || c == '\t'))
11615 ++bytepos;
11616
11617 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11618 {
11619 if (bytepos != PT_BYTE)
11620 return 1;
11621 }
11622 return 0;
11623 }
11624
11625
11626 /* Highlight trailing whitespace, if any, in ROW. */
11627
11628 void
11629 highlight_trailing_whitespace (f, row)
11630 struct frame *f;
11631 struct glyph_row *row;
11632 {
11633 int used = row->used[TEXT_AREA];
11634
11635 if (used)
11636 {
11637 struct glyph *start = row->glyphs[TEXT_AREA];
11638 struct glyph *glyph = start + used - 1;
11639
11640 /* Skip over the space glyph inserted to display the
11641 cursor at the end of a line. */
11642 if (glyph->type == CHAR_GLYPH
11643 && glyph->u.ch == ' '
11644 && INTEGERP (glyph->object))
11645 --glyph;
11646
11647 /* If last glyph is a space or stretch, and it's trailing
11648 whitespace, set the face of all trailing whitespace glyphs in
11649 IT->glyph_row to `trailing-whitespace'. */
11650 if (glyph >= start
11651 && BUFFERP (glyph->object)
11652 && (glyph->type == STRETCH_GLYPH
11653 || (glyph->type == CHAR_GLYPH
11654 && glyph->u.ch == ' '))
11655 && trailing_whitespace_p (glyph->charpos))
11656 {
11657 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11658
11659 while (glyph >= start
11660 && BUFFERP (glyph->object)
11661 && (glyph->type == STRETCH_GLYPH
11662 || (glyph->type == CHAR_GLYPH
11663 && glyph->u.ch == ' ')))
11664 (glyph--)->face_id = face_id;
11665 }
11666 }
11667 }
11668
11669
11670 /* Value is non-zero if glyph row ROW in window W should be
11671 used to put the cursor on. */
11672
11673 static int
11674 cursor_row_p (w, row)
11675 struct window *w;
11676 struct glyph_row *row;
11677 {
11678 int cursor_row_p = 1;
11679
11680 if (PT == MATRIX_ROW_END_CHARPOS (row))
11681 {
11682 /* If the row ends with a newline from a string, we don't want
11683 the cursor there (if the row is continued it doesn't end in a
11684 newline). */
11685 if (CHARPOS (row->end.string_pos) >= 0
11686 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11687 cursor_row_p = row->continued_p;
11688
11689 /* If the row ends at ZV, display the cursor at the end of that
11690 row instead of at the start of the row below. */
11691 else if (row->ends_at_zv_p)
11692 cursor_row_p = 1;
11693 else
11694 cursor_row_p = 0;
11695 }
11696
11697 return cursor_row_p;
11698 }
11699
11700
11701 /* Construct the glyph row IT->glyph_row in the desired matrix of
11702 IT->w from text at the current position of IT. See dispextern.h
11703 for an overview of struct it. Value is non-zero if
11704 IT->glyph_row displays text, as opposed to a line displaying ZV
11705 only. */
11706
11707 static int
11708 display_line (it)
11709 struct it *it;
11710 {
11711 struct glyph_row *row = it->glyph_row;
11712
11713 /* We always start displaying at hpos zero even if hscrolled. */
11714 xassert (it->hpos == 0 && it->current_x == 0);
11715
11716 /* We must not display in a row that's not a text row. */
11717 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11718 < it->w->desired_matrix->nrows);
11719
11720 /* Is IT->w showing the region? */
11721 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11722
11723 /* Clear the result glyph row and enable it. */
11724 prepare_desired_row (row);
11725
11726 row->y = it->current_y;
11727 row->start = it->current;
11728 row->continuation_lines_width = it->continuation_lines_width;
11729 row->displays_text_p = 1;
11730 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11731 it->starts_in_middle_of_char_p = 0;
11732
11733 /* Arrange the overlays nicely for our purposes. Usually, we call
11734 display_line on only one line at a time, in which case this
11735 can't really hurt too much, or we call it on lines which appear
11736 one after another in the buffer, in which case all calls to
11737 recenter_overlay_lists but the first will be pretty cheap. */
11738 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
11739
11740 /* Move over display elements that are not visible because we are
11741 hscrolled. This may stop at an x-position < IT->first_visible_x
11742 if the first glyph is partially visible or if we hit a line end. */
11743 if (it->current_x < it->first_visible_x)
11744 move_it_in_display_line_to (it, ZV, it->first_visible_x,
11745 MOVE_TO_POS | MOVE_TO_X);
11746
11747 /* Get the initial row height. This is either the height of the
11748 text hscrolled, if there is any, or zero. */
11749 row->ascent = it->max_ascent;
11750 row->height = it->max_ascent + it->max_descent;
11751 row->phys_ascent = it->max_phys_ascent;
11752 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11753
11754 /* Loop generating characters. The loop is left with IT on the next
11755 character to display. */
11756 while (1)
11757 {
11758 int n_glyphs_before, hpos_before, x_before;
11759 int x, i, nglyphs;
11760 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
11761
11762 /* Retrieve the next thing to display. Value is zero if end of
11763 buffer reached. */
11764 if (!get_next_display_element (it))
11765 {
11766 /* Maybe add a space at the end of this line that is used to
11767 display the cursor there under X. Set the charpos of the
11768 first glyph of blank lines not corresponding to any text
11769 to -1. */
11770 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
11771 || row->used[TEXT_AREA] == 0)
11772 {
11773 row->glyphs[TEXT_AREA]->charpos = -1;
11774 row->displays_text_p = 0;
11775
11776 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
11777 row->indicate_empty_line_p = 1;
11778 }
11779
11780 it->continuation_lines_width = 0;
11781 row->ends_at_zv_p = 1;
11782 break;
11783 }
11784
11785 /* Now, get the metrics of what we want to display. This also
11786 generates glyphs in `row' (which is IT->glyph_row). */
11787 n_glyphs_before = row->used[TEXT_AREA];
11788 x = it->current_x;
11789
11790 /* Remember the line height so far in case the next element doesn't
11791 fit on the line. */
11792 if (!it->truncate_lines_p)
11793 {
11794 ascent = it->max_ascent;
11795 descent = it->max_descent;
11796 phys_ascent = it->max_phys_ascent;
11797 phys_descent = it->max_phys_descent;
11798 }
11799
11800 PRODUCE_GLYPHS (it);
11801
11802 /* If this display element was in marginal areas, continue with
11803 the next one. */
11804 if (it->area != TEXT_AREA)
11805 {
11806 row->ascent = max (row->ascent, it->max_ascent);
11807 row->height = max (row->height, it->max_ascent + it->max_descent);
11808 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11809 row->phys_height = max (row->phys_height,
11810 it->max_phys_ascent + it->max_phys_descent);
11811 set_iterator_to_next (it, 1);
11812 continue;
11813 }
11814
11815 /* Does the display element fit on the line? If we truncate
11816 lines, we should draw past the right edge of the window. If
11817 we don't truncate, we want to stop so that we can display the
11818 continuation glyph before the right margin. If lines are
11819 continued, there are two possible strategies for characters
11820 resulting in more than 1 glyph (e.g. tabs): Display as many
11821 glyphs as possible in this line and leave the rest for the
11822 continuation line, or display the whole element in the next
11823 line. Original redisplay did the former, so we do it also. */
11824 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11825 hpos_before = it->hpos;
11826 x_before = x;
11827
11828 if (nglyphs == 1
11829 && it->current_x < it->last_visible_x)
11830 {
11831 ++it->hpos;
11832 row->ascent = max (row->ascent, it->max_ascent);
11833 row->height = max (row->height, it->max_ascent + it->max_descent);
11834 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11835 row->phys_height = max (row->phys_height,
11836 it->max_phys_ascent + it->max_phys_descent);
11837 if (it->current_x - it->pixel_width < it->first_visible_x)
11838 row->x = x - it->first_visible_x;
11839 }
11840 else
11841 {
11842 int new_x;
11843 struct glyph *glyph;
11844
11845 for (i = 0; i < nglyphs; ++i, x = new_x)
11846 {
11847 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11848 new_x = x + glyph->pixel_width;
11849
11850 if (/* Lines are continued. */
11851 !it->truncate_lines_p
11852 && (/* Glyph doesn't fit on the line. */
11853 new_x > it->last_visible_x
11854 /* Or it fits exactly on a window system frame. */
11855 || (new_x == it->last_visible_x
11856 && FRAME_WINDOW_P (it->f))))
11857 {
11858 /* End of a continued line. */
11859
11860 if (it->hpos == 0
11861 || (new_x == it->last_visible_x
11862 && FRAME_WINDOW_P (it->f)))
11863 {
11864 /* Current glyph is the only one on the line or
11865 fits exactly on the line. We must continue
11866 the line because we can't draw the cursor
11867 after the glyph. */
11868 row->continued_p = 1;
11869 it->current_x = new_x;
11870 it->continuation_lines_width += new_x;
11871 ++it->hpos;
11872 if (i == nglyphs - 1)
11873 set_iterator_to_next (it, 1);
11874 }
11875 else if (CHAR_GLYPH_PADDING_P (*glyph)
11876 && !FRAME_WINDOW_P (it->f))
11877 {
11878 /* A padding glyph that doesn't fit on this line.
11879 This means the whole character doesn't fit
11880 on the line. */
11881 row->used[TEXT_AREA] = n_glyphs_before;
11882
11883 /* Fill the rest of the row with continuation
11884 glyphs like in 20.x. */
11885 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
11886 < row->glyphs[1 + TEXT_AREA])
11887 produce_special_glyphs (it, IT_CONTINUATION);
11888
11889 row->continued_p = 1;
11890 it->current_x = x_before;
11891 it->continuation_lines_width += x_before;
11892
11893 /* Restore the height to what it was before the
11894 element not fitting on the line. */
11895 it->max_ascent = ascent;
11896 it->max_descent = descent;
11897 it->max_phys_ascent = phys_ascent;
11898 it->max_phys_descent = phys_descent;
11899 }
11900 else
11901 {
11902 /* Display element draws past the right edge of
11903 the window. Restore positions to values
11904 before the element. The next line starts
11905 with current_x before the glyph that could
11906 not be displayed, so that TAB works right. */
11907 row->used[TEXT_AREA] = n_glyphs_before + i;
11908
11909 /* Display continuation glyphs. */
11910 if (!FRAME_WINDOW_P (it->f))
11911 produce_special_glyphs (it, IT_CONTINUATION);
11912 row->continued_p = 1;
11913
11914 it->current_x = x;
11915 it->continuation_lines_width += x;
11916 if (nglyphs > 1 && i > 0)
11917 {
11918 row->ends_in_middle_of_char_p = 1;
11919 it->starts_in_middle_of_char_p = 1;
11920 }
11921
11922 /* Restore the height to what it was before the
11923 element not fitting on the line. */
11924 it->max_ascent = ascent;
11925 it->max_descent = descent;
11926 it->max_phys_ascent = phys_ascent;
11927 it->max_phys_descent = phys_descent;
11928 }
11929
11930 break;
11931 }
11932 else if (new_x > it->first_visible_x)
11933 {
11934 /* Increment number of glyphs actually displayed. */
11935 ++it->hpos;
11936
11937 if (x < it->first_visible_x)
11938 /* Glyph is partially visible, i.e. row starts at
11939 negative X position. */
11940 row->x = x - it->first_visible_x;
11941 }
11942 else
11943 {
11944 /* Glyph is completely off the left margin of the
11945 window. This should not happen because of the
11946 move_it_in_display_line at the start of
11947 this function. */
11948 abort ();
11949 }
11950 }
11951
11952 row->ascent = max (row->ascent, it->max_ascent);
11953 row->height = max (row->height, it->max_ascent + it->max_descent);
11954 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
11955 row->phys_height = max (row->phys_height,
11956 it->max_phys_ascent + it->max_phys_descent);
11957
11958 /* End of this display line if row is continued. */
11959 if (row->continued_p)
11960 break;
11961 }
11962
11963 /* Is this a line end? If yes, we're also done, after making
11964 sure that a non-default face is extended up to the right
11965 margin of the window. */
11966 if (ITERATOR_AT_END_OF_LINE_P (it))
11967 {
11968 int used_before = row->used[TEXT_AREA];
11969
11970 /* Add a space at the end of the line that is used to
11971 display the cursor there. */
11972 append_space (it, 0);
11973
11974 /* Extend the face to the end of the line. */
11975 extend_face_to_end_of_line (it);
11976
11977 /* Make sure we have the position. */
11978 if (used_before == 0)
11979 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
11980
11981 /* Consume the line end. This skips over invisible lines. */
11982 set_iterator_to_next (it, 1);
11983 it->continuation_lines_width = 0;
11984 break;
11985 }
11986
11987 /* Proceed with next display element. Note that this skips
11988 over lines invisible because of selective display. */
11989 set_iterator_to_next (it, 1);
11990
11991 /* If we truncate lines, we are done when the last displayed
11992 glyphs reach past the right margin of the window. */
11993 if (it->truncate_lines_p
11994 && (FRAME_WINDOW_P (it->f)
11995 ? (it->current_x >= it->last_visible_x)
11996 : (it->current_x > it->last_visible_x)))
11997 {
11998 /* Maybe add truncation glyphs. */
11999 if (!FRAME_WINDOW_P (it->f))
12000 {
12001 --it->glyph_row->used[TEXT_AREA];
12002 produce_special_glyphs (it, IT_TRUNCATION);
12003 }
12004
12005 row->truncated_on_right_p = 1;
12006 it->continuation_lines_width = 0;
12007 reseat_at_next_visible_line_start (it, 0);
12008 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12009 it->hpos = hpos_before;
12010 it->current_x = x_before;
12011 break;
12012 }
12013 }
12014
12015 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12016 at the left window margin. */
12017 if (it->first_visible_x
12018 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12019 {
12020 if (!FRAME_WINDOW_P (it->f))
12021 insert_left_trunc_glyphs (it);
12022 row->truncated_on_left_p = 1;
12023 }
12024
12025 /* If the start of this line is the overlay arrow-position, then
12026 mark this glyph row as the one containing the overlay arrow.
12027 This is clearly a mess with variable size fonts. It would be
12028 better to let it be displayed like cursors under X. */
12029 if (MARKERP (Voverlay_arrow_position)
12030 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12031 && (MATRIX_ROW_START_CHARPOS (row)
12032 == marker_position (Voverlay_arrow_position))
12033 && STRINGP (Voverlay_arrow_string)
12034 && ! overlay_arrow_seen)
12035 {
12036 /* Overlay arrow in window redisplay is a bitmap. */
12037 if (!FRAME_WINDOW_P (it->f))
12038 {
12039 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12040 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12041 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12042 struct glyph *p = row->glyphs[TEXT_AREA];
12043 struct glyph *p2, *end;
12044
12045 /* Copy the arrow glyphs. */
12046 while (glyph < arrow_end)
12047 *p++ = *glyph++;
12048
12049 /* Throw away padding glyphs. */
12050 p2 = p;
12051 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12052 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12053 ++p2;
12054 if (p2 > p)
12055 {
12056 while (p2 < end)
12057 *p++ = *p2++;
12058 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12059 }
12060 }
12061
12062 overlay_arrow_seen = 1;
12063 row->overlay_arrow_p = 1;
12064 }
12065
12066 /* Compute pixel dimensions of this line. */
12067 compute_line_metrics (it);
12068
12069 /* Remember the position at which this line ends. */
12070 row->end = it->current;
12071
12072 /* Maybe set the cursor. */
12073 if (it->w->cursor.vpos < 0
12074 && PT >= MATRIX_ROW_START_CHARPOS (row)
12075 && PT <= MATRIX_ROW_END_CHARPOS (row)
12076 && cursor_row_p (it->w, row))
12077 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12078
12079 /* Highlight trailing whitespace. */
12080 if (!NILP (Vshow_trailing_whitespace))
12081 highlight_trailing_whitespace (it->f, it->glyph_row);
12082
12083 /* Prepare for the next line. This line starts horizontally at (X
12084 HPOS) = (0 0). Vertical positions are incremented. As a
12085 convenience for the caller, IT->glyph_row is set to the next
12086 row to be used. */
12087 it->current_x = it->hpos = 0;
12088 it->current_y += row->height;
12089 ++it->vpos;
12090 ++it->glyph_row;
12091 return row->displays_text_p;
12092 }
12093
12094
12095 \f
12096 /***********************************************************************
12097 Menu Bar
12098 ***********************************************************************/
12099
12100 /* Redisplay the menu bar in the frame for window W.
12101
12102 The menu bar of X frames that don't have X toolkit support is
12103 displayed in a special window W->frame->menu_bar_window.
12104
12105 The menu bar of terminal frames is treated specially as far as
12106 glyph matrices are concerned. Menu bar lines are not part of
12107 windows, so the update is done directly on the frame matrix rows
12108 for the menu bar. */
12109
12110 static void
12111 display_menu_bar (w)
12112 struct window *w;
12113 {
12114 struct frame *f = XFRAME (WINDOW_FRAME (w));
12115 struct it it;
12116 Lisp_Object items;
12117 int i;
12118
12119 /* Don't do all this for graphical frames. */
12120 #ifdef HAVE_NTGUI
12121 if (!NILP (Vwindow_system))
12122 return;
12123 #endif
12124 #ifdef USE_X_TOOLKIT
12125 if (FRAME_X_P (f))
12126 return;
12127 #endif
12128 #ifdef macintosh
12129 if (FRAME_MAC_P (f))
12130 return;
12131 #endif
12132
12133 #ifdef USE_X_TOOLKIT
12134 xassert (!FRAME_WINDOW_P (f));
12135 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12136 it.first_visible_x = 0;
12137 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12138 #else /* not USE_X_TOOLKIT */
12139 if (FRAME_WINDOW_P (f))
12140 {
12141 /* Menu bar lines are displayed in the desired matrix of the
12142 dummy window menu_bar_window. */
12143 struct window *menu_w;
12144 xassert (WINDOWP (f->menu_bar_window));
12145 menu_w = XWINDOW (f->menu_bar_window);
12146 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12147 MENU_FACE_ID);
12148 it.first_visible_x = 0;
12149 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12150 }
12151 else
12152 {
12153 /* This is a TTY frame, i.e. character hpos/vpos are used as
12154 pixel x/y. */
12155 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12156 MENU_FACE_ID);
12157 it.first_visible_x = 0;
12158 it.last_visible_x = FRAME_WIDTH (f);
12159 }
12160 #endif /* not USE_X_TOOLKIT */
12161
12162 /* Clear all rows of the menu bar. */
12163 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12164 {
12165 struct glyph_row *row = it.glyph_row + i;
12166 clear_glyph_row (row);
12167 row->enabled_p = 1;
12168 row->full_width_p = 1;
12169 }
12170
12171 /* Make the first line of the menu bar appear in reverse video. */
12172 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12173
12174 /* Display all items of the menu bar. */
12175 items = FRAME_MENU_BAR_ITEMS (it.f);
12176 for (i = 0; i < XVECTOR (items)->size; i += 4)
12177 {
12178 Lisp_Object string;
12179
12180 /* Stop at nil string. */
12181 string = XVECTOR (items)->contents[i + 1];
12182 if (NILP (string))
12183 break;
12184
12185 /* Remember where item was displayed. */
12186 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12187
12188 /* Display the item, pad with one space. */
12189 if (it.current_x < it.last_visible_x)
12190 display_string (NULL, string, Qnil, 0, 0, &it,
12191 XSTRING (string)->size + 1, 0, 0, -1);
12192 }
12193
12194 /* Fill out the line with spaces. */
12195 if (it.current_x < it.last_visible_x)
12196 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12197
12198 /* Compute the total height of the lines. */
12199 compute_line_metrics (&it);
12200 }
12201
12202
12203 \f
12204 /***********************************************************************
12205 Mode Line
12206 ***********************************************************************/
12207
12208 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12209 FORCE is non-zero, redisplay mode lines unconditionally.
12210 Otherwise, redisplay only mode lines that are garbaged. Value is
12211 the number of windows whose mode lines were redisplayed. */
12212
12213 static int
12214 redisplay_mode_lines (window, force)
12215 Lisp_Object window;
12216 int force;
12217 {
12218 int nwindows = 0;
12219
12220 while (!NILP (window))
12221 {
12222 struct window *w = XWINDOW (window);
12223
12224 if (WINDOWP (w->hchild))
12225 nwindows += redisplay_mode_lines (w->hchild, force);
12226 else if (WINDOWP (w->vchild))
12227 nwindows += redisplay_mode_lines (w->vchild, force);
12228 else if (force
12229 || FRAME_GARBAGED_P (XFRAME (w->frame))
12230 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12231 {
12232 Lisp_Object old_selected_frame;
12233 struct text_pos lpoint;
12234 struct buffer *old = current_buffer;
12235
12236 /* Set the window's buffer for the mode line display. */
12237 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12238 set_buffer_internal_1 (XBUFFER (w->buffer));
12239
12240 /* Point refers normally to the selected window. For any
12241 other window, set up appropriate value. */
12242 if (!EQ (window, selected_window))
12243 {
12244 struct text_pos pt;
12245
12246 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12247 if (CHARPOS (pt) < BEGV)
12248 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12249 else if (CHARPOS (pt) > (ZV - 1))
12250 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12251 else
12252 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12253 }
12254
12255 /* Temporarily set up the selected frame. */
12256 old_selected_frame = selected_frame;
12257 selected_frame = w->frame;
12258
12259 /* Display mode lines. */
12260 clear_glyph_matrix (w->desired_matrix);
12261 if (display_mode_lines (w))
12262 {
12263 ++nwindows;
12264 w->must_be_updated_p = 1;
12265 }
12266
12267 /* Restore old settings. */
12268 selected_frame = old_selected_frame;
12269 set_buffer_internal_1 (old);
12270 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12271 }
12272
12273 window = w->next;
12274 }
12275
12276 return nwindows;
12277 }
12278
12279
12280 /* Display the mode and/or top line of window W. Value is the number
12281 of mode lines displayed. */
12282
12283 static int
12284 display_mode_lines (w)
12285 struct window *w;
12286 {
12287 int n = 0;
12288
12289 /* These will be set while the mode line specs are processed. */
12290 line_number_displayed = 0;
12291 w->column_number_displayed = Qnil;
12292
12293 if (WINDOW_WANTS_MODELINE_P (w))
12294 {
12295 display_mode_line (w, MODE_LINE_FACE_ID,
12296 current_buffer->mode_line_format);
12297 ++n;
12298 }
12299
12300 if (WINDOW_WANTS_HEADER_LINE_P (w))
12301 {
12302 display_mode_line (w, HEADER_LINE_FACE_ID,
12303 current_buffer->header_line_format);
12304 ++n;
12305 }
12306
12307 return n;
12308 }
12309
12310
12311 /* Display mode or top line of window W. FACE_ID specifies which line
12312 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12313 FORMAT is the mode line format to display. */
12314
12315 static void
12316 display_mode_line (w, face_id, format)
12317 struct window *w;
12318 enum face_id face_id;
12319 Lisp_Object format;
12320 {
12321 struct it it;
12322 struct face *face;
12323
12324 init_iterator (&it, w, -1, -1, NULL, face_id);
12325 prepare_desired_row (it.glyph_row);
12326
12327 /* Temporarily make frame's keyboard the current kboard so that
12328 kboard-local variables in the mode_line_format will get the right
12329 values. */
12330 push_frame_kboard (it.f);
12331 display_mode_element (&it, 0, 0, 0, format);
12332 pop_frame_kboard ();
12333
12334 /* Fill up with spaces. */
12335 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12336
12337 compute_line_metrics (&it);
12338 it.glyph_row->full_width_p = 1;
12339 it.glyph_row->mode_line_p = 1;
12340 it.glyph_row->inverse_p = mode_line_inverse_video != 0;
12341 it.glyph_row->continued_p = 0;
12342 it.glyph_row->truncated_on_left_p = 0;
12343 it.glyph_row->truncated_on_right_p = 0;
12344
12345 /* Make a 3D mode-line have a shadow at its right end. */
12346 face = FACE_FROM_ID (it.f, face_id);
12347 extend_face_to_end_of_line (&it);
12348 if (face->box != FACE_NO_BOX)
12349 {
12350 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12351 + it.glyph_row->used[TEXT_AREA] - 1);
12352 last->right_box_line_p = 1;
12353 }
12354 }
12355
12356
12357 /* Contribute ELT to the mode line for window IT->w. How it
12358 translates into text depends on its data type.
12359
12360 IT describes the display environment in which we display, as usual.
12361
12362 DEPTH is the depth in recursion. It is used to prevent
12363 infinite recursion here.
12364
12365 FIELD_WIDTH is the number of characters the display of ELT should
12366 occupy in the mode line, and PRECISION is the maximum number of
12367 characters to display from ELT's representation. See
12368 display_string for details. *
12369
12370 Returns the hpos of the end of the text generated by ELT. */
12371
12372 static int
12373 display_mode_element (it, depth, field_width, precision, elt)
12374 struct it *it;
12375 int depth;
12376 int field_width, precision;
12377 Lisp_Object elt;
12378 {
12379 int n = 0, field, prec;
12380
12381 tail_recurse:
12382 if (depth > 10)
12383 goto invalid;
12384
12385 depth++;
12386
12387 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12388 {
12389 case Lisp_String:
12390 {
12391 /* A string: output it and check for %-constructs within it. */
12392 unsigned char c;
12393 unsigned char *this = XSTRING (elt)->data;
12394 unsigned char *lisp_string = this;
12395
12396 while ((precision <= 0 || n < precision)
12397 && *this
12398 && (frame_title_ptr
12399 || it->current_x < it->last_visible_x))
12400 {
12401 unsigned char *last = this;
12402
12403 /* Advance to end of string or next format specifier. */
12404 while ((c = *this++) != '\0' && c != '%')
12405 ;
12406
12407 if (this - 1 != last)
12408 {
12409 /* Output to end of string or up to '%'. Field width
12410 is length of string. Don't output more than
12411 PRECISION allows us. */
12412 prec = --this - last;
12413 if (precision > 0 && prec > precision - n)
12414 prec = precision - n;
12415
12416 if (frame_title_ptr)
12417 n += store_frame_title (last, prec, prec);
12418 else
12419 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12420 it, 0, prec, 0, -1);
12421 }
12422 else /* c == '%' */
12423 {
12424 unsigned char *percent_position = this;
12425
12426 /* Get the specified minimum width. Zero means
12427 don't pad. */
12428 field = 0;
12429 while ((c = *this++) >= '0' && c <= '9')
12430 field = field * 10 + c - '0';
12431
12432 /* Don't pad beyond the total padding allowed. */
12433 if (field_width - n > 0 && field > field_width - n)
12434 field = field_width - n;
12435
12436 /* Note that either PRECISION <= 0 or N < PRECISION. */
12437 prec = precision - n;
12438
12439 if (c == 'M')
12440 n += display_mode_element (it, depth, field, prec,
12441 Vglobal_mode_string);
12442 else if (c != 0)
12443 {
12444 unsigned char *spec
12445 = decode_mode_spec (it->w, c, field, prec);
12446
12447 if (frame_title_ptr)
12448 n += store_frame_title (spec, field, prec);
12449 else
12450 {
12451 int nglyphs_before
12452 = it->glyph_row->used[TEXT_AREA];
12453 int charpos
12454 = percent_position - XSTRING (elt)->data;
12455 int nwritten
12456 = display_string (spec, Qnil, elt, charpos, 0, it,
12457 field, prec, 0, -1);
12458
12459 /* Assign to the glyphs written above the
12460 string where the `%x' came from, position
12461 of the `%'. */
12462 if (nwritten > 0)
12463 {
12464 struct glyph *glyph
12465 = (it->glyph_row->glyphs[TEXT_AREA]
12466 + nglyphs_before);
12467 int i;
12468
12469 for (i = 0; i < nwritten; ++i)
12470 {
12471 glyph[i].object = elt;
12472 glyph[i].charpos = charpos;
12473 }
12474
12475 n += nwritten;
12476 }
12477 }
12478 }
12479 }
12480 }
12481 }
12482 break;
12483
12484 case Lisp_Symbol:
12485 /* A symbol: process the value of the symbol recursively
12486 as if it appeared here directly. Avoid error if symbol void.
12487 Special case: if value of symbol is a string, output the string
12488 literally. */
12489 {
12490 register Lisp_Object tem;
12491 tem = Fboundp (elt);
12492 if (!NILP (tem))
12493 {
12494 tem = Fsymbol_value (elt);
12495 /* If value is a string, output that string literally:
12496 don't check for % within it. */
12497 if (STRINGP (tem))
12498 {
12499 prec = XSTRING (tem)->size;
12500 if (precision > 0 && prec > precision - n)
12501 prec = precision - n;
12502 if (frame_title_ptr)
12503 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12504 else
12505 n += display_string (NULL, tem, Qnil, 0, 0, it,
12506 0, prec, 0, -1);
12507 }
12508 else if (!EQ (tem, elt))
12509 {
12510 /* Give up right away for nil or t. */
12511 elt = tem;
12512 goto tail_recurse;
12513 }
12514 }
12515 }
12516 break;
12517
12518 case Lisp_Cons:
12519 {
12520 register Lisp_Object car, tem;
12521
12522 /* A cons cell: three distinct cases.
12523 If first element is a string or a cons, process all the elements
12524 and effectively concatenate them.
12525 If first element is a negative number, truncate displaying cdr to
12526 at most that many characters. If positive, pad (with spaces)
12527 to at least that many characters.
12528 If first element is a symbol, process the cadr or caddr recursively
12529 according to whether the symbol's value is non-nil or nil. */
12530 car = XCAR (elt);
12531 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12532 {
12533 /* An element of the form (:eval FORM) means evaluate FORM
12534 and use the result as mode line elements. */
12535 struct gcpro gcpro1;
12536 Lisp_Object spec;
12537
12538 spec = safe_eval (XCAR (XCDR (elt)));
12539 GCPRO1 (spec);
12540 n += display_mode_element (it, depth, field_width - n,
12541 precision - n, spec);
12542 UNGCPRO;
12543 }
12544 else if (SYMBOLP (car))
12545 {
12546 tem = Fboundp (car);
12547 elt = XCDR (elt);
12548 if (!CONSP (elt))
12549 goto invalid;
12550 /* elt is now the cdr, and we know it is a cons cell.
12551 Use its car if CAR has a non-nil value. */
12552 if (!NILP (tem))
12553 {
12554 tem = Fsymbol_value (car);
12555 if (!NILP (tem))
12556 {
12557 elt = XCAR (elt);
12558 goto tail_recurse;
12559 }
12560 }
12561 /* Symbol's value is nil (or symbol is unbound)
12562 Get the cddr of the original list
12563 and if possible find the caddr and use that. */
12564 elt = XCDR (elt);
12565 if (NILP (elt))
12566 break;
12567 else if (!CONSP (elt))
12568 goto invalid;
12569 elt = XCAR (elt);
12570 goto tail_recurse;
12571 }
12572 else if (INTEGERP (car))
12573 {
12574 register int lim = XINT (car);
12575 elt = XCDR (elt);
12576 if (lim < 0)
12577 {
12578 /* Negative int means reduce maximum width. */
12579 if (precision <= 0)
12580 precision = -lim;
12581 else
12582 precision = min (precision, -lim);
12583 }
12584 else if (lim > 0)
12585 {
12586 /* Padding specified. Don't let it be more than
12587 current maximum. */
12588 if (precision > 0)
12589 lim = min (precision, lim);
12590
12591 /* If that's more padding than already wanted, queue it.
12592 But don't reduce padding already specified even if
12593 that is beyond the current truncation point. */
12594 field_width = max (lim, field_width);
12595 }
12596 goto tail_recurse;
12597 }
12598 else if (STRINGP (car) || CONSP (car))
12599 {
12600 register int limit = 50;
12601 /* Limit is to protect against circular lists. */
12602 while (CONSP (elt)
12603 && --limit > 0
12604 && (precision <= 0 || n < precision))
12605 {
12606 n += display_mode_element (it, depth, field_width - n,
12607 precision - n, XCAR (elt));
12608 elt = XCDR (elt);
12609 }
12610 }
12611 }
12612 break;
12613
12614 default:
12615 invalid:
12616 if (frame_title_ptr)
12617 n += store_frame_title ("*invalid*", 0, precision - n);
12618 else
12619 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12620 precision - n, 0, 0);
12621 return n;
12622 }
12623
12624 /* Pad to FIELD_WIDTH. */
12625 if (field_width > 0 && n < field_width)
12626 {
12627 if (frame_title_ptr)
12628 n += store_frame_title ("", field_width - n, 0);
12629 else
12630 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12631 0, 0, 0);
12632 }
12633
12634 return n;
12635 }
12636
12637
12638 /* Write a null-terminated, right justified decimal representation of
12639 the positive integer D to BUF using a minimal field width WIDTH. */
12640
12641 static void
12642 pint2str (buf, width, d)
12643 register char *buf;
12644 register int width;
12645 register int d;
12646 {
12647 register char *p = buf;
12648
12649 if (d <= 0)
12650 *p++ = '0';
12651 else
12652 {
12653 while (d > 0)
12654 {
12655 *p++ = d % 10 + '0';
12656 d /= 10;
12657 }
12658 }
12659
12660 for (width -= (int) (p - buf); width > 0; --width)
12661 *p++ = ' ';
12662 *p-- = '\0';
12663 while (p > buf)
12664 {
12665 d = *buf;
12666 *buf++ = *p;
12667 *p-- = d;
12668 }
12669 }
12670
12671 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12672 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12673 type of CODING_SYSTEM. Return updated pointer into BUF. */
12674
12675 static unsigned char invalid_eol_type[] = "(*invalid*)";
12676
12677 static char *
12678 decode_mode_spec_coding (coding_system, buf, eol_flag)
12679 Lisp_Object coding_system;
12680 register char *buf;
12681 int eol_flag;
12682 {
12683 Lisp_Object val;
12684 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12685 unsigned char *eol_str;
12686 int eol_str_len;
12687 /* The EOL conversion we are using. */
12688 Lisp_Object eoltype;
12689
12690 val = Fget (coding_system, Qcoding_system);
12691 eoltype = Qnil;
12692
12693 if (!VECTORP (val)) /* Not yet decided. */
12694 {
12695 if (multibyte)
12696 *buf++ = '-';
12697 if (eol_flag)
12698 eoltype = eol_mnemonic_undecided;
12699 /* Don't mention EOL conversion if it isn't decided. */
12700 }
12701 else
12702 {
12703 Lisp_Object eolvalue;
12704
12705 eolvalue = Fget (coding_system, Qeol_type);
12706
12707 if (multibyte)
12708 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12709
12710 if (eol_flag)
12711 {
12712 /* The EOL conversion that is normal on this system. */
12713
12714 if (NILP (eolvalue)) /* Not yet decided. */
12715 eoltype = eol_mnemonic_undecided;
12716 else if (VECTORP (eolvalue)) /* Not yet decided. */
12717 eoltype = eol_mnemonic_undecided;
12718 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12719 eoltype = (XFASTINT (eolvalue) == 0
12720 ? eol_mnemonic_unix
12721 : (XFASTINT (eolvalue) == 1
12722 ? eol_mnemonic_dos : eol_mnemonic_mac));
12723 }
12724 }
12725
12726 if (eol_flag)
12727 {
12728 /* Mention the EOL conversion if it is not the usual one. */
12729 if (STRINGP (eoltype))
12730 {
12731 eol_str = XSTRING (eoltype)->data;
12732 eol_str_len = XSTRING (eoltype)->size;
12733 }
12734 else if (INTEGERP (eoltype)
12735 && CHAR_VALID_P (XINT (eoltype), 0))
12736 {
12737 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
12738 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
12739 }
12740 else
12741 {
12742 eol_str = invalid_eol_type;
12743 eol_str_len = sizeof (invalid_eol_type) - 1;
12744 }
12745 bcopy (eol_str, buf, eol_str_len);
12746 buf += eol_str_len;
12747 }
12748
12749 return buf;
12750 }
12751
12752 /* Return a string for the output of a mode line %-spec for window W,
12753 generated by character C. PRECISION >= 0 means don't return a
12754 string longer than that value. FIELD_WIDTH > 0 means pad the
12755 string returned with spaces to that value. */
12756
12757 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
12758
12759 static char *
12760 decode_mode_spec (w, c, field_width, precision)
12761 struct window *w;
12762 register int c;
12763 int field_width, precision;
12764 {
12765 Lisp_Object obj;
12766 struct frame *f = XFRAME (WINDOW_FRAME (w));
12767 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
12768 struct buffer *b = XBUFFER (w->buffer);
12769
12770 obj = Qnil;
12771
12772 switch (c)
12773 {
12774 case '*':
12775 if (!NILP (b->read_only))
12776 return "%";
12777 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12778 return "*";
12779 return "-";
12780
12781 case '+':
12782 /* This differs from %* only for a modified read-only buffer. */
12783 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12784 return "*";
12785 if (!NILP (b->read_only))
12786 return "%";
12787 return "-";
12788
12789 case '&':
12790 /* This differs from %* in ignoring read-only-ness. */
12791 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
12792 return "*";
12793 return "-";
12794
12795 case '%':
12796 return "%";
12797
12798 case '[':
12799 {
12800 int i;
12801 char *p;
12802
12803 if (command_loop_level > 5)
12804 return "[[[... ";
12805 p = decode_mode_spec_buf;
12806 for (i = 0; i < command_loop_level; i++)
12807 *p++ = '[';
12808 *p = 0;
12809 return decode_mode_spec_buf;
12810 }
12811
12812 case ']':
12813 {
12814 int i;
12815 char *p;
12816
12817 if (command_loop_level > 5)
12818 return " ...]]]";
12819 p = decode_mode_spec_buf;
12820 for (i = 0; i < command_loop_level; i++)
12821 *p++ = ']';
12822 *p = 0;
12823 return decode_mode_spec_buf;
12824 }
12825
12826 case '-':
12827 {
12828 register int i;
12829
12830 /* Let lots_of_dashes be a string of infinite length. */
12831 if (field_width <= 0
12832 || field_width > sizeof (lots_of_dashes))
12833 {
12834 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
12835 decode_mode_spec_buf[i] = '-';
12836 decode_mode_spec_buf[i] = '\0';
12837 return decode_mode_spec_buf;
12838 }
12839 else
12840 return lots_of_dashes;
12841 }
12842
12843 case 'b':
12844 obj = b->name;
12845 break;
12846
12847 case 'c':
12848 {
12849 int col = current_column ();
12850 XSETFASTINT (w->column_number_displayed, col);
12851 pint2str (decode_mode_spec_buf, field_width, col);
12852 return decode_mode_spec_buf;
12853 }
12854
12855 case 'F':
12856 /* %F displays the frame name. */
12857 if (!NILP (f->title))
12858 return (char *) XSTRING (f->title)->data;
12859 if (f->explicit_name || ! FRAME_WINDOW_P (f))
12860 return (char *) XSTRING (f->name)->data;
12861 return "Emacs";
12862
12863 case 'f':
12864 obj = b->filename;
12865 break;
12866
12867 case 'l':
12868 {
12869 int startpos = XMARKER (w->start)->charpos;
12870 int startpos_byte = marker_byte_position (w->start);
12871 int line, linepos, linepos_byte, topline;
12872 int nlines, junk;
12873 int height = XFASTINT (w->height);
12874
12875 /* If we decided that this buffer isn't suitable for line numbers,
12876 don't forget that too fast. */
12877 if (EQ (w->base_line_pos, w->buffer))
12878 goto no_value;
12879 /* But do forget it, if the window shows a different buffer now. */
12880 else if (BUFFERP (w->base_line_pos))
12881 w->base_line_pos = Qnil;
12882
12883 /* If the buffer is very big, don't waste time. */
12884 if (INTEGERP (Vline_number_display_limit)
12885 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
12886 {
12887 w->base_line_pos = Qnil;
12888 w->base_line_number = Qnil;
12889 goto no_value;
12890 }
12891
12892 if (!NILP (w->base_line_number)
12893 && !NILP (w->base_line_pos)
12894 && XFASTINT (w->base_line_pos) <= startpos)
12895 {
12896 line = XFASTINT (w->base_line_number);
12897 linepos = XFASTINT (w->base_line_pos);
12898 linepos_byte = buf_charpos_to_bytepos (b, linepos);
12899 }
12900 else
12901 {
12902 line = 1;
12903 linepos = BUF_BEGV (b);
12904 linepos_byte = BUF_BEGV_BYTE (b);
12905 }
12906
12907 /* Count lines from base line to window start position. */
12908 nlines = display_count_lines (linepos, linepos_byte,
12909 startpos_byte,
12910 startpos, &junk);
12911
12912 topline = nlines + line;
12913
12914 /* Determine a new base line, if the old one is too close
12915 or too far away, or if we did not have one.
12916 "Too close" means it's plausible a scroll-down would
12917 go back past it. */
12918 if (startpos == BUF_BEGV (b))
12919 {
12920 XSETFASTINT (w->base_line_number, topline);
12921 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
12922 }
12923 else if (nlines < height + 25 || nlines > height * 3 + 50
12924 || linepos == BUF_BEGV (b))
12925 {
12926 int limit = BUF_BEGV (b);
12927 int limit_byte = BUF_BEGV_BYTE (b);
12928 int position;
12929 int distance = (height * 2 + 30) * line_number_display_limit_width;
12930
12931 if (startpos - distance > limit)
12932 {
12933 limit = startpos - distance;
12934 limit_byte = CHAR_TO_BYTE (limit);
12935 }
12936
12937 nlines = display_count_lines (startpos, startpos_byte,
12938 limit_byte,
12939 - (height * 2 + 30),
12940 &position);
12941 /* If we couldn't find the lines we wanted within
12942 line_number_display_limit_width chars per line,
12943 give up on line numbers for this window. */
12944 if (position == limit_byte && limit == startpos - distance)
12945 {
12946 w->base_line_pos = w->buffer;
12947 w->base_line_number = Qnil;
12948 goto no_value;
12949 }
12950
12951 XSETFASTINT (w->base_line_number, topline - nlines);
12952 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
12953 }
12954
12955 /* Now count lines from the start pos to point. */
12956 nlines = display_count_lines (startpos, startpos_byte,
12957 PT_BYTE, PT, &junk);
12958
12959 /* Record that we did display the line number. */
12960 line_number_displayed = 1;
12961
12962 /* Make the string to show. */
12963 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
12964 return decode_mode_spec_buf;
12965 no_value:
12966 {
12967 char* p = decode_mode_spec_buf;
12968 int pad = field_width - 2;
12969 while (pad-- > 0)
12970 *p++ = ' ';
12971 *p++ = '?';
12972 *p++ = '?';
12973 *p = '\0';
12974 return decode_mode_spec_buf;
12975 }
12976 }
12977 break;
12978
12979 case 'm':
12980 obj = b->mode_name;
12981 break;
12982
12983 case 'n':
12984 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
12985 return " Narrow";
12986 break;
12987
12988 case 'p':
12989 {
12990 int pos = marker_position (w->start);
12991 int total = BUF_ZV (b) - BUF_BEGV (b);
12992
12993 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
12994 {
12995 if (pos <= BUF_BEGV (b))
12996 return "All";
12997 else
12998 return "Bottom";
12999 }
13000 else if (pos <= BUF_BEGV (b))
13001 return "Top";
13002 else
13003 {
13004 if (total > 1000000)
13005 /* Do it differently for a large value, to avoid overflow. */
13006 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13007 else
13008 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13009 /* We can't normally display a 3-digit number,
13010 so get us a 2-digit number that is close. */
13011 if (total == 100)
13012 total = 99;
13013 sprintf (decode_mode_spec_buf, "%2d%%", total);
13014 return decode_mode_spec_buf;
13015 }
13016 }
13017
13018 /* Display percentage of size above the bottom of the screen. */
13019 case 'P':
13020 {
13021 int toppos = marker_position (w->start);
13022 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13023 int total = BUF_ZV (b) - BUF_BEGV (b);
13024
13025 if (botpos >= BUF_ZV (b))
13026 {
13027 if (toppos <= BUF_BEGV (b))
13028 return "All";
13029 else
13030 return "Bottom";
13031 }
13032 else
13033 {
13034 if (total > 1000000)
13035 /* Do it differently for a large value, to avoid overflow. */
13036 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13037 else
13038 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13039 /* We can't normally display a 3-digit number,
13040 so get us a 2-digit number that is close. */
13041 if (total == 100)
13042 total = 99;
13043 if (toppos <= BUF_BEGV (b))
13044 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13045 else
13046 sprintf (decode_mode_spec_buf, "%2d%%", total);
13047 return decode_mode_spec_buf;
13048 }
13049 }
13050
13051 case 's':
13052 /* status of process */
13053 obj = Fget_buffer_process (w->buffer);
13054 if (NILP (obj))
13055 return "no process";
13056 #ifdef subprocesses
13057 obj = Fsymbol_name (Fprocess_status (obj));
13058 #endif
13059 break;
13060
13061 case 't': /* indicate TEXT or BINARY */
13062 #ifdef MODE_LINE_BINARY_TEXT
13063 return MODE_LINE_BINARY_TEXT (b);
13064 #else
13065 return "T";
13066 #endif
13067
13068 case 'z':
13069 /* coding-system (not including end-of-line format) */
13070 case 'Z':
13071 /* coding-system (including end-of-line type) */
13072 {
13073 int eol_flag = (c == 'Z');
13074 char *p = decode_mode_spec_buf;
13075
13076 if (! FRAME_WINDOW_P (f))
13077 {
13078 /* No need to mention EOL here--the terminal never needs
13079 to do EOL conversion. */
13080 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13081 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13082 }
13083 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13084 p, eol_flag);
13085
13086 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13087 #ifdef subprocesses
13088 obj = Fget_buffer_process (Fcurrent_buffer ());
13089 if (PROCESSP (obj))
13090 {
13091 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13092 p, eol_flag);
13093 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13094 p, eol_flag);
13095 }
13096 #endif /* subprocesses */
13097 #endif /* 0 */
13098 *p = 0;
13099 return decode_mode_spec_buf;
13100 }
13101 }
13102
13103 if (STRINGP (obj))
13104 return (char *) XSTRING (obj)->data;
13105 else
13106 return "";
13107 }
13108
13109
13110 /* Count up to COUNT lines starting from START / START_BYTE.
13111 But don't go beyond LIMIT_BYTE.
13112 Return the number of lines thus found (always nonnegative).
13113
13114 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13115
13116 static int
13117 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13118 int start, start_byte, limit_byte, count;
13119 int *byte_pos_ptr;
13120 {
13121 register unsigned char *cursor;
13122 unsigned char *base;
13123
13124 register int ceiling;
13125 register unsigned char *ceiling_addr;
13126 int orig_count = count;
13127
13128 /* If we are not in selective display mode,
13129 check only for newlines. */
13130 int selective_display = (!NILP (current_buffer->selective_display)
13131 && !INTEGERP (current_buffer->selective_display));
13132
13133 if (count > 0)
13134 {
13135 while (start_byte < limit_byte)
13136 {
13137 ceiling = BUFFER_CEILING_OF (start_byte);
13138 ceiling = min (limit_byte - 1, ceiling);
13139 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13140 base = (cursor = BYTE_POS_ADDR (start_byte));
13141 while (1)
13142 {
13143 if (selective_display)
13144 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13145 ;
13146 else
13147 while (*cursor != '\n' && ++cursor != ceiling_addr)
13148 ;
13149
13150 if (cursor != ceiling_addr)
13151 {
13152 if (--count == 0)
13153 {
13154 start_byte += cursor - base + 1;
13155 *byte_pos_ptr = start_byte;
13156 return orig_count;
13157 }
13158 else
13159 if (++cursor == ceiling_addr)
13160 break;
13161 }
13162 else
13163 break;
13164 }
13165 start_byte += cursor - base;
13166 }
13167 }
13168 else
13169 {
13170 while (start_byte > limit_byte)
13171 {
13172 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13173 ceiling = max (limit_byte, ceiling);
13174 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13175 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13176 while (1)
13177 {
13178 if (selective_display)
13179 while (--cursor != ceiling_addr
13180 && *cursor != '\n' && *cursor != 015)
13181 ;
13182 else
13183 while (--cursor != ceiling_addr && *cursor != '\n')
13184 ;
13185
13186 if (cursor != ceiling_addr)
13187 {
13188 if (++count == 0)
13189 {
13190 start_byte += cursor - base + 1;
13191 *byte_pos_ptr = start_byte;
13192 /* When scanning backwards, we should
13193 not count the newline posterior to which we stop. */
13194 return - orig_count - 1;
13195 }
13196 }
13197 else
13198 break;
13199 }
13200 /* Here we add 1 to compensate for the last decrement
13201 of CURSOR, which took it past the valid range. */
13202 start_byte += cursor - base + 1;
13203 }
13204 }
13205
13206 *byte_pos_ptr = limit_byte;
13207
13208 if (count < 0)
13209 return - orig_count + count;
13210 return orig_count - count;
13211
13212 }
13213
13214
13215 \f
13216 /***********************************************************************
13217 Displaying strings
13218 ***********************************************************************/
13219
13220 /* Display a NUL-terminated string, starting with index START.
13221
13222 If STRING is non-null, display that C string. Otherwise, the Lisp
13223 string LISP_STRING is displayed.
13224
13225 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13226 FACE_STRING. Display STRING or LISP_STRING with the face at
13227 FACE_STRING_POS in FACE_STRING:
13228
13229 Display the string in the environment given by IT, but use the
13230 standard display table, temporarily.
13231
13232 FIELD_WIDTH is the minimum number of output glyphs to produce.
13233 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13234 with spaces. If STRING has more characters, more than FIELD_WIDTH
13235 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13236
13237 PRECISION is the maximum number of characters to output from
13238 STRING. PRECISION < 0 means don't truncate the string.
13239
13240 This is roughly equivalent to printf format specifiers:
13241
13242 FIELD_WIDTH PRECISION PRINTF
13243 ----------------------------------------
13244 -1 -1 %s
13245 -1 10 %.10s
13246 10 -1 %10s
13247 20 10 %20.10s
13248
13249 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13250 display them, and < 0 means obey the current buffer's value of
13251 enable_multibyte_characters.
13252
13253 Value is the number of glyphs produced. */
13254
13255 static int
13256 display_string (string, lisp_string, face_string, face_string_pos,
13257 start, it, field_width, precision, max_x, multibyte)
13258 unsigned char *string;
13259 Lisp_Object lisp_string;
13260 Lisp_Object face_string;
13261 int face_string_pos;
13262 int start;
13263 struct it *it;
13264 int field_width, precision, max_x;
13265 int multibyte;
13266 {
13267 int hpos_at_start = it->hpos;
13268 int saved_face_id = it->face_id;
13269 struct glyph_row *row = it->glyph_row;
13270
13271 /* Initialize the iterator IT for iteration over STRING beginning
13272 with index START. We assume that IT may be modified here (which
13273 means that display_line has to do something when displaying a
13274 mini-buffer prompt, which it does). */
13275 reseat_to_string (it, string, lisp_string, start,
13276 precision, field_width, multibyte);
13277
13278 /* If displaying STRING, set up the face of the iterator
13279 from LISP_STRING, if that's given. */
13280 if (STRINGP (face_string))
13281 {
13282 int endptr;
13283 struct face *face;
13284
13285 it->face_id
13286 = face_at_string_position (it->w, face_string, face_string_pos,
13287 0, it->region_beg_charpos,
13288 it->region_end_charpos,
13289 &endptr, it->base_face_id);
13290 face = FACE_FROM_ID (it->f, it->face_id);
13291 it->face_box_p = face->box != FACE_NO_BOX;
13292 }
13293
13294 /* Set max_x to the maximum allowed X position. Don't let it go
13295 beyond the right edge of the window. */
13296 if (max_x <= 0)
13297 max_x = it->last_visible_x;
13298 else
13299 max_x = min (max_x, it->last_visible_x);
13300
13301 /* Skip over display elements that are not visible. because IT->w is
13302 hscrolled. */
13303 if (it->current_x < it->first_visible_x)
13304 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13305 MOVE_TO_POS | MOVE_TO_X);
13306
13307 row->ascent = it->max_ascent;
13308 row->height = it->max_ascent + it->max_descent;
13309 row->phys_ascent = it->max_phys_ascent;
13310 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13311
13312 /* This condition is for the case that we are called with current_x
13313 past last_visible_x. */
13314 while (it->current_x < max_x)
13315 {
13316 int x_before, x, n_glyphs_before, i, nglyphs;
13317
13318 /* Get the next display element. */
13319 if (!get_next_display_element (it))
13320 break;
13321
13322 /* Produce glyphs. */
13323 x_before = it->current_x;
13324 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13325 PRODUCE_GLYPHS (it);
13326
13327 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13328 i = 0;
13329 x = x_before;
13330 while (i < nglyphs)
13331 {
13332 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13333
13334 if (!it->truncate_lines_p
13335 && x + glyph->pixel_width > max_x)
13336 {
13337 /* End of continued line or max_x reached. */
13338 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13339 it->current_x = x;
13340 break;
13341 }
13342 else if (x + glyph->pixel_width > it->first_visible_x)
13343 {
13344 /* Glyph is at least partially visible. */
13345 ++it->hpos;
13346 if (x < it->first_visible_x)
13347 it->glyph_row->x = x - it->first_visible_x;
13348 }
13349 else
13350 {
13351 /* Glyph is off the left margin of the display area.
13352 Should not happen. */
13353 abort ();
13354 }
13355
13356 row->ascent = max (row->ascent, it->max_ascent);
13357 row->height = max (row->height, it->max_ascent + it->max_descent);
13358 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13359 row->phys_height = max (row->phys_height,
13360 it->max_phys_ascent + it->max_phys_descent);
13361 x += glyph->pixel_width;
13362 ++i;
13363 }
13364
13365 /* Stop if max_x reached. */
13366 if (i < nglyphs)
13367 break;
13368
13369 /* Stop at line ends. */
13370 if (ITERATOR_AT_END_OF_LINE_P (it))
13371 {
13372 it->continuation_lines_width = 0;
13373 break;
13374 }
13375
13376 set_iterator_to_next (it, 1);
13377
13378 /* Stop if truncating at the right edge. */
13379 if (it->truncate_lines_p
13380 && it->current_x >= it->last_visible_x)
13381 {
13382 /* Add truncation mark, but don't do it if the line is
13383 truncated at a padding space. */
13384 if (IT_CHARPOS (*it) < it->string_nchars)
13385 {
13386 if (!FRAME_WINDOW_P (it->f))
13387 produce_special_glyphs (it, IT_TRUNCATION);
13388 it->glyph_row->truncated_on_right_p = 1;
13389 }
13390 break;
13391 }
13392 }
13393
13394 /* Maybe insert a truncation at the left. */
13395 if (it->first_visible_x
13396 && IT_CHARPOS (*it) > 0)
13397 {
13398 if (!FRAME_WINDOW_P (it->f))
13399 insert_left_trunc_glyphs (it);
13400 it->glyph_row->truncated_on_left_p = 1;
13401 }
13402
13403 it->face_id = saved_face_id;
13404
13405 /* Value is number of columns displayed. */
13406 return it->hpos - hpos_at_start;
13407 }
13408
13409
13410 \f
13411 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13412 appears as an element of LIST or as the car of an element of LIST.
13413 If PROPVAL is a list, compare each element against LIST in that
13414 way, and return 1 if any element of PROPVAL is found in LIST.
13415 Otherwise return 0. This function cannot quit. */
13416
13417 int
13418 invisible_p (propval, list)
13419 register Lisp_Object propval;
13420 Lisp_Object list;
13421 {
13422 register Lisp_Object tail, proptail;
13423
13424 for (tail = list; CONSP (tail); tail = XCDR (tail))
13425 {
13426 register Lisp_Object tem;
13427 tem = XCAR (tail);
13428 if (EQ (propval, tem))
13429 return 1;
13430 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13431 return 1;
13432 }
13433
13434 if (CONSP (propval))
13435 {
13436 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13437 {
13438 Lisp_Object propelt;
13439 propelt = XCAR (proptail);
13440 for (tail = list; CONSP (tail); tail = XCDR (tail))
13441 {
13442 register Lisp_Object tem;
13443 tem = XCAR (tail);
13444 if (EQ (propelt, tem))
13445 return 1;
13446 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13447 return 1;
13448 }
13449 }
13450 }
13451
13452 return 0;
13453 }
13454
13455
13456 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13457 the cdr of that element is non-nil. If PROPVAL is a list, check
13458 each element of PROPVAL in that way, and the first time some
13459 element is found, return 1 if the cdr of that element is non-nil.
13460 Otherwise return 0. This function cannot quit. */
13461
13462 int
13463 invisible_ellipsis_p (propval, list)
13464 register Lisp_Object propval;
13465 Lisp_Object list;
13466 {
13467 register Lisp_Object tail, proptail;
13468
13469 for (tail = list; CONSP (tail); tail = XCDR (tail))
13470 {
13471 register Lisp_Object tem;
13472 tem = XCAR (tail);
13473 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13474 return ! NILP (XCDR (tem));
13475 }
13476
13477 if (CONSP (propval))
13478 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13479 {
13480 Lisp_Object propelt;
13481 propelt = XCAR (proptail);
13482 for (tail = list; CONSP (tail); tail = XCDR (tail))
13483 {
13484 register Lisp_Object tem;
13485 tem = XCAR (tail);
13486 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13487 return ! NILP (XCDR (tem));
13488 }
13489 }
13490
13491 return 0;
13492 }
13493
13494
13495 \f
13496 /***********************************************************************
13497 Initialization
13498 ***********************************************************************/
13499
13500 void
13501 syms_of_xdisp ()
13502 {
13503 Vwith_echo_area_save_vector = Qnil;
13504 staticpro (&Vwith_echo_area_save_vector);
13505
13506 Vmessage_stack = Qnil;
13507 staticpro (&Vmessage_stack);
13508
13509 Qinhibit_redisplay = intern ("inhibit-redisplay");
13510 staticpro (&Qinhibit_redisplay);
13511
13512 #if GLYPH_DEBUG
13513 defsubr (&Sdump_glyph_matrix);
13514 defsubr (&Sdump_glyph_row);
13515 defsubr (&Sdump_tool_bar_row);
13516 defsubr (&Strace_redisplay_toggle);
13517 defsubr (&Strace_to_stderr);
13518 #endif
13519
13520 staticpro (&Qmenu_bar_update_hook);
13521 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13522
13523 staticpro (&Qoverriding_terminal_local_map);
13524 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13525
13526 staticpro (&Qoverriding_local_map);
13527 Qoverriding_local_map = intern ("overriding-local-map");
13528
13529 staticpro (&Qwindow_scroll_functions);
13530 Qwindow_scroll_functions = intern ("window-scroll-functions");
13531
13532 staticpro (&Qredisplay_end_trigger_functions);
13533 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13534
13535 staticpro (&Qinhibit_point_motion_hooks);
13536 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13537
13538 QCdata = intern (":data");
13539 staticpro (&QCdata);
13540 Qdisplay = intern ("display");
13541 staticpro (&Qdisplay);
13542 Qspace_width = intern ("space-width");
13543 staticpro (&Qspace_width);
13544 Qraise = intern ("raise");
13545 staticpro (&Qraise);
13546 Qspace = intern ("space");
13547 staticpro (&Qspace);
13548 Qmargin = intern ("margin");
13549 staticpro (&Qmargin);
13550 Qleft_margin = intern ("left-margin");
13551 staticpro (&Qleft_margin);
13552 Qright_margin = intern ("right-margin");
13553 staticpro (&Qright_margin);
13554 Qalign_to = intern ("align-to");
13555 staticpro (&Qalign_to);
13556 QCalign_to = intern (":align-to");
13557 staticpro (&QCalign_to);
13558 Qrelative_width = intern ("relative-width");
13559 staticpro (&Qrelative_width);
13560 QCrelative_width = intern (":relative-width");
13561 staticpro (&QCrelative_width);
13562 QCrelative_height = intern (":relative-height");
13563 staticpro (&QCrelative_height);
13564 QCeval = intern (":eval");
13565 staticpro (&QCeval);
13566 Qwhen = intern ("when");
13567 staticpro (&Qwhen);
13568 QCfile = intern (":file");
13569 staticpro (&QCfile);
13570 Qfontified = intern ("fontified");
13571 staticpro (&Qfontified);
13572 Qfontification_functions = intern ("fontification-functions");
13573 staticpro (&Qfontification_functions);
13574 Qtrailing_whitespace = intern ("trailing-whitespace");
13575 staticpro (&Qtrailing_whitespace);
13576 Qimage = intern ("image");
13577 staticpro (&Qimage);
13578 Qmessage_truncate_lines = intern ("message-truncate-lines");
13579 staticpro (&Qmessage_truncate_lines);
13580
13581 last_arrow_position = Qnil;
13582 last_arrow_string = Qnil;
13583 staticpro (&last_arrow_position);
13584 staticpro (&last_arrow_string);
13585
13586 echo_buffer[0] = echo_buffer[1] = Qnil;
13587 staticpro (&echo_buffer[0]);
13588 staticpro (&echo_buffer[1]);
13589
13590 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13591 staticpro (&echo_area_buffer[0]);
13592 staticpro (&echo_area_buffer[1]);
13593
13594 Vmessages_buffer_name = build_string ("*Messages*");
13595 staticpro (&Vmessages_buffer_name);
13596
13597 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13598 "Non-nil means highlight trailing whitespace.\n\
13599 The face used for trailing whitespace is `trailing-whitespace'.");
13600 Vshow_trailing_whitespace = Qnil;
13601
13602 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13603 "Non-nil means don't actually do any redisplay.\n\
13604 This is used for internal purposes.");
13605 Vinhibit_redisplay = Qnil;
13606
13607 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13608 "String (or mode line construct) included (normally) in `mode-line-format'.");
13609 Vglobal_mode_string = Qnil;
13610
13611 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13612 "Marker for where to display an arrow on top of the buffer text.\n\
13613 This must be the beginning of a line in order to work.\n\
13614 See also `overlay-arrow-string'.");
13615 Voverlay_arrow_position = Qnil;
13616
13617 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13618 "String to display as an arrow. See also `overlay-arrow-position'.");
13619 Voverlay_arrow_string = Qnil;
13620
13621 DEFVAR_INT ("scroll-step", &scroll_step,
13622 "*The number of lines to try scrolling a window by when point moves out.\n\
13623 If that fails to bring point back on frame, point is centered instead.\n\
13624 If this is zero, point is always centered after it moves off frame.");
13625
13626 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13627 "*Scroll up to this many lines, to bring point back on screen.\n\
13628 A value of zero means to scroll the text to center point vertically\n\
13629 in the window.");
13630 scroll_conservatively = 0;
13631
13632 DEFVAR_INT ("scroll-margin", &scroll_margin,
13633 "*Number of lines of margin at the top and bottom of a window.\n\
13634 Recenter the window whenever point gets within this many lines\n\
13635 of the top or bottom of the window.");
13636 scroll_margin = 0;
13637
13638 #if GLYPH_DEBUG
13639 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13640 #endif
13641
13642 DEFVAR_BOOL ("truncate-partial-width-windows",
13643 &truncate_partial_width_windows,
13644 "*Non-nil means truncate lines in all windows less than full frame wide.");
13645 truncate_partial_width_windows = 1;
13646
13647 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13648 "*Non-nil means use inverse video for the mode line.");
13649 mode_line_inverse_video = 1;
13650
13651 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13652 "*Maximum buffer size for which line number should be displayed.\n\
13653 If the buffer is bigger than this, the line number does not appear\n\
13654 in the mode line. A value of nil means no limit.");
13655 Vline_number_display_limit = Qnil;
13656
13657 DEFVAR_INT ("line-number-display-limit-width",
13658 &line_number_display_limit_width,
13659 "*Maximum line width (in characters) for line number display.\n\
13660 If the average length of the lines near point is bigger than this, then the\n\
13661 line number may be omitted from the mode line.");
13662 line_number_display_limit_width = 200;
13663
13664 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13665 "*Non-nil means highlight region even in nonselected windows.");
13666 highlight_nonselected_windows = 0;
13667
13668 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13669 "Non-nil if more than one frame is visible on this display.\n\
13670 Minibuffer-only frames don't count, but iconified frames do.\n\
13671 This variable is not guaranteed to be accurate except while processing\n\
13672 `frame-title-format' and `icon-title-format'.");
13673
13674 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13675 "Template for displaying the title bar of visible frames.\n\
13676 \(Assuming the window manager supports this feature.)\n\
13677 This variable has the same structure as `mode-line-format' (which see),\n\
13678 and is used only on frames for which no explicit name has been set\n\
13679 \(see `modify-frame-parameters').");
13680 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13681 "Template for displaying the title bar of an iconified frame.\n\
13682 \(Assuming the window manager supports this feature.)\n\
13683 This variable has the same structure as `mode-line-format' (which see),\n\
13684 and is used only on frames for which no explicit name has been set\n\
13685 \(see `modify-frame-parameters').");
13686 Vicon_title_format
13687 = Vframe_title_format
13688 = Fcons (intern ("multiple-frames"),
13689 Fcons (build_string ("%b"),
13690 Fcons (Fcons (build_string (""),
13691 Fcons (intern ("invocation-name"),
13692 Fcons (build_string ("@"),
13693 Fcons (intern ("system-name"),
13694 Qnil)))),
13695 Qnil)));
13696
13697 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13698 "Maximum number of lines to keep in the message log buffer.\n\
13699 If nil, disable message logging. If t, log messages but don't truncate\n\
13700 the buffer when it becomes large.");
13701 XSETFASTINT (Vmessage_log_max, 50);
13702
13703 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13704 "Functions called before redisplay, if window sizes have changed.\n\
13705 The value should be a list of functions that take one argument.\n\
13706 Just before redisplay, for each frame, if any of its windows have changed\n\
13707 size since the last redisplay, or have been split or deleted,\n\
13708 all the functions in the list are called, with the frame as argument.");
13709 Vwindow_size_change_functions = Qnil;
13710
13711 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13712 "List of Functions to call before redisplaying a window with scrolling.\n\
13713 Each function is called with two arguments, the window\n\
13714 and its new display-start position. Note that the value of `window-end'\n\
13715 is not valid when these functions are called.");
13716 Vwindow_scroll_functions = Qnil;
13717
13718 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13719 "*Non-nil means automatically resize tool-bars.\n\
13720 This increases a tool-bar's height if not all tool-bar items are visible.\n\
13721 It decreases a tool-bar's height when it would display blank lines\n\
13722 otherwise.");
13723 auto_resize_tool_bars_p = 1;
13724
13725 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
13726 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
13727 auto_raise_tool_bar_buttons_p = 1;
13728
13729 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
13730 "*Margin around tool-bar buttons in pixels.");
13731 tool_bar_button_margin = 1;
13732
13733 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
13734 "Relief thickness of tool-bar buttons.");
13735 tool_bar_button_relief = 3;
13736
13737 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
13738 "List of functions to call to fontify regions of text.\n\
13739 Each function is called with one argument POS. Functions must\n\
13740 fontify a region starting at POS in the current buffer, and give\n\
13741 fontified regions the property `fontified'.\n\
13742 This variable automatically becomes buffer-local when set.");
13743 Vfontification_functions = Qnil;
13744 Fmake_local_variable (Qfontification_functions);
13745
13746 DEFVAR_BOOL ("unibyte-display-via-language-environment",
13747 &unibyte_display_via_language_environment,
13748 "*Non-nil means display unibyte text according to language environment.\n\
13749 Specifically this means that unibyte non-ASCII characters\n\
13750 are displayed by converting them to the equivalent multibyte characters\n\
13751 according to the current language environment. As a result, they are\n\
13752 displayed according to the current fontset.");
13753 unibyte_display_via_language_environment = 0;
13754
13755 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
13756 "*Maximum height for resizing mini-windows.\n\
13757 If a float, it specifies a fraction of the mini-window frame's height.\n\
13758 If an integer, it specifies a number of lines.\n\
13759 If nil, don't resize.");
13760 Vmax_mini_window_height = make_float (0.25);
13761
13762 DEFVAR_BOOL ("cursor-in-non-selected-windows",
13763 &cursor_in_non_selected_windows,
13764 "*Non-nil means display a hollow cursor in non-selected windows.\n\
13765 Nil means don't display a cursor there.");
13766 cursor_in_non_selected_windows = 1;
13767
13768 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
13769 "*Non-nil means scroll the display automatically to make point visible.");
13770 automatic_hscrolling_p = 1;
13771
13772 DEFVAR_LISP ("image-types", &Vimage_types,
13773 "List of supported image types.\n\
13774 Each element of the list is a symbol for a supported image type.");
13775 Vimage_types = Qnil;
13776
13777 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
13778 "If non-nil, messages are truncated instead of resizing the echo area.\n\
13779 Bind this around calls to `message' to let it take effect.");
13780 message_truncate_lines = 0;
13781
13782 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
13783 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
13784 Can be used to update submenus whose contents should vary.");
13785
13786 }
13787
13788
13789 /* Initialize this module when Emacs starts. */
13790
13791 void
13792 init_xdisp ()
13793 {
13794 Lisp_Object root_window;
13795 struct window *mini_w;
13796
13797 CHARPOS (this_line_start_pos) = 0;
13798
13799 mini_w = XWINDOW (minibuf_window);
13800 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
13801
13802 if (!noninteractive)
13803 {
13804 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
13805 int i;
13806
13807 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
13808 set_window_height (root_window,
13809 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
13810 0);
13811 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
13812 set_window_height (minibuf_window, 1, 0);
13813
13814 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
13815 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
13816
13817 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
13818 scratch_glyph_row.glyphs[TEXT_AREA + 1]
13819 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
13820
13821 /* The default ellipsis glyphs `...'. */
13822 for (i = 0; i < 3; ++i)
13823 XSETFASTINT (default_invis_vector[i], '.');
13824 }
13825
13826 #ifdef HAVE_WINDOW_SYSTEM
13827 {
13828 /* Allocate the buffer for frame titles. */
13829 int size = 100;
13830 frame_title_buf = (char *) xmalloc (size);
13831 frame_title_buf_end = frame_title_buf + size;
13832 frame_title_ptr = NULL;
13833 }
13834 #endif /* HAVE_WINDOW_SYSTEM */
13835
13836 help_echo_showing_p = 0;
13837 }
13838
13839