]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(verify_interval_modification): Don't run
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
200
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229 Lisp_Object Qinhibit_eval_during_redisplay;
230 Lisp_Object Qbuffer_position, Qposition, Qobject;
231
232 /* Functions called to fontify regions of text. */
233
234 Lisp_Object Vfontification_functions;
235 Lisp_Object Qfontification_functions;
236
237 /* Non-zero means draw tool bar buttons raised when the mouse moves
238 over them. */
239
240 int auto_raise_tool_bar_buttons_p;
241
242 /* Margin around tool bar buttons in pixels. */
243
244 Lisp_Object Vtool_bar_button_margin;
245
246 /* Thickness of shadow to draw around tool bar buttons. */
247
248 int tool_bar_button_relief;
249
250 /* Non-zero means automatically resize tool-bars so that all tool-bar
251 items are visible, and no blank lines remain. */
252
253 int auto_resize_tool_bars_p;
254
255 /* Non-nil means don't actually do any redisplay. */
256
257 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
258
259 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
260
261 int inhibit_eval_during_redisplay;
262
263 /* Names of text properties relevant for redisplay. */
264
265 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
266 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
267
268 /* Symbols used in text property values. */
269
270 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
271 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
272 Lisp_Object Qmargin;
273 extern Lisp_Object Qheight;
274
275 /* Non-nil means highlight trailing whitespace. */
276
277 Lisp_Object Vshow_trailing_whitespace;
278
279 /* Name of the face used to highlight trailing whitespace. */
280
281 Lisp_Object Qtrailing_whitespace;
282
283 /* The symbol `image' which is the car of the lists used to represent
284 images in Lisp. */
285
286 Lisp_Object Qimage;
287
288 /* Non-zero means print newline to stdout before next mini-buffer
289 message. */
290
291 int noninteractive_need_newline;
292
293 /* Non-zero means print newline to message log before next message. */
294
295 static int message_log_need_newline;
296
297 \f
298 /* The buffer position of the first character appearing entirely or
299 partially on the line of the selected window which contains the
300 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
301 redisplay optimization in redisplay_internal. */
302
303 static struct text_pos this_line_start_pos;
304
305 /* Number of characters past the end of the line above, including the
306 terminating newline. */
307
308 static struct text_pos this_line_end_pos;
309
310 /* The vertical positions and the height of this line. */
311
312 static int this_line_vpos;
313 static int this_line_y;
314 static int this_line_pixel_height;
315
316 /* X position at which this display line starts. Usually zero;
317 negative if first character is partially visible. */
318
319 static int this_line_start_x;
320
321 /* Buffer that this_line_.* variables are referring to. */
322
323 static struct buffer *this_line_buffer;
324
325 /* Nonzero means truncate lines in all windows less wide than the
326 frame. */
327
328 int truncate_partial_width_windows;
329
330 /* A flag to control how to display unibyte 8-bit character. */
331
332 int unibyte_display_via_language_environment;
333
334 /* Nonzero means we have more than one non-mini-buffer-only frame.
335 Not guaranteed to be accurate except while parsing
336 frame-title-format. */
337
338 int multiple_frames;
339
340 Lisp_Object Vglobal_mode_string;
341
342 /* Marker for where to display an arrow on top of the buffer text. */
343
344 Lisp_Object Voverlay_arrow_position;
345
346 /* String to display for the arrow. Only used on terminal frames. */
347
348 Lisp_Object Voverlay_arrow_string;
349
350 /* Values of those variables at last redisplay. However, if
351 Voverlay_arrow_position is a marker, last_arrow_position is its
352 numerical position. */
353
354 static Lisp_Object last_arrow_position, last_arrow_string;
355
356 /* Like mode-line-format, but for the title bar on a visible frame. */
357
358 Lisp_Object Vframe_title_format;
359
360 /* Like mode-line-format, but for the title bar on an iconified frame. */
361
362 Lisp_Object Vicon_title_format;
363
364 /* List of functions to call when a window's size changes. These
365 functions get one arg, a frame on which one or more windows' sizes
366 have changed. */
367
368 static Lisp_Object Vwindow_size_change_functions;
369
370 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
371
372 /* Nonzero if overlay arrow has been displayed once in this window. */
373
374 static int overlay_arrow_seen;
375
376 /* Nonzero means highlight the region even in nonselected windows. */
377
378 int highlight_nonselected_windows;
379
380 /* If cursor motion alone moves point off frame, try scrolling this
381 many lines up or down if that will bring it back. */
382
383 static int scroll_step;
384
385 /* Non-0 means scroll just far enough to bring point back on the
386 screen, when appropriate. */
387
388 static int scroll_conservatively;
389
390 /* Recenter the window whenever point gets within this many lines of
391 the top or bottom of the window. This value is translated into a
392 pixel value by multiplying it with CANON_Y_UNIT, which means that
393 there is really a fixed pixel height scroll margin. */
394
395 int scroll_margin;
396
397 /* Number of windows showing the buffer of the selected window (or
398 another buffer with the same base buffer). keyboard.c refers to
399 this. */
400
401 int buffer_shared;
402
403 /* Vector containing glyphs for an ellipsis `...'. */
404
405 static Lisp_Object default_invis_vector[3];
406
407 /* Zero means display the mode-line/header-line/menu-bar in the default face
408 (this slightly odd definition is for compatibility with previous versions
409 of emacs), non-zero means display them using their respective faces.
410
411 This variable is deprecated. */
412
413 int mode_line_inverse_video;
414
415 /* Prompt to display in front of the mini-buffer contents. */
416
417 Lisp_Object minibuf_prompt;
418
419 /* Width of current mini-buffer prompt. Only set after display_line
420 of the line that contains the prompt. */
421
422 int minibuf_prompt_width;
423 int minibuf_prompt_pixel_width;
424
425 /* This is the window where the echo area message was displayed. It
426 is always a mini-buffer window, but it may not be the same window
427 currently active as a mini-buffer. */
428
429 Lisp_Object echo_area_window;
430
431 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
432 pushes the current message and the value of
433 message_enable_multibyte on the stack, the function restore_message
434 pops the stack and displays MESSAGE again. */
435
436 Lisp_Object Vmessage_stack;
437
438 /* Nonzero means multibyte characters were enabled when the echo area
439 message was specified. */
440
441 int message_enable_multibyte;
442
443 /* True if we should redraw the mode lines on the next redisplay. */
444
445 int update_mode_lines;
446
447 /* Nonzero if window sizes or contents have changed since last
448 redisplay that finished */
449
450 int windows_or_buffers_changed;
451
452 /* Nonzero after display_mode_line if %l was used and it displayed a
453 line number. */
454
455 int line_number_displayed;
456
457 /* Maximum buffer size for which to display line numbers. */
458
459 Lisp_Object Vline_number_display_limit;
460
461 /* line width to consider when repostioning for line number display */
462
463 static int line_number_display_limit_width;
464
465 /* Number of lines to keep in the message log buffer. t means
466 infinite. nil means don't log at all. */
467
468 Lisp_Object Vmessage_log_max;
469
470 /* The name of the *Messages* buffer, a string. */
471
472 static Lisp_Object Vmessages_buffer_name;
473
474 /* Current, index 0, and last displayed echo area message. Either
475 buffers from echo_buffers, or nil to indicate no message. */
476
477 Lisp_Object echo_area_buffer[2];
478
479 /* The buffers referenced from echo_area_buffer. */
480
481 static Lisp_Object echo_buffer[2];
482
483 /* A vector saved used in with_area_buffer to reduce consing. */
484
485 static Lisp_Object Vwith_echo_area_save_vector;
486
487 /* Non-zero means display_echo_area should display the last echo area
488 message again. Set by redisplay_preserve_echo_area. */
489
490 static int display_last_displayed_message_p;
491
492 /* Nonzero if echo area is being used by print; zero if being used by
493 message. */
494
495 int message_buf_print;
496
497 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
498
499 Lisp_Object Qinhibit_menubar_update;
500 int inhibit_menubar_update;
501
502 /* Maximum height for resizing mini-windows. Either a float
503 specifying a fraction of the available height, or an integer
504 specifying a number of lines. */
505
506 Lisp_Object Vmax_mini_window_height;
507
508 /* Non-zero means messages should be displayed with truncated
509 lines instead of being continued. */
510
511 int message_truncate_lines;
512 Lisp_Object Qmessage_truncate_lines;
513
514 /* Set to 1 in clear_message to make redisplay_internal aware
515 of an emptied echo area. */
516
517 static int message_cleared_p;
518
519 /* Non-zero means we want a hollow cursor in windows that are not
520 selected. Zero means there's no cursor in such windows. */
521
522 int cursor_in_non_selected_windows;
523
524 /* A scratch glyph row with contents used for generating truncation
525 glyphs. Also used in direct_output_for_insert. */
526
527 #define MAX_SCRATCH_GLYPHS 100
528 struct glyph_row scratch_glyph_row;
529 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
530
531 /* Ascent and height of the last line processed by move_it_to. */
532
533 static int last_max_ascent, last_height;
534
535 /* Non-zero if there's a help-echo in the echo area. */
536
537 int help_echo_showing_p;
538
539 /* If >= 0, computed, exact values of mode-line and header-line height
540 to use in the macros CURRENT_MODE_LINE_HEIGHT and
541 CURRENT_HEADER_LINE_HEIGHT. */
542
543 int current_mode_line_height, current_header_line_height;
544
545 /* The maximum distance to look ahead for text properties. Values
546 that are too small let us call compute_char_face and similar
547 functions too often which is expensive. Values that are too large
548 let us call compute_char_face and alike too often because we
549 might not be interested in text properties that far away. */
550
551 #define TEXT_PROP_DISTANCE_LIMIT 100
552
553 #if GLYPH_DEBUG
554
555 /* Non-zero means print traces of redisplay if compiled with
556 GLYPH_DEBUG != 0. */
557
558 int trace_redisplay_p;
559
560 #endif /* GLYPH_DEBUG */
561
562 #ifdef DEBUG_TRACE_MOVE
563 /* Non-zero means trace with TRACE_MOVE to stderr. */
564 int trace_move;
565
566 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
567 #else
568 #define TRACE_MOVE(x) (void) 0
569 #endif
570
571 /* Non-zero means automatically scroll windows horizontally to make
572 point visible. */
573
574 int automatic_hscrolling_p;
575
576 /* A list of symbols, one for each supported image type. */
577
578 Lisp_Object Vimage_types;
579
580 /* The variable `resize-mini-windows'. If nil, don't resize
581 mini-windows. If t, always resize them to fit the text they
582 display. If `grow-only', let mini-windows grow only until they
583 become empty. */
584
585 Lisp_Object Vresize_mini_windows;
586
587 /* Value returned from text property handlers (see below). */
588
589 enum prop_handled
590 {
591 HANDLED_NORMALLY,
592 HANDLED_RECOMPUTE_PROPS,
593 HANDLED_OVERLAY_STRING_CONSUMED,
594 HANDLED_RETURN
595 };
596
597 /* A description of text properties that redisplay is interested
598 in. */
599
600 struct props
601 {
602 /* The name of the property. */
603 Lisp_Object *name;
604
605 /* A unique index for the property. */
606 enum prop_idx idx;
607
608 /* A handler function called to set up iterator IT from the property
609 at IT's current position. Value is used to steer handle_stop. */
610 enum prop_handled (*handler) P_ ((struct it *it));
611 };
612
613 static enum prop_handled handle_face_prop P_ ((struct it *));
614 static enum prop_handled handle_invisible_prop P_ ((struct it *));
615 static enum prop_handled handle_display_prop P_ ((struct it *));
616 static enum prop_handled handle_composition_prop P_ ((struct it *));
617 static enum prop_handled handle_overlay_change P_ ((struct it *));
618 static enum prop_handled handle_fontified_prop P_ ((struct it *));
619
620 /* Properties handled by iterators. */
621
622 static struct props it_props[] =
623 {
624 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
625 /* Handle `face' before `display' because some sub-properties of
626 `display' need to know the face. */
627 {&Qface, FACE_PROP_IDX, handle_face_prop},
628 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
629 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
630 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
631 {NULL, 0, NULL}
632 };
633
634 /* Value is the position described by X. If X is a marker, value is
635 the marker_position of X. Otherwise, value is X. */
636
637 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
638
639 /* Enumeration returned by some move_it_.* functions internally. */
640
641 enum move_it_result
642 {
643 /* Not used. Undefined value. */
644 MOVE_UNDEFINED,
645
646 /* Move ended at the requested buffer position or ZV. */
647 MOVE_POS_MATCH_OR_ZV,
648
649 /* Move ended at the requested X pixel position. */
650 MOVE_X_REACHED,
651
652 /* Move within a line ended at the end of a line that must be
653 continued. */
654 MOVE_LINE_CONTINUED,
655
656 /* Move within a line ended at the end of a line that would
657 be displayed truncated. */
658 MOVE_LINE_TRUNCATED,
659
660 /* Move within a line ended at a line end. */
661 MOVE_NEWLINE_OR_CR
662 };
663
664
665 \f
666 /* Function prototypes. */
667
668 static void setup_for_ellipsis P_ ((struct it *));
669 static void mark_window_display_accurate_1 P_ ((struct window *, int));
670 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
671 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
672 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
673 static int redisplay_mode_lines P_ ((Lisp_Object, int));
674 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
675 static int invisible_text_between_p P_ ((struct it *, int, int));
676 static int next_element_from_ellipsis P_ ((struct it *));
677 static void pint2str P_ ((char *, int, int));
678 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
679 struct text_pos));
680 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
681 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
682 static void store_frame_title_char P_ ((char));
683 static int store_frame_title P_ ((unsigned char *, int, int));
684 static void x_consider_frame_title P_ ((Lisp_Object));
685 static void handle_stop P_ ((struct it *));
686 static int tool_bar_lines_needed P_ ((struct frame *));
687 static int single_display_prop_intangible_p P_ ((Lisp_Object));
688 static void ensure_echo_area_buffers P_ ((void));
689 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
690 struct glyph_row *,
691 struct glyph_row *));
692 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
693 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
694 static int with_echo_area_buffer P_ ((struct window *, int,
695 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
696 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
697 static void clear_garbaged_frames P_ ((void));
698 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
699 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
700 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
701 static int display_echo_area P_ ((struct window *));
702 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
703 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
704 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
705 static int string_char_and_length P_ ((unsigned char *, int, int *));
706 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
707 struct text_pos));
708 static int compute_window_start_on_continuation_line P_ ((struct window *));
709 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
710 static void insert_left_trunc_glyphs P_ ((struct it *));
711 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
712 static void extend_face_to_end_of_line P_ ((struct it *));
713 static int append_space P_ ((struct it *, int));
714 static void make_cursor_line_fully_visible P_ ((struct window *));
715 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
716 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
717 static int trailing_whitespace_p P_ ((int));
718 static int message_log_check_duplicate P_ ((int, int, int, int));
719 int invisible_p P_ ((Lisp_Object, Lisp_Object));
720 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
721 static void push_it P_ ((struct it *));
722 static void pop_it P_ ((struct it *));
723 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
724 static void redisplay_internal P_ ((int));
725 static int echo_area_display P_ ((int));
726 static void redisplay_windows P_ ((Lisp_Object));
727 static void redisplay_window P_ ((Lisp_Object, int));
728 static void update_menu_bar P_ ((struct frame *, int));
729 static int try_window_reusing_current_matrix P_ ((struct window *));
730 static int try_window_id P_ ((struct window *));
731 static int display_line P_ ((struct it *));
732 static int display_mode_lines P_ ((struct window *));
733 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
734 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
735 static char *decode_mode_spec P_ ((struct window *, int, int, int));
736 static void display_menu_bar P_ ((struct window *));
737 static int display_count_lines P_ ((int, int, int, int, int *));
738 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
739 int, int, struct it *, int, int, int, int));
740 static void compute_line_metrics P_ ((struct it *));
741 static void run_redisplay_end_trigger_hook P_ ((struct it *));
742 static int get_overlay_strings P_ ((struct it *, int));
743 static void next_overlay_string P_ ((struct it *));
744 static void reseat P_ ((struct it *, struct text_pos, int));
745 static void reseat_1 P_ ((struct it *, struct text_pos, int));
746 static void back_to_previous_visible_line_start P_ ((struct it *));
747 static void reseat_at_previous_visible_line_start P_ ((struct it *));
748 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
749 static int next_element_from_display_vector P_ ((struct it *));
750 static int next_element_from_string P_ ((struct it *));
751 static int next_element_from_c_string P_ ((struct it *));
752 static int next_element_from_buffer P_ ((struct it *));
753 static int next_element_from_composition P_ ((struct it *));
754 static int next_element_from_image P_ ((struct it *));
755 static int next_element_from_stretch P_ ((struct it *));
756 static void load_overlay_strings P_ ((struct it *, int));
757 static int init_from_display_pos P_ ((struct it *, struct window *,
758 struct display_pos *));
759 static void reseat_to_string P_ ((struct it *, unsigned char *,
760 Lisp_Object, int, int, int, int));
761 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
762 int, int, int));
763 void move_it_vertically_backward P_ ((struct it *, int));
764 static void init_to_row_start P_ ((struct it *, struct window *,
765 struct glyph_row *));
766 static int init_to_row_end P_ ((struct it *, struct window *,
767 struct glyph_row *));
768 static void back_to_previous_line_start P_ ((struct it *));
769 static int forward_to_next_line_start P_ ((struct it *, int *));
770 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
771 Lisp_Object, int));
772 static struct text_pos string_pos P_ ((int, Lisp_Object));
773 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
774 static int number_of_chars P_ ((unsigned char *, int));
775 static void compute_stop_pos P_ ((struct it *));
776 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
777 Lisp_Object));
778 static int face_before_or_after_it_pos P_ ((struct it *, int));
779 static int next_overlay_change P_ ((int));
780 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
781 Lisp_Object, struct text_pos *,
782 int));
783 static int underlying_face_id P_ ((struct it *));
784 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
785 struct window *));
786
787 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
788 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
789
790 #ifdef HAVE_WINDOW_SYSTEM
791
792 static void update_tool_bar P_ ((struct frame *, int));
793 static void build_desired_tool_bar_string P_ ((struct frame *f));
794 static int redisplay_tool_bar P_ ((struct frame *));
795 static void display_tool_bar_line P_ ((struct it *));
796
797 #endif /* HAVE_WINDOW_SYSTEM */
798
799 \f
800 /***********************************************************************
801 Window display dimensions
802 ***********************************************************************/
803
804 /* Return the window-relative maximum y + 1 for glyph rows displaying
805 text in window W. This is the height of W minus the height of a
806 mode line, if any. */
807
808 INLINE int
809 window_text_bottom_y (w)
810 struct window *w;
811 {
812 struct frame *f = XFRAME (w->frame);
813 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
814
815 if (WINDOW_WANTS_MODELINE_P (w))
816 height -= CURRENT_MODE_LINE_HEIGHT (w);
817 return height;
818 }
819
820
821 /* Return the pixel width of display area AREA of window W. AREA < 0
822 means return the total width of W, not including bitmap areas to
823 the left and right of the window. */
824
825 INLINE int
826 window_box_width (w, area)
827 struct window *w;
828 int area;
829 {
830 struct frame *f = XFRAME (w->frame);
831 int width = XFASTINT (w->width);
832
833 if (!w->pseudo_window_p)
834 {
835 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
836
837 if (area == TEXT_AREA)
838 {
839 if (INTEGERP (w->left_margin_width))
840 width -= XFASTINT (w->left_margin_width);
841 if (INTEGERP (w->right_margin_width))
842 width -= XFASTINT (w->right_margin_width);
843 }
844 else if (area == LEFT_MARGIN_AREA)
845 width = (INTEGERP (w->left_margin_width)
846 ? XFASTINT (w->left_margin_width) : 0);
847 else if (area == RIGHT_MARGIN_AREA)
848 width = (INTEGERP (w->right_margin_width)
849 ? XFASTINT (w->right_margin_width) : 0);
850 }
851
852 return width * CANON_X_UNIT (f);
853 }
854
855
856 /* Return the pixel height of the display area of window W, not
857 including mode lines of W, if any.. */
858
859 INLINE int
860 window_box_height (w)
861 struct window *w;
862 {
863 struct frame *f = XFRAME (w->frame);
864 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
865
866 xassert (height >= 0);
867
868 /* Note: the code below that determines the mode-line/header-line
869 height is essentially the same as that contained in the macro
870 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
871 the appropriate glyph row has its `mode_line_p' flag set,
872 and if it doesn't, uses estimate_mode_line_height instead. */
873
874 if (WINDOW_WANTS_MODELINE_P (w))
875 {
876 struct glyph_row *ml_row
877 = (w->current_matrix && w->current_matrix->rows
878 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
879 : 0);
880 if (ml_row && ml_row->mode_line_p)
881 height -= ml_row->height;
882 else
883 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
884 }
885
886 if (WINDOW_WANTS_HEADER_LINE_P (w))
887 {
888 struct glyph_row *hl_row
889 = (w->current_matrix && w->current_matrix->rows
890 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
891 : 0);
892 if (hl_row && hl_row->mode_line_p)
893 height -= hl_row->height;
894 else
895 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
896 }
897
898 return height;
899 }
900
901
902 /* Return the frame-relative coordinate of the left edge of display
903 area AREA of window W. AREA < 0 means return the left edge of the
904 whole window, to the right of any bitmap area at the left side of
905 W. */
906
907 INLINE int
908 window_box_left (w, area)
909 struct window *w;
910 int area;
911 {
912 struct frame *f = XFRAME (w->frame);
913 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
914
915 if (!w->pseudo_window_p)
916 {
917 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
918 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
919
920 if (area == TEXT_AREA)
921 x += window_box_width (w, LEFT_MARGIN_AREA);
922 else if (area == RIGHT_MARGIN_AREA)
923 x += (window_box_width (w, LEFT_MARGIN_AREA)
924 + window_box_width (w, TEXT_AREA));
925 }
926
927 return x;
928 }
929
930
931 /* Return the frame-relative coordinate of the right edge of display
932 area AREA of window W. AREA < 0 means return the left edge of the
933 whole window, to the left of any bitmap area at the right side of
934 W. */
935
936 INLINE int
937 window_box_right (w, area)
938 struct window *w;
939 int area;
940 {
941 return window_box_left (w, area) + window_box_width (w, area);
942 }
943
944
945 /* Get the bounding box of the display area AREA of window W, without
946 mode lines, in frame-relative coordinates. AREA < 0 means the
947 whole window, not including bitmap areas to the left and right of
948 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
949 coordinates of the upper-left corner of the box. Return in
950 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
951
952 INLINE void
953 window_box (w, area, box_x, box_y, box_width, box_height)
954 struct window *w;
955 int area;
956 int *box_x, *box_y, *box_width, *box_height;
957 {
958 struct frame *f = XFRAME (w->frame);
959
960 *box_width = window_box_width (w, area);
961 *box_height = window_box_height (w);
962 *box_x = window_box_left (w, area);
963 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
964 + XFASTINT (w->top) * CANON_Y_UNIT (f));
965 if (WINDOW_WANTS_HEADER_LINE_P (w))
966 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
967 }
968
969
970 /* Get the bounding box of the display area AREA of window W, without
971 mode lines. AREA < 0 means the whole window, not including bitmap
972 areas to the left and right of the window. Return in *TOP_LEFT_X
973 and TOP_LEFT_Y the frame-relative pixel coordinates of the
974 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
975 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
976 box. */
977
978 INLINE void
979 window_box_edges (w, area, top_left_x, top_left_y,
980 bottom_right_x, bottom_right_y)
981 struct window *w;
982 int area;
983 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
984 {
985 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
986 bottom_right_y);
987 *bottom_right_x += *top_left_x;
988 *bottom_right_y += *top_left_y;
989 }
990
991
992 \f
993 /***********************************************************************
994 Utilities
995 ***********************************************************************/
996
997 /* Return the bottom y-position of the line the iterator IT is in.
998 This can modify IT's settings. */
999
1000 int
1001 line_bottom_y (it)
1002 struct it *it;
1003 {
1004 int line_height = it->max_ascent + it->max_descent;
1005 int line_top_y = it->current_y;
1006
1007 if (line_height == 0)
1008 {
1009 if (last_height)
1010 line_height = last_height;
1011 else if (IT_CHARPOS (*it) < ZV)
1012 {
1013 move_it_by_lines (it, 1, 1);
1014 line_height = (it->max_ascent || it->max_descent
1015 ? it->max_ascent + it->max_descent
1016 : last_height);
1017 }
1018 else
1019 {
1020 struct glyph_row *row = it->glyph_row;
1021
1022 /* Use the default character height. */
1023 it->glyph_row = NULL;
1024 it->what = IT_CHARACTER;
1025 it->c = ' ';
1026 it->len = 1;
1027 PRODUCE_GLYPHS (it);
1028 line_height = it->ascent + it->descent;
1029 it->glyph_row = row;
1030 }
1031 }
1032
1033 return line_top_y + line_height;
1034 }
1035
1036
1037 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1038 1 if POS is visible and the line containing POS is fully visible.
1039 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1040 and header-lines heights. */
1041
1042 int
1043 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1044 struct window *w;
1045 int charpos, *fully, exact_mode_line_heights_p;
1046 {
1047 struct it it;
1048 struct text_pos top;
1049 int visible_p;
1050 struct buffer *old_buffer = NULL;
1051
1052 if (XBUFFER (w->buffer) != current_buffer)
1053 {
1054 old_buffer = current_buffer;
1055 set_buffer_internal_1 (XBUFFER (w->buffer));
1056 }
1057
1058 *fully = visible_p = 0;
1059 SET_TEXT_POS_FROM_MARKER (top, w->start);
1060
1061 /* Compute exact mode line heights, if requested. */
1062 if (exact_mode_line_heights_p)
1063 {
1064 if (WINDOW_WANTS_MODELINE_P (w))
1065 current_mode_line_height
1066 = display_mode_line (w, MODE_LINE_FACE_ID,
1067 current_buffer->mode_line_format);
1068
1069 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 current_header_line_height
1071 = display_mode_line (w, HEADER_LINE_FACE_ID,
1072 current_buffer->header_line_format);
1073 }
1074
1075 start_display (&it, w, top);
1076 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1077 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1078
1079 /* Note that we may overshoot because of invisible text. */
1080 if (IT_CHARPOS (it) >= charpos)
1081 {
1082 int top_y = it.current_y;
1083 int bottom_y = line_bottom_y (&it);
1084 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1085
1086 if (top_y < window_top_y)
1087 visible_p = bottom_y > window_top_y;
1088 else if (top_y < it.last_visible_y)
1089 {
1090 visible_p = 1;
1091 *fully = bottom_y <= it.last_visible_y;
1092 }
1093 }
1094 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1095 {
1096 move_it_by_lines (&it, 1, 0);
1097 if (charpos < IT_CHARPOS (it))
1098 {
1099 visible_p = 1;
1100 *fully = 0;
1101 }
1102 }
1103
1104 if (old_buffer)
1105 set_buffer_internal_1 (old_buffer);
1106
1107 current_header_line_height = current_mode_line_height = -1;
1108 return visible_p;
1109 }
1110
1111
1112 /* Return the next character from STR which is MAXLEN bytes long.
1113 Return in *LEN the length of the character. This is like
1114 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1115 we find one, we return a `?', but with the length of the invalid
1116 character. */
1117
1118 static INLINE int
1119 string_char_and_length (str, maxlen, len)
1120 unsigned char *str;
1121 int maxlen, *len;
1122 {
1123 int c;
1124
1125 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1126 if (!CHAR_VALID_P (c, 1))
1127 /* We may not change the length here because other places in Emacs
1128 don't use this function, i.e. they silently accept invalid
1129 characters. */
1130 c = '?';
1131
1132 return c;
1133 }
1134
1135
1136
1137 /* Given a position POS containing a valid character and byte position
1138 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1139
1140 static struct text_pos
1141 string_pos_nchars_ahead (pos, string, nchars)
1142 struct text_pos pos;
1143 Lisp_Object string;
1144 int nchars;
1145 {
1146 xassert (STRINGP (string) && nchars >= 0);
1147
1148 if (STRING_MULTIBYTE (string))
1149 {
1150 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1151 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1152 int len;
1153
1154 while (nchars--)
1155 {
1156 string_char_and_length (p, rest, &len);
1157 p += len, rest -= len;
1158 xassert (rest >= 0);
1159 CHARPOS (pos) += 1;
1160 BYTEPOS (pos) += len;
1161 }
1162 }
1163 else
1164 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1165
1166 return pos;
1167 }
1168
1169
1170 /* Value is the text position, i.e. character and byte position,
1171 for character position CHARPOS in STRING. */
1172
1173 static INLINE struct text_pos
1174 string_pos (charpos, string)
1175 int charpos;
1176 Lisp_Object string;
1177 {
1178 struct text_pos pos;
1179 xassert (STRINGP (string));
1180 xassert (charpos >= 0);
1181 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1182 return pos;
1183 }
1184
1185
1186 /* Value is a text position, i.e. character and byte position, for
1187 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1188 means recognize multibyte characters. */
1189
1190 static struct text_pos
1191 c_string_pos (charpos, s, multibyte_p)
1192 int charpos;
1193 unsigned char *s;
1194 int multibyte_p;
1195 {
1196 struct text_pos pos;
1197
1198 xassert (s != NULL);
1199 xassert (charpos >= 0);
1200
1201 if (multibyte_p)
1202 {
1203 int rest = strlen (s), len;
1204
1205 SET_TEXT_POS (pos, 0, 0);
1206 while (charpos--)
1207 {
1208 string_char_and_length (s, rest, &len);
1209 s += len, rest -= len;
1210 xassert (rest >= 0);
1211 CHARPOS (pos) += 1;
1212 BYTEPOS (pos) += len;
1213 }
1214 }
1215 else
1216 SET_TEXT_POS (pos, charpos, charpos);
1217
1218 return pos;
1219 }
1220
1221
1222 /* Value is the number of characters in C string S. MULTIBYTE_P
1223 non-zero means recognize multibyte characters. */
1224
1225 static int
1226 number_of_chars (s, multibyte_p)
1227 unsigned char *s;
1228 int multibyte_p;
1229 {
1230 int nchars;
1231
1232 if (multibyte_p)
1233 {
1234 int rest = strlen (s), len;
1235 unsigned char *p = (unsigned char *) s;
1236
1237 for (nchars = 0; rest > 0; ++nchars)
1238 {
1239 string_char_and_length (p, rest, &len);
1240 rest -= len, p += len;
1241 }
1242 }
1243 else
1244 nchars = strlen (s);
1245
1246 return nchars;
1247 }
1248
1249
1250 /* Compute byte position NEWPOS->bytepos corresponding to
1251 NEWPOS->charpos. POS is a known position in string STRING.
1252 NEWPOS->charpos must be >= POS.charpos. */
1253
1254 static void
1255 compute_string_pos (newpos, pos, string)
1256 struct text_pos *newpos, pos;
1257 Lisp_Object string;
1258 {
1259 xassert (STRINGP (string));
1260 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1261
1262 if (STRING_MULTIBYTE (string))
1263 *newpos = string_pos_nchars_ahead (pos, string,
1264 CHARPOS (*newpos) - CHARPOS (pos));
1265 else
1266 BYTEPOS (*newpos) = CHARPOS (*newpos);
1267 }
1268
1269
1270 \f
1271 /***********************************************************************
1272 Lisp form evaluation
1273 ***********************************************************************/
1274
1275 /* Error handler for safe_eval and safe_call. */
1276
1277 static Lisp_Object
1278 safe_eval_handler (arg)
1279 Lisp_Object arg;
1280 {
1281 add_to_log ("Error during redisplay: %s", arg, Qnil);
1282 return Qnil;
1283 }
1284
1285
1286 /* Evaluate SEXPR and return the result, or nil if something went
1287 wrong. Prevent redisplay during the evaluation. */
1288
1289 Lisp_Object
1290 safe_eval (sexpr)
1291 Lisp_Object sexpr;
1292 {
1293 Lisp_Object val;
1294
1295 if (inhibit_eval_during_redisplay)
1296 val = Qnil;
1297 else
1298 {
1299 int count = BINDING_STACK_SIZE ();
1300 struct gcpro gcpro1;
1301
1302 GCPRO1 (sexpr);
1303 specbind (Qinhibit_redisplay, Qt);
1304 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1305 safe_eval_handler);
1306 UNGCPRO;
1307 val = unbind_to (count, val);
1308 }
1309
1310 return val;
1311 }
1312
1313
1314 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1315 Return the result, or nil if something went wrong. Prevent
1316 redisplay during the evaluation. */
1317
1318 Lisp_Object
1319 safe_call (nargs, args)
1320 int nargs;
1321 Lisp_Object *args;
1322 {
1323 Lisp_Object val;
1324
1325 if (inhibit_eval_during_redisplay)
1326 val = Qnil;
1327 else
1328 {
1329 int count = BINDING_STACK_SIZE ();
1330 struct gcpro gcpro1;
1331
1332 GCPRO1 (args[0]);
1333 gcpro1.nvars = nargs;
1334 specbind (Qinhibit_redisplay, Qt);
1335 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1336 safe_eval_handler);
1337 UNGCPRO;
1338 val = unbind_to (count, val);
1339 }
1340
1341 return val;
1342 }
1343
1344
1345 /* Call function FN with one argument ARG.
1346 Return the result, or nil if something went wrong. */
1347
1348 Lisp_Object
1349 safe_call1 (fn, arg)
1350 Lisp_Object fn, arg;
1351 {
1352 Lisp_Object args[2];
1353 args[0] = fn;
1354 args[1] = arg;
1355 return safe_call (2, args);
1356 }
1357
1358
1359 \f
1360 /***********************************************************************
1361 Debugging
1362 ***********************************************************************/
1363
1364 #if 0
1365
1366 /* Define CHECK_IT to perform sanity checks on iterators.
1367 This is for debugging. It is too slow to do unconditionally. */
1368
1369 static void
1370 check_it (it)
1371 struct it *it;
1372 {
1373 if (it->method == next_element_from_string)
1374 {
1375 xassert (STRINGP (it->string));
1376 xassert (IT_STRING_CHARPOS (*it) >= 0);
1377 }
1378 else if (it->method == next_element_from_buffer)
1379 {
1380 /* Check that character and byte positions agree. */
1381 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1382 }
1383
1384 if (it->dpvec)
1385 xassert (it->current.dpvec_index >= 0);
1386 else
1387 xassert (it->current.dpvec_index < 0);
1388 }
1389
1390 #define CHECK_IT(IT) check_it ((IT))
1391
1392 #else /* not 0 */
1393
1394 #define CHECK_IT(IT) (void) 0
1395
1396 #endif /* not 0 */
1397
1398
1399 #if GLYPH_DEBUG
1400
1401 /* Check that the window end of window W is what we expect it
1402 to be---the last row in the current matrix displaying text. */
1403
1404 static void
1405 check_window_end (w)
1406 struct window *w;
1407 {
1408 if (!MINI_WINDOW_P (w)
1409 && !NILP (w->window_end_valid))
1410 {
1411 struct glyph_row *row;
1412 xassert ((row = MATRIX_ROW (w->current_matrix,
1413 XFASTINT (w->window_end_vpos)),
1414 !row->enabled_p
1415 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1416 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1417 }
1418 }
1419
1420 #define CHECK_WINDOW_END(W) check_window_end ((W))
1421
1422 #else /* not GLYPH_DEBUG */
1423
1424 #define CHECK_WINDOW_END(W) (void) 0
1425
1426 #endif /* not GLYPH_DEBUG */
1427
1428
1429 \f
1430 /***********************************************************************
1431 Iterator initialization
1432 ***********************************************************************/
1433
1434 /* Initialize IT for displaying current_buffer in window W, starting
1435 at character position CHARPOS. CHARPOS < 0 means that no buffer
1436 position is specified which is useful when the iterator is assigned
1437 a position later. BYTEPOS is the byte position corresponding to
1438 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1439
1440 If ROW is not null, calls to produce_glyphs with IT as parameter
1441 will produce glyphs in that row.
1442
1443 BASE_FACE_ID is the id of a base face to use. It must be one of
1444 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1445 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1446 displaying the tool-bar.
1447
1448 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1449 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1450 corresponding mode line glyph row of the desired matrix of W. */
1451
1452 void
1453 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1454 struct it *it;
1455 struct window *w;
1456 int charpos, bytepos;
1457 struct glyph_row *row;
1458 enum face_id base_face_id;
1459 {
1460 int highlight_region_p;
1461
1462 /* Some precondition checks. */
1463 xassert (w != NULL && it != NULL);
1464 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1465
1466 /* If face attributes have been changed since the last redisplay,
1467 free realized faces now because they depend on face definitions
1468 that might have changed. */
1469 if (face_change_count)
1470 {
1471 face_change_count = 0;
1472 free_all_realized_faces (Qnil);
1473 }
1474
1475 /* Use one of the mode line rows of W's desired matrix if
1476 appropriate. */
1477 if (row == NULL)
1478 {
1479 if (base_face_id == MODE_LINE_FACE_ID)
1480 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1481 else if (base_face_id == HEADER_LINE_FACE_ID)
1482 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1483 }
1484
1485 /* Clear IT. */
1486 bzero (it, sizeof *it);
1487 it->current.overlay_string_index = -1;
1488 it->current.dpvec_index = -1;
1489 it->base_face_id = base_face_id;
1490
1491 /* The window in which we iterate over current_buffer: */
1492 XSETWINDOW (it->window, w);
1493 it->w = w;
1494 it->f = XFRAME (w->frame);
1495
1496 /* Extra space between lines (on window systems only). */
1497 if (base_face_id == DEFAULT_FACE_ID
1498 && FRAME_WINDOW_P (it->f))
1499 {
1500 if (NATNUMP (current_buffer->extra_line_spacing))
1501 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1502 else if (it->f->extra_line_spacing > 0)
1503 it->extra_line_spacing = it->f->extra_line_spacing;
1504 }
1505
1506 /* If realized faces have been removed, e.g. because of face
1507 attribute changes of named faces, recompute them. When running
1508 in batch mode, the face cache of Vterminal_frame is null. If
1509 we happen to get called, make a dummy face cache. */
1510 if (
1511 #ifndef WINDOWSNT
1512 noninteractive &&
1513 #endif
1514 FRAME_FACE_CACHE (it->f) == NULL)
1515 init_frame_faces (it->f);
1516 if (FRAME_FACE_CACHE (it->f)->used == 0)
1517 recompute_basic_faces (it->f);
1518
1519 /* Current value of the `space-width', and 'height' properties. */
1520 it->space_width = Qnil;
1521 it->font_height = Qnil;
1522
1523 /* Are control characters displayed as `^C'? */
1524 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1525
1526 /* -1 means everything between a CR and the following line end
1527 is invisible. >0 means lines indented more than this value are
1528 invisible. */
1529 it->selective = (INTEGERP (current_buffer->selective_display)
1530 ? XFASTINT (current_buffer->selective_display)
1531 : (!NILP (current_buffer->selective_display)
1532 ? -1 : 0));
1533 it->selective_display_ellipsis_p
1534 = !NILP (current_buffer->selective_display_ellipses);
1535
1536 /* Display table to use. */
1537 it->dp = window_display_table (w);
1538
1539 /* Are multibyte characters enabled in current_buffer? */
1540 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1541
1542 /* Non-zero if we should highlight the region. */
1543 highlight_region_p
1544 = (!NILP (Vtransient_mark_mode)
1545 && !NILP (current_buffer->mark_active)
1546 && XMARKER (current_buffer->mark)->buffer != 0);
1547
1548 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1549 start and end of a visible region in window IT->w. Set both to
1550 -1 to indicate no region. */
1551 if (highlight_region_p
1552 /* Maybe highlight only in selected window. */
1553 && (/* Either show region everywhere. */
1554 highlight_nonselected_windows
1555 /* Or show region in the selected window. */
1556 || w == XWINDOW (selected_window)
1557 /* Or show the region if we are in the mini-buffer and W is
1558 the window the mini-buffer refers to. */
1559 || (MINI_WINDOW_P (XWINDOW (selected_window))
1560 && WINDOWP (Vminibuf_scroll_window)
1561 && w == XWINDOW (Vminibuf_scroll_window))))
1562 {
1563 int charpos = marker_position (current_buffer->mark);
1564 it->region_beg_charpos = min (PT, charpos);
1565 it->region_end_charpos = max (PT, charpos);
1566 }
1567 else
1568 it->region_beg_charpos = it->region_end_charpos = -1;
1569
1570 /* Get the position at which the redisplay_end_trigger hook should
1571 be run, if it is to be run at all. */
1572 if (MARKERP (w->redisplay_end_trigger)
1573 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1574 it->redisplay_end_trigger_charpos
1575 = marker_position (w->redisplay_end_trigger);
1576 else if (INTEGERP (w->redisplay_end_trigger))
1577 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1578
1579 /* Correct bogus values of tab_width. */
1580 it->tab_width = XINT (current_buffer->tab_width);
1581 if (it->tab_width <= 0 || it->tab_width > 1000)
1582 it->tab_width = 8;
1583
1584 /* Are lines in the display truncated? */
1585 it->truncate_lines_p
1586 = (base_face_id != DEFAULT_FACE_ID
1587 || XINT (it->w->hscroll)
1588 || (truncate_partial_width_windows
1589 && !WINDOW_FULL_WIDTH_P (it->w))
1590 || !NILP (current_buffer->truncate_lines));
1591
1592 /* Get dimensions of truncation and continuation glyphs. These are
1593 displayed as bitmaps under X, so we don't need them for such
1594 frames. */
1595 if (!FRAME_WINDOW_P (it->f))
1596 {
1597 if (it->truncate_lines_p)
1598 {
1599 /* We will need the truncation glyph. */
1600 xassert (it->glyph_row == NULL);
1601 produce_special_glyphs (it, IT_TRUNCATION);
1602 it->truncation_pixel_width = it->pixel_width;
1603 }
1604 else
1605 {
1606 /* We will need the continuation glyph. */
1607 xassert (it->glyph_row == NULL);
1608 produce_special_glyphs (it, IT_CONTINUATION);
1609 it->continuation_pixel_width = it->pixel_width;
1610 }
1611
1612 /* Reset these values to zero becaue the produce_special_glyphs
1613 above has changed them. */
1614 it->pixel_width = it->ascent = it->descent = 0;
1615 it->phys_ascent = it->phys_descent = 0;
1616 }
1617
1618 /* Set this after getting the dimensions of truncation and
1619 continuation glyphs, so that we don't produce glyphs when calling
1620 produce_special_glyphs, above. */
1621 it->glyph_row = row;
1622 it->area = TEXT_AREA;
1623
1624 /* Get the dimensions of the display area. The display area
1625 consists of the visible window area plus a horizontally scrolled
1626 part to the left of the window. All x-values are relative to the
1627 start of this total display area. */
1628 if (base_face_id != DEFAULT_FACE_ID)
1629 {
1630 /* Mode lines, menu bar in terminal frames. */
1631 it->first_visible_x = 0;
1632 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1633 }
1634 else
1635 {
1636 it->first_visible_x
1637 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1638 it->last_visible_x = (it->first_visible_x
1639 + window_box_width (w, TEXT_AREA));
1640
1641 /* If we truncate lines, leave room for the truncator glyph(s) at
1642 the right margin. Otherwise, leave room for the continuation
1643 glyph(s). Truncation and continuation glyphs are not inserted
1644 for window-based redisplay. */
1645 if (!FRAME_WINDOW_P (it->f))
1646 {
1647 if (it->truncate_lines_p)
1648 it->last_visible_x -= it->truncation_pixel_width;
1649 else
1650 it->last_visible_x -= it->continuation_pixel_width;
1651 }
1652
1653 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1654 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1655 }
1656
1657 /* Leave room for a border glyph. */
1658 if (!FRAME_WINDOW_P (it->f)
1659 && !WINDOW_RIGHTMOST_P (it->w))
1660 it->last_visible_x -= 1;
1661
1662 it->last_visible_y = window_text_bottom_y (w);
1663
1664 /* For mode lines and alike, arrange for the first glyph having a
1665 left box line if the face specifies a box. */
1666 if (base_face_id != DEFAULT_FACE_ID)
1667 {
1668 struct face *face;
1669
1670 it->face_id = base_face_id;
1671
1672 /* If we have a boxed mode line, make the first character appear
1673 with a left box line. */
1674 face = FACE_FROM_ID (it->f, base_face_id);
1675 if (face->box != FACE_NO_BOX)
1676 it->start_of_box_run_p = 1;
1677 }
1678
1679 /* If a buffer position was specified, set the iterator there,
1680 getting overlays and face properties from that position. */
1681 if (charpos > 0)
1682 {
1683 it->end_charpos = ZV;
1684 it->face_id = -1;
1685 IT_CHARPOS (*it) = charpos;
1686
1687 /* Compute byte position if not specified. */
1688 if (bytepos <= 0)
1689 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1690 else
1691 IT_BYTEPOS (*it) = bytepos;
1692
1693 /* Compute faces etc. */
1694 reseat (it, it->current.pos, 1);
1695 }
1696
1697 CHECK_IT (it);
1698 }
1699
1700
1701 /* Initialize IT for the display of window W with window start POS. */
1702
1703 void
1704 start_display (it, w, pos)
1705 struct it *it;
1706 struct window *w;
1707 struct text_pos pos;
1708 {
1709 struct glyph_row *row;
1710 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1711
1712 row = w->desired_matrix->rows + first_vpos;
1713 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1714
1715 if (!it->truncate_lines_p)
1716 {
1717 int start_at_line_beg_p;
1718 int first_y = it->current_y;
1719
1720 /* If window start is not at a line start, skip forward to POS to
1721 get the correct continuation lines width. */
1722 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1723 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1724 if (!start_at_line_beg_p)
1725 {
1726 reseat_at_previous_visible_line_start (it);
1727 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1728
1729 /* If lines are continued, this line may end in the middle
1730 of a multi-glyph character (e.g. a control character
1731 displayed as \003, or in the middle of an overlay
1732 string). In this case move_it_to above will not have
1733 taken us to the start of the continuation line but to the
1734 end of the continued line. */
1735 if (it->current_x > 0)
1736 {
1737 if (it->current.dpvec_index >= 0
1738 || it->current.overlay_string_index >= 0)
1739 {
1740 set_iterator_to_next (it, 1);
1741 move_it_in_display_line_to (it, -1, -1, 0);
1742 }
1743
1744 it->continuation_lines_width += it->current_x;
1745 }
1746
1747 /* We're starting a new display line, not affected by the
1748 height of the continued line, so clear the appropriate
1749 fields in the iterator structure. */
1750 it->max_ascent = it->max_descent = 0;
1751 it->max_phys_ascent = it->max_phys_descent = 0;
1752
1753 it->current_y = first_y;
1754 it->vpos = 0;
1755 it->current_x = it->hpos = 0;
1756 }
1757 }
1758
1759 #if 0 /* Don't assert the following because start_display is sometimes
1760 called intentionally with a window start that is not at a
1761 line start. Please leave this code in as a comment. */
1762
1763 /* Window start should be on a line start, now. */
1764 xassert (it->continuation_lines_width
1765 || IT_CHARPOS (it) == BEGV
1766 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1767 #endif /* 0 */
1768 }
1769
1770
1771 /* Return 1 if POS is a position in ellipses displayed for invisible
1772 text. W is the window we display, for text property lookup. */
1773
1774 static int
1775 in_ellipses_for_invisible_text_p (pos, w)
1776 struct display_pos *pos;
1777 struct window *w;
1778 {
1779 Lisp_Object prop, window;
1780 int ellipses_p = 0;
1781 int charpos = CHARPOS (pos->pos);
1782
1783 /* If POS specifies a position in a display vector, this might
1784 be for an ellipsis displayed for invisible text. We won't
1785 get the iterator set up for delivering that ellipsis unless
1786 we make sure that it gets aware of the invisible text. */
1787 if (pos->dpvec_index >= 0
1788 && pos->overlay_string_index < 0
1789 && CHARPOS (pos->string_pos) < 0
1790 && charpos > BEGV
1791 && (XSETWINDOW (window, w),
1792 prop = Fget_char_property (make_number (charpos),
1793 Qinvisible, window),
1794 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1795 {
1796 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1797 window);
1798 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1799 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
1800 ellipses_p = 1;
1801 }
1802
1803 return ellipses_p;
1804 }
1805
1806
1807 /* Initialize IT for stepping through current_buffer in window W,
1808 starting at position POS that includes overlay string and display
1809 vector/ control character translation position information. Value
1810 is zero if there are overlay strings with newlines at POS. */
1811
1812 static int
1813 init_from_display_pos (it, w, pos)
1814 struct it *it;
1815 struct window *w;
1816 struct display_pos *pos;
1817 {
1818 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1819 int i, overlay_strings_with_newlines = 0;
1820
1821 /* If POS specifies a position in a display vector, this might
1822 be for an ellipsis displayed for invisible text. We won't
1823 get the iterator set up for delivering that ellipsis unless
1824 we make sure that it gets aware of the invisible text. */
1825 if (in_ellipses_for_invisible_text_p (pos, w))
1826 {
1827 --charpos;
1828 bytepos = 0;
1829 }
1830
1831 /* Keep in mind: the call to reseat in init_iterator skips invisible
1832 text, so we might end up at a position different from POS. This
1833 is only a problem when POS is a row start after a newline and an
1834 overlay starts there with an after-string, and the overlay has an
1835 invisible property. Since we don't skip invisible text in
1836 display_line and elsewhere immediately after consuming the
1837 newline before the row start, such a POS will not be in a string,
1838 but the call to init_iterator below will move us to the
1839 after-string. */
1840 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1841
1842 for (i = 0; i < it->n_overlay_strings; ++i)
1843 {
1844 char *s = XSTRING (it->overlay_strings[i])->data;
1845 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1846
1847 while (s < e && *s != '\n')
1848 ++s;
1849
1850 if (s < e)
1851 {
1852 overlay_strings_with_newlines = 1;
1853 break;
1854 }
1855 }
1856
1857 /* If position is within an overlay string, set up IT to the right
1858 overlay string. */
1859 if (pos->overlay_string_index >= 0)
1860 {
1861 int relative_index;
1862
1863 /* If the first overlay string happens to have a `display'
1864 property for an image, the iterator will be set up for that
1865 image, and we have to undo that setup first before we can
1866 correct the overlay string index. */
1867 if (it->method == next_element_from_image)
1868 pop_it (it);
1869
1870 /* We already have the first chunk of overlay strings in
1871 IT->overlay_strings. Load more until the one for
1872 pos->overlay_string_index is in IT->overlay_strings. */
1873 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1874 {
1875 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1876 it->current.overlay_string_index = 0;
1877 while (n--)
1878 {
1879 load_overlay_strings (it, 0);
1880 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1881 }
1882 }
1883
1884 it->current.overlay_string_index = pos->overlay_string_index;
1885 relative_index = (it->current.overlay_string_index
1886 % OVERLAY_STRING_CHUNK_SIZE);
1887 it->string = it->overlay_strings[relative_index];
1888 xassert (STRINGP (it->string));
1889 it->current.string_pos = pos->string_pos;
1890 it->method = next_element_from_string;
1891 }
1892
1893 #if 0 /* This is bogus because POS not having an overlay string
1894 position does not mean it's after the string. Example: A
1895 line starting with a before-string and initialization of IT
1896 to the previous row's end position. */
1897 else if (it->current.overlay_string_index >= 0)
1898 {
1899 /* If POS says we're already after an overlay string ending at
1900 POS, make sure to pop the iterator because it will be in
1901 front of that overlay string. When POS is ZV, we've thereby
1902 also ``processed'' overlay strings at ZV. */
1903 while (it->sp)
1904 pop_it (it);
1905 it->current.overlay_string_index = -1;
1906 it->method = next_element_from_buffer;
1907 if (CHARPOS (pos->pos) == ZV)
1908 it->overlay_strings_at_end_processed_p = 1;
1909 }
1910 #endif /* 0 */
1911
1912 if (CHARPOS (pos->string_pos) >= 0)
1913 {
1914 /* Recorded position is not in an overlay string, but in another
1915 string. This can only be a string from a `display' property.
1916 IT should already be filled with that string. */
1917 it->current.string_pos = pos->string_pos;
1918 xassert (STRINGP (it->string));
1919 }
1920
1921 /* Restore position in display vector translations, control
1922 character translations or ellipses. */
1923 if (pos->dpvec_index >= 0)
1924 {
1925 if (it->dpvec == NULL)
1926 get_next_display_element (it);
1927 xassert (it->dpvec && it->current.dpvec_index == 0);
1928 it->current.dpvec_index = pos->dpvec_index;
1929 }
1930
1931 CHECK_IT (it);
1932 return !overlay_strings_with_newlines;
1933 }
1934
1935
1936 /* Initialize IT for stepping through current_buffer in window W
1937 starting at ROW->start. */
1938
1939 static void
1940 init_to_row_start (it, w, row)
1941 struct it *it;
1942 struct window *w;
1943 struct glyph_row *row;
1944 {
1945 init_from_display_pos (it, w, &row->start);
1946 it->continuation_lines_width = row->continuation_lines_width;
1947 CHECK_IT (it);
1948 }
1949
1950
1951 /* Initialize IT for stepping through current_buffer in window W
1952 starting in the line following ROW, i.e. starting at ROW->end.
1953 Value is zero if there are overlay strings with newlines at ROW's
1954 end position. */
1955
1956 static int
1957 init_to_row_end (it, w, row)
1958 struct it *it;
1959 struct window *w;
1960 struct glyph_row *row;
1961 {
1962 int success = 0;
1963
1964 if (init_from_display_pos (it, w, &row->end))
1965 {
1966 if (row->continued_p)
1967 it->continuation_lines_width
1968 = row->continuation_lines_width + row->pixel_width;
1969 CHECK_IT (it);
1970 success = 1;
1971 }
1972
1973 return success;
1974 }
1975
1976
1977
1978 \f
1979 /***********************************************************************
1980 Text properties
1981 ***********************************************************************/
1982
1983 /* Called when IT reaches IT->stop_charpos. Handle text property and
1984 overlay changes. Set IT->stop_charpos to the next position where
1985 to stop. */
1986
1987 static void
1988 handle_stop (it)
1989 struct it *it;
1990 {
1991 enum prop_handled handled;
1992 int handle_overlay_change_p = 1;
1993 struct props *p;
1994
1995 it->dpvec = NULL;
1996 it->current.dpvec_index = -1;
1997
1998 do
1999 {
2000 handled = HANDLED_NORMALLY;
2001
2002 /* Call text property handlers. */
2003 for (p = it_props; p->handler; ++p)
2004 {
2005 handled = p->handler (it);
2006
2007 if (handled == HANDLED_RECOMPUTE_PROPS)
2008 break;
2009 else if (handled == HANDLED_RETURN)
2010 return;
2011 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2012 handle_overlay_change_p = 0;
2013 }
2014
2015 if (handled != HANDLED_RECOMPUTE_PROPS)
2016 {
2017 /* Don't check for overlay strings below when set to deliver
2018 characters from a display vector. */
2019 if (it->method == next_element_from_display_vector)
2020 handle_overlay_change_p = 0;
2021
2022 /* Handle overlay changes. */
2023 if (handle_overlay_change_p)
2024 handled = handle_overlay_change (it);
2025
2026 /* Determine where to stop next. */
2027 if (handled == HANDLED_NORMALLY)
2028 compute_stop_pos (it);
2029 }
2030 }
2031 while (handled == HANDLED_RECOMPUTE_PROPS);
2032 }
2033
2034
2035 /* Compute IT->stop_charpos from text property and overlay change
2036 information for IT's current position. */
2037
2038 static void
2039 compute_stop_pos (it)
2040 struct it *it;
2041 {
2042 register INTERVAL iv, next_iv;
2043 Lisp_Object object, limit, position;
2044
2045 /* If nowhere else, stop at the end. */
2046 it->stop_charpos = it->end_charpos;
2047
2048 if (STRINGP (it->string))
2049 {
2050 /* Strings are usually short, so don't limit the search for
2051 properties. */
2052 object = it->string;
2053 limit = Qnil;
2054 position = make_number (IT_STRING_CHARPOS (*it));
2055 }
2056 else
2057 {
2058 int charpos;
2059
2060 /* If next overlay change is in front of the current stop pos
2061 (which is IT->end_charpos), stop there. Note: value of
2062 next_overlay_change is point-max if no overlay change
2063 follows. */
2064 charpos = next_overlay_change (IT_CHARPOS (*it));
2065 if (charpos < it->stop_charpos)
2066 it->stop_charpos = charpos;
2067
2068 /* If showing the region, we have to stop at the region
2069 start or end because the face might change there. */
2070 if (it->region_beg_charpos > 0)
2071 {
2072 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2073 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2074 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2075 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2076 }
2077
2078 /* Set up variables for computing the stop position from text
2079 property changes. */
2080 XSETBUFFER (object, current_buffer);
2081 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2082 position = make_number (IT_CHARPOS (*it));
2083
2084 }
2085
2086 /* Get the interval containing IT's position. Value is a null
2087 interval if there isn't such an interval. */
2088 iv = validate_interval_range (object, &position, &position, 0);
2089 if (!NULL_INTERVAL_P (iv))
2090 {
2091 Lisp_Object values_here[LAST_PROP_IDX];
2092 struct props *p;
2093
2094 /* Get properties here. */
2095 for (p = it_props; p->handler; ++p)
2096 values_here[p->idx] = textget (iv->plist, *p->name);
2097
2098 /* Look for an interval following iv that has different
2099 properties. */
2100 for (next_iv = next_interval (iv);
2101 (!NULL_INTERVAL_P (next_iv)
2102 && (NILP (limit)
2103 || XFASTINT (limit) > next_iv->position));
2104 next_iv = next_interval (next_iv))
2105 {
2106 for (p = it_props; p->handler; ++p)
2107 {
2108 Lisp_Object new_value;
2109
2110 new_value = textget (next_iv->plist, *p->name);
2111 if (!EQ (values_here[p->idx], new_value))
2112 break;
2113 }
2114
2115 if (p->handler)
2116 break;
2117 }
2118
2119 if (!NULL_INTERVAL_P (next_iv))
2120 {
2121 if (INTEGERP (limit)
2122 && next_iv->position >= XFASTINT (limit))
2123 /* No text property change up to limit. */
2124 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2125 else
2126 /* Text properties change in next_iv. */
2127 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2128 }
2129 }
2130
2131 xassert (STRINGP (it->string)
2132 || (it->stop_charpos >= BEGV
2133 && it->stop_charpos >= IT_CHARPOS (*it)));
2134 }
2135
2136
2137 /* Return the position of the next overlay change after POS in
2138 current_buffer. Value is point-max if no overlay change
2139 follows. This is like `next-overlay-change' but doesn't use
2140 xmalloc. */
2141
2142 static int
2143 next_overlay_change (pos)
2144 int pos;
2145 {
2146 int noverlays;
2147 int endpos;
2148 Lisp_Object *overlays;
2149 int len;
2150 int i;
2151
2152 /* Get all overlays at the given position. */
2153 len = 10;
2154 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2155 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2156 if (noverlays > len)
2157 {
2158 len = noverlays;
2159 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2160 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2161 }
2162
2163 /* If any of these overlays ends before endpos,
2164 use its ending point instead. */
2165 for (i = 0; i < noverlays; ++i)
2166 {
2167 Lisp_Object oend;
2168 int oendpos;
2169
2170 oend = OVERLAY_END (overlays[i]);
2171 oendpos = OVERLAY_POSITION (oend);
2172 endpos = min (endpos, oendpos);
2173 }
2174
2175 return endpos;
2176 }
2177
2178
2179 \f
2180 /***********************************************************************
2181 Fontification
2182 ***********************************************************************/
2183
2184 /* Handle changes in the `fontified' property of the current buffer by
2185 calling hook functions from Qfontification_functions to fontify
2186 regions of text. */
2187
2188 static enum prop_handled
2189 handle_fontified_prop (it)
2190 struct it *it;
2191 {
2192 Lisp_Object prop, pos;
2193 enum prop_handled handled = HANDLED_NORMALLY;
2194
2195 /* Get the value of the `fontified' property at IT's current buffer
2196 position. (The `fontified' property doesn't have a special
2197 meaning in strings.) If the value is nil, call functions from
2198 Qfontification_functions. */
2199 if (!STRINGP (it->string)
2200 && it->s == NULL
2201 && !NILP (Vfontification_functions)
2202 && !NILP (Vrun_hooks)
2203 && (pos = make_number (IT_CHARPOS (*it)),
2204 prop = Fget_char_property (pos, Qfontified, Qnil),
2205 NILP (prop)))
2206 {
2207 int count = BINDING_STACK_SIZE ();
2208 Lisp_Object val;
2209
2210 val = Vfontification_functions;
2211 specbind (Qfontification_functions, Qnil);
2212 specbind (Qafter_change_functions, Qnil);
2213
2214 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2215 safe_call1 (val, pos);
2216 else
2217 {
2218 Lisp_Object globals, fn;
2219 struct gcpro gcpro1, gcpro2;
2220
2221 globals = Qnil;
2222 GCPRO2 (val, globals);
2223
2224 for (; CONSP (val); val = XCDR (val))
2225 {
2226 fn = XCAR (val);
2227
2228 if (EQ (fn, Qt))
2229 {
2230 /* A value of t indicates this hook has a local
2231 binding; it means to run the global binding too.
2232 In a global value, t should not occur. If it
2233 does, we must ignore it to avoid an endless
2234 loop. */
2235 for (globals = Fdefault_value (Qfontification_functions);
2236 CONSP (globals);
2237 globals = XCDR (globals))
2238 {
2239 fn = XCAR (globals);
2240 if (!EQ (fn, Qt))
2241 safe_call1 (fn, pos);
2242 }
2243 }
2244 else
2245 safe_call1 (fn, pos);
2246 }
2247
2248 UNGCPRO;
2249 }
2250
2251 unbind_to (count, Qnil);
2252
2253 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2254 something. This avoids an endless loop if they failed to
2255 fontify the text for which reason ever. */
2256 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2257 handled = HANDLED_RECOMPUTE_PROPS;
2258 }
2259
2260 return handled;
2261 }
2262
2263
2264 \f
2265 /***********************************************************************
2266 Faces
2267 ***********************************************************************/
2268
2269 /* Set up iterator IT from face properties at its current position.
2270 Called from handle_stop. */
2271
2272 static enum prop_handled
2273 handle_face_prop (it)
2274 struct it *it;
2275 {
2276 int new_face_id, next_stop;
2277
2278 if (!STRINGP (it->string))
2279 {
2280 new_face_id
2281 = face_at_buffer_position (it->w,
2282 IT_CHARPOS (*it),
2283 it->region_beg_charpos,
2284 it->region_end_charpos,
2285 &next_stop,
2286 (IT_CHARPOS (*it)
2287 + TEXT_PROP_DISTANCE_LIMIT),
2288 0);
2289
2290 /* Is this a start of a run of characters with box face?
2291 Caveat: this can be called for a freshly initialized
2292 iterator; face_id is -1 is this case. We know that the new
2293 face will not change until limit, i.e. if the new face has a
2294 box, all characters up to limit will have one. But, as
2295 usual, we don't know whether limit is really the end. */
2296 if (new_face_id != it->face_id)
2297 {
2298 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2299
2300 /* If new face has a box but old face has not, this is
2301 the start of a run of characters with box, i.e. it has
2302 a shadow on the left side. The value of face_id of the
2303 iterator will be -1 if this is the initial call that gets
2304 the face. In this case, we have to look in front of IT's
2305 position and see whether there is a face != new_face_id. */
2306 it->start_of_box_run_p
2307 = (new_face->box != FACE_NO_BOX
2308 && (it->face_id >= 0
2309 || IT_CHARPOS (*it) == BEG
2310 || new_face_id != face_before_it_pos (it)));
2311 it->face_box_p = new_face->box != FACE_NO_BOX;
2312 }
2313 }
2314 else
2315 {
2316 int base_face_id, bufpos;
2317
2318 if (it->current.overlay_string_index >= 0)
2319 bufpos = IT_CHARPOS (*it);
2320 else
2321 bufpos = 0;
2322
2323 /* For strings from a buffer, i.e. overlay strings or strings
2324 from a `display' property, use the face at IT's current
2325 buffer position as the base face to merge with, so that
2326 overlay strings appear in the same face as surrounding
2327 text, unless they specify their own faces. */
2328 base_face_id = underlying_face_id (it);
2329
2330 new_face_id = face_at_string_position (it->w,
2331 it->string,
2332 IT_STRING_CHARPOS (*it),
2333 bufpos,
2334 it->region_beg_charpos,
2335 it->region_end_charpos,
2336 &next_stop,
2337 base_face_id, 0);
2338
2339 #if 0 /* This shouldn't be neccessary. Let's check it. */
2340 /* If IT is used to display a mode line we would really like to
2341 use the mode line face instead of the frame's default face. */
2342 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2343 && new_face_id == DEFAULT_FACE_ID)
2344 new_face_id = MODE_LINE_FACE_ID;
2345 #endif
2346
2347 /* Is this a start of a run of characters with box? Caveat:
2348 this can be called for a freshly allocated iterator; face_id
2349 is -1 is this case. We know that the new face will not
2350 change until the next check pos, i.e. if the new face has a
2351 box, all characters up to that position will have a
2352 box. But, as usual, we don't know whether that position
2353 is really the end. */
2354 if (new_face_id != it->face_id)
2355 {
2356 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2357 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2358
2359 /* If new face has a box but old face hasn't, this is the
2360 start of a run of characters with box, i.e. it has a
2361 shadow on the left side. */
2362 it->start_of_box_run_p
2363 = new_face->box && (old_face == NULL || !old_face->box);
2364 it->face_box_p = new_face->box != FACE_NO_BOX;
2365 }
2366 }
2367
2368 it->face_id = new_face_id;
2369 return HANDLED_NORMALLY;
2370 }
2371
2372
2373 /* Return the ID of the face ``underlying'' IT's current position,
2374 which is in a string. If the iterator is associated with a
2375 buffer, return the face at IT's current buffer position.
2376 Otherwise, use the iterator's base_face_id. */
2377
2378 static int
2379 underlying_face_id (it)
2380 struct it *it;
2381 {
2382 int face_id = it->base_face_id, i;
2383
2384 xassert (STRINGP (it->string));
2385
2386 for (i = it->sp - 1; i >= 0; --i)
2387 if (NILP (it->stack[i].string))
2388 face_id = it->stack[i].face_id;
2389
2390 return face_id;
2391 }
2392
2393
2394 /* Compute the face one character before or after the current position
2395 of IT. BEFORE_P non-zero means get the face in front of IT's
2396 position. Value is the id of the face. */
2397
2398 static int
2399 face_before_or_after_it_pos (it, before_p)
2400 struct it *it;
2401 int before_p;
2402 {
2403 int face_id, limit;
2404 int next_check_charpos;
2405 struct text_pos pos;
2406
2407 xassert (it->s == NULL);
2408
2409 if (STRINGP (it->string))
2410 {
2411 int bufpos, base_face_id;
2412
2413 /* No face change past the end of the string (for the case
2414 we are padding with spaces). No face change before the
2415 string start. */
2416 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2417 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2418 return it->face_id;
2419
2420 /* Set pos to the position before or after IT's current position. */
2421 if (before_p)
2422 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2423 else
2424 /* For composition, we must check the character after the
2425 composition. */
2426 pos = (it->what == IT_COMPOSITION
2427 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2428 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2429
2430 if (it->current.overlay_string_index >= 0)
2431 bufpos = IT_CHARPOS (*it);
2432 else
2433 bufpos = 0;
2434
2435 base_face_id = underlying_face_id (it);
2436
2437 /* Get the face for ASCII, or unibyte. */
2438 face_id = face_at_string_position (it->w,
2439 it->string,
2440 CHARPOS (pos),
2441 bufpos,
2442 it->region_beg_charpos,
2443 it->region_end_charpos,
2444 &next_check_charpos,
2445 base_face_id, 0);
2446
2447 /* Correct the face for charsets different from ASCII. Do it
2448 for the multibyte case only. The face returned above is
2449 suitable for unibyte text if IT->string is unibyte. */
2450 if (STRING_MULTIBYTE (it->string))
2451 {
2452 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2453 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2454 int c, len;
2455 struct face *face = FACE_FROM_ID (it->f, face_id);
2456
2457 c = string_char_and_length (p, rest, &len);
2458 face_id = FACE_FOR_CHAR (it->f, face, c);
2459 }
2460 }
2461 else
2462 {
2463 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2464 || (IT_CHARPOS (*it) <= BEGV && before_p))
2465 return it->face_id;
2466
2467 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2468 pos = it->current.pos;
2469
2470 if (before_p)
2471 DEC_TEXT_POS (pos, it->multibyte_p);
2472 else
2473 {
2474 if (it->what == IT_COMPOSITION)
2475 /* For composition, we must check the position after the
2476 composition. */
2477 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2478 else
2479 INC_TEXT_POS (pos, it->multibyte_p);
2480 }
2481
2482 /* Determine face for CHARSET_ASCII, or unibyte. */
2483 face_id = face_at_buffer_position (it->w,
2484 CHARPOS (pos),
2485 it->region_beg_charpos,
2486 it->region_end_charpos,
2487 &next_check_charpos,
2488 limit, 0);
2489
2490 /* Correct the face for charsets different from ASCII. Do it
2491 for the multibyte case only. The face returned above is
2492 suitable for unibyte text if current_buffer is unibyte. */
2493 if (it->multibyte_p)
2494 {
2495 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2496 struct face *face = FACE_FROM_ID (it->f, face_id);
2497 face_id = FACE_FOR_CHAR (it->f, face, c);
2498 }
2499 }
2500
2501 return face_id;
2502 }
2503
2504
2505 \f
2506 /***********************************************************************
2507 Invisible text
2508 ***********************************************************************/
2509
2510 /* Set up iterator IT from invisible properties at its current
2511 position. Called from handle_stop. */
2512
2513 static enum prop_handled
2514 handle_invisible_prop (it)
2515 struct it *it;
2516 {
2517 enum prop_handled handled = HANDLED_NORMALLY;
2518
2519 if (STRINGP (it->string))
2520 {
2521 extern Lisp_Object Qinvisible;
2522 Lisp_Object prop, end_charpos, limit, charpos;
2523
2524 /* Get the value of the invisible text property at the
2525 current position. Value will be nil if there is no such
2526 property. */
2527 charpos = make_number (IT_STRING_CHARPOS (*it));
2528 prop = Fget_text_property (charpos, Qinvisible, it->string);
2529
2530 if (!NILP (prop)
2531 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2532 {
2533 handled = HANDLED_RECOMPUTE_PROPS;
2534
2535 /* Get the position at which the next change of the
2536 invisible text property can be found in IT->string.
2537 Value will be nil if the property value is the same for
2538 all the rest of IT->string. */
2539 XSETINT (limit, XSTRING (it->string)->size);
2540 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2541 it->string, limit);
2542
2543 /* Text at current position is invisible. The next
2544 change in the property is at position end_charpos.
2545 Move IT's current position to that position. */
2546 if (INTEGERP (end_charpos)
2547 && XFASTINT (end_charpos) < XFASTINT (limit))
2548 {
2549 struct text_pos old;
2550 old = it->current.string_pos;
2551 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2552 compute_string_pos (&it->current.string_pos, old, it->string);
2553 }
2554 else
2555 {
2556 /* The rest of the string is invisible. If this is an
2557 overlay string, proceed with the next overlay string
2558 or whatever comes and return a character from there. */
2559 if (it->current.overlay_string_index >= 0)
2560 {
2561 next_overlay_string (it);
2562 /* Don't check for overlay strings when we just
2563 finished processing them. */
2564 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2565 }
2566 else
2567 {
2568 struct Lisp_String *s = XSTRING (it->string);
2569 IT_STRING_CHARPOS (*it) = s->size;
2570 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2571 }
2572 }
2573 }
2574 }
2575 else
2576 {
2577 int visible_p, newpos, next_stop, start_charpos;
2578 Lisp_Object pos, prop, overlay;
2579
2580 /* First of all, is there invisible text at this position? */
2581 start_charpos = IT_CHARPOS (*it);
2582 pos = make_number (IT_CHARPOS (*it));
2583 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2584 &overlay);
2585
2586 /* If we are on invisible text, skip over it. */
2587 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2588 && IT_CHARPOS (*it) < it->end_charpos)
2589 {
2590 /* Record whether we have to display an ellipsis for the
2591 invisible text. */
2592 int display_ellipsis_p
2593 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2594
2595 handled = HANDLED_RECOMPUTE_PROPS;
2596
2597 /* Loop skipping over invisible text. The loop is left at
2598 ZV or with IT on the first char being visible again. */
2599 do
2600 {
2601 /* Try to skip some invisible text. Return value is the
2602 position reached which can be equal to IT's position
2603 if there is nothing invisible here. This skips both
2604 over invisible text properties and overlays with
2605 invisible property. */
2606 newpos = skip_invisible (IT_CHARPOS (*it),
2607 &next_stop, ZV, it->window);
2608
2609 /* If we skipped nothing at all we weren't at invisible
2610 text in the first place. If everything to the end of
2611 the buffer was skipped, end the loop. */
2612 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2613 visible_p = 1;
2614 else
2615 {
2616 /* We skipped some characters but not necessarily
2617 all there are. Check if we ended up on visible
2618 text. Fget_char_property returns the property of
2619 the char before the given position, i.e. if we
2620 get visible_p = 1, this means that the char at
2621 newpos is visible. */
2622 pos = make_number (newpos);
2623 prop = Fget_char_property (pos, Qinvisible, it->window);
2624 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2625 }
2626
2627 /* If we ended up on invisible text, proceed to
2628 skip starting with next_stop. */
2629 if (!visible_p)
2630 IT_CHARPOS (*it) = next_stop;
2631 }
2632 while (!visible_p);
2633
2634 /* The position newpos is now either ZV or on visible text. */
2635 IT_CHARPOS (*it) = newpos;
2636 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2637
2638 /* If there are before-strings at the start of invisible
2639 text, and the text is invisible because of a text
2640 property, arrange to show before-strings because 20.x did
2641 it that way. (If the text is invisible because of an
2642 overlay property instead of a text property, this is
2643 already handled in the overlay code.) */
2644 if (NILP (overlay)
2645 && get_overlay_strings (it, start_charpos))
2646 {
2647 handled = HANDLED_RECOMPUTE_PROPS;
2648 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2649 }
2650 else if (display_ellipsis_p)
2651 setup_for_ellipsis (it);
2652 }
2653 }
2654
2655 return handled;
2656 }
2657
2658
2659 /* Make iterator IT return `...' next. */
2660
2661 static void
2662 setup_for_ellipsis (it)
2663 struct it *it;
2664 {
2665 if (it->dp
2666 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2667 {
2668 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2669 it->dpvec = v->contents;
2670 it->dpend = v->contents + v->size;
2671 }
2672 else
2673 {
2674 /* Default `...'. */
2675 it->dpvec = default_invis_vector;
2676 it->dpend = default_invis_vector + 3;
2677 }
2678
2679 /* The ellipsis display does not replace the display of the
2680 character at the new position. Indicate this by setting
2681 IT->dpvec_char_len to zero. */
2682 it->dpvec_char_len = 0;
2683
2684 it->current.dpvec_index = 0;
2685 it->method = next_element_from_display_vector;
2686 }
2687
2688
2689 \f
2690 /***********************************************************************
2691 'display' property
2692 ***********************************************************************/
2693
2694 /* Set up iterator IT from `display' property at its current position.
2695 Called from handle_stop. */
2696
2697 static enum prop_handled
2698 handle_display_prop (it)
2699 struct it *it;
2700 {
2701 Lisp_Object prop, object;
2702 struct text_pos *position;
2703 int display_replaced_p = 0;
2704
2705 if (STRINGP (it->string))
2706 {
2707 object = it->string;
2708 position = &it->current.string_pos;
2709 }
2710 else
2711 {
2712 object = it->w->buffer;
2713 position = &it->current.pos;
2714 }
2715
2716 /* Reset those iterator values set from display property values. */
2717 it->font_height = Qnil;
2718 it->space_width = Qnil;
2719 it->voffset = 0;
2720
2721 /* We don't support recursive `display' properties, i.e. string
2722 values that have a string `display' property, that have a string
2723 `display' property etc. */
2724 if (!it->string_from_display_prop_p)
2725 it->area = TEXT_AREA;
2726
2727 prop = Fget_char_property (make_number (position->charpos),
2728 Qdisplay, object);
2729 if (NILP (prop))
2730 return HANDLED_NORMALLY;
2731
2732 if (CONSP (prop)
2733 /* Simple properties. */
2734 && !EQ (XCAR (prop), Qimage)
2735 && !EQ (XCAR (prop), Qspace)
2736 && !EQ (XCAR (prop), Qwhen)
2737 && !EQ (XCAR (prop), Qspace_width)
2738 && !EQ (XCAR (prop), Qheight)
2739 && !EQ (XCAR (prop), Qraise)
2740 /* Marginal area specifications. */
2741 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2742 && !NILP (XCAR (prop)))
2743 {
2744 for (; CONSP (prop); prop = XCDR (prop))
2745 {
2746 if (handle_single_display_prop (it, XCAR (prop), object,
2747 position, display_replaced_p))
2748 display_replaced_p = 1;
2749 }
2750 }
2751 else if (VECTORP (prop))
2752 {
2753 int i;
2754 for (i = 0; i < ASIZE (prop); ++i)
2755 if (handle_single_display_prop (it, AREF (prop, i), object,
2756 position, display_replaced_p))
2757 display_replaced_p = 1;
2758 }
2759 else
2760 {
2761 if (handle_single_display_prop (it, prop, object, position, 0))
2762 display_replaced_p = 1;
2763 }
2764
2765 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2766 }
2767
2768
2769 /* Value is the position of the end of the `display' property starting
2770 at START_POS in OBJECT. */
2771
2772 static struct text_pos
2773 display_prop_end (it, object, start_pos)
2774 struct it *it;
2775 Lisp_Object object;
2776 struct text_pos start_pos;
2777 {
2778 Lisp_Object end;
2779 struct text_pos end_pos;
2780
2781 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2782 Qdisplay, object, Qnil);
2783 CHARPOS (end_pos) = XFASTINT (end);
2784 if (STRINGP (object))
2785 compute_string_pos (&end_pos, start_pos, it->string);
2786 else
2787 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2788
2789 return end_pos;
2790 }
2791
2792
2793 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2794 is the object in which the `display' property was found. *POSITION
2795 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2796 means that we previously saw a display sub-property which already
2797 replaced text display with something else, for example an image;
2798 ignore such properties after the first one has been processed.
2799
2800 If PROP is a `space' or `image' sub-property, set *POSITION to the
2801 end position of the `display' property.
2802
2803 Value is non-zero something was found which replaces the display
2804 of buffer or string text. */
2805
2806 static int
2807 handle_single_display_prop (it, prop, object, position,
2808 display_replaced_before_p)
2809 struct it *it;
2810 Lisp_Object prop;
2811 Lisp_Object object;
2812 struct text_pos *position;
2813 int display_replaced_before_p;
2814 {
2815 Lisp_Object value;
2816 int replaces_text_display_p = 0;
2817 Lisp_Object form;
2818
2819 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2820 evaluated. If the result is nil, VALUE is ignored. */
2821 form = Qt;
2822 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2823 {
2824 prop = XCDR (prop);
2825 if (!CONSP (prop))
2826 return 0;
2827 form = XCAR (prop);
2828 prop = XCDR (prop);
2829 }
2830
2831 if (!NILP (form) && !EQ (form, Qt))
2832 {
2833 int count = BINDING_STACK_SIZE ();
2834 struct gcpro gcpro1;
2835
2836 /* Bind `object' to the object having the `display' property, a
2837 buffer or string. Bind `position' to the position in the
2838 object where the property was found, and `buffer-position'
2839 to the current position in the buffer. */
2840 specbind (Qobject, object);
2841 specbind (Qposition, make_number (CHARPOS (*position)));
2842 specbind (Qbuffer_position,
2843 make_number (STRINGP (object)
2844 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2845 GCPRO1 (form);
2846 form = safe_eval (form);
2847 UNGCPRO;
2848 unbind_to (count, Qnil);
2849 }
2850
2851 if (NILP (form))
2852 return 0;
2853
2854 if (CONSP (prop)
2855 && EQ (XCAR (prop), Qheight)
2856 && CONSP (XCDR (prop)))
2857 {
2858 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2859 return 0;
2860
2861 /* `(height HEIGHT)'. */
2862 it->font_height = XCAR (XCDR (prop));
2863 if (!NILP (it->font_height))
2864 {
2865 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2866 int new_height = -1;
2867
2868 if (CONSP (it->font_height)
2869 && (EQ (XCAR (it->font_height), Qplus)
2870 || EQ (XCAR (it->font_height), Qminus))
2871 && CONSP (XCDR (it->font_height))
2872 && INTEGERP (XCAR (XCDR (it->font_height))))
2873 {
2874 /* `(+ N)' or `(- N)' where N is an integer. */
2875 int steps = XINT (XCAR (XCDR (it->font_height)));
2876 if (EQ (XCAR (it->font_height), Qplus))
2877 steps = - steps;
2878 it->face_id = smaller_face (it->f, it->face_id, steps);
2879 }
2880 else if (FUNCTIONP (it->font_height))
2881 {
2882 /* Call function with current height as argument.
2883 Value is the new height. */
2884 Lisp_Object height;
2885 height = safe_call1 (it->font_height,
2886 face->lface[LFACE_HEIGHT_INDEX]);
2887 if (NUMBERP (height))
2888 new_height = XFLOATINT (height);
2889 }
2890 else if (NUMBERP (it->font_height))
2891 {
2892 /* Value is a multiple of the canonical char height. */
2893 struct face *face;
2894
2895 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2896 new_height = (XFLOATINT (it->font_height)
2897 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2898 }
2899 else
2900 {
2901 /* Evaluate IT->font_height with `height' bound to the
2902 current specified height to get the new height. */
2903 Lisp_Object value;
2904 int count = BINDING_STACK_SIZE ();
2905
2906 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2907 value = safe_eval (it->font_height);
2908 unbind_to (count, Qnil);
2909
2910 if (NUMBERP (value))
2911 new_height = XFLOATINT (value);
2912 }
2913
2914 if (new_height > 0)
2915 it->face_id = face_with_height (it->f, it->face_id, new_height);
2916 }
2917 }
2918 else if (CONSP (prop)
2919 && EQ (XCAR (prop), Qspace_width)
2920 && CONSP (XCDR (prop)))
2921 {
2922 /* `(space_width WIDTH)'. */
2923 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2924 return 0;
2925
2926 value = XCAR (XCDR (prop));
2927 if (NUMBERP (value) && XFLOATINT (value) > 0)
2928 it->space_width = value;
2929 }
2930 else if (CONSP (prop)
2931 && EQ (XCAR (prop), Qraise)
2932 && CONSP (XCDR (prop)))
2933 {
2934 /* `(raise FACTOR)'. */
2935 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2936 return 0;
2937
2938 #ifdef HAVE_WINDOW_SYSTEM
2939 value = XCAR (XCDR (prop));
2940 if (NUMBERP (value))
2941 {
2942 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2943 it->voffset = - (XFLOATINT (value)
2944 * (FONT_HEIGHT (face->font)));
2945 }
2946 #endif /* HAVE_WINDOW_SYSTEM */
2947 }
2948 else if (!it->string_from_display_prop_p)
2949 {
2950 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2951 VALUE) or `((margin nil) VALUE)' or VALUE. */
2952 Lisp_Object location, value;
2953 struct text_pos start_pos;
2954 int valid_p;
2955
2956 /* Characters having this form of property are not displayed, so
2957 we have to find the end of the property. */
2958 start_pos = *position;
2959 *position = display_prop_end (it, object, start_pos);
2960 value = Qnil;
2961
2962 /* Let's stop at the new position and assume that all
2963 text properties change there. */
2964 it->stop_charpos = position->charpos;
2965
2966 location = Qunbound;
2967 if (CONSP (prop) && CONSP (XCAR (prop)))
2968 {
2969 Lisp_Object tem;
2970
2971 value = XCDR (prop);
2972 if (CONSP (value))
2973 value = XCAR (value);
2974
2975 tem = XCAR (prop);
2976 if (EQ (XCAR (tem), Qmargin)
2977 && (tem = XCDR (tem),
2978 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2979 (NILP (tem)
2980 || EQ (tem, Qleft_margin)
2981 || EQ (tem, Qright_margin))))
2982 location = tem;
2983 }
2984
2985 if (EQ (location, Qunbound))
2986 {
2987 location = Qnil;
2988 value = prop;
2989 }
2990
2991 #ifdef HAVE_WINDOW_SYSTEM
2992 if (FRAME_TERMCAP_P (it->f))
2993 valid_p = STRINGP (value);
2994 else
2995 valid_p = (STRINGP (value)
2996 || (CONSP (value) && EQ (XCAR (value), Qspace))
2997 || valid_image_p (value));
2998 #else /* not HAVE_WINDOW_SYSTEM */
2999 valid_p = STRINGP (value);
3000 #endif /* not HAVE_WINDOW_SYSTEM */
3001
3002 if ((EQ (location, Qleft_margin)
3003 || EQ (location, Qright_margin)
3004 || NILP (location))
3005 && valid_p
3006 && !display_replaced_before_p)
3007 {
3008 replaces_text_display_p = 1;
3009
3010 /* Save current settings of IT so that we can restore them
3011 when we are finished with the glyph property value. */
3012 push_it (it);
3013
3014 if (NILP (location))
3015 it->area = TEXT_AREA;
3016 else if (EQ (location, Qleft_margin))
3017 it->area = LEFT_MARGIN_AREA;
3018 else
3019 it->area = RIGHT_MARGIN_AREA;
3020
3021 if (STRINGP (value))
3022 {
3023 it->string = value;
3024 it->multibyte_p = STRING_MULTIBYTE (it->string);
3025 it->current.overlay_string_index = -1;
3026 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3027 it->end_charpos = it->string_nchars
3028 = XSTRING (it->string)->size;
3029 it->method = next_element_from_string;
3030 it->stop_charpos = 0;
3031 it->string_from_display_prop_p = 1;
3032 /* Say that we haven't consumed the characters with
3033 `display' property yet. The call to pop_it in
3034 set_iterator_to_next will clean this up. */
3035 *position = start_pos;
3036 }
3037 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3038 {
3039 it->method = next_element_from_stretch;
3040 it->object = value;
3041 it->current.pos = it->position = start_pos;
3042 }
3043 #ifdef HAVE_WINDOW_SYSTEM
3044 else
3045 {
3046 it->what = IT_IMAGE;
3047 it->image_id = lookup_image (it->f, value);
3048 it->position = start_pos;
3049 it->object = NILP (object) ? it->w->buffer : object;
3050 it->method = next_element_from_image;
3051
3052 /* Say that we haven't consumed the characters with
3053 `display' property yet. The call to pop_it in
3054 set_iterator_to_next will clean this up. */
3055 *position = start_pos;
3056 }
3057 #endif /* HAVE_WINDOW_SYSTEM */
3058 }
3059 else
3060 /* Invalid property or property not supported. Restore
3061 the position to what it was before. */
3062 *position = start_pos;
3063 }
3064
3065 return replaces_text_display_p;
3066 }
3067
3068
3069 /* Check if PROP is a display sub-property value whose text should be
3070 treated as intangible. */
3071
3072 static int
3073 single_display_prop_intangible_p (prop)
3074 Lisp_Object prop;
3075 {
3076 /* Skip over `when FORM'. */
3077 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3078 {
3079 prop = XCDR (prop);
3080 if (!CONSP (prop))
3081 return 0;
3082 prop = XCDR (prop);
3083 }
3084
3085 if (!CONSP (prop))
3086 return 0;
3087
3088 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3089 we don't need to treat text as intangible. */
3090 if (EQ (XCAR (prop), Qmargin))
3091 {
3092 prop = XCDR (prop);
3093 if (!CONSP (prop))
3094 return 0;
3095
3096 prop = XCDR (prop);
3097 if (!CONSP (prop)
3098 || EQ (XCAR (prop), Qleft_margin)
3099 || EQ (XCAR (prop), Qright_margin))
3100 return 0;
3101 }
3102
3103 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3104 }
3105
3106
3107 /* Check if PROP is a display property value whose text should be
3108 treated as intangible. */
3109
3110 int
3111 display_prop_intangible_p (prop)
3112 Lisp_Object prop;
3113 {
3114 if (CONSP (prop)
3115 && CONSP (XCAR (prop))
3116 && !EQ (Qmargin, XCAR (XCAR (prop))))
3117 {
3118 /* A list of sub-properties. */
3119 while (CONSP (prop))
3120 {
3121 if (single_display_prop_intangible_p (XCAR (prop)))
3122 return 1;
3123 prop = XCDR (prop);
3124 }
3125 }
3126 else if (VECTORP (prop))
3127 {
3128 /* A vector of sub-properties. */
3129 int i;
3130 for (i = 0; i < ASIZE (prop); ++i)
3131 if (single_display_prop_intangible_p (AREF (prop, i)))
3132 return 1;
3133 }
3134 else
3135 return single_display_prop_intangible_p (prop);
3136
3137 return 0;
3138 }
3139
3140
3141 /* Return 1 if PROP is a display sub-property value containing STRING. */
3142
3143 static int
3144 single_display_prop_string_p (prop, string)
3145 Lisp_Object prop, string;
3146 {
3147 extern Lisp_Object Qwhen, Qmargin;
3148
3149 if (EQ (string, prop))
3150 return 1;
3151
3152 /* Skip over `when FORM'. */
3153 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3154 {
3155 prop = XCDR (prop);
3156 if (!CONSP (prop))
3157 return 0;
3158 prop = XCDR (prop);
3159 }
3160
3161 if (CONSP (prop))
3162 /* Skip over `margin LOCATION'. */
3163 if (EQ (XCAR (prop), Qmargin))
3164 {
3165 prop = XCDR (prop);
3166 if (!CONSP (prop))
3167 return 0;
3168
3169 prop = XCDR (prop);
3170 if (!CONSP (prop))
3171 return 0;
3172 }
3173
3174 return CONSP (prop) && EQ (XCAR (prop), string);
3175 }
3176
3177
3178 /* Return 1 if STRING appears in the `display' property PROP. */
3179
3180 static int
3181 display_prop_string_p (prop, string)
3182 Lisp_Object prop, string;
3183 {
3184 extern Lisp_Object Qwhen, Qmargin;
3185
3186 if (CONSP (prop)
3187 && CONSP (XCAR (prop))
3188 && !EQ (Qmargin, XCAR (XCAR (prop))))
3189 {
3190 /* A list of sub-properties. */
3191 while (CONSP (prop))
3192 {
3193 if (single_display_prop_string_p (XCAR (prop), string))
3194 return 1;
3195 prop = XCDR (prop);
3196 }
3197 }
3198 else if (VECTORP (prop))
3199 {
3200 /* A vector of sub-properties. */
3201 int i;
3202 for (i = 0; i < ASIZE (prop); ++i)
3203 if (single_display_prop_string_p (AREF (prop, i), string))
3204 return 1;
3205 }
3206 else
3207 return single_display_prop_string_p (prop, string);
3208
3209 return 0;
3210 }
3211
3212
3213 /* Determine from which buffer position in W's buffer STRING comes
3214 from. AROUND_CHARPOS is an approximate position where it could
3215 be from. Value is the buffer position or 0 if it couldn't be
3216 determined.
3217
3218 W's buffer must be current.
3219
3220 This function is necessary because we don't record buffer positions
3221 in glyphs generated from strings (to keep struct glyph small).
3222 This function may only use code that doesn't eval because it is
3223 called asynchronously from note_mouse_highlight. */
3224
3225 int
3226 string_buffer_position (w, string, around_charpos)
3227 struct window *w;
3228 Lisp_Object string;
3229 int around_charpos;
3230 {
3231 Lisp_Object around = make_number (around_charpos);
3232 Lisp_Object limit, prop, pos;
3233 const int MAX_DISTANCE = 1000;
3234 int found = 0;
3235
3236 pos = make_number (around_charpos);
3237 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3238 while (!found && !EQ (pos, limit))
3239 {
3240 prop = Fget_char_property (pos, Qdisplay, Qnil);
3241 if (!NILP (prop) && display_prop_string_p (prop, string))
3242 found = 1;
3243 else
3244 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3245 }
3246
3247 if (!found)
3248 {
3249 pos = make_number (around_charpos);
3250 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3251 while (!found && !EQ (pos, limit))
3252 {
3253 prop = Fget_char_property (pos, Qdisplay, Qnil);
3254 if (!NILP (prop) && display_prop_string_p (prop, string))
3255 found = 1;
3256 else
3257 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3258 limit);
3259 }
3260 }
3261
3262 return found ? XINT (pos) : 0;
3263 }
3264
3265
3266 \f
3267 /***********************************************************************
3268 `composition' property
3269 ***********************************************************************/
3270
3271 /* Set up iterator IT from `composition' property at its current
3272 position. Called from handle_stop. */
3273
3274 static enum prop_handled
3275 handle_composition_prop (it)
3276 struct it *it;
3277 {
3278 Lisp_Object prop, string;
3279 int pos, pos_byte, end;
3280 enum prop_handled handled = HANDLED_NORMALLY;
3281
3282 if (STRINGP (it->string))
3283 {
3284 pos = IT_STRING_CHARPOS (*it);
3285 pos_byte = IT_STRING_BYTEPOS (*it);
3286 string = it->string;
3287 }
3288 else
3289 {
3290 pos = IT_CHARPOS (*it);
3291 pos_byte = IT_BYTEPOS (*it);
3292 string = Qnil;
3293 }
3294
3295 /* If there's a valid composition and point is not inside of the
3296 composition (in the case that the composition is from the current
3297 buffer), draw a glyph composed from the composition components. */
3298 if (find_composition (pos, -1, &pos, &end, &prop, string)
3299 && COMPOSITION_VALID_P (pos, end, prop)
3300 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3301 {
3302 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3303
3304 if (id >= 0)
3305 {
3306 it->method = next_element_from_composition;
3307 it->cmp_id = id;
3308 it->cmp_len = COMPOSITION_LENGTH (prop);
3309 /* For a terminal, draw only the first character of the
3310 components. */
3311 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3312 it->len = (STRINGP (it->string)
3313 ? string_char_to_byte (it->string, end)
3314 : CHAR_TO_BYTE (end)) - pos_byte;
3315 it->stop_charpos = end;
3316 handled = HANDLED_RETURN;
3317 }
3318 }
3319
3320 return handled;
3321 }
3322
3323
3324 \f
3325 /***********************************************************************
3326 Overlay strings
3327 ***********************************************************************/
3328
3329 /* The following structure is used to record overlay strings for
3330 later sorting in load_overlay_strings. */
3331
3332 struct overlay_entry
3333 {
3334 Lisp_Object overlay;
3335 Lisp_Object string;
3336 int priority;
3337 int after_string_p;
3338 };
3339
3340
3341 /* Set up iterator IT from overlay strings at its current position.
3342 Called from handle_stop. */
3343
3344 static enum prop_handled
3345 handle_overlay_change (it)
3346 struct it *it;
3347 {
3348 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3349 return HANDLED_RECOMPUTE_PROPS;
3350 else
3351 return HANDLED_NORMALLY;
3352 }
3353
3354
3355 /* Set up the next overlay string for delivery by IT, if there is an
3356 overlay string to deliver. Called by set_iterator_to_next when the
3357 end of the current overlay string is reached. If there are more
3358 overlay strings to display, IT->string and
3359 IT->current.overlay_string_index are set appropriately here.
3360 Otherwise IT->string is set to nil. */
3361
3362 static void
3363 next_overlay_string (it)
3364 struct it *it;
3365 {
3366 ++it->current.overlay_string_index;
3367 if (it->current.overlay_string_index == it->n_overlay_strings)
3368 {
3369 /* No more overlay strings. Restore IT's settings to what
3370 they were before overlay strings were processed, and
3371 continue to deliver from current_buffer. */
3372 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3373
3374 pop_it (it);
3375 xassert (it->stop_charpos >= BEGV
3376 && it->stop_charpos <= it->end_charpos);
3377 it->string = Qnil;
3378 it->current.overlay_string_index = -1;
3379 SET_TEXT_POS (it->current.string_pos, -1, -1);
3380 it->n_overlay_strings = 0;
3381 it->method = next_element_from_buffer;
3382
3383 /* If we're at the end of the buffer, record that we have
3384 processed the overlay strings there already, so that
3385 next_element_from_buffer doesn't try it again. */
3386 if (IT_CHARPOS (*it) >= it->end_charpos)
3387 it->overlay_strings_at_end_processed_p = 1;
3388
3389 /* If we have to display `...' for invisible text, set
3390 the iterator up for that. */
3391 if (display_ellipsis_p)
3392 setup_for_ellipsis (it);
3393 }
3394 else
3395 {
3396 /* There are more overlay strings to process. If
3397 IT->current.overlay_string_index has advanced to a position
3398 where we must load IT->overlay_strings with more strings, do
3399 it. */
3400 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3401
3402 if (it->current.overlay_string_index && i == 0)
3403 load_overlay_strings (it, 0);
3404
3405 /* Initialize IT to deliver display elements from the overlay
3406 string. */
3407 it->string = it->overlay_strings[i];
3408 it->multibyte_p = STRING_MULTIBYTE (it->string);
3409 SET_TEXT_POS (it->current.string_pos, 0, 0);
3410 it->method = next_element_from_string;
3411 it->stop_charpos = 0;
3412 }
3413
3414 CHECK_IT (it);
3415 }
3416
3417
3418 /* Compare two overlay_entry structures E1 and E2. Used as a
3419 comparison function for qsort in load_overlay_strings. Overlay
3420 strings for the same position are sorted so that
3421
3422 1. All after-strings come in front of before-strings, except
3423 when they come from the same overlay.
3424
3425 2. Within after-strings, strings are sorted so that overlay strings
3426 from overlays with higher priorities come first.
3427
3428 2. Within before-strings, strings are sorted so that overlay
3429 strings from overlays with higher priorities come last.
3430
3431 Value is analogous to strcmp. */
3432
3433
3434 static int
3435 compare_overlay_entries (e1, e2)
3436 void *e1, *e2;
3437 {
3438 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3439 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3440 int result;
3441
3442 if (entry1->after_string_p != entry2->after_string_p)
3443 {
3444 /* Let after-strings appear in front of before-strings if
3445 they come from different overlays. */
3446 if (EQ (entry1->overlay, entry2->overlay))
3447 result = entry1->after_string_p ? 1 : -1;
3448 else
3449 result = entry1->after_string_p ? -1 : 1;
3450 }
3451 else if (entry1->after_string_p)
3452 /* After-strings sorted in order of decreasing priority. */
3453 result = entry2->priority - entry1->priority;
3454 else
3455 /* Before-strings sorted in order of increasing priority. */
3456 result = entry1->priority - entry2->priority;
3457
3458 return result;
3459 }
3460
3461
3462 /* Load the vector IT->overlay_strings with overlay strings from IT's
3463 current buffer position, or from CHARPOS if that is > 0. Set
3464 IT->n_overlays to the total number of overlay strings found.
3465
3466 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3467 a time. On entry into load_overlay_strings,
3468 IT->current.overlay_string_index gives the number of overlay
3469 strings that have already been loaded by previous calls to this
3470 function.
3471
3472 IT->add_overlay_start contains an additional overlay start
3473 position to consider for taking overlay strings from, if non-zero.
3474 This position comes into play when the overlay has an `invisible'
3475 property, and both before and after-strings. When we've skipped to
3476 the end of the overlay, because of its `invisible' property, we
3477 nevertheless want its before-string to appear.
3478 IT->add_overlay_start will contain the overlay start position
3479 in this case.
3480
3481 Overlay strings are sorted so that after-string strings come in
3482 front of before-string strings. Within before and after-strings,
3483 strings are sorted by overlay priority. See also function
3484 compare_overlay_entries. */
3485
3486 static void
3487 load_overlay_strings (it, charpos)
3488 struct it *it;
3489 int charpos;
3490 {
3491 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3492 Lisp_Object ov, overlay, window, str, invisible;
3493 int start, end;
3494 int size = 20;
3495 int n = 0, i, j, invis_p;
3496 struct overlay_entry *entries
3497 = (struct overlay_entry *) alloca (size * sizeof *entries);
3498
3499 if (charpos <= 0)
3500 charpos = IT_CHARPOS (*it);
3501
3502 /* Append the overlay string STRING of overlay OVERLAY to vector
3503 `entries' which has size `size' and currently contains `n'
3504 elements. AFTER_P non-zero means STRING is an after-string of
3505 OVERLAY. */
3506 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3507 do \
3508 { \
3509 Lisp_Object priority; \
3510 \
3511 if (n == size) \
3512 { \
3513 int new_size = 2 * size; \
3514 struct overlay_entry *old = entries; \
3515 entries = \
3516 (struct overlay_entry *) alloca (new_size \
3517 * sizeof *entries); \
3518 bcopy (old, entries, size * sizeof *entries); \
3519 size = new_size; \
3520 } \
3521 \
3522 entries[n].string = (STRING); \
3523 entries[n].overlay = (OVERLAY); \
3524 priority = Foverlay_get ((OVERLAY), Qpriority); \
3525 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3526 entries[n].after_string_p = (AFTER_P); \
3527 ++n; \
3528 } \
3529 while (0)
3530
3531 /* Process overlay before the overlay center. */
3532 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3533 {
3534 overlay = XCAR (ov);
3535 xassert (OVERLAYP (overlay));
3536 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3537 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3538
3539 if (end < charpos)
3540 break;
3541
3542 /* Skip this overlay if it doesn't start or end at IT's current
3543 position. */
3544 if (end != charpos && start != charpos)
3545 continue;
3546
3547 /* Skip this overlay if it doesn't apply to IT->w. */
3548 window = Foverlay_get (overlay, Qwindow);
3549 if (WINDOWP (window) && XWINDOW (window) != it->w)
3550 continue;
3551
3552 /* If the text ``under'' the overlay is invisible, both before-
3553 and after-strings from this overlay are visible; start and
3554 end position are indistinguishable. */
3555 invisible = Foverlay_get (overlay, Qinvisible);
3556 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3557
3558 /* If overlay has a non-empty before-string, record it. */
3559 if ((start == charpos || (end == charpos && invis_p))
3560 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3561 && XSTRING (str)->size)
3562 RECORD_OVERLAY_STRING (overlay, str, 0);
3563
3564 /* If overlay has a non-empty after-string, record it. */
3565 if ((end == charpos || (start == charpos && invis_p))
3566 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3567 && XSTRING (str)->size)
3568 RECORD_OVERLAY_STRING (overlay, str, 1);
3569 }
3570
3571 /* Process overlays after the overlay center. */
3572 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3573 {
3574 overlay = XCAR (ov);
3575 xassert (OVERLAYP (overlay));
3576 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3577 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3578
3579 if (start > charpos)
3580 break;
3581
3582 /* Skip this overlay if it doesn't start or end at IT's current
3583 position. */
3584 if (end != charpos && start != charpos)
3585 continue;
3586
3587 /* Skip this overlay if it doesn't apply to IT->w. */
3588 window = Foverlay_get (overlay, Qwindow);
3589 if (WINDOWP (window) && XWINDOW (window) != it->w)
3590 continue;
3591
3592 /* If the text ``under'' the overlay is invisible, it has a zero
3593 dimension, and both before- and after-strings apply. */
3594 invisible = Foverlay_get (overlay, Qinvisible);
3595 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3596
3597 /* If overlay has a non-empty before-string, record it. */
3598 if ((start == charpos || (end == charpos && invis_p))
3599 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3600 && XSTRING (str)->size)
3601 RECORD_OVERLAY_STRING (overlay, str, 0);
3602
3603 /* If overlay has a non-empty after-string, record it. */
3604 if ((end == charpos || (start == charpos && invis_p))
3605 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3606 && XSTRING (str)->size)
3607 RECORD_OVERLAY_STRING (overlay, str, 1);
3608 }
3609
3610 #undef RECORD_OVERLAY_STRING
3611
3612 /* Sort entries. */
3613 if (n > 1)
3614 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3615
3616 /* Record the total number of strings to process. */
3617 it->n_overlay_strings = n;
3618
3619 /* IT->current.overlay_string_index is the number of overlay strings
3620 that have already been consumed by IT. Copy some of the
3621 remaining overlay strings to IT->overlay_strings. */
3622 i = 0;
3623 j = it->current.overlay_string_index;
3624 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3625 it->overlay_strings[i++] = entries[j++].string;
3626
3627 CHECK_IT (it);
3628 }
3629
3630
3631 /* Get the first chunk of overlay strings at IT's current buffer
3632 position, or at CHARPOS if that is > 0. Value is non-zero if at
3633 least one overlay string was found. */
3634
3635 static int
3636 get_overlay_strings (it, charpos)
3637 struct it *it;
3638 int charpos;
3639 {
3640 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3641 process. This fills IT->overlay_strings with strings, and sets
3642 IT->n_overlay_strings to the total number of strings to process.
3643 IT->pos.overlay_string_index has to be set temporarily to zero
3644 because load_overlay_strings needs this; it must be set to -1
3645 when no overlay strings are found because a zero value would
3646 indicate a position in the first overlay string. */
3647 it->current.overlay_string_index = 0;
3648 load_overlay_strings (it, charpos);
3649
3650 /* If we found overlay strings, set up IT to deliver display
3651 elements from the first one. Otherwise set up IT to deliver
3652 from current_buffer. */
3653 if (it->n_overlay_strings)
3654 {
3655 /* Make sure we know settings in current_buffer, so that we can
3656 restore meaningful values when we're done with the overlay
3657 strings. */
3658 compute_stop_pos (it);
3659 xassert (it->face_id >= 0);
3660
3661 /* Save IT's settings. They are restored after all overlay
3662 strings have been processed. */
3663 xassert (it->sp == 0);
3664 push_it (it);
3665
3666 /* Set up IT to deliver display elements from the first overlay
3667 string. */
3668 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3669 it->string = it->overlay_strings[0];
3670 it->stop_charpos = 0;
3671 it->end_charpos = XSTRING (it->string)->size;
3672 it->multibyte_p = STRING_MULTIBYTE (it->string);
3673 xassert (STRINGP (it->string));
3674 it->method = next_element_from_string;
3675 }
3676 else
3677 {
3678 it->string = Qnil;
3679 it->current.overlay_string_index = -1;
3680 it->method = next_element_from_buffer;
3681 }
3682
3683 CHECK_IT (it);
3684
3685 /* Value is non-zero if we found at least one overlay string. */
3686 return STRINGP (it->string);
3687 }
3688
3689
3690 \f
3691 /***********************************************************************
3692 Saving and restoring state
3693 ***********************************************************************/
3694
3695 /* Save current settings of IT on IT->stack. Called, for example,
3696 before setting up IT for an overlay string, to be able to restore
3697 IT's settings to what they were after the overlay string has been
3698 processed. */
3699
3700 static void
3701 push_it (it)
3702 struct it *it;
3703 {
3704 struct iterator_stack_entry *p;
3705
3706 xassert (it->sp < 2);
3707 p = it->stack + it->sp;
3708
3709 p->stop_charpos = it->stop_charpos;
3710 xassert (it->face_id >= 0);
3711 p->face_id = it->face_id;
3712 p->string = it->string;
3713 p->pos = it->current;
3714 p->end_charpos = it->end_charpos;
3715 p->string_nchars = it->string_nchars;
3716 p->area = it->area;
3717 p->multibyte_p = it->multibyte_p;
3718 p->space_width = it->space_width;
3719 p->font_height = it->font_height;
3720 p->voffset = it->voffset;
3721 p->string_from_display_prop_p = it->string_from_display_prop_p;
3722 p->display_ellipsis_p = 0;
3723 ++it->sp;
3724 }
3725
3726
3727 /* Restore IT's settings from IT->stack. Called, for example, when no
3728 more overlay strings must be processed, and we return to delivering
3729 display elements from a buffer, or when the end of a string from a
3730 `display' property is reached and we return to delivering display
3731 elements from an overlay string, or from a buffer. */
3732
3733 static void
3734 pop_it (it)
3735 struct it *it;
3736 {
3737 struct iterator_stack_entry *p;
3738
3739 xassert (it->sp > 0);
3740 --it->sp;
3741 p = it->stack + it->sp;
3742 it->stop_charpos = p->stop_charpos;
3743 it->face_id = p->face_id;
3744 it->string = p->string;
3745 it->current = p->pos;
3746 it->end_charpos = p->end_charpos;
3747 it->string_nchars = p->string_nchars;
3748 it->area = p->area;
3749 it->multibyte_p = p->multibyte_p;
3750 it->space_width = p->space_width;
3751 it->font_height = p->font_height;
3752 it->voffset = p->voffset;
3753 it->string_from_display_prop_p = p->string_from_display_prop_p;
3754 }
3755
3756
3757 \f
3758 /***********************************************************************
3759 Moving over lines
3760 ***********************************************************************/
3761
3762 /* Set IT's current position to the previous line start. */
3763
3764 static void
3765 back_to_previous_line_start (it)
3766 struct it *it;
3767 {
3768 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3769 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3770 }
3771
3772
3773 /* Move IT to the next line start.
3774
3775 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3776 we skipped over part of the text (as opposed to moving the iterator
3777 continuously over the text). Otherwise, don't change the value
3778 of *SKIPPED_P.
3779
3780 Newlines may come from buffer text, overlay strings, or strings
3781 displayed via the `display' property. That's the reason we can't
3782 simply use find_next_newline_no_quit.
3783
3784 Note that this function may not skip over invisible text that is so
3785 because of text properties and immediately follows a newline. If
3786 it would, function reseat_at_next_visible_line_start, when called
3787 from set_iterator_to_next, would effectively make invisible
3788 characters following a newline part of the wrong glyph row, which
3789 leads to wrong cursor motion. */
3790
3791 static int
3792 forward_to_next_line_start (it, skipped_p)
3793 struct it *it;
3794 int *skipped_p;
3795 {
3796 int old_selective, newline_found_p, n;
3797 const int MAX_NEWLINE_DISTANCE = 500;
3798
3799 /* If already on a newline, just consume it to avoid unintended
3800 skipping over invisible text below. */
3801 if (it->what == IT_CHARACTER
3802 && it->c == '\n'
3803 && CHARPOS (it->position) == IT_CHARPOS (*it))
3804 {
3805 set_iterator_to_next (it, 0);
3806 it->c = 0;
3807 return 1;
3808 }
3809
3810 /* Don't handle selective display in the following. It's (a)
3811 unnecessary because it's done by the caller, and (b) leads to an
3812 infinite recursion because next_element_from_ellipsis indirectly
3813 calls this function. */
3814 old_selective = it->selective;
3815 it->selective = 0;
3816
3817 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3818 from buffer text. */
3819 for (n = newline_found_p = 0;
3820 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3821 n += STRINGP (it->string) ? 0 : 1)
3822 {
3823 if (!get_next_display_element (it))
3824 break;
3825 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3826 set_iterator_to_next (it, 0);
3827 }
3828
3829 /* If we didn't find a newline near enough, see if we can use a
3830 short-cut. */
3831 if (n == MAX_NEWLINE_DISTANCE)
3832 {
3833 int start = IT_CHARPOS (*it);
3834 int limit = find_next_newline_no_quit (start, 1);
3835 Lisp_Object pos;
3836
3837 xassert (!STRINGP (it->string));
3838
3839 /* If there isn't any `display' property in sight, and no
3840 overlays, we can just use the position of the newline in
3841 buffer text. */
3842 if (it->stop_charpos >= limit
3843 || ((pos = Fnext_single_property_change (make_number (start),
3844 Qdisplay,
3845 Qnil, make_number (limit)),
3846 NILP (pos))
3847 && next_overlay_change (start) == ZV))
3848 {
3849 IT_CHARPOS (*it) = limit;
3850 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3851 *skipped_p = newline_found_p = 1;
3852 }
3853 else
3854 {
3855 while (get_next_display_element (it)
3856 && !newline_found_p)
3857 {
3858 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3859 set_iterator_to_next (it, 0);
3860 }
3861 }
3862 }
3863
3864 it->selective = old_selective;
3865 return newline_found_p;
3866 }
3867
3868
3869 /* Set IT's current position to the previous visible line start. Skip
3870 invisible text that is so either due to text properties or due to
3871 selective display. Caution: this does not change IT->current_x and
3872 IT->hpos. */
3873
3874 static void
3875 back_to_previous_visible_line_start (it)
3876 struct it *it;
3877 {
3878 int visible_p = 0;
3879
3880 /* Go back one newline if not on BEGV already. */
3881 if (IT_CHARPOS (*it) > BEGV)
3882 back_to_previous_line_start (it);
3883
3884 /* Move over lines that are invisible because of selective display
3885 or text properties. */
3886 while (IT_CHARPOS (*it) > BEGV
3887 && !visible_p)
3888 {
3889 visible_p = 1;
3890
3891 /* If selective > 0, then lines indented more than that values
3892 are invisible. */
3893 if (it->selective > 0
3894 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3895 it->selective))
3896 visible_p = 0;
3897 else
3898 {
3899 Lisp_Object prop;
3900
3901 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3902 Qinvisible, it->window);
3903 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3904 visible_p = 0;
3905 }
3906
3907 /* Back one more newline if the current one is invisible. */
3908 if (!visible_p)
3909 back_to_previous_line_start (it);
3910 }
3911
3912 xassert (IT_CHARPOS (*it) >= BEGV);
3913 xassert (IT_CHARPOS (*it) == BEGV
3914 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3915 CHECK_IT (it);
3916 }
3917
3918
3919 /* Reseat iterator IT at the previous visible line start. Skip
3920 invisible text that is so either due to text properties or due to
3921 selective display. At the end, update IT's overlay information,
3922 face information etc. */
3923
3924 static void
3925 reseat_at_previous_visible_line_start (it)
3926 struct it *it;
3927 {
3928 back_to_previous_visible_line_start (it);
3929 reseat (it, it->current.pos, 1);
3930 CHECK_IT (it);
3931 }
3932
3933
3934 /* Reseat iterator IT on the next visible line start in the current
3935 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3936 preceding the line start. Skip over invisible text that is so
3937 because of selective display. Compute faces, overlays etc at the
3938 new position. Note that this function does not skip over text that
3939 is invisible because of text properties. */
3940
3941 static void
3942 reseat_at_next_visible_line_start (it, on_newline_p)
3943 struct it *it;
3944 int on_newline_p;
3945 {
3946 int newline_found_p, skipped_p = 0;
3947
3948 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3949
3950 /* Skip over lines that are invisible because they are indented
3951 more than the value of IT->selective. */
3952 if (it->selective > 0)
3953 while (IT_CHARPOS (*it) < ZV
3954 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3955 it->selective))
3956 {
3957 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3958 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3959 }
3960
3961 /* Position on the newline if that's what's requested. */
3962 if (on_newline_p && newline_found_p)
3963 {
3964 if (STRINGP (it->string))
3965 {
3966 if (IT_STRING_CHARPOS (*it) > 0)
3967 {
3968 --IT_STRING_CHARPOS (*it);
3969 --IT_STRING_BYTEPOS (*it);
3970 }
3971 }
3972 else if (IT_CHARPOS (*it) > BEGV)
3973 {
3974 --IT_CHARPOS (*it);
3975 --IT_BYTEPOS (*it);
3976 reseat (it, it->current.pos, 0);
3977 }
3978 }
3979 else if (skipped_p)
3980 reseat (it, it->current.pos, 0);
3981
3982 CHECK_IT (it);
3983 }
3984
3985
3986 \f
3987 /***********************************************************************
3988 Changing an iterator's position
3989 ***********************************************************************/
3990
3991 /* Change IT's current position to POS in current_buffer. If FORCE_P
3992 is non-zero, always check for text properties at the new position.
3993 Otherwise, text properties are only looked up if POS >=
3994 IT->check_charpos of a property. */
3995
3996 static void
3997 reseat (it, pos, force_p)
3998 struct it *it;
3999 struct text_pos pos;
4000 int force_p;
4001 {
4002 int original_pos = IT_CHARPOS (*it);
4003
4004 reseat_1 (it, pos, 0);
4005
4006 /* Determine where to check text properties. Avoid doing it
4007 where possible because text property lookup is very expensive. */
4008 if (force_p
4009 || CHARPOS (pos) > it->stop_charpos
4010 || CHARPOS (pos) < original_pos)
4011 handle_stop (it);
4012
4013 CHECK_IT (it);
4014 }
4015
4016
4017 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4018 IT->stop_pos to POS, also. */
4019
4020 static void
4021 reseat_1 (it, pos, set_stop_p)
4022 struct it *it;
4023 struct text_pos pos;
4024 int set_stop_p;
4025 {
4026 /* Don't call this function when scanning a C string. */
4027 xassert (it->s == NULL);
4028
4029 /* POS must be a reasonable value. */
4030 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4031
4032 it->current.pos = it->position = pos;
4033 XSETBUFFER (it->object, current_buffer);
4034 it->end_charpos = ZV;
4035 it->dpvec = NULL;
4036 it->current.dpvec_index = -1;
4037 it->current.overlay_string_index = -1;
4038 IT_STRING_CHARPOS (*it) = -1;
4039 IT_STRING_BYTEPOS (*it) = -1;
4040 it->string = Qnil;
4041 it->method = next_element_from_buffer;
4042 it->sp = 0;
4043 it->face_before_selective_p = 0;
4044
4045 if (set_stop_p)
4046 it->stop_charpos = CHARPOS (pos);
4047 }
4048
4049
4050 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4051 If S is non-null, it is a C string to iterate over. Otherwise,
4052 STRING gives a Lisp string to iterate over.
4053
4054 If PRECISION > 0, don't return more then PRECISION number of
4055 characters from the string.
4056
4057 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4058 characters have been returned. FIELD_WIDTH < 0 means an infinite
4059 field width.
4060
4061 MULTIBYTE = 0 means disable processing of multibyte characters,
4062 MULTIBYTE > 0 means enable it,
4063 MULTIBYTE < 0 means use IT->multibyte_p.
4064
4065 IT must be initialized via a prior call to init_iterator before
4066 calling this function. */
4067
4068 static void
4069 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4070 struct it *it;
4071 unsigned char *s;
4072 Lisp_Object string;
4073 int charpos;
4074 int precision, field_width, multibyte;
4075 {
4076 /* No region in strings. */
4077 it->region_beg_charpos = it->region_end_charpos = -1;
4078
4079 /* No text property checks performed by default, but see below. */
4080 it->stop_charpos = -1;
4081
4082 /* Set iterator position and end position. */
4083 bzero (&it->current, sizeof it->current);
4084 it->current.overlay_string_index = -1;
4085 it->current.dpvec_index = -1;
4086 xassert (charpos >= 0);
4087
4088 /* If STRING is specified, use its multibyteness, otherwise use the
4089 setting of MULTIBYTE, if specified. */
4090 if (multibyte >= 0)
4091 it->multibyte_p = multibyte > 0;
4092
4093 if (s == NULL)
4094 {
4095 xassert (STRINGP (string));
4096 it->string = string;
4097 it->s = NULL;
4098 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4099 it->method = next_element_from_string;
4100 it->current.string_pos = string_pos (charpos, string);
4101 }
4102 else
4103 {
4104 it->s = s;
4105 it->string = Qnil;
4106
4107 /* Note that we use IT->current.pos, not it->current.string_pos,
4108 for displaying C strings. */
4109 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4110 if (it->multibyte_p)
4111 {
4112 it->current.pos = c_string_pos (charpos, s, 1);
4113 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4114 }
4115 else
4116 {
4117 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4118 it->end_charpos = it->string_nchars = strlen (s);
4119 }
4120
4121 it->method = next_element_from_c_string;
4122 }
4123
4124 /* PRECISION > 0 means don't return more than PRECISION characters
4125 from the string. */
4126 if (precision > 0 && it->end_charpos - charpos > precision)
4127 it->end_charpos = it->string_nchars = charpos + precision;
4128
4129 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4130 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4131 FIELD_WIDTH < 0 means infinite field width. This is useful for
4132 padding with `-' at the end of a mode line. */
4133 if (field_width < 0)
4134 field_width = INFINITY;
4135 if (field_width > it->end_charpos - charpos)
4136 it->end_charpos = charpos + field_width;
4137
4138 /* Use the standard display table for displaying strings. */
4139 if (DISP_TABLE_P (Vstandard_display_table))
4140 it->dp = XCHAR_TABLE (Vstandard_display_table);
4141
4142 it->stop_charpos = charpos;
4143 CHECK_IT (it);
4144 }
4145
4146
4147 \f
4148 /***********************************************************************
4149 Iteration
4150 ***********************************************************************/
4151
4152 /* Load IT's display element fields with information about the next
4153 display element from the current position of IT. Value is zero if
4154 end of buffer (or C string) is reached. */
4155
4156 int
4157 get_next_display_element (it)
4158 struct it *it;
4159 {
4160 /* Non-zero means that we found an display element. Zero means that
4161 we hit the end of what we iterate over. Performance note: the
4162 function pointer `method' used here turns out to be faster than
4163 using a sequence of if-statements. */
4164 int success_p = (*it->method) (it);
4165
4166 if (it->what == IT_CHARACTER)
4167 {
4168 /* Map via display table or translate control characters.
4169 IT->c, IT->len etc. have been set to the next character by
4170 the function call above. If we have a display table, and it
4171 contains an entry for IT->c, translate it. Don't do this if
4172 IT->c itself comes from a display table, otherwise we could
4173 end up in an infinite recursion. (An alternative could be to
4174 count the recursion depth of this function and signal an
4175 error when a certain maximum depth is reached.) Is it worth
4176 it? */
4177 if (success_p && it->dpvec == NULL)
4178 {
4179 Lisp_Object dv;
4180
4181 if (it->dp
4182 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4183 VECTORP (dv)))
4184 {
4185 struct Lisp_Vector *v = XVECTOR (dv);
4186
4187 /* Return the first character from the display table
4188 entry, if not empty. If empty, don't display the
4189 current character. */
4190 if (v->size)
4191 {
4192 it->dpvec_char_len = it->len;
4193 it->dpvec = v->contents;
4194 it->dpend = v->contents + v->size;
4195 it->current.dpvec_index = 0;
4196 it->method = next_element_from_display_vector;
4197 success_p = get_next_display_element (it);
4198 }
4199 else
4200 {
4201 set_iterator_to_next (it, 0);
4202 success_p = get_next_display_element (it);
4203 }
4204 }
4205
4206 /* Translate control characters into `\003' or `^C' form.
4207 Control characters coming from a display table entry are
4208 currently not translated because we use IT->dpvec to hold
4209 the translation. This could easily be changed but I
4210 don't believe that it is worth doing.
4211
4212 Non-printable multibyte characters are also translated
4213 octal form. */
4214 else if ((it->c < ' '
4215 && (it->area != TEXT_AREA
4216 || (it->c != '\n' && it->c != '\t')))
4217 || (it->c >= 127
4218 && it->len == 1)
4219 || !CHAR_PRINTABLE_P (it->c))
4220 {
4221 /* IT->c is a control character which must be displayed
4222 either as '\003' or as `^C' where the '\\' and '^'
4223 can be defined in the display table. Fill
4224 IT->ctl_chars with glyphs for what we have to
4225 display. Then, set IT->dpvec to these glyphs. */
4226 GLYPH g;
4227
4228 if (it->c < 128 && it->ctl_arrow_p)
4229 {
4230 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4231 if (it->dp
4232 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4233 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4234 g = XINT (DISP_CTRL_GLYPH (it->dp));
4235 else
4236 g = FAST_MAKE_GLYPH ('^', 0);
4237 XSETINT (it->ctl_chars[0], g);
4238
4239 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4240 XSETINT (it->ctl_chars[1], g);
4241
4242 /* Set up IT->dpvec and return first character from it. */
4243 it->dpvec_char_len = it->len;
4244 it->dpvec = it->ctl_chars;
4245 it->dpend = it->dpvec + 2;
4246 it->current.dpvec_index = 0;
4247 it->method = next_element_from_display_vector;
4248 get_next_display_element (it);
4249 }
4250 else
4251 {
4252 unsigned char str[MAX_MULTIBYTE_LENGTH];
4253 int len;
4254 int i;
4255 GLYPH escape_glyph;
4256
4257 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4258 if (it->dp
4259 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4260 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4261 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4262 else
4263 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4264
4265 if (SINGLE_BYTE_CHAR_P (it->c))
4266 str[0] = it->c, len = 1;
4267 else
4268 {
4269 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4270 if (len < 0)
4271 {
4272 /* It's an invalid character, which
4273 shouldn't happen actually, but due to
4274 bugs it may happen. Let's print the char
4275 as is, there's not much meaningful we can
4276 do with it. */
4277 str[0] = it->c;
4278 str[1] = it->c >> 8;
4279 str[2] = it->c >> 16;
4280 str[3] = it->c >> 24;
4281 len = 4;
4282 }
4283 }
4284
4285 for (i = 0; i < len; i++)
4286 {
4287 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4288 /* Insert three more glyphs into IT->ctl_chars for
4289 the octal display of the character. */
4290 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4291 XSETINT (it->ctl_chars[i * 4 + 1], g);
4292 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4293 XSETINT (it->ctl_chars[i * 4 + 2], g);
4294 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4295 XSETINT (it->ctl_chars[i * 4 + 3], g);
4296 }
4297
4298 /* Set up IT->dpvec and return the first character
4299 from it. */
4300 it->dpvec_char_len = it->len;
4301 it->dpvec = it->ctl_chars;
4302 it->dpend = it->dpvec + len * 4;
4303 it->current.dpvec_index = 0;
4304 it->method = next_element_from_display_vector;
4305 get_next_display_element (it);
4306 }
4307 }
4308 }
4309
4310 /* Adjust face id for a multibyte character. There are no
4311 multibyte character in unibyte text. */
4312 if (it->multibyte_p
4313 && success_p
4314 && FRAME_WINDOW_P (it->f))
4315 {
4316 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4317 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4318 }
4319 }
4320
4321 /* Is this character the last one of a run of characters with
4322 box? If yes, set IT->end_of_box_run_p to 1. */
4323 if (it->face_box_p
4324 && it->s == NULL)
4325 {
4326 int face_id;
4327 struct face *face;
4328
4329 it->end_of_box_run_p
4330 = ((face_id = face_after_it_pos (it),
4331 face_id != it->face_id)
4332 && (face = FACE_FROM_ID (it->f, face_id),
4333 face->box == FACE_NO_BOX));
4334 }
4335
4336 /* Value is 0 if end of buffer or string reached. */
4337 return success_p;
4338 }
4339
4340
4341 /* Move IT to the next display element.
4342
4343 RESEAT_P non-zero means if called on a newline in buffer text,
4344 skip to the next visible line start.
4345
4346 Functions get_next_display_element and set_iterator_to_next are
4347 separate because I find this arrangement easier to handle than a
4348 get_next_display_element function that also increments IT's
4349 position. The way it is we can first look at an iterator's current
4350 display element, decide whether it fits on a line, and if it does,
4351 increment the iterator position. The other way around we probably
4352 would either need a flag indicating whether the iterator has to be
4353 incremented the next time, or we would have to implement a
4354 decrement position function which would not be easy to write. */
4355
4356 void
4357 set_iterator_to_next (it, reseat_p)
4358 struct it *it;
4359 int reseat_p;
4360 {
4361 /* Reset flags indicating start and end of a sequence of characters
4362 with box. Reset them at the start of this function because
4363 moving the iterator to a new position might set them. */
4364 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4365
4366 if (it->method == next_element_from_buffer)
4367 {
4368 /* The current display element of IT is a character from
4369 current_buffer. Advance in the buffer, and maybe skip over
4370 invisible lines that are so because of selective display. */
4371 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4372 reseat_at_next_visible_line_start (it, 0);
4373 else
4374 {
4375 xassert (it->len != 0);
4376 IT_BYTEPOS (*it) += it->len;
4377 IT_CHARPOS (*it) += 1;
4378 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4379 }
4380 }
4381 else if (it->method == next_element_from_composition)
4382 {
4383 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4384 if (STRINGP (it->string))
4385 {
4386 IT_STRING_BYTEPOS (*it) += it->len;
4387 IT_STRING_CHARPOS (*it) += it->cmp_len;
4388 it->method = next_element_from_string;
4389 goto consider_string_end;
4390 }
4391 else
4392 {
4393 IT_BYTEPOS (*it) += it->len;
4394 IT_CHARPOS (*it) += it->cmp_len;
4395 it->method = next_element_from_buffer;
4396 }
4397 }
4398 else if (it->method == next_element_from_c_string)
4399 {
4400 /* Current display element of IT is from a C string. */
4401 IT_BYTEPOS (*it) += it->len;
4402 IT_CHARPOS (*it) += 1;
4403 }
4404 else if (it->method == next_element_from_display_vector)
4405 {
4406 /* Current display element of IT is from a display table entry.
4407 Advance in the display table definition. Reset it to null if
4408 end reached, and continue with characters from buffers/
4409 strings. */
4410 ++it->current.dpvec_index;
4411
4412 /* Restore face of the iterator to what they were before the
4413 display vector entry (these entries may contain faces). */
4414 it->face_id = it->saved_face_id;
4415
4416 if (it->dpvec + it->current.dpvec_index == it->dpend)
4417 {
4418 if (it->s)
4419 it->method = next_element_from_c_string;
4420 else if (STRINGP (it->string))
4421 it->method = next_element_from_string;
4422 else
4423 it->method = next_element_from_buffer;
4424
4425 it->dpvec = NULL;
4426 it->current.dpvec_index = -1;
4427
4428 /* Skip over characters which were displayed via IT->dpvec. */
4429 if (it->dpvec_char_len < 0)
4430 reseat_at_next_visible_line_start (it, 1);
4431 else if (it->dpvec_char_len > 0)
4432 {
4433 it->len = it->dpvec_char_len;
4434 set_iterator_to_next (it, reseat_p);
4435 }
4436 }
4437 }
4438 else if (it->method == next_element_from_string)
4439 {
4440 /* Current display element is a character from a Lisp string. */
4441 xassert (it->s == NULL && STRINGP (it->string));
4442 IT_STRING_BYTEPOS (*it) += it->len;
4443 IT_STRING_CHARPOS (*it) += 1;
4444
4445 consider_string_end:
4446
4447 if (it->current.overlay_string_index >= 0)
4448 {
4449 /* IT->string is an overlay string. Advance to the
4450 next, if there is one. */
4451 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4452 next_overlay_string (it);
4453 }
4454 else
4455 {
4456 /* IT->string is not an overlay string. If we reached
4457 its end, and there is something on IT->stack, proceed
4458 with what is on the stack. This can be either another
4459 string, this time an overlay string, or a buffer. */
4460 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4461 && it->sp > 0)
4462 {
4463 pop_it (it);
4464 if (!STRINGP (it->string))
4465 it->method = next_element_from_buffer;
4466 else
4467 goto consider_string_end;
4468 }
4469 }
4470 }
4471 else if (it->method == next_element_from_image
4472 || it->method == next_element_from_stretch)
4473 {
4474 /* The position etc with which we have to proceed are on
4475 the stack. The position may be at the end of a string,
4476 if the `display' property takes up the whole string. */
4477 pop_it (it);
4478 it->image_id = 0;
4479 if (STRINGP (it->string))
4480 {
4481 it->method = next_element_from_string;
4482 goto consider_string_end;
4483 }
4484 else
4485 it->method = next_element_from_buffer;
4486 }
4487 else
4488 /* There are no other methods defined, so this should be a bug. */
4489 abort ();
4490
4491 xassert (it->method != next_element_from_string
4492 || (STRINGP (it->string)
4493 && IT_STRING_CHARPOS (*it) >= 0));
4494 }
4495
4496
4497 /* Load IT's display element fields with information about the next
4498 display element which comes from a display table entry or from the
4499 result of translating a control character to one of the forms `^C'
4500 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4501
4502 static int
4503 next_element_from_display_vector (it)
4504 struct it *it;
4505 {
4506 /* Precondition. */
4507 xassert (it->dpvec && it->current.dpvec_index >= 0);
4508
4509 /* Remember the current face id in case glyphs specify faces.
4510 IT's face is restored in set_iterator_to_next. */
4511 it->saved_face_id = it->face_id;
4512
4513 if (INTEGERP (*it->dpvec)
4514 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4515 {
4516 int lface_id;
4517 GLYPH g;
4518
4519 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4520 it->c = FAST_GLYPH_CHAR (g);
4521 it->len = CHAR_BYTES (it->c);
4522
4523 /* The entry may contain a face id to use. Such a face id is
4524 the id of a Lisp face, not a realized face. A face id of
4525 zero means no face is specified. */
4526 lface_id = FAST_GLYPH_FACE (g);
4527 if (lface_id)
4528 {
4529 /* The function returns -1 if lface_id is invalid. */
4530 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4531 if (face_id >= 0)
4532 it->face_id = face_id;
4533 }
4534 }
4535 else
4536 /* Display table entry is invalid. Return a space. */
4537 it->c = ' ', it->len = 1;
4538
4539 /* Don't change position and object of the iterator here. They are
4540 still the values of the character that had this display table
4541 entry or was translated, and that's what we want. */
4542 it->what = IT_CHARACTER;
4543 return 1;
4544 }
4545
4546
4547 /* Load IT with the next display element from Lisp string IT->string.
4548 IT->current.string_pos is the current position within the string.
4549 If IT->current.overlay_string_index >= 0, the Lisp string is an
4550 overlay string. */
4551
4552 static int
4553 next_element_from_string (it)
4554 struct it *it;
4555 {
4556 struct text_pos position;
4557
4558 xassert (STRINGP (it->string));
4559 xassert (IT_STRING_CHARPOS (*it) >= 0);
4560 position = it->current.string_pos;
4561
4562 /* Time to check for invisible text? */
4563 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4564 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4565 {
4566 handle_stop (it);
4567
4568 /* Since a handler may have changed IT->method, we must
4569 recurse here. */
4570 return get_next_display_element (it);
4571 }
4572
4573 if (it->current.overlay_string_index >= 0)
4574 {
4575 /* Get the next character from an overlay string. In overlay
4576 strings, There is no field width or padding with spaces to
4577 do. */
4578 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4579 {
4580 it->what = IT_EOB;
4581 return 0;
4582 }
4583 else if (STRING_MULTIBYTE (it->string))
4584 {
4585 int remaining = (STRING_BYTES (XSTRING (it->string))
4586 - IT_STRING_BYTEPOS (*it));
4587 unsigned char *s = (XSTRING (it->string)->data
4588 + IT_STRING_BYTEPOS (*it));
4589 it->c = string_char_and_length (s, remaining, &it->len);
4590 }
4591 else
4592 {
4593 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4594 it->len = 1;
4595 }
4596 }
4597 else
4598 {
4599 /* Get the next character from a Lisp string that is not an
4600 overlay string. Such strings come from the mode line, for
4601 example. We may have to pad with spaces, or truncate the
4602 string. See also next_element_from_c_string. */
4603 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4604 {
4605 it->what = IT_EOB;
4606 return 0;
4607 }
4608 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4609 {
4610 /* Pad with spaces. */
4611 it->c = ' ', it->len = 1;
4612 CHARPOS (position) = BYTEPOS (position) = -1;
4613 }
4614 else if (STRING_MULTIBYTE (it->string))
4615 {
4616 int maxlen = (STRING_BYTES (XSTRING (it->string))
4617 - IT_STRING_BYTEPOS (*it));
4618 unsigned char *s = (XSTRING (it->string)->data
4619 + IT_STRING_BYTEPOS (*it));
4620 it->c = string_char_and_length (s, maxlen, &it->len);
4621 }
4622 else
4623 {
4624 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4625 it->len = 1;
4626 }
4627 }
4628
4629 /* Record what we have and where it came from. Note that we store a
4630 buffer position in IT->position although it could arguably be a
4631 string position. */
4632 it->what = IT_CHARACTER;
4633 it->object = it->string;
4634 it->position = position;
4635 return 1;
4636 }
4637
4638
4639 /* Load IT with next display element from C string IT->s.
4640 IT->string_nchars is the maximum number of characters to return
4641 from the string. IT->end_charpos may be greater than
4642 IT->string_nchars when this function is called, in which case we
4643 may have to return padding spaces. Value is zero if end of string
4644 reached, including padding spaces. */
4645
4646 static int
4647 next_element_from_c_string (it)
4648 struct it *it;
4649 {
4650 int success_p = 1;
4651
4652 xassert (it->s);
4653 it->what = IT_CHARACTER;
4654 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4655 it->object = Qnil;
4656
4657 /* IT's position can be greater IT->string_nchars in case a field
4658 width or precision has been specified when the iterator was
4659 initialized. */
4660 if (IT_CHARPOS (*it) >= it->end_charpos)
4661 {
4662 /* End of the game. */
4663 it->what = IT_EOB;
4664 success_p = 0;
4665 }
4666 else if (IT_CHARPOS (*it) >= it->string_nchars)
4667 {
4668 /* Pad with spaces. */
4669 it->c = ' ', it->len = 1;
4670 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4671 }
4672 else if (it->multibyte_p)
4673 {
4674 /* Implementation note: The calls to strlen apparently aren't a
4675 performance problem because there is no noticeable performance
4676 difference between Emacs running in unibyte or multibyte mode. */
4677 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4678 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4679 maxlen, &it->len);
4680 }
4681 else
4682 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4683
4684 return success_p;
4685 }
4686
4687
4688 /* Set up IT to return characters from an ellipsis, if appropriate.
4689 The definition of the ellipsis glyphs may come from a display table
4690 entry. This function Fills IT with the first glyph from the
4691 ellipsis if an ellipsis is to be displayed. */
4692
4693 static int
4694 next_element_from_ellipsis (it)
4695 struct it *it;
4696 {
4697 if (it->selective_display_ellipsis_p)
4698 {
4699 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4700 {
4701 /* Use the display table definition for `...'. Invalid glyphs
4702 will be handled by the method returning elements from dpvec. */
4703 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4704 it->dpvec_char_len = it->len;
4705 it->dpvec = v->contents;
4706 it->dpend = v->contents + v->size;
4707 it->current.dpvec_index = 0;
4708 it->method = next_element_from_display_vector;
4709 }
4710 else
4711 {
4712 /* Use default `...' which is stored in default_invis_vector. */
4713 it->dpvec_char_len = it->len;
4714 it->dpvec = default_invis_vector;
4715 it->dpend = default_invis_vector + 3;
4716 it->current.dpvec_index = 0;
4717 it->method = next_element_from_display_vector;
4718 }
4719 }
4720 else
4721 {
4722 /* The face at the current position may be different from the
4723 face we find after the invisible text. Remember what it
4724 was in IT->saved_face_id, and signal that it's there by
4725 setting face_before_selective_p. */
4726 it->saved_face_id = it->face_id;
4727 it->method = next_element_from_buffer;
4728 reseat_at_next_visible_line_start (it, 1);
4729 it->face_before_selective_p = 1;
4730 }
4731
4732 return get_next_display_element (it);
4733 }
4734
4735
4736 /* Deliver an image display element. The iterator IT is already
4737 filled with image information (done in handle_display_prop). Value
4738 is always 1. */
4739
4740
4741 static int
4742 next_element_from_image (it)
4743 struct it *it;
4744 {
4745 it->what = IT_IMAGE;
4746 return 1;
4747 }
4748
4749
4750 /* Fill iterator IT with next display element from a stretch glyph
4751 property. IT->object is the value of the text property. Value is
4752 always 1. */
4753
4754 static int
4755 next_element_from_stretch (it)
4756 struct it *it;
4757 {
4758 it->what = IT_STRETCH;
4759 return 1;
4760 }
4761
4762
4763 /* Load IT with the next display element from current_buffer. Value
4764 is zero if end of buffer reached. IT->stop_charpos is the next
4765 position at which to stop and check for text properties or buffer
4766 end. */
4767
4768 static int
4769 next_element_from_buffer (it)
4770 struct it *it;
4771 {
4772 int success_p = 1;
4773
4774 /* Check this assumption, otherwise, we would never enter the
4775 if-statement, below. */
4776 xassert (IT_CHARPOS (*it) >= BEGV
4777 && IT_CHARPOS (*it) <= it->stop_charpos);
4778
4779 if (IT_CHARPOS (*it) >= it->stop_charpos)
4780 {
4781 if (IT_CHARPOS (*it) >= it->end_charpos)
4782 {
4783 int overlay_strings_follow_p;
4784
4785 /* End of the game, except when overlay strings follow that
4786 haven't been returned yet. */
4787 if (it->overlay_strings_at_end_processed_p)
4788 overlay_strings_follow_p = 0;
4789 else
4790 {
4791 it->overlay_strings_at_end_processed_p = 1;
4792 overlay_strings_follow_p = get_overlay_strings (it, 0);
4793 }
4794
4795 if (overlay_strings_follow_p)
4796 success_p = get_next_display_element (it);
4797 else
4798 {
4799 it->what = IT_EOB;
4800 it->position = it->current.pos;
4801 success_p = 0;
4802 }
4803 }
4804 else
4805 {
4806 handle_stop (it);
4807 return get_next_display_element (it);
4808 }
4809 }
4810 else
4811 {
4812 /* No face changes, overlays etc. in sight, so just return a
4813 character from current_buffer. */
4814 unsigned char *p;
4815
4816 /* Maybe run the redisplay end trigger hook. Performance note:
4817 This doesn't seem to cost measurable time. */
4818 if (it->redisplay_end_trigger_charpos
4819 && it->glyph_row
4820 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4821 run_redisplay_end_trigger_hook (it);
4822
4823 /* Get the next character, maybe multibyte. */
4824 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4825 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4826 {
4827 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4828 - IT_BYTEPOS (*it));
4829 it->c = string_char_and_length (p, maxlen, &it->len);
4830 }
4831 else
4832 it->c = *p, it->len = 1;
4833
4834 /* Record what we have and where it came from. */
4835 it->what = IT_CHARACTER;;
4836 it->object = it->w->buffer;
4837 it->position = it->current.pos;
4838
4839 /* Normally we return the character found above, except when we
4840 really want to return an ellipsis for selective display. */
4841 if (it->selective)
4842 {
4843 if (it->c == '\n')
4844 {
4845 /* A value of selective > 0 means hide lines indented more
4846 than that number of columns. */
4847 if (it->selective > 0
4848 && IT_CHARPOS (*it) + 1 < ZV
4849 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4850 IT_BYTEPOS (*it) + 1,
4851 it->selective))
4852 {
4853 success_p = next_element_from_ellipsis (it);
4854 it->dpvec_char_len = -1;
4855 }
4856 }
4857 else if (it->c == '\r' && it->selective == -1)
4858 {
4859 /* A value of selective == -1 means that everything from the
4860 CR to the end of the line is invisible, with maybe an
4861 ellipsis displayed for it. */
4862 success_p = next_element_from_ellipsis (it);
4863 it->dpvec_char_len = -1;
4864 }
4865 }
4866 }
4867
4868 /* Value is zero if end of buffer reached. */
4869 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4870 return success_p;
4871 }
4872
4873
4874 /* Run the redisplay end trigger hook for IT. */
4875
4876 static void
4877 run_redisplay_end_trigger_hook (it)
4878 struct it *it;
4879 {
4880 Lisp_Object args[3];
4881
4882 /* IT->glyph_row should be non-null, i.e. we should be actually
4883 displaying something, or otherwise we should not run the hook. */
4884 xassert (it->glyph_row);
4885
4886 /* Set up hook arguments. */
4887 args[0] = Qredisplay_end_trigger_functions;
4888 args[1] = it->window;
4889 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4890 it->redisplay_end_trigger_charpos = 0;
4891
4892 /* Since we are *trying* to run these functions, don't try to run
4893 them again, even if they get an error. */
4894 it->w->redisplay_end_trigger = Qnil;
4895 Frun_hook_with_args (3, args);
4896
4897 /* Notice if it changed the face of the character we are on. */
4898 handle_face_prop (it);
4899 }
4900
4901
4902 /* Deliver a composition display element. The iterator IT is already
4903 filled with composition information (done in
4904 handle_composition_prop). Value is always 1. */
4905
4906 static int
4907 next_element_from_composition (it)
4908 struct it *it;
4909 {
4910 it->what = IT_COMPOSITION;
4911 it->position = (STRINGP (it->string)
4912 ? it->current.string_pos
4913 : it->current.pos);
4914 return 1;
4915 }
4916
4917
4918 \f
4919 /***********************************************************************
4920 Moving an iterator without producing glyphs
4921 ***********************************************************************/
4922
4923 /* Move iterator IT to a specified buffer or X position within one
4924 line on the display without producing glyphs.
4925
4926 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4927 whichever is reached first.
4928
4929 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4930
4931 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4932 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4933 TO_X includes the amount by which a window is horizontally
4934 scrolled.
4935
4936 Value is
4937
4938 MOVE_POS_MATCH_OR_ZV
4939 - when TO_POS or ZV was reached.
4940
4941 MOVE_X_REACHED
4942 -when TO_X was reached before TO_POS or ZV were reached.
4943
4944 MOVE_LINE_CONTINUED
4945 - when we reached the end of the display area and the line must
4946 be continued.
4947
4948 MOVE_LINE_TRUNCATED
4949 - when we reached the end of the display area and the line is
4950 truncated.
4951
4952 MOVE_NEWLINE_OR_CR
4953 - when we stopped at a line end, i.e. a newline or a CR and selective
4954 display is on. */
4955
4956 static enum move_it_result
4957 move_it_in_display_line_to (it, to_charpos, to_x, op)
4958 struct it *it;
4959 int to_charpos, to_x, op;
4960 {
4961 enum move_it_result result = MOVE_UNDEFINED;
4962 struct glyph_row *saved_glyph_row;
4963
4964 /* Don't produce glyphs in produce_glyphs. */
4965 saved_glyph_row = it->glyph_row;
4966 it->glyph_row = NULL;
4967
4968 while (1)
4969 {
4970 int x, i, ascent = 0, descent = 0;
4971
4972 /* Stop when ZV or TO_CHARPOS reached. */
4973 if (!get_next_display_element (it)
4974 || ((op & MOVE_TO_POS) != 0
4975 && BUFFERP (it->object)
4976 && IT_CHARPOS (*it) >= to_charpos))
4977 {
4978 result = MOVE_POS_MATCH_OR_ZV;
4979 break;
4980 }
4981
4982 /* The call to produce_glyphs will get the metrics of the
4983 display element IT is loaded with. We record in x the
4984 x-position before this display element in case it does not
4985 fit on the line. */
4986 x = it->current_x;
4987
4988 /* Remember the line height so far in case the next element doesn't
4989 fit on the line. */
4990 if (!it->truncate_lines_p)
4991 {
4992 ascent = it->max_ascent;
4993 descent = it->max_descent;
4994 }
4995
4996 PRODUCE_GLYPHS (it);
4997
4998 if (it->area != TEXT_AREA)
4999 {
5000 set_iterator_to_next (it, 1);
5001 continue;
5002 }
5003
5004 /* The number of glyphs we get back in IT->nglyphs will normally
5005 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5006 character on a terminal frame, or (iii) a line end. For the
5007 second case, IT->nglyphs - 1 padding glyphs will be present
5008 (on X frames, there is only one glyph produced for a
5009 composite character.
5010
5011 The behavior implemented below means, for continuation lines,
5012 that as many spaces of a TAB as fit on the current line are
5013 displayed there. For terminal frames, as many glyphs of a
5014 multi-glyph character are displayed in the current line, too.
5015 This is what the old redisplay code did, and we keep it that
5016 way. Under X, the whole shape of a complex character must
5017 fit on the line or it will be completely displayed in the
5018 next line.
5019
5020 Note that both for tabs and padding glyphs, all glyphs have
5021 the same width. */
5022 if (it->nglyphs)
5023 {
5024 /* More than one glyph or glyph doesn't fit on line. All
5025 glyphs have the same width. */
5026 int single_glyph_width = it->pixel_width / it->nglyphs;
5027 int new_x;
5028
5029 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5030 {
5031 new_x = x + single_glyph_width;
5032
5033 /* We want to leave anything reaching TO_X to the caller. */
5034 if ((op & MOVE_TO_X) && new_x > to_x)
5035 {
5036 it->current_x = x;
5037 result = MOVE_X_REACHED;
5038 break;
5039 }
5040 else if (/* Lines are continued. */
5041 !it->truncate_lines_p
5042 && (/* And glyph doesn't fit on the line. */
5043 new_x > it->last_visible_x
5044 /* Or it fits exactly and we're on a window
5045 system frame. */
5046 || (new_x == it->last_visible_x
5047 && FRAME_WINDOW_P (it->f))))
5048 {
5049 if (/* IT->hpos == 0 means the very first glyph
5050 doesn't fit on the line, e.g. a wide image. */
5051 it->hpos == 0
5052 || (new_x == it->last_visible_x
5053 && FRAME_WINDOW_P (it->f)))
5054 {
5055 ++it->hpos;
5056 it->current_x = new_x;
5057 if (i == it->nglyphs - 1)
5058 set_iterator_to_next (it, 1);
5059 }
5060 else
5061 {
5062 it->current_x = x;
5063 it->max_ascent = ascent;
5064 it->max_descent = descent;
5065 }
5066
5067 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5068 IT_CHARPOS (*it)));
5069 result = MOVE_LINE_CONTINUED;
5070 break;
5071 }
5072 else if (new_x > it->first_visible_x)
5073 {
5074 /* Glyph is visible. Increment number of glyphs that
5075 would be displayed. */
5076 ++it->hpos;
5077 }
5078 else
5079 {
5080 /* Glyph is completely off the left margin of the display
5081 area. Nothing to do. */
5082 }
5083 }
5084
5085 if (result != MOVE_UNDEFINED)
5086 break;
5087 }
5088 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5089 {
5090 /* Stop when TO_X specified and reached. This check is
5091 necessary here because of lines consisting of a line end,
5092 only. The line end will not produce any glyphs and we
5093 would never get MOVE_X_REACHED. */
5094 xassert (it->nglyphs == 0);
5095 result = MOVE_X_REACHED;
5096 break;
5097 }
5098
5099 /* Is this a line end? If yes, we're done. */
5100 if (ITERATOR_AT_END_OF_LINE_P (it))
5101 {
5102 result = MOVE_NEWLINE_OR_CR;
5103 break;
5104 }
5105
5106 /* The current display element has been consumed. Advance
5107 to the next. */
5108 set_iterator_to_next (it, 1);
5109
5110 /* Stop if lines are truncated and IT's current x-position is
5111 past the right edge of the window now. */
5112 if (it->truncate_lines_p
5113 && it->current_x >= it->last_visible_x)
5114 {
5115 result = MOVE_LINE_TRUNCATED;
5116 break;
5117 }
5118 }
5119
5120 /* Restore the iterator settings altered at the beginning of this
5121 function. */
5122 it->glyph_row = saved_glyph_row;
5123 return result;
5124 }
5125
5126
5127 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5128 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5129 the description of enum move_operation_enum.
5130
5131 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5132 screen line, this function will set IT to the next position >
5133 TO_CHARPOS. */
5134
5135 void
5136 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5137 struct it *it;
5138 int to_charpos, to_x, to_y, to_vpos;
5139 int op;
5140 {
5141 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5142 int line_height;
5143 int reached = 0;
5144
5145 for (;;)
5146 {
5147 if (op & MOVE_TO_VPOS)
5148 {
5149 /* If no TO_CHARPOS and no TO_X specified, stop at the
5150 start of the line TO_VPOS. */
5151 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5152 {
5153 if (it->vpos == to_vpos)
5154 {
5155 reached = 1;
5156 break;
5157 }
5158 else
5159 skip = move_it_in_display_line_to (it, -1, -1, 0);
5160 }
5161 else
5162 {
5163 /* TO_VPOS >= 0 means stop at TO_X in the line at
5164 TO_VPOS, or at TO_POS, whichever comes first. */
5165 if (it->vpos == to_vpos)
5166 {
5167 reached = 2;
5168 break;
5169 }
5170
5171 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5172
5173 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5174 {
5175 reached = 3;
5176 break;
5177 }
5178 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5179 {
5180 /* We have reached TO_X but not in the line we want. */
5181 skip = move_it_in_display_line_to (it, to_charpos,
5182 -1, MOVE_TO_POS);
5183 if (skip == MOVE_POS_MATCH_OR_ZV)
5184 {
5185 reached = 4;
5186 break;
5187 }
5188 }
5189 }
5190 }
5191 else if (op & MOVE_TO_Y)
5192 {
5193 struct it it_backup;
5194
5195 /* TO_Y specified means stop at TO_X in the line containing
5196 TO_Y---or at TO_CHARPOS if this is reached first. The
5197 problem is that we can't really tell whether the line
5198 contains TO_Y before we have completely scanned it, and
5199 this may skip past TO_X. What we do is to first scan to
5200 TO_X.
5201
5202 If TO_X is not specified, use a TO_X of zero. The reason
5203 is to make the outcome of this function more predictable.
5204 If we didn't use TO_X == 0, we would stop at the end of
5205 the line which is probably not what a caller would expect
5206 to happen. */
5207 skip = move_it_in_display_line_to (it, to_charpos,
5208 ((op & MOVE_TO_X)
5209 ? to_x : 0),
5210 (MOVE_TO_X
5211 | (op & MOVE_TO_POS)));
5212
5213 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5214 if (skip == MOVE_POS_MATCH_OR_ZV)
5215 {
5216 reached = 5;
5217 break;
5218 }
5219
5220 /* If TO_X was reached, we would like to know whether TO_Y
5221 is in the line. This can only be said if we know the
5222 total line height which requires us to scan the rest of
5223 the line. */
5224 if (skip == MOVE_X_REACHED)
5225 {
5226 it_backup = *it;
5227 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5228 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5229 op & MOVE_TO_POS);
5230 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5231 }
5232
5233 /* Now, decide whether TO_Y is in this line. */
5234 line_height = it->max_ascent + it->max_descent;
5235 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5236
5237 if (to_y >= it->current_y
5238 && to_y < it->current_y + line_height)
5239 {
5240 if (skip == MOVE_X_REACHED)
5241 /* If TO_Y is in this line and TO_X was reached above,
5242 we scanned too far. We have to restore IT's settings
5243 to the ones before skipping. */
5244 *it = it_backup;
5245 reached = 6;
5246 }
5247 else if (skip == MOVE_X_REACHED)
5248 {
5249 skip = skip2;
5250 if (skip == MOVE_POS_MATCH_OR_ZV)
5251 reached = 7;
5252 }
5253
5254 if (reached)
5255 break;
5256 }
5257 else
5258 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5259
5260 switch (skip)
5261 {
5262 case MOVE_POS_MATCH_OR_ZV:
5263 reached = 8;
5264 goto out;
5265
5266 case MOVE_NEWLINE_OR_CR:
5267 set_iterator_to_next (it, 1);
5268 it->continuation_lines_width = 0;
5269 break;
5270
5271 case MOVE_LINE_TRUNCATED:
5272 it->continuation_lines_width = 0;
5273 reseat_at_next_visible_line_start (it, 0);
5274 if ((op & MOVE_TO_POS) != 0
5275 && IT_CHARPOS (*it) > to_charpos)
5276 {
5277 reached = 9;
5278 goto out;
5279 }
5280 break;
5281
5282 case MOVE_LINE_CONTINUED:
5283 it->continuation_lines_width += it->current_x;
5284 break;
5285
5286 default:
5287 abort ();
5288 }
5289
5290 /* Reset/increment for the next run. */
5291 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5292 it->current_x = it->hpos = 0;
5293 it->current_y += it->max_ascent + it->max_descent;
5294 ++it->vpos;
5295 last_height = it->max_ascent + it->max_descent;
5296 last_max_ascent = it->max_ascent;
5297 it->max_ascent = it->max_descent = 0;
5298 }
5299
5300 out:
5301
5302 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5303 }
5304
5305
5306 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5307
5308 If DY > 0, move IT backward at least that many pixels. DY = 0
5309 means move IT backward to the preceding line start or BEGV. This
5310 function may move over more than DY pixels if IT->current_y - DY
5311 ends up in the middle of a line; in this case IT->current_y will be
5312 set to the top of the line moved to. */
5313
5314 void
5315 move_it_vertically_backward (it, dy)
5316 struct it *it;
5317 int dy;
5318 {
5319 int nlines, h, line_height;
5320 struct it it2;
5321 int start_pos = IT_CHARPOS (*it);
5322
5323 xassert (dy >= 0);
5324
5325 /* Estimate how many newlines we must move back. */
5326 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5327
5328 /* Set the iterator's position that many lines back. */
5329 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5330 back_to_previous_visible_line_start (it);
5331
5332 /* Reseat the iterator here. When moving backward, we don't want
5333 reseat to skip forward over invisible text, set up the iterator
5334 to deliver from overlay strings at the new position etc. So,
5335 use reseat_1 here. */
5336 reseat_1 (it, it->current.pos, 1);
5337
5338 /* We are now surely at a line start. */
5339 it->current_x = it->hpos = 0;
5340
5341 /* Move forward and see what y-distance we moved. First move to the
5342 start of the next line so that we get its height. We need this
5343 height to be able to tell whether we reached the specified
5344 y-distance. */
5345 it2 = *it;
5346 it2.max_ascent = it2.max_descent = 0;
5347 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5348 MOVE_TO_POS | MOVE_TO_VPOS);
5349 xassert (IT_CHARPOS (*it) >= BEGV);
5350 line_height = it2.max_ascent + it2.max_descent;
5351
5352 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5353 xassert (IT_CHARPOS (*it) >= BEGV);
5354 h = it2.current_y - it->current_y;
5355 nlines = it2.vpos - it->vpos;
5356
5357 /* Correct IT's y and vpos position. */
5358 it->vpos -= nlines;
5359 it->current_y -= h;
5360
5361 if (dy == 0)
5362 {
5363 /* DY == 0 means move to the start of the screen line. The
5364 value of nlines is > 0 if continuation lines were involved. */
5365 if (nlines > 0)
5366 move_it_by_lines (it, nlines, 1);
5367 xassert (IT_CHARPOS (*it) <= start_pos);
5368 }
5369 else if (nlines)
5370 {
5371 /* The y-position we try to reach. Note that h has been
5372 subtracted in front of the if-statement. */
5373 int target_y = it->current_y + h - dy;
5374
5375 /* If we did not reach target_y, try to move further backward if
5376 we can. If we moved too far backward, try to move forward. */
5377 if (target_y < it->current_y
5378 && IT_CHARPOS (*it) > BEGV)
5379 {
5380 move_it_vertically (it, target_y - it->current_y);
5381 xassert (IT_CHARPOS (*it) >= BEGV);
5382 }
5383 else if (target_y >= it->current_y + line_height
5384 && IT_CHARPOS (*it) < ZV)
5385 {
5386 move_it_vertically (it, target_y - (it->current_y + line_height));
5387 xassert (IT_CHARPOS (*it) >= BEGV);
5388 }
5389 }
5390 }
5391
5392
5393 /* Move IT by a specified amount of pixel lines DY. DY negative means
5394 move backwards. DY = 0 means move to start of screen line. At the
5395 end, IT will be on the start of a screen line. */
5396
5397 void
5398 move_it_vertically (it, dy)
5399 struct it *it;
5400 int dy;
5401 {
5402 if (dy <= 0)
5403 move_it_vertically_backward (it, -dy);
5404 else if (dy > 0)
5405 {
5406 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5407 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5408 MOVE_TO_POS | MOVE_TO_Y);
5409 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5410
5411 /* If buffer ends in ZV without a newline, move to the start of
5412 the line to satisfy the post-condition. */
5413 if (IT_CHARPOS (*it) == ZV
5414 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5415 move_it_by_lines (it, 0, 0);
5416 }
5417 }
5418
5419
5420 /* Move iterator IT past the end of the text line it is in. */
5421
5422 void
5423 move_it_past_eol (it)
5424 struct it *it;
5425 {
5426 enum move_it_result rc;
5427
5428 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5429 if (rc == MOVE_NEWLINE_OR_CR)
5430 set_iterator_to_next (it, 0);
5431 }
5432
5433
5434 #if 0 /* Currently not used. */
5435
5436 /* Return non-zero if some text between buffer positions START_CHARPOS
5437 and END_CHARPOS is invisible. IT->window is the window for text
5438 property lookup. */
5439
5440 static int
5441 invisible_text_between_p (it, start_charpos, end_charpos)
5442 struct it *it;
5443 int start_charpos, end_charpos;
5444 {
5445 Lisp_Object prop, limit;
5446 int invisible_found_p;
5447
5448 xassert (it != NULL && start_charpos <= end_charpos);
5449
5450 /* Is text at START invisible? */
5451 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5452 it->window);
5453 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5454 invisible_found_p = 1;
5455 else
5456 {
5457 limit = Fnext_single_char_property_change (make_number (start_charpos),
5458 Qinvisible, Qnil,
5459 make_number (end_charpos));
5460 invisible_found_p = XFASTINT (limit) < end_charpos;
5461 }
5462
5463 return invisible_found_p;
5464 }
5465
5466 #endif /* 0 */
5467
5468
5469 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5470 negative means move up. DVPOS == 0 means move to the start of the
5471 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5472 NEED_Y_P is zero, IT->current_y will be left unchanged.
5473
5474 Further optimization ideas: If we would know that IT->f doesn't use
5475 a face with proportional font, we could be faster for
5476 truncate-lines nil. */
5477
5478 void
5479 move_it_by_lines (it, dvpos, need_y_p)
5480 struct it *it;
5481 int dvpos, need_y_p;
5482 {
5483 struct position pos;
5484
5485 if (!FRAME_WINDOW_P (it->f))
5486 {
5487 struct text_pos textpos;
5488
5489 /* We can use vmotion on frames without proportional fonts. */
5490 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5491 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5492 reseat (it, textpos, 1);
5493 it->vpos += pos.vpos;
5494 it->current_y += pos.vpos;
5495 }
5496 else if (dvpos == 0)
5497 {
5498 /* DVPOS == 0 means move to the start of the screen line. */
5499 move_it_vertically_backward (it, 0);
5500 xassert (it->current_x == 0 && it->hpos == 0);
5501 }
5502 else if (dvpos > 0)
5503 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5504 else
5505 {
5506 struct it it2;
5507 int start_charpos, i;
5508
5509 /* Start at the beginning of the screen line containing IT's
5510 position. */
5511 move_it_vertically_backward (it, 0);
5512
5513 /* Go back -DVPOS visible lines and reseat the iterator there. */
5514 start_charpos = IT_CHARPOS (*it);
5515 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5516 back_to_previous_visible_line_start (it);
5517 reseat (it, it->current.pos, 1);
5518 it->current_x = it->hpos = 0;
5519
5520 /* Above call may have moved too far if continuation lines
5521 are involved. Scan forward and see if it did. */
5522 it2 = *it;
5523 it2.vpos = it2.current_y = 0;
5524 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5525 it->vpos -= it2.vpos;
5526 it->current_y -= it2.current_y;
5527 it->current_x = it->hpos = 0;
5528
5529 /* If we moved too far, move IT some lines forward. */
5530 if (it2.vpos > -dvpos)
5531 {
5532 int delta = it2.vpos + dvpos;
5533 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5534 }
5535 }
5536 }
5537
5538
5539 \f
5540 /***********************************************************************
5541 Messages
5542 ***********************************************************************/
5543
5544
5545 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5546 to *Messages*. */
5547
5548 void
5549 add_to_log (format, arg1, arg2)
5550 char *format;
5551 Lisp_Object arg1, arg2;
5552 {
5553 Lisp_Object args[3];
5554 Lisp_Object msg, fmt;
5555 char *buffer;
5556 int len;
5557 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5558
5559 /* Do nothing if called asynchronously. Inserting text into
5560 a buffer may call after-change-functions and alike and
5561 that would means running Lisp asynchronously. */
5562 if (handling_signal)
5563 return;
5564
5565 fmt = msg = Qnil;
5566 GCPRO4 (fmt, msg, arg1, arg2);
5567
5568 args[0] = fmt = build_string (format);
5569 args[1] = arg1;
5570 args[2] = arg2;
5571 msg = Fformat (3, args);
5572
5573 len = STRING_BYTES (XSTRING (msg)) + 1;
5574 buffer = (char *) alloca (len);
5575 bcopy (XSTRING (msg)->data, buffer, len);
5576
5577 message_dolog (buffer, len - 1, 1, 0);
5578 UNGCPRO;
5579 }
5580
5581
5582 /* Output a newline in the *Messages* buffer if "needs" one. */
5583
5584 void
5585 message_log_maybe_newline ()
5586 {
5587 if (message_log_need_newline)
5588 message_dolog ("", 0, 1, 0);
5589 }
5590
5591
5592 /* Add a string M of length NBYTES to the message log, optionally
5593 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5594 nonzero, means interpret the contents of M as multibyte. This
5595 function calls low-level routines in order to bypass text property
5596 hooks, etc. which might not be safe to run. */
5597
5598 void
5599 message_dolog (m, nbytes, nlflag, multibyte)
5600 char *m;
5601 int nbytes, nlflag, multibyte;
5602 {
5603 if (!NILP (Vmessage_log_max))
5604 {
5605 struct buffer *oldbuf;
5606 Lisp_Object oldpoint, oldbegv, oldzv;
5607 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5608 int point_at_end = 0;
5609 int zv_at_end = 0;
5610 Lisp_Object old_deactivate_mark, tem;
5611 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5612
5613 old_deactivate_mark = Vdeactivate_mark;
5614 oldbuf = current_buffer;
5615 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5616 current_buffer->undo_list = Qt;
5617
5618 oldpoint = Fpoint_marker ();
5619 oldbegv = Fpoint_min_marker ();
5620 oldzv = Fpoint_max_marker ();
5621 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5622
5623 if (PT == Z)
5624 point_at_end = 1;
5625 if (ZV == Z)
5626 zv_at_end = 1;
5627
5628 BEGV = BEG;
5629 BEGV_BYTE = BEG_BYTE;
5630 ZV = Z;
5631 ZV_BYTE = Z_BYTE;
5632 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5633
5634 /* Insert the string--maybe converting multibyte to single byte
5635 or vice versa, so that all the text fits the buffer. */
5636 if (multibyte
5637 && NILP (current_buffer->enable_multibyte_characters))
5638 {
5639 int i, c, char_bytes;
5640 unsigned char work[1];
5641
5642 /* Convert a multibyte string to single-byte
5643 for the *Message* buffer. */
5644 for (i = 0; i < nbytes; i += nbytes)
5645 {
5646 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5647 work[0] = (SINGLE_BYTE_CHAR_P (c)
5648 ? c
5649 : multibyte_char_to_unibyte (c, Qnil));
5650 insert_1_both (work, 1, 1, 1, 0, 0);
5651 }
5652 }
5653 else if (! multibyte
5654 && ! NILP (current_buffer->enable_multibyte_characters))
5655 {
5656 int i, c, char_bytes;
5657 unsigned char *msg = (unsigned char *) m;
5658 unsigned char str[MAX_MULTIBYTE_LENGTH];
5659 /* Convert a single-byte string to multibyte
5660 for the *Message* buffer. */
5661 for (i = 0; i < nbytes; i++)
5662 {
5663 c = unibyte_char_to_multibyte (msg[i]);
5664 char_bytes = CHAR_STRING (c, str);
5665 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5666 }
5667 }
5668 else if (nbytes)
5669 insert_1 (m, nbytes, 1, 0, 0);
5670
5671 if (nlflag)
5672 {
5673 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5674 insert_1 ("\n", 1, 1, 0, 0);
5675
5676 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5677 this_bol = PT;
5678 this_bol_byte = PT_BYTE;
5679
5680 if (this_bol > BEG)
5681 {
5682 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5683 prev_bol = PT;
5684 prev_bol_byte = PT_BYTE;
5685
5686 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5687 this_bol, this_bol_byte);
5688 if (dup)
5689 {
5690 del_range_both (prev_bol, prev_bol_byte,
5691 this_bol, this_bol_byte, 0);
5692 if (dup > 1)
5693 {
5694 char dupstr[40];
5695 int duplen;
5696
5697 /* If you change this format, don't forget to also
5698 change message_log_check_duplicate. */
5699 sprintf (dupstr, " [%d times]", dup);
5700 duplen = strlen (dupstr);
5701 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5702 insert_1 (dupstr, duplen, 1, 0, 1);
5703 }
5704 }
5705 }
5706
5707 if (NATNUMP (Vmessage_log_max))
5708 {
5709 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5710 -XFASTINT (Vmessage_log_max) - 1, 0);
5711 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5712 }
5713 }
5714 BEGV = XMARKER (oldbegv)->charpos;
5715 BEGV_BYTE = marker_byte_position (oldbegv);
5716
5717 if (zv_at_end)
5718 {
5719 ZV = Z;
5720 ZV_BYTE = Z_BYTE;
5721 }
5722 else
5723 {
5724 ZV = XMARKER (oldzv)->charpos;
5725 ZV_BYTE = marker_byte_position (oldzv);
5726 }
5727
5728 if (point_at_end)
5729 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5730 else
5731 /* We can't do Fgoto_char (oldpoint) because it will run some
5732 Lisp code. */
5733 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5734 XMARKER (oldpoint)->bytepos);
5735
5736 UNGCPRO;
5737 free_marker (oldpoint);
5738 free_marker (oldbegv);
5739 free_marker (oldzv);
5740
5741 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5742 set_buffer_internal (oldbuf);
5743 if (NILP (tem))
5744 windows_or_buffers_changed = old_windows_or_buffers_changed;
5745 message_log_need_newline = !nlflag;
5746 Vdeactivate_mark = old_deactivate_mark;
5747 }
5748 }
5749
5750
5751 /* We are at the end of the buffer after just having inserted a newline.
5752 (Note: We depend on the fact we won't be crossing the gap.)
5753 Check to see if the most recent message looks a lot like the previous one.
5754 Return 0 if different, 1 if the new one should just replace it, or a
5755 value N > 1 if we should also append " [N times]". */
5756
5757 static int
5758 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5759 int prev_bol, this_bol;
5760 int prev_bol_byte, this_bol_byte;
5761 {
5762 int i;
5763 int len = Z_BYTE - 1 - this_bol_byte;
5764 int seen_dots = 0;
5765 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5766 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5767
5768 for (i = 0; i < len; i++)
5769 {
5770 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5771 seen_dots = 1;
5772 if (p1[i] != p2[i])
5773 return seen_dots;
5774 }
5775 p1 += len;
5776 if (*p1 == '\n')
5777 return 2;
5778 if (*p1++ == ' ' && *p1++ == '[')
5779 {
5780 int n = 0;
5781 while (*p1 >= '0' && *p1 <= '9')
5782 n = n * 10 + *p1++ - '0';
5783 if (strncmp (p1, " times]\n", 8) == 0)
5784 return n+1;
5785 }
5786 return 0;
5787 }
5788
5789
5790 /* Display an echo area message M with a specified length of NBYTES
5791 bytes. The string may include null characters. If M is 0, clear
5792 out any existing message, and let the mini-buffer text show
5793 through.
5794
5795 The buffer M must continue to exist until after the echo area gets
5796 cleared or some other message gets displayed there. This means do
5797 not pass text that is stored in a Lisp string; do not pass text in
5798 a buffer that was alloca'd. */
5799
5800 void
5801 message2 (m, nbytes, multibyte)
5802 char *m;
5803 int nbytes;
5804 int multibyte;
5805 {
5806 /* First flush out any partial line written with print. */
5807 message_log_maybe_newline ();
5808 if (m)
5809 message_dolog (m, nbytes, 1, multibyte);
5810 message2_nolog (m, nbytes, multibyte);
5811 }
5812
5813
5814 /* The non-logging counterpart of message2. */
5815
5816 void
5817 message2_nolog (m, nbytes, multibyte)
5818 char *m;
5819 int nbytes;
5820 {
5821 struct frame *sf = SELECTED_FRAME ();
5822 message_enable_multibyte = multibyte;
5823
5824 if (noninteractive)
5825 {
5826 if (noninteractive_need_newline)
5827 putc ('\n', stderr);
5828 noninteractive_need_newline = 0;
5829 if (m)
5830 fwrite (m, nbytes, 1, stderr);
5831 if (cursor_in_echo_area == 0)
5832 fprintf (stderr, "\n");
5833 fflush (stderr);
5834 }
5835 /* A null message buffer means that the frame hasn't really been
5836 initialized yet. Error messages get reported properly by
5837 cmd_error, so this must be just an informative message; toss it. */
5838 else if (INTERACTIVE
5839 && sf->glyphs_initialized_p
5840 && FRAME_MESSAGE_BUF (sf))
5841 {
5842 Lisp_Object mini_window;
5843 struct frame *f;
5844
5845 /* Get the frame containing the mini-buffer
5846 that the selected frame is using. */
5847 mini_window = FRAME_MINIBUF_WINDOW (sf);
5848 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5849
5850 FRAME_SAMPLE_VISIBILITY (f);
5851 if (FRAME_VISIBLE_P (sf)
5852 && ! FRAME_VISIBLE_P (f))
5853 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5854
5855 if (m)
5856 {
5857 set_message (m, Qnil, nbytes, multibyte);
5858 if (minibuffer_auto_raise)
5859 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5860 }
5861 else
5862 clear_message (1, 1);
5863
5864 do_pending_window_change (0);
5865 echo_area_display (1);
5866 do_pending_window_change (0);
5867 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5868 (*frame_up_to_date_hook) (f);
5869 }
5870 }
5871
5872
5873 /* Display an echo area message M with a specified length of NBYTES
5874 bytes. The string may include null characters. If M is not a
5875 string, clear out any existing message, and let the mini-buffer
5876 text show through. */
5877
5878 void
5879 message3 (m, nbytes, multibyte)
5880 Lisp_Object m;
5881 int nbytes;
5882 int multibyte;
5883 {
5884 struct gcpro gcpro1;
5885
5886 GCPRO1 (m);
5887
5888 /* First flush out any partial line written with print. */
5889 message_log_maybe_newline ();
5890 if (STRINGP (m))
5891 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5892 message3_nolog (m, nbytes, multibyte);
5893
5894 UNGCPRO;
5895 }
5896
5897
5898 /* The non-logging version of message3. */
5899
5900 void
5901 message3_nolog (m, nbytes, multibyte)
5902 Lisp_Object m;
5903 int nbytes, multibyte;
5904 {
5905 struct frame *sf = SELECTED_FRAME ();
5906 message_enable_multibyte = multibyte;
5907
5908 if (noninteractive)
5909 {
5910 if (noninteractive_need_newline)
5911 putc ('\n', stderr);
5912 noninteractive_need_newline = 0;
5913 if (STRINGP (m))
5914 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5915 if (cursor_in_echo_area == 0)
5916 fprintf (stderr, "\n");
5917 fflush (stderr);
5918 }
5919 /* A null message buffer means that the frame hasn't really been
5920 initialized yet. Error messages get reported properly by
5921 cmd_error, so this must be just an informative message; toss it. */
5922 else if (INTERACTIVE
5923 && sf->glyphs_initialized_p
5924 && FRAME_MESSAGE_BUF (sf))
5925 {
5926 Lisp_Object mini_window;
5927 Lisp_Object frame;
5928 struct frame *f;
5929
5930 /* Get the frame containing the mini-buffer
5931 that the selected frame is using. */
5932 mini_window = FRAME_MINIBUF_WINDOW (sf);
5933 frame = XWINDOW (mini_window)->frame;
5934 f = XFRAME (frame);
5935
5936 FRAME_SAMPLE_VISIBILITY (f);
5937 if (FRAME_VISIBLE_P (sf)
5938 && !FRAME_VISIBLE_P (f))
5939 Fmake_frame_visible (frame);
5940
5941 if (STRINGP (m) && XSTRING (m)->size)
5942 {
5943 set_message (NULL, m, nbytes, multibyte);
5944 if (minibuffer_auto_raise)
5945 Fraise_frame (frame);
5946 }
5947 else
5948 clear_message (1, 1);
5949
5950 do_pending_window_change (0);
5951 echo_area_display (1);
5952 do_pending_window_change (0);
5953 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5954 (*frame_up_to_date_hook) (f);
5955 }
5956 }
5957
5958
5959 /* Display a null-terminated echo area message M. If M is 0, clear
5960 out any existing message, and let the mini-buffer text show through.
5961
5962 The buffer M must continue to exist until after the echo area gets
5963 cleared or some other message gets displayed there. Do not pass
5964 text that is stored in a Lisp string. Do not pass text in a buffer
5965 that was alloca'd. */
5966
5967 void
5968 message1 (m)
5969 char *m;
5970 {
5971 message2 (m, (m ? strlen (m) : 0), 0);
5972 }
5973
5974
5975 /* The non-logging counterpart of message1. */
5976
5977 void
5978 message1_nolog (m)
5979 char *m;
5980 {
5981 message2_nolog (m, (m ? strlen (m) : 0), 0);
5982 }
5983
5984 /* Display a message M which contains a single %s
5985 which gets replaced with STRING. */
5986
5987 void
5988 message_with_string (m, string, log)
5989 char *m;
5990 Lisp_Object string;
5991 int log;
5992 {
5993 if (noninteractive)
5994 {
5995 if (m)
5996 {
5997 if (noninteractive_need_newline)
5998 putc ('\n', stderr);
5999 noninteractive_need_newline = 0;
6000 fprintf (stderr, m, XSTRING (string)->data);
6001 if (cursor_in_echo_area == 0)
6002 fprintf (stderr, "\n");
6003 fflush (stderr);
6004 }
6005 }
6006 else if (INTERACTIVE)
6007 {
6008 /* The frame whose minibuffer we're going to display the message on.
6009 It may be larger than the selected frame, so we need
6010 to use its buffer, not the selected frame's buffer. */
6011 Lisp_Object mini_window;
6012 struct frame *f, *sf = SELECTED_FRAME ();
6013
6014 /* Get the frame containing the minibuffer
6015 that the selected frame is using. */
6016 mini_window = FRAME_MINIBUF_WINDOW (sf);
6017 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6018
6019 /* A null message buffer means that the frame hasn't really been
6020 initialized yet. Error messages get reported properly by
6021 cmd_error, so this must be just an informative message; toss it. */
6022 if (FRAME_MESSAGE_BUF (f))
6023 {
6024 int len;
6025 char *a[1];
6026 a[0] = (char *) XSTRING (string)->data;
6027
6028 len = doprnt (FRAME_MESSAGE_BUF (f),
6029 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6030
6031 if (log)
6032 message2 (FRAME_MESSAGE_BUF (f), len,
6033 STRING_MULTIBYTE (string));
6034 else
6035 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6036 STRING_MULTIBYTE (string));
6037
6038 /* Print should start at the beginning of the message
6039 buffer next time. */
6040 message_buf_print = 0;
6041 }
6042 }
6043 }
6044
6045
6046 /* Dump an informative message to the minibuf. If M is 0, clear out
6047 any existing message, and let the mini-buffer text show through. */
6048
6049 /* VARARGS 1 */
6050 void
6051 message (m, a1, a2, a3)
6052 char *m;
6053 EMACS_INT a1, a2, a3;
6054 {
6055 if (noninteractive)
6056 {
6057 if (m)
6058 {
6059 if (noninteractive_need_newline)
6060 putc ('\n', stderr);
6061 noninteractive_need_newline = 0;
6062 fprintf (stderr, m, a1, a2, a3);
6063 if (cursor_in_echo_area == 0)
6064 fprintf (stderr, "\n");
6065 fflush (stderr);
6066 }
6067 }
6068 else if (INTERACTIVE)
6069 {
6070 /* The frame whose mini-buffer we're going to display the message
6071 on. It may be larger than the selected frame, so we need to
6072 use its buffer, not the selected frame's buffer. */
6073 Lisp_Object mini_window;
6074 struct frame *f, *sf = SELECTED_FRAME ();
6075
6076 /* Get the frame containing the mini-buffer
6077 that the selected frame is using. */
6078 mini_window = FRAME_MINIBUF_WINDOW (sf);
6079 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6080
6081 /* A null message buffer means that the frame hasn't really been
6082 initialized yet. Error messages get reported properly by
6083 cmd_error, so this must be just an informative message; toss
6084 it. */
6085 if (FRAME_MESSAGE_BUF (f))
6086 {
6087 if (m)
6088 {
6089 int len;
6090 #ifdef NO_ARG_ARRAY
6091 char *a[3];
6092 a[0] = (char *) a1;
6093 a[1] = (char *) a2;
6094 a[2] = (char *) a3;
6095
6096 len = doprnt (FRAME_MESSAGE_BUF (f),
6097 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6098 #else
6099 len = doprnt (FRAME_MESSAGE_BUF (f),
6100 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6101 (char **) &a1);
6102 #endif /* NO_ARG_ARRAY */
6103
6104 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6105 }
6106 else
6107 message1 (0);
6108
6109 /* Print should start at the beginning of the message
6110 buffer next time. */
6111 message_buf_print = 0;
6112 }
6113 }
6114 }
6115
6116
6117 /* The non-logging version of message. */
6118
6119 void
6120 message_nolog (m, a1, a2, a3)
6121 char *m;
6122 EMACS_INT a1, a2, a3;
6123 {
6124 Lisp_Object old_log_max;
6125 old_log_max = Vmessage_log_max;
6126 Vmessage_log_max = Qnil;
6127 message (m, a1, a2, a3);
6128 Vmessage_log_max = old_log_max;
6129 }
6130
6131
6132 /* Display the current message in the current mini-buffer. This is
6133 only called from error handlers in process.c, and is not time
6134 critical. */
6135
6136 void
6137 update_echo_area ()
6138 {
6139 if (!NILP (echo_area_buffer[0]))
6140 {
6141 Lisp_Object string;
6142 string = Fcurrent_message ();
6143 message3 (string, XSTRING (string)->size,
6144 !NILP (current_buffer->enable_multibyte_characters));
6145 }
6146 }
6147
6148
6149 /* Make sure echo area buffers in echo_buffers[] are life. If they
6150 aren't, make new ones. */
6151
6152 static void
6153 ensure_echo_area_buffers ()
6154 {
6155 int i;
6156
6157 for (i = 0; i < 2; ++i)
6158 if (!BUFFERP (echo_buffer[i])
6159 || NILP (XBUFFER (echo_buffer[i])->name))
6160 {
6161 char name[30];
6162 Lisp_Object old_buffer;
6163 int j;
6164
6165 old_buffer = echo_buffer[i];
6166 sprintf (name, " *Echo Area %d*", i);
6167 echo_buffer[i] = Fget_buffer_create (build_string (name));
6168 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6169
6170 for (j = 0; j < 2; ++j)
6171 if (EQ (old_buffer, echo_area_buffer[j]))
6172 echo_area_buffer[j] = echo_buffer[i];
6173 }
6174 }
6175
6176
6177 /* Call FN with args A1..A4 with either the current or last displayed
6178 echo_area_buffer as current buffer.
6179
6180 WHICH zero means use the current message buffer
6181 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6182 from echo_buffer[] and clear it.
6183
6184 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6185 suitable buffer from echo_buffer[] and clear it.
6186
6187 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6188 that the current message becomes the last displayed one, make
6189 choose a suitable buffer for echo_area_buffer[0], and clear it.
6190
6191 Value is what FN returns. */
6192
6193 static int
6194 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6195 struct window *w;
6196 int which;
6197 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6198 EMACS_INT a1;
6199 Lisp_Object a2;
6200 EMACS_INT a3, a4;
6201 {
6202 Lisp_Object buffer;
6203 int this_one, the_other, clear_buffer_p, rc;
6204 int count = BINDING_STACK_SIZE ();
6205
6206 /* If buffers aren't life, make new ones. */
6207 ensure_echo_area_buffers ();
6208
6209 clear_buffer_p = 0;
6210
6211 if (which == 0)
6212 this_one = 0, the_other = 1;
6213 else if (which > 0)
6214 this_one = 1, the_other = 0;
6215 else
6216 {
6217 this_one = 0, the_other = 1;
6218 clear_buffer_p = 1;
6219
6220 /* We need a fresh one in case the current echo buffer equals
6221 the one containing the last displayed echo area message. */
6222 if (!NILP (echo_area_buffer[this_one])
6223 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6224 echo_area_buffer[this_one] = Qnil;
6225 }
6226
6227 /* Choose a suitable buffer from echo_buffer[] is we don't
6228 have one. */
6229 if (NILP (echo_area_buffer[this_one]))
6230 {
6231 echo_area_buffer[this_one]
6232 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6233 ? echo_buffer[the_other]
6234 : echo_buffer[this_one]);
6235 clear_buffer_p = 1;
6236 }
6237
6238 buffer = echo_area_buffer[this_one];
6239
6240 /* Don't get confused by reusing the buffer used for echoing
6241 for a different purpose. */
6242 if (!echoing && EQ (buffer, echo_message_buffer))
6243 cancel_echoing ();
6244
6245 record_unwind_protect (unwind_with_echo_area_buffer,
6246 with_echo_area_buffer_unwind_data (w));
6247
6248 /* Make the echo area buffer current. Note that for display
6249 purposes, it is not necessary that the displayed window's buffer
6250 == current_buffer, except for text property lookup. So, let's
6251 only set that buffer temporarily here without doing a full
6252 Fset_window_buffer. We must also change w->pointm, though,
6253 because otherwise an assertions in unshow_buffer fails, and Emacs
6254 aborts. */
6255 set_buffer_internal_1 (XBUFFER (buffer));
6256 if (w)
6257 {
6258 w->buffer = buffer;
6259 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6260 }
6261
6262 current_buffer->undo_list = Qt;
6263 current_buffer->read_only = Qnil;
6264 specbind (Qinhibit_read_only, Qt);
6265
6266 if (clear_buffer_p && Z > BEG)
6267 del_range (BEG, Z);
6268
6269 xassert (BEGV >= BEG);
6270 xassert (ZV <= Z && ZV >= BEGV);
6271
6272 rc = fn (a1, a2, a3, a4);
6273
6274 xassert (BEGV >= BEG);
6275 xassert (ZV <= Z && ZV >= BEGV);
6276
6277 unbind_to (count, Qnil);
6278 return rc;
6279 }
6280
6281
6282 /* Save state that should be preserved around the call to the function
6283 FN called in with_echo_area_buffer. */
6284
6285 static Lisp_Object
6286 with_echo_area_buffer_unwind_data (w)
6287 struct window *w;
6288 {
6289 int i = 0;
6290 Lisp_Object vector;
6291
6292 /* Reduce consing by keeping one vector in
6293 Vwith_echo_area_save_vector. */
6294 vector = Vwith_echo_area_save_vector;
6295 Vwith_echo_area_save_vector = Qnil;
6296
6297 if (NILP (vector))
6298 vector = Fmake_vector (make_number (7), Qnil);
6299
6300 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6301 AREF (vector, i) = Vdeactivate_mark, ++i;
6302 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6303
6304 if (w)
6305 {
6306 XSETWINDOW (AREF (vector, i), w); ++i;
6307 AREF (vector, i) = w->buffer; ++i;
6308 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6309 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6310 }
6311 else
6312 {
6313 int end = i + 4;
6314 for (; i < end; ++i)
6315 AREF (vector, i) = Qnil;
6316 }
6317
6318 xassert (i == ASIZE (vector));
6319 return vector;
6320 }
6321
6322
6323 /* Restore global state from VECTOR which was created by
6324 with_echo_area_buffer_unwind_data. */
6325
6326 static Lisp_Object
6327 unwind_with_echo_area_buffer (vector)
6328 Lisp_Object vector;
6329 {
6330 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6331 Vdeactivate_mark = AREF (vector, 1);
6332 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6333
6334 if (WINDOWP (AREF (vector, 3)))
6335 {
6336 struct window *w;
6337 Lisp_Object buffer, charpos, bytepos;
6338
6339 w = XWINDOW (AREF (vector, 3));
6340 buffer = AREF (vector, 4);
6341 charpos = AREF (vector, 5);
6342 bytepos = AREF (vector, 6);
6343
6344 w->buffer = buffer;
6345 set_marker_both (w->pointm, buffer,
6346 XFASTINT (charpos), XFASTINT (bytepos));
6347 }
6348
6349 Vwith_echo_area_save_vector = vector;
6350 return Qnil;
6351 }
6352
6353
6354 /* Set up the echo area for use by print functions. MULTIBYTE_P
6355 non-zero means we will print multibyte. */
6356
6357 void
6358 setup_echo_area_for_printing (multibyte_p)
6359 int multibyte_p;
6360 {
6361 ensure_echo_area_buffers ();
6362
6363 if (!message_buf_print)
6364 {
6365 /* A message has been output since the last time we printed.
6366 Choose a fresh echo area buffer. */
6367 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6368 echo_area_buffer[0] = echo_buffer[1];
6369 else
6370 echo_area_buffer[0] = echo_buffer[0];
6371
6372 /* Switch to that buffer and clear it. */
6373 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6374 current_buffer->truncate_lines = Qnil;
6375
6376 if (Z > BEG)
6377 {
6378 int count = BINDING_STACK_SIZE ();
6379 specbind (Qinhibit_read_only, Qt);
6380 del_range (BEG, Z);
6381 unbind_to (count, Qnil);
6382 }
6383 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6384
6385 /* Set up the buffer for the multibyteness we need. */
6386 if (multibyte_p
6387 != !NILP (current_buffer->enable_multibyte_characters))
6388 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6389
6390 /* Raise the frame containing the echo area. */
6391 if (minibuffer_auto_raise)
6392 {
6393 struct frame *sf = SELECTED_FRAME ();
6394 Lisp_Object mini_window;
6395 mini_window = FRAME_MINIBUF_WINDOW (sf);
6396 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6397 }
6398
6399 message_log_maybe_newline ();
6400 message_buf_print = 1;
6401 }
6402 else
6403 {
6404 if (NILP (echo_area_buffer[0]))
6405 {
6406 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6407 echo_area_buffer[0] = echo_buffer[1];
6408 else
6409 echo_area_buffer[0] = echo_buffer[0];
6410 }
6411
6412 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6413 {
6414 /* Someone switched buffers between print requests. */
6415 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6416 current_buffer->truncate_lines = Qnil;
6417 }
6418 }
6419 }
6420
6421
6422 /* Display an echo area message in window W. Value is non-zero if W's
6423 height is changed. If display_last_displayed_message_p is
6424 non-zero, display the message that was last displayed, otherwise
6425 display the current message. */
6426
6427 static int
6428 display_echo_area (w)
6429 struct window *w;
6430 {
6431 int i, no_message_p, window_height_changed_p, count;
6432
6433 /* Temporarily disable garbage collections while displaying the echo
6434 area. This is done because a GC can print a message itself.
6435 That message would modify the echo area buffer's contents while a
6436 redisplay of the buffer is going on, and seriously confuse
6437 redisplay. */
6438 count = inhibit_garbage_collection ();
6439
6440 /* If there is no message, we must call display_echo_area_1
6441 nevertheless because it resizes the window. But we will have to
6442 reset the echo_area_buffer in question to nil at the end because
6443 with_echo_area_buffer will sets it to an empty buffer. */
6444 i = display_last_displayed_message_p ? 1 : 0;
6445 no_message_p = NILP (echo_area_buffer[i]);
6446
6447 window_height_changed_p
6448 = with_echo_area_buffer (w, display_last_displayed_message_p,
6449 display_echo_area_1,
6450 (EMACS_INT) w, Qnil, 0, 0);
6451
6452 if (no_message_p)
6453 echo_area_buffer[i] = Qnil;
6454
6455 unbind_to (count, Qnil);
6456 return window_height_changed_p;
6457 }
6458
6459
6460 /* Helper for display_echo_area. Display the current buffer which
6461 contains the current echo area message in window W, a mini-window,
6462 a pointer to which is passed in A1. A2..A4 are currently not used.
6463 Change the height of W so that all of the message is displayed.
6464 Value is non-zero if height of W was changed. */
6465
6466 static int
6467 display_echo_area_1 (a1, a2, a3, a4)
6468 EMACS_INT a1;
6469 Lisp_Object a2;
6470 EMACS_INT a3, a4;
6471 {
6472 struct window *w = (struct window *) a1;
6473 Lisp_Object window;
6474 struct text_pos start;
6475 int window_height_changed_p = 0;
6476
6477 /* Do this before displaying, so that we have a large enough glyph
6478 matrix for the display. */
6479 window_height_changed_p = resize_mini_window (w, 0);
6480
6481 /* Display. */
6482 clear_glyph_matrix (w->desired_matrix);
6483 XSETWINDOW (window, w);
6484 SET_TEXT_POS (start, BEG, BEG_BYTE);
6485 try_window (window, start);
6486
6487 return window_height_changed_p;
6488 }
6489
6490
6491 /* Resize the echo area window to exactly the size needed for the
6492 currently displayed message, if there is one. If a mini-buffer
6493 is active, don't shrink it. */
6494
6495 void
6496 resize_echo_area_exactly ()
6497 {
6498 if (BUFFERP (echo_area_buffer[0])
6499 && WINDOWP (echo_area_window))
6500 {
6501 struct window *w = XWINDOW (echo_area_window);
6502 int resized_p;
6503 Lisp_Object resize_exactly;
6504
6505 if (minibuf_level == 0)
6506 resize_exactly = Qt;
6507 else
6508 resize_exactly = Qnil;
6509
6510 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6511 (EMACS_INT) w, resize_exactly, 0, 0);
6512 if (resized_p)
6513 {
6514 ++windows_or_buffers_changed;
6515 ++update_mode_lines;
6516 redisplay_internal (0);
6517 }
6518 }
6519 }
6520
6521
6522 /* Callback function for with_echo_area_buffer, when used from
6523 resize_echo_area_exactly. A1 contains a pointer to the window to
6524 resize, EXACTLY non-nil means resize the mini-window exactly to the
6525 size of the text displayed. A3 and A4 are not used. Value is what
6526 resize_mini_window returns. */
6527
6528 static int
6529 resize_mini_window_1 (a1, exactly, a3, a4)
6530 EMACS_INT a1;
6531 Lisp_Object exactly;
6532 EMACS_INT a3, a4;
6533 {
6534 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6535 }
6536
6537
6538 /* Resize mini-window W to fit the size of its contents. EXACT:P
6539 means size the window exactly to the size needed. Otherwise, it's
6540 only enlarged until W's buffer is empty. Value is non-zero if
6541 the window height has been changed. */
6542
6543 int
6544 resize_mini_window (w, exact_p)
6545 struct window *w;
6546 int exact_p;
6547 {
6548 struct frame *f = XFRAME (w->frame);
6549 int window_height_changed_p = 0;
6550
6551 xassert (MINI_WINDOW_P (w));
6552
6553 /* Don't resize windows while redisplaying a window; it would
6554 confuse redisplay functions when the size of the window they are
6555 displaying changes from under them. Such a resizing can happen,
6556 for instance, when which-func prints a long message while
6557 we are running fontification-functions. We're running these
6558 functions with safe_call which binds inhibit-redisplay to t. */
6559 if (!NILP (Vinhibit_redisplay))
6560 return 0;
6561
6562 /* Nil means don't try to resize. */
6563 if (NILP (Vresize_mini_windows)
6564 || (FRAME_X_P (f) && f->output_data.x == NULL))
6565 return 0;
6566
6567 if (!FRAME_MINIBUF_ONLY_P (f))
6568 {
6569 struct it it;
6570 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6571 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6572 int height, max_height;
6573 int unit = CANON_Y_UNIT (f);
6574 struct text_pos start;
6575 struct buffer *old_current_buffer = NULL;
6576
6577 if (current_buffer != XBUFFER (w->buffer))
6578 {
6579 old_current_buffer = current_buffer;
6580 set_buffer_internal (XBUFFER (w->buffer));
6581 }
6582
6583 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6584
6585 /* Compute the max. number of lines specified by the user. */
6586 if (FLOATP (Vmax_mini_window_height))
6587 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6588 else if (INTEGERP (Vmax_mini_window_height))
6589 max_height = XINT (Vmax_mini_window_height);
6590 else
6591 max_height = total_height / 4;
6592
6593 /* Correct that max. height if it's bogus. */
6594 max_height = max (1, max_height);
6595 max_height = min (total_height, max_height);
6596
6597 /* Find out the height of the text in the window. */
6598 if (it.truncate_lines_p)
6599 height = 1;
6600 else
6601 {
6602 last_height = 0;
6603 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6604 if (it.max_ascent == 0 && it.max_descent == 0)
6605 height = it.current_y + last_height;
6606 else
6607 height = it.current_y + it.max_ascent + it.max_descent;
6608 height -= it.extra_line_spacing;
6609 height = (height + unit - 1) / unit;
6610 }
6611
6612 /* Compute a suitable window start. */
6613 if (height > max_height)
6614 {
6615 height = max_height;
6616 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6617 move_it_vertically_backward (&it, (height - 1) * unit);
6618 start = it.current.pos;
6619 }
6620 else
6621 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6622 SET_MARKER_FROM_TEXT_POS (w->start, start);
6623
6624 if (EQ (Vresize_mini_windows, Qgrow_only))
6625 {
6626 /* Let it grow only, until we display an empty message, in which
6627 case the window shrinks again. */
6628 if (height > XFASTINT (w->height))
6629 {
6630 int old_height = XFASTINT (w->height);
6631 freeze_window_starts (f, 1);
6632 grow_mini_window (w, height - XFASTINT (w->height));
6633 window_height_changed_p = XFASTINT (w->height) != old_height;
6634 }
6635 else if (height < XFASTINT (w->height)
6636 && (exact_p || BEGV == ZV))
6637 {
6638 int old_height = XFASTINT (w->height);
6639 freeze_window_starts (f, 0);
6640 shrink_mini_window (w);
6641 window_height_changed_p = XFASTINT (w->height) != old_height;
6642 }
6643 }
6644 else
6645 {
6646 /* Always resize to exact size needed. */
6647 if (height > XFASTINT (w->height))
6648 {
6649 int old_height = XFASTINT (w->height);
6650 freeze_window_starts (f, 1);
6651 grow_mini_window (w, height - XFASTINT (w->height));
6652 window_height_changed_p = XFASTINT (w->height) != old_height;
6653 }
6654 else if (height < XFASTINT (w->height))
6655 {
6656 int old_height = XFASTINT (w->height);
6657 freeze_window_starts (f, 0);
6658 shrink_mini_window (w);
6659
6660 if (height)
6661 {
6662 freeze_window_starts (f, 1);
6663 grow_mini_window (w, height - XFASTINT (w->height));
6664 }
6665
6666 window_height_changed_p = XFASTINT (w->height) != old_height;
6667 }
6668 }
6669
6670 if (old_current_buffer)
6671 set_buffer_internal (old_current_buffer);
6672 }
6673
6674 return window_height_changed_p;
6675 }
6676
6677
6678 /* Value is the current message, a string, or nil if there is no
6679 current message. */
6680
6681 Lisp_Object
6682 current_message ()
6683 {
6684 Lisp_Object msg;
6685
6686 if (NILP (echo_area_buffer[0]))
6687 msg = Qnil;
6688 else
6689 {
6690 with_echo_area_buffer (0, 0, current_message_1,
6691 (EMACS_INT) &msg, Qnil, 0, 0);
6692 if (NILP (msg))
6693 echo_area_buffer[0] = Qnil;
6694 }
6695
6696 return msg;
6697 }
6698
6699
6700 static int
6701 current_message_1 (a1, a2, a3, a4)
6702 EMACS_INT a1;
6703 Lisp_Object a2;
6704 EMACS_INT a3, a4;
6705 {
6706 Lisp_Object *msg = (Lisp_Object *) a1;
6707
6708 if (Z > BEG)
6709 *msg = make_buffer_string (BEG, Z, 1);
6710 else
6711 *msg = Qnil;
6712 return 0;
6713 }
6714
6715
6716 /* Push the current message on Vmessage_stack for later restauration
6717 by restore_message. Value is non-zero if the current message isn't
6718 empty. This is a relatively infrequent operation, so it's not
6719 worth optimizing. */
6720
6721 int
6722 push_message ()
6723 {
6724 Lisp_Object msg;
6725 msg = current_message ();
6726 Vmessage_stack = Fcons (msg, Vmessage_stack);
6727 return STRINGP (msg);
6728 }
6729
6730
6731 /* Handler for record_unwind_protect calling pop_message. */
6732
6733 Lisp_Object
6734 push_message_unwind (dummy)
6735 Lisp_Object dummy;
6736 {
6737 pop_message ();
6738 return Qnil;
6739 }
6740
6741
6742 /* Restore message display from the top of Vmessage_stack. */
6743
6744 void
6745 restore_message ()
6746 {
6747 Lisp_Object msg;
6748
6749 xassert (CONSP (Vmessage_stack));
6750 msg = XCAR (Vmessage_stack);
6751 if (STRINGP (msg))
6752 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6753 else
6754 message3_nolog (msg, 0, 0);
6755 }
6756
6757
6758 /* Pop the top-most entry off Vmessage_stack. */
6759
6760 void
6761 pop_message ()
6762 {
6763 xassert (CONSP (Vmessage_stack));
6764 Vmessage_stack = XCDR (Vmessage_stack);
6765 }
6766
6767
6768 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6769 exits. If the stack is not empty, we have a missing pop_message
6770 somewhere. */
6771
6772 void
6773 check_message_stack ()
6774 {
6775 if (!NILP (Vmessage_stack))
6776 abort ();
6777 }
6778
6779
6780 /* Truncate to NCHARS what will be displayed in the echo area the next
6781 time we display it---but don't redisplay it now. */
6782
6783 void
6784 truncate_echo_area (nchars)
6785 int nchars;
6786 {
6787 if (nchars == 0)
6788 echo_area_buffer[0] = Qnil;
6789 /* A null message buffer means that the frame hasn't really been
6790 initialized yet. Error messages get reported properly by
6791 cmd_error, so this must be just an informative message; toss it. */
6792 else if (!noninteractive
6793 && INTERACTIVE
6794 && !NILP (echo_area_buffer[0]))
6795 {
6796 struct frame *sf = SELECTED_FRAME ();
6797 if (FRAME_MESSAGE_BUF (sf))
6798 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6799 }
6800 }
6801
6802
6803 /* Helper function for truncate_echo_area. Truncate the current
6804 message to at most NCHARS characters. */
6805
6806 static int
6807 truncate_message_1 (nchars, a2, a3, a4)
6808 EMACS_INT nchars;
6809 Lisp_Object a2;
6810 EMACS_INT a3, a4;
6811 {
6812 if (BEG + nchars < Z)
6813 del_range (BEG + nchars, Z);
6814 if (Z == BEG)
6815 echo_area_buffer[0] = Qnil;
6816 return 0;
6817 }
6818
6819
6820 /* Set the current message to a substring of S or STRING.
6821
6822 If STRING is a Lisp string, set the message to the first NBYTES
6823 bytes from STRING. NBYTES zero means use the whole string. If
6824 STRING is multibyte, the message will be displayed multibyte.
6825
6826 If S is not null, set the message to the first LEN bytes of S. LEN
6827 zero means use the whole string. MULTIBYTE_P non-zero means S is
6828 multibyte. Display the message multibyte in that case. */
6829
6830 void
6831 set_message (s, string, nbytes, multibyte_p)
6832 char *s;
6833 Lisp_Object string;
6834 int nbytes;
6835 {
6836 message_enable_multibyte
6837 = ((s && multibyte_p)
6838 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6839
6840 with_echo_area_buffer (0, -1, set_message_1,
6841 (EMACS_INT) s, string, nbytes, multibyte_p);
6842 message_buf_print = 0;
6843 help_echo_showing_p = 0;
6844 }
6845
6846
6847 /* Helper function for set_message. Arguments have the same meaning
6848 as there, with A1 corresponding to S and A2 corresponding to STRING
6849 This function is called with the echo area buffer being
6850 current. */
6851
6852 static int
6853 set_message_1 (a1, a2, nbytes, multibyte_p)
6854 EMACS_INT a1;
6855 Lisp_Object a2;
6856 EMACS_INT nbytes, multibyte_p;
6857 {
6858 char *s = (char *) a1;
6859 Lisp_Object string = a2;
6860
6861 xassert (BEG == Z);
6862
6863 /* Change multibyteness of the echo buffer appropriately. */
6864 if (message_enable_multibyte
6865 != !NILP (current_buffer->enable_multibyte_characters))
6866 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6867
6868 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6869
6870 /* Insert new message at BEG. */
6871 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6872
6873 if (STRINGP (string))
6874 {
6875 int nchars;
6876
6877 if (nbytes == 0)
6878 nbytes = XSTRING (string)->size_byte;
6879 nchars = string_byte_to_char (string, nbytes);
6880
6881 /* This function takes care of single/multibyte conversion. We
6882 just have to ensure that the echo area buffer has the right
6883 setting of enable_multibyte_characters. */
6884 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6885 }
6886 else if (s)
6887 {
6888 if (nbytes == 0)
6889 nbytes = strlen (s);
6890
6891 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6892 {
6893 /* Convert from multi-byte to single-byte. */
6894 int i, c, n;
6895 unsigned char work[1];
6896
6897 /* Convert a multibyte string to single-byte. */
6898 for (i = 0; i < nbytes; i += n)
6899 {
6900 c = string_char_and_length (s + i, nbytes - i, &n);
6901 work[0] = (SINGLE_BYTE_CHAR_P (c)
6902 ? c
6903 : multibyte_char_to_unibyte (c, Qnil));
6904 insert_1_both (work, 1, 1, 1, 0, 0);
6905 }
6906 }
6907 else if (!multibyte_p
6908 && !NILP (current_buffer->enable_multibyte_characters))
6909 {
6910 /* Convert from single-byte to multi-byte. */
6911 int i, c, n;
6912 unsigned char *msg = (unsigned char *) s;
6913 unsigned char str[MAX_MULTIBYTE_LENGTH];
6914
6915 /* Convert a single-byte string to multibyte. */
6916 for (i = 0; i < nbytes; i++)
6917 {
6918 c = unibyte_char_to_multibyte (msg[i]);
6919 n = CHAR_STRING (c, str);
6920 insert_1_both (str, 1, n, 1, 0, 0);
6921 }
6922 }
6923 else
6924 insert_1 (s, nbytes, 1, 0, 0);
6925 }
6926
6927 return 0;
6928 }
6929
6930
6931 /* Clear messages. CURRENT_P non-zero means clear the current
6932 message. LAST_DISPLAYED_P non-zero means clear the message
6933 last displayed. */
6934
6935 void
6936 clear_message (current_p, last_displayed_p)
6937 int current_p, last_displayed_p;
6938 {
6939 if (current_p)
6940 {
6941 echo_area_buffer[0] = Qnil;
6942 message_cleared_p = 1;
6943 }
6944
6945 if (last_displayed_p)
6946 echo_area_buffer[1] = Qnil;
6947
6948 message_buf_print = 0;
6949 }
6950
6951 /* Clear garbaged frames.
6952
6953 This function is used where the old redisplay called
6954 redraw_garbaged_frames which in turn called redraw_frame which in
6955 turn called clear_frame. The call to clear_frame was a source of
6956 flickering. I believe a clear_frame is not necessary. It should
6957 suffice in the new redisplay to invalidate all current matrices,
6958 and ensure a complete redisplay of all windows. */
6959
6960 static void
6961 clear_garbaged_frames ()
6962 {
6963 if (frame_garbaged)
6964 {
6965 Lisp_Object tail, frame;
6966
6967 FOR_EACH_FRAME (tail, frame)
6968 {
6969 struct frame *f = XFRAME (frame);
6970
6971 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6972 {
6973 clear_current_matrices (f);
6974 f->garbaged = 0;
6975 }
6976 }
6977
6978 frame_garbaged = 0;
6979 ++windows_or_buffers_changed;
6980 }
6981 }
6982
6983
6984 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6985 is non-zero update selected_frame. Value is non-zero if the
6986 mini-windows height has been changed. */
6987
6988 static int
6989 echo_area_display (update_frame_p)
6990 int update_frame_p;
6991 {
6992 Lisp_Object mini_window;
6993 struct window *w;
6994 struct frame *f;
6995 int window_height_changed_p = 0;
6996 struct frame *sf = SELECTED_FRAME ();
6997
6998 mini_window = FRAME_MINIBUF_WINDOW (sf);
6999 w = XWINDOW (mini_window);
7000 f = XFRAME (WINDOW_FRAME (w));
7001
7002 /* Don't display if frame is invisible or not yet initialized. */
7003 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7004 return 0;
7005
7006 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7007 #ifndef macintosh
7008 #ifdef HAVE_WINDOW_SYSTEM
7009 /* When Emacs starts, selected_frame may be a visible terminal
7010 frame, even if we run under a window system. If we let this
7011 through, a message would be displayed on the terminal. */
7012 if (EQ (selected_frame, Vterminal_frame)
7013 && !NILP (Vwindow_system))
7014 return 0;
7015 #endif /* HAVE_WINDOW_SYSTEM */
7016 #endif
7017
7018 /* Redraw garbaged frames. */
7019 if (frame_garbaged)
7020 clear_garbaged_frames ();
7021
7022 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7023 {
7024 echo_area_window = mini_window;
7025 window_height_changed_p = display_echo_area (w);
7026 w->must_be_updated_p = 1;
7027
7028 /* Update the display, unless called from redisplay_internal.
7029 Also don't update the screen during redisplay itself. The
7030 update will happen at the end of redisplay, and an update
7031 here could cause confusion. */
7032 if (update_frame_p && !redisplaying_p)
7033 {
7034 int n = 0;
7035
7036 /* If the display update has been interrupted by pending
7037 input, update mode lines in the frame. Due to the
7038 pending input, it might have been that redisplay hasn't
7039 been called, so that mode lines above the echo area are
7040 garbaged. This looks odd, so we prevent it here. */
7041 if (!display_completed)
7042 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7043
7044 if (window_height_changed_p
7045 /* Don't do this if Emacs is shutting down. Redisplay
7046 needs to run hooks. */
7047 && !NILP (Vrun_hooks))
7048 {
7049 /* Must update other windows. Likewise as in other
7050 cases, don't let this update be interrupted by
7051 pending input. */
7052 int count = BINDING_STACK_SIZE ();
7053 specbind (Qredisplay_dont_pause, Qt);
7054 windows_or_buffers_changed = 1;
7055 redisplay_internal (0);
7056 unbind_to (count, Qnil);
7057 }
7058 else if (FRAME_WINDOW_P (f) && n == 0)
7059 {
7060 /* Window configuration is the same as before.
7061 Can do with a display update of the echo area,
7062 unless we displayed some mode lines. */
7063 update_single_window (w, 1);
7064 rif->flush_display (f);
7065 }
7066 else
7067 update_frame (f, 1, 1);
7068
7069 /* If cursor is in the echo area, make sure that the next
7070 redisplay displays the minibuffer, so that the cursor will
7071 be replaced with what the minibuffer wants. */
7072 if (cursor_in_echo_area)
7073 ++windows_or_buffers_changed;
7074 }
7075 }
7076 else if (!EQ (mini_window, selected_window))
7077 windows_or_buffers_changed++;
7078
7079 /* Last displayed message is now the current message. */
7080 echo_area_buffer[1] = echo_area_buffer[0];
7081
7082 /* Prevent redisplay optimization in redisplay_internal by resetting
7083 this_line_start_pos. This is done because the mini-buffer now
7084 displays the message instead of its buffer text. */
7085 if (EQ (mini_window, selected_window))
7086 CHARPOS (this_line_start_pos) = 0;
7087
7088 return window_height_changed_p;
7089 }
7090
7091
7092 \f
7093 /***********************************************************************
7094 Frame Titles
7095 ***********************************************************************/
7096
7097
7098 #ifdef HAVE_WINDOW_SYSTEM
7099
7100 /* A buffer for constructing frame titles in it; allocated from the
7101 heap in init_xdisp and resized as needed in store_frame_title_char. */
7102
7103 static char *frame_title_buf;
7104
7105 /* The buffer's end, and a current output position in it. */
7106
7107 static char *frame_title_buf_end;
7108 static char *frame_title_ptr;
7109
7110
7111 /* Store a single character C for the frame title in frame_title_buf.
7112 Re-allocate frame_title_buf if necessary. */
7113
7114 static void
7115 store_frame_title_char (c)
7116 char c;
7117 {
7118 /* If output position has reached the end of the allocated buffer,
7119 double the buffer's size. */
7120 if (frame_title_ptr == frame_title_buf_end)
7121 {
7122 int len = frame_title_ptr - frame_title_buf;
7123 int new_size = 2 * len * sizeof *frame_title_buf;
7124 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7125 frame_title_buf_end = frame_title_buf + new_size;
7126 frame_title_ptr = frame_title_buf + len;
7127 }
7128
7129 *frame_title_ptr++ = c;
7130 }
7131
7132
7133 /* Store part of a frame title in frame_title_buf, beginning at
7134 frame_title_ptr. STR is the string to store. Do not copy
7135 characters that yield more columns than PRECISION; PRECISION <= 0
7136 means copy the whole string. Pad with spaces until FIELD_WIDTH
7137 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7138 pad. Called from display_mode_element when it is used to build a
7139 frame title. */
7140
7141 static int
7142 store_frame_title (str, field_width, precision)
7143 unsigned char *str;
7144 int field_width, precision;
7145 {
7146 int n = 0;
7147 int dummy, nbytes, width;
7148
7149 /* Copy at most PRECISION chars from STR. */
7150 nbytes = strlen (str);
7151 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7152 while (nbytes--)
7153 store_frame_title_char (*str++);
7154
7155 /* Fill up with spaces until FIELD_WIDTH reached. */
7156 while (field_width > 0
7157 && n < field_width)
7158 {
7159 store_frame_title_char (' ');
7160 ++n;
7161 }
7162
7163 return n;
7164 }
7165
7166
7167 /* Set the title of FRAME, if it has changed. The title format is
7168 Vicon_title_format if FRAME is iconified, otherwise it is
7169 frame_title_format. */
7170
7171 static void
7172 x_consider_frame_title (frame)
7173 Lisp_Object frame;
7174 {
7175 struct frame *f = XFRAME (frame);
7176
7177 if (FRAME_WINDOW_P (f)
7178 || FRAME_MINIBUF_ONLY_P (f)
7179 || f->explicit_name)
7180 {
7181 /* Do we have more than one visible frame on this X display? */
7182 Lisp_Object tail;
7183 Lisp_Object fmt;
7184 struct buffer *obuf;
7185 int len;
7186 struct it it;
7187
7188 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7189 {
7190 struct frame *tf = XFRAME (XCAR (tail));
7191
7192 if (tf != f
7193 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7194 && !FRAME_MINIBUF_ONLY_P (tf)
7195 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7196 break;
7197 }
7198
7199 /* Set global variable indicating that multiple frames exist. */
7200 multiple_frames = CONSP (tail);
7201
7202 /* Switch to the buffer of selected window of the frame. Set up
7203 frame_title_ptr so that display_mode_element will output into it;
7204 then display the title. */
7205 obuf = current_buffer;
7206 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7207 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7208 frame_title_ptr = frame_title_buf;
7209 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7210 NULL, DEFAULT_FACE_ID);
7211 display_mode_element (&it, 0, -1, -1, fmt);
7212 len = frame_title_ptr - frame_title_buf;
7213 frame_title_ptr = NULL;
7214 set_buffer_internal (obuf);
7215
7216 /* Set the title only if it's changed. This avoids consing in
7217 the common case where it hasn't. (If it turns out that we've
7218 already wasted too much time by walking through the list with
7219 display_mode_element, then we might need to optimize at a
7220 higher level than this.) */
7221 if (! STRINGP (f->name)
7222 || STRING_BYTES (XSTRING (f->name)) != len
7223 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7224 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7225 }
7226 }
7227
7228 #else /* not HAVE_WINDOW_SYSTEM */
7229
7230 #define frame_title_ptr ((char *)0)
7231 #define store_frame_title(str, mincol, maxcol) 0
7232
7233 #endif /* not HAVE_WINDOW_SYSTEM */
7234
7235
7236
7237 \f
7238 /***********************************************************************
7239 Menu Bars
7240 ***********************************************************************/
7241
7242
7243 /* Prepare for redisplay by updating menu-bar item lists when
7244 appropriate. This can call eval. */
7245
7246 void
7247 prepare_menu_bars ()
7248 {
7249 int all_windows;
7250 struct gcpro gcpro1, gcpro2;
7251 struct frame *f;
7252 Lisp_Object tooltip_frame;
7253
7254 #ifdef HAVE_X_WINDOWS
7255 tooltip_frame = tip_frame;
7256 #else
7257 tooltip_frame = Qnil;
7258 #endif
7259
7260 /* Update all frame titles based on their buffer names, etc. We do
7261 this before the menu bars so that the buffer-menu will show the
7262 up-to-date frame titles. */
7263 #ifdef HAVE_WINDOW_SYSTEM
7264 if (windows_or_buffers_changed || update_mode_lines)
7265 {
7266 Lisp_Object tail, frame;
7267
7268 FOR_EACH_FRAME (tail, frame)
7269 {
7270 f = XFRAME (frame);
7271 if (!EQ (frame, tooltip_frame)
7272 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7273 x_consider_frame_title (frame);
7274 }
7275 }
7276 #endif /* HAVE_WINDOW_SYSTEM */
7277
7278 /* Update the menu bar item lists, if appropriate. This has to be
7279 done before any actual redisplay or generation of display lines. */
7280 all_windows = (update_mode_lines
7281 || buffer_shared > 1
7282 || windows_or_buffers_changed);
7283 if (all_windows)
7284 {
7285 Lisp_Object tail, frame;
7286 int count = BINDING_STACK_SIZE ();
7287
7288 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7289
7290 FOR_EACH_FRAME (tail, frame)
7291 {
7292 f = XFRAME (frame);
7293
7294 /* Ignore tooltip frame. */
7295 if (EQ (frame, tooltip_frame))
7296 continue;
7297
7298 /* If a window on this frame changed size, report that to
7299 the user and clear the size-change flag. */
7300 if (FRAME_WINDOW_SIZES_CHANGED (f))
7301 {
7302 Lisp_Object functions;
7303
7304 /* Clear flag first in case we get an error below. */
7305 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7306 functions = Vwindow_size_change_functions;
7307 GCPRO2 (tail, functions);
7308
7309 while (CONSP (functions))
7310 {
7311 call1 (XCAR (functions), frame);
7312 functions = XCDR (functions);
7313 }
7314 UNGCPRO;
7315 }
7316
7317 GCPRO1 (tail);
7318 update_menu_bar (f, 0);
7319 #ifdef HAVE_WINDOW_SYSTEM
7320 update_tool_bar (f, 0);
7321 #endif
7322 UNGCPRO;
7323 }
7324
7325 unbind_to (count, Qnil);
7326 }
7327 else
7328 {
7329 struct frame *sf = SELECTED_FRAME ();
7330 update_menu_bar (sf, 1);
7331 #ifdef HAVE_WINDOW_SYSTEM
7332 update_tool_bar (sf, 1);
7333 #endif
7334 }
7335
7336 /* Motif needs this. See comment in xmenu.c. Turn it off when
7337 pending_menu_activation is not defined. */
7338 #ifdef USE_X_TOOLKIT
7339 pending_menu_activation = 0;
7340 #endif
7341 }
7342
7343
7344 /* Update the menu bar item list for frame F. This has to be done
7345 before we start to fill in any display lines, because it can call
7346 eval.
7347
7348 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7349
7350 static void
7351 update_menu_bar (f, save_match_data)
7352 struct frame *f;
7353 int save_match_data;
7354 {
7355 Lisp_Object window;
7356 register struct window *w;
7357
7358 /* If called recursively during a menu update, do nothing. This can
7359 happen when, for instance, an activate-menubar-hook causes a
7360 redisplay. */
7361 if (inhibit_menubar_update)
7362 return;
7363
7364 window = FRAME_SELECTED_WINDOW (f);
7365 w = XWINDOW (window);
7366
7367 if (update_mode_lines)
7368 w->update_mode_line = Qt;
7369
7370 if (FRAME_WINDOW_P (f)
7371 ?
7372 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7373 FRAME_EXTERNAL_MENU_BAR (f)
7374 #else
7375 FRAME_MENU_BAR_LINES (f) > 0
7376 #endif
7377 : FRAME_MENU_BAR_LINES (f) > 0)
7378 {
7379 /* If the user has switched buffers or windows, we need to
7380 recompute to reflect the new bindings. But we'll
7381 recompute when update_mode_lines is set too; that means
7382 that people can use force-mode-line-update to request
7383 that the menu bar be recomputed. The adverse effect on
7384 the rest of the redisplay algorithm is about the same as
7385 windows_or_buffers_changed anyway. */
7386 if (windows_or_buffers_changed
7387 || !NILP (w->update_mode_line)
7388 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7389 < BUF_MODIFF (XBUFFER (w->buffer)))
7390 != !NILP (w->last_had_star))
7391 || ((!NILP (Vtransient_mark_mode)
7392 && !NILP (XBUFFER (w->buffer)->mark_active))
7393 != !NILP (w->region_showing)))
7394 {
7395 struct buffer *prev = current_buffer;
7396 int count = BINDING_STACK_SIZE ();
7397
7398 specbind (Qinhibit_menubar_update, Qt);
7399
7400 set_buffer_internal_1 (XBUFFER (w->buffer));
7401 if (save_match_data)
7402 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7403 if (NILP (Voverriding_local_map_menu_flag))
7404 {
7405 specbind (Qoverriding_terminal_local_map, Qnil);
7406 specbind (Qoverriding_local_map, Qnil);
7407 }
7408
7409 /* Run the Lucid hook. */
7410 safe_run_hooks (Qactivate_menubar_hook);
7411
7412 /* If it has changed current-menubar from previous value,
7413 really recompute the menu-bar from the value. */
7414 if (! NILP (Vlucid_menu_bar_dirty_flag))
7415 call0 (Qrecompute_lucid_menubar);
7416
7417 safe_run_hooks (Qmenu_bar_update_hook);
7418 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7419
7420 /* Redisplay the menu bar in case we changed it. */
7421 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7422 if (FRAME_WINDOW_P (f)
7423 #if defined (macintosh)
7424 /* All frames on Mac OS share the same menubar. So only the
7425 selected frame should be allowed to set it. */
7426 && f == SELECTED_FRAME ()
7427 #endif
7428 )
7429 set_frame_menubar (f, 0, 0);
7430 else
7431 /* On a terminal screen, the menu bar is an ordinary screen
7432 line, and this makes it get updated. */
7433 w->update_mode_line = Qt;
7434 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7435 /* In the non-toolkit version, the menu bar is an ordinary screen
7436 line, and this makes it get updated. */
7437 w->update_mode_line = Qt;
7438 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7439
7440 unbind_to (count, Qnil);
7441 set_buffer_internal_1 (prev);
7442 }
7443 }
7444 }
7445
7446
7447 \f
7448 /***********************************************************************
7449 Tool-bars
7450 ***********************************************************************/
7451
7452 #ifdef HAVE_WINDOW_SYSTEM
7453
7454 /* Update the tool-bar item list for frame F. This has to be done
7455 before we start to fill in any display lines. Called from
7456 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7457 and restore it here. */
7458
7459 static void
7460 update_tool_bar (f, save_match_data)
7461 struct frame *f;
7462 int save_match_data;
7463 {
7464 if (WINDOWP (f->tool_bar_window)
7465 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7466 {
7467 Lisp_Object window;
7468 struct window *w;
7469
7470 window = FRAME_SELECTED_WINDOW (f);
7471 w = XWINDOW (window);
7472
7473 /* If the user has switched buffers or windows, we need to
7474 recompute to reflect the new bindings. But we'll
7475 recompute when update_mode_lines is set too; that means
7476 that people can use force-mode-line-update to request
7477 that the menu bar be recomputed. The adverse effect on
7478 the rest of the redisplay algorithm is about the same as
7479 windows_or_buffers_changed anyway. */
7480 if (windows_or_buffers_changed
7481 || !NILP (w->update_mode_line)
7482 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7483 < BUF_MODIFF (XBUFFER (w->buffer)))
7484 != !NILP (w->last_had_star))
7485 || ((!NILP (Vtransient_mark_mode)
7486 && !NILP (XBUFFER (w->buffer)->mark_active))
7487 != !NILP (w->region_showing)))
7488 {
7489 struct buffer *prev = current_buffer;
7490 int count = BINDING_STACK_SIZE ();
7491
7492 /* Set current_buffer to the buffer of the selected
7493 window of the frame, so that we get the right local
7494 keymaps. */
7495 set_buffer_internal_1 (XBUFFER (w->buffer));
7496
7497 /* Save match data, if we must. */
7498 if (save_match_data)
7499 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7500
7501 /* Make sure that we don't accidentally use bogus keymaps. */
7502 if (NILP (Voverriding_local_map_menu_flag))
7503 {
7504 specbind (Qoverriding_terminal_local_map, Qnil);
7505 specbind (Qoverriding_local_map, Qnil);
7506 }
7507
7508 /* Build desired tool-bar items from keymaps. */
7509 f->tool_bar_items
7510 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7511
7512 /* Redisplay the tool-bar in case we changed it. */
7513 w->update_mode_line = Qt;
7514
7515 unbind_to (count, Qnil);
7516 set_buffer_internal_1 (prev);
7517 }
7518 }
7519 }
7520
7521
7522 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7523 F's desired tool-bar contents. F->tool_bar_items must have
7524 been set up previously by calling prepare_menu_bars. */
7525
7526 static void
7527 build_desired_tool_bar_string (f)
7528 struct frame *f;
7529 {
7530 int i, size, size_needed;
7531 struct gcpro gcpro1, gcpro2, gcpro3;
7532 Lisp_Object image, plist, props;
7533
7534 image = plist = props = Qnil;
7535 GCPRO3 (image, plist, props);
7536
7537 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7538 Otherwise, make a new string. */
7539
7540 /* The size of the string we might be able to reuse. */
7541 size = (STRINGP (f->desired_tool_bar_string)
7542 ? XSTRING (f->desired_tool_bar_string)->size
7543 : 0);
7544
7545 /* We need one space in the string for each image. */
7546 size_needed = f->n_tool_bar_items;
7547
7548 /* Reuse f->desired_tool_bar_string, if possible. */
7549 if (size < size_needed || NILP (f->desired_tool_bar_string))
7550 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7551 make_number (' '));
7552 else
7553 {
7554 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7555 Fremove_text_properties (make_number (0), make_number (size),
7556 props, f->desired_tool_bar_string);
7557 }
7558
7559 /* Put a `display' property on the string for the images to display,
7560 put a `menu_item' property on tool-bar items with a value that
7561 is the index of the item in F's tool-bar item vector. */
7562 for (i = 0; i < f->n_tool_bar_items; ++i)
7563 {
7564 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7565
7566 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7567 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7568 int hmargin, vmargin, relief, idx, end;
7569 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7570 extern Lisp_Object Qlaplace;
7571
7572 /* If image is a vector, choose the image according to the
7573 button state. */
7574 image = PROP (TOOL_BAR_ITEM_IMAGES);
7575 if (VECTORP (image))
7576 {
7577 if (enabled_p)
7578 idx = (selected_p
7579 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7580 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7581 else
7582 idx = (selected_p
7583 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7584 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7585
7586 xassert (ASIZE (image) >= idx);
7587 image = AREF (image, idx);
7588 }
7589 else
7590 idx = -1;
7591
7592 /* Ignore invalid image specifications. */
7593 if (!valid_image_p (image))
7594 continue;
7595
7596 /* Display the tool-bar button pressed, or depressed. */
7597 plist = Fcopy_sequence (XCDR (image));
7598
7599 /* Compute margin and relief to draw. */
7600 relief = (tool_bar_button_relief > 0
7601 ? tool_bar_button_relief
7602 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7603 hmargin = vmargin = relief;
7604
7605 if (INTEGERP (Vtool_bar_button_margin)
7606 && XINT (Vtool_bar_button_margin) > 0)
7607 {
7608 hmargin += XFASTINT (Vtool_bar_button_margin);
7609 vmargin += XFASTINT (Vtool_bar_button_margin);
7610 }
7611 else if (CONSP (Vtool_bar_button_margin))
7612 {
7613 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7614 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7615 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7616
7617 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7618 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7619 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7620 }
7621
7622 if (auto_raise_tool_bar_buttons_p)
7623 {
7624 /* Add a `:relief' property to the image spec if the item is
7625 selected. */
7626 if (selected_p)
7627 {
7628 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7629 hmargin -= relief;
7630 vmargin -= relief;
7631 }
7632 }
7633 else
7634 {
7635 /* If image is selected, display it pressed, i.e. with a
7636 negative relief. If it's not selected, display it with a
7637 raised relief. */
7638 plist = Fplist_put (plist, QCrelief,
7639 (selected_p
7640 ? make_number (-relief)
7641 : make_number (relief)));
7642 hmargin -= relief;
7643 vmargin -= relief;
7644 }
7645
7646 /* Put a margin around the image. */
7647 if (hmargin || vmargin)
7648 {
7649 if (hmargin == vmargin)
7650 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7651 else
7652 plist = Fplist_put (plist, QCmargin,
7653 Fcons (make_number (hmargin),
7654 make_number (vmargin)));
7655 }
7656
7657 /* If button is not enabled, and we don't have special images
7658 for the disabled state, make the image appear disabled by
7659 applying an appropriate algorithm to it. */
7660 if (!enabled_p && idx < 0)
7661 plist = Fplist_put (plist, QCconversion, Qdisabled);
7662
7663 /* Put a `display' text property on the string for the image to
7664 display. Put a `menu-item' property on the string that gives
7665 the start of this item's properties in the tool-bar items
7666 vector. */
7667 image = Fcons (Qimage, plist);
7668 props = list4 (Qdisplay, image,
7669 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7670
7671 /* Let the last image hide all remaining spaces in the tool bar
7672 string. The string can be longer than needed when we reuse a
7673 previous string. */
7674 if (i + 1 == f->n_tool_bar_items)
7675 end = XSTRING (f->desired_tool_bar_string)->size;
7676 else
7677 end = i + 1;
7678 Fadd_text_properties (make_number (i), make_number (end),
7679 props, f->desired_tool_bar_string);
7680 #undef PROP
7681 }
7682
7683 UNGCPRO;
7684 }
7685
7686
7687 /* Display one line of the tool-bar of frame IT->f. */
7688
7689 static void
7690 display_tool_bar_line (it)
7691 struct it *it;
7692 {
7693 struct glyph_row *row = it->glyph_row;
7694 int max_x = it->last_visible_x;
7695 struct glyph *last;
7696
7697 prepare_desired_row (row);
7698 row->y = it->current_y;
7699
7700 /* Note that this isn't made use of if the face hasn't a box,
7701 so there's no need to check the face here. */
7702 it->start_of_box_run_p = 1;
7703
7704 while (it->current_x < max_x)
7705 {
7706 int x_before, x, n_glyphs_before, i, nglyphs;
7707
7708 /* Get the next display element. */
7709 if (!get_next_display_element (it))
7710 break;
7711
7712 /* Produce glyphs. */
7713 x_before = it->current_x;
7714 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7715 PRODUCE_GLYPHS (it);
7716
7717 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7718 i = 0;
7719 x = x_before;
7720 while (i < nglyphs)
7721 {
7722 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7723
7724 if (x + glyph->pixel_width > max_x)
7725 {
7726 /* Glyph doesn't fit on line. */
7727 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7728 it->current_x = x;
7729 goto out;
7730 }
7731
7732 ++it->hpos;
7733 x += glyph->pixel_width;
7734 ++i;
7735 }
7736
7737 /* Stop at line ends. */
7738 if (ITERATOR_AT_END_OF_LINE_P (it))
7739 break;
7740
7741 set_iterator_to_next (it, 1);
7742 }
7743
7744 out:;
7745
7746 row->displays_text_p = row->used[TEXT_AREA] != 0;
7747 extend_face_to_end_of_line (it);
7748 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7749 last->right_box_line_p = 1;
7750 if (last == row->glyphs[TEXT_AREA])
7751 last->left_box_line_p = 1;
7752 compute_line_metrics (it);
7753
7754 /* If line is empty, make it occupy the rest of the tool-bar. */
7755 if (!row->displays_text_p)
7756 {
7757 row->height = row->phys_height = it->last_visible_y - row->y;
7758 row->ascent = row->phys_ascent = 0;
7759 }
7760
7761 row->full_width_p = 1;
7762 row->continued_p = 0;
7763 row->truncated_on_left_p = 0;
7764 row->truncated_on_right_p = 0;
7765
7766 it->current_x = it->hpos = 0;
7767 it->current_y += row->height;
7768 ++it->vpos;
7769 ++it->glyph_row;
7770 }
7771
7772
7773 /* Value is the number of screen lines needed to make all tool-bar
7774 items of frame F visible. */
7775
7776 static int
7777 tool_bar_lines_needed (f)
7778 struct frame *f;
7779 {
7780 struct window *w = XWINDOW (f->tool_bar_window);
7781 struct it it;
7782
7783 /* Initialize an iterator for iteration over
7784 F->desired_tool_bar_string in the tool-bar window of frame F. */
7785 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7786 it.first_visible_x = 0;
7787 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7788 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7789
7790 while (!ITERATOR_AT_END_P (&it))
7791 {
7792 it.glyph_row = w->desired_matrix->rows;
7793 clear_glyph_row (it.glyph_row);
7794 display_tool_bar_line (&it);
7795 }
7796
7797 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7798 }
7799
7800
7801 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7802 0, 1, 0,
7803 "Return the number of lines occupied by the tool bar of FRAME.")
7804 (frame)
7805 Lisp_Object frame;
7806 {
7807 struct frame *f;
7808 struct window *w;
7809 int nlines = 0;
7810
7811 if (NILP (frame))
7812 frame = selected_frame;
7813 else
7814 CHECK_FRAME (frame, 0);
7815 f = XFRAME (frame);
7816
7817 if (WINDOWP (f->tool_bar_window)
7818 || (w = XWINDOW (f->tool_bar_window),
7819 XFASTINT (w->height) > 0))
7820 {
7821 update_tool_bar (f, 1);
7822 if (f->n_tool_bar_items)
7823 {
7824 build_desired_tool_bar_string (f);
7825 nlines = tool_bar_lines_needed (f);
7826 }
7827 }
7828
7829 return make_number (nlines);
7830 }
7831
7832
7833 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7834 height should be changed. */
7835
7836 static int
7837 redisplay_tool_bar (f)
7838 struct frame *f;
7839 {
7840 struct window *w;
7841 struct it it;
7842 struct glyph_row *row;
7843 int change_height_p = 0;
7844
7845 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7846 do anything. This means you must start with tool-bar-lines
7847 non-zero to get the auto-sizing effect. Or in other words, you
7848 can turn off tool-bars by specifying tool-bar-lines zero. */
7849 if (!WINDOWP (f->tool_bar_window)
7850 || (w = XWINDOW (f->tool_bar_window),
7851 XFASTINT (w->height) == 0))
7852 return 0;
7853
7854 /* Set up an iterator for the tool-bar window. */
7855 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7856 it.first_visible_x = 0;
7857 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7858 row = it.glyph_row;
7859
7860 /* Build a string that represents the contents of the tool-bar. */
7861 build_desired_tool_bar_string (f);
7862 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7863
7864 /* Display as many lines as needed to display all tool-bar items. */
7865 while (it.current_y < it.last_visible_y)
7866 display_tool_bar_line (&it);
7867
7868 /* It doesn't make much sense to try scrolling in the tool-bar
7869 window, so don't do it. */
7870 w->desired_matrix->no_scrolling_p = 1;
7871 w->must_be_updated_p = 1;
7872
7873 if (auto_resize_tool_bars_p)
7874 {
7875 int nlines;
7876
7877 /* If we couldn't display everything, change the tool-bar's
7878 height. */
7879 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7880 change_height_p = 1;
7881
7882 /* If there are blank lines at the end, except for a partially
7883 visible blank line at the end that is smaller than
7884 CANON_Y_UNIT, change the tool-bar's height. */
7885 row = it.glyph_row - 1;
7886 if (!row->displays_text_p
7887 && row->height >= CANON_Y_UNIT (f))
7888 change_height_p = 1;
7889
7890 /* If row displays tool-bar items, but is partially visible,
7891 change the tool-bar's height. */
7892 if (row->displays_text_p
7893 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7894 change_height_p = 1;
7895
7896 /* Resize windows as needed by changing the `tool-bar-lines'
7897 frame parameter. */
7898 if (change_height_p
7899 && (nlines = tool_bar_lines_needed (f),
7900 nlines != XFASTINT (w->height)))
7901 {
7902 extern Lisp_Object Qtool_bar_lines;
7903 Lisp_Object frame;
7904 int old_height = XFASTINT (w->height);
7905
7906 XSETFRAME (frame, f);
7907 clear_glyph_matrix (w->desired_matrix);
7908 Fmodify_frame_parameters (frame,
7909 Fcons (Fcons (Qtool_bar_lines,
7910 make_number (nlines)),
7911 Qnil));
7912 if (XFASTINT (w->height) != old_height)
7913 fonts_changed_p = 1;
7914 }
7915 }
7916
7917 return change_height_p;
7918 }
7919
7920
7921 /* Get information about the tool-bar item which is displayed in GLYPH
7922 on frame F. Return in *PROP_IDX the index where tool-bar item
7923 properties start in F->tool_bar_items. Value is zero if
7924 GLYPH doesn't display a tool-bar item. */
7925
7926 int
7927 tool_bar_item_info (f, glyph, prop_idx)
7928 struct frame *f;
7929 struct glyph *glyph;
7930 int *prop_idx;
7931 {
7932 Lisp_Object prop;
7933 int success_p;
7934
7935 /* Get the text property `menu-item' at pos. The value of that
7936 property is the start index of this item's properties in
7937 F->tool_bar_items. */
7938 prop = Fget_text_property (make_number (glyph->charpos),
7939 Qmenu_item, f->current_tool_bar_string);
7940 if (INTEGERP (prop))
7941 {
7942 *prop_idx = XINT (prop);
7943 success_p = 1;
7944 }
7945 else
7946 success_p = 0;
7947
7948 return success_p;
7949 }
7950
7951 #endif /* HAVE_WINDOW_SYSTEM */
7952
7953
7954 \f
7955 /************************************************************************
7956 Horizontal scrolling
7957 ************************************************************************/
7958
7959 static int hscroll_window_tree P_ ((Lisp_Object));
7960 static int hscroll_windows P_ ((Lisp_Object));
7961
7962 /* For all leaf windows in the window tree rooted at WINDOW, set their
7963 hscroll value so that PT is (i) visible in the window, and (ii) so
7964 that it is not within a certain margin at the window's left and
7965 right border. Value is non-zero if any window's hscroll has been
7966 changed. */
7967
7968 static int
7969 hscroll_window_tree (window)
7970 Lisp_Object window;
7971 {
7972 int hscrolled_p = 0;
7973
7974 while (WINDOWP (window))
7975 {
7976 struct window *w = XWINDOW (window);
7977
7978 if (WINDOWP (w->hchild))
7979 hscrolled_p |= hscroll_window_tree (w->hchild);
7980 else if (WINDOWP (w->vchild))
7981 hscrolled_p |= hscroll_window_tree (w->vchild);
7982 else if (w->cursor.vpos >= 0)
7983 {
7984 int hscroll_margin, text_area_x, text_area_y;
7985 int text_area_width, text_area_height;
7986 struct glyph_row *current_cursor_row
7987 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7988 struct glyph_row *desired_cursor_row
7989 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7990 struct glyph_row *cursor_row
7991 = (desired_cursor_row->enabled_p
7992 ? desired_cursor_row
7993 : current_cursor_row);
7994
7995 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7996 &text_area_width, &text_area_height);
7997
7998 /* Scroll when cursor is inside this scroll margin. */
7999 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
8000
8001 if ((XFASTINT (w->hscroll)
8002 && w->cursor.x < hscroll_margin)
8003 || (cursor_row->enabled_p
8004 && cursor_row->truncated_on_right_p
8005 && (w->cursor.x > text_area_width - hscroll_margin)))
8006 {
8007 struct it it;
8008 int hscroll;
8009 struct buffer *saved_current_buffer;
8010 int pt;
8011
8012 /* Find point in a display of infinite width. */
8013 saved_current_buffer = current_buffer;
8014 current_buffer = XBUFFER (w->buffer);
8015
8016 if (w == XWINDOW (selected_window))
8017 pt = BUF_PT (current_buffer);
8018 else
8019 {
8020 pt = marker_position (w->pointm);
8021 pt = max (BEGV, pt);
8022 pt = min (ZV, pt);
8023 }
8024
8025 /* Move iterator to pt starting at cursor_row->start in
8026 a line with infinite width. */
8027 init_to_row_start (&it, w, cursor_row);
8028 it.last_visible_x = INFINITY;
8029 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8030 current_buffer = saved_current_buffer;
8031
8032 /* Center cursor in window. */
8033 hscroll = (max (0, it.current_x - text_area_width / 2)
8034 / CANON_X_UNIT (it.f));
8035 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8036
8037 /* Don't call Fset_window_hscroll if value hasn't
8038 changed because it will prevent redisplay
8039 optimizations. */
8040 if (XFASTINT (w->hscroll) != hscroll)
8041 {
8042 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8043 w->hscroll = make_number (hscroll);
8044 hscrolled_p = 1;
8045 }
8046 }
8047 }
8048
8049 window = w->next;
8050 }
8051
8052 /* Value is non-zero if hscroll of any leaf window has been changed. */
8053 return hscrolled_p;
8054 }
8055
8056
8057 /* Set hscroll so that cursor is visible and not inside horizontal
8058 scroll margins for all windows in the tree rooted at WINDOW. See
8059 also hscroll_window_tree above. Value is non-zero if any window's
8060 hscroll has been changed. If it has, desired matrices on the frame
8061 of WINDOW are cleared. */
8062
8063 static int
8064 hscroll_windows (window)
8065 Lisp_Object window;
8066 {
8067 int hscrolled_p;
8068
8069 if (automatic_hscrolling_p)
8070 {
8071 hscrolled_p = hscroll_window_tree (window);
8072 if (hscrolled_p)
8073 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8074 }
8075 else
8076 hscrolled_p = 0;
8077 return hscrolled_p;
8078 }
8079
8080
8081 \f
8082 /************************************************************************
8083 Redisplay
8084 ************************************************************************/
8085
8086 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8087 to a non-zero value. This is sometimes handy to have in a debugger
8088 session. */
8089
8090 #if GLYPH_DEBUG
8091
8092 /* First and last unchanged row for try_window_id. */
8093
8094 int debug_first_unchanged_at_end_vpos;
8095 int debug_last_unchanged_at_beg_vpos;
8096
8097 /* Delta vpos and y. */
8098
8099 int debug_dvpos, debug_dy;
8100
8101 /* Delta in characters and bytes for try_window_id. */
8102
8103 int debug_delta, debug_delta_bytes;
8104
8105 /* Values of window_end_pos and window_end_vpos at the end of
8106 try_window_id. */
8107
8108 int debug_end_pos, debug_end_vpos;
8109
8110 /* Append a string to W->desired_matrix->method. FMT is a printf
8111 format string. A1...A9 are a supplement for a variable-length
8112 argument list. If trace_redisplay_p is non-zero also printf the
8113 resulting string to stderr. */
8114
8115 static void
8116 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8117 struct window *w;
8118 char *fmt;
8119 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8120 {
8121 char buffer[512];
8122 char *method = w->desired_matrix->method;
8123 int len = strlen (method);
8124 int size = sizeof w->desired_matrix->method;
8125 int remaining = size - len - 1;
8126
8127 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8128 if (len && remaining)
8129 {
8130 method[len] = '|';
8131 --remaining, ++len;
8132 }
8133
8134 strncpy (method + len, buffer, remaining);
8135
8136 if (trace_redisplay_p)
8137 fprintf (stderr, "%p (%s): %s\n",
8138 w,
8139 ((BUFFERP (w->buffer)
8140 && STRINGP (XBUFFER (w->buffer)->name))
8141 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8142 : "no buffer"),
8143 buffer);
8144 }
8145
8146 #endif /* GLYPH_DEBUG */
8147
8148
8149 /* This counter is used to clear the face cache every once in a while
8150 in redisplay_internal. It is incremented for each redisplay.
8151 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8152 cleared. */
8153
8154 #define CLEAR_FACE_CACHE_COUNT 10000
8155 static int clear_face_cache_count;
8156
8157 /* Record the previous terminal frame we displayed. */
8158
8159 static struct frame *previous_terminal_frame;
8160
8161 /* Non-zero while redisplay_internal is in progress. */
8162
8163 int redisplaying_p;
8164
8165
8166 /* Value is non-zero if all changes in window W, which displays
8167 current_buffer, are in the text between START and END. START is a
8168 buffer position, END is given as a distance from Z. Used in
8169 redisplay_internal for display optimization. */
8170
8171 static INLINE int
8172 text_outside_line_unchanged_p (w, start, end)
8173 struct window *w;
8174 int start, end;
8175 {
8176 int unchanged_p = 1;
8177
8178 /* If text or overlays have changed, see where. */
8179 if (XFASTINT (w->last_modified) < MODIFF
8180 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8181 {
8182 /* Gap in the line? */
8183 if (GPT < start || Z - GPT < end)
8184 unchanged_p = 0;
8185
8186 /* Changes start in front of the line, or end after it? */
8187 if (unchanged_p
8188 && (BEG_UNCHANGED < start - 1
8189 || END_UNCHANGED < end))
8190 unchanged_p = 0;
8191
8192 /* If selective display, can't optimize if changes start at the
8193 beginning of the line. */
8194 if (unchanged_p
8195 && INTEGERP (current_buffer->selective_display)
8196 && XINT (current_buffer->selective_display) > 0
8197 && (BEG_UNCHANGED < start || GPT <= start))
8198 unchanged_p = 0;
8199
8200 /* If there are overlays at the start or end of the line, these
8201 may have overlay strings with newlines in them. A change at
8202 START, for instance, may actually concern the display of such
8203 overlay strings as well, and they are displayed on different
8204 lines. So, quickly rule out this case. (For the future, it
8205 might be desirable to implement something more telling than
8206 just BEG/END_UNCHANGED.) */
8207 if (unchanged_p)
8208 {
8209 if (BEG + BEG_UNCHANGED == start
8210 && overlay_touches_p (start))
8211 unchanged_p = 0;
8212 if (END_UNCHANGED == end
8213 && overlay_touches_p (Z - end))
8214 unchanged_p = 0;
8215 }
8216 }
8217
8218 return unchanged_p;
8219 }
8220
8221
8222 /* Do a frame update, taking possible shortcuts into account. This is
8223 the main external entry point for redisplay.
8224
8225 If the last redisplay displayed an echo area message and that message
8226 is no longer requested, we clear the echo area or bring back the
8227 mini-buffer if that is in use. */
8228
8229 void
8230 redisplay ()
8231 {
8232 redisplay_internal (0);
8233 }
8234
8235
8236 /* Return 1 if point moved out of or into a composition. Otherwise
8237 return 0. PREV_BUF and PREV_PT are the last point buffer and
8238 position. BUF and PT are the current point buffer and position. */
8239
8240 int
8241 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8242 struct buffer *prev_buf, *buf;
8243 int prev_pt, pt;
8244 {
8245 int start, end;
8246 Lisp_Object prop;
8247 Lisp_Object buffer;
8248
8249 XSETBUFFER (buffer, buf);
8250 /* Check a composition at the last point if point moved within the
8251 same buffer. */
8252 if (prev_buf == buf)
8253 {
8254 if (prev_pt == pt)
8255 /* Point didn't move. */
8256 return 0;
8257
8258 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8259 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8260 && COMPOSITION_VALID_P (start, end, prop)
8261 && start < prev_pt && end > prev_pt)
8262 /* The last point was within the composition. Return 1 iff
8263 point moved out of the composition. */
8264 return (pt <= start || pt >= end);
8265 }
8266
8267 /* Check a composition at the current point. */
8268 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8269 && find_composition (pt, -1, &start, &end, &prop, buffer)
8270 && COMPOSITION_VALID_P (start, end, prop)
8271 && start < pt && end > pt);
8272 }
8273
8274
8275 /* Reconsider the setting of B->clip_changed which is displayed
8276 in window W. */
8277
8278 static INLINE void
8279 reconsider_clip_changes (w, b)
8280 struct window *w;
8281 struct buffer *b;
8282 {
8283 if (b->prevent_redisplay_optimizations_p)
8284 b->clip_changed = 1;
8285 else if (b->clip_changed
8286 && !NILP (w->window_end_valid)
8287 && w->current_matrix->buffer == b
8288 && w->current_matrix->zv == BUF_ZV (b)
8289 && w->current_matrix->begv == BUF_BEGV (b))
8290 b->clip_changed = 0;
8291
8292 /* If display wasn't paused, and W is not a tool bar window, see if
8293 point has been moved into or out of a composition. In that case,
8294 we set b->clip_changed to 1 to force updating the screen. If
8295 b->clip_changed has already been set to 1, we can skip this
8296 check. */
8297 if (!b->clip_changed
8298 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8299 {
8300 int pt;
8301
8302 if (w == XWINDOW (selected_window))
8303 pt = BUF_PT (current_buffer);
8304 else
8305 pt = marker_position (w->pointm);
8306
8307 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8308 || pt != XINT (w->last_point))
8309 && check_point_in_composition (w->current_matrix->buffer,
8310 XINT (w->last_point),
8311 XBUFFER (w->buffer), pt))
8312 b->clip_changed = 1;
8313 }
8314 }
8315
8316
8317 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8318 response to any user action; therefore, we should preserve the echo
8319 area. (Actually, our caller does that job.) Perhaps in the future
8320 avoid recentering windows if it is not necessary; currently that
8321 causes some problems. */
8322
8323 static void
8324 redisplay_internal (preserve_echo_area)
8325 int preserve_echo_area;
8326 {
8327 struct window *w = XWINDOW (selected_window);
8328 struct frame *f = XFRAME (w->frame);
8329 int pause;
8330 int must_finish = 0;
8331 struct text_pos tlbufpos, tlendpos;
8332 int number_of_visible_frames;
8333 int count;
8334 struct frame *sf = SELECTED_FRAME ();
8335
8336 /* Non-zero means redisplay has to consider all windows on all
8337 frames. Zero means, only selected_window is considered. */
8338 int consider_all_windows_p;
8339
8340 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8341
8342 /* No redisplay if running in batch mode or frame is not yet fully
8343 initialized, or redisplay is explicitly turned off by setting
8344 Vinhibit_redisplay. */
8345 if (noninteractive
8346 || !NILP (Vinhibit_redisplay)
8347 || !f->glyphs_initialized_p)
8348 return;
8349
8350 /* The flag redisplay_performed_directly_p is set by
8351 direct_output_for_insert when it already did the whole screen
8352 update necessary. */
8353 if (redisplay_performed_directly_p)
8354 {
8355 redisplay_performed_directly_p = 0;
8356 if (!hscroll_windows (selected_window))
8357 return;
8358 }
8359
8360 #ifdef USE_X_TOOLKIT
8361 if (popup_activated ())
8362 return;
8363 #endif
8364
8365 /* I don't think this happens but let's be paranoid. */
8366 if (redisplaying_p)
8367 return;
8368
8369 /* Record a function that resets redisplaying_p to its old value
8370 when we leave this function. */
8371 count = BINDING_STACK_SIZE ();
8372 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8373 ++redisplaying_p;
8374
8375 retry:
8376 pause = 0;
8377 reconsider_clip_changes (w, current_buffer);
8378
8379 /* If new fonts have been loaded that make a glyph matrix adjustment
8380 necessary, do it. */
8381 if (fonts_changed_p)
8382 {
8383 adjust_glyphs (NULL);
8384 ++windows_or_buffers_changed;
8385 fonts_changed_p = 0;
8386 }
8387
8388 /* If face_change_count is non-zero, init_iterator will free all
8389 realized faces, which includes the faces referenced from current
8390 matrices. So, we can't reuse current matrices in this case. */
8391 if (face_change_count)
8392 ++windows_or_buffers_changed;
8393
8394 if (! FRAME_WINDOW_P (sf)
8395 && previous_terminal_frame != sf)
8396 {
8397 /* Since frames on an ASCII terminal share the same display
8398 area, displaying a different frame means redisplay the whole
8399 thing. */
8400 windows_or_buffers_changed++;
8401 SET_FRAME_GARBAGED (sf);
8402 XSETFRAME (Vterminal_frame, sf);
8403 }
8404 previous_terminal_frame = sf;
8405
8406 /* Set the visible flags for all frames. Do this before checking
8407 for resized or garbaged frames; they want to know if their frames
8408 are visible. See the comment in frame.h for
8409 FRAME_SAMPLE_VISIBILITY. */
8410 {
8411 Lisp_Object tail, frame;
8412
8413 number_of_visible_frames = 0;
8414
8415 FOR_EACH_FRAME (tail, frame)
8416 {
8417 struct frame *f = XFRAME (frame);
8418
8419 FRAME_SAMPLE_VISIBILITY (f);
8420 if (FRAME_VISIBLE_P (f))
8421 ++number_of_visible_frames;
8422 clear_desired_matrices (f);
8423 }
8424 }
8425
8426 /* Notice any pending interrupt request to change frame size. */
8427 do_pending_window_change (1);
8428
8429 /* Clear frames marked as garbaged. */
8430 if (frame_garbaged)
8431 clear_garbaged_frames ();
8432
8433 /* Build menubar and tool-bar items. */
8434 prepare_menu_bars ();
8435
8436 if (windows_or_buffers_changed)
8437 update_mode_lines++;
8438
8439 /* Detect case that we need to write or remove a star in the mode line. */
8440 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8441 {
8442 w->update_mode_line = Qt;
8443 if (buffer_shared > 1)
8444 update_mode_lines++;
8445 }
8446
8447 /* If %c is in the mode line, update it if needed. */
8448 if (!NILP (w->column_number_displayed)
8449 /* This alternative quickly identifies a common case
8450 where no change is needed. */
8451 && !(PT == XFASTINT (w->last_point)
8452 && XFASTINT (w->last_modified) >= MODIFF
8453 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8454 && XFASTINT (w->column_number_displayed) != current_column ())
8455 w->update_mode_line = Qt;
8456
8457 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8458
8459 /* The variable buffer_shared is set in redisplay_window and
8460 indicates that we redisplay a buffer in different windows. See
8461 there. */
8462 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8463
8464 /* If specs for an arrow have changed, do thorough redisplay
8465 to ensure we remove any arrow that should no longer exist. */
8466 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8467 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8468 consider_all_windows_p = windows_or_buffers_changed = 1;
8469
8470 /* Normally the message* functions will have already displayed and
8471 updated the echo area, but the frame may have been trashed, or
8472 the update may have been preempted, so display the echo area
8473 again here. Checking message_cleared_p captures the case that
8474 the echo area should be cleared. */
8475 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8476 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8477 || (message_cleared_p && minibuf_level == 0))
8478 {
8479 int window_height_changed_p = echo_area_display (0);
8480 must_finish = 1;
8481
8482 /* If we don't display the current message, don't clear the
8483 message_cleared_p flag, because, if we did, we wouldn't clear
8484 the echo area in the next redisplay which doesn't preserve
8485 the echo area. */
8486 if (!display_last_displayed_message_p)
8487 message_cleared_p = 0;
8488
8489 if (fonts_changed_p)
8490 goto retry;
8491 else if (window_height_changed_p)
8492 {
8493 consider_all_windows_p = 1;
8494 ++update_mode_lines;
8495 ++windows_or_buffers_changed;
8496
8497 /* If window configuration was changed, frames may have been
8498 marked garbaged. Clear them or we will experience
8499 surprises wrt scrolling. */
8500 if (frame_garbaged)
8501 clear_garbaged_frames ();
8502 }
8503 }
8504 else if (EQ (selected_window, minibuf_window)
8505 && (current_buffer->clip_changed
8506 || XFASTINT (w->last_modified) < MODIFF
8507 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8508 && resize_mini_window (w, 0))
8509 {
8510 /* Resized active mini-window to fit the size of what it is
8511 showing if its contents might have changed. */
8512 must_finish = 1;
8513 consider_all_windows_p = 1;
8514 ++windows_or_buffers_changed;
8515 ++update_mode_lines;
8516
8517 /* If window configuration was changed, frames may have been
8518 marked garbaged. Clear them or we will experience
8519 surprises wrt scrolling. */
8520 if (frame_garbaged)
8521 clear_garbaged_frames ();
8522 }
8523
8524
8525 /* If showing the region, and mark has changed, we must redisplay
8526 the whole window. The assignment to this_line_start_pos prevents
8527 the optimization directly below this if-statement. */
8528 if (((!NILP (Vtransient_mark_mode)
8529 && !NILP (XBUFFER (w->buffer)->mark_active))
8530 != !NILP (w->region_showing))
8531 || (!NILP (w->region_showing)
8532 && !EQ (w->region_showing,
8533 Fmarker_position (XBUFFER (w->buffer)->mark))))
8534 CHARPOS (this_line_start_pos) = 0;
8535
8536 /* Optimize the case that only the line containing the cursor in the
8537 selected window has changed. Variables starting with this_ are
8538 set in display_line and record information about the line
8539 containing the cursor. */
8540 tlbufpos = this_line_start_pos;
8541 tlendpos = this_line_end_pos;
8542 if (!consider_all_windows_p
8543 && CHARPOS (tlbufpos) > 0
8544 && NILP (w->update_mode_line)
8545 && !current_buffer->clip_changed
8546 && FRAME_VISIBLE_P (XFRAME (w->frame))
8547 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8548 /* Make sure recorded data applies to current buffer, etc. */
8549 && this_line_buffer == current_buffer
8550 && current_buffer == XBUFFER (w->buffer)
8551 && NILP (w->force_start)
8552 /* Point must be on the line that we have info recorded about. */
8553 && PT >= CHARPOS (tlbufpos)
8554 && PT <= Z - CHARPOS (tlendpos)
8555 /* All text outside that line, including its final newline,
8556 must be unchanged */
8557 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8558 CHARPOS (tlendpos)))
8559 {
8560 if (CHARPOS (tlbufpos) > BEGV
8561 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8562 && (CHARPOS (tlbufpos) == ZV
8563 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8564 /* Former continuation line has disappeared by becoming empty */
8565 goto cancel;
8566 else if (XFASTINT (w->last_modified) < MODIFF
8567 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8568 || MINI_WINDOW_P (w))
8569 {
8570 /* We have to handle the case of continuation around a
8571 wide-column character (See the comment in indent.c around
8572 line 885).
8573
8574 For instance, in the following case:
8575
8576 -------- Insert --------
8577 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8578 J_I_ ==> J_I_ `^^' are cursors.
8579 ^^ ^^
8580 -------- --------
8581
8582 As we have to redraw the line above, we should goto cancel. */
8583
8584 struct it it;
8585 int line_height_before = this_line_pixel_height;
8586
8587 /* Note that start_display will handle the case that the
8588 line starting at tlbufpos is a continuation lines. */
8589 start_display (&it, w, tlbufpos);
8590
8591 /* Implementation note: It this still necessary? */
8592 if (it.current_x != this_line_start_x)
8593 goto cancel;
8594
8595 TRACE ((stderr, "trying display optimization 1\n"));
8596 w->cursor.vpos = -1;
8597 overlay_arrow_seen = 0;
8598 it.vpos = this_line_vpos;
8599 it.current_y = this_line_y;
8600 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8601 display_line (&it);
8602
8603 /* If line contains point, is not continued,
8604 and ends at same distance from eob as before, we win */
8605 if (w->cursor.vpos >= 0
8606 /* Line is not continued, otherwise this_line_start_pos
8607 would have been set to 0 in display_line. */
8608 && CHARPOS (this_line_start_pos)
8609 /* Line ends as before. */
8610 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8611 /* Line has same height as before. Otherwise other lines
8612 would have to be shifted up or down. */
8613 && this_line_pixel_height == line_height_before)
8614 {
8615 /* If this is not the window's last line, we must adjust
8616 the charstarts of the lines below. */
8617 if (it.current_y < it.last_visible_y)
8618 {
8619 struct glyph_row *row
8620 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8621 int delta, delta_bytes;
8622
8623 if (Z - CHARPOS (tlendpos) == ZV)
8624 {
8625 /* This line ends at end of (accessible part of)
8626 buffer. There is no newline to count. */
8627 delta = (Z
8628 - CHARPOS (tlendpos)
8629 - MATRIX_ROW_START_CHARPOS (row));
8630 delta_bytes = (Z_BYTE
8631 - BYTEPOS (tlendpos)
8632 - MATRIX_ROW_START_BYTEPOS (row));
8633 }
8634 else
8635 {
8636 /* This line ends in a newline. Must take
8637 account of the newline and the rest of the
8638 text that follows. */
8639 delta = (Z
8640 - CHARPOS (tlendpos)
8641 - MATRIX_ROW_START_CHARPOS (row));
8642 delta_bytes = (Z_BYTE
8643 - BYTEPOS (tlendpos)
8644 - MATRIX_ROW_START_BYTEPOS (row));
8645 }
8646
8647 increment_matrix_positions (w->current_matrix,
8648 this_line_vpos + 1,
8649 w->current_matrix->nrows,
8650 delta, delta_bytes);
8651 }
8652
8653 /* If this row displays text now but previously didn't,
8654 or vice versa, w->window_end_vpos may have to be
8655 adjusted. */
8656 if ((it.glyph_row - 1)->displays_text_p)
8657 {
8658 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8659 XSETINT (w->window_end_vpos, this_line_vpos);
8660 }
8661 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8662 && this_line_vpos > 0)
8663 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8664 w->window_end_valid = Qnil;
8665
8666 /* Update hint: No need to try to scroll in update_window. */
8667 w->desired_matrix->no_scrolling_p = 1;
8668
8669 #if GLYPH_DEBUG
8670 *w->desired_matrix->method = 0;
8671 debug_method_add (w, "optimization 1");
8672 #endif
8673 goto update;
8674 }
8675 else
8676 goto cancel;
8677 }
8678 else if (/* Cursor position hasn't changed. */
8679 PT == XFASTINT (w->last_point)
8680 /* Make sure the cursor was last displayed
8681 in this window. Otherwise we have to reposition it. */
8682 && 0 <= w->cursor.vpos
8683 && XINT (w->height) > w->cursor.vpos)
8684 {
8685 if (!must_finish)
8686 {
8687 do_pending_window_change (1);
8688
8689 /* We used to always goto end_of_redisplay here, but this
8690 isn't enough if we have a blinking cursor. */
8691 if (w->cursor_off_p == w->last_cursor_off_p)
8692 goto end_of_redisplay;
8693 }
8694 goto update;
8695 }
8696 /* If highlighting the region, or if the cursor is in the echo area,
8697 then we can't just move the cursor. */
8698 else if (! (!NILP (Vtransient_mark_mode)
8699 && !NILP (current_buffer->mark_active))
8700 && (EQ (selected_window, current_buffer->last_selected_window)
8701 || highlight_nonselected_windows)
8702 && NILP (w->region_showing)
8703 && NILP (Vshow_trailing_whitespace)
8704 && !cursor_in_echo_area)
8705 {
8706 struct it it;
8707 struct glyph_row *row;
8708
8709 /* Skip from tlbufpos to PT and see where it is. Note that
8710 PT may be in invisible text. If so, we will end at the
8711 next visible position. */
8712 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8713 NULL, DEFAULT_FACE_ID);
8714 it.current_x = this_line_start_x;
8715 it.current_y = this_line_y;
8716 it.vpos = this_line_vpos;
8717
8718 /* The call to move_it_to stops in front of PT, but
8719 moves over before-strings. */
8720 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8721
8722 if (it.vpos == this_line_vpos
8723 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8724 row->enabled_p))
8725 {
8726 xassert (this_line_vpos == it.vpos);
8727 xassert (this_line_y == it.current_y);
8728 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8729 #if GLYPH_DEBUG
8730 *w->desired_matrix->method = 0;
8731 debug_method_add (w, "optimization 3");
8732 #endif
8733 goto update;
8734 }
8735 else
8736 goto cancel;
8737 }
8738
8739 cancel:
8740 /* Text changed drastically or point moved off of line. */
8741 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8742 }
8743
8744 CHARPOS (this_line_start_pos) = 0;
8745 consider_all_windows_p |= buffer_shared > 1;
8746 ++clear_face_cache_count;
8747
8748
8749 /* Build desired matrices, and update the display. If
8750 consider_all_windows_p is non-zero, do it for all windows on all
8751 frames. Otherwise do it for selected_window, only. */
8752
8753 if (consider_all_windows_p)
8754 {
8755 Lisp_Object tail, frame;
8756 int i, n = 0, size = 50;
8757 struct frame **updated
8758 = (struct frame **) alloca (size * sizeof *updated);
8759
8760 /* Clear the face cache eventually. */
8761 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8762 {
8763 clear_face_cache (0);
8764 clear_face_cache_count = 0;
8765 }
8766
8767 /* Recompute # windows showing selected buffer. This will be
8768 incremented each time such a window is displayed. */
8769 buffer_shared = 0;
8770
8771 FOR_EACH_FRAME (tail, frame)
8772 {
8773 struct frame *f = XFRAME (frame);
8774
8775 if (FRAME_WINDOW_P (f) || f == sf)
8776 {
8777 /* Mark all the scroll bars to be removed; we'll redeem
8778 the ones we want when we redisplay their windows. */
8779 if (condemn_scroll_bars_hook)
8780 condemn_scroll_bars_hook (f);
8781
8782 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8783 redisplay_windows (FRAME_ROOT_WINDOW (f));
8784
8785 /* Any scroll bars which redisplay_windows should have
8786 nuked should now go away. */
8787 if (judge_scroll_bars_hook)
8788 judge_scroll_bars_hook (f);
8789
8790 /* If fonts changed, display again. */
8791 if (fonts_changed_p)
8792 goto retry;
8793
8794 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8795 {
8796 /* See if we have to hscroll. */
8797 if (hscroll_windows (f->root_window))
8798 goto retry;
8799
8800 /* Prevent various kinds of signals during display
8801 update. stdio is not robust about handling
8802 signals, which can cause an apparent I/O
8803 error. */
8804 if (interrupt_input)
8805 unrequest_sigio ();
8806 stop_polling ();
8807
8808 /* Update the display. */
8809 set_window_update_flags (XWINDOW (f->root_window), 1);
8810 pause |= update_frame (f, 0, 0);
8811 if (pause)
8812 break;
8813
8814 if (n == size)
8815 {
8816 int nbytes = size * sizeof *updated;
8817 struct frame **p = (struct frame **) alloca (2 * nbytes);
8818 bcopy (updated, p, nbytes);
8819 size *= 2;
8820 }
8821
8822 updated[n++] = f;
8823 }
8824 }
8825 }
8826
8827 /* Do the mark_window_display_accurate after all windows have
8828 been redisplayed because this call resets flags in buffers
8829 which are needed for proper redisplay. */
8830 for (i = 0; i < n; ++i)
8831 {
8832 struct frame *f = updated[i];
8833 mark_window_display_accurate (f->root_window, 1);
8834 if (frame_up_to_date_hook)
8835 frame_up_to_date_hook (f);
8836 }
8837 }
8838 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8839 {
8840 Lisp_Object mini_window;
8841 struct frame *mini_frame;
8842
8843 redisplay_window (selected_window, 1);
8844
8845 /* Compare desired and current matrices, perform output. */
8846 update:
8847
8848 /* If fonts changed, display again. */
8849 if (fonts_changed_p)
8850 goto retry;
8851
8852 /* Prevent various kinds of signals during display update.
8853 stdio is not robust about handling signals,
8854 which can cause an apparent I/O error. */
8855 if (interrupt_input)
8856 unrequest_sigio ();
8857 stop_polling ();
8858
8859 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8860 {
8861 if (hscroll_windows (selected_window))
8862 goto retry;
8863
8864 XWINDOW (selected_window)->must_be_updated_p = 1;
8865 pause = update_frame (sf, 0, 0);
8866 }
8867
8868 /* We may have called echo_area_display at the top of this
8869 function. If the echo area is on another frame, that may
8870 have put text on a frame other than the selected one, so the
8871 above call to update_frame would not have caught it. Catch
8872 it here. */
8873 mini_window = FRAME_MINIBUF_WINDOW (sf);
8874 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8875
8876 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8877 {
8878 XWINDOW (mini_window)->must_be_updated_p = 1;
8879 pause |= update_frame (mini_frame, 0, 0);
8880 if (!pause && hscroll_windows (mini_window))
8881 goto retry;
8882 }
8883 }
8884
8885 /* If display was paused because of pending input, make sure we do a
8886 thorough update the next time. */
8887 if (pause)
8888 {
8889 /* Prevent the optimization at the beginning of
8890 redisplay_internal that tries a single-line update of the
8891 line containing the cursor in the selected window. */
8892 CHARPOS (this_line_start_pos) = 0;
8893
8894 /* Let the overlay arrow be updated the next time. */
8895 if (!NILP (last_arrow_position))
8896 {
8897 last_arrow_position = Qt;
8898 last_arrow_string = Qt;
8899 }
8900
8901 /* If we pause after scrolling, some rows in the current
8902 matrices of some windows are not valid. */
8903 if (!WINDOW_FULL_WIDTH_P (w)
8904 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8905 update_mode_lines = 1;
8906 }
8907 else
8908 {
8909 if (!consider_all_windows_p)
8910 {
8911 /* This has already been done above if
8912 consider_all_windows_p is set. */
8913 mark_window_display_accurate_1 (w, 1);
8914
8915 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8916 last_arrow_string = Voverlay_arrow_string;
8917
8918 if (frame_up_to_date_hook != 0)
8919 frame_up_to_date_hook (sf);
8920 }
8921
8922 update_mode_lines = 0;
8923 windows_or_buffers_changed = 0;
8924 }
8925
8926 /* Start SIGIO interrupts coming again. Having them off during the
8927 code above makes it less likely one will discard output, but not
8928 impossible, since there might be stuff in the system buffer here.
8929 But it is much hairier to try to do anything about that. */
8930 if (interrupt_input)
8931 request_sigio ();
8932 start_polling ();
8933
8934 /* If a frame has become visible which was not before, redisplay
8935 again, so that we display it. Expose events for such a frame
8936 (which it gets when becoming visible) don't call the parts of
8937 redisplay constructing glyphs, so simply exposing a frame won't
8938 display anything in this case. So, we have to display these
8939 frames here explicitly. */
8940 if (!pause)
8941 {
8942 Lisp_Object tail, frame;
8943 int new_count = 0;
8944
8945 FOR_EACH_FRAME (tail, frame)
8946 {
8947 int this_is_visible = 0;
8948
8949 if (XFRAME (frame)->visible)
8950 this_is_visible = 1;
8951 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8952 if (XFRAME (frame)->visible)
8953 this_is_visible = 1;
8954
8955 if (this_is_visible)
8956 new_count++;
8957 }
8958
8959 if (new_count != number_of_visible_frames)
8960 windows_or_buffers_changed++;
8961 }
8962
8963 /* Change frame size now if a change is pending. */
8964 do_pending_window_change (1);
8965
8966 /* If we just did a pending size change, or have additional
8967 visible frames, redisplay again. */
8968 if (windows_or_buffers_changed && !pause)
8969 goto retry;
8970
8971 end_of_redisplay:;
8972
8973 unbind_to (count, Qnil);
8974 }
8975
8976
8977 /* Redisplay, but leave alone any recent echo area message unless
8978 another message has been requested in its place.
8979
8980 This is useful in situations where you need to redisplay but no
8981 user action has occurred, making it inappropriate for the message
8982 area to be cleared. See tracking_off and
8983 wait_reading_process_input for examples of these situations.
8984
8985 FROM_WHERE is an integer saying from where this function was
8986 called. This is useful for debugging. */
8987
8988 void
8989 redisplay_preserve_echo_area (from_where)
8990 int from_where;
8991 {
8992 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8993
8994 if (!NILP (echo_area_buffer[1]))
8995 {
8996 /* We have a previously displayed message, but no current
8997 message. Redisplay the previous message. */
8998 display_last_displayed_message_p = 1;
8999 redisplay_internal (1);
9000 display_last_displayed_message_p = 0;
9001 }
9002 else
9003 redisplay_internal (1);
9004 }
9005
9006
9007 /* Function registered with record_unwind_protect in
9008 redisplay_internal. Clears the flag indicating that a redisplay is
9009 in progress. */
9010
9011 static Lisp_Object
9012 unwind_redisplay (old_redisplaying_p)
9013 Lisp_Object old_redisplaying_p;
9014 {
9015 redisplaying_p = XFASTINT (old_redisplaying_p);
9016 return Qnil;
9017 }
9018
9019
9020 /* Mark the display of window W as accurate or inaccurate. If
9021 ACCURATE_P is non-zero mark display of W as accurate. If
9022 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9023 redisplay_internal is called. */
9024
9025 static void
9026 mark_window_display_accurate_1 (w, accurate_p)
9027 struct window *w;
9028 int accurate_p;
9029 {
9030 if (BUFFERP (w->buffer))
9031 {
9032 struct buffer *b = XBUFFER (w->buffer);
9033
9034 w->last_modified
9035 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9036 w->last_overlay_modified
9037 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9038 w->last_had_star
9039 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9040
9041 if (accurate_p)
9042 {
9043 b->clip_changed = 0;
9044 b->prevent_redisplay_optimizations_p = 0;
9045
9046 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9047 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9048 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9049 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9050
9051 w->current_matrix->buffer = b;
9052 w->current_matrix->begv = BUF_BEGV (b);
9053 w->current_matrix->zv = BUF_ZV (b);
9054
9055 w->last_cursor = w->cursor;
9056 w->last_cursor_off_p = w->cursor_off_p;
9057
9058 if (w == XWINDOW (selected_window))
9059 w->last_point = make_number (BUF_PT (b));
9060 else
9061 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9062 }
9063 }
9064
9065 if (accurate_p)
9066 {
9067 w->window_end_valid = w->buffer;
9068 #if 0 /* This is incorrect with variable-height lines. */
9069 xassert (XINT (w->window_end_vpos)
9070 < (XINT (w->height)
9071 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9072 #endif
9073 w->update_mode_line = Qnil;
9074 }
9075 }
9076
9077
9078 /* Mark the display of windows in the window tree rooted at WINDOW as
9079 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9080 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9081 be redisplayed the next time redisplay_internal is called. */
9082
9083 void
9084 mark_window_display_accurate (window, accurate_p)
9085 Lisp_Object window;
9086 int accurate_p;
9087 {
9088 struct window *w;
9089
9090 for (; !NILP (window); window = w->next)
9091 {
9092 w = XWINDOW (window);
9093 mark_window_display_accurate_1 (w, accurate_p);
9094
9095 if (!NILP (w->vchild))
9096 mark_window_display_accurate (w->vchild, accurate_p);
9097 if (!NILP (w->hchild))
9098 mark_window_display_accurate (w->hchild, accurate_p);
9099 }
9100
9101 if (accurate_p)
9102 {
9103 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9104 last_arrow_string = Voverlay_arrow_string;
9105 }
9106 else
9107 {
9108 /* Force a thorough redisplay the next time by setting
9109 last_arrow_position and last_arrow_string to t, which is
9110 unequal to any useful value of Voverlay_arrow_... */
9111 last_arrow_position = Qt;
9112 last_arrow_string = Qt;
9113 }
9114 }
9115
9116
9117 /* Return value in display table DP (Lisp_Char_Table *) for character
9118 C. Since a display table doesn't have any parent, we don't have to
9119 follow parent. Do not call this function directly but use the
9120 macro DISP_CHAR_VECTOR. */
9121
9122 Lisp_Object
9123 disp_char_vector (dp, c)
9124 struct Lisp_Char_Table *dp;
9125 int c;
9126 {
9127 int code[4], i;
9128 Lisp_Object val;
9129
9130 if (SINGLE_BYTE_CHAR_P (c))
9131 return (dp->contents[c]);
9132
9133 SPLIT_CHAR (c, code[0], code[1], code[2]);
9134 if (code[1] < 32)
9135 code[1] = -1;
9136 else if (code[2] < 32)
9137 code[2] = -1;
9138
9139 /* Here, the possible range of code[0] (== charset ID) is
9140 128..max_charset. Since the top level char table contains data
9141 for multibyte characters after 256th element, we must increment
9142 code[0] by 128 to get a correct index. */
9143 code[0] += 128;
9144 code[3] = -1; /* anchor */
9145
9146 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9147 {
9148 val = dp->contents[code[i]];
9149 if (!SUB_CHAR_TABLE_P (val))
9150 return (NILP (val) ? dp->defalt : val);
9151 }
9152
9153 /* Here, val is a sub char table. We return the default value of
9154 it. */
9155 return (dp->defalt);
9156 }
9157
9158
9159 \f
9160 /***********************************************************************
9161 Window Redisplay
9162 ***********************************************************************/
9163
9164 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9165
9166 static void
9167 redisplay_windows (window)
9168 Lisp_Object window;
9169 {
9170 while (!NILP (window))
9171 {
9172 struct window *w = XWINDOW (window);
9173
9174 if (!NILP (w->hchild))
9175 redisplay_windows (w->hchild);
9176 else if (!NILP (w->vchild))
9177 redisplay_windows (w->vchild);
9178 else
9179 redisplay_window (window, 0);
9180
9181 window = w->next;
9182 }
9183 }
9184
9185
9186 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9187 DELTA is the number of bytes by which positions recorded in ROW
9188 differ from current buffer positions. */
9189
9190 void
9191 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9192 struct window *w;
9193 struct glyph_row *row;
9194 struct glyph_matrix *matrix;
9195 int delta, delta_bytes, dy, dvpos;
9196 {
9197 struct glyph *glyph = row->glyphs[TEXT_AREA];
9198 struct glyph *end = glyph + row->used[TEXT_AREA];
9199 int x = row->x;
9200 int pt_old = PT - delta;
9201
9202 /* Skip over glyphs not having an object at the start of the row.
9203 These are special glyphs like truncation marks on terminal
9204 frames. */
9205 if (row->displays_text_p)
9206 while (glyph < end
9207 && INTEGERP (glyph->object)
9208 && glyph->charpos < 0)
9209 {
9210 x += glyph->pixel_width;
9211 ++glyph;
9212 }
9213
9214 while (glyph < end
9215 && !INTEGERP (glyph->object)
9216 && (!BUFFERP (glyph->object)
9217 || glyph->charpos < pt_old))
9218 {
9219 x += glyph->pixel_width;
9220 ++glyph;
9221 }
9222
9223 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9224 w->cursor.x = x;
9225 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9226 w->cursor.y = row->y + dy;
9227
9228 if (w == XWINDOW (selected_window))
9229 {
9230 if (!row->continued_p
9231 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9232 && row->x == 0)
9233 {
9234 this_line_buffer = XBUFFER (w->buffer);
9235
9236 CHARPOS (this_line_start_pos)
9237 = MATRIX_ROW_START_CHARPOS (row) + delta;
9238 BYTEPOS (this_line_start_pos)
9239 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9240
9241 CHARPOS (this_line_end_pos)
9242 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9243 BYTEPOS (this_line_end_pos)
9244 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9245
9246 this_line_y = w->cursor.y;
9247 this_line_pixel_height = row->height;
9248 this_line_vpos = w->cursor.vpos;
9249 this_line_start_x = row->x;
9250 }
9251 else
9252 CHARPOS (this_line_start_pos) = 0;
9253 }
9254 }
9255
9256
9257 /* Run window scroll functions, if any, for WINDOW with new window
9258 start STARTP. Sets the window start of WINDOW to that position.
9259
9260 We assume that the window's buffer is really current. */
9261
9262 static INLINE struct text_pos
9263 run_window_scroll_functions (window, startp)
9264 Lisp_Object window;
9265 struct text_pos startp;
9266 {
9267 struct window *w = XWINDOW (window);
9268 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9269
9270 if (current_buffer != XBUFFER (w->buffer))
9271 abort ();
9272
9273 if (!NILP (Vwindow_scroll_functions))
9274 {
9275 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9276 make_number (CHARPOS (startp)));
9277 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9278 /* In case the hook functions switch buffers. */
9279 if (current_buffer != XBUFFER (w->buffer))
9280 set_buffer_internal_1 (XBUFFER (w->buffer));
9281 }
9282
9283 return startp;
9284 }
9285
9286
9287 /* Modify the desired matrix of window W and W->vscroll so that the
9288 line containing the cursor is fully visible. */
9289
9290 static void
9291 make_cursor_line_fully_visible (w)
9292 struct window *w;
9293 {
9294 struct glyph_matrix *matrix;
9295 struct glyph_row *row;
9296 int window_height;
9297
9298 /* It's not always possible to find the cursor, e.g, when a window
9299 is full of overlay strings. Don't do anything in that case. */
9300 if (w->cursor.vpos < 0)
9301 return;
9302
9303 matrix = w->desired_matrix;
9304 row = MATRIX_ROW (matrix, w->cursor.vpos);
9305
9306 /* If the cursor row is not partially visible, there's nothing
9307 to do. */
9308 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9309 return;
9310
9311 /* If the row the cursor is in is taller than the window's height,
9312 it's not clear what to do, so do nothing. */
9313 window_height = window_box_height (w);
9314 if (row->height >= window_height)
9315 return;
9316
9317 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9318 {
9319 int dy = row->height - row->visible_height;
9320 w->vscroll = 0;
9321 w->cursor.y += dy;
9322 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9323 }
9324 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9325 {
9326 int dy = - (row->height - row->visible_height);
9327 w->vscroll = dy;
9328 w->cursor.y += dy;
9329 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9330 }
9331
9332 /* When we change the cursor y-position of the selected window,
9333 change this_line_y as well so that the display optimization for
9334 the cursor line of the selected window in redisplay_internal uses
9335 the correct y-position. */
9336 if (w == XWINDOW (selected_window))
9337 this_line_y = w->cursor.y;
9338 }
9339
9340
9341 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9342 non-zero means only WINDOW is redisplayed in redisplay_internal.
9343 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9344 in redisplay_window to bring a partially visible line into view in
9345 the case that only the cursor has moved.
9346
9347 Value is
9348
9349 1 if scrolling succeeded
9350
9351 0 if scrolling didn't find point.
9352
9353 -1 if new fonts have been loaded so that we must interrupt
9354 redisplay, adjust glyph matrices, and try again. */
9355
9356 static int
9357 try_scrolling (window, just_this_one_p, scroll_conservatively,
9358 scroll_step, temp_scroll_step)
9359 Lisp_Object window;
9360 int just_this_one_p;
9361 int scroll_conservatively, scroll_step;
9362 int temp_scroll_step;
9363 {
9364 struct window *w = XWINDOW (window);
9365 struct frame *f = XFRAME (w->frame);
9366 struct text_pos scroll_margin_pos;
9367 struct text_pos pos;
9368 struct text_pos startp;
9369 struct it it;
9370 Lisp_Object window_end;
9371 int this_scroll_margin;
9372 int dy = 0;
9373 int scroll_max;
9374 int rc;
9375 int amount_to_scroll = 0;
9376 Lisp_Object aggressive;
9377 int height;
9378
9379 #if GLYPH_DEBUG
9380 debug_method_add (w, "try_scrolling");
9381 #endif
9382
9383 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9384
9385 /* Compute scroll margin height in pixels. We scroll when point is
9386 within this distance from the top or bottom of the window. */
9387 if (scroll_margin > 0)
9388 {
9389 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9390 this_scroll_margin *= CANON_Y_UNIT (f);
9391 }
9392 else
9393 this_scroll_margin = 0;
9394
9395 /* Compute how much we should try to scroll maximally to bring point
9396 into view. */
9397 if (scroll_step || scroll_conservatively || temp_scroll_step)
9398 scroll_max = max (scroll_step,
9399 max (scroll_conservatively, temp_scroll_step));
9400 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9401 || NUMBERP (current_buffer->scroll_up_aggressively))
9402 /* We're trying to scroll because of aggressive scrolling
9403 but no scroll_step is set. Choose an arbitrary one. Maybe
9404 there should be a variable for this. */
9405 scroll_max = 10;
9406 else
9407 scroll_max = 0;
9408 scroll_max *= CANON_Y_UNIT (f);
9409
9410 /* Decide whether we have to scroll down. Start at the window end
9411 and move this_scroll_margin up to find the position of the scroll
9412 margin. */
9413 window_end = Fwindow_end (window, Qt);
9414 CHARPOS (scroll_margin_pos) = XINT (window_end);
9415 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9416 if (this_scroll_margin)
9417 {
9418 start_display (&it, w, scroll_margin_pos);
9419 move_it_vertically (&it, - this_scroll_margin);
9420 scroll_margin_pos = it.current.pos;
9421 }
9422
9423 if (PT >= CHARPOS (scroll_margin_pos))
9424 {
9425 int y0;
9426
9427 /* Point is in the scroll margin at the bottom of the window, or
9428 below. Compute a new window start that makes point visible. */
9429
9430 /* Compute the distance from the scroll margin to PT.
9431 Give up if the distance is greater than scroll_max. */
9432 start_display (&it, w, scroll_margin_pos);
9433 y0 = it.current_y;
9434 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9435 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9436
9437 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9438 end, which is one line below the window. The iterator's
9439 current_y will be same as y0 in that case, but we have to
9440 scroll a line to make PT visible. That's the reason why 1 is
9441 added below. */
9442 dy = 1 + it.current_y - y0;
9443
9444 if (dy > scroll_max)
9445 return 0;
9446
9447 /* Move the window start down. If scrolling conservatively,
9448 move it just enough down to make point visible. If
9449 scroll_step is set, move it down by scroll_step. */
9450 start_display (&it, w, startp);
9451
9452 if (scroll_conservatively)
9453 amount_to_scroll
9454 = max (max (dy, CANON_Y_UNIT (f)),
9455 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9456 else if (scroll_step || temp_scroll_step)
9457 amount_to_scroll = scroll_max;
9458 else
9459 {
9460 aggressive = current_buffer->scroll_down_aggressively;
9461 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9462 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9463 if (NUMBERP (aggressive))
9464 amount_to_scroll = XFLOATINT (aggressive) * height;
9465 }
9466
9467 if (amount_to_scroll <= 0)
9468 return 0;
9469
9470 move_it_vertically (&it, amount_to_scroll);
9471 startp = it.current.pos;
9472 }
9473 else
9474 {
9475 /* See if point is inside the scroll margin at the top of the
9476 window. */
9477 scroll_margin_pos = startp;
9478 if (this_scroll_margin)
9479 {
9480 start_display (&it, w, startp);
9481 move_it_vertically (&it, this_scroll_margin);
9482 scroll_margin_pos = it.current.pos;
9483 }
9484
9485 if (PT < CHARPOS (scroll_margin_pos))
9486 {
9487 /* Point is in the scroll margin at the top of the window or
9488 above what is displayed in the window. */
9489 int y0;
9490
9491 /* Compute the vertical distance from PT to the scroll
9492 margin position. Give up if distance is greater than
9493 scroll_max. */
9494 SET_TEXT_POS (pos, PT, PT_BYTE);
9495 start_display (&it, w, pos);
9496 y0 = it.current_y;
9497 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9498 it.last_visible_y, -1,
9499 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9500 dy = it.current_y - y0;
9501 if (dy > scroll_max)
9502 return 0;
9503
9504 /* Compute new window start. */
9505 start_display (&it, w, startp);
9506
9507 if (scroll_conservatively)
9508 amount_to_scroll =
9509 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9510 else if (scroll_step || temp_scroll_step)
9511 amount_to_scroll = scroll_max;
9512 else
9513 {
9514 aggressive = current_buffer->scroll_up_aggressively;
9515 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9516 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9517 if (NUMBERP (aggressive))
9518 amount_to_scroll = XFLOATINT (aggressive) * height;
9519 }
9520
9521 if (amount_to_scroll <= 0)
9522 return 0;
9523
9524 move_it_vertically (&it, - amount_to_scroll);
9525 startp = it.current.pos;
9526 }
9527 }
9528
9529 /* Run window scroll functions. */
9530 startp = run_window_scroll_functions (window, startp);
9531
9532 /* Display the window. Give up if new fonts are loaded, or if point
9533 doesn't appear. */
9534 if (!try_window (window, startp))
9535 rc = -1;
9536 else if (w->cursor.vpos < 0)
9537 {
9538 clear_glyph_matrix (w->desired_matrix);
9539 rc = 0;
9540 }
9541 else
9542 {
9543 /* Maybe forget recorded base line for line number display. */
9544 if (!just_this_one_p
9545 || current_buffer->clip_changed
9546 || BEG_UNCHANGED < CHARPOS (startp))
9547 w->base_line_number = Qnil;
9548
9549 /* If cursor ends up on a partially visible line, shift display
9550 lines up or down. */
9551 make_cursor_line_fully_visible (w);
9552 rc = 1;
9553 }
9554
9555 return rc;
9556 }
9557
9558
9559 /* Compute a suitable window start for window W if display of W starts
9560 on a continuation line. Value is non-zero if a new window start
9561 was computed.
9562
9563 The new window start will be computed, based on W's width, starting
9564 from the start of the continued line. It is the start of the
9565 screen line with the minimum distance from the old start W->start. */
9566
9567 static int
9568 compute_window_start_on_continuation_line (w)
9569 struct window *w;
9570 {
9571 struct text_pos pos, start_pos;
9572 int window_start_changed_p = 0;
9573
9574 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9575
9576 /* If window start is on a continuation line... Window start may be
9577 < BEGV in case there's invisible text at the start of the
9578 buffer (M-x rmail, for example). */
9579 if (CHARPOS (start_pos) > BEGV
9580 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9581 {
9582 struct it it;
9583 struct glyph_row *row;
9584
9585 /* Handle the case that the window start is out of range. */
9586 if (CHARPOS (start_pos) < BEGV)
9587 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9588 else if (CHARPOS (start_pos) > ZV)
9589 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9590
9591 /* Find the start of the continued line. This should be fast
9592 because scan_buffer is fast (newline cache). */
9593 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9594 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9595 row, DEFAULT_FACE_ID);
9596 reseat_at_previous_visible_line_start (&it);
9597
9598 /* If the line start is "too far" away from the window start,
9599 say it takes too much time to compute a new window start. */
9600 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9601 < XFASTINT (w->height) * XFASTINT (w->width))
9602 {
9603 int min_distance, distance;
9604
9605 /* Move forward by display lines to find the new window
9606 start. If window width was enlarged, the new start can
9607 be expected to be > the old start. If window width was
9608 decreased, the new window start will be < the old start.
9609 So, we're looking for the display line start with the
9610 minimum distance from the old window start. */
9611 pos = it.current.pos;
9612 min_distance = INFINITY;
9613 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9614 distance < min_distance)
9615 {
9616 min_distance = distance;
9617 pos = it.current.pos;
9618 move_it_by_lines (&it, 1, 0);
9619 }
9620
9621 /* Set the window start there. */
9622 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9623 window_start_changed_p = 1;
9624 }
9625 }
9626
9627 return window_start_changed_p;
9628 }
9629
9630
9631 /* Try cursor movement in case text has not changes in window WINDOW,
9632 with window start STARTP. Value is
9633
9634 1 if successful
9635
9636 0 if this method cannot be used
9637
9638 -1 if we know we have to scroll the display. *SCROLL_STEP is
9639 set to 1, under certain circumstances, if we want to scroll as
9640 if scroll-step were set to 1. See the code. */
9641
9642 static int
9643 try_cursor_movement (window, startp, scroll_step)
9644 Lisp_Object window;
9645 struct text_pos startp;
9646 int *scroll_step;
9647 {
9648 struct window *w = XWINDOW (window);
9649 struct frame *f = XFRAME (w->frame);
9650 int rc = 0;
9651
9652 /* Handle case where text has not changed, only point, and it has
9653 not moved off the frame. */
9654 if (/* Point may be in this window. */
9655 PT >= CHARPOS (startp)
9656 /* Selective display hasn't changed. */
9657 && !current_buffer->clip_changed
9658 /* Function force-mode-line-update is used to force a thorough
9659 redisplay. It sets either windows_or_buffers_changed or
9660 update_mode_lines. So don't take a shortcut here for these
9661 cases. */
9662 && !update_mode_lines
9663 && !windows_or_buffers_changed
9664 /* Can't use this case if highlighting a region. When a
9665 region exists, cursor movement has to do more than just
9666 set the cursor. */
9667 && !(!NILP (Vtransient_mark_mode)
9668 && !NILP (current_buffer->mark_active))
9669 && NILP (w->region_showing)
9670 && NILP (Vshow_trailing_whitespace)
9671 /* Right after splitting windows, last_point may be nil. */
9672 && INTEGERP (w->last_point)
9673 /* This code is not used for mini-buffer for the sake of the case
9674 of redisplaying to replace an echo area message; since in
9675 that case the mini-buffer contents per se are usually
9676 unchanged. This code is of no real use in the mini-buffer
9677 since the handling of this_line_start_pos, etc., in redisplay
9678 handles the same cases. */
9679 && !EQ (window, minibuf_window)
9680 /* When splitting windows or for new windows, it happens that
9681 redisplay is called with a nil window_end_vpos or one being
9682 larger than the window. This should really be fixed in
9683 window.c. I don't have this on my list, now, so we do
9684 approximately the same as the old redisplay code. --gerd. */
9685 && INTEGERP (w->window_end_vpos)
9686 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9687 && (FRAME_WINDOW_P (f)
9688 || !MARKERP (Voverlay_arrow_position)
9689 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9690 {
9691 int this_scroll_margin;
9692 struct glyph_row *row = NULL;
9693
9694 #if GLYPH_DEBUG
9695 debug_method_add (w, "cursor movement");
9696 #endif
9697
9698 /* Scroll if point within this distance from the top or bottom
9699 of the window. This is a pixel value. */
9700 this_scroll_margin = max (0, scroll_margin);
9701 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9702 this_scroll_margin *= CANON_Y_UNIT (f);
9703
9704 /* Start with the row the cursor was displayed during the last
9705 not paused redisplay. Give up if that row is not valid. */
9706 if (w->last_cursor.vpos < 0
9707 || w->last_cursor.vpos >= w->current_matrix->nrows)
9708 rc = -1;
9709 else
9710 {
9711 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9712 if (row->mode_line_p)
9713 ++row;
9714 if (!row->enabled_p)
9715 rc = -1;
9716 }
9717
9718 if (rc == 0)
9719 {
9720 int scroll_p = 0;
9721 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9722
9723 if (PT > XFASTINT (w->last_point))
9724 {
9725 /* Point has moved forward. */
9726 while (MATRIX_ROW_END_CHARPOS (row) < PT
9727 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9728 {
9729 xassert (row->enabled_p);
9730 ++row;
9731 }
9732
9733 /* The end position of a row equals the start position
9734 of the next row. If PT is there, we would rather
9735 display it in the next line. */
9736 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9737 && MATRIX_ROW_END_CHARPOS (row) == PT
9738 && !cursor_row_p (w, row))
9739 ++row;
9740
9741 /* If within the scroll margin, scroll. Note that
9742 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9743 the next line would be drawn, and that
9744 this_scroll_margin can be zero. */
9745 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9746 || PT > MATRIX_ROW_END_CHARPOS (row)
9747 /* Line is completely visible last line in window
9748 and PT is to be set in the next line. */
9749 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9750 && PT == MATRIX_ROW_END_CHARPOS (row)
9751 && !row->ends_at_zv_p
9752 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9753 scroll_p = 1;
9754 }
9755 else if (PT < XFASTINT (w->last_point))
9756 {
9757 /* Cursor has to be moved backward. Note that PT >=
9758 CHARPOS (startp) because of the outer
9759 if-statement. */
9760 while (!row->mode_line_p
9761 && (MATRIX_ROW_START_CHARPOS (row) > PT
9762 || (MATRIX_ROW_START_CHARPOS (row) == PT
9763 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9764 && (row->y > this_scroll_margin
9765 || CHARPOS (startp) == BEGV))
9766 {
9767 xassert (row->enabled_p);
9768 --row;
9769 }
9770
9771 /* Consider the following case: Window starts at BEGV,
9772 there is invisible, intangible text at BEGV, so that
9773 display starts at some point START > BEGV. It can
9774 happen that we are called with PT somewhere between
9775 BEGV and START. Try to handle that case. */
9776 if (row < w->current_matrix->rows
9777 || row->mode_line_p)
9778 {
9779 row = w->current_matrix->rows;
9780 if (row->mode_line_p)
9781 ++row;
9782 }
9783
9784 /* Due to newlines in overlay strings, we may have to
9785 skip forward over overlay strings. */
9786 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9787 && MATRIX_ROW_END_CHARPOS (row) == PT
9788 && !cursor_row_p (w, row))
9789 ++row;
9790
9791 /* If within the scroll margin, scroll. */
9792 if (row->y < this_scroll_margin
9793 && CHARPOS (startp) != BEGV)
9794 scroll_p = 1;
9795 }
9796
9797 if (PT < MATRIX_ROW_START_CHARPOS (row)
9798 || PT > MATRIX_ROW_END_CHARPOS (row))
9799 {
9800 /* if PT is not in the glyph row, give up. */
9801 rc = -1;
9802 }
9803 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9804 {
9805 if (PT == MATRIX_ROW_END_CHARPOS (row)
9806 && !row->ends_at_zv_p
9807 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9808 rc = -1;
9809 else if (row->height > window_box_height (w))
9810 {
9811 /* If we end up in a partially visible line, let's
9812 make it fully visible, except when it's taller
9813 than the window, in which case we can't do much
9814 about it. */
9815 *scroll_step = 1;
9816 rc = -1;
9817 }
9818 else
9819 {
9820 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9821 try_window (window, startp);
9822 make_cursor_line_fully_visible (w);
9823 rc = 1;
9824 }
9825 }
9826 else if (scroll_p)
9827 rc = -1;
9828 else
9829 {
9830 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9831 rc = 1;
9832 }
9833 }
9834 }
9835
9836 return rc;
9837 }
9838
9839
9840 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9841 selected_window is redisplayed. */
9842
9843 static void
9844 redisplay_window (window, just_this_one_p)
9845 Lisp_Object window;
9846 int just_this_one_p;
9847 {
9848 struct window *w = XWINDOW (window);
9849 struct frame *f = XFRAME (w->frame);
9850 struct buffer *buffer = XBUFFER (w->buffer);
9851 struct buffer *old = current_buffer;
9852 struct text_pos lpoint, opoint, startp;
9853 int update_mode_line;
9854 int tem;
9855 struct it it;
9856 /* Record it now because it's overwritten. */
9857 int current_matrix_up_to_date_p = 0;
9858 int temp_scroll_step = 0;
9859 int count = BINDING_STACK_SIZE ();
9860 int rc;
9861
9862 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9863 opoint = lpoint;
9864
9865 /* W must be a leaf window here. */
9866 xassert (!NILP (w->buffer));
9867 #if GLYPH_DEBUG
9868 *w->desired_matrix->method = 0;
9869 #endif
9870
9871 specbind (Qinhibit_point_motion_hooks, Qt);
9872
9873 reconsider_clip_changes (w, buffer);
9874
9875 /* Has the mode line to be updated? */
9876 update_mode_line = (!NILP (w->update_mode_line)
9877 || update_mode_lines
9878 || buffer->clip_changed);
9879
9880 if (MINI_WINDOW_P (w))
9881 {
9882 if (w == XWINDOW (echo_area_window)
9883 && !NILP (echo_area_buffer[0]))
9884 {
9885 if (update_mode_line)
9886 /* We may have to update a tty frame's menu bar or a
9887 tool-bar. Example `M-x C-h C-h C-g'. */
9888 goto finish_menu_bars;
9889 else
9890 /* We've already displayed the echo area glyphs in this window. */
9891 goto finish_scroll_bars;
9892 }
9893 else if (w != XWINDOW (minibuf_window))
9894 {
9895 /* W is a mini-buffer window, but it's not the currently
9896 active one, so clear it. */
9897 int yb = window_text_bottom_y (w);
9898 struct glyph_row *row;
9899 int y;
9900
9901 for (y = 0, row = w->desired_matrix->rows;
9902 y < yb;
9903 y += row->height, ++row)
9904 blank_row (w, row, y);
9905 goto finish_scroll_bars;
9906 }
9907
9908 clear_glyph_matrix (w->desired_matrix);
9909 }
9910
9911 /* Otherwise set up data on this window; select its buffer and point
9912 value. */
9913 /* Really select the buffer, for the sake of buffer-local
9914 variables. */
9915 set_buffer_internal_1 (XBUFFER (w->buffer));
9916 SET_TEXT_POS (opoint, PT, PT_BYTE);
9917
9918 current_matrix_up_to_date_p
9919 = (!NILP (w->window_end_valid)
9920 && !current_buffer->clip_changed
9921 && XFASTINT (w->last_modified) >= MODIFF
9922 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9923
9924 /* When windows_or_buffers_changed is non-zero, we can't rely on
9925 the window end being valid, so set it to nil there. */
9926 if (windows_or_buffers_changed)
9927 {
9928 /* If window starts on a continuation line, maybe adjust the
9929 window start in case the window's width changed. */
9930 if (XMARKER (w->start)->buffer == current_buffer)
9931 compute_window_start_on_continuation_line (w);
9932
9933 w->window_end_valid = Qnil;
9934 }
9935
9936 /* Some sanity checks. */
9937 CHECK_WINDOW_END (w);
9938 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9939 abort ();
9940 if (BYTEPOS (opoint) < CHARPOS (opoint))
9941 abort ();
9942
9943 /* If %c is in mode line, update it if needed. */
9944 if (!NILP (w->column_number_displayed)
9945 /* This alternative quickly identifies a common case
9946 where no change is needed. */
9947 && !(PT == XFASTINT (w->last_point)
9948 && XFASTINT (w->last_modified) >= MODIFF
9949 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9950 && XFASTINT (w->column_number_displayed) != current_column ())
9951 update_mode_line = 1;
9952
9953 /* Count number of windows showing the selected buffer. An indirect
9954 buffer counts as its base buffer. */
9955 if (!just_this_one_p)
9956 {
9957 struct buffer *current_base, *window_base;
9958 current_base = current_buffer;
9959 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9960 if (current_base->base_buffer)
9961 current_base = current_base->base_buffer;
9962 if (window_base->base_buffer)
9963 window_base = window_base->base_buffer;
9964 if (current_base == window_base)
9965 buffer_shared++;
9966 }
9967
9968 /* Point refers normally to the selected window. For any other
9969 window, set up appropriate value. */
9970 if (!EQ (window, selected_window))
9971 {
9972 int new_pt = XMARKER (w->pointm)->charpos;
9973 int new_pt_byte = marker_byte_position (w->pointm);
9974 if (new_pt < BEGV)
9975 {
9976 new_pt = BEGV;
9977 new_pt_byte = BEGV_BYTE;
9978 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9979 }
9980 else if (new_pt > (ZV - 1))
9981 {
9982 new_pt = ZV;
9983 new_pt_byte = ZV_BYTE;
9984 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9985 }
9986
9987 /* We don't use SET_PT so that the point-motion hooks don't run. */
9988 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9989 }
9990
9991 /* If any of the character widths specified in the display table
9992 have changed, invalidate the width run cache. It's true that
9993 this may be a bit late to catch such changes, but the rest of
9994 redisplay goes (non-fatally) haywire when the display table is
9995 changed, so why should we worry about doing any better? */
9996 if (current_buffer->width_run_cache)
9997 {
9998 struct Lisp_Char_Table *disptab = buffer_display_table ();
9999
10000 if (! disptab_matches_widthtab (disptab,
10001 XVECTOR (current_buffer->width_table)))
10002 {
10003 invalidate_region_cache (current_buffer,
10004 current_buffer->width_run_cache,
10005 BEG, Z);
10006 recompute_width_table (current_buffer, disptab);
10007 }
10008 }
10009
10010 /* If window-start is screwed up, choose a new one. */
10011 if (XMARKER (w->start)->buffer != current_buffer)
10012 goto recenter;
10013
10014 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10015
10016 /* If someone specified a new starting point but did not insist,
10017 check whether it can be used. */
10018 if (!NILP (w->optional_new_start)
10019 && CHARPOS (startp) >= BEGV
10020 && CHARPOS (startp) <= ZV)
10021 {
10022 w->optional_new_start = Qnil;
10023 start_display (&it, w, startp);
10024 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10025 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10026 if (IT_CHARPOS (it) == PT)
10027 w->force_start = Qt;
10028 }
10029
10030 /* Handle case where place to start displaying has been specified,
10031 unless the specified location is outside the accessible range. */
10032 if (!NILP (w->force_start)
10033 || w->frozen_window_start_p)
10034 {
10035 w->force_start = Qnil;
10036 w->vscroll = 0;
10037 w->window_end_valid = Qnil;
10038
10039 /* Forget any recorded base line for line number display. */
10040 if (!current_matrix_up_to_date_p
10041 || current_buffer->clip_changed)
10042 w->base_line_number = Qnil;
10043
10044 /* Redisplay the mode line. Select the buffer properly for that.
10045 Also, run the hook window-scroll-functions
10046 because we have scrolled. */
10047 /* Note, we do this after clearing force_start because
10048 if there's an error, it is better to forget about force_start
10049 than to get into an infinite loop calling the hook functions
10050 and having them get more errors. */
10051 if (!update_mode_line
10052 || ! NILP (Vwindow_scroll_functions))
10053 {
10054 update_mode_line = 1;
10055 w->update_mode_line = Qt;
10056 startp = run_window_scroll_functions (window, startp);
10057 }
10058
10059 w->last_modified = make_number (0);
10060 w->last_overlay_modified = make_number (0);
10061 if (CHARPOS (startp) < BEGV)
10062 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10063 else if (CHARPOS (startp) > ZV)
10064 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10065
10066 /* Redisplay, then check if cursor has been set during the
10067 redisplay. Give up if new fonts were loaded. */
10068 if (!try_window (window, startp))
10069 {
10070 w->force_start = Qt;
10071 clear_glyph_matrix (w->desired_matrix);
10072 goto finish_scroll_bars;
10073 }
10074
10075 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10076 {
10077 /* If point does not appear, try to move point so it does
10078 appear. The desired matrix has been built above, so we
10079 can use it here. */
10080 int window_height;
10081 struct glyph_row *row;
10082
10083 window_height = window_box_height (w) / 2;
10084 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10085 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10086 ++row;
10087
10088 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10089 MATRIX_ROW_START_BYTEPOS (row));
10090
10091 if (w != XWINDOW (selected_window))
10092 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10093 else if (current_buffer == old)
10094 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10095
10096 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10097
10098 /* If we are highlighting the region, then we just changed
10099 the region, so redisplay to show it. */
10100 if (!NILP (Vtransient_mark_mode)
10101 && !NILP (current_buffer->mark_active))
10102 {
10103 clear_glyph_matrix (w->desired_matrix);
10104 if (!try_window (window, startp))
10105 goto finish_scroll_bars;
10106 }
10107 }
10108
10109 make_cursor_line_fully_visible (w);
10110 #if GLYPH_DEBUG
10111 debug_method_add (w, "forced window start");
10112 #endif
10113 goto done;
10114 }
10115
10116 /* Handle case where text has not changed, only point, and it has
10117 not moved off the frame. */
10118 if (current_matrix_up_to_date_p
10119 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10120 rc != 0))
10121 {
10122 if (rc == -1)
10123 goto try_to_scroll;
10124 else
10125 goto done;
10126 }
10127 /* If current starting point was originally the beginning of a line
10128 but no longer is, find a new starting point. */
10129 else if (!NILP (w->start_at_line_beg)
10130 && !(CHARPOS (startp) <= BEGV
10131 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10132 {
10133 #if GLYPH_DEBUG
10134 debug_method_add (w, "recenter 1");
10135 #endif
10136 goto recenter;
10137 }
10138
10139 /* Try scrolling with try_window_id. Value is > 0 if update has
10140 been done, it is -1 if we know that the same window start will
10141 not work. It is 0 if unsuccessful for some other reason. */
10142 else if ((tem = try_window_id (w)) != 0)
10143 {
10144 #if GLYPH_DEBUG
10145 debug_method_add (w, "try_window_id %d", tem);
10146 #endif
10147
10148 if (fonts_changed_p)
10149 goto finish_scroll_bars;
10150 if (tem > 0)
10151 goto done;
10152
10153 /* Otherwise try_window_id has returned -1 which means that we
10154 don't want the alternative below this comment to execute. */
10155 }
10156 else if (CHARPOS (startp) >= BEGV
10157 && CHARPOS (startp) <= ZV
10158 && PT >= CHARPOS (startp)
10159 && (CHARPOS (startp) < ZV
10160 /* Avoid starting at end of buffer. */
10161 || CHARPOS (startp) == BEGV
10162 || (XFASTINT (w->last_modified) >= MODIFF
10163 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10164 {
10165 #if GLYPH_DEBUG
10166 debug_method_add (w, "same window start");
10167 #endif
10168
10169 /* Try to redisplay starting at same place as before.
10170 If point has not moved off frame, accept the results. */
10171 if (!current_matrix_up_to_date_p
10172 /* Don't use try_window_reusing_current_matrix in this case
10173 because a window scroll function can have changed the
10174 buffer. */
10175 || !NILP (Vwindow_scroll_functions)
10176 || MINI_WINDOW_P (w)
10177 || !try_window_reusing_current_matrix (w))
10178 {
10179 IF_DEBUG (debug_method_add (w, "1"));
10180 try_window (window, startp);
10181 }
10182
10183 if (fonts_changed_p)
10184 goto finish_scroll_bars;
10185
10186 if (w->cursor.vpos >= 0)
10187 {
10188 if (!just_this_one_p
10189 || current_buffer->clip_changed
10190 || BEG_UNCHANGED < CHARPOS (startp))
10191 /* Forget any recorded base line for line number display. */
10192 w->base_line_number = Qnil;
10193
10194 make_cursor_line_fully_visible (w);
10195 goto done;
10196 }
10197 else
10198 clear_glyph_matrix (w->desired_matrix);
10199 }
10200
10201 try_to_scroll:
10202
10203 w->last_modified = make_number (0);
10204 w->last_overlay_modified = make_number (0);
10205
10206 /* Redisplay the mode line. Select the buffer properly for that. */
10207 if (!update_mode_line)
10208 {
10209 update_mode_line = 1;
10210 w->update_mode_line = Qt;
10211 }
10212
10213 /* Try to scroll by specified few lines. */
10214 if ((scroll_conservatively
10215 || scroll_step
10216 || temp_scroll_step
10217 || NUMBERP (current_buffer->scroll_up_aggressively)
10218 || NUMBERP (current_buffer->scroll_down_aggressively))
10219 && !current_buffer->clip_changed
10220 && CHARPOS (startp) >= BEGV
10221 && CHARPOS (startp) <= ZV)
10222 {
10223 /* The function returns -1 if new fonts were loaded, 1 if
10224 successful, 0 if not successful. */
10225 int rc = try_scrolling (window, just_this_one_p,
10226 scroll_conservatively,
10227 scroll_step,
10228 temp_scroll_step);
10229 if (rc > 0)
10230 goto done;
10231 else if (rc < 0)
10232 goto finish_scroll_bars;
10233 }
10234
10235 /* Finally, just choose place to start which centers point */
10236
10237 recenter:
10238
10239 #if GLYPH_DEBUG
10240 debug_method_add (w, "recenter");
10241 #endif
10242
10243 /* w->vscroll = 0; */
10244
10245 /* Forget any previously recorded base line for line number display. */
10246 if (!current_matrix_up_to_date_p
10247 || current_buffer->clip_changed)
10248 w->base_line_number = Qnil;
10249
10250 /* Move backward half the height of the window. */
10251 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10252 it.current_y = it.last_visible_y;
10253 move_it_vertically_backward (&it, window_box_height (w) / 2);
10254 xassert (IT_CHARPOS (it) >= BEGV);
10255
10256 /* The function move_it_vertically_backward may move over more
10257 than the specified y-distance. If it->w is small, e.g. a
10258 mini-buffer window, we may end up in front of the window's
10259 display area. Start displaying at the start of the line
10260 containing PT in this case. */
10261 if (it.current_y <= 0)
10262 {
10263 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10264 move_it_vertically (&it, 0);
10265 xassert (IT_CHARPOS (it) <= PT);
10266 it.current_y = 0;
10267 }
10268
10269 it.current_x = it.hpos = 0;
10270
10271 /* Set startp here explicitly in case that helps avoid an infinite loop
10272 in case the window-scroll-functions functions get errors. */
10273 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10274
10275 /* Run scroll hooks. */
10276 startp = run_window_scroll_functions (window, it.current.pos);
10277
10278 /* Redisplay the window. */
10279 if (!current_matrix_up_to_date_p
10280 || windows_or_buffers_changed
10281 /* Don't use try_window_reusing_current_matrix in this case
10282 because it can have changed the buffer. */
10283 || !NILP (Vwindow_scroll_functions)
10284 || !just_this_one_p
10285 || MINI_WINDOW_P (w)
10286 || !try_window_reusing_current_matrix (w))
10287 try_window (window, startp);
10288
10289 /* If new fonts have been loaded (due to fontsets), give up. We
10290 have to start a new redisplay since we need to re-adjust glyph
10291 matrices. */
10292 if (fonts_changed_p)
10293 goto finish_scroll_bars;
10294
10295 /* If cursor did not appear assume that the middle of the window is
10296 in the first line of the window. Do it again with the next line.
10297 (Imagine a window of height 100, displaying two lines of height
10298 60. Moving back 50 from it->last_visible_y will end in the first
10299 line.) */
10300 if (w->cursor.vpos < 0)
10301 {
10302 if (!NILP (w->window_end_valid)
10303 && PT >= Z - XFASTINT (w->window_end_pos))
10304 {
10305 clear_glyph_matrix (w->desired_matrix);
10306 move_it_by_lines (&it, 1, 0);
10307 try_window (window, it.current.pos);
10308 }
10309 else if (PT < IT_CHARPOS (it))
10310 {
10311 clear_glyph_matrix (w->desired_matrix);
10312 move_it_by_lines (&it, -1, 0);
10313 try_window (window, it.current.pos);
10314 }
10315 else
10316 {
10317 /* Not much we can do about it. */
10318 }
10319 }
10320
10321 /* Consider the following case: Window starts at BEGV, there is
10322 invisible, intangible text at BEGV, so that display starts at
10323 some point START > BEGV. It can happen that we are called with
10324 PT somewhere between BEGV and START. Try to handle that case. */
10325 if (w->cursor.vpos < 0)
10326 {
10327 struct glyph_row *row = w->current_matrix->rows;
10328 if (row->mode_line_p)
10329 ++row;
10330 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10331 }
10332
10333 make_cursor_line_fully_visible (w);
10334
10335 done:
10336
10337 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10338 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10339 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10340 ? Qt : Qnil);
10341
10342 /* Display the mode line, if we must. */
10343 if ((update_mode_line
10344 /* If window not full width, must redo its mode line
10345 if (a) the window to its side is being redone and
10346 (b) we do a frame-based redisplay. This is a consequence
10347 of how inverted lines are drawn in frame-based redisplay. */
10348 || (!just_this_one_p
10349 && !FRAME_WINDOW_P (f)
10350 && !WINDOW_FULL_WIDTH_P (w))
10351 /* Line number to display. */
10352 || INTEGERP (w->base_line_pos)
10353 /* Column number is displayed and different from the one displayed. */
10354 || (!NILP (w->column_number_displayed)
10355 && XFASTINT (w->column_number_displayed) != current_column ()))
10356 /* This means that the window has a mode line. */
10357 && (WINDOW_WANTS_MODELINE_P (w)
10358 || WINDOW_WANTS_HEADER_LINE_P (w)))
10359 {
10360 Lisp_Object old_selected_frame;
10361
10362 old_selected_frame = selected_frame;
10363
10364 XSETFRAME (selected_frame, f);
10365 display_mode_lines (w);
10366 selected_frame = old_selected_frame;
10367
10368 /* If mode line height has changed, arrange for a thorough
10369 immediate redisplay using the correct mode line height. */
10370 if (WINDOW_WANTS_MODELINE_P (w)
10371 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10372 {
10373 fonts_changed_p = 1;
10374 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10375 = DESIRED_MODE_LINE_HEIGHT (w);
10376 }
10377
10378 /* If top line height has changed, arrange for a thorough
10379 immediate redisplay using the correct mode line height. */
10380 if (WINDOW_WANTS_HEADER_LINE_P (w)
10381 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10382 {
10383 fonts_changed_p = 1;
10384 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10385 = DESIRED_HEADER_LINE_HEIGHT (w);
10386 }
10387
10388 if (fonts_changed_p)
10389 goto finish_scroll_bars;
10390 }
10391
10392 if (!line_number_displayed
10393 && !BUFFERP (w->base_line_pos))
10394 {
10395 w->base_line_pos = Qnil;
10396 w->base_line_number = Qnil;
10397 }
10398
10399 finish_menu_bars:
10400
10401 /* When we reach a frame's selected window, redo the frame's menu bar. */
10402 if (update_mode_line
10403 && EQ (FRAME_SELECTED_WINDOW (f), window))
10404 {
10405 int redisplay_menu_p = 0;
10406
10407 if (FRAME_WINDOW_P (f))
10408 {
10409 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10410 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10411 #else
10412 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10413 #endif
10414 }
10415 else
10416 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10417
10418 if (redisplay_menu_p)
10419 display_menu_bar (w);
10420
10421 #ifdef HAVE_WINDOW_SYSTEM
10422 if (WINDOWP (f->tool_bar_window)
10423 && (FRAME_TOOL_BAR_LINES (f) > 0
10424 || auto_resize_tool_bars_p))
10425 redisplay_tool_bar (f);
10426 #endif
10427 }
10428
10429 finish_scroll_bars:
10430
10431 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10432 {
10433 int start, end, whole;
10434
10435 /* Calculate the start and end positions for the current window.
10436 At some point, it would be nice to choose between scrollbars
10437 which reflect the whole buffer size, with special markers
10438 indicating narrowing, and scrollbars which reflect only the
10439 visible region.
10440
10441 Note that mini-buffers sometimes aren't displaying any text. */
10442 if (!MINI_WINDOW_P (w)
10443 || (w == XWINDOW (minibuf_window)
10444 && NILP (echo_area_buffer[0])))
10445 {
10446 whole = ZV - BEGV;
10447 start = marker_position (w->start) - BEGV;
10448 /* I don't think this is guaranteed to be right. For the
10449 moment, we'll pretend it is. */
10450 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10451
10452 if (end < start)
10453 end = start;
10454 if (whole < (end - start))
10455 whole = end - start;
10456 }
10457 else
10458 start = end = whole = 0;
10459
10460 /* Indicate what this scroll bar ought to be displaying now. */
10461 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10462
10463 /* Note that we actually used the scroll bar attached to this
10464 window, so it shouldn't be deleted at the end of redisplay. */
10465 redeem_scroll_bar_hook (w);
10466 }
10467
10468 /* Restore current_buffer and value of point in it. */
10469 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10470 set_buffer_internal_1 (old);
10471 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10472
10473 unbind_to (count, Qnil);
10474 }
10475
10476
10477 /* Build the complete desired matrix of WINDOW with a window start
10478 buffer position POS. Value is non-zero if successful. It is zero
10479 if fonts were loaded during redisplay which makes re-adjusting
10480 glyph matrices necessary. */
10481
10482 int
10483 try_window (window, pos)
10484 Lisp_Object window;
10485 struct text_pos pos;
10486 {
10487 struct window *w = XWINDOW (window);
10488 struct it it;
10489 struct glyph_row *last_text_row = NULL;
10490
10491 /* Make POS the new window start. */
10492 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10493
10494 /* Mark cursor position as unknown. No overlay arrow seen. */
10495 w->cursor.vpos = -1;
10496 overlay_arrow_seen = 0;
10497
10498 /* Initialize iterator and info to start at POS. */
10499 start_display (&it, w, pos);
10500
10501 /* Display all lines of W. */
10502 while (it.current_y < it.last_visible_y)
10503 {
10504 if (display_line (&it))
10505 last_text_row = it.glyph_row - 1;
10506 if (fonts_changed_p)
10507 return 0;
10508 }
10509
10510 /* If bottom moved off end of frame, change mode line percentage. */
10511 if (XFASTINT (w->window_end_pos) <= 0
10512 && Z != IT_CHARPOS (it))
10513 w->update_mode_line = Qt;
10514
10515 /* Set window_end_pos to the offset of the last character displayed
10516 on the window from the end of current_buffer. Set
10517 window_end_vpos to its row number. */
10518 if (last_text_row)
10519 {
10520 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10521 w->window_end_bytepos
10522 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10523 w->window_end_pos
10524 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10525 w->window_end_vpos
10526 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10527 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10528 ->displays_text_p);
10529 }
10530 else
10531 {
10532 w->window_end_bytepos = 0;
10533 w->window_end_pos = w->window_end_vpos = make_number (0);
10534 }
10535
10536 /* But that is not valid info until redisplay finishes. */
10537 w->window_end_valid = Qnil;
10538 return 1;
10539 }
10540
10541
10542 \f
10543 /************************************************************************
10544 Window redisplay reusing current matrix when buffer has not changed
10545 ************************************************************************/
10546
10547 /* Try redisplay of window W showing an unchanged buffer with a
10548 different window start than the last time it was displayed by
10549 reusing its current matrix. Value is non-zero if successful.
10550 W->start is the new window start. */
10551
10552 static int
10553 try_window_reusing_current_matrix (w)
10554 struct window *w;
10555 {
10556 struct frame *f = XFRAME (w->frame);
10557 struct glyph_row *row, *bottom_row;
10558 struct it it;
10559 struct run run;
10560 struct text_pos start, new_start;
10561 int nrows_scrolled, i;
10562 struct glyph_row *last_text_row;
10563 struct glyph_row *last_reused_text_row;
10564 struct glyph_row *start_row;
10565 int start_vpos, min_y, max_y;
10566
10567 if (/* This function doesn't handle terminal frames. */
10568 !FRAME_WINDOW_P (f)
10569 /* Don't try to reuse the display if windows have been split
10570 or such. */
10571 || windows_or_buffers_changed)
10572 return 0;
10573
10574 /* Can't do this if region may have changed. */
10575 if ((!NILP (Vtransient_mark_mode)
10576 && !NILP (current_buffer->mark_active))
10577 || !NILP (w->region_showing)
10578 || !NILP (Vshow_trailing_whitespace))
10579 return 0;
10580
10581 /* If top-line visibility has changed, give up. */
10582 if (WINDOW_WANTS_HEADER_LINE_P (w)
10583 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10584 return 0;
10585
10586 /* Give up if old or new display is scrolled vertically. We could
10587 make this function handle this, but right now it doesn't. */
10588 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10589 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10590 return 0;
10591
10592 /* The variable new_start now holds the new window start. The old
10593 start `start' can be determined from the current matrix. */
10594 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10595 start = start_row->start.pos;
10596 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10597
10598 /* Clear the desired matrix for the display below. */
10599 clear_glyph_matrix (w->desired_matrix);
10600
10601 if (CHARPOS (new_start) <= CHARPOS (start))
10602 {
10603 int first_row_y;
10604
10605 /* Don't use this method if the display starts with an ellipsis
10606 displayed for invisible text. It's not easy to handle that case
10607 below, and it's certainly not worth the effort since this is
10608 not a frequent case. */
10609 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10610 return 0;
10611
10612 IF_DEBUG (debug_method_add (w, "twu1"));
10613
10614 /* Display up to a row that can be reused. The variable
10615 last_text_row is set to the last row displayed that displays
10616 text. Note that it.vpos == 0 if or if not there is a
10617 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10618 start_display (&it, w, new_start);
10619 first_row_y = it.current_y;
10620 w->cursor.vpos = -1;
10621 last_text_row = last_reused_text_row = NULL;
10622
10623 while (it.current_y < it.last_visible_y
10624 && IT_CHARPOS (it) < CHARPOS (start)
10625 && !fonts_changed_p)
10626 if (display_line (&it))
10627 last_text_row = it.glyph_row - 1;
10628
10629 /* A value of current_y < last_visible_y means that we stopped
10630 at the previous window start, which in turn means that we
10631 have at least one reusable row. */
10632 if (it.current_y < it.last_visible_y)
10633 {
10634 /* IT.vpos always starts from 0; it counts text lines. */
10635 nrows_scrolled = it.vpos;
10636
10637 /* Find PT if not already found in the lines displayed. */
10638 if (w->cursor.vpos < 0)
10639 {
10640 int dy = it.current_y - first_row_y;
10641
10642 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10643 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10644 {
10645 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10646 && PT < MATRIX_ROW_END_CHARPOS (row))
10647 {
10648 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10649 dy, nrows_scrolled);
10650 break;
10651 }
10652
10653 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10654 break;
10655
10656 ++row;
10657 }
10658
10659 /* Give up if point was not found. This shouldn't
10660 happen often; not more often than with try_window
10661 itself. */
10662 if (w->cursor.vpos < 0)
10663 {
10664 clear_glyph_matrix (w->desired_matrix);
10665 return 0;
10666 }
10667 }
10668
10669 /* Scroll the display. Do it before the current matrix is
10670 changed. The problem here is that update has not yet
10671 run, i.e. part of the current matrix is not up to date.
10672 scroll_run_hook will clear the cursor, and use the
10673 current matrix to get the height of the row the cursor is
10674 in. */
10675 run.current_y = first_row_y;
10676 run.desired_y = it.current_y;
10677 run.height = it.last_visible_y - it.current_y;
10678
10679 if (run.height > 0 && run.current_y != run.desired_y)
10680 {
10681 update_begin (f);
10682 rif->update_window_begin_hook (w);
10683 rif->clear_mouse_face (w);
10684 rif->scroll_run_hook (w, &run);
10685 rif->update_window_end_hook (w, 0, 0);
10686 update_end (f);
10687 }
10688
10689 /* Shift current matrix down by nrows_scrolled lines. */
10690 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10691 rotate_matrix (w->current_matrix,
10692 start_vpos,
10693 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10694 nrows_scrolled);
10695
10696 /* Disable lines that must be updated. */
10697 for (i = 0; i < it.vpos; ++i)
10698 (start_row + i)->enabled_p = 0;
10699
10700 /* Re-compute Y positions. */
10701 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10702 max_y = it.last_visible_y;
10703 for (row = start_row + nrows_scrolled;
10704 row < bottom_row;
10705 ++row)
10706 {
10707 row->y = it.current_y;
10708 row->visible_height = row->height;
10709
10710 if (row->y < min_y)
10711 row->visible_height -= min_y - row->y;
10712 if (row->y + row->height > max_y)
10713 row->visible_height -= row->y + row->height - max_y;
10714
10715 it.current_y += row->height;
10716
10717 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10718 last_reused_text_row = row;
10719 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10720 break;
10721 }
10722
10723 /* Disable lines in the current matrix which are now
10724 below the window. */
10725 for (++row; row < bottom_row; ++row)
10726 row->enabled_p = 0;
10727 }
10728
10729 /* Update window_end_pos etc.; last_reused_text_row is the last
10730 reused row from the current matrix containing text, if any.
10731 The value of last_text_row is the last displayed line
10732 containing text. */
10733 if (last_reused_text_row)
10734 {
10735 w->window_end_bytepos
10736 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10737 w->window_end_pos
10738 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10739 w->window_end_vpos
10740 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10741 w->current_matrix));
10742 }
10743 else if (last_text_row)
10744 {
10745 w->window_end_bytepos
10746 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10747 w->window_end_pos
10748 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10749 w->window_end_vpos
10750 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10751 }
10752 else
10753 {
10754 /* This window must be completely empty. */
10755 w->window_end_bytepos = 0;
10756 w->window_end_pos = w->window_end_vpos = make_number (0);
10757 }
10758 w->window_end_valid = Qnil;
10759
10760 /* Update hint: don't try scrolling again in update_window. */
10761 w->desired_matrix->no_scrolling_p = 1;
10762
10763 #if GLYPH_DEBUG
10764 debug_method_add (w, "try_window_reusing_current_matrix 1");
10765 #endif
10766 return 1;
10767 }
10768 else if (CHARPOS (new_start) > CHARPOS (start))
10769 {
10770 struct glyph_row *pt_row, *row;
10771 struct glyph_row *first_reusable_row;
10772 struct glyph_row *first_row_to_display;
10773 int dy;
10774 int yb = window_text_bottom_y (w);
10775
10776 /* Find the row starting at new_start, if there is one. Don't
10777 reuse a partially visible line at the end. */
10778 first_reusable_row = start_row;
10779 while (first_reusable_row->enabled_p
10780 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10781 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10782 < CHARPOS (new_start)))
10783 ++first_reusable_row;
10784
10785 /* Give up if there is no row to reuse. */
10786 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10787 || !first_reusable_row->enabled_p
10788 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10789 != CHARPOS (new_start)))
10790 return 0;
10791
10792 /* We can reuse fully visible rows beginning with
10793 first_reusable_row to the end of the window. Set
10794 first_row_to_display to the first row that cannot be reused.
10795 Set pt_row to the row containing point, if there is any. */
10796 pt_row = NULL;
10797 for (first_row_to_display = first_reusable_row;
10798 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10799 ++first_row_to_display)
10800 {
10801 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10802 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10803 pt_row = first_row_to_display;
10804 }
10805
10806 /* Start displaying at the start of first_row_to_display. */
10807 xassert (first_row_to_display->y < yb);
10808 init_to_row_start (&it, w, first_row_to_display);
10809
10810 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10811 - start_vpos);
10812 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10813 - nrows_scrolled);
10814 it.current_y = (first_row_to_display->y - first_reusable_row->y
10815 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10816
10817 /* Display lines beginning with first_row_to_display in the
10818 desired matrix. Set last_text_row to the last row displayed
10819 that displays text. */
10820 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10821 if (pt_row == NULL)
10822 w->cursor.vpos = -1;
10823 last_text_row = NULL;
10824 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10825 if (display_line (&it))
10826 last_text_row = it.glyph_row - 1;
10827
10828 /* Give up If point isn't in a row displayed or reused. */
10829 if (w->cursor.vpos < 0)
10830 {
10831 clear_glyph_matrix (w->desired_matrix);
10832 return 0;
10833 }
10834
10835 /* If point is in a reused row, adjust y and vpos of the cursor
10836 position. */
10837 if (pt_row)
10838 {
10839 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10840 w->current_matrix);
10841 w->cursor.y -= first_reusable_row->y;
10842 }
10843
10844 /* Scroll the display. */
10845 run.current_y = first_reusable_row->y;
10846 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10847 run.height = it.last_visible_y - run.current_y;
10848 dy = run.current_y - run.desired_y;
10849
10850 if (run.height)
10851 {
10852 struct frame *f = XFRAME (WINDOW_FRAME (w));
10853 update_begin (f);
10854 rif->update_window_begin_hook (w);
10855 rif->clear_mouse_face (w);
10856 rif->scroll_run_hook (w, &run);
10857 rif->update_window_end_hook (w, 0, 0);
10858 update_end (f);
10859 }
10860
10861 /* Adjust Y positions of reused rows. */
10862 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10863 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10864 max_y = it.last_visible_y;
10865 for (row = first_reusable_row; row < first_row_to_display; ++row)
10866 {
10867 row->y -= dy;
10868 row->visible_height = row->height;
10869 if (row->y < min_y)
10870 row->visible_height -= min_y - row->y;
10871 if (row->y + row->height > max_y)
10872 row->visible_height -= row->y + row->height - max_y;
10873 }
10874
10875 /* Scroll the current matrix. */
10876 xassert (nrows_scrolled > 0);
10877 rotate_matrix (w->current_matrix,
10878 start_vpos,
10879 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10880 -nrows_scrolled);
10881
10882 /* Disable rows not reused. */
10883 for (row -= nrows_scrolled; row < bottom_row; ++row)
10884 row->enabled_p = 0;
10885
10886 /* Adjust window end. A null value of last_text_row means that
10887 the window end is in reused rows which in turn means that
10888 only its vpos can have changed. */
10889 if (last_text_row)
10890 {
10891 w->window_end_bytepos
10892 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10893 w->window_end_pos
10894 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10895 w->window_end_vpos
10896 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10897 }
10898 else
10899 {
10900 w->window_end_vpos
10901 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10902 }
10903
10904 w->window_end_valid = Qnil;
10905 w->desired_matrix->no_scrolling_p = 1;
10906
10907 #if GLYPH_DEBUG
10908 debug_method_add (w, "try_window_reusing_current_matrix 2");
10909 #endif
10910 return 1;
10911 }
10912
10913 return 0;
10914 }
10915
10916
10917 \f
10918 /************************************************************************
10919 Window redisplay reusing current matrix when buffer has changed
10920 ************************************************************************/
10921
10922 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10923 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10924 int *, int *));
10925 static struct glyph_row *
10926 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10927 struct glyph_row *));
10928
10929
10930 /* Return the last row in MATRIX displaying text. If row START is
10931 non-null, start searching with that row. IT gives the dimensions
10932 of the display. Value is null if matrix is empty; otherwise it is
10933 a pointer to the row found. */
10934
10935 static struct glyph_row *
10936 find_last_row_displaying_text (matrix, it, start)
10937 struct glyph_matrix *matrix;
10938 struct it *it;
10939 struct glyph_row *start;
10940 {
10941 struct glyph_row *row, *row_found;
10942
10943 /* Set row_found to the last row in IT->w's current matrix
10944 displaying text. The loop looks funny but think of partially
10945 visible lines. */
10946 row_found = NULL;
10947 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10948 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10949 {
10950 xassert (row->enabled_p);
10951 row_found = row;
10952 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10953 break;
10954 ++row;
10955 }
10956
10957 return row_found;
10958 }
10959
10960
10961 /* Return the last row in the current matrix of W that is not affected
10962 by changes at the start of current_buffer that occurred since W's
10963 current matrix was built. Value is null if no such row exists.
10964
10965 BEG_UNCHANGED us the number of characters unchanged at the start of
10966 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
10967 first changed character in current_buffer. Characters at positions <
10968 BEG + BEG_UNCHANGED are at the same buffer positions as they were
10969 when the current matrix was built. */
10970
10971 static struct glyph_row *
10972 find_last_unchanged_at_beg_row (w)
10973 struct window *w;
10974 {
10975 int first_changed_pos = BEG + BEG_UNCHANGED;
10976 struct glyph_row *row;
10977 struct glyph_row *row_found = NULL;
10978 int yb = window_text_bottom_y (w);
10979
10980 /* Find the last row displaying unchanged text. */
10981 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10982 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10983 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10984 {
10985 if (/* If row ends before first_changed_pos, it is unchanged,
10986 except in some case. */
10987 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10988 /* When row ends in ZV and we write at ZV it is not
10989 unchanged. */
10990 && !row->ends_at_zv_p
10991 /* When first_changed_pos is the end of a continued line,
10992 row is not unchanged because it may be no longer
10993 continued. */
10994 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10995 && row->continued_p))
10996 row_found = row;
10997
10998 /* Stop if last visible row. */
10999 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11000 break;
11001
11002 ++row;
11003 }
11004
11005 return row_found;
11006 }
11007
11008
11009 /* Find the first glyph row in the current matrix of W that is not
11010 affected by changes at the end of current_buffer since the
11011 time W's current matrix was built.
11012
11013 Return in *DELTA the number of chars by which buffer positions in
11014 unchanged text at the end of current_buffer must be adjusted.
11015
11016 Return in *DELTA_BYTES the corresponding number of bytes.
11017
11018 Value is null if no such row exists, i.e. all rows are affected by
11019 changes. */
11020
11021 static struct glyph_row *
11022 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11023 struct window *w;
11024 int *delta, *delta_bytes;
11025 {
11026 struct glyph_row *row;
11027 struct glyph_row *row_found = NULL;
11028
11029 *delta = *delta_bytes = 0;
11030
11031 /* Display must not have been paused, otherwise the current matrix
11032 is not up to date. */
11033 if (NILP (w->window_end_valid))
11034 abort ();
11035
11036 /* A value of window_end_pos >= END_UNCHANGED means that the window
11037 end is in the range of changed text. If so, there is no
11038 unchanged row at the end of W's current matrix. */
11039 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11040 return NULL;
11041
11042 /* Set row to the last row in W's current matrix displaying text. */
11043 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11044
11045 /* If matrix is entirely empty, no unchanged row exists. */
11046 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11047 {
11048 /* The value of row is the last glyph row in the matrix having a
11049 meaningful buffer position in it. The end position of row
11050 corresponds to window_end_pos. This allows us to translate
11051 buffer positions in the current matrix to current buffer
11052 positions for characters not in changed text. */
11053 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11054 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11055 int last_unchanged_pos, last_unchanged_pos_old;
11056 struct glyph_row *first_text_row
11057 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11058
11059 *delta = Z - Z_old;
11060 *delta_bytes = Z_BYTE - Z_BYTE_old;
11061
11062 /* Set last_unchanged_pos to the buffer position of the last
11063 character in the buffer that has not been changed. Z is the
11064 index + 1 of the last character in current_buffer, i.e. by
11065 subtracting END_UNCHANGED we get the index of the last
11066 unchanged character, and we have to add BEG to get its buffer
11067 position. */
11068 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11069 last_unchanged_pos_old = last_unchanged_pos - *delta;
11070
11071 /* Search backward from ROW for a row displaying a line that
11072 starts at a minimum position >= last_unchanged_pos_old. */
11073 for (; row > first_text_row; --row)
11074 {
11075 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11076 abort ();
11077
11078 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11079 row_found = row;
11080 }
11081 }
11082
11083 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11084 abort ();
11085
11086 return row_found;
11087 }
11088
11089
11090 /* Make sure that glyph rows in the current matrix of window W
11091 reference the same glyph memory as corresponding rows in the
11092 frame's frame matrix. This function is called after scrolling W's
11093 current matrix on a terminal frame in try_window_id and
11094 try_window_reusing_current_matrix. */
11095
11096 static void
11097 sync_frame_with_window_matrix_rows (w)
11098 struct window *w;
11099 {
11100 struct frame *f = XFRAME (w->frame);
11101 struct glyph_row *window_row, *window_row_end, *frame_row;
11102
11103 /* Preconditions: W must be a leaf window and full-width. Its frame
11104 must have a frame matrix. */
11105 xassert (NILP (w->hchild) && NILP (w->vchild));
11106 xassert (WINDOW_FULL_WIDTH_P (w));
11107 xassert (!FRAME_WINDOW_P (f));
11108
11109 /* If W is a full-width window, glyph pointers in W's current matrix
11110 have, by definition, to be the same as glyph pointers in the
11111 corresponding frame matrix. */
11112 window_row = w->current_matrix->rows;
11113 window_row_end = window_row + w->current_matrix->nrows;
11114 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11115 while (window_row < window_row_end)
11116 {
11117 int area;
11118
11119 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11120 frame_row->glyphs[area] = window_row->glyphs[area];
11121
11122 /* Disable frame rows whose corresponding window rows have
11123 been disabled in try_window_id. */
11124 if (!window_row->enabled_p)
11125 frame_row->enabled_p = 0;
11126
11127 ++window_row, ++frame_row;
11128 }
11129 }
11130
11131
11132 /* Find the glyph row in window W containing CHARPOS. Consider all
11133 rows between START and END (not inclusive). END null means search
11134 all rows to the end of the display area of W. Value is the row
11135 containing CHARPOS or null. */
11136
11137 static struct glyph_row *
11138 row_containing_pos (w, charpos, start, end)
11139 struct window *w;
11140 int charpos;
11141 struct glyph_row *start, *end;
11142 {
11143 struct glyph_row *row = start;
11144 int last_y;
11145
11146 /* If we happen to start on a header-line, skip that. */
11147 if (row->mode_line_p)
11148 ++row;
11149
11150 if ((end && row >= end) || !row->enabled_p)
11151 return NULL;
11152
11153 last_y = window_text_bottom_y (w);
11154
11155 while ((end == NULL || row < end)
11156 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11157 /* The end position of a row equals the start
11158 position of the next row. If CHARPOS is there, we
11159 would rather display it in the next line, except
11160 when this line ends in ZV. */
11161 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11162 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11163 || !row->ends_at_zv_p)))
11164 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11165 ++row;
11166
11167 /* Give up if CHARPOS not found. */
11168 if ((end && row >= end)
11169 || charpos < MATRIX_ROW_START_CHARPOS (row)
11170 || charpos > MATRIX_ROW_END_CHARPOS (row))
11171 row = NULL;
11172
11173 return row;
11174 }
11175
11176
11177 /* Try to redisplay window W by reusing its existing display. W's
11178 current matrix must be up to date when this function is called,
11179 i.e. window_end_valid must not be nil.
11180
11181 Value is
11182
11183 1 if display has been updated
11184 0 if otherwise unsuccessful
11185 -1 if redisplay with same window start is known not to succeed
11186
11187 The following steps are performed:
11188
11189 1. Find the last row in the current matrix of W that is not
11190 affected by changes at the start of current_buffer. If no such row
11191 is found, give up.
11192
11193 2. Find the first row in W's current matrix that is not affected by
11194 changes at the end of current_buffer. Maybe there is no such row.
11195
11196 3. Display lines beginning with the row + 1 found in step 1 to the
11197 row found in step 2 or, if step 2 didn't find a row, to the end of
11198 the window.
11199
11200 4. If cursor is not known to appear on the window, give up.
11201
11202 5. If display stopped at the row found in step 2, scroll the
11203 display and current matrix as needed.
11204
11205 6. Maybe display some lines at the end of W, if we must. This can
11206 happen under various circumstances, like a partially visible line
11207 becoming fully visible, or because newly displayed lines are displayed
11208 in smaller font sizes.
11209
11210 7. Update W's window end information. */
11211
11212 static int
11213 try_window_id (w)
11214 struct window *w;
11215 {
11216 struct frame *f = XFRAME (w->frame);
11217 struct glyph_matrix *current_matrix = w->current_matrix;
11218 struct glyph_matrix *desired_matrix = w->desired_matrix;
11219 struct glyph_row *last_unchanged_at_beg_row;
11220 struct glyph_row *first_unchanged_at_end_row;
11221 struct glyph_row *row;
11222 struct glyph_row *bottom_row;
11223 int bottom_vpos;
11224 struct it it;
11225 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11226 struct text_pos start_pos;
11227 struct run run;
11228 int first_unchanged_at_end_vpos = 0;
11229 struct glyph_row *last_text_row, *last_text_row_at_end;
11230 struct text_pos start;
11231 int first_changed_charpos, last_changed_charpos;
11232
11233 /* This is handy for debugging. */
11234 #if 0
11235 #define GIVE_UP(X) \
11236 do { \
11237 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11238 return 0; \
11239 } while (0)
11240 #else
11241 #define GIVE_UP(X) return 0
11242 #endif
11243
11244 SET_TEXT_POS_FROM_MARKER (start, w->start);
11245
11246 /* Don't use this for mini-windows because these can show
11247 messages and mini-buffers, and we don't handle that here. */
11248 if (MINI_WINDOW_P (w))
11249 GIVE_UP (1);
11250
11251 /* This flag is used to prevent redisplay optimizations. */
11252 if (windows_or_buffers_changed)
11253 GIVE_UP (2);
11254
11255 /* Verify that narrowing has not changed. This flag is also set to prevent
11256 redisplay optimizations. It would be nice to further
11257 reduce the number of cases where this prevents try_window_id. */
11258 if (current_buffer->clip_changed)
11259 GIVE_UP (3);
11260
11261 /* Window must either use window-based redisplay or be full width. */
11262 if (!FRAME_WINDOW_P (f)
11263 && (!line_ins_del_ok
11264 || !WINDOW_FULL_WIDTH_P (w)))
11265 GIVE_UP (4);
11266
11267 /* Give up if point is not known NOT to appear in W. */
11268 if (PT < CHARPOS (start))
11269 GIVE_UP (5);
11270
11271 /* Another way to prevent redisplay optimizations. */
11272 if (XFASTINT (w->last_modified) == 0)
11273 GIVE_UP (6);
11274
11275 /* Verify that window is not hscrolled. */
11276 if (XFASTINT (w->hscroll) != 0)
11277 GIVE_UP (7);
11278
11279 /* Verify that display wasn't paused. */
11280 if (NILP (w->window_end_valid))
11281 GIVE_UP (8);
11282
11283 /* Can't use this if highlighting a region because a cursor movement
11284 will do more than just set the cursor. */
11285 if (!NILP (Vtransient_mark_mode)
11286 && !NILP (current_buffer->mark_active))
11287 GIVE_UP (9);
11288
11289 /* Likewise if highlighting trailing whitespace. */
11290 if (!NILP (Vshow_trailing_whitespace))
11291 GIVE_UP (11);
11292
11293 /* Likewise if showing a region. */
11294 if (!NILP (w->region_showing))
11295 GIVE_UP (10);
11296
11297 /* Can use this if overlay arrow position and or string have changed. */
11298 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11299 || !EQ (last_arrow_string, Voverlay_arrow_string))
11300 GIVE_UP (12);
11301
11302
11303 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11304 only if buffer has really changed. The reason is that the gap is
11305 initially at Z for freshly visited files. The code below would
11306 set end_unchanged to 0 in that case. */
11307 if (MODIFF > SAVE_MODIFF
11308 /* This seems to happen sometimes after saving a buffer. */
11309 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11310 {
11311 if (GPT - BEG < BEG_UNCHANGED)
11312 BEG_UNCHANGED = GPT - BEG;
11313 if (Z - GPT < END_UNCHANGED)
11314 END_UNCHANGED = Z - GPT;
11315 }
11316
11317 /* The position of the first and last character that has been changed. */
11318 first_changed_charpos = BEG + BEG_UNCHANGED;
11319 last_changed_charpos = Z - END_UNCHANGED;
11320
11321 /* If window starts after a line end, and the last change is in
11322 front of that newline, then changes don't affect the display.
11323 This case happens with stealth-fontification. Note that although
11324 the display is unchanged, glyph positions in the matrix have to
11325 be adjusted, of course. */
11326 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11327 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11328 && ((last_changed_charpos < CHARPOS (start)
11329 && CHARPOS (start) == BEGV)
11330 || (last_changed_charpos < CHARPOS (start) - 1
11331 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11332 {
11333 int Z_old, delta, Z_BYTE_old, delta_bytes;
11334 struct glyph_row *r0;
11335
11336 /* Compute how many chars/bytes have been added to or removed
11337 from the buffer. */
11338 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11339 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11340 delta = Z - Z_old;
11341 delta_bytes = Z_BYTE - Z_BYTE_old;
11342
11343 /* Give up if PT is not in the window. Note that it already has
11344 been checked at the start of try_window_id that PT is not in
11345 front of the window start. */
11346 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11347 GIVE_UP (13);
11348
11349 /* If window start is unchanged, we can reuse the whole matrix
11350 as is, after adjusting glyph positions. No need to compute
11351 the window end again, since its offset from Z hasn't changed. */
11352 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11353 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11354 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11355 {
11356 /* Adjust positions in the glyph matrix. */
11357 if (delta || delta_bytes)
11358 {
11359 struct glyph_row *r1
11360 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11361 increment_matrix_positions (w->current_matrix,
11362 MATRIX_ROW_VPOS (r0, current_matrix),
11363 MATRIX_ROW_VPOS (r1, current_matrix),
11364 delta, delta_bytes);
11365 }
11366
11367 /* Set the cursor. */
11368 row = row_containing_pos (w, PT, r0, NULL);
11369 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11370 return 1;
11371 }
11372 }
11373
11374 /* Handle the case that changes are all below what is displayed in
11375 the window, and that PT is in the window. This shortcut cannot
11376 be taken if ZV is visible in the window, and text has been added
11377 there that is visible in the window. */
11378 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11379 /* ZV is not visible in the window, or there are no
11380 changes at ZV, actually. */
11381 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11382 || first_changed_charpos == last_changed_charpos))
11383 {
11384 struct glyph_row *r0;
11385
11386 /* Give up if PT is not in the window. Note that it already has
11387 been checked at the start of try_window_id that PT is not in
11388 front of the window start. */
11389 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11390 GIVE_UP (14);
11391
11392 /* If window start is unchanged, we can reuse the whole matrix
11393 as is, without changing glyph positions since no text has
11394 been added/removed in front of the window end. */
11395 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11396 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11397 {
11398 /* We have to compute the window end anew since text
11399 can have been added/removed after it. */
11400 w->window_end_pos
11401 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11402 w->window_end_bytepos
11403 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11404
11405 /* Set the cursor. */
11406 row = row_containing_pos (w, PT, r0, NULL);
11407 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11408 return 2;
11409 }
11410 }
11411
11412 /* Give up if window start is in the changed area.
11413
11414 The condition used to read
11415
11416 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11417
11418 but why that was tested escapes me at the moment. */
11419 if (CHARPOS (start) >= first_changed_charpos
11420 && CHARPOS (start) <= last_changed_charpos)
11421 GIVE_UP (15);
11422
11423 /* Check that window start agrees with the start of the first glyph
11424 row in its current matrix. Check this after we know the window
11425 start is not in changed text, otherwise positions would not be
11426 comparable. */
11427 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11428 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11429 GIVE_UP (16);
11430
11431 /* Compute the position at which we have to start displaying new
11432 lines. Some of the lines at the top of the window might be
11433 reusable because they are not displaying changed text. Find the
11434 last row in W's current matrix not affected by changes at the
11435 start of current_buffer. Value is null if changes start in the
11436 first line of window. */
11437 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11438 if (last_unchanged_at_beg_row)
11439 {
11440 /* Avoid starting to display in the moddle of a character, a TAB
11441 for instance. This is easier than to set up the iterator
11442 exactly, and it's not a frequent case, so the additional
11443 effort wouldn't really pay off. */
11444 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11445 && last_unchanged_at_beg_row > w->current_matrix->rows)
11446 --last_unchanged_at_beg_row;
11447
11448 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11449 GIVE_UP (17);
11450
11451 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11452 GIVE_UP (18);
11453 start_pos = it.current.pos;
11454
11455 /* Start displaying new lines in the desired matrix at the same
11456 vpos we would use in the current matrix, i.e. below
11457 last_unchanged_at_beg_row. */
11458 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11459 current_matrix);
11460 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11461 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11462
11463 xassert (it.hpos == 0 && it.current_x == 0);
11464 }
11465 else
11466 {
11467 /* There are no reusable lines at the start of the window.
11468 Start displaying in the first line. */
11469 start_display (&it, w, start);
11470 start_pos = it.current.pos;
11471 }
11472
11473 /* Find the first row that is not affected by changes at the end of
11474 the buffer. Value will be null if there is no unchanged row, in
11475 which case we must redisplay to the end of the window. delta
11476 will be set to the value by which buffer positions beginning with
11477 first_unchanged_at_end_row have to be adjusted due to text
11478 changes. */
11479 first_unchanged_at_end_row
11480 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11481 IF_DEBUG (debug_delta = delta);
11482 IF_DEBUG (debug_delta_bytes = delta_bytes);
11483
11484 /* Set stop_pos to the buffer position up to which we will have to
11485 display new lines. If first_unchanged_at_end_row != NULL, this
11486 is the buffer position of the start of the line displayed in that
11487 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11488 that we don't stop at a buffer position. */
11489 stop_pos = 0;
11490 if (first_unchanged_at_end_row)
11491 {
11492 xassert (last_unchanged_at_beg_row == NULL
11493 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11494
11495 /* If this is a continuation line, move forward to the next one
11496 that isn't. Changes in lines above affect this line.
11497 Caution: this may move first_unchanged_at_end_row to a row
11498 not displaying text. */
11499 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11500 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11501 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11502 < it.last_visible_y))
11503 ++first_unchanged_at_end_row;
11504
11505 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11506 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11507 >= it.last_visible_y))
11508 first_unchanged_at_end_row = NULL;
11509 else
11510 {
11511 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11512 + delta);
11513 first_unchanged_at_end_vpos
11514 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11515 xassert (stop_pos >= Z - END_UNCHANGED);
11516 }
11517 }
11518 else if (last_unchanged_at_beg_row == NULL)
11519 GIVE_UP (18);
11520
11521
11522 #if GLYPH_DEBUG
11523
11524 /* Either there is no unchanged row at the end, or the one we have
11525 now displays text. This is a necessary condition for the window
11526 end pos calculation at the end of this function. */
11527 xassert (first_unchanged_at_end_row == NULL
11528 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11529
11530 debug_last_unchanged_at_beg_vpos
11531 = (last_unchanged_at_beg_row
11532 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11533 : -1);
11534 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11535
11536 #endif /* GLYPH_DEBUG != 0 */
11537
11538
11539 /* Display new lines. Set last_text_row to the last new line
11540 displayed which has text on it, i.e. might end up as being the
11541 line where the window_end_vpos is. */
11542 w->cursor.vpos = -1;
11543 last_text_row = NULL;
11544 overlay_arrow_seen = 0;
11545 while (it.current_y < it.last_visible_y
11546 && !fonts_changed_p
11547 && (first_unchanged_at_end_row == NULL
11548 || IT_CHARPOS (it) < stop_pos))
11549 {
11550 if (display_line (&it))
11551 last_text_row = it.glyph_row - 1;
11552 }
11553
11554 if (fonts_changed_p)
11555 return -1;
11556
11557
11558 /* Compute differences in buffer positions, y-positions etc. for
11559 lines reused at the bottom of the window. Compute what we can
11560 scroll. */
11561 if (first_unchanged_at_end_row
11562 /* No lines reused because we displayed everything up to the
11563 bottom of the window. */
11564 && it.current_y < it.last_visible_y)
11565 {
11566 dvpos = (it.vpos
11567 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11568 current_matrix));
11569 dy = it.current_y - first_unchanged_at_end_row->y;
11570 run.current_y = first_unchanged_at_end_row->y;
11571 run.desired_y = run.current_y + dy;
11572 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11573 }
11574 else
11575 {
11576 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11577 first_unchanged_at_end_row = NULL;
11578 }
11579 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11580
11581
11582 /* Find the cursor if not already found. We have to decide whether
11583 PT will appear on this window (it sometimes doesn't, but this is
11584 not a very frequent case.) This decision has to be made before
11585 the current matrix is altered. A value of cursor.vpos < 0 means
11586 that PT is either in one of the lines beginning at
11587 first_unchanged_at_end_row or below the window. Don't care for
11588 lines that might be displayed later at the window end; as
11589 mentioned, this is not a frequent case. */
11590 if (w->cursor.vpos < 0)
11591 {
11592 /* Cursor in unchanged rows at the top? */
11593 if (PT < CHARPOS (start_pos)
11594 && last_unchanged_at_beg_row)
11595 {
11596 row = row_containing_pos (w, PT,
11597 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11598 last_unchanged_at_beg_row + 1);
11599 if (row)
11600 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11601 }
11602
11603 /* Start from first_unchanged_at_end_row looking for PT. */
11604 else if (first_unchanged_at_end_row)
11605 {
11606 row = row_containing_pos (w, PT - delta,
11607 first_unchanged_at_end_row, NULL);
11608 if (row)
11609 set_cursor_from_row (w, row, w->current_matrix, delta,
11610 delta_bytes, dy, dvpos);
11611 }
11612
11613 /* Give up if cursor was not found. */
11614 if (w->cursor.vpos < 0)
11615 {
11616 clear_glyph_matrix (w->desired_matrix);
11617 return -1;
11618 }
11619 }
11620
11621 /* Don't let the cursor end in the scroll margins. */
11622 {
11623 int this_scroll_margin, cursor_height;
11624
11625 this_scroll_margin = max (0, scroll_margin);
11626 this_scroll_margin = min (this_scroll_margin,
11627 XFASTINT (w->height) / 4);
11628 this_scroll_margin *= CANON_Y_UNIT (it.f);
11629 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11630
11631 if ((w->cursor.y < this_scroll_margin
11632 && CHARPOS (start) > BEGV)
11633 /* Don't take scroll margin into account at the bottom because
11634 old redisplay didn't do it either. */
11635 || w->cursor.y + cursor_height > it.last_visible_y)
11636 {
11637 w->cursor.vpos = -1;
11638 clear_glyph_matrix (w->desired_matrix);
11639 return -1;
11640 }
11641 }
11642
11643 /* Scroll the display. Do it before changing the current matrix so
11644 that xterm.c doesn't get confused about where the cursor glyph is
11645 found. */
11646 if (dy && run.height)
11647 {
11648 update_begin (f);
11649
11650 if (FRAME_WINDOW_P (f))
11651 {
11652 rif->update_window_begin_hook (w);
11653 rif->clear_mouse_face (w);
11654 rif->scroll_run_hook (w, &run);
11655 rif->update_window_end_hook (w, 0, 0);
11656 }
11657 else
11658 {
11659 /* Terminal frame. In this case, dvpos gives the number of
11660 lines to scroll by; dvpos < 0 means scroll up. */
11661 int first_unchanged_at_end_vpos
11662 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11663 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11664 int end = (XFASTINT (w->top)
11665 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11666 + window_internal_height (w));
11667
11668 /* Perform the operation on the screen. */
11669 if (dvpos > 0)
11670 {
11671 /* Scroll last_unchanged_at_beg_row to the end of the
11672 window down dvpos lines. */
11673 set_terminal_window (end);
11674
11675 /* On dumb terminals delete dvpos lines at the end
11676 before inserting dvpos empty lines. */
11677 if (!scroll_region_ok)
11678 ins_del_lines (end - dvpos, -dvpos);
11679
11680 /* Insert dvpos empty lines in front of
11681 last_unchanged_at_beg_row. */
11682 ins_del_lines (from, dvpos);
11683 }
11684 else if (dvpos < 0)
11685 {
11686 /* Scroll up last_unchanged_at_beg_vpos to the end of
11687 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11688 set_terminal_window (end);
11689
11690 /* Delete dvpos lines in front of
11691 last_unchanged_at_beg_vpos. ins_del_lines will set
11692 the cursor to the given vpos and emit |dvpos| delete
11693 line sequences. */
11694 ins_del_lines (from + dvpos, dvpos);
11695
11696 /* On a dumb terminal insert dvpos empty lines at the
11697 end. */
11698 if (!scroll_region_ok)
11699 ins_del_lines (end + dvpos, -dvpos);
11700 }
11701
11702 set_terminal_window (0);
11703 }
11704
11705 update_end (f);
11706 }
11707
11708 /* Shift reused rows of the current matrix to the right position.
11709 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11710 text. */
11711 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11712 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11713 if (dvpos < 0)
11714 {
11715 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11716 bottom_vpos, dvpos);
11717 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11718 bottom_vpos, 0);
11719 }
11720 else if (dvpos > 0)
11721 {
11722 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11723 bottom_vpos, dvpos);
11724 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11725 first_unchanged_at_end_vpos + dvpos, 0);
11726 }
11727
11728 /* For frame-based redisplay, make sure that current frame and window
11729 matrix are in sync with respect to glyph memory. */
11730 if (!FRAME_WINDOW_P (f))
11731 sync_frame_with_window_matrix_rows (w);
11732
11733 /* Adjust buffer positions in reused rows. */
11734 if (delta)
11735 increment_matrix_positions (current_matrix,
11736 first_unchanged_at_end_vpos + dvpos,
11737 bottom_vpos, delta, delta_bytes);
11738
11739 /* Adjust Y positions. */
11740 if (dy)
11741 shift_glyph_matrix (w, current_matrix,
11742 first_unchanged_at_end_vpos + dvpos,
11743 bottom_vpos, dy);
11744
11745 if (first_unchanged_at_end_row)
11746 first_unchanged_at_end_row += dvpos;
11747
11748 /* If scrolling up, there may be some lines to display at the end of
11749 the window. */
11750 last_text_row_at_end = NULL;
11751 if (dy < 0)
11752 {
11753 /* Set last_row to the glyph row in the current matrix where the
11754 window end line is found. It has been moved up or down in
11755 the matrix by dvpos. */
11756 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11757 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11758
11759 /* If last_row is the window end line, it should display text. */
11760 xassert (last_row->displays_text_p);
11761
11762 /* If window end line was partially visible before, begin
11763 displaying at that line. Otherwise begin displaying with the
11764 line following it. */
11765 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11766 {
11767 init_to_row_start (&it, w, last_row);
11768 it.vpos = last_vpos;
11769 it.current_y = last_row->y;
11770 }
11771 else
11772 {
11773 init_to_row_end (&it, w, last_row);
11774 it.vpos = 1 + last_vpos;
11775 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11776 ++last_row;
11777 }
11778
11779 /* We may start in a continuation line. If so, we have to get
11780 the right continuation_lines_width and current_x. */
11781 it.continuation_lines_width = last_row->continuation_lines_width;
11782 it.hpos = it.current_x = 0;
11783
11784 /* Display the rest of the lines at the window end. */
11785 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11786 while (it.current_y < it.last_visible_y
11787 && !fonts_changed_p)
11788 {
11789 /* Is it always sure that the display agrees with lines in
11790 the current matrix? I don't think so, so we mark rows
11791 displayed invalid in the current matrix by setting their
11792 enabled_p flag to zero. */
11793 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11794 if (display_line (&it))
11795 last_text_row_at_end = it.glyph_row - 1;
11796 }
11797 }
11798
11799 /* Update window_end_pos and window_end_vpos. */
11800 if (first_unchanged_at_end_row
11801 && first_unchanged_at_end_row->y < it.last_visible_y
11802 && !last_text_row_at_end)
11803 {
11804 /* Window end line if one of the preserved rows from the current
11805 matrix. Set row to the last row displaying text in current
11806 matrix starting at first_unchanged_at_end_row, after
11807 scrolling. */
11808 xassert (first_unchanged_at_end_row->displays_text_p);
11809 row = find_last_row_displaying_text (w->current_matrix, &it,
11810 first_unchanged_at_end_row);
11811 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11812
11813 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11814 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11815 w->window_end_vpos
11816 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11817 xassert (w->window_end_bytepos >= 0);
11818 IF_DEBUG (debug_method_add (w, "A"));
11819 }
11820 else if (last_text_row_at_end)
11821 {
11822 w->window_end_pos
11823 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11824 w->window_end_bytepos
11825 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11826 w->window_end_vpos
11827 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11828 xassert (w->window_end_bytepos >= 0);
11829 IF_DEBUG (debug_method_add (w, "B"));
11830 }
11831 else if (last_text_row)
11832 {
11833 /* We have displayed either to the end of the window or at the
11834 end of the window, i.e. the last row with text is to be found
11835 in the desired matrix. */
11836 w->window_end_pos
11837 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11838 w->window_end_bytepos
11839 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11840 w->window_end_vpos
11841 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11842 xassert (w->window_end_bytepos >= 0);
11843 }
11844 else if (first_unchanged_at_end_row == NULL
11845 && last_text_row == NULL
11846 && last_text_row_at_end == NULL)
11847 {
11848 /* Displayed to end of window, but no line containing text was
11849 displayed. Lines were deleted at the end of the window. */
11850 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11851 int vpos = XFASTINT (w->window_end_vpos);
11852 struct glyph_row *current_row = current_matrix->rows + vpos;
11853 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11854
11855 for (row = NULL;
11856 row == NULL && vpos >= first_vpos;
11857 --vpos, --current_row, --desired_row)
11858 {
11859 if (desired_row->enabled_p)
11860 {
11861 if (desired_row->displays_text_p)
11862 row = desired_row;
11863 }
11864 else if (current_row->displays_text_p)
11865 row = current_row;
11866 }
11867
11868 xassert (row != NULL);
11869 w->window_end_vpos = make_number (vpos + 1);
11870 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11871 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11872 xassert (w->window_end_bytepos >= 0);
11873 IF_DEBUG (debug_method_add (w, "C"));
11874 }
11875 else
11876 abort ();
11877
11878 #if 0 /* This leads to problems, for instance when the cursor is
11879 at ZV, and the cursor line displays no text. */
11880 /* Disable rows below what's displayed in the window. This makes
11881 debugging easier. */
11882 enable_glyph_matrix_rows (current_matrix,
11883 XFASTINT (w->window_end_vpos) + 1,
11884 bottom_vpos, 0);
11885 #endif
11886
11887 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11888 debug_end_vpos = XFASTINT (w->window_end_vpos));
11889
11890 /* Record that display has not been completed. */
11891 w->window_end_valid = Qnil;
11892 w->desired_matrix->no_scrolling_p = 1;
11893 return 3;
11894
11895 #undef GIVE_UP
11896 }
11897
11898
11899 \f
11900 /***********************************************************************
11901 More debugging support
11902 ***********************************************************************/
11903
11904 #if GLYPH_DEBUG
11905
11906 void dump_glyph_row P_ ((struct glyph_row *, int, int));
11907 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11908 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11909
11910
11911 /* Dump the contents of glyph matrix MATRIX on stderr.
11912
11913 GLYPHS 0 means don't show glyph contents.
11914 GLYPHS 1 means show glyphs in short form
11915 GLYPHS > 1 means show glyphs in long form. */
11916
11917 void
11918 dump_glyph_matrix (matrix, glyphs)
11919 struct glyph_matrix *matrix;
11920 int glyphs;
11921 {
11922 int i;
11923 for (i = 0; i < matrix->nrows; ++i)
11924 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
11925 }
11926
11927
11928 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11929 the glyph row and area where the glyph comes from. */
11930
11931 void
11932 dump_glyph (row, glyph, area)
11933 struct glyph_row *row;
11934 struct glyph *glyph;
11935 int area;
11936 {
11937 if (glyph->type == CHAR_GLYPH)
11938 {
11939 fprintf (stderr,
11940 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11941 glyph - row->glyphs[TEXT_AREA],
11942 'C',
11943 glyph->charpos,
11944 (BUFFERP (glyph->object)
11945 ? 'B'
11946 : (STRINGP (glyph->object)
11947 ? 'S'
11948 : '-')),
11949 glyph->pixel_width,
11950 glyph->u.ch,
11951 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11952 ? glyph->u.ch
11953 : '.'),
11954 glyph->face_id,
11955 glyph->left_box_line_p,
11956 glyph->right_box_line_p);
11957 }
11958 else if (glyph->type == STRETCH_GLYPH)
11959 {
11960 fprintf (stderr,
11961 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11962 glyph - row->glyphs[TEXT_AREA],
11963 'S',
11964 glyph->charpos,
11965 (BUFFERP (glyph->object)
11966 ? 'B'
11967 : (STRINGP (glyph->object)
11968 ? 'S'
11969 : '-')),
11970 glyph->pixel_width,
11971 0,
11972 '.',
11973 glyph->face_id,
11974 glyph->left_box_line_p,
11975 glyph->right_box_line_p);
11976 }
11977 else if (glyph->type == IMAGE_GLYPH)
11978 {
11979 fprintf (stderr,
11980 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11981 glyph - row->glyphs[TEXT_AREA],
11982 'I',
11983 glyph->charpos,
11984 (BUFFERP (glyph->object)
11985 ? 'B'
11986 : (STRINGP (glyph->object)
11987 ? 'S'
11988 : '-')),
11989 glyph->pixel_width,
11990 glyph->u.img_id,
11991 '.',
11992 glyph->face_id,
11993 glyph->left_box_line_p,
11994 glyph->right_box_line_p);
11995 }
11996 }
11997
11998
11999 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12000 GLYPHS 0 means don't show glyph contents.
12001 GLYPHS 1 means show glyphs in short form
12002 GLYPHS > 1 means show glyphs in long form. */
12003
12004 void
12005 dump_glyph_row (row, vpos, glyphs)
12006 struct glyph_row *row;
12007 int vpos, glyphs;
12008 {
12009 if (glyphs != 1)
12010 {
12011 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12012 fprintf (stderr, "=======================================================================\n");
12013
12014 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
12015 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12016 vpos,
12017 MATRIX_ROW_START_CHARPOS (row),
12018 MATRIX_ROW_END_CHARPOS (row),
12019 row->used[TEXT_AREA],
12020 row->contains_overlapping_glyphs_p,
12021 row->enabled_p,
12022 row->inverse_p,
12023 row->truncated_on_left_p,
12024 row->truncated_on_right_p,
12025 row->overlay_arrow_p,
12026 row->continued_p,
12027 MATRIX_ROW_CONTINUATION_LINE_P (row),
12028 row->displays_text_p,
12029 row->ends_at_zv_p,
12030 row->fill_line_p,
12031 row->ends_in_middle_of_char_p,
12032 row->starts_in_middle_of_char_p,
12033 row->mouse_face_p,
12034 row->x,
12035 row->y,
12036 row->pixel_width,
12037 row->height,
12038 row->visible_height,
12039 row->ascent,
12040 row->phys_ascent);
12041 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12042 row->end.overlay_string_index,
12043 row->continuation_lines_width);
12044 fprintf (stderr, "%9d %5d\n",
12045 CHARPOS (row->start.string_pos),
12046 CHARPOS (row->end.string_pos));
12047 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12048 row->end.dpvec_index);
12049 }
12050
12051 if (glyphs > 1)
12052 {
12053 int area;
12054
12055 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12056 {
12057 struct glyph *glyph = row->glyphs[area];
12058 struct glyph *glyph_end = glyph + row->used[area];
12059
12060 /* Glyph for a line end in text. */
12061 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12062 ++glyph_end;
12063
12064 if (glyph < glyph_end)
12065 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12066
12067 for (; glyph < glyph_end; ++glyph)
12068 dump_glyph (row, glyph, area);
12069 }
12070 }
12071 else if (glyphs == 1)
12072 {
12073 int area;
12074
12075 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12076 {
12077 char *s = (char *) alloca (row->used[area] + 1);
12078 int i;
12079
12080 for (i = 0; i < row->used[area]; ++i)
12081 {
12082 struct glyph *glyph = row->glyphs[area] + i;
12083 if (glyph->type == CHAR_GLYPH
12084 && glyph->u.ch < 0x80
12085 && glyph->u.ch >= ' ')
12086 s[i] = glyph->u.ch;
12087 else
12088 s[i] = '.';
12089 }
12090
12091 s[i] = '\0';
12092 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12093 }
12094 }
12095 }
12096
12097
12098 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12099 Sdump_glyph_matrix, 0, 1, "p",
12100 "Dump the current matrix of the selected window to stderr.\n\
12101 Shows contents of glyph row structures. With non-nil\n\
12102 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
12103 glyphs in short form, otherwise show glyphs in long form.")
12104 (glyphs)
12105 Lisp_Object glyphs;
12106 {
12107 struct window *w = XWINDOW (selected_window);
12108 struct buffer *buffer = XBUFFER (w->buffer);
12109
12110 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12111 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12112 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12113 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12114 fprintf (stderr, "=============================================\n");
12115 dump_glyph_matrix (w->current_matrix,
12116 NILP (glyphs) ? 0 : XINT (glyphs));
12117 return Qnil;
12118 }
12119
12120
12121 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12122 "Dump glyph row ROW to stderr.\n\
12123 GLYPH 0 means don't dump glyphs.\n\
12124 GLYPH 1 means dump glyphs in short form.\n\
12125 GLYPH > 1 or omitted means dump glyphs in long form.")
12126 (row, glyphs)
12127 Lisp_Object row, glyphs;
12128 {
12129 struct glyph_matrix *matrix;
12130 int vpos;
12131
12132 CHECK_NUMBER (row, 0);
12133 matrix = XWINDOW (selected_window)->current_matrix;
12134 vpos = XINT (row);
12135 if (vpos >= 0 && vpos < matrix->nrows)
12136 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12137 vpos,
12138 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12139 return Qnil;
12140 }
12141
12142
12143 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12144 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
12145 GLYPH 0 means don't dump glyphs.\n\
12146 GLYPH 1 means dump glyphs in short form.\n\
12147 GLYPH > 1 or omitted means dump glyphs in long form.")
12148 (row, glyphs)
12149 Lisp_Object row, glyphs;
12150 {
12151 struct frame *sf = SELECTED_FRAME ();
12152 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12153 int vpos;
12154
12155 CHECK_NUMBER (row, 0);
12156 vpos = XINT (row);
12157 if (vpos >= 0 && vpos < m->nrows)
12158 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12159 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12160 return Qnil;
12161 }
12162
12163
12164 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12165 "Toggle tracing of redisplay.\n\
12166 With ARG, turn tracing on if and only if ARG is positive.")
12167 (arg)
12168 Lisp_Object arg;
12169 {
12170 if (NILP (arg))
12171 trace_redisplay_p = !trace_redisplay_p;
12172 else
12173 {
12174 arg = Fprefix_numeric_value (arg);
12175 trace_redisplay_p = XINT (arg) > 0;
12176 }
12177
12178 return Qnil;
12179 }
12180
12181
12182 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12183 "Like `format', but print result to stderr.")
12184 (nargs, args)
12185 int nargs;
12186 Lisp_Object *args;
12187 {
12188 Lisp_Object s = Fformat (nargs, args);
12189 fprintf (stderr, "%s", XSTRING (s)->data);
12190 return Qnil;
12191 }
12192
12193 #endif /* GLYPH_DEBUG */
12194
12195
12196 \f
12197 /***********************************************************************
12198 Building Desired Matrix Rows
12199 ***********************************************************************/
12200
12201 /* Return a temporary glyph row holding the glyphs of an overlay
12202 arrow. Only used for non-window-redisplay windows. */
12203
12204 static struct glyph_row *
12205 get_overlay_arrow_glyph_row (w)
12206 struct window *w;
12207 {
12208 struct frame *f = XFRAME (WINDOW_FRAME (w));
12209 struct buffer *buffer = XBUFFER (w->buffer);
12210 struct buffer *old = current_buffer;
12211 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12212 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12213 unsigned char *arrow_end = arrow_string + arrow_len;
12214 unsigned char *p;
12215 struct it it;
12216 int multibyte_p;
12217 int n_glyphs_before;
12218
12219 set_buffer_temp (buffer);
12220 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12221 it.glyph_row->used[TEXT_AREA] = 0;
12222 SET_TEXT_POS (it.position, 0, 0);
12223
12224 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12225 p = arrow_string;
12226 while (p < arrow_end)
12227 {
12228 Lisp_Object face, ilisp;
12229
12230 /* Get the next character. */
12231 if (multibyte_p)
12232 it.c = string_char_and_length (p, arrow_len, &it.len);
12233 else
12234 it.c = *p, it.len = 1;
12235 p += it.len;
12236
12237 /* Get its face. */
12238 ilisp = make_number (p - arrow_string);
12239 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12240 it.face_id = compute_char_face (f, it.c, face);
12241
12242 /* Compute its width, get its glyphs. */
12243 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12244 SET_TEXT_POS (it.position, -1, -1);
12245 PRODUCE_GLYPHS (&it);
12246
12247 /* If this character doesn't fit any more in the line, we have
12248 to remove some glyphs. */
12249 if (it.current_x > it.last_visible_x)
12250 {
12251 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12252 break;
12253 }
12254 }
12255
12256 set_buffer_temp (old);
12257 return it.glyph_row;
12258 }
12259
12260
12261 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12262 glyphs are only inserted for terminal frames since we can't really
12263 win with truncation glyphs when partially visible glyphs are
12264 involved. Which glyphs to insert is determined by
12265 produce_special_glyphs. */
12266
12267 static void
12268 insert_left_trunc_glyphs (it)
12269 struct it *it;
12270 {
12271 struct it truncate_it;
12272 struct glyph *from, *end, *to, *toend;
12273
12274 xassert (!FRAME_WINDOW_P (it->f));
12275
12276 /* Get the truncation glyphs. */
12277 truncate_it = *it;
12278 truncate_it.current_x = 0;
12279 truncate_it.face_id = DEFAULT_FACE_ID;
12280 truncate_it.glyph_row = &scratch_glyph_row;
12281 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12282 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12283 truncate_it.object = make_number (0);
12284 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12285
12286 /* Overwrite glyphs from IT with truncation glyphs. */
12287 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12288 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12289 to = it->glyph_row->glyphs[TEXT_AREA];
12290 toend = to + it->glyph_row->used[TEXT_AREA];
12291
12292 while (from < end)
12293 *to++ = *from++;
12294
12295 /* There may be padding glyphs left over. Overwrite them too. */
12296 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12297 {
12298 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12299 while (from < end)
12300 *to++ = *from++;
12301 }
12302
12303 if (to > toend)
12304 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12305 }
12306
12307
12308 /* Compute the pixel height and width of IT->glyph_row.
12309
12310 Most of the time, ascent and height of a display line will be equal
12311 to the max_ascent and max_height values of the display iterator
12312 structure. This is not the case if
12313
12314 1. We hit ZV without displaying anything. In this case, max_ascent
12315 and max_height will be zero.
12316
12317 2. We have some glyphs that don't contribute to the line height.
12318 (The glyph row flag contributes_to_line_height_p is for future
12319 pixmap extensions).
12320
12321 The first case is easily covered by using default values because in
12322 these cases, the line height does not really matter, except that it
12323 must not be zero. */
12324
12325 static void
12326 compute_line_metrics (it)
12327 struct it *it;
12328 {
12329 struct glyph_row *row = it->glyph_row;
12330 int area, i;
12331
12332 if (FRAME_WINDOW_P (it->f))
12333 {
12334 int i, min_y, max_y;
12335
12336 /* The line may consist of one space only, that was added to
12337 place the cursor on it. If so, the row's height hasn't been
12338 computed yet. */
12339 if (row->height == 0)
12340 {
12341 if (it->max_ascent + it->max_descent == 0)
12342 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12343 row->ascent = it->max_ascent;
12344 row->height = it->max_ascent + it->max_descent;
12345 row->phys_ascent = it->max_phys_ascent;
12346 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12347 }
12348
12349 /* Compute the width of this line. */
12350 row->pixel_width = row->x;
12351 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12352 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12353
12354 xassert (row->pixel_width >= 0);
12355 xassert (row->ascent >= 0 && row->height > 0);
12356
12357 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12358 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12359
12360 /* If first line's physical ascent is larger than its logical
12361 ascent, use the physical ascent, and make the row taller.
12362 This makes accented characters fully visible. */
12363 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12364 && row->phys_ascent > row->ascent)
12365 {
12366 row->height += row->phys_ascent - row->ascent;
12367 row->ascent = row->phys_ascent;
12368 }
12369
12370 /* Compute how much of the line is visible. */
12371 row->visible_height = row->height;
12372
12373 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12374 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12375
12376 if (row->y < min_y)
12377 row->visible_height -= min_y - row->y;
12378 if (row->y + row->height > max_y)
12379 row->visible_height -= row->y + row->height - max_y;
12380 }
12381 else
12382 {
12383 row->pixel_width = row->used[TEXT_AREA];
12384 if (row->continued_p)
12385 row->pixel_width -= it->continuation_pixel_width;
12386 else if (row->truncated_on_right_p)
12387 row->pixel_width -= it->truncation_pixel_width;
12388 row->ascent = row->phys_ascent = 0;
12389 row->height = row->phys_height = row->visible_height = 1;
12390 }
12391
12392 /* Compute a hash code for this row. */
12393 row->hash = 0;
12394 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12395 for (i = 0; i < row->used[area]; ++i)
12396 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12397 + row->glyphs[area][i].u.val
12398 + row->glyphs[area][i].face_id
12399 + row->glyphs[area][i].padding_p
12400 + (row->glyphs[area][i].type << 2));
12401
12402 it->max_ascent = it->max_descent = 0;
12403 it->max_phys_ascent = it->max_phys_descent = 0;
12404 }
12405
12406
12407 /* Append one space to the glyph row of iterator IT if doing a
12408 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12409 space have the default face, otherwise let it have the same face as
12410 IT->face_id. Value is non-zero if a space was added.
12411
12412 This function is called to make sure that there is always one glyph
12413 at the end of a glyph row that the cursor can be set on under
12414 window-systems. (If there weren't such a glyph we would not know
12415 how wide and tall a box cursor should be displayed).
12416
12417 At the same time this space let's a nicely handle clearing to the
12418 end of the line if the row ends in italic text. */
12419
12420 static int
12421 append_space (it, default_face_p)
12422 struct it *it;
12423 int default_face_p;
12424 {
12425 if (FRAME_WINDOW_P (it->f))
12426 {
12427 int n = it->glyph_row->used[TEXT_AREA];
12428
12429 if (it->glyph_row->glyphs[TEXT_AREA] + n
12430 < it->glyph_row->glyphs[1 + TEXT_AREA])
12431 {
12432 /* Save some values that must not be changed.
12433 Must save IT->c and IT->len because otherwise
12434 ITERATOR_AT_END_P wouldn't work anymore after
12435 append_space has been called. */
12436 enum display_element_type saved_what = it->what;
12437 int saved_c = it->c, saved_len = it->len;
12438 int saved_x = it->current_x;
12439 int saved_face_id = it->face_id;
12440 struct text_pos saved_pos;
12441 Lisp_Object saved_object;
12442 struct face *face;
12443
12444 saved_object = it->object;
12445 saved_pos = it->position;
12446
12447 it->what = IT_CHARACTER;
12448 bzero (&it->position, sizeof it->position);
12449 it->object = make_number (0);
12450 it->c = ' ';
12451 it->len = 1;
12452
12453 if (default_face_p)
12454 it->face_id = DEFAULT_FACE_ID;
12455 else if (it->face_before_selective_p)
12456 it->face_id = it->saved_face_id;
12457 face = FACE_FROM_ID (it->f, it->face_id);
12458 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12459
12460 PRODUCE_GLYPHS (it);
12461
12462 it->current_x = saved_x;
12463 it->object = saved_object;
12464 it->position = saved_pos;
12465 it->what = saved_what;
12466 it->face_id = saved_face_id;
12467 it->len = saved_len;
12468 it->c = saved_c;
12469 return 1;
12470 }
12471 }
12472
12473 return 0;
12474 }
12475
12476
12477 /* Extend the face of the last glyph in the text area of IT->glyph_row
12478 to the end of the display line. Called from display_line.
12479 If the glyph row is empty, add a space glyph to it so that we
12480 know the face to draw. Set the glyph row flag fill_line_p. */
12481
12482 static void
12483 extend_face_to_end_of_line (it)
12484 struct it *it;
12485 {
12486 struct face *face;
12487 struct frame *f = it->f;
12488
12489 /* If line is already filled, do nothing. */
12490 if (it->current_x >= it->last_visible_x)
12491 return;
12492
12493 /* Face extension extends the background and box of IT->face_id
12494 to the end of the line. If the background equals the background
12495 of the frame, we don't have to do anything. */
12496 if (it->face_before_selective_p)
12497 face = FACE_FROM_ID (it->f, it->saved_face_id);
12498 else
12499 face = FACE_FROM_ID (f, it->face_id);
12500
12501 if (FRAME_WINDOW_P (f)
12502 && face->box == FACE_NO_BOX
12503 && face->background == FRAME_BACKGROUND_PIXEL (f)
12504 && !face->stipple)
12505 return;
12506
12507 /* Set the glyph row flag indicating that the face of the last glyph
12508 in the text area has to be drawn to the end of the text area. */
12509 it->glyph_row->fill_line_p = 1;
12510
12511 /* If current character of IT is not ASCII, make sure we have the
12512 ASCII face. This will be automatically undone the next time
12513 get_next_display_element returns a multibyte character. Note
12514 that the character will always be single byte in unibyte text. */
12515 if (!SINGLE_BYTE_CHAR_P (it->c))
12516 {
12517 it->face_id = FACE_FOR_CHAR (f, face, 0);
12518 }
12519
12520 if (FRAME_WINDOW_P (f))
12521 {
12522 /* If the row is empty, add a space with the current face of IT,
12523 so that we know which face to draw. */
12524 if (it->glyph_row->used[TEXT_AREA] == 0)
12525 {
12526 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12527 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12528 it->glyph_row->used[TEXT_AREA] = 1;
12529 }
12530 }
12531 else
12532 {
12533 /* Save some values that must not be changed. */
12534 int saved_x = it->current_x;
12535 struct text_pos saved_pos;
12536 Lisp_Object saved_object;
12537 enum display_element_type saved_what = it->what;
12538 int saved_face_id = it->face_id;
12539
12540 saved_object = it->object;
12541 saved_pos = it->position;
12542
12543 it->what = IT_CHARACTER;
12544 bzero (&it->position, sizeof it->position);
12545 it->object = make_number (0);
12546 it->c = ' ';
12547 it->len = 1;
12548 it->face_id = face->id;
12549
12550 PRODUCE_GLYPHS (it);
12551
12552 while (it->current_x <= it->last_visible_x)
12553 PRODUCE_GLYPHS (it);
12554
12555 /* Don't count these blanks really. It would let us insert a left
12556 truncation glyph below and make us set the cursor on them, maybe. */
12557 it->current_x = saved_x;
12558 it->object = saved_object;
12559 it->position = saved_pos;
12560 it->what = saved_what;
12561 it->face_id = saved_face_id;
12562 }
12563 }
12564
12565
12566 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12567 trailing whitespace. */
12568
12569 static int
12570 trailing_whitespace_p (charpos)
12571 int charpos;
12572 {
12573 int bytepos = CHAR_TO_BYTE (charpos);
12574 int c = 0;
12575
12576 while (bytepos < ZV_BYTE
12577 && (c = FETCH_CHAR (bytepos),
12578 c == ' ' || c == '\t'))
12579 ++bytepos;
12580
12581 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12582 {
12583 if (bytepos != PT_BYTE)
12584 return 1;
12585 }
12586 return 0;
12587 }
12588
12589
12590 /* Highlight trailing whitespace, if any, in ROW. */
12591
12592 void
12593 highlight_trailing_whitespace (f, row)
12594 struct frame *f;
12595 struct glyph_row *row;
12596 {
12597 int used = row->used[TEXT_AREA];
12598
12599 if (used)
12600 {
12601 struct glyph *start = row->glyphs[TEXT_AREA];
12602 struct glyph *glyph = start + used - 1;
12603
12604 /* Skip over glyphs inserted to display the cursor at the
12605 end of a line, for extending the face of the last glyph
12606 to the end of the line on terminals, and for truncation
12607 and continuation glyphs. */
12608 while (glyph >= start
12609 && glyph->type == CHAR_GLYPH
12610 && INTEGERP (glyph->object))
12611 --glyph;
12612
12613 /* If last glyph is a space or stretch, and it's trailing
12614 whitespace, set the face of all trailing whitespace glyphs in
12615 IT->glyph_row to `trailing-whitespace'. */
12616 if (glyph >= start
12617 && BUFFERP (glyph->object)
12618 && (glyph->type == STRETCH_GLYPH
12619 || (glyph->type == CHAR_GLYPH
12620 && glyph->u.ch == ' '))
12621 && trailing_whitespace_p (glyph->charpos))
12622 {
12623 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12624
12625 while (glyph >= start
12626 && BUFFERP (glyph->object)
12627 && (glyph->type == STRETCH_GLYPH
12628 || (glyph->type == CHAR_GLYPH
12629 && glyph->u.ch == ' ')))
12630 (glyph--)->face_id = face_id;
12631 }
12632 }
12633 }
12634
12635
12636 /* Value is non-zero if glyph row ROW in window W should be
12637 used to hold the cursor. */
12638
12639 static int
12640 cursor_row_p (w, row)
12641 struct window *w;
12642 struct glyph_row *row;
12643 {
12644 int cursor_row_p = 1;
12645
12646 if (PT == MATRIX_ROW_END_CHARPOS (row))
12647 {
12648 /* If the row ends with a newline from a string, we don't want
12649 the cursor there (if the row is continued it doesn't end in a
12650 newline). */
12651 if (CHARPOS (row->end.string_pos) >= 0
12652 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12653 cursor_row_p = row->continued_p;
12654
12655 /* If the row ends at ZV, display the cursor at the end of that
12656 row instead of at the start of the row below. */
12657 else if (row->ends_at_zv_p)
12658 cursor_row_p = 1;
12659 else
12660 cursor_row_p = 0;
12661 }
12662
12663 return cursor_row_p;
12664 }
12665
12666
12667 /* Construct the glyph row IT->glyph_row in the desired matrix of
12668 IT->w from text at the current position of IT. See dispextern.h
12669 for an overview of struct it. Value is non-zero if
12670 IT->glyph_row displays text, as opposed to a line displaying ZV
12671 only. */
12672
12673 static int
12674 display_line (it)
12675 struct it *it;
12676 {
12677 struct glyph_row *row = it->glyph_row;
12678
12679 /* We always start displaying at hpos zero even if hscrolled. */
12680 xassert (it->hpos == 0 && it->current_x == 0);
12681
12682 /* We must not display in a row that's not a text row. */
12683 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12684 < it->w->desired_matrix->nrows);
12685
12686 /* Is IT->w showing the region? */
12687 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12688
12689 /* Clear the result glyph row and enable it. */
12690 prepare_desired_row (row);
12691
12692 row->y = it->current_y;
12693 row->start = it->current;
12694 row->continuation_lines_width = it->continuation_lines_width;
12695 row->displays_text_p = 1;
12696 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12697 it->starts_in_middle_of_char_p = 0;
12698
12699 /* Arrange the overlays nicely for our purposes. Usually, we call
12700 display_line on only one line at a time, in which case this
12701 can't really hurt too much, or we call it on lines which appear
12702 one after another in the buffer, in which case all calls to
12703 recenter_overlay_lists but the first will be pretty cheap. */
12704 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12705
12706 /* Move over display elements that are not visible because we are
12707 hscrolled. This may stop at an x-position < IT->first_visible_x
12708 if the first glyph is partially visible or if we hit a line end. */
12709 if (it->current_x < it->first_visible_x)
12710 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12711 MOVE_TO_POS | MOVE_TO_X);
12712
12713 /* Get the initial row height. This is either the height of the
12714 text hscrolled, if there is any, or zero. */
12715 row->ascent = it->max_ascent;
12716 row->height = it->max_ascent + it->max_descent;
12717 row->phys_ascent = it->max_phys_ascent;
12718 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12719
12720 /* Loop generating characters. The loop is left with IT on the next
12721 character to display. */
12722 while (1)
12723 {
12724 int n_glyphs_before, hpos_before, x_before;
12725 int x, i, nglyphs;
12726 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12727
12728 /* Retrieve the next thing to display. Value is zero if end of
12729 buffer reached. */
12730 if (!get_next_display_element (it))
12731 {
12732 /* Maybe add a space at the end of this line that is used to
12733 display the cursor there under X. Set the charpos of the
12734 first glyph of blank lines not corresponding to any text
12735 to -1. */
12736 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12737 || row->used[TEXT_AREA] == 0)
12738 {
12739 row->glyphs[TEXT_AREA]->charpos = -1;
12740 row->displays_text_p = 0;
12741
12742 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12743 row->indicate_empty_line_p = 1;
12744 }
12745
12746 it->continuation_lines_width = 0;
12747 row->ends_at_zv_p = 1;
12748 break;
12749 }
12750
12751 /* Now, get the metrics of what we want to display. This also
12752 generates glyphs in `row' (which is IT->glyph_row). */
12753 n_glyphs_before = row->used[TEXT_AREA];
12754 x = it->current_x;
12755
12756 /* Remember the line height so far in case the next element doesn't
12757 fit on the line. */
12758 if (!it->truncate_lines_p)
12759 {
12760 ascent = it->max_ascent;
12761 descent = it->max_descent;
12762 phys_ascent = it->max_phys_ascent;
12763 phys_descent = it->max_phys_descent;
12764 }
12765
12766 PRODUCE_GLYPHS (it);
12767
12768 /* If this display element was in marginal areas, continue with
12769 the next one. */
12770 if (it->area != TEXT_AREA)
12771 {
12772 row->ascent = max (row->ascent, it->max_ascent);
12773 row->height = max (row->height, it->max_ascent + it->max_descent);
12774 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12775 row->phys_height = max (row->phys_height,
12776 it->max_phys_ascent + it->max_phys_descent);
12777 set_iterator_to_next (it, 1);
12778 continue;
12779 }
12780
12781 /* Does the display element fit on the line? If we truncate
12782 lines, we should draw past the right edge of the window. If
12783 we don't truncate, we want to stop so that we can display the
12784 continuation glyph before the right margin. If lines are
12785 continued, there are two possible strategies for characters
12786 resulting in more than 1 glyph (e.g. tabs): Display as many
12787 glyphs as possible in this line and leave the rest for the
12788 continuation line, or display the whole element in the next
12789 line. Original redisplay did the former, so we do it also. */
12790 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12791 hpos_before = it->hpos;
12792 x_before = x;
12793
12794 if (/* Not a newline. */
12795 nglyphs > 0
12796 /* Glyphs produced fit entirely in the line. */
12797 && it->current_x < it->last_visible_x)
12798 {
12799 it->hpos += nglyphs;
12800 row->ascent = max (row->ascent, it->max_ascent);
12801 row->height = max (row->height, it->max_ascent + it->max_descent);
12802 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12803 row->phys_height = max (row->phys_height,
12804 it->max_phys_ascent + it->max_phys_descent);
12805 if (it->current_x - it->pixel_width < it->first_visible_x)
12806 row->x = x - it->first_visible_x;
12807 }
12808 else
12809 {
12810 int new_x;
12811 struct glyph *glyph;
12812
12813 for (i = 0; i < nglyphs; ++i, x = new_x)
12814 {
12815 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12816 new_x = x + glyph->pixel_width;
12817
12818 if (/* Lines are continued. */
12819 !it->truncate_lines_p
12820 && (/* Glyph doesn't fit on the line. */
12821 new_x > it->last_visible_x
12822 /* Or it fits exactly on a window system frame. */
12823 || (new_x == it->last_visible_x
12824 && FRAME_WINDOW_P (it->f))))
12825 {
12826 /* End of a continued line. */
12827
12828 if (it->hpos == 0
12829 || (new_x == it->last_visible_x
12830 && FRAME_WINDOW_P (it->f)))
12831 {
12832 /* Current glyph is the only one on the line or
12833 fits exactly on the line. We must continue
12834 the line because we can't draw the cursor
12835 after the glyph. */
12836 row->continued_p = 1;
12837 it->current_x = new_x;
12838 it->continuation_lines_width += new_x;
12839 ++it->hpos;
12840 if (i == nglyphs - 1)
12841 set_iterator_to_next (it, 1);
12842 }
12843 else if (CHAR_GLYPH_PADDING_P (*glyph)
12844 && !FRAME_WINDOW_P (it->f))
12845 {
12846 /* A padding glyph that doesn't fit on this line.
12847 This means the whole character doesn't fit
12848 on the line. */
12849 row->used[TEXT_AREA] = n_glyphs_before;
12850
12851 /* Fill the rest of the row with continuation
12852 glyphs like in 20.x. */
12853 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12854 < row->glyphs[1 + TEXT_AREA])
12855 produce_special_glyphs (it, IT_CONTINUATION);
12856
12857 row->continued_p = 1;
12858 it->current_x = x_before;
12859 it->continuation_lines_width += x_before;
12860
12861 /* Restore the height to what it was before the
12862 element not fitting on the line. */
12863 it->max_ascent = ascent;
12864 it->max_descent = descent;
12865 it->max_phys_ascent = phys_ascent;
12866 it->max_phys_descent = phys_descent;
12867 }
12868 else
12869 {
12870 /* Display element draws past the right edge of
12871 the window. Restore positions to values
12872 before the element. The next line starts
12873 with current_x before the glyph that could
12874 not be displayed, so that TAB works right. */
12875 row->used[TEXT_AREA] = n_glyphs_before + i;
12876
12877 /* Display continuation glyphs. */
12878 if (!FRAME_WINDOW_P (it->f))
12879 produce_special_glyphs (it, IT_CONTINUATION);
12880 row->continued_p = 1;
12881
12882 it->current_x = x;
12883 it->continuation_lines_width += x;
12884 if (nglyphs > 1 && i > 0)
12885 {
12886 row->ends_in_middle_of_char_p = 1;
12887 it->starts_in_middle_of_char_p = 1;
12888 }
12889
12890 /* Restore the height to what it was before the
12891 element not fitting on the line. */
12892 it->max_ascent = ascent;
12893 it->max_descent = descent;
12894 it->max_phys_ascent = phys_ascent;
12895 it->max_phys_descent = phys_descent;
12896 }
12897
12898 break;
12899 }
12900 else if (new_x > it->first_visible_x)
12901 {
12902 /* Increment number of glyphs actually displayed. */
12903 ++it->hpos;
12904
12905 if (x < it->first_visible_x)
12906 /* Glyph is partially visible, i.e. row starts at
12907 negative X position. */
12908 row->x = x - it->first_visible_x;
12909 }
12910 else
12911 {
12912 /* Glyph is completely off the left margin of the
12913 window. This should not happen because of the
12914 move_it_in_display_line at the start of
12915 this function. */
12916 abort ();
12917 }
12918 }
12919
12920 row->ascent = max (row->ascent, it->max_ascent);
12921 row->height = max (row->height, it->max_ascent + it->max_descent);
12922 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12923 row->phys_height = max (row->phys_height,
12924 it->max_phys_ascent + it->max_phys_descent);
12925
12926 /* End of this display line if row is continued. */
12927 if (row->continued_p)
12928 break;
12929 }
12930
12931 /* Is this a line end? If yes, we're also done, after making
12932 sure that a non-default face is extended up to the right
12933 margin of the window. */
12934 if (ITERATOR_AT_END_OF_LINE_P (it))
12935 {
12936 int used_before = row->used[TEXT_AREA];
12937
12938 /* Add a space at the end of the line that is used to
12939 display the cursor there. */
12940 append_space (it, 0);
12941
12942 /* Extend the face to the end of the line. */
12943 extend_face_to_end_of_line (it);
12944
12945 /* Make sure we have the position. */
12946 if (used_before == 0)
12947 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12948
12949 /* Consume the line end. This skips over invisible lines. */
12950 set_iterator_to_next (it, 1);
12951 it->continuation_lines_width = 0;
12952 break;
12953 }
12954
12955 /* Proceed with next display element. Note that this skips
12956 over lines invisible because of selective display. */
12957 set_iterator_to_next (it, 1);
12958
12959 /* If we truncate lines, we are done when the last displayed
12960 glyphs reach past the right margin of the window. */
12961 if (it->truncate_lines_p
12962 && (FRAME_WINDOW_P (it->f)
12963 ? (it->current_x >= it->last_visible_x)
12964 : (it->current_x > it->last_visible_x)))
12965 {
12966 /* Maybe add truncation glyphs. */
12967 if (!FRAME_WINDOW_P (it->f))
12968 {
12969 int i, n;
12970
12971 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12972 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12973 break;
12974
12975 for (n = row->used[TEXT_AREA]; i < n; ++i)
12976 {
12977 row->used[TEXT_AREA] = i;
12978 produce_special_glyphs (it, IT_TRUNCATION);
12979 }
12980 }
12981
12982 row->truncated_on_right_p = 1;
12983 it->continuation_lines_width = 0;
12984 reseat_at_next_visible_line_start (it, 0);
12985 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12986 it->hpos = hpos_before;
12987 it->current_x = x_before;
12988 break;
12989 }
12990 }
12991
12992 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12993 at the left window margin. */
12994 if (it->first_visible_x
12995 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12996 {
12997 if (!FRAME_WINDOW_P (it->f))
12998 insert_left_trunc_glyphs (it);
12999 row->truncated_on_left_p = 1;
13000 }
13001
13002 /* If the start of this line is the overlay arrow-position, then
13003 mark this glyph row as the one containing the overlay arrow.
13004 This is clearly a mess with variable size fonts. It would be
13005 better to let it be displayed like cursors under X. */
13006 if (MARKERP (Voverlay_arrow_position)
13007 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13008 && (MATRIX_ROW_START_CHARPOS (row)
13009 == marker_position (Voverlay_arrow_position))
13010 && STRINGP (Voverlay_arrow_string)
13011 && ! overlay_arrow_seen)
13012 {
13013 /* Overlay arrow in window redisplay is a bitmap. */
13014 if (!FRAME_WINDOW_P (it->f))
13015 {
13016 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13017 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13018 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13019 struct glyph *p = row->glyphs[TEXT_AREA];
13020 struct glyph *p2, *end;
13021
13022 /* Copy the arrow glyphs. */
13023 while (glyph < arrow_end)
13024 *p++ = *glyph++;
13025
13026 /* Throw away padding glyphs. */
13027 p2 = p;
13028 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13029 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13030 ++p2;
13031 if (p2 > p)
13032 {
13033 while (p2 < end)
13034 *p++ = *p2++;
13035 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13036 }
13037 }
13038
13039 overlay_arrow_seen = 1;
13040 row->overlay_arrow_p = 1;
13041 }
13042
13043 /* Compute pixel dimensions of this line. */
13044 compute_line_metrics (it);
13045
13046 /* Remember the position at which this line ends. */
13047 row->end = it->current;
13048
13049 /* Maybe set the cursor. */
13050 if (it->w->cursor.vpos < 0
13051 && PT >= MATRIX_ROW_START_CHARPOS (row)
13052 && PT <= MATRIX_ROW_END_CHARPOS (row)
13053 && cursor_row_p (it->w, row))
13054 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13055
13056 /* Highlight trailing whitespace. */
13057 if (!NILP (Vshow_trailing_whitespace))
13058 highlight_trailing_whitespace (it->f, it->glyph_row);
13059
13060 /* Prepare for the next line. This line starts horizontally at (X
13061 HPOS) = (0 0). Vertical positions are incremented. As a
13062 convenience for the caller, IT->glyph_row is set to the next
13063 row to be used. */
13064 it->current_x = it->hpos = 0;
13065 it->current_y += row->height;
13066 ++it->vpos;
13067 ++it->glyph_row;
13068 return row->displays_text_p;
13069 }
13070
13071
13072 \f
13073 /***********************************************************************
13074 Menu Bar
13075 ***********************************************************************/
13076
13077 /* Redisplay the menu bar in the frame for window W.
13078
13079 The menu bar of X frames that don't have X toolkit support is
13080 displayed in a special window W->frame->menu_bar_window.
13081
13082 The menu bar of terminal frames is treated specially as far as
13083 glyph matrices are concerned. Menu bar lines are not part of
13084 windows, so the update is done directly on the frame matrix rows
13085 for the menu bar. */
13086
13087 static void
13088 display_menu_bar (w)
13089 struct window *w;
13090 {
13091 struct frame *f = XFRAME (WINDOW_FRAME (w));
13092 struct it it;
13093 Lisp_Object items;
13094 int i;
13095
13096 /* Don't do all this for graphical frames. */
13097 #ifdef HAVE_NTGUI
13098 if (!NILP (Vwindow_system))
13099 return;
13100 #endif
13101 #ifdef USE_X_TOOLKIT
13102 if (FRAME_X_P (f))
13103 return;
13104 #endif
13105 #ifdef macintosh
13106 if (FRAME_MAC_P (f))
13107 return;
13108 #endif
13109
13110 #ifdef USE_X_TOOLKIT
13111 xassert (!FRAME_WINDOW_P (f));
13112 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13113 it.first_visible_x = 0;
13114 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13115 #else /* not USE_X_TOOLKIT */
13116 if (FRAME_WINDOW_P (f))
13117 {
13118 /* Menu bar lines are displayed in the desired matrix of the
13119 dummy window menu_bar_window. */
13120 struct window *menu_w;
13121 xassert (WINDOWP (f->menu_bar_window));
13122 menu_w = XWINDOW (f->menu_bar_window);
13123 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13124 MENU_FACE_ID);
13125 it.first_visible_x = 0;
13126 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13127 }
13128 else
13129 {
13130 /* This is a TTY frame, i.e. character hpos/vpos are used as
13131 pixel x/y. */
13132 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13133 MENU_FACE_ID);
13134 it.first_visible_x = 0;
13135 it.last_visible_x = FRAME_WIDTH (f);
13136 }
13137 #endif /* not USE_X_TOOLKIT */
13138
13139 if (! mode_line_inverse_video)
13140 /* Force the menu-bar to be displayed in the default face. */
13141 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13142
13143 /* Clear all rows of the menu bar. */
13144 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13145 {
13146 struct glyph_row *row = it.glyph_row + i;
13147 clear_glyph_row (row);
13148 row->enabled_p = 1;
13149 row->full_width_p = 1;
13150 }
13151
13152 /* Display all items of the menu bar. */
13153 items = FRAME_MENU_BAR_ITEMS (it.f);
13154 for (i = 0; i < XVECTOR (items)->size; i += 4)
13155 {
13156 Lisp_Object string;
13157
13158 /* Stop at nil string. */
13159 string = AREF (items, i + 1);
13160 if (NILP (string))
13161 break;
13162
13163 /* Remember where item was displayed. */
13164 AREF (items, i + 3) = make_number (it.hpos);
13165
13166 /* Display the item, pad with one space. */
13167 if (it.current_x < it.last_visible_x)
13168 display_string (NULL, string, Qnil, 0, 0, &it,
13169 XSTRING (string)->size + 1, 0, 0, -1);
13170 }
13171
13172 /* Fill out the line with spaces. */
13173 if (it.current_x < it.last_visible_x)
13174 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13175
13176 /* Compute the total height of the lines. */
13177 compute_line_metrics (&it);
13178 }
13179
13180
13181 \f
13182 /***********************************************************************
13183 Mode Line
13184 ***********************************************************************/
13185
13186 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13187 FORCE is non-zero, redisplay mode lines unconditionally.
13188 Otherwise, redisplay only mode lines that are garbaged. Value is
13189 the number of windows whose mode lines were redisplayed. */
13190
13191 static int
13192 redisplay_mode_lines (window, force)
13193 Lisp_Object window;
13194 int force;
13195 {
13196 int nwindows = 0;
13197
13198 while (!NILP (window))
13199 {
13200 struct window *w = XWINDOW (window);
13201
13202 if (WINDOWP (w->hchild))
13203 nwindows += redisplay_mode_lines (w->hchild, force);
13204 else if (WINDOWP (w->vchild))
13205 nwindows += redisplay_mode_lines (w->vchild, force);
13206 else if (force
13207 || FRAME_GARBAGED_P (XFRAME (w->frame))
13208 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13209 {
13210 Lisp_Object old_selected_frame;
13211 struct text_pos lpoint;
13212 struct buffer *old = current_buffer;
13213
13214 /* Set the window's buffer for the mode line display. */
13215 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13216 set_buffer_internal_1 (XBUFFER (w->buffer));
13217
13218 /* Point refers normally to the selected window. For any
13219 other window, set up appropriate value. */
13220 if (!EQ (window, selected_window))
13221 {
13222 struct text_pos pt;
13223
13224 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13225 if (CHARPOS (pt) < BEGV)
13226 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13227 else if (CHARPOS (pt) > (ZV - 1))
13228 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13229 else
13230 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13231 }
13232
13233 /* Temporarily set up the selected frame. */
13234 old_selected_frame = selected_frame;
13235 selected_frame = w->frame;
13236
13237 /* Display mode lines. */
13238 clear_glyph_matrix (w->desired_matrix);
13239 if (display_mode_lines (w))
13240 {
13241 ++nwindows;
13242 w->must_be_updated_p = 1;
13243 }
13244
13245 /* Restore old settings. */
13246 selected_frame = old_selected_frame;
13247 set_buffer_internal_1 (old);
13248 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13249 }
13250
13251 window = w->next;
13252 }
13253
13254 return nwindows;
13255 }
13256
13257
13258 /* Display the mode and/or top line of window W. Value is the number
13259 of mode lines displayed. */
13260
13261 static int
13262 display_mode_lines (w)
13263 struct window *w;
13264 {
13265 int n = 0;
13266
13267 /* These will be set while the mode line specs are processed. */
13268 line_number_displayed = 0;
13269 w->column_number_displayed = Qnil;
13270
13271 if (WINDOW_WANTS_MODELINE_P (w))
13272 {
13273 display_mode_line (w, MODE_LINE_FACE_ID,
13274 current_buffer->mode_line_format);
13275 ++n;
13276 }
13277
13278 if (WINDOW_WANTS_HEADER_LINE_P (w))
13279 {
13280 display_mode_line (w, HEADER_LINE_FACE_ID,
13281 current_buffer->header_line_format);
13282 ++n;
13283 }
13284
13285 return n;
13286 }
13287
13288
13289 /* Display mode or top line of window W. FACE_ID specifies which line
13290 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13291 FORMAT is the mode line format to display. Value is the pixel
13292 height of the mode line displayed. */
13293
13294 static int
13295 display_mode_line (w, face_id, format)
13296 struct window *w;
13297 enum face_id face_id;
13298 Lisp_Object format;
13299 {
13300 struct it it;
13301 struct face *face;
13302
13303 init_iterator (&it, w, -1, -1, NULL, face_id);
13304 prepare_desired_row (it.glyph_row);
13305
13306 if (! mode_line_inverse_video)
13307 /* Force the mode-line to be displayed in the default face. */
13308 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13309
13310 /* Temporarily make frame's keyboard the current kboard so that
13311 kboard-local variables in the mode_line_format will get the right
13312 values. */
13313 push_frame_kboard (it.f);
13314 display_mode_element (&it, 0, 0, 0, format);
13315 pop_frame_kboard ();
13316
13317 /* Fill up with spaces. */
13318 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13319
13320 compute_line_metrics (&it);
13321 it.glyph_row->full_width_p = 1;
13322 it.glyph_row->mode_line_p = 1;
13323 it.glyph_row->inverse_p = 0;
13324 it.glyph_row->continued_p = 0;
13325 it.glyph_row->truncated_on_left_p = 0;
13326 it.glyph_row->truncated_on_right_p = 0;
13327
13328 /* Make a 3D mode-line have a shadow at its right end. */
13329 face = FACE_FROM_ID (it.f, face_id);
13330 extend_face_to_end_of_line (&it);
13331 if (face->box != FACE_NO_BOX)
13332 {
13333 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13334 + it.glyph_row->used[TEXT_AREA] - 1);
13335 last->right_box_line_p = 1;
13336 }
13337
13338 return it.glyph_row->height;
13339 }
13340
13341
13342 /* Contribute ELT to the mode line for window IT->w. How it
13343 translates into text depends on its data type.
13344
13345 IT describes the display environment in which we display, as usual.
13346
13347 DEPTH is the depth in recursion. It is used to prevent
13348 infinite recursion here.
13349
13350 FIELD_WIDTH is the number of characters the display of ELT should
13351 occupy in the mode line, and PRECISION is the maximum number of
13352 characters to display from ELT's representation. See
13353 display_string for details. *
13354
13355 Returns the hpos of the end of the text generated by ELT. */
13356
13357 static int
13358 display_mode_element (it, depth, field_width, precision, elt)
13359 struct it *it;
13360 int depth;
13361 int field_width, precision;
13362 Lisp_Object elt;
13363 {
13364 int n = 0, field, prec;
13365
13366 tail_recurse:
13367 if (depth > 10)
13368 goto invalid;
13369
13370 depth++;
13371
13372 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13373 {
13374 case Lisp_String:
13375 {
13376 /* A string: output it and check for %-constructs within it. */
13377 unsigned char c;
13378 unsigned char *this = XSTRING (elt)->data;
13379 unsigned char *lisp_string = this;
13380
13381 while ((precision <= 0 || n < precision)
13382 && *this
13383 && (frame_title_ptr
13384 || it->current_x < it->last_visible_x))
13385 {
13386 unsigned char *last = this;
13387
13388 /* Advance to end of string or next format specifier. */
13389 while ((c = *this++) != '\0' && c != '%')
13390 ;
13391
13392 if (this - 1 != last)
13393 {
13394 /* Output to end of string or up to '%'. Field width
13395 is length of string. Don't output more than
13396 PRECISION allows us. */
13397 --this;
13398
13399 prec = chars_in_text (last, this - last);
13400 if (precision > 0 && prec > precision - n)
13401 prec = precision - n;
13402
13403 if (frame_title_ptr)
13404 n += store_frame_title (last, 0, prec);
13405 else
13406 {
13407 int bytepos = last - lisp_string;
13408 int charpos = string_byte_to_char (elt, bytepos);
13409 n += display_string (NULL, elt, Qnil, 0, charpos,
13410 it, 0, prec, 0, -1);
13411 }
13412 }
13413 else /* c == '%' */
13414 {
13415 unsigned char *percent_position = this;
13416
13417 /* Get the specified minimum width. Zero means
13418 don't pad. */
13419 field = 0;
13420 while ((c = *this++) >= '0' && c <= '9')
13421 field = field * 10 + c - '0';
13422
13423 /* Don't pad beyond the total padding allowed. */
13424 if (field_width - n > 0 && field > field_width - n)
13425 field = field_width - n;
13426
13427 /* Note that either PRECISION <= 0 or N < PRECISION. */
13428 prec = precision - n;
13429
13430 if (c == 'M')
13431 n += display_mode_element (it, depth, field, prec,
13432 Vglobal_mode_string);
13433 else if (c != 0)
13434 {
13435 unsigned char *spec
13436 = decode_mode_spec (it->w, c, field, prec);
13437
13438 if (frame_title_ptr)
13439 n += store_frame_title (spec, field, prec);
13440 else
13441 {
13442 int nglyphs_before
13443 = it->glyph_row->used[TEXT_AREA];
13444 int bytepos
13445 = percent_position - XSTRING (elt)->data;
13446 int charpos
13447 = string_byte_to_char (elt, bytepos);
13448 int nwritten
13449 = display_string (spec, Qnil, elt, charpos, 0, it,
13450 field, prec, 0, -1);
13451
13452 /* Assign to the glyphs written above the
13453 string where the `%x' came from, position
13454 of the `%'. */
13455 if (nwritten > 0)
13456 {
13457 struct glyph *glyph
13458 = (it->glyph_row->glyphs[TEXT_AREA]
13459 + nglyphs_before);
13460 int i;
13461
13462 for (i = 0; i < nwritten; ++i)
13463 {
13464 glyph[i].object = elt;
13465 glyph[i].charpos = charpos;
13466 }
13467
13468 n += nwritten;
13469 }
13470 }
13471 }
13472 }
13473 }
13474 }
13475 break;
13476
13477 case Lisp_Symbol:
13478 /* A symbol: process the value of the symbol recursively
13479 as if it appeared here directly. Avoid error if symbol void.
13480 Special case: if value of symbol is a string, output the string
13481 literally. */
13482 {
13483 register Lisp_Object tem;
13484 tem = Fboundp (elt);
13485 if (!NILP (tem))
13486 {
13487 tem = Fsymbol_value (elt);
13488 /* If value is a string, output that string literally:
13489 don't check for % within it. */
13490 if (STRINGP (tem))
13491 {
13492 prec = precision - n;
13493 if (frame_title_ptr)
13494 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13495 else
13496 n += display_string (NULL, tem, Qnil, 0, 0, it,
13497 0, prec, 0, -1);
13498 }
13499 else if (!EQ (tem, elt))
13500 {
13501 /* Give up right away for nil or t. */
13502 elt = tem;
13503 goto tail_recurse;
13504 }
13505 }
13506 }
13507 break;
13508
13509 case Lisp_Cons:
13510 {
13511 register Lisp_Object car, tem;
13512
13513 /* A cons cell: three distinct cases.
13514 If first element is a string or a cons, process all the elements
13515 and effectively concatenate them.
13516 If first element is a negative number, truncate displaying cdr to
13517 at most that many characters. If positive, pad (with spaces)
13518 to at least that many characters.
13519 If first element is a symbol, process the cadr or caddr recursively
13520 according to whether the symbol's value is non-nil or nil. */
13521 car = XCAR (elt);
13522 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13523 {
13524 /* An element of the form (:eval FORM) means evaluate FORM
13525 and use the result as mode line elements. */
13526 struct gcpro gcpro1;
13527 Lisp_Object spec;
13528
13529 spec = safe_eval (XCAR (XCDR (elt)));
13530 GCPRO1 (spec);
13531 n += display_mode_element (it, depth, field_width - n,
13532 precision - n, spec);
13533 UNGCPRO;
13534 }
13535 else if (SYMBOLP (car))
13536 {
13537 tem = Fboundp (car);
13538 elt = XCDR (elt);
13539 if (!CONSP (elt))
13540 goto invalid;
13541 /* elt is now the cdr, and we know it is a cons cell.
13542 Use its car if CAR has a non-nil value. */
13543 if (!NILP (tem))
13544 {
13545 tem = Fsymbol_value (car);
13546 if (!NILP (tem))
13547 {
13548 elt = XCAR (elt);
13549 goto tail_recurse;
13550 }
13551 }
13552 /* Symbol's value is nil (or symbol is unbound)
13553 Get the cddr of the original list
13554 and if possible find the caddr and use that. */
13555 elt = XCDR (elt);
13556 if (NILP (elt))
13557 break;
13558 else if (!CONSP (elt))
13559 goto invalid;
13560 elt = XCAR (elt);
13561 goto tail_recurse;
13562 }
13563 else if (INTEGERP (car))
13564 {
13565 register int lim = XINT (car);
13566 elt = XCDR (elt);
13567 if (lim < 0)
13568 {
13569 /* Negative int means reduce maximum width. */
13570 if (precision <= 0)
13571 precision = -lim;
13572 else
13573 precision = min (precision, -lim);
13574 }
13575 else if (lim > 0)
13576 {
13577 /* Padding specified. Don't let it be more than
13578 current maximum. */
13579 if (precision > 0)
13580 lim = min (precision, lim);
13581
13582 /* If that's more padding than already wanted, queue it.
13583 But don't reduce padding already specified even if
13584 that is beyond the current truncation point. */
13585 field_width = max (lim, field_width);
13586 }
13587 goto tail_recurse;
13588 }
13589 else if (STRINGP (car) || CONSP (car))
13590 {
13591 register int limit = 50;
13592 /* Limit is to protect against circular lists. */
13593 while (CONSP (elt)
13594 && --limit > 0
13595 && (precision <= 0 || n < precision))
13596 {
13597 n += display_mode_element (it, depth, field_width - n,
13598 precision - n, XCAR (elt));
13599 elt = XCDR (elt);
13600 }
13601 }
13602 }
13603 break;
13604
13605 default:
13606 invalid:
13607 if (frame_title_ptr)
13608 n += store_frame_title ("*invalid*", 0, precision - n);
13609 else
13610 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13611 precision - n, 0, 0);
13612 return n;
13613 }
13614
13615 /* Pad to FIELD_WIDTH. */
13616 if (field_width > 0 && n < field_width)
13617 {
13618 if (frame_title_ptr)
13619 n += store_frame_title ("", field_width - n, 0);
13620 else
13621 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13622 0, 0, 0);
13623 }
13624
13625 return n;
13626 }
13627
13628
13629 /* Write a null-terminated, right justified decimal representation of
13630 the positive integer D to BUF using a minimal field width WIDTH. */
13631
13632 static void
13633 pint2str (buf, width, d)
13634 register char *buf;
13635 register int width;
13636 register int d;
13637 {
13638 register char *p = buf;
13639
13640 if (d <= 0)
13641 *p++ = '0';
13642 else
13643 {
13644 while (d > 0)
13645 {
13646 *p++ = d % 10 + '0';
13647 d /= 10;
13648 }
13649 }
13650
13651 for (width -= (int) (p - buf); width > 0; --width)
13652 *p++ = ' ';
13653 *p-- = '\0';
13654 while (p > buf)
13655 {
13656 d = *buf;
13657 *buf++ = *p;
13658 *p-- = d;
13659 }
13660 }
13661
13662 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13663 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13664 type of CODING_SYSTEM. Return updated pointer into BUF. */
13665
13666 static unsigned char invalid_eol_type[] = "(*invalid*)";
13667
13668 static char *
13669 decode_mode_spec_coding (coding_system, buf, eol_flag)
13670 Lisp_Object coding_system;
13671 register char *buf;
13672 int eol_flag;
13673 {
13674 Lisp_Object val;
13675 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13676 unsigned char *eol_str;
13677 int eol_str_len;
13678 /* The EOL conversion we are using. */
13679 Lisp_Object eoltype;
13680
13681 val = Fget (coding_system, Qcoding_system);
13682 eoltype = Qnil;
13683
13684 if (!VECTORP (val)) /* Not yet decided. */
13685 {
13686 if (multibyte)
13687 *buf++ = '-';
13688 if (eol_flag)
13689 eoltype = eol_mnemonic_undecided;
13690 /* Don't mention EOL conversion if it isn't decided. */
13691 }
13692 else
13693 {
13694 Lisp_Object eolvalue;
13695
13696 eolvalue = Fget (coding_system, Qeol_type);
13697
13698 if (multibyte)
13699 *buf++ = XFASTINT (AREF (val, 1));
13700
13701 if (eol_flag)
13702 {
13703 /* The EOL conversion that is normal on this system. */
13704
13705 if (NILP (eolvalue)) /* Not yet decided. */
13706 eoltype = eol_mnemonic_undecided;
13707 else if (VECTORP (eolvalue)) /* Not yet decided. */
13708 eoltype = eol_mnemonic_undecided;
13709 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13710 eoltype = (XFASTINT (eolvalue) == 0
13711 ? eol_mnemonic_unix
13712 : (XFASTINT (eolvalue) == 1
13713 ? eol_mnemonic_dos : eol_mnemonic_mac));
13714 }
13715 }
13716
13717 if (eol_flag)
13718 {
13719 /* Mention the EOL conversion if it is not the usual one. */
13720 if (STRINGP (eoltype))
13721 {
13722 eol_str = XSTRING (eoltype)->data;
13723 eol_str_len = XSTRING (eoltype)->size;
13724 }
13725 else if (INTEGERP (eoltype)
13726 && CHAR_VALID_P (XINT (eoltype), 0))
13727 {
13728 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13729 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13730 }
13731 else
13732 {
13733 eol_str = invalid_eol_type;
13734 eol_str_len = sizeof (invalid_eol_type) - 1;
13735 }
13736 bcopy (eol_str, buf, eol_str_len);
13737 buf += eol_str_len;
13738 }
13739
13740 return buf;
13741 }
13742
13743 /* Return a string for the output of a mode line %-spec for window W,
13744 generated by character C. PRECISION >= 0 means don't return a
13745 string longer than that value. FIELD_WIDTH > 0 means pad the
13746 string returned with spaces to that value. */
13747
13748 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13749
13750 static char *
13751 decode_mode_spec (w, c, field_width, precision)
13752 struct window *w;
13753 register int c;
13754 int field_width, precision;
13755 {
13756 Lisp_Object obj;
13757 struct frame *f = XFRAME (WINDOW_FRAME (w));
13758 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13759 struct buffer *b = XBUFFER (w->buffer);
13760
13761 obj = Qnil;
13762
13763 switch (c)
13764 {
13765 case '*':
13766 if (!NILP (b->read_only))
13767 return "%";
13768 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13769 return "*";
13770 return "-";
13771
13772 case '+':
13773 /* This differs from %* only for a modified read-only buffer. */
13774 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13775 return "*";
13776 if (!NILP (b->read_only))
13777 return "%";
13778 return "-";
13779
13780 case '&':
13781 /* This differs from %* in ignoring read-only-ness. */
13782 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13783 return "*";
13784 return "-";
13785
13786 case '%':
13787 return "%";
13788
13789 case '[':
13790 {
13791 int i;
13792 char *p;
13793
13794 if (command_loop_level > 5)
13795 return "[[[... ";
13796 p = decode_mode_spec_buf;
13797 for (i = 0; i < command_loop_level; i++)
13798 *p++ = '[';
13799 *p = 0;
13800 return decode_mode_spec_buf;
13801 }
13802
13803 case ']':
13804 {
13805 int i;
13806 char *p;
13807
13808 if (command_loop_level > 5)
13809 return " ...]]]";
13810 p = decode_mode_spec_buf;
13811 for (i = 0; i < command_loop_level; i++)
13812 *p++ = ']';
13813 *p = 0;
13814 return decode_mode_spec_buf;
13815 }
13816
13817 case '-':
13818 {
13819 register int i;
13820
13821 /* Let lots_of_dashes be a string of infinite length. */
13822 if (field_width <= 0
13823 || field_width > sizeof (lots_of_dashes))
13824 {
13825 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13826 decode_mode_spec_buf[i] = '-';
13827 decode_mode_spec_buf[i] = '\0';
13828 return decode_mode_spec_buf;
13829 }
13830 else
13831 return lots_of_dashes;
13832 }
13833
13834 case 'b':
13835 obj = b->name;
13836 break;
13837
13838 case 'c':
13839 {
13840 int col = current_column ();
13841 w->column_number_displayed = make_number (col);
13842 pint2str (decode_mode_spec_buf, field_width, col);
13843 return decode_mode_spec_buf;
13844 }
13845
13846 case 'F':
13847 /* %F displays the frame name. */
13848 if (!NILP (f->title))
13849 return (char *) XSTRING (f->title)->data;
13850 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13851 return (char *) XSTRING (f->name)->data;
13852 return "Emacs";
13853
13854 case 'f':
13855 obj = b->filename;
13856 break;
13857
13858 case 'l':
13859 {
13860 int startpos = XMARKER (w->start)->charpos;
13861 int startpos_byte = marker_byte_position (w->start);
13862 int line, linepos, linepos_byte, topline;
13863 int nlines, junk;
13864 int height = XFASTINT (w->height);
13865
13866 /* If we decided that this buffer isn't suitable for line numbers,
13867 don't forget that too fast. */
13868 if (EQ (w->base_line_pos, w->buffer))
13869 goto no_value;
13870 /* But do forget it, if the window shows a different buffer now. */
13871 else if (BUFFERP (w->base_line_pos))
13872 w->base_line_pos = Qnil;
13873
13874 /* If the buffer is very big, don't waste time. */
13875 if (INTEGERP (Vline_number_display_limit)
13876 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13877 {
13878 w->base_line_pos = Qnil;
13879 w->base_line_number = Qnil;
13880 goto no_value;
13881 }
13882
13883 if (!NILP (w->base_line_number)
13884 && !NILP (w->base_line_pos)
13885 && XFASTINT (w->base_line_pos) <= startpos)
13886 {
13887 line = XFASTINT (w->base_line_number);
13888 linepos = XFASTINT (w->base_line_pos);
13889 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13890 }
13891 else
13892 {
13893 line = 1;
13894 linepos = BUF_BEGV (b);
13895 linepos_byte = BUF_BEGV_BYTE (b);
13896 }
13897
13898 /* Count lines from base line to window start position. */
13899 nlines = display_count_lines (linepos, linepos_byte,
13900 startpos_byte,
13901 startpos, &junk);
13902
13903 topline = nlines + line;
13904
13905 /* Determine a new base line, if the old one is too close
13906 or too far away, or if we did not have one.
13907 "Too close" means it's plausible a scroll-down would
13908 go back past it. */
13909 if (startpos == BUF_BEGV (b))
13910 {
13911 w->base_line_number = make_number (topline);
13912 w->base_line_pos = make_number (BUF_BEGV (b));
13913 }
13914 else if (nlines < height + 25 || nlines > height * 3 + 50
13915 || linepos == BUF_BEGV (b))
13916 {
13917 int limit = BUF_BEGV (b);
13918 int limit_byte = BUF_BEGV_BYTE (b);
13919 int position;
13920 int distance = (height * 2 + 30) * line_number_display_limit_width;
13921
13922 if (startpos - distance > limit)
13923 {
13924 limit = startpos - distance;
13925 limit_byte = CHAR_TO_BYTE (limit);
13926 }
13927
13928 nlines = display_count_lines (startpos, startpos_byte,
13929 limit_byte,
13930 - (height * 2 + 30),
13931 &position);
13932 /* If we couldn't find the lines we wanted within
13933 line_number_display_limit_width chars per line,
13934 give up on line numbers for this window. */
13935 if (position == limit_byte && limit == startpos - distance)
13936 {
13937 w->base_line_pos = w->buffer;
13938 w->base_line_number = Qnil;
13939 goto no_value;
13940 }
13941
13942 w->base_line_number = make_number (topline - nlines);
13943 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
13944 }
13945
13946 /* Now count lines from the start pos to point. */
13947 nlines = display_count_lines (startpos, startpos_byte,
13948 PT_BYTE, PT, &junk);
13949
13950 /* Record that we did display the line number. */
13951 line_number_displayed = 1;
13952
13953 /* Make the string to show. */
13954 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13955 return decode_mode_spec_buf;
13956 no_value:
13957 {
13958 char* p = decode_mode_spec_buf;
13959 int pad = field_width - 2;
13960 while (pad-- > 0)
13961 *p++ = ' ';
13962 *p++ = '?';
13963 *p++ = '?';
13964 *p = '\0';
13965 return decode_mode_spec_buf;
13966 }
13967 }
13968 break;
13969
13970 case 'm':
13971 obj = b->mode_name;
13972 break;
13973
13974 case 'n':
13975 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13976 return " Narrow";
13977 break;
13978
13979 case 'p':
13980 {
13981 int pos = marker_position (w->start);
13982 int total = BUF_ZV (b) - BUF_BEGV (b);
13983
13984 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13985 {
13986 if (pos <= BUF_BEGV (b))
13987 return "All";
13988 else
13989 return "Bottom";
13990 }
13991 else if (pos <= BUF_BEGV (b))
13992 return "Top";
13993 else
13994 {
13995 if (total > 1000000)
13996 /* Do it differently for a large value, to avoid overflow. */
13997 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13998 else
13999 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14000 /* We can't normally display a 3-digit number,
14001 so get us a 2-digit number that is close. */
14002 if (total == 100)
14003 total = 99;
14004 sprintf (decode_mode_spec_buf, "%2d%%", total);
14005 return decode_mode_spec_buf;
14006 }
14007 }
14008
14009 /* Display percentage of size above the bottom of the screen. */
14010 case 'P':
14011 {
14012 int toppos = marker_position (w->start);
14013 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14014 int total = BUF_ZV (b) - BUF_BEGV (b);
14015
14016 if (botpos >= BUF_ZV (b))
14017 {
14018 if (toppos <= BUF_BEGV (b))
14019 return "All";
14020 else
14021 return "Bottom";
14022 }
14023 else
14024 {
14025 if (total > 1000000)
14026 /* Do it differently for a large value, to avoid overflow. */
14027 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14028 else
14029 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14030 /* We can't normally display a 3-digit number,
14031 so get us a 2-digit number that is close. */
14032 if (total == 100)
14033 total = 99;
14034 if (toppos <= BUF_BEGV (b))
14035 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14036 else
14037 sprintf (decode_mode_spec_buf, "%2d%%", total);
14038 return decode_mode_spec_buf;
14039 }
14040 }
14041
14042 case 's':
14043 /* status of process */
14044 obj = Fget_buffer_process (w->buffer);
14045 if (NILP (obj))
14046 return "no process";
14047 #ifdef subprocesses
14048 obj = Fsymbol_name (Fprocess_status (obj));
14049 #endif
14050 break;
14051
14052 case 't': /* indicate TEXT or BINARY */
14053 #ifdef MODE_LINE_BINARY_TEXT
14054 return MODE_LINE_BINARY_TEXT (b);
14055 #else
14056 return "T";
14057 #endif
14058
14059 case 'z':
14060 /* coding-system (not including end-of-line format) */
14061 case 'Z':
14062 /* coding-system (including end-of-line type) */
14063 {
14064 int eol_flag = (c == 'Z');
14065 char *p = decode_mode_spec_buf;
14066
14067 if (! FRAME_WINDOW_P (f))
14068 {
14069 /* No need to mention EOL here--the terminal never needs
14070 to do EOL conversion. */
14071 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14072 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14073 }
14074 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14075 p, eol_flag);
14076
14077 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14078 #ifdef subprocesses
14079 obj = Fget_buffer_process (Fcurrent_buffer ());
14080 if (PROCESSP (obj))
14081 {
14082 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14083 p, eol_flag);
14084 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14085 p, eol_flag);
14086 }
14087 #endif /* subprocesses */
14088 #endif /* 0 */
14089 *p = 0;
14090 return decode_mode_spec_buf;
14091 }
14092 }
14093
14094 if (STRINGP (obj))
14095 return (char *) XSTRING (obj)->data;
14096 else
14097 return "";
14098 }
14099
14100
14101 /* Count up to COUNT lines starting from START / START_BYTE.
14102 But don't go beyond LIMIT_BYTE.
14103 Return the number of lines thus found (always nonnegative).
14104
14105 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14106
14107 static int
14108 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14109 int start, start_byte, limit_byte, count;
14110 int *byte_pos_ptr;
14111 {
14112 register unsigned char *cursor;
14113 unsigned char *base;
14114
14115 register int ceiling;
14116 register unsigned char *ceiling_addr;
14117 int orig_count = count;
14118
14119 /* If we are not in selective display mode,
14120 check only for newlines. */
14121 int selective_display = (!NILP (current_buffer->selective_display)
14122 && !INTEGERP (current_buffer->selective_display));
14123
14124 if (count > 0)
14125 {
14126 while (start_byte < limit_byte)
14127 {
14128 ceiling = BUFFER_CEILING_OF (start_byte);
14129 ceiling = min (limit_byte - 1, ceiling);
14130 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14131 base = (cursor = BYTE_POS_ADDR (start_byte));
14132 while (1)
14133 {
14134 if (selective_display)
14135 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14136 ;
14137 else
14138 while (*cursor != '\n' && ++cursor != ceiling_addr)
14139 ;
14140
14141 if (cursor != ceiling_addr)
14142 {
14143 if (--count == 0)
14144 {
14145 start_byte += cursor - base + 1;
14146 *byte_pos_ptr = start_byte;
14147 return orig_count;
14148 }
14149 else
14150 if (++cursor == ceiling_addr)
14151 break;
14152 }
14153 else
14154 break;
14155 }
14156 start_byte += cursor - base;
14157 }
14158 }
14159 else
14160 {
14161 while (start_byte > limit_byte)
14162 {
14163 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14164 ceiling = max (limit_byte, ceiling);
14165 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14166 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14167 while (1)
14168 {
14169 if (selective_display)
14170 while (--cursor != ceiling_addr
14171 && *cursor != '\n' && *cursor != 015)
14172 ;
14173 else
14174 while (--cursor != ceiling_addr && *cursor != '\n')
14175 ;
14176
14177 if (cursor != ceiling_addr)
14178 {
14179 if (++count == 0)
14180 {
14181 start_byte += cursor - base + 1;
14182 *byte_pos_ptr = start_byte;
14183 /* When scanning backwards, we should
14184 not count the newline posterior to which we stop. */
14185 return - orig_count - 1;
14186 }
14187 }
14188 else
14189 break;
14190 }
14191 /* Here we add 1 to compensate for the last decrement
14192 of CURSOR, which took it past the valid range. */
14193 start_byte += cursor - base + 1;
14194 }
14195 }
14196
14197 *byte_pos_ptr = limit_byte;
14198
14199 if (count < 0)
14200 return - orig_count + count;
14201 return orig_count - count;
14202
14203 }
14204
14205
14206 \f
14207 /***********************************************************************
14208 Displaying strings
14209 ***********************************************************************/
14210
14211 /* Display a NUL-terminated string, starting with index START.
14212
14213 If STRING is non-null, display that C string. Otherwise, the Lisp
14214 string LISP_STRING is displayed.
14215
14216 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14217 FACE_STRING. Display STRING or LISP_STRING with the face at
14218 FACE_STRING_POS in FACE_STRING:
14219
14220 Display the string in the environment given by IT, but use the
14221 standard display table, temporarily.
14222
14223 FIELD_WIDTH is the minimum number of output glyphs to produce.
14224 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14225 with spaces. If STRING has more characters, more than FIELD_WIDTH
14226 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14227
14228 PRECISION is the maximum number of characters to output from
14229 STRING. PRECISION < 0 means don't truncate the string.
14230
14231 This is roughly equivalent to printf format specifiers:
14232
14233 FIELD_WIDTH PRECISION PRINTF
14234 ----------------------------------------
14235 -1 -1 %s
14236 -1 10 %.10s
14237 10 -1 %10s
14238 20 10 %20.10s
14239
14240 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14241 display them, and < 0 means obey the current buffer's value of
14242 enable_multibyte_characters.
14243
14244 Value is the number of glyphs produced. */
14245
14246 static int
14247 display_string (string, lisp_string, face_string, face_string_pos,
14248 start, it, field_width, precision, max_x, multibyte)
14249 unsigned char *string;
14250 Lisp_Object lisp_string;
14251 Lisp_Object face_string;
14252 int face_string_pos;
14253 int start;
14254 struct it *it;
14255 int field_width, precision, max_x;
14256 int multibyte;
14257 {
14258 int hpos_at_start = it->hpos;
14259 int saved_face_id = it->face_id;
14260 struct glyph_row *row = it->glyph_row;
14261
14262 /* Initialize the iterator IT for iteration over STRING beginning
14263 with index START. */
14264 reseat_to_string (it, string, lisp_string, start,
14265 precision, field_width, multibyte);
14266
14267 /* If displaying STRING, set up the face of the iterator
14268 from LISP_STRING, if that's given. */
14269 if (STRINGP (face_string))
14270 {
14271 int endptr;
14272 struct face *face;
14273
14274 it->face_id
14275 = face_at_string_position (it->w, face_string, face_string_pos,
14276 0, it->region_beg_charpos,
14277 it->region_end_charpos,
14278 &endptr, it->base_face_id, 0);
14279 face = FACE_FROM_ID (it->f, it->face_id);
14280 it->face_box_p = face->box != FACE_NO_BOX;
14281 }
14282
14283 /* Set max_x to the maximum allowed X position. Don't let it go
14284 beyond the right edge of the window. */
14285 if (max_x <= 0)
14286 max_x = it->last_visible_x;
14287 else
14288 max_x = min (max_x, it->last_visible_x);
14289
14290 /* Skip over display elements that are not visible. because IT->w is
14291 hscrolled. */
14292 if (it->current_x < it->first_visible_x)
14293 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14294 MOVE_TO_POS | MOVE_TO_X);
14295
14296 row->ascent = it->max_ascent;
14297 row->height = it->max_ascent + it->max_descent;
14298 row->phys_ascent = it->max_phys_ascent;
14299 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14300
14301 /* This condition is for the case that we are called with current_x
14302 past last_visible_x. */
14303 while (it->current_x < max_x)
14304 {
14305 int x_before, x, n_glyphs_before, i, nglyphs;
14306
14307 /* Get the next display element. */
14308 if (!get_next_display_element (it))
14309 break;
14310
14311 /* Produce glyphs. */
14312 x_before = it->current_x;
14313 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14314 PRODUCE_GLYPHS (it);
14315
14316 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14317 i = 0;
14318 x = x_before;
14319 while (i < nglyphs)
14320 {
14321 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14322
14323 if (!it->truncate_lines_p
14324 && x + glyph->pixel_width > max_x)
14325 {
14326 /* End of continued line or max_x reached. */
14327 if (CHAR_GLYPH_PADDING_P (*glyph))
14328 {
14329 /* A wide character is unbreakable. */
14330 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14331 it->current_x = x_before;
14332 }
14333 else
14334 {
14335 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14336 it->current_x = x;
14337 }
14338 break;
14339 }
14340 else if (x + glyph->pixel_width > it->first_visible_x)
14341 {
14342 /* Glyph is at least partially visible. */
14343 ++it->hpos;
14344 if (x < it->first_visible_x)
14345 it->glyph_row->x = x - it->first_visible_x;
14346 }
14347 else
14348 {
14349 /* Glyph is off the left margin of the display area.
14350 Should not happen. */
14351 abort ();
14352 }
14353
14354 row->ascent = max (row->ascent, it->max_ascent);
14355 row->height = max (row->height, it->max_ascent + it->max_descent);
14356 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14357 row->phys_height = max (row->phys_height,
14358 it->max_phys_ascent + it->max_phys_descent);
14359 x += glyph->pixel_width;
14360 ++i;
14361 }
14362
14363 /* Stop if max_x reached. */
14364 if (i < nglyphs)
14365 break;
14366
14367 /* Stop at line ends. */
14368 if (ITERATOR_AT_END_OF_LINE_P (it))
14369 {
14370 it->continuation_lines_width = 0;
14371 break;
14372 }
14373
14374 set_iterator_to_next (it, 1);
14375
14376 /* Stop if truncating at the right edge. */
14377 if (it->truncate_lines_p
14378 && it->current_x >= it->last_visible_x)
14379 {
14380 /* Add truncation mark, but don't do it if the line is
14381 truncated at a padding space. */
14382 if (IT_CHARPOS (*it) < it->string_nchars)
14383 {
14384 if (!FRAME_WINDOW_P (it->f))
14385 {
14386 int i, n;
14387
14388 if (it->current_x > it->last_visible_x)
14389 {
14390 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14391 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14392 break;
14393 for (n = row->used[TEXT_AREA]; i < n; ++i)
14394 {
14395 row->used[TEXT_AREA] = i;
14396 produce_special_glyphs (it, IT_TRUNCATION);
14397 }
14398 }
14399 produce_special_glyphs (it, IT_TRUNCATION);
14400 }
14401 it->glyph_row->truncated_on_right_p = 1;
14402 }
14403 break;
14404 }
14405 }
14406
14407 /* Maybe insert a truncation at the left. */
14408 if (it->first_visible_x
14409 && IT_CHARPOS (*it) > 0)
14410 {
14411 if (!FRAME_WINDOW_P (it->f))
14412 insert_left_trunc_glyphs (it);
14413 it->glyph_row->truncated_on_left_p = 1;
14414 }
14415
14416 it->face_id = saved_face_id;
14417
14418 /* Value is number of columns displayed. */
14419 return it->hpos - hpos_at_start;
14420 }
14421
14422
14423 \f
14424 /* This is like a combination of memq and assq. Return 1 if PROPVAL
14425 appears as an element of LIST or as the car of an element of LIST.
14426 If PROPVAL is a list, compare each element against LIST in that
14427 way, and return 1 if any element of PROPVAL is found in LIST.
14428 Otherwise return 0. This function cannot quit. */
14429
14430 int
14431 invisible_p (propval, list)
14432 register Lisp_Object propval;
14433 Lisp_Object list;
14434 {
14435 register Lisp_Object tail, proptail;
14436
14437 for (tail = list; CONSP (tail); tail = XCDR (tail))
14438 {
14439 register Lisp_Object tem;
14440 tem = XCAR (tail);
14441 if (EQ (propval, tem))
14442 return 1;
14443 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14444 return 1;
14445 }
14446
14447 if (CONSP (propval))
14448 {
14449 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14450 {
14451 Lisp_Object propelt;
14452 propelt = XCAR (proptail);
14453 for (tail = list; CONSP (tail); tail = XCDR (tail))
14454 {
14455 register Lisp_Object tem;
14456 tem = XCAR (tail);
14457 if (EQ (propelt, tem))
14458 return 1;
14459 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14460 return 1;
14461 }
14462 }
14463 }
14464
14465 return 0;
14466 }
14467
14468
14469 /* Return 1 if PROPVAL appears as the car of an element of LIST and
14470 the cdr of that element is non-nil. If PROPVAL is a list, check
14471 each element of PROPVAL in that way, and the first time some
14472 element is found, return 1 if the cdr of that element is non-nil.
14473 Otherwise return 0. This function cannot quit. */
14474
14475 int
14476 invisible_ellipsis_p (propval, list)
14477 register Lisp_Object propval;
14478 Lisp_Object list;
14479 {
14480 register Lisp_Object tail, proptail;
14481
14482 for (tail = list; CONSP (tail); tail = XCDR (tail))
14483 {
14484 register Lisp_Object tem;
14485 tem = XCAR (tail);
14486 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14487 return ! NILP (XCDR (tem));
14488 }
14489
14490 if (CONSP (propval))
14491 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14492 {
14493 Lisp_Object propelt;
14494 propelt = XCAR (proptail);
14495 for (tail = list; CONSP (tail); tail = XCDR (tail))
14496 {
14497 register Lisp_Object tem;
14498 tem = XCAR (tail);
14499 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14500 return ! NILP (XCDR (tem));
14501 }
14502 }
14503
14504 return 0;
14505 }
14506
14507
14508 \f
14509 /***********************************************************************
14510 Initialization
14511 ***********************************************************************/
14512
14513 void
14514 syms_of_xdisp ()
14515 {
14516 Vwith_echo_area_save_vector = Qnil;
14517 staticpro (&Vwith_echo_area_save_vector);
14518
14519 Vmessage_stack = Qnil;
14520 staticpro (&Vmessage_stack);
14521
14522 Qinhibit_redisplay = intern ("inhibit-redisplay");
14523 staticpro (&Qinhibit_redisplay);
14524
14525 #if GLYPH_DEBUG
14526 defsubr (&Sdump_glyph_matrix);
14527 defsubr (&Sdump_glyph_row);
14528 defsubr (&Sdump_tool_bar_row);
14529 defsubr (&Strace_redisplay);
14530 defsubr (&Strace_to_stderr);
14531 #endif
14532 #ifdef HAVE_WINDOW_SYSTEM
14533 defsubr (&Stool_bar_lines_needed);
14534 #endif
14535
14536 staticpro (&Qmenu_bar_update_hook);
14537 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14538
14539 staticpro (&Qoverriding_terminal_local_map);
14540 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14541
14542 staticpro (&Qoverriding_local_map);
14543 Qoverriding_local_map = intern ("overriding-local-map");
14544
14545 staticpro (&Qwindow_scroll_functions);
14546 Qwindow_scroll_functions = intern ("window-scroll-functions");
14547
14548 staticpro (&Qredisplay_end_trigger_functions);
14549 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14550
14551 staticpro (&Qinhibit_point_motion_hooks);
14552 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14553
14554 QCdata = intern (":data");
14555 staticpro (&QCdata);
14556 Qdisplay = intern ("display");
14557 staticpro (&Qdisplay);
14558 Qspace_width = intern ("space-width");
14559 staticpro (&Qspace_width);
14560 Qraise = intern ("raise");
14561 staticpro (&Qraise);
14562 Qspace = intern ("space");
14563 staticpro (&Qspace);
14564 Qmargin = intern ("margin");
14565 staticpro (&Qmargin);
14566 Qleft_margin = intern ("left-margin");
14567 staticpro (&Qleft_margin);
14568 Qright_margin = intern ("right-margin");
14569 staticpro (&Qright_margin);
14570 Qalign_to = intern ("align-to");
14571 staticpro (&Qalign_to);
14572 QCalign_to = intern (":align-to");
14573 staticpro (&QCalign_to);
14574 Qrelative_width = intern ("relative-width");
14575 staticpro (&Qrelative_width);
14576 QCrelative_width = intern (":relative-width");
14577 staticpro (&QCrelative_width);
14578 QCrelative_height = intern (":relative-height");
14579 staticpro (&QCrelative_height);
14580 QCeval = intern (":eval");
14581 staticpro (&QCeval);
14582 Qwhen = intern ("when");
14583 staticpro (&Qwhen);
14584 QCfile = intern (":file");
14585 staticpro (&QCfile);
14586 Qfontified = intern ("fontified");
14587 staticpro (&Qfontified);
14588 Qfontification_functions = intern ("fontification-functions");
14589 staticpro (&Qfontification_functions);
14590 Qtrailing_whitespace = intern ("trailing-whitespace");
14591 staticpro (&Qtrailing_whitespace);
14592 Qimage = intern ("image");
14593 staticpro (&Qimage);
14594 Qmessage_truncate_lines = intern ("message-truncate-lines");
14595 staticpro (&Qmessage_truncate_lines);
14596 Qgrow_only = intern ("grow-only");
14597 staticpro (&Qgrow_only);
14598 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14599 staticpro (&Qinhibit_menubar_update);
14600 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14601 staticpro (&Qinhibit_eval_during_redisplay);
14602 Qposition = intern ("position");
14603 staticpro (&Qposition);
14604 Qbuffer_position = intern ("buffer-position");
14605 staticpro (&Qbuffer_position);
14606 Qobject = intern ("object");
14607 staticpro (&Qobject);
14608
14609 last_arrow_position = Qnil;
14610 last_arrow_string = Qnil;
14611 staticpro (&last_arrow_position);
14612 staticpro (&last_arrow_string);
14613
14614 echo_buffer[0] = echo_buffer[1] = Qnil;
14615 staticpro (&echo_buffer[0]);
14616 staticpro (&echo_buffer[1]);
14617
14618 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14619 staticpro (&echo_area_buffer[0]);
14620 staticpro (&echo_area_buffer[1]);
14621
14622 Vmessages_buffer_name = build_string ("*Messages*");
14623 staticpro (&Vmessages_buffer_name);
14624
14625 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14626 "Non-nil means highlight trailing whitespace.\n\
14627 The face used for trailing whitespace is `trailing-whitespace'.");
14628 Vshow_trailing_whitespace = Qnil;
14629
14630 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14631 "Non-nil means don't actually do any redisplay.\n\
14632 This is used for internal purposes.");
14633 Vinhibit_redisplay = Qnil;
14634
14635 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14636 "String (or mode line construct) included (normally) in `mode-line-format'.");
14637 Vglobal_mode_string = Qnil;
14638
14639 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14640 "Marker for where to display an arrow on top of the buffer text.\n\
14641 This must be the beginning of a line in order to work.\n\
14642 See also `overlay-arrow-string'.");
14643 Voverlay_arrow_position = Qnil;
14644
14645 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14646 "String to display as an arrow. See also `overlay-arrow-position'.");
14647 Voverlay_arrow_string = Qnil;
14648
14649 DEFVAR_INT ("scroll-step", &scroll_step,
14650 "*The number of lines to try scrolling a window by when point moves out.\n\
14651 If that fails to bring point back on frame, point is centered instead.\n\
14652 If this is zero, point is always centered after it moves off frame.\n\
14653 If you want scrolling to always be a line at a time, you should set\n\
14654 `scroll-conservatively' to a large value rather than set this to 1.");
14655
14656 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14657 "*Scroll up to this many lines, to bring point back on screen.\n\
14658 A value of zero means to scroll the text to center point vertically\n\
14659 in the window.");
14660 scroll_conservatively = 0;
14661
14662 DEFVAR_INT ("scroll-margin", &scroll_margin,
14663 "*Number of lines of margin at the top and bottom of a window.\n\
14664 Recenter the window whenever point gets within this many lines\n\
14665 of the top or bottom of the window.");
14666 scroll_margin = 0;
14667
14668 #if GLYPH_DEBUG
14669 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
14670 #endif
14671
14672 DEFVAR_BOOL ("truncate-partial-width-windows",
14673 &truncate_partial_width_windows,
14674 "*Non-nil means truncate lines in all windows less than full frame wide.");
14675 truncate_partial_width_windows = 1;
14676
14677 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14678 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14679 Any other value means to use the appropriate face, `mode-line',\n\
14680 `header-line', or `menu' respectively.\n\
14681 \n\
14682 This variable is deprecated; please change the above faces instead.");
14683 mode_line_inverse_video = 1;
14684
14685 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14686 "*Maximum buffer size for which line number should be displayed.\n\
14687 If the buffer is bigger than this, the line number does not appear\n\
14688 in the mode line. A value of nil means no limit.");
14689 Vline_number_display_limit = Qnil;
14690
14691 DEFVAR_INT ("line-number-display-limit-width",
14692 &line_number_display_limit_width,
14693 "*Maximum line width (in characters) for line number display.\n\
14694 If the average length of the lines near point is bigger than this, then the\n\
14695 line number may be omitted from the mode line.");
14696 line_number_display_limit_width = 200;
14697
14698 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14699 "*Non-nil means highlight region even in nonselected windows.");
14700 highlight_nonselected_windows = 0;
14701
14702 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14703 "Non-nil if more than one frame is visible on this display.\n\
14704 Minibuffer-only frames don't count, but iconified frames do.\n\
14705 This variable is not guaranteed to be accurate except while processing\n\
14706 `frame-title-format' and `icon-title-format'.");
14707
14708 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14709 "Template for displaying the title bar of visible frames.\n\
14710 \(Assuming the window manager supports this feature.)\n\
14711 This variable has the same structure as `mode-line-format' (which see),\n\
14712 and is used only on frames for which no explicit name has been set\n\
14713 \(see `modify-frame-parameters').");
14714 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14715 "Template for displaying the title bar of an iconified frame.\n\
14716 \(Assuming the window manager supports this feature.)\n\
14717 This variable has the same structure as `mode-line-format' (which see),\n\
14718 and is used only on frames for which no explicit name has been set\n\
14719 \(see `modify-frame-parameters').");
14720 Vicon_title_format
14721 = Vframe_title_format
14722 = Fcons (intern ("multiple-frames"),
14723 Fcons (build_string ("%b"),
14724 Fcons (Fcons (build_string (""),
14725 Fcons (intern ("invocation-name"),
14726 Fcons (build_string ("@"),
14727 Fcons (intern ("system-name"),
14728 Qnil)))),
14729 Qnil)));
14730
14731 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14732 "Maximum number of lines to keep in the message log buffer.\n\
14733 If nil, disable message logging. If t, log messages but don't truncate\n\
14734 the buffer when it becomes large.");
14735 Vmessage_log_max = make_number (50);
14736
14737 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14738 "Functions called before redisplay, if window sizes have changed.\n\
14739 The value should be a list of functions that take one argument.\n\
14740 Just before redisplay, for each frame, if any of its windows have changed\n\
14741 size since the last redisplay, or have been split or deleted,\n\
14742 all the functions in the list are called, with the frame as argument.");
14743 Vwindow_size_change_functions = Qnil;
14744
14745 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14746 "List of Functions to call before redisplaying a window with scrolling.\n\
14747 Each function is called with two arguments, the window\n\
14748 and its new display-start position. Note that the value of `window-end'\n\
14749 is not valid when these functions are called.");
14750 Vwindow_scroll_functions = Qnil;
14751
14752 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14753 "*Non-nil means automatically resize tool-bars.\n\
14754 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14755 It decreases a tool-bar's height when it would display blank lines\n\
14756 otherwise.");
14757 auto_resize_tool_bars_p = 1;
14758
14759 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14760 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14761 auto_raise_tool_bar_buttons_p = 1;
14762
14763 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14764 "*Margin around tool-bar buttons in pixels.\n\
14765 If an integer, use that for both horizontal and vertical margins.\n\
14766 Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14767 HORZ specifying the horizontal margin, and VERT specifying the\n\
14768 vertical margin.");
14769 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14770
14771 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14772 "Relief thickness of tool-bar buttons.");
14773 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14774
14775 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14776 "List of functions to call to fontify regions of text.\n\
14777 Each function is called with one argument POS. Functions must\n\
14778 fontify a region starting at POS in the current buffer, and give\n\
14779 fontified regions the property `fontified'.\n\
14780 This variable automatically becomes buffer-local when set.");
14781 Vfontification_functions = Qnil;
14782 Fmake_variable_buffer_local (Qfontification_functions);
14783
14784 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14785 &unibyte_display_via_language_environment,
14786 "*Non-nil means display unibyte text according to language environment.\n\
14787 Specifically this means that unibyte non-ASCII characters\n\
14788 are displayed by converting them to the equivalent multibyte characters\n\
14789 according to the current language environment. As a result, they are\n\
14790 displayed according to the current fontset.");
14791 unibyte_display_via_language_environment = 0;
14792
14793 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14794 "*Maximum height for resizing mini-windows.\n\
14795 If a float, it specifies a fraction of the mini-window frame's height.\n\
14796 If an integer, it specifies a number of lines.");
14797 Vmax_mini_window_height = make_float (0.25);
14798
14799 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14800 "*How to resize mini-windows.\n\
14801 A value of nil means don't automatically resize mini-windows.\n\
14802 A value of t means resize them to fit the text displayed in them.\n\
14803 A value of `grow-only', the default, means let mini-windows grow\n\
14804 only, until their display becomes empty, at which point the windows\n\
14805 go back to their normal size.");
14806 Vresize_mini_windows = Qgrow_only;
14807
14808 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14809 &cursor_in_non_selected_windows,
14810 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14811 Nil means don't display a cursor there.");
14812 cursor_in_non_selected_windows = 1;
14813
14814 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14815 "*Non-nil means scroll the display automatically to make point visible.");
14816 automatic_hscrolling_p = 1;
14817
14818 DEFVAR_LISP ("image-types", &Vimage_types,
14819 "List of supported image types.\n\
14820 Each element of the list is a symbol for a supported image type.");
14821 Vimage_types = Qnil;
14822
14823 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14824 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14825 Bind this around calls to `message' to let it take effect.");
14826 message_truncate_lines = 0;
14827
14828 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14829 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14830 Can be used to update submenus whose contents should vary.");
14831 Vmenu_bar_update_hook = Qnil;
14832
14833 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14834 "Non-nil means don't update menu bars. Internal use only.");
14835 inhibit_menubar_update = 0;
14836
14837 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14838 "Non-nil means don't eval Lisp during redisplay.");
14839 inhibit_eval_during_redisplay = 0;
14840 }
14841
14842
14843 /* Initialize this module when Emacs starts. */
14844
14845 void
14846 init_xdisp ()
14847 {
14848 Lisp_Object root_window;
14849 struct window *mini_w;
14850
14851 current_header_line_height = current_mode_line_height = -1;
14852
14853 CHARPOS (this_line_start_pos) = 0;
14854
14855 mini_w = XWINDOW (minibuf_window);
14856 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14857
14858 if (!noninteractive)
14859 {
14860 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14861 int i;
14862
14863 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14864 set_window_height (root_window,
14865 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14866 0);
14867 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14868 set_window_height (minibuf_window, 1, 0);
14869
14870 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14871 mini_w->width = make_number (FRAME_WIDTH (f));
14872
14873 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14874 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14875 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14876
14877 /* The default ellipsis glyphs `...'. */
14878 for (i = 0; i < 3; ++i)
14879 default_invis_vector[i] = make_number ('.');
14880 }
14881
14882 #ifdef HAVE_WINDOW_SYSTEM
14883 {
14884 /* Allocate the buffer for frame titles. */
14885 int size = 100;
14886 frame_title_buf = (char *) xmalloc (size);
14887 frame_title_buf_end = frame_title_buf + size;
14888 frame_title_ptr = NULL;
14889 }
14890 #endif /* HAVE_WINDOW_SYSTEM */
14891
14892 help_echo_showing_p = 0;
14893 }
14894
14895