e11347dca90e77cbdc850ef821f2b5ae01112573
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191
192 #ifdef HAVE_X_WINDOWS
193 #include "xterm.h"
194 #endif
195 #ifdef WINDOWSNT
196 #include "w32term.h"
197 #endif
198 #ifdef macintosh
199 #include "macterm.h"
200 #endif
201
202 #define INFINITY 10000000
203
204 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
208
209 extern int interrupt_input;
210 extern int command_loop_level;
211
212 extern int minibuffer_auto_raise;
213
214 extern Lisp_Object Qface;
215
216 extern Lisp_Object Voverriding_local_map;
217 extern Lisp_Object Voverriding_local_map_menu_flag;
218 extern Lisp_Object Qmenu_item;
219
220 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
221 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
222 Lisp_Object Qredisplay_end_trigger_functions;
223 Lisp_Object Qinhibit_point_motion_hooks;
224 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
225 Lisp_Object Qfontified;
226 Lisp_Object Qgrow_only;
227 Lisp_Object Qinhibit_eval_during_redisplay;
228 Lisp_Object Qbuffer_position, Qposition, Qobject;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 Lisp_Object Vtool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
258
259 int inhibit_eval_during_redisplay;
260
261 /* Names of text properties relevant for redisplay. */
262
263 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
264 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
265
266 /* Symbols used in text property values. */
267
268 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
269 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
270 Lisp_Object Qmargin;
271 extern Lisp_Object Qheight;
272
273 /* Non-nil means highlight trailing whitespace. */
274
275 Lisp_Object Vshow_trailing_whitespace;
276
277 /* Name of the face used to highlight trailing whitespace. */
278
279 Lisp_Object Qtrailing_whitespace;
280
281 /* The symbol `image' which is the car of the lists used to represent
282 images in Lisp. */
283
284 Lisp_Object Qimage;
285
286 /* Non-zero means print newline to stdout before next mini-buffer
287 message. */
288
289 int noninteractive_need_newline;
290
291 /* Non-zero means print newline to message log before next message. */
292
293 static int message_log_need_newline;
294
295 \f
296 /* The buffer position of the first character appearing entirely or
297 partially on the line of the selected window which contains the
298 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
299 redisplay optimization in redisplay_internal. */
300
301 static struct text_pos this_line_start_pos;
302
303 /* Number of characters past the end of the line above, including the
304 terminating newline. */
305
306 static struct text_pos this_line_end_pos;
307
308 /* The vertical positions and the height of this line. */
309
310 static int this_line_vpos;
311 static int this_line_y;
312 static int this_line_pixel_height;
313
314 /* X position at which this display line starts. Usually zero;
315 negative if first character is partially visible. */
316
317 static int this_line_start_x;
318
319 /* Buffer that this_line_.* variables are referring to. */
320
321 static struct buffer *this_line_buffer;
322
323 /* Nonzero means truncate lines in all windows less wide than the
324 frame. */
325
326 int truncate_partial_width_windows;
327
328 /* A flag to control how to display unibyte 8-bit character. */
329
330 int unibyte_display_via_language_environment;
331
332 /* Nonzero means we have more than one non-mini-buffer-only frame.
333 Not guaranteed to be accurate except while parsing
334 frame-title-format. */
335
336 int multiple_frames;
337
338 Lisp_Object Vglobal_mode_string;
339
340 /* Marker for where to display an arrow on top of the buffer text. */
341
342 Lisp_Object Voverlay_arrow_position;
343
344 /* String to display for the arrow. Only used on terminal frames. */
345
346 Lisp_Object Voverlay_arrow_string;
347
348 /* Values of those variables at last redisplay. However, if
349 Voverlay_arrow_position is a marker, last_arrow_position is its
350 numerical position. */
351
352 static Lisp_Object last_arrow_position, last_arrow_string;
353
354 /* Like mode-line-format, but for the title bar on a visible frame. */
355
356 Lisp_Object Vframe_title_format;
357
358 /* Like mode-line-format, but for the title bar on an iconified frame. */
359
360 Lisp_Object Vicon_title_format;
361
362 /* List of functions to call when a window's size changes. These
363 functions get one arg, a frame on which one or more windows' sizes
364 have changed. */
365
366 static Lisp_Object Vwindow_size_change_functions;
367
368 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
369
370 /* Nonzero if overlay arrow has been displayed once in this window. */
371
372 static int overlay_arrow_seen;
373
374 /* Nonzero means highlight the region even in nonselected windows. */
375
376 int highlight_nonselected_windows;
377
378 /* If cursor motion alone moves point off frame, try scrolling this
379 many lines up or down if that will bring it back. */
380
381 static int scroll_step;
382
383 /* Non-0 means scroll just far enough to bring point back on the
384 screen, when appropriate. */
385
386 static int scroll_conservatively;
387
388 /* Recenter the window whenever point gets within this many lines of
389 the top or bottom of the window. This value is translated into a
390 pixel value by multiplying it with CANON_Y_UNIT, which means that
391 there is really a fixed pixel height scroll margin. */
392
393 int scroll_margin;
394
395 /* Number of windows showing the buffer of the selected window (or
396 another buffer with the same base buffer). keyboard.c refers to
397 this. */
398
399 int buffer_shared;
400
401 /* Vector containing glyphs for an ellipsis `...'. */
402
403 static Lisp_Object default_invis_vector[3];
404
405 /* Zero means display the mode-line/header-line/menu-bar in the default face
406 (this slightly odd definition is for compatibility with previous versions
407 of emacs), non-zero means display them using their respective faces.
408
409 This variable is deprecated. */
410
411 int mode_line_inverse_video;
412
413 /* Prompt to display in front of the mini-buffer contents. */
414
415 Lisp_Object minibuf_prompt;
416
417 /* Width of current mini-buffer prompt. Only set after display_line
418 of the line that contains the prompt. */
419
420 int minibuf_prompt_width;
421 int minibuf_prompt_pixel_width;
422
423 /* This is the window where the echo area message was displayed. It
424 is always a mini-buffer window, but it may not be the same window
425 currently active as a mini-buffer. */
426
427 Lisp_Object echo_area_window;
428
429 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
430 pushes the current message and the value of
431 message_enable_multibyte on the stack, the function restore_message
432 pops the stack and displays MESSAGE again. */
433
434 Lisp_Object Vmessage_stack;
435
436 /* Nonzero means multibyte characters were enabled when the echo area
437 message was specified. */
438
439 int message_enable_multibyte;
440
441 /* True if we should redraw the mode lines on the next redisplay. */
442
443 int update_mode_lines;
444
445 /* Nonzero if window sizes or contents have changed since last
446 redisplay that finished */
447
448 int windows_or_buffers_changed;
449
450 /* Nonzero after display_mode_line if %l was used and it displayed a
451 line number. */
452
453 int line_number_displayed;
454
455 /* Maximum buffer size for which to display line numbers. */
456
457 Lisp_Object Vline_number_display_limit;
458
459 /* line width to consider when repostioning for line number display */
460
461 static int line_number_display_limit_width;
462
463 /* Number of lines to keep in the message log buffer. t means
464 infinite. nil means don't log at all. */
465
466 Lisp_Object Vmessage_log_max;
467
468 /* The name of the *Messages* buffer, a string. */
469
470 static Lisp_Object Vmessages_buffer_name;
471
472 /* Current, index 0, and last displayed echo area message. Either
473 buffers from echo_buffers, or nil to indicate no message. */
474
475 Lisp_Object echo_area_buffer[2];
476
477 /* The buffers referenced from echo_area_buffer. */
478
479 static Lisp_Object echo_buffer[2];
480
481 /* A vector saved used in with_area_buffer to reduce consing. */
482
483 static Lisp_Object Vwith_echo_area_save_vector;
484
485 /* Non-zero means display_echo_area should display the last echo area
486 message again. Set by redisplay_preserve_echo_area. */
487
488 static int display_last_displayed_message_p;
489
490 /* Nonzero if echo area is being used by print; zero if being used by
491 message. */
492
493 int message_buf_print;
494
495 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
496
497 Lisp_Object Qinhibit_menubar_update;
498 int inhibit_menubar_update;
499
500 /* Maximum height for resizing mini-windows. Either a float
501 specifying a fraction of the available height, or an integer
502 specifying a number of lines. */
503
504 Lisp_Object Vmax_mini_window_height;
505
506 /* Non-zero means messages should be displayed with truncated
507 lines instead of being continued. */
508
509 int message_truncate_lines;
510 Lisp_Object Qmessage_truncate_lines;
511
512 /* Set to 1 in clear_message to make redisplay_internal aware
513 of an emptied echo area. */
514
515 static int message_cleared_p;
516
517 /* Non-zero means we want a hollow cursor in windows that are not
518 selected. Zero means there's no cursor in such windows. */
519
520 int cursor_in_non_selected_windows;
521
522 /* A scratch glyph row with contents used for generating truncation
523 glyphs. Also used in direct_output_for_insert. */
524
525 #define MAX_SCRATCH_GLYPHS 100
526 struct glyph_row scratch_glyph_row;
527 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
528
529 /* Ascent and height of the last line processed by move_it_to. */
530
531 static int last_max_ascent, last_height;
532
533 /* Non-zero if there's a help-echo in the echo area. */
534
535 int help_echo_showing_p;
536
537 /* If >= 0, computed, exact values of mode-line and header-line height
538 to use in the macros CURRENT_MODE_LINE_HEIGHT and
539 CURRENT_HEADER_LINE_HEIGHT. */
540
541 int current_mode_line_height, current_header_line_height;
542
543 /* The maximum distance to look ahead for text properties. Values
544 that are too small let us call compute_char_face and similar
545 functions too often which is expensive. Values that are too large
546 let us call compute_char_face and alike too often because we
547 might not be interested in text properties that far away. */
548
549 #define TEXT_PROP_DISTANCE_LIMIT 100
550
551 #if GLYPH_DEBUG
552
553 /* Non-zero means print traces of redisplay if compiled with
554 GLYPH_DEBUG != 0. */
555
556 int trace_redisplay_p;
557
558 #endif /* GLYPH_DEBUG */
559
560 #ifdef DEBUG_TRACE_MOVE
561 /* Non-zero means trace with TRACE_MOVE to stderr. */
562 int trace_move;
563
564 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
565 #else
566 #define TRACE_MOVE(x) (void) 0
567 #endif
568
569 /* Non-zero means automatically scroll windows horizontally to make
570 point visible. */
571
572 int automatic_hscrolling_p;
573
574 /* A list of symbols, one for each supported image type. */
575
576 Lisp_Object Vimage_types;
577
578 /* The variable `resize-mini-windows'. If nil, don't resize
579 mini-windows. If t, always resize them to fit the text they
580 display. If `grow-only', let mini-windows grow only until they
581 become empty. */
582
583 Lisp_Object Vresize_mini_windows;
584
585 /* Value returned from text property handlers (see below). */
586
587 enum prop_handled
588 {
589 HANDLED_NORMALLY,
590 HANDLED_RECOMPUTE_PROPS,
591 HANDLED_OVERLAY_STRING_CONSUMED,
592 HANDLED_RETURN
593 };
594
595 /* A description of text properties that redisplay is interested
596 in. */
597
598 struct props
599 {
600 /* The name of the property. */
601 Lisp_Object *name;
602
603 /* A unique index for the property. */
604 enum prop_idx idx;
605
606 /* A handler function called to set up iterator IT from the property
607 at IT's current position. Value is used to steer handle_stop. */
608 enum prop_handled (*handler) P_ ((struct it *it));
609 };
610
611 static enum prop_handled handle_face_prop P_ ((struct it *));
612 static enum prop_handled handle_invisible_prop P_ ((struct it *));
613 static enum prop_handled handle_display_prop P_ ((struct it *));
614 static enum prop_handled handle_composition_prop P_ ((struct it *));
615 static enum prop_handled handle_overlay_change P_ ((struct it *));
616 static enum prop_handled handle_fontified_prop P_ ((struct it *));
617
618 /* Properties handled by iterators. */
619
620 static struct props it_props[] =
621 {
622 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
623 /* Handle `face' before `display' because some sub-properties of
624 `display' need to know the face. */
625 {&Qface, FACE_PROP_IDX, handle_face_prop},
626 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
627 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
628 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
629 {NULL, 0, NULL}
630 };
631
632 /* Value is the position described by X. If X is a marker, value is
633 the marker_position of X. Otherwise, value is X. */
634
635 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
636
637 /* Enumeration returned by some move_it_.* functions internally. */
638
639 enum move_it_result
640 {
641 /* Not used. Undefined value. */
642 MOVE_UNDEFINED,
643
644 /* Move ended at the requested buffer position or ZV. */
645 MOVE_POS_MATCH_OR_ZV,
646
647 /* Move ended at the requested X pixel position. */
648 MOVE_X_REACHED,
649
650 /* Move within a line ended at the end of a line that must be
651 continued. */
652 MOVE_LINE_CONTINUED,
653
654 /* Move within a line ended at the end of a line that would
655 be displayed truncated. */
656 MOVE_LINE_TRUNCATED,
657
658 /* Move within a line ended at a line end. */
659 MOVE_NEWLINE_OR_CR
660 };
661
662
663 \f
664 /* Function prototypes. */
665
666 static void setup_for_ellipsis P_ ((struct it *));
667 static void mark_window_display_accurate_1 P_ ((struct window *, int));
668 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
669 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
670 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
671 static int redisplay_mode_lines P_ ((Lisp_Object, int));
672 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
673 static int invisible_text_between_p P_ ((struct it *, int, int));
674 static int next_element_from_ellipsis P_ ((struct it *));
675 static void pint2str P_ ((char *, int, int));
676 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
677 struct text_pos));
678 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
679 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
680 static void store_frame_title_char P_ ((char));
681 static int store_frame_title P_ ((unsigned char *, int, int));
682 static void x_consider_frame_title P_ ((Lisp_Object));
683 static void handle_stop P_ ((struct it *));
684 static int tool_bar_lines_needed P_ ((struct frame *));
685 static int single_display_prop_intangible_p P_ ((Lisp_Object));
686 static void ensure_echo_area_buffers P_ ((void));
687 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
688 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
689 static int with_echo_area_buffer P_ ((struct window *, int,
690 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
691 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
692 static void clear_garbaged_frames P_ ((void));
693 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
694 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
695 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
696 static int display_echo_area P_ ((struct window *));
697 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
698 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
699 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
700 static int string_char_and_length P_ ((unsigned char *, int, int *));
701 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
702 struct text_pos));
703 static int compute_window_start_on_continuation_line P_ ((struct window *));
704 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
705 static void insert_left_trunc_glyphs P_ ((struct it *));
706 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
707 static void extend_face_to_end_of_line P_ ((struct it *));
708 static int append_space P_ ((struct it *, int));
709 static int make_cursor_line_fully_visible P_ ((struct window *));
710 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
711 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
712 static int trailing_whitespace_p P_ ((int));
713 static int message_log_check_duplicate P_ ((int, int, int, int));
714 static void push_it P_ ((struct it *));
715 static void pop_it P_ ((struct it *));
716 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
717 static void redisplay_internal P_ ((int));
718 static int echo_area_display P_ ((int));
719 static void redisplay_windows P_ ((Lisp_Object));
720 static void redisplay_window P_ ((Lisp_Object, int));
721 static void update_menu_bar P_ ((struct frame *, int));
722 static int try_window_reusing_current_matrix P_ ((struct window *));
723 static int try_window_id P_ ((struct window *));
724 static int display_line P_ ((struct it *));
725 static int display_mode_lines P_ ((struct window *));
726 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
727 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
728 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
729 static void display_menu_bar P_ ((struct window *));
730 static int display_count_lines P_ ((int, int, int, int, int *));
731 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
732 int, int, struct it *, int, int, int, int));
733 static void compute_line_metrics P_ ((struct it *));
734 static void run_redisplay_end_trigger_hook P_ ((struct it *));
735 static int get_overlay_strings P_ ((struct it *, int));
736 static void next_overlay_string P_ ((struct it *));
737 static void reseat P_ ((struct it *, struct text_pos, int));
738 static void reseat_1 P_ ((struct it *, struct text_pos, int));
739 static void back_to_previous_visible_line_start P_ ((struct it *));
740 static void reseat_at_previous_visible_line_start P_ ((struct it *));
741 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
742 static int next_element_from_display_vector P_ ((struct it *));
743 static int next_element_from_string P_ ((struct it *));
744 static int next_element_from_c_string P_ ((struct it *));
745 static int next_element_from_buffer P_ ((struct it *));
746 static int next_element_from_composition P_ ((struct it *));
747 static int next_element_from_image P_ ((struct it *));
748 static int next_element_from_stretch P_ ((struct it *));
749 static void load_overlay_strings P_ ((struct it *, int));
750 static int init_from_display_pos P_ ((struct it *, struct window *,
751 struct display_pos *));
752 static void reseat_to_string P_ ((struct it *, unsigned char *,
753 Lisp_Object, int, int, int, int));
754 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
755 int, int, int));
756 void move_it_vertically_backward P_ ((struct it *, int));
757 static void init_to_row_start P_ ((struct it *, struct window *,
758 struct glyph_row *));
759 static int init_to_row_end P_ ((struct it *, struct window *,
760 struct glyph_row *));
761 static void back_to_previous_line_start P_ ((struct it *));
762 static int forward_to_next_line_start P_ ((struct it *, int *));
763 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
764 Lisp_Object, int));
765 static struct text_pos string_pos P_ ((int, Lisp_Object));
766 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
767 static int number_of_chars P_ ((unsigned char *, int));
768 static void compute_stop_pos P_ ((struct it *));
769 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
770 Lisp_Object));
771 static int face_before_or_after_it_pos P_ ((struct it *, int));
772 static int next_overlay_change P_ ((int));
773 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
774 Lisp_Object, struct text_pos *,
775 int));
776 static int underlying_face_id P_ ((struct it *));
777 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
778 struct window *));
779
780 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
781 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
782
783 #ifdef HAVE_WINDOW_SYSTEM
784
785 static void update_tool_bar P_ ((struct frame *, int));
786 static void build_desired_tool_bar_string P_ ((struct frame *f));
787 static int redisplay_tool_bar P_ ((struct frame *));
788 static void display_tool_bar_line P_ ((struct it *));
789
790 #endif /* HAVE_WINDOW_SYSTEM */
791
792 \f
793 /***********************************************************************
794 Window display dimensions
795 ***********************************************************************/
796
797 /* Return the window-relative maximum y + 1 for glyph rows displaying
798 text in window W. This is the height of W minus the height of a
799 mode line, if any. */
800
801 INLINE int
802 window_text_bottom_y (w)
803 struct window *w;
804 {
805 struct frame *f = XFRAME (w->frame);
806 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
807
808 if (WINDOW_WANTS_MODELINE_P (w))
809 height -= CURRENT_MODE_LINE_HEIGHT (w);
810 return height;
811 }
812
813
814 /* Return the pixel width of display area AREA of window W. AREA < 0
815 means return the total width of W, not including bitmap areas to
816 the left and right of the window. */
817
818 INLINE int
819 window_box_width (w, area)
820 struct window *w;
821 int area;
822 {
823 struct frame *f = XFRAME (w->frame);
824 int width = XFASTINT (w->width);
825
826 if (!w->pseudo_window_p)
827 {
828 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
829
830 if (area == TEXT_AREA)
831 {
832 if (INTEGERP (w->left_margin_width))
833 width -= XFASTINT (w->left_margin_width);
834 if (INTEGERP (w->right_margin_width))
835 width -= XFASTINT (w->right_margin_width);
836 }
837 else if (area == LEFT_MARGIN_AREA)
838 width = (INTEGERP (w->left_margin_width)
839 ? XFASTINT (w->left_margin_width) : 0);
840 else if (area == RIGHT_MARGIN_AREA)
841 width = (INTEGERP (w->right_margin_width)
842 ? XFASTINT (w->right_margin_width) : 0);
843 }
844
845 return width * CANON_X_UNIT (f);
846 }
847
848
849 /* Return the pixel height of the display area of window W, not
850 including mode lines of W, if any.. */
851
852 INLINE int
853 window_box_height (w)
854 struct window *w;
855 {
856 struct frame *f = XFRAME (w->frame);
857 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
858
859 xassert (height >= 0);
860
861 /* Note: the code below that determines the mode-line/header-line
862 height is essentially the same as that contained in the macro
863 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
864 the appropriate glyph row has its `mode_line_p' flag set,
865 and if it doesn't, uses estimate_mode_line_height instead. */
866
867 if (WINDOW_WANTS_MODELINE_P (w))
868 {
869 struct glyph_row *ml_row
870 = (w->current_matrix && w->current_matrix->rows
871 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
872 : 0);
873 if (ml_row && ml_row->mode_line_p)
874 height -= ml_row->height;
875 else
876 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
877 }
878
879 if (WINDOW_WANTS_HEADER_LINE_P (w))
880 {
881 struct glyph_row *hl_row
882 = (w->current_matrix && w->current_matrix->rows
883 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
884 : 0);
885 if (hl_row && hl_row->mode_line_p)
886 height -= hl_row->height;
887 else
888 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
889 }
890
891 return height;
892 }
893
894
895 /* Return the frame-relative coordinate of the left edge of display
896 area AREA of window W. AREA < 0 means return the left edge of the
897 whole window, to the right of any bitmap area at the left side of
898 W. */
899
900 INLINE int
901 window_box_left (w, area)
902 struct window *w;
903 int area;
904 {
905 struct frame *f = XFRAME (w->frame);
906 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
907
908 if (!w->pseudo_window_p)
909 {
910 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
911 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
912
913 if (area == TEXT_AREA)
914 x += window_box_width (w, LEFT_MARGIN_AREA);
915 else if (area == RIGHT_MARGIN_AREA)
916 x += (window_box_width (w, LEFT_MARGIN_AREA)
917 + window_box_width (w, TEXT_AREA));
918 }
919
920 return x;
921 }
922
923
924 /* Return the frame-relative coordinate of the right edge of display
925 area AREA of window W. AREA < 0 means return the left edge of the
926 whole window, to the left of any bitmap area at the right side of
927 W. */
928
929 INLINE int
930 window_box_right (w, area)
931 struct window *w;
932 int area;
933 {
934 return window_box_left (w, area) + window_box_width (w, area);
935 }
936
937
938 /* Get the bounding box of the display area AREA of window W, without
939 mode lines, in frame-relative coordinates. AREA < 0 means the
940 whole window, not including bitmap areas to the left and right of
941 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
942 coordinates of the upper-left corner of the box. Return in
943 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
944
945 INLINE void
946 window_box (w, area, box_x, box_y, box_width, box_height)
947 struct window *w;
948 int area;
949 int *box_x, *box_y, *box_width, *box_height;
950 {
951 struct frame *f = XFRAME (w->frame);
952
953 *box_width = window_box_width (w, area);
954 *box_height = window_box_height (w);
955 *box_x = window_box_left (w, area);
956 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
957 + XFASTINT (w->top) * CANON_Y_UNIT (f));
958 if (WINDOW_WANTS_HEADER_LINE_P (w))
959 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
960 }
961
962
963 /* Get the bounding box of the display area AREA of window W, without
964 mode lines. AREA < 0 means the whole window, not including bitmap
965 areas to the left and right of the window. Return in *TOP_LEFT_X
966 and TOP_LEFT_Y the frame-relative pixel coordinates of the
967 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
968 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
969 box. */
970
971 INLINE void
972 window_box_edges (w, area, top_left_x, top_left_y,
973 bottom_right_x, bottom_right_y)
974 struct window *w;
975 int area;
976 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
977 {
978 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
979 bottom_right_y);
980 *bottom_right_x += *top_left_x;
981 *bottom_right_y += *top_left_y;
982 }
983
984
985 \f
986 /***********************************************************************
987 Utilities
988 ***********************************************************************/
989
990 /* Return the bottom y-position of the line the iterator IT is in.
991 This can modify IT's settings. */
992
993 int
994 line_bottom_y (it)
995 struct it *it;
996 {
997 int line_height = it->max_ascent + it->max_descent;
998 int line_top_y = it->current_y;
999
1000 if (line_height == 0)
1001 {
1002 if (last_height)
1003 line_height = last_height;
1004 else if (IT_CHARPOS (*it) < ZV)
1005 {
1006 move_it_by_lines (it, 1, 1);
1007 line_height = (it->max_ascent || it->max_descent
1008 ? it->max_ascent + it->max_descent
1009 : last_height);
1010 }
1011 else
1012 {
1013 struct glyph_row *row = it->glyph_row;
1014
1015 /* Use the default character height. */
1016 it->glyph_row = NULL;
1017 it->what = IT_CHARACTER;
1018 it->c = ' ';
1019 it->len = 1;
1020 PRODUCE_GLYPHS (it);
1021 line_height = it->ascent + it->descent;
1022 it->glyph_row = row;
1023 }
1024 }
1025
1026 return line_top_y + line_height;
1027 }
1028
1029
1030 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1031 1 if POS is visible and the line containing POS is fully visible.
1032 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1033 and header-lines heights. */
1034
1035 int
1036 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1037 struct window *w;
1038 int charpos, *fully, exact_mode_line_heights_p;
1039 {
1040 struct it it;
1041 struct text_pos top;
1042 int visible_p;
1043 struct buffer *old_buffer = NULL;
1044
1045 if (XBUFFER (w->buffer) != current_buffer)
1046 {
1047 old_buffer = current_buffer;
1048 set_buffer_internal_1 (XBUFFER (w->buffer));
1049 }
1050
1051 *fully = visible_p = 0;
1052 SET_TEXT_POS_FROM_MARKER (top, w->start);
1053
1054 /* Compute exact mode line heights, if requested. */
1055 if (exact_mode_line_heights_p)
1056 {
1057 if (WINDOW_WANTS_MODELINE_P (w))
1058 current_mode_line_height
1059 = display_mode_line (w, MODE_LINE_FACE_ID,
1060 current_buffer->mode_line_format);
1061
1062 if (WINDOW_WANTS_HEADER_LINE_P (w))
1063 current_header_line_height
1064 = display_mode_line (w, HEADER_LINE_FACE_ID,
1065 current_buffer->header_line_format);
1066 }
1067
1068 start_display (&it, w, top);
1069 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1070 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1071
1072 /* Note that we may overshoot because of invisible text. */
1073 if (IT_CHARPOS (it) >= charpos)
1074 {
1075 int top_y = it.current_y;
1076 int bottom_y = line_bottom_y (&it);
1077 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1078
1079 if (top_y < window_top_y)
1080 visible_p = bottom_y > window_top_y;
1081 else if (top_y < it.last_visible_y)
1082 {
1083 visible_p = 1;
1084 *fully = bottom_y <= it.last_visible_y;
1085 }
1086 }
1087 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1088 {
1089 move_it_by_lines (&it, 1, 0);
1090 if (charpos < IT_CHARPOS (it))
1091 {
1092 visible_p = 1;
1093 *fully = 0;
1094 }
1095 }
1096
1097 if (old_buffer)
1098 set_buffer_internal_1 (old_buffer);
1099
1100 current_header_line_height = current_mode_line_height = -1;
1101 return visible_p;
1102 }
1103
1104
1105 /* Return the next character from STR which is MAXLEN bytes long.
1106 Return in *LEN the length of the character. This is like
1107 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1108 we find one, we return a `?', but with the length of the invalid
1109 character. */
1110
1111 static INLINE int
1112 string_char_and_length (str, maxlen, len)
1113 unsigned char *str;
1114 int maxlen, *len;
1115 {
1116 int c;
1117
1118 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1119 if (!CHAR_VALID_P (c, 1))
1120 /* We may not change the length here because other places in Emacs
1121 don't use this function, i.e. they silently accept invalid
1122 characters. */
1123 c = '?';
1124
1125 return c;
1126 }
1127
1128
1129
1130 /* Given a position POS containing a valid character and byte position
1131 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1132
1133 static struct text_pos
1134 string_pos_nchars_ahead (pos, string, nchars)
1135 struct text_pos pos;
1136 Lisp_Object string;
1137 int nchars;
1138 {
1139 xassert (STRINGP (string) && nchars >= 0);
1140
1141 if (STRING_MULTIBYTE (string))
1142 {
1143 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1144 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1145 int len;
1146
1147 while (nchars--)
1148 {
1149 string_char_and_length (p, rest, &len);
1150 p += len, rest -= len;
1151 xassert (rest >= 0);
1152 CHARPOS (pos) += 1;
1153 BYTEPOS (pos) += len;
1154 }
1155 }
1156 else
1157 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1158
1159 return pos;
1160 }
1161
1162
1163 /* Value is the text position, i.e. character and byte position,
1164 for character position CHARPOS in STRING. */
1165
1166 static INLINE struct text_pos
1167 string_pos (charpos, string)
1168 int charpos;
1169 Lisp_Object string;
1170 {
1171 struct text_pos pos;
1172 xassert (STRINGP (string));
1173 xassert (charpos >= 0);
1174 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1175 return pos;
1176 }
1177
1178
1179 /* Value is a text position, i.e. character and byte position, for
1180 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1181 means recognize multibyte characters. */
1182
1183 static struct text_pos
1184 c_string_pos (charpos, s, multibyte_p)
1185 int charpos;
1186 unsigned char *s;
1187 int multibyte_p;
1188 {
1189 struct text_pos pos;
1190
1191 xassert (s != NULL);
1192 xassert (charpos >= 0);
1193
1194 if (multibyte_p)
1195 {
1196 int rest = strlen (s), len;
1197
1198 SET_TEXT_POS (pos, 0, 0);
1199 while (charpos--)
1200 {
1201 string_char_and_length (s, rest, &len);
1202 s += len, rest -= len;
1203 xassert (rest >= 0);
1204 CHARPOS (pos) += 1;
1205 BYTEPOS (pos) += len;
1206 }
1207 }
1208 else
1209 SET_TEXT_POS (pos, charpos, charpos);
1210
1211 return pos;
1212 }
1213
1214
1215 /* Value is the number of characters in C string S. MULTIBYTE_P
1216 non-zero means recognize multibyte characters. */
1217
1218 static int
1219 number_of_chars (s, multibyte_p)
1220 unsigned char *s;
1221 int multibyte_p;
1222 {
1223 int nchars;
1224
1225 if (multibyte_p)
1226 {
1227 int rest = strlen (s), len;
1228 unsigned char *p = (unsigned char *) s;
1229
1230 for (nchars = 0; rest > 0; ++nchars)
1231 {
1232 string_char_and_length (p, rest, &len);
1233 rest -= len, p += len;
1234 }
1235 }
1236 else
1237 nchars = strlen (s);
1238
1239 return nchars;
1240 }
1241
1242
1243 /* Compute byte position NEWPOS->bytepos corresponding to
1244 NEWPOS->charpos. POS is a known position in string STRING.
1245 NEWPOS->charpos must be >= POS.charpos. */
1246
1247 static void
1248 compute_string_pos (newpos, pos, string)
1249 struct text_pos *newpos, pos;
1250 Lisp_Object string;
1251 {
1252 xassert (STRINGP (string));
1253 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1254
1255 if (STRING_MULTIBYTE (string))
1256 *newpos = string_pos_nchars_ahead (pos, string,
1257 CHARPOS (*newpos) - CHARPOS (pos));
1258 else
1259 BYTEPOS (*newpos) = CHARPOS (*newpos);
1260 }
1261
1262
1263 \f
1264 /***********************************************************************
1265 Lisp form evaluation
1266 ***********************************************************************/
1267
1268 /* Error handler for safe_eval and safe_call. */
1269
1270 static Lisp_Object
1271 safe_eval_handler (arg)
1272 Lisp_Object arg;
1273 {
1274 add_to_log ("Error during redisplay: %s", arg, Qnil);
1275 return Qnil;
1276 }
1277
1278
1279 /* Evaluate SEXPR and return the result, or nil if something went
1280 wrong. Prevent redisplay during the evaluation. */
1281
1282 Lisp_Object
1283 safe_eval (sexpr)
1284 Lisp_Object sexpr;
1285 {
1286 Lisp_Object val;
1287
1288 if (inhibit_eval_during_redisplay)
1289 val = Qnil;
1290 else
1291 {
1292 int count = BINDING_STACK_SIZE ();
1293 struct gcpro gcpro1;
1294
1295 GCPRO1 (sexpr);
1296 specbind (Qinhibit_redisplay, Qt);
1297 val = internal_condition_case_1 (Feval, sexpr, Qerror,
1298 safe_eval_handler);
1299 UNGCPRO;
1300 val = unbind_to (count, val);
1301 }
1302
1303 return val;
1304 }
1305
1306
1307 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1308 Return the result, or nil if something went wrong. Prevent
1309 redisplay during the evaluation. */
1310
1311 Lisp_Object
1312 safe_call (nargs, args)
1313 int nargs;
1314 Lisp_Object *args;
1315 {
1316 Lisp_Object val;
1317
1318 if (inhibit_eval_during_redisplay)
1319 val = Qnil;
1320 else
1321 {
1322 int count = BINDING_STACK_SIZE ();
1323 struct gcpro gcpro1;
1324
1325 GCPRO1 (args[0]);
1326 gcpro1.nvars = nargs;
1327 specbind (Qinhibit_redisplay, Qt);
1328 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1329 safe_eval_handler);
1330 UNGCPRO;
1331 val = unbind_to (count, val);
1332 }
1333
1334 return val;
1335 }
1336
1337
1338 /* Call function FN with one argument ARG.
1339 Return the result, or nil if something went wrong. */
1340
1341 Lisp_Object
1342 safe_call1 (fn, arg)
1343 Lisp_Object fn, arg;
1344 {
1345 Lisp_Object args[2];
1346 args[0] = fn;
1347 args[1] = arg;
1348 return safe_call (2, args);
1349 }
1350
1351
1352 \f
1353 /***********************************************************************
1354 Debugging
1355 ***********************************************************************/
1356
1357 #if 0
1358
1359 /* Define CHECK_IT to perform sanity checks on iterators.
1360 This is for debugging. It is too slow to do unconditionally. */
1361
1362 static void
1363 check_it (it)
1364 struct it *it;
1365 {
1366 if (it->method == next_element_from_string)
1367 {
1368 xassert (STRINGP (it->string));
1369 xassert (IT_STRING_CHARPOS (*it) >= 0);
1370 }
1371 else if (it->method == next_element_from_buffer)
1372 {
1373 /* Check that character and byte positions agree. */
1374 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1375 }
1376
1377 if (it->dpvec)
1378 xassert (it->current.dpvec_index >= 0);
1379 else
1380 xassert (it->current.dpvec_index < 0);
1381 }
1382
1383 #define CHECK_IT(IT) check_it ((IT))
1384
1385 #else /* not 0 */
1386
1387 #define CHECK_IT(IT) (void) 0
1388
1389 #endif /* not 0 */
1390
1391
1392 #if GLYPH_DEBUG
1393
1394 /* Check that the window end of window W is what we expect it
1395 to be---the last row in the current matrix displaying text. */
1396
1397 static void
1398 check_window_end (w)
1399 struct window *w;
1400 {
1401 if (!MINI_WINDOW_P (w)
1402 && !NILP (w->window_end_valid))
1403 {
1404 struct glyph_row *row;
1405 xassert ((row = MATRIX_ROW (w->current_matrix,
1406 XFASTINT (w->window_end_vpos)),
1407 !row->enabled_p
1408 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1409 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1410 }
1411 }
1412
1413 #define CHECK_WINDOW_END(W) check_window_end ((W))
1414
1415 #else /* not GLYPH_DEBUG */
1416
1417 #define CHECK_WINDOW_END(W) (void) 0
1418
1419 #endif /* not GLYPH_DEBUG */
1420
1421
1422 \f
1423 /***********************************************************************
1424 Iterator initialization
1425 ***********************************************************************/
1426
1427 /* Initialize IT for displaying current_buffer in window W, starting
1428 at character position CHARPOS. CHARPOS < 0 means that no buffer
1429 position is specified which is useful when the iterator is assigned
1430 a position later. BYTEPOS is the byte position corresponding to
1431 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1432
1433 If ROW is not null, calls to produce_glyphs with IT as parameter
1434 will produce glyphs in that row.
1435
1436 BASE_FACE_ID is the id of a base face to use. It must be one of
1437 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1438 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1439 displaying the tool-bar.
1440
1441 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1442 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1443 corresponding mode line glyph row of the desired matrix of W. */
1444
1445 void
1446 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1447 struct it *it;
1448 struct window *w;
1449 int charpos, bytepos;
1450 struct glyph_row *row;
1451 enum face_id base_face_id;
1452 {
1453 int highlight_region_p;
1454
1455 /* Some precondition checks. */
1456 xassert (w != NULL && it != NULL);
1457 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1458 && charpos <= ZV));
1459
1460 /* If face attributes have been changed since the last redisplay,
1461 free realized faces now because they depend on face definitions
1462 that might have changed. */
1463 if (face_change_count)
1464 {
1465 face_change_count = 0;
1466 free_all_realized_faces (Qnil);
1467 }
1468
1469 /* Use one of the mode line rows of W's desired matrix if
1470 appropriate. */
1471 if (row == NULL)
1472 {
1473 if (base_face_id == MODE_LINE_FACE_ID)
1474 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1475 else if (base_face_id == HEADER_LINE_FACE_ID)
1476 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1477 }
1478
1479 /* Clear IT. */
1480 bzero (it, sizeof *it);
1481 it->current.overlay_string_index = -1;
1482 it->current.dpvec_index = -1;
1483 it->base_face_id = base_face_id;
1484
1485 /* The window in which we iterate over current_buffer: */
1486 XSETWINDOW (it->window, w);
1487 it->w = w;
1488 it->f = XFRAME (w->frame);
1489
1490 /* Extra space between lines (on window systems only). */
1491 if (base_face_id == DEFAULT_FACE_ID
1492 && FRAME_WINDOW_P (it->f))
1493 {
1494 if (NATNUMP (current_buffer->extra_line_spacing))
1495 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1496 else if (it->f->extra_line_spacing > 0)
1497 it->extra_line_spacing = it->f->extra_line_spacing;
1498 }
1499
1500 /* If realized faces have been removed, e.g. because of face
1501 attribute changes of named faces, recompute them. When running
1502 in batch mode, the face cache of Vterminal_frame is null. If
1503 we happen to get called, make a dummy face cache. */
1504 if (
1505 #ifndef WINDOWSNT
1506 noninteractive &&
1507 #endif
1508 FRAME_FACE_CACHE (it->f) == NULL)
1509 init_frame_faces (it->f);
1510 if (FRAME_FACE_CACHE (it->f)->used == 0)
1511 recompute_basic_faces (it->f);
1512
1513 /* Current value of the `space-width', and 'height' properties. */
1514 it->space_width = Qnil;
1515 it->font_height = Qnil;
1516
1517 /* Are control characters displayed as `^C'? */
1518 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1519
1520 /* -1 means everything between a CR and the following line end
1521 is invisible. >0 means lines indented more than this value are
1522 invisible. */
1523 it->selective = (INTEGERP (current_buffer->selective_display)
1524 ? XFASTINT (current_buffer->selective_display)
1525 : (!NILP (current_buffer->selective_display)
1526 ? -1 : 0));
1527 it->selective_display_ellipsis_p
1528 = !NILP (current_buffer->selective_display_ellipses);
1529
1530 /* Display table to use. */
1531 it->dp = window_display_table (w);
1532
1533 /* Are multibyte characters enabled in current_buffer? */
1534 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1535
1536 /* Non-zero if we should highlight the region. */
1537 highlight_region_p
1538 = (!NILP (Vtransient_mark_mode)
1539 && !NILP (current_buffer->mark_active)
1540 && XMARKER (current_buffer->mark)->buffer != 0);
1541
1542 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1543 start and end of a visible region in window IT->w. Set both to
1544 -1 to indicate no region. */
1545 if (highlight_region_p
1546 /* Maybe highlight only in selected window. */
1547 && (/* Either show region everywhere. */
1548 highlight_nonselected_windows
1549 /* Or show region in the selected window. */
1550 || w == XWINDOW (selected_window)
1551 /* Or show the region if we are in the mini-buffer and W is
1552 the window the mini-buffer refers to. */
1553 || (MINI_WINDOW_P (XWINDOW (selected_window))
1554 && WINDOWP (Vminibuf_scroll_window)
1555 && w == XWINDOW (Vminibuf_scroll_window))))
1556 {
1557 int charpos = marker_position (current_buffer->mark);
1558 it->region_beg_charpos = min (PT, charpos);
1559 it->region_end_charpos = max (PT, charpos);
1560 }
1561 else
1562 it->region_beg_charpos = it->region_end_charpos = -1;
1563
1564 /* Get the position at which the redisplay_end_trigger hook should
1565 be run, if it is to be run at all. */
1566 if (MARKERP (w->redisplay_end_trigger)
1567 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1568 it->redisplay_end_trigger_charpos
1569 = marker_position (w->redisplay_end_trigger);
1570 else if (INTEGERP (w->redisplay_end_trigger))
1571 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1572
1573 /* Correct bogus values of tab_width. */
1574 it->tab_width = XINT (current_buffer->tab_width);
1575 if (it->tab_width <= 0 || it->tab_width > 1000)
1576 it->tab_width = 8;
1577
1578 /* Are lines in the display truncated? */
1579 it->truncate_lines_p
1580 = (base_face_id != DEFAULT_FACE_ID
1581 || XINT (it->w->hscroll)
1582 || (truncate_partial_width_windows
1583 && !WINDOW_FULL_WIDTH_P (it->w))
1584 || !NILP (current_buffer->truncate_lines));
1585
1586 /* Get dimensions of truncation and continuation glyphs. These are
1587 displayed as bitmaps under X, so we don't need them for such
1588 frames. */
1589 if (!FRAME_WINDOW_P (it->f))
1590 {
1591 if (it->truncate_lines_p)
1592 {
1593 /* We will need the truncation glyph. */
1594 xassert (it->glyph_row == NULL);
1595 produce_special_glyphs (it, IT_TRUNCATION);
1596 it->truncation_pixel_width = it->pixel_width;
1597 }
1598 else
1599 {
1600 /* We will need the continuation glyph. */
1601 xassert (it->glyph_row == NULL);
1602 produce_special_glyphs (it, IT_CONTINUATION);
1603 it->continuation_pixel_width = it->pixel_width;
1604 }
1605
1606 /* Reset these values to zero becaue the produce_special_glyphs
1607 above has changed them. */
1608 it->pixel_width = it->ascent = it->descent = 0;
1609 it->phys_ascent = it->phys_descent = 0;
1610 }
1611
1612 /* Set this after getting the dimensions of truncation and
1613 continuation glyphs, so that we don't produce glyphs when calling
1614 produce_special_glyphs, above. */
1615 it->glyph_row = row;
1616 it->area = TEXT_AREA;
1617
1618 /* Get the dimensions of the display area. The display area
1619 consists of the visible window area plus a horizontally scrolled
1620 part to the left of the window. All x-values are relative to the
1621 start of this total display area. */
1622 if (base_face_id != DEFAULT_FACE_ID)
1623 {
1624 /* Mode lines, menu bar in terminal frames. */
1625 it->first_visible_x = 0;
1626 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1627 }
1628 else
1629 {
1630 it->first_visible_x
1631 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1632 it->last_visible_x = (it->first_visible_x
1633 + window_box_width (w, TEXT_AREA));
1634
1635 /* If we truncate lines, leave room for the truncator glyph(s) at
1636 the right margin. Otherwise, leave room for the continuation
1637 glyph(s). Truncation and continuation glyphs are not inserted
1638 for window-based redisplay. */
1639 if (!FRAME_WINDOW_P (it->f))
1640 {
1641 if (it->truncate_lines_p)
1642 it->last_visible_x -= it->truncation_pixel_width;
1643 else
1644 it->last_visible_x -= it->continuation_pixel_width;
1645 }
1646
1647 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1648 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1649 }
1650
1651 /* Leave room for a border glyph. */
1652 if (!FRAME_WINDOW_P (it->f)
1653 && !WINDOW_RIGHTMOST_P (it->w))
1654 it->last_visible_x -= 1;
1655
1656 it->last_visible_y = window_text_bottom_y (w);
1657
1658 /* For mode lines and alike, arrange for the first glyph having a
1659 left box line if the face specifies a box. */
1660 if (base_face_id != DEFAULT_FACE_ID)
1661 {
1662 struct face *face;
1663
1664 it->face_id = base_face_id;
1665
1666 /* If we have a boxed mode line, make the first character appear
1667 with a left box line. */
1668 face = FACE_FROM_ID (it->f, base_face_id);
1669 if (face->box != FACE_NO_BOX)
1670 it->start_of_box_run_p = 1;
1671 }
1672
1673 /* If a buffer position was specified, set the iterator there,
1674 getting overlays and face properties from that position. */
1675 if (charpos >= BUF_BEG (current_buffer))
1676 {
1677 it->end_charpos = ZV;
1678 it->face_id = -1;
1679 IT_CHARPOS (*it) = charpos;
1680
1681 /* Compute byte position if not specified. */
1682 if (bytepos < charpos)
1683 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1684 else
1685 IT_BYTEPOS (*it) = bytepos;
1686
1687 /* Compute faces etc. */
1688 reseat (it, it->current.pos, 1);
1689 }
1690
1691 CHECK_IT (it);
1692 }
1693
1694
1695 /* Initialize IT for the display of window W with window start POS. */
1696
1697 void
1698 start_display (it, w, pos)
1699 struct it *it;
1700 struct window *w;
1701 struct text_pos pos;
1702 {
1703 struct glyph_row *row;
1704 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1705
1706 row = w->desired_matrix->rows + first_vpos;
1707 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1708
1709 if (!it->truncate_lines_p)
1710 {
1711 int start_at_line_beg_p;
1712 int first_y = it->current_y;
1713
1714 /* If window start is not at a line start, skip forward to POS to
1715 get the correct continuation lines width. */
1716 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1717 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1718 if (!start_at_line_beg_p)
1719 {
1720 reseat_at_previous_visible_line_start (it);
1721 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1722
1723 /* If lines are continued, this line may end in the middle
1724 of a multi-glyph character (e.g. a control character
1725 displayed as \003, or in the middle of an overlay
1726 string). In this case move_it_to above will not have
1727 taken us to the start of the continuation line but to the
1728 end of the continued line. */
1729 if (it->current_x > 0)
1730 {
1731 if (it->current.dpvec_index >= 0
1732 || it->current.overlay_string_index >= 0)
1733 {
1734 set_iterator_to_next (it, 1);
1735 move_it_in_display_line_to (it, -1, -1, 0);
1736 }
1737
1738 it->continuation_lines_width += it->current_x;
1739 }
1740
1741 /* We're starting a new display line, not affected by the
1742 height of the continued line, so clear the appropriate
1743 fields in the iterator structure. */
1744 it->max_ascent = it->max_descent = 0;
1745 it->max_phys_ascent = it->max_phys_descent = 0;
1746
1747 it->current_y = first_y;
1748 it->vpos = 0;
1749 it->current_x = it->hpos = 0;
1750 }
1751 }
1752
1753 #if 0 /* Don't assert the following because start_display is sometimes
1754 called intentionally with a window start that is not at a
1755 line start. Please leave this code in as a comment. */
1756
1757 /* Window start should be on a line start, now. */
1758 xassert (it->continuation_lines_width
1759 || IT_CHARPOS (it) == BEGV
1760 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1761 #endif /* 0 */
1762 }
1763
1764
1765 /* Return 1 if POS is a position in ellipses displayed for invisible
1766 text. W is the window we display, for text property lookup. */
1767
1768 static int
1769 in_ellipses_for_invisible_text_p (pos, w)
1770 struct display_pos *pos;
1771 struct window *w;
1772 {
1773 Lisp_Object prop, window;
1774 int ellipses_p = 0;
1775 int charpos = CHARPOS (pos->pos);
1776
1777 /* If POS specifies a position in a display vector, this might
1778 be for an ellipsis displayed for invisible text. We won't
1779 get the iterator set up for delivering that ellipsis unless
1780 we make sure that it gets aware of the invisible text. */
1781 if (pos->dpvec_index >= 0
1782 && pos->overlay_string_index < 0
1783 && CHARPOS (pos->string_pos) < 0
1784 && charpos > BEGV
1785 && (XSETWINDOW (window, w),
1786 prop = Fget_char_property (make_number (charpos),
1787 Qinvisible, window),
1788 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1789 {
1790 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1791 window);
1792 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1793 }
1794
1795 return ellipses_p;
1796 }
1797
1798
1799 /* Initialize IT for stepping through current_buffer in window W,
1800 starting at position POS that includes overlay string and display
1801 vector/ control character translation position information. Value
1802 is zero if there are overlay strings with newlines at POS. */
1803
1804 static int
1805 init_from_display_pos (it, w, pos)
1806 struct it *it;
1807 struct window *w;
1808 struct display_pos *pos;
1809 {
1810 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1811 int i, overlay_strings_with_newlines = 0;
1812
1813 /* If POS specifies a position in a display vector, this might
1814 be for an ellipsis displayed for invisible text. We won't
1815 get the iterator set up for delivering that ellipsis unless
1816 we make sure that it gets aware of the invisible text. */
1817 if (in_ellipses_for_invisible_text_p (pos, w))
1818 {
1819 --charpos;
1820 bytepos = 0;
1821 }
1822
1823 /* Keep in mind: the call to reseat in init_iterator skips invisible
1824 text, so we might end up at a position different from POS. This
1825 is only a problem when POS is a row start after a newline and an
1826 overlay starts there with an after-string, and the overlay has an
1827 invisible property. Since we don't skip invisible text in
1828 display_line and elsewhere immediately after consuming the
1829 newline before the row start, such a POS will not be in a string,
1830 but the call to init_iterator below will move us to the
1831 after-string. */
1832 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1833
1834 for (i = 0; i < it->n_overlay_strings; ++i)
1835 {
1836 char *s = XSTRING (it->overlay_strings[i])->data;
1837 char *e = s + STRING_BYTES (XSTRING (it->overlay_strings[i]));
1838
1839 while (s < e && *s != '\n')
1840 ++s;
1841
1842 if (s < e)
1843 {
1844 overlay_strings_with_newlines = 1;
1845 break;
1846 }
1847 }
1848
1849 /* If position is within an overlay string, set up IT to the right
1850 overlay string. */
1851 if (pos->overlay_string_index >= 0)
1852 {
1853 int relative_index;
1854
1855 /* If the first overlay string happens to have a `display'
1856 property for an image, the iterator will be set up for that
1857 image, and we have to undo that setup first before we can
1858 correct the overlay string index. */
1859 if (it->method == next_element_from_image)
1860 pop_it (it);
1861
1862 /* We already have the first chunk of overlay strings in
1863 IT->overlay_strings. Load more until the one for
1864 pos->overlay_string_index is in IT->overlay_strings. */
1865 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1866 {
1867 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1868 it->current.overlay_string_index = 0;
1869 while (n--)
1870 {
1871 load_overlay_strings (it, 0);
1872 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1873 }
1874 }
1875
1876 it->current.overlay_string_index = pos->overlay_string_index;
1877 relative_index = (it->current.overlay_string_index
1878 % OVERLAY_STRING_CHUNK_SIZE);
1879 it->string = it->overlay_strings[relative_index];
1880 xassert (STRINGP (it->string));
1881 it->current.string_pos = pos->string_pos;
1882 it->method = next_element_from_string;
1883 }
1884
1885 #if 0 /* This is bogus because POS not having an overlay string
1886 position does not mean it's after the string. Example: A
1887 line starting with a before-string and initialization of IT
1888 to the previous row's end position. */
1889 else if (it->current.overlay_string_index >= 0)
1890 {
1891 /* If POS says we're already after an overlay string ending at
1892 POS, make sure to pop the iterator because it will be in
1893 front of that overlay string. When POS is ZV, we've thereby
1894 also ``processed'' overlay strings at ZV. */
1895 while (it->sp)
1896 pop_it (it);
1897 it->current.overlay_string_index = -1;
1898 it->method = next_element_from_buffer;
1899 if (CHARPOS (pos->pos) == ZV)
1900 it->overlay_strings_at_end_processed_p = 1;
1901 }
1902 #endif /* 0 */
1903
1904 if (CHARPOS (pos->string_pos) >= 0)
1905 {
1906 /* Recorded position is not in an overlay string, but in another
1907 string. This can only be a string from a `display' property.
1908 IT should already be filled with that string. */
1909 it->current.string_pos = pos->string_pos;
1910 xassert (STRINGP (it->string));
1911 }
1912
1913 /* Restore position in display vector translations, control
1914 character translations or ellipses. */
1915 if (pos->dpvec_index >= 0)
1916 {
1917 if (it->dpvec == NULL)
1918 get_next_display_element (it);
1919 xassert (it->dpvec && it->current.dpvec_index == 0);
1920 it->current.dpvec_index = pos->dpvec_index;
1921 }
1922
1923 CHECK_IT (it);
1924 return !overlay_strings_with_newlines;
1925 }
1926
1927
1928 /* Initialize IT for stepping through current_buffer in window W
1929 starting at ROW->start. */
1930
1931 static void
1932 init_to_row_start (it, w, row)
1933 struct it *it;
1934 struct window *w;
1935 struct glyph_row *row;
1936 {
1937 init_from_display_pos (it, w, &row->start);
1938 it->continuation_lines_width = row->continuation_lines_width;
1939 CHECK_IT (it);
1940 }
1941
1942
1943 /* Initialize IT for stepping through current_buffer in window W
1944 starting in the line following ROW, i.e. starting at ROW->end.
1945 Value is zero if there are overlay strings with newlines at ROW's
1946 end position. */
1947
1948 static int
1949 init_to_row_end (it, w, row)
1950 struct it *it;
1951 struct window *w;
1952 struct glyph_row *row;
1953 {
1954 int success = 0;
1955
1956 if (init_from_display_pos (it, w, &row->end))
1957 {
1958 if (row->continued_p)
1959 it->continuation_lines_width
1960 = row->continuation_lines_width + row->pixel_width;
1961 CHECK_IT (it);
1962 success = 1;
1963 }
1964
1965 return success;
1966 }
1967
1968
1969
1970 \f
1971 /***********************************************************************
1972 Text properties
1973 ***********************************************************************/
1974
1975 /* Called when IT reaches IT->stop_charpos. Handle text property and
1976 overlay changes. Set IT->stop_charpos to the next position where
1977 to stop. */
1978
1979 static void
1980 handle_stop (it)
1981 struct it *it;
1982 {
1983 enum prop_handled handled;
1984 int handle_overlay_change_p = 1;
1985 struct props *p;
1986
1987 it->dpvec = NULL;
1988 it->current.dpvec_index = -1;
1989
1990 do
1991 {
1992 handled = HANDLED_NORMALLY;
1993
1994 /* Call text property handlers. */
1995 for (p = it_props; p->handler; ++p)
1996 {
1997 handled = p->handler (it);
1998
1999 if (handled == HANDLED_RECOMPUTE_PROPS)
2000 break;
2001 else if (handled == HANDLED_RETURN)
2002 return;
2003 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2004 handle_overlay_change_p = 0;
2005 }
2006
2007 if (handled != HANDLED_RECOMPUTE_PROPS)
2008 {
2009 /* Don't check for overlay strings below when set to deliver
2010 characters from a display vector. */
2011 if (it->method == next_element_from_display_vector)
2012 handle_overlay_change_p = 0;
2013
2014 /* Handle overlay changes. */
2015 if (handle_overlay_change_p)
2016 handled = handle_overlay_change (it);
2017
2018 /* Determine where to stop next. */
2019 if (handled == HANDLED_NORMALLY)
2020 compute_stop_pos (it);
2021 }
2022 }
2023 while (handled == HANDLED_RECOMPUTE_PROPS);
2024 }
2025
2026
2027 /* Compute IT->stop_charpos from text property and overlay change
2028 information for IT's current position. */
2029
2030 static void
2031 compute_stop_pos (it)
2032 struct it *it;
2033 {
2034 register INTERVAL iv, next_iv;
2035 Lisp_Object object, limit, position;
2036
2037 /* If nowhere else, stop at the end. */
2038 it->stop_charpos = it->end_charpos;
2039
2040 if (STRINGP (it->string))
2041 {
2042 /* Strings are usually short, so don't limit the search for
2043 properties. */
2044 object = it->string;
2045 limit = Qnil;
2046 position = make_number (IT_STRING_CHARPOS (*it));
2047 }
2048 else
2049 {
2050 int charpos;
2051
2052 /* If next overlay change is in front of the current stop pos
2053 (which is IT->end_charpos), stop there. Note: value of
2054 next_overlay_change is point-max if no overlay change
2055 follows. */
2056 charpos = next_overlay_change (IT_CHARPOS (*it));
2057 if (charpos < it->stop_charpos)
2058 it->stop_charpos = charpos;
2059
2060 /* If showing the region, we have to stop at the region
2061 start or end because the face might change there. */
2062 if (it->region_beg_charpos > 0)
2063 {
2064 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2065 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2066 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2067 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2068 }
2069
2070 /* Set up variables for computing the stop position from text
2071 property changes. */
2072 XSETBUFFER (object, current_buffer);
2073 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2074 position = make_number (IT_CHARPOS (*it));
2075
2076 }
2077
2078 /* Get the interval containing IT's position. Value is a null
2079 interval if there isn't such an interval. */
2080 iv = validate_interval_range (object, &position, &position, 0);
2081 if (!NULL_INTERVAL_P (iv))
2082 {
2083 Lisp_Object values_here[LAST_PROP_IDX];
2084 struct props *p;
2085
2086 /* Get properties here. */
2087 for (p = it_props; p->handler; ++p)
2088 values_here[p->idx] = textget (iv->plist, *p->name);
2089
2090 /* Look for an interval following iv that has different
2091 properties. */
2092 for (next_iv = next_interval (iv);
2093 (!NULL_INTERVAL_P (next_iv)
2094 && (NILP (limit)
2095 || XFASTINT (limit) > next_iv->position));
2096 next_iv = next_interval (next_iv))
2097 {
2098 for (p = it_props; p->handler; ++p)
2099 {
2100 Lisp_Object new_value;
2101
2102 new_value = textget (next_iv->plist, *p->name);
2103 if (!EQ (values_here[p->idx], new_value))
2104 break;
2105 }
2106
2107 if (p->handler)
2108 break;
2109 }
2110
2111 if (!NULL_INTERVAL_P (next_iv))
2112 {
2113 if (INTEGERP (limit)
2114 && next_iv->position >= XFASTINT (limit))
2115 /* No text property change up to limit. */
2116 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2117 else
2118 /* Text properties change in next_iv. */
2119 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2120 }
2121 }
2122
2123 xassert (STRINGP (it->string)
2124 || (it->stop_charpos >= BEGV
2125 && it->stop_charpos >= IT_CHARPOS (*it)));
2126 }
2127
2128
2129 /* Return the position of the next overlay change after POS in
2130 current_buffer. Value is point-max if no overlay change
2131 follows. This is like `next-overlay-change' but doesn't use
2132 xmalloc. */
2133
2134 static int
2135 next_overlay_change (pos)
2136 int pos;
2137 {
2138 int noverlays;
2139 int endpos;
2140 Lisp_Object *overlays;
2141 int len;
2142 int i;
2143
2144 /* Get all overlays at the given position. */
2145 len = 10;
2146 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2147 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2148 if (noverlays > len)
2149 {
2150 len = noverlays;
2151 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2152 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2153 }
2154
2155 /* If any of these overlays ends before endpos,
2156 use its ending point instead. */
2157 for (i = 0; i < noverlays; ++i)
2158 {
2159 Lisp_Object oend;
2160 int oendpos;
2161
2162 oend = OVERLAY_END (overlays[i]);
2163 oendpos = OVERLAY_POSITION (oend);
2164 endpos = min (endpos, oendpos);
2165 }
2166
2167 return endpos;
2168 }
2169
2170
2171 \f
2172 /***********************************************************************
2173 Fontification
2174 ***********************************************************************/
2175
2176 /* Handle changes in the `fontified' property of the current buffer by
2177 calling hook functions from Qfontification_functions to fontify
2178 regions of text. */
2179
2180 static enum prop_handled
2181 handle_fontified_prop (it)
2182 struct it *it;
2183 {
2184 Lisp_Object prop, pos;
2185 enum prop_handled handled = HANDLED_NORMALLY;
2186
2187 /* Get the value of the `fontified' property at IT's current buffer
2188 position. (The `fontified' property doesn't have a special
2189 meaning in strings.) If the value is nil, call functions from
2190 Qfontification_functions. */
2191 if (!STRINGP (it->string)
2192 && it->s == NULL
2193 && !NILP (Vfontification_functions)
2194 && !NILP (Vrun_hooks)
2195 && (pos = make_number (IT_CHARPOS (*it)),
2196 prop = Fget_char_property (pos, Qfontified, Qnil),
2197 NILP (prop)))
2198 {
2199 int count = BINDING_STACK_SIZE ();
2200 Lisp_Object val;
2201
2202 val = Vfontification_functions;
2203 specbind (Qfontification_functions, Qnil);
2204
2205 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2206 safe_call1 (val, pos);
2207 else
2208 {
2209 Lisp_Object globals, fn;
2210 struct gcpro gcpro1, gcpro2;
2211
2212 globals = Qnil;
2213 GCPRO2 (val, globals);
2214
2215 for (; CONSP (val); val = XCDR (val))
2216 {
2217 fn = XCAR (val);
2218
2219 if (EQ (fn, Qt))
2220 {
2221 /* A value of t indicates this hook has a local
2222 binding; it means to run the global binding too.
2223 In a global value, t should not occur. If it
2224 does, we must ignore it to avoid an endless
2225 loop. */
2226 for (globals = Fdefault_value (Qfontification_functions);
2227 CONSP (globals);
2228 globals = XCDR (globals))
2229 {
2230 fn = XCAR (globals);
2231 if (!EQ (fn, Qt))
2232 safe_call1 (fn, pos);
2233 }
2234 }
2235 else
2236 safe_call1 (fn, pos);
2237 }
2238
2239 UNGCPRO;
2240 }
2241
2242 unbind_to (count, Qnil);
2243
2244 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2245 something. This avoids an endless loop if they failed to
2246 fontify the text for which reason ever. */
2247 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2248 handled = HANDLED_RECOMPUTE_PROPS;
2249 }
2250
2251 return handled;
2252 }
2253
2254
2255 \f
2256 /***********************************************************************
2257 Faces
2258 ***********************************************************************/
2259
2260 /* Set up iterator IT from face properties at its current position.
2261 Called from handle_stop. */
2262
2263 static enum prop_handled
2264 handle_face_prop (it)
2265 struct it *it;
2266 {
2267 int new_face_id, next_stop;
2268
2269 if (!STRINGP (it->string))
2270 {
2271 new_face_id
2272 = face_at_buffer_position (it->w,
2273 IT_CHARPOS (*it),
2274 it->region_beg_charpos,
2275 it->region_end_charpos,
2276 &next_stop,
2277 (IT_CHARPOS (*it)
2278 + TEXT_PROP_DISTANCE_LIMIT),
2279 0);
2280
2281 /* Is this a start of a run of characters with box face?
2282 Caveat: this can be called for a freshly initialized
2283 iterator; face_id is -1 is this case. We know that the new
2284 face will not change until limit, i.e. if the new face has a
2285 box, all characters up to limit will have one. But, as
2286 usual, we don't know whether limit is really the end. */
2287 if (new_face_id != it->face_id)
2288 {
2289 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2290
2291 /* If new face has a box but old face has not, this is
2292 the start of a run of characters with box, i.e. it has
2293 a shadow on the left side. The value of face_id of the
2294 iterator will be -1 if this is the initial call that gets
2295 the face. In this case, we have to look in front of IT's
2296 position and see whether there is a face != new_face_id. */
2297 it->start_of_box_run_p
2298 = (new_face->box != FACE_NO_BOX
2299 && (it->face_id >= 0
2300 || IT_CHARPOS (*it) == BEG
2301 || new_face_id != face_before_it_pos (it)));
2302 it->face_box_p = new_face->box != FACE_NO_BOX;
2303 }
2304 }
2305 else
2306 {
2307 int base_face_id, bufpos;
2308
2309 if (it->current.overlay_string_index >= 0)
2310 bufpos = IT_CHARPOS (*it);
2311 else
2312 bufpos = 0;
2313
2314 /* For strings from a buffer, i.e. overlay strings or strings
2315 from a `display' property, use the face at IT's current
2316 buffer position as the base face to merge with, so that
2317 overlay strings appear in the same face as surrounding
2318 text, unless they specify their own faces. */
2319 base_face_id = underlying_face_id (it);
2320
2321 new_face_id = face_at_string_position (it->w,
2322 it->string,
2323 IT_STRING_CHARPOS (*it),
2324 bufpos,
2325 it->region_beg_charpos,
2326 it->region_end_charpos,
2327 &next_stop,
2328 base_face_id, 0);
2329
2330 #if 0 /* This shouldn't be neccessary. Let's check it. */
2331 /* If IT is used to display a mode line we would really like to
2332 use the mode line face instead of the frame's default face. */
2333 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2334 && new_face_id == DEFAULT_FACE_ID)
2335 new_face_id = MODE_LINE_FACE_ID;
2336 #endif
2337
2338 /* Is this a start of a run of characters with box? Caveat:
2339 this can be called for a freshly allocated iterator; face_id
2340 is -1 is this case. We know that the new face will not
2341 change until the next check pos, i.e. if the new face has a
2342 box, all characters up to that position will have a
2343 box. But, as usual, we don't know whether that position
2344 is really the end. */
2345 if (new_face_id != it->face_id)
2346 {
2347 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2348 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2349
2350 /* If new face has a box but old face hasn't, this is the
2351 start of a run of characters with box, i.e. it has a
2352 shadow on the left side. */
2353 it->start_of_box_run_p
2354 = new_face->box && (old_face == NULL || !old_face->box);
2355 it->face_box_p = new_face->box != FACE_NO_BOX;
2356 }
2357 }
2358
2359 it->face_id = new_face_id;
2360 return HANDLED_NORMALLY;
2361 }
2362
2363
2364 /* Return the ID of the face ``underlying'' IT's current position,
2365 which is in a string. If the iterator is associated with a
2366 buffer, return the face at IT's current buffer position.
2367 Otherwise, use the iterator's base_face_id. */
2368
2369 static int
2370 underlying_face_id (it)
2371 struct it *it;
2372 {
2373 int face_id = it->base_face_id, i;
2374
2375 xassert (STRINGP (it->string));
2376
2377 for (i = it->sp - 1; i >= 0; --i)
2378 if (NILP (it->stack[i].string))
2379 face_id = it->stack[i].face_id;
2380
2381 return face_id;
2382 }
2383
2384
2385 /* Compute the face one character before or after the current position
2386 of IT. BEFORE_P non-zero means get the face in front of IT's
2387 position. Value is the id of the face. */
2388
2389 static int
2390 face_before_or_after_it_pos (it, before_p)
2391 struct it *it;
2392 int before_p;
2393 {
2394 int face_id, limit;
2395 int next_check_charpos;
2396 struct text_pos pos;
2397
2398 xassert (it->s == NULL);
2399
2400 if (STRINGP (it->string))
2401 {
2402 int bufpos, base_face_id;
2403
2404 /* No face change past the end of the string (for the case
2405 we are padding with spaces). No face change before the
2406 string start. */
2407 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2408 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2409 return it->face_id;
2410
2411 /* Set pos to the position before or after IT's current position. */
2412 if (before_p)
2413 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2414 else
2415 /* For composition, we must check the character after the
2416 composition. */
2417 pos = (it->what == IT_COMPOSITION
2418 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2419 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2420
2421 if (it->current.overlay_string_index >= 0)
2422 bufpos = IT_CHARPOS (*it);
2423 else
2424 bufpos = 0;
2425
2426 base_face_id = underlying_face_id (it);
2427
2428 /* Get the face for ASCII, or unibyte. */
2429 face_id = face_at_string_position (it->w,
2430 it->string,
2431 CHARPOS (pos),
2432 bufpos,
2433 it->region_beg_charpos,
2434 it->region_end_charpos,
2435 &next_check_charpos,
2436 base_face_id, 0);
2437
2438 /* Correct the face for charsets different from ASCII. Do it
2439 for the multibyte case only. The face returned above is
2440 suitable for unibyte text if IT->string is unibyte. */
2441 if (STRING_MULTIBYTE (it->string))
2442 {
2443 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2444 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2445 int c, len;
2446 struct face *face = FACE_FROM_ID (it->f, face_id);
2447
2448 c = string_char_and_length (p, rest, &len);
2449 face_id = FACE_FOR_CHAR (it->f, face, c);
2450 }
2451 }
2452 else
2453 {
2454 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2455 || (IT_CHARPOS (*it) <= BEGV && before_p))
2456 return it->face_id;
2457
2458 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2459 pos = it->current.pos;
2460
2461 if (before_p)
2462 DEC_TEXT_POS (pos, it->multibyte_p);
2463 else
2464 {
2465 if (it->what == IT_COMPOSITION)
2466 /* For composition, we must check the position after the
2467 composition. */
2468 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2469 else
2470 INC_TEXT_POS (pos, it->multibyte_p);
2471 }
2472
2473 /* Determine face for CHARSET_ASCII, or unibyte. */
2474 face_id = face_at_buffer_position (it->w,
2475 CHARPOS (pos),
2476 it->region_beg_charpos,
2477 it->region_end_charpos,
2478 &next_check_charpos,
2479 limit, 0);
2480
2481 /* Correct the face for charsets different from ASCII. Do it
2482 for the multibyte case only. The face returned above is
2483 suitable for unibyte text if current_buffer is unibyte. */
2484 if (it->multibyte_p)
2485 {
2486 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2487 struct face *face = FACE_FROM_ID (it->f, face_id);
2488 face_id = FACE_FOR_CHAR (it->f, face, c);
2489 }
2490 }
2491
2492 return face_id;
2493 }
2494
2495
2496 \f
2497 /***********************************************************************
2498 Invisible text
2499 ***********************************************************************/
2500
2501 /* Set up iterator IT from invisible properties at its current
2502 position. Called from handle_stop. */
2503
2504 static enum prop_handled
2505 handle_invisible_prop (it)
2506 struct it *it;
2507 {
2508 enum prop_handled handled = HANDLED_NORMALLY;
2509
2510 if (STRINGP (it->string))
2511 {
2512 extern Lisp_Object Qinvisible;
2513 Lisp_Object prop, end_charpos, limit, charpos;
2514
2515 /* Get the value of the invisible text property at the
2516 current position. Value will be nil if there is no such
2517 property. */
2518 charpos = make_number (IT_STRING_CHARPOS (*it));
2519 prop = Fget_text_property (charpos, Qinvisible, it->string);
2520
2521 if (!NILP (prop)
2522 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2523 {
2524 handled = HANDLED_RECOMPUTE_PROPS;
2525
2526 /* Get the position at which the next change of the
2527 invisible text property can be found in IT->string.
2528 Value will be nil if the property value is the same for
2529 all the rest of IT->string. */
2530 XSETINT (limit, XSTRING (it->string)->size);
2531 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2532 it->string, limit);
2533
2534 /* Text at current position is invisible. The next
2535 change in the property is at position end_charpos.
2536 Move IT's current position to that position. */
2537 if (INTEGERP (end_charpos)
2538 && XFASTINT (end_charpos) < XFASTINT (limit))
2539 {
2540 struct text_pos old;
2541 old = it->current.string_pos;
2542 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2543 compute_string_pos (&it->current.string_pos, old, it->string);
2544 }
2545 else
2546 {
2547 /* The rest of the string is invisible. If this is an
2548 overlay string, proceed with the next overlay string
2549 or whatever comes and return a character from there. */
2550 if (it->current.overlay_string_index >= 0)
2551 {
2552 next_overlay_string (it);
2553 /* Don't check for overlay strings when we just
2554 finished processing them. */
2555 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2556 }
2557 else
2558 {
2559 struct Lisp_String *s = XSTRING (it->string);
2560 IT_STRING_CHARPOS (*it) = s->size;
2561 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2562 }
2563 }
2564 }
2565 }
2566 else
2567 {
2568 int invis_p, newpos, next_stop, start_charpos;
2569 Lisp_Object pos, prop, overlay;
2570
2571 /* First of all, is there invisible text at this position? */
2572 start_charpos = IT_CHARPOS (*it);
2573 pos = make_number (IT_CHARPOS (*it));
2574 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2575 &overlay);
2576 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2577
2578 /* If we are on invisible text, skip over it. */
2579 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2580 {
2581 /* Record whether we have to display an ellipsis for the
2582 invisible text. */
2583 int display_ellipsis_p = invis_p == 2;
2584
2585 handled = HANDLED_RECOMPUTE_PROPS;
2586
2587 /* Loop skipping over invisible text. The loop is left at
2588 ZV or with IT on the first char being visible again. */
2589 do
2590 {
2591 /* Try to skip some invisible text. Return value is the
2592 position reached which can be equal to IT's position
2593 if there is nothing invisible here. This skips both
2594 over invisible text properties and overlays with
2595 invisible property. */
2596 newpos = skip_invisible (IT_CHARPOS (*it),
2597 &next_stop, ZV, it->window);
2598
2599 /* If we skipped nothing at all we weren't at invisible
2600 text in the first place. If everything to the end of
2601 the buffer was skipped, end the loop. */
2602 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2603 invis_p = 0;
2604 else
2605 {
2606 /* We skipped some characters but not necessarily
2607 all there are. Check if we ended up on visible
2608 text. Fget_char_property returns the property of
2609 the char before the given position, i.e. if we
2610 get invis_p = 0, this means that the char at
2611 newpos is visible. */
2612 pos = make_number (newpos);
2613 prop = Fget_char_property (pos, Qinvisible, it->window);
2614 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2615 }
2616
2617 /* If we ended up on invisible text, proceed to
2618 skip starting with next_stop. */
2619 if (invis_p)
2620 IT_CHARPOS (*it) = next_stop;
2621 }
2622 while (invis_p);
2623
2624 /* The position newpos is now either ZV or on visible text. */
2625 IT_CHARPOS (*it) = newpos;
2626 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2627
2628 /* If there are before-strings at the start of invisible
2629 text, and the text is invisible because of a text
2630 property, arrange to show before-strings because 20.x did
2631 it that way. (If the text is invisible because of an
2632 overlay property instead of a text property, this is
2633 already handled in the overlay code.) */
2634 if (NILP (overlay)
2635 && get_overlay_strings (it, start_charpos))
2636 {
2637 handled = HANDLED_RECOMPUTE_PROPS;
2638 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2639 }
2640 else if (display_ellipsis_p)
2641 setup_for_ellipsis (it);
2642 }
2643 }
2644
2645 return handled;
2646 }
2647
2648
2649 /* Make iterator IT return `...' next. */
2650
2651 static void
2652 setup_for_ellipsis (it)
2653 struct it *it;
2654 {
2655 if (it->dp
2656 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2657 {
2658 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2659 it->dpvec = v->contents;
2660 it->dpend = v->contents + v->size;
2661 }
2662 else
2663 {
2664 /* Default `...'. */
2665 it->dpvec = default_invis_vector;
2666 it->dpend = default_invis_vector + 3;
2667 }
2668
2669 /* The ellipsis display does not replace the display of the
2670 character at the new position. Indicate this by setting
2671 IT->dpvec_char_len to zero. */
2672 it->dpvec_char_len = 0;
2673
2674 it->current.dpvec_index = 0;
2675 it->method = next_element_from_display_vector;
2676 }
2677
2678
2679 \f
2680 /***********************************************************************
2681 'display' property
2682 ***********************************************************************/
2683
2684 /* Set up iterator IT from `display' property at its current position.
2685 Called from handle_stop. */
2686
2687 static enum prop_handled
2688 handle_display_prop (it)
2689 struct it *it;
2690 {
2691 Lisp_Object prop, object;
2692 struct text_pos *position;
2693 int display_replaced_p = 0;
2694
2695 if (STRINGP (it->string))
2696 {
2697 object = it->string;
2698 position = &it->current.string_pos;
2699 }
2700 else
2701 {
2702 object = it->w->buffer;
2703 position = &it->current.pos;
2704 }
2705
2706 /* Reset those iterator values set from display property values. */
2707 it->font_height = Qnil;
2708 it->space_width = Qnil;
2709 it->voffset = 0;
2710
2711 /* We don't support recursive `display' properties, i.e. string
2712 values that have a string `display' property, that have a string
2713 `display' property etc. */
2714 if (!it->string_from_display_prop_p)
2715 it->area = TEXT_AREA;
2716
2717 prop = Fget_char_property (make_number (position->charpos),
2718 Qdisplay, object);
2719 if (NILP (prop))
2720 return HANDLED_NORMALLY;
2721
2722 if (CONSP (prop)
2723 /* Simple properties. */
2724 && !EQ (XCAR (prop), Qimage)
2725 && !EQ (XCAR (prop), Qspace)
2726 && !EQ (XCAR (prop), Qwhen)
2727 && !EQ (XCAR (prop), Qspace_width)
2728 && !EQ (XCAR (prop), Qheight)
2729 && !EQ (XCAR (prop), Qraise)
2730 /* Marginal area specifications. */
2731 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2732 && !NILP (XCAR (prop)))
2733 {
2734 for (; CONSP (prop); prop = XCDR (prop))
2735 {
2736 if (handle_single_display_prop (it, XCAR (prop), object,
2737 position, display_replaced_p))
2738 display_replaced_p = 1;
2739 }
2740 }
2741 else if (VECTORP (prop))
2742 {
2743 int i;
2744 for (i = 0; i < ASIZE (prop); ++i)
2745 if (handle_single_display_prop (it, AREF (prop, i), object,
2746 position, display_replaced_p))
2747 display_replaced_p = 1;
2748 }
2749 else
2750 {
2751 if (handle_single_display_prop (it, prop, object, position, 0))
2752 display_replaced_p = 1;
2753 }
2754
2755 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2756 }
2757
2758
2759 /* Value is the position of the end of the `display' property starting
2760 at START_POS in OBJECT. */
2761
2762 static struct text_pos
2763 display_prop_end (it, object, start_pos)
2764 struct it *it;
2765 Lisp_Object object;
2766 struct text_pos start_pos;
2767 {
2768 Lisp_Object end;
2769 struct text_pos end_pos;
2770
2771 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2772 Qdisplay, object, Qnil);
2773 CHARPOS (end_pos) = XFASTINT (end);
2774 if (STRINGP (object))
2775 compute_string_pos (&end_pos, start_pos, it->string);
2776 else
2777 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2778
2779 return end_pos;
2780 }
2781
2782
2783 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2784 is the object in which the `display' property was found. *POSITION
2785 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2786 means that we previously saw a display sub-property which already
2787 replaced text display with something else, for example an image;
2788 ignore such properties after the first one has been processed.
2789
2790 If PROP is a `space' or `image' sub-property, set *POSITION to the
2791 end position of the `display' property.
2792
2793 Value is non-zero something was found which replaces the display
2794 of buffer or string text. */
2795
2796 static int
2797 handle_single_display_prop (it, prop, object, position,
2798 display_replaced_before_p)
2799 struct it *it;
2800 Lisp_Object prop;
2801 Lisp_Object object;
2802 struct text_pos *position;
2803 int display_replaced_before_p;
2804 {
2805 Lisp_Object value;
2806 int replaces_text_display_p = 0;
2807 Lisp_Object form;
2808
2809 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2810 evaluated. If the result is nil, VALUE is ignored. */
2811 form = Qt;
2812 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2813 {
2814 prop = XCDR (prop);
2815 if (!CONSP (prop))
2816 return 0;
2817 form = XCAR (prop);
2818 prop = XCDR (prop);
2819 }
2820
2821 if (!NILP (form) && !EQ (form, Qt))
2822 {
2823 int count = BINDING_STACK_SIZE ();
2824 struct gcpro gcpro1;
2825
2826 /* Bind `object' to the object having the `display' property, a
2827 buffer or string. Bind `position' to the position in the
2828 object where the property was found, and `buffer-position'
2829 to the current position in the buffer. */
2830 specbind (Qobject, object);
2831 specbind (Qposition, make_number (CHARPOS (*position)));
2832 specbind (Qbuffer_position,
2833 make_number (STRINGP (object)
2834 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2835 GCPRO1 (form);
2836 form = safe_eval (form);
2837 UNGCPRO;
2838 unbind_to (count, Qnil);
2839 }
2840
2841 if (NILP (form))
2842 return 0;
2843
2844 if (CONSP (prop)
2845 && EQ (XCAR (prop), Qheight)
2846 && CONSP (XCDR (prop)))
2847 {
2848 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2849 return 0;
2850
2851 /* `(height HEIGHT)'. */
2852 it->font_height = XCAR (XCDR (prop));
2853 if (!NILP (it->font_height))
2854 {
2855 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2856 int new_height = -1;
2857
2858 if (CONSP (it->font_height)
2859 && (EQ (XCAR (it->font_height), Qplus)
2860 || EQ (XCAR (it->font_height), Qminus))
2861 && CONSP (XCDR (it->font_height))
2862 && INTEGERP (XCAR (XCDR (it->font_height))))
2863 {
2864 /* `(+ N)' or `(- N)' where N is an integer. */
2865 int steps = XINT (XCAR (XCDR (it->font_height)));
2866 if (EQ (XCAR (it->font_height), Qplus))
2867 steps = - steps;
2868 it->face_id = smaller_face (it->f, it->face_id, steps);
2869 }
2870 else if (FUNCTIONP (it->font_height))
2871 {
2872 /* Call function with current height as argument.
2873 Value is the new height. */
2874 Lisp_Object height;
2875 height = safe_call1 (it->font_height,
2876 face->lface[LFACE_HEIGHT_INDEX]);
2877 if (NUMBERP (height))
2878 new_height = XFLOATINT (height);
2879 }
2880 else if (NUMBERP (it->font_height))
2881 {
2882 /* Value is a multiple of the canonical char height. */
2883 struct face *face;
2884
2885 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2886 new_height = (XFLOATINT (it->font_height)
2887 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2888 }
2889 else
2890 {
2891 /* Evaluate IT->font_height with `height' bound to the
2892 current specified height to get the new height. */
2893 Lisp_Object value;
2894 int count = BINDING_STACK_SIZE ();
2895
2896 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2897 value = safe_eval (it->font_height);
2898 unbind_to (count, Qnil);
2899
2900 if (NUMBERP (value))
2901 new_height = XFLOATINT (value);
2902 }
2903
2904 if (new_height > 0)
2905 it->face_id = face_with_height (it->f, it->face_id, new_height);
2906 }
2907 }
2908 else if (CONSP (prop)
2909 && EQ (XCAR (prop), Qspace_width)
2910 && CONSP (XCDR (prop)))
2911 {
2912 /* `(space_width WIDTH)'. */
2913 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2914 return 0;
2915
2916 value = XCAR (XCDR (prop));
2917 if (NUMBERP (value) && XFLOATINT (value) > 0)
2918 it->space_width = value;
2919 }
2920 else if (CONSP (prop)
2921 && EQ (XCAR (prop), Qraise)
2922 && CONSP (XCDR (prop)))
2923 {
2924 /* `(raise FACTOR)'. */
2925 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2926 return 0;
2927
2928 #ifdef HAVE_WINDOW_SYSTEM
2929 value = XCAR (XCDR (prop));
2930 if (NUMBERP (value))
2931 {
2932 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2933 it->voffset = - (XFLOATINT (value)
2934 * (FONT_HEIGHT (face->font)));
2935 }
2936 #endif /* HAVE_WINDOW_SYSTEM */
2937 }
2938 else if (!it->string_from_display_prop_p)
2939 {
2940 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2941 VALUE) or `((margin nil) VALUE)' or VALUE. */
2942 Lisp_Object location, value;
2943 struct text_pos start_pos;
2944 int valid_p;
2945
2946 /* Characters having this form of property are not displayed, so
2947 we have to find the end of the property. */
2948 start_pos = *position;
2949 *position = display_prop_end (it, object, start_pos);
2950 value = Qnil;
2951
2952 /* Let's stop at the new position and assume that all
2953 text properties change there. */
2954 it->stop_charpos = position->charpos;
2955
2956 location = Qunbound;
2957 if (CONSP (prop) && CONSP (XCAR (prop)))
2958 {
2959 Lisp_Object tem;
2960
2961 value = XCDR (prop);
2962 if (CONSP (value))
2963 value = XCAR (value);
2964
2965 tem = XCAR (prop);
2966 if (EQ (XCAR (tem), Qmargin)
2967 && (tem = XCDR (tem),
2968 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2969 (NILP (tem)
2970 || EQ (tem, Qleft_margin)
2971 || EQ (tem, Qright_margin))))
2972 location = tem;
2973 }
2974
2975 if (EQ (location, Qunbound))
2976 {
2977 location = Qnil;
2978 value = prop;
2979 }
2980
2981 #ifdef HAVE_WINDOW_SYSTEM
2982 if (FRAME_TERMCAP_P (it->f))
2983 valid_p = STRINGP (value);
2984 else
2985 valid_p = (STRINGP (value)
2986 || (CONSP (value) && EQ (XCAR (value), Qspace))
2987 || valid_image_p (value));
2988 #else /* not HAVE_WINDOW_SYSTEM */
2989 valid_p = STRINGP (value);
2990 #endif /* not HAVE_WINDOW_SYSTEM */
2991
2992 if ((EQ (location, Qleft_margin)
2993 || EQ (location, Qright_margin)
2994 || NILP (location))
2995 && valid_p
2996 && !display_replaced_before_p)
2997 {
2998 replaces_text_display_p = 1;
2999
3000 /* Save current settings of IT so that we can restore them
3001 when we are finished with the glyph property value. */
3002 push_it (it);
3003
3004 if (NILP (location))
3005 it->area = TEXT_AREA;
3006 else if (EQ (location, Qleft_margin))
3007 it->area = LEFT_MARGIN_AREA;
3008 else
3009 it->area = RIGHT_MARGIN_AREA;
3010
3011 if (STRINGP (value))
3012 {
3013 it->string = value;
3014 it->multibyte_p = STRING_MULTIBYTE (it->string);
3015 it->current.overlay_string_index = -1;
3016 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3017 it->end_charpos = it->string_nchars
3018 = XSTRING (it->string)->size;
3019 it->method = next_element_from_string;
3020 it->stop_charpos = 0;
3021 it->string_from_display_prop_p = 1;
3022 /* Say that we haven't consumed the characters with
3023 `display' property yet. The call to pop_it in
3024 set_iterator_to_next will clean this up. */
3025 *position = start_pos;
3026 }
3027 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3028 {
3029 it->method = next_element_from_stretch;
3030 it->object = value;
3031 it->current.pos = it->position = start_pos;
3032 }
3033 #ifdef HAVE_WINDOW_SYSTEM
3034 else
3035 {
3036 it->what = IT_IMAGE;
3037 it->image_id = lookup_image (it->f, value);
3038 it->position = start_pos;
3039 it->object = NILP (object) ? it->w->buffer : object;
3040 it->method = next_element_from_image;
3041
3042 /* Say that we haven't consumed the characters with
3043 `display' property yet. The call to pop_it in
3044 set_iterator_to_next will clean this up. */
3045 *position = start_pos;
3046 }
3047 #endif /* HAVE_WINDOW_SYSTEM */
3048 }
3049 else
3050 /* Invalid property or property not supported. Restore
3051 the position to what it was before. */
3052 *position = start_pos;
3053 }
3054
3055 return replaces_text_display_p;
3056 }
3057
3058
3059 /* Check if PROP is a display sub-property value whose text should be
3060 treated as intangible. */
3061
3062 static int
3063 single_display_prop_intangible_p (prop)
3064 Lisp_Object prop;
3065 {
3066 /* Skip over `when FORM'. */
3067 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3068 {
3069 prop = XCDR (prop);
3070 if (!CONSP (prop))
3071 return 0;
3072 prop = XCDR (prop);
3073 }
3074
3075 if (!CONSP (prop))
3076 return 0;
3077
3078 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3079 we don't need to treat text as intangible. */
3080 if (EQ (XCAR (prop), Qmargin))
3081 {
3082 prop = XCDR (prop);
3083 if (!CONSP (prop))
3084 return 0;
3085
3086 prop = XCDR (prop);
3087 if (!CONSP (prop)
3088 || EQ (XCAR (prop), Qleft_margin)
3089 || EQ (XCAR (prop), Qright_margin))
3090 return 0;
3091 }
3092
3093 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3094 }
3095
3096
3097 /* Check if PROP is a display property value whose text should be
3098 treated as intangible. */
3099
3100 int
3101 display_prop_intangible_p (prop)
3102 Lisp_Object prop;
3103 {
3104 if (CONSP (prop)
3105 && CONSP (XCAR (prop))
3106 && !EQ (Qmargin, XCAR (XCAR (prop))))
3107 {
3108 /* A list of sub-properties. */
3109 while (CONSP (prop))
3110 {
3111 if (single_display_prop_intangible_p (XCAR (prop)))
3112 return 1;
3113 prop = XCDR (prop);
3114 }
3115 }
3116 else if (VECTORP (prop))
3117 {
3118 /* A vector of sub-properties. */
3119 int i;
3120 for (i = 0; i < ASIZE (prop); ++i)
3121 if (single_display_prop_intangible_p (AREF (prop, i)))
3122 return 1;
3123 }
3124 else
3125 return single_display_prop_intangible_p (prop);
3126
3127 return 0;
3128 }
3129
3130
3131 /* Return 1 if PROP is a display sub-property value containing STRING. */
3132
3133 static int
3134 single_display_prop_string_p (prop, string)
3135 Lisp_Object prop, string;
3136 {
3137 extern Lisp_Object Qwhen, Qmargin;
3138
3139 if (EQ (string, prop))
3140 return 1;
3141
3142 /* Skip over `when FORM'. */
3143 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3144 {
3145 prop = XCDR (prop);
3146 if (!CONSP (prop))
3147 return 0;
3148 prop = XCDR (prop);
3149 }
3150
3151 if (CONSP (prop))
3152 /* Skip over `margin LOCATION'. */
3153 if (EQ (XCAR (prop), Qmargin))
3154 {
3155 prop = XCDR (prop);
3156 if (!CONSP (prop))
3157 return 0;
3158
3159 prop = XCDR (prop);
3160 if (!CONSP (prop))
3161 return 0;
3162 }
3163
3164 return CONSP (prop) && EQ (XCAR (prop), string);
3165 }
3166
3167
3168 /* Return 1 if STRING appears in the `display' property PROP. */
3169
3170 static int
3171 display_prop_string_p (prop, string)
3172 Lisp_Object prop, string;
3173 {
3174 extern Lisp_Object Qwhen, Qmargin;
3175
3176 if (CONSP (prop)
3177 && CONSP (XCAR (prop))
3178 && !EQ (Qmargin, XCAR (XCAR (prop))))
3179 {
3180 /* A list of sub-properties. */
3181 while (CONSP (prop))
3182 {
3183 if (single_display_prop_string_p (XCAR (prop), string))
3184 return 1;
3185 prop = XCDR (prop);
3186 }
3187 }
3188 else if (VECTORP (prop))
3189 {
3190 /* A vector of sub-properties. */
3191 int i;
3192 for (i = 0; i < ASIZE (prop); ++i)
3193 if (single_display_prop_string_p (AREF (prop, i), string))
3194 return 1;
3195 }
3196 else
3197 return single_display_prop_string_p (prop, string);
3198
3199 return 0;
3200 }
3201
3202
3203 /* Determine from which buffer position in W's buffer STRING comes
3204 from. AROUND_CHARPOS is an approximate position where it could
3205 be from. Value is the buffer position or 0 if it couldn't be
3206 determined.
3207
3208 W's buffer must be current.
3209
3210 This function is necessary because we don't record buffer positions
3211 in glyphs generated from strings (to keep struct glyph small).
3212 This function may only use code that doesn't eval because it is
3213 called asynchronously from note_mouse_highlight. */
3214
3215 int
3216 string_buffer_position (w, string, around_charpos)
3217 struct window *w;
3218 Lisp_Object string;
3219 int around_charpos;
3220 {
3221 Lisp_Object around = make_number (around_charpos);
3222 Lisp_Object limit, prop, pos;
3223 const int MAX_DISTANCE = 1000;
3224 int found = 0;
3225
3226 pos = make_number (around_charpos);
3227 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3228 while (!found && !EQ (pos, limit))
3229 {
3230 prop = Fget_char_property (pos, Qdisplay, Qnil);
3231 if (!NILP (prop) && display_prop_string_p (prop, string))
3232 found = 1;
3233 else
3234 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3235 }
3236
3237 if (!found)
3238 {
3239 pos = make_number (around_charpos);
3240 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3241 while (!found && !EQ (pos, limit))
3242 {
3243 prop = Fget_char_property (pos, Qdisplay, Qnil);
3244 if (!NILP (prop) && display_prop_string_p (prop, string))
3245 found = 1;
3246 else
3247 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3248 limit);
3249 }
3250 }
3251
3252 return found ? XINT (pos) : 0;
3253 }
3254
3255
3256 \f
3257 /***********************************************************************
3258 `composition' property
3259 ***********************************************************************/
3260
3261 /* Set up iterator IT from `composition' property at its current
3262 position. Called from handle_stop. */
3263
3264 static enum prop_handled
3265 handle_composition_prop (it)
3266 struct it *it;
3267 {
3268 Lisp_Object prop, string;
3269 int pos, pos_byte, end;
3270 enum prop_handled handled = HANDLED_NORMALLY;
3271
3272 if (STRINGP (it->string))
3273 {
3274 pos = IT_STRING_CHARPOS (*it);
3275 pos_byte = IT_STRING_BYTEPOS (*it);
3276 string = it->string;
3277 }
3278 else
3279 {
3280 pos = IT_CHARPOS (*it);
3281 pos_byte = IT_BYTEPOS (*it);
3282 string = Qnil;
3283 }
3284
3285 /* If there's a valid composition and point is not inside of the
3286 composition (in the case that the composition is from the current
3287 buffer), draw a glyph composed from the composition components. */
3288 if (find_composition (pos, -1, &pos, &end, &prop, string)
3289 && COMPOSITION_VALID_P (pos, end, prop)
3290 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3291 {
3292 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3293
3294 if (id >= 0)
3295 {
3296 it->method = next_element_from_composition;
3297 it->cmp_id = id;
3298 it->cmp_len = COMPOSITION_LENGTH (prop);
3299 /* For a terminal, draw only the first character of the
3300 components. */
3301 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3302 it->len = (STRINGP (it->string)
3303 ? string_char_to_byte (it->string, end)
3304 : CHAR_TO_BYTE (end)) - pos_byte;
3305 it->stop_charpos = end;
3306 handled = HANDLED_RETURN;
3307 }
3308 }
3309
3310 return handled;
3311 }
3312
3313
3314 \f
3315 /***********************************************************************
3316 Overlay strings
3317 ***********************************************************************/
3318
3319 /* The following structure is used to record overlay strings for
3320 later sorting in load_overlay_strings. */
3321
3322 struct overlay_entry
3323 {
3324 Lisp_Object overlay;
3325 Lisp_Object string;
3326 int priority;
3327 int after_string_p;
3328 };
3329
3330
3331 /* Set up iterator IT from overlay strings at its current position.
3332 Called from handle_stop. */
3333
3334 static enum prop_handled
3335 handle_overlay_change (it)
3336 struct it *it;
3337 {
3338 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3339 return HANDLED_RECOMPUTE_PROPS;
3340 else
3341 return HANDLED_NORMALLY;
3342 }
3343
3344
3345 /* Set up the next overlay string for delivery by IT, if there is an
3346 overlay string to deliver. Called by set_iterator_to_next when the
3347 end of the current overlay string is reached. If there are more
3348 overlay strings to display, IT->string and
3349 IT->current.overlay_string_index are set appropriately here.
3350 Otherwise IT->string is set to nil. */
3351
3352 static void
3353 next_overlay_string (it)
3354 struct it *it;
3355 {
3356 ++it->current.overlay_string_index;
3357 if (it->current.overlay_string_index == it->n_overlay_strings)
3358 {
3359 /* No more overlay strings. Restore IT's settings to what
3360 they were before overlay strings were processed, and
3361 continue to deliver from current_buffer. */
3362 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3363
3364 pop_it (it);
3365 xassert (it->stop_charpos >= BEGV
3366 && it->stop_charpos <= it->end_charpos);
3367 it->string = Qnil;
3368 it->current.overlay_string_index = -1;
3369 SET_TEXT_POS (it->current.string_pos, -1, -1);
3370 it->n_overlay_strings = 0;
3371 it->method = next_element_from_buffer;
3372
3373 /* If we're at the end of the buffer, record that we have
3374 processed the overlay strings there already, so that
3375 next_element_from_buffer doesn't try it again. */
3376 if (IT_CHARPOS (*it) >= it->end_charpos)
3377 it->overlay_strings_at_end_processed_p = 1;
3378
3379 /* If we have to display `...' for invisible text, set
3380 the iterator up for that. */
3381 if (display_ellipsis_p)
3382 setup_for_ellipsis (it);
3383 }
3384 else
3385 {
3386 /* There are more overlay strings to process. If
3387 IT->current.overlay_string_index has advanced to a position
3388 where we must load IT->overlay_strings with more strings, do
3389 it. */
3390 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3391
3392 if (it->current.overlay_string_index && i == 0)
3393 load_overlay_strings (it, 0);
3394
3395 /* Initialize IT to deliver display elements from the overlay
3396 string. */
3397 it->string = it->overlay_strings[i];
3398 it->multibyte_p = STRING_MULTIBYTE (it->string);
3399 SET_TEXT_POS (it->current.string_pos, 0, 0);
3400 it->method = next_element_from_string;
3401 it->stop_charpos = 0;
3402 }
3403
3404 CHECK_IT (it);
3405 }
3406
3407
3408 /* Compare two overlay_entry structures E1 and E2. Used as a
3409 comparison function for qsort in load_overlay_strings. Overlay
3410 strings for the same position are sorted so that
3411
3412 1. All after-strings come in front of before-strings, except
3413 when they come from the same overlay.
3414
3415 2. Within after-strings, strings are sorted so that overlay strings
3416 from overlays with higher priorities come first.
3417
3418 2. Within before-strings, strings are sorted so that overlay
3419 strings from overlays with higher priorities come last.
3420
3421 Value is analogous to strcmp. */
3422
3423
3424 static int
3425 compare_overlay_entries (e1, e2)
3426 void *e1, *e2;
3427 {
3428 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3429 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3430 int result;
3431
3432 if (entry1->after_string_p != entry2->after_string_p)
3433 {
3434 /* Let after-strings appear in front of before-strings if
3435 they come from different overlays. */
3436 if (EQ (entry1->overlay, entry2->overlay))
3437 result = entry1->after_string_p ? 1 : -1;
3438 else
3439 result = entry1->after_string_p ? -1 : 1;
3440 }
3441 else if (entry1->after_string_p)
3442 /* After-strings sorted in order of decreasing priority. */
3443 result = entry2->priority - entry1->priority;
3444 else
3445 /* Before-strings sorted in order of increasing priority. */
3446 result = entry1->priority - entry2->priority;
3447
3448 return result;
3449 }
3450
3451
3452 /* Load the vector IT->overlay_strings with overlay strings from IT's
3453 current buffer position, or from CHARPOS if that is > 0. Set
3454 IT->n_overlays to the total number of overlay strings found.
3455
3456 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3457 a time. On entry into load_overlay_strings,
3458 IT->current.overlay_string_index gives the number of overlay
3459 strings that have already been loaded by previous calls to this
3460 function.
3461
3462 IT->add_overlay_start contains an additional overlay start
3463 position to consider for taking overlay strings from, if non-zero.
3464 This position comes into play when the overlay has an `invisible'
3465 property, and both before and after-strings. When we've skipped to
3466 the end of the overlay, because of its `invisible' property, we
3467 nevertheless want its before-string to appear.
3468 IT->add_overlay_start will contain the overlay start position
3469 in this case.
3470
3471 Overlay strings are sorted so that after-string strings come in
3472 front of before-string strings. Within before and after-strings,
3473 strings are sorted by overlay priority. See also function
3474 compare_overlay_entries. */
3475
3476 static void
3477 load_overlay_strings (it, charpos)
3478 struct it *it;
3479 int charpos;
3480 {
3481 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3482 Lisp_Object ov, overlay, window, str, invisible;
3483 int start, end;
3484 int size = 20;
3485 int n = 0, i, j, invis_p;
3486 struct overlay_entry *entries
3487 = (struct overlay_entry *) alloca (size * sizeof *entries);
3488
3489 if (charpos <= 0)
3490 charpos = IT_CHARPOS (*it);
3491
3492 /* Append the overlay string STRING of overlay OVERLAY to vector
3493 `entries' which has size `size' and currently contains `n'
3494 elements. AFTER_P non-zero means STRING is an after-string of
3495 OVERLAY. */
3496 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3497 do \
3498 { \
3499 Lisp_Object priority; \
3500 \
3501 if (n == size) \
3502 { \
3503 int new_size = 2 * size; \
3504 struct overlay_entry *old = entries; \
3505 entries = \
3506 (struct overlay_entry *) alloca (new_size \
3507 * sizeof *entries); \
3508 bcopy (old, entries, size * sizeof *entries); \
3509 size = new_size; \
3510 } \
3511 \
3512 entries[n].string = (STRING); \
3513 entries[n].overlay = (OVERLAY); \
3514 priority = Foverlay_get ((OVERLAY), Qpriority); \
3515 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3516 entries[n].after_string_p = (AFTER_P); \
3517 ++n; \
3518 } \
3519 while (0)
3520
3521 /* Process overlay before the overlay center. */
3522 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3523 {
3524 overlay = XCAR (ov);
3525 xassert (OVERLAYP (overlay));
3526 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3527 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3528
3529 if (end < charpos)
3530 break;
3531
3532 /* Skip this overlay if it doesn't start or end at IT's current
3533 position. */
3534 if (end != charpos && start != charpos)
3535 continue;
3536
3537 /* Skip this overlay if it doesn't apply to IT->w. */
3538 window = Foverlay_get (overlay, Qwindow);
3539 if (WINDOWP (window) && XWINDOW (window) != it->w)
3540 continue;
3541
3542 /* If the text ``under'' the overlay is invisible, both before-
3543 and after-strings from this overlay are visible; start and
3544 end position are indistinguishable. */
3545 invisible = Foverlay_get (overlay, Qinvisible);
3546 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3547
3548 /* If overlay has a non-empty before-string, record it. */
3549 if ((start == charpos || (end == charpos && invis_p))
3550 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3551 && XSTRING (str)->size)
3552 RECORD_OVERLAY_STRING (overlay, str, 0);
3553
3554 /* If overlay has a non-empty after-string, record it. */
3555 if ((end == charpos || (start == charpos && invis_p))
3556 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3557 && XSTRING (str)->size)
3558 RECORD_OVERLAY_STRING (overlay, str, 1);
3559 }
3560
3561 /* Process overlays after the overlay center. */
3562 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3563 {
3564 overlay = XCAR (ov);
3565 xassert (OVERLAYP (overlay));
3566 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3567 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3568
3569 if (start > charpos)
3570 break;
3571
3572 /* Skip this overlay if it doesn't start or end at IT's current
3573 position. */
3574 if (end != charpos && start != charpos)
3575 continue;
3576
3577 /* Skip this overlay if it doesn't apply to IT->w. */
3578 window = Foverlay_get (overlay, Qwindow);
3579 if (WINDOWP (window) && XWINDOW (window) != it->w)
3580 continue;
3581
3582 /* If the text ``under'' the overlay is invisible, it has a zero
3583 dimension, and both before- and after-strings apply. */
3584 invisible = Foverlay_get (overlay, Qinvisible);
3585 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3586
3587 /* If overlay has a non-empty before-string, record it. */
3588 if ((start == charpos || (end == charpos && invis_p))
3589 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3590 && XSTRING (str)->size)
3591 RECORD_OVERLAY_STRING (overlay, str, 0);
3592
3593 /* If overlay has a non-empty after-string, record it. */
3594 if ((end == charpos || (start == charpos && invis_p))
3595 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3596 && XSTRING (str)->size)
3597 RECORD_OVERLAY_STRING (overlay, str, 1);
3598 }
3599
3600 #undef RECORD_OVERLAY_STRING
3601
3602 /* Sort entries. */
3603 if (n > 1)
3604 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3605
3606 /* Record the total number of strings to process. */
3607 it->n_overlay_strings = n;
3608
3609 /* IT->current.overlay_string_index is the number of overlay strings
3610 that have already been consumed by IT. Copy some of the
3611 remaining overlay strings to IT->overlay_strings. */
3612 i = 0;
3613 j = it->current.overlay_string_index;
3614 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3615 it->overlay_strings[i++] = entries[j++].string;
3616
3617 CHECK_IT (it);
3618 }
3619
3620
3621 /* Get the first chunk of overlay strings at IT's current buffer
3622 position, or at CHARPOS if that is > 0. Value is non-zero if at
3623 least one overlay string was found. */
3624
3625 static int
3626 get_overlay_strings (it, charpos)
3627 struct it *it;
3628 int charpos;
3629 {
3630 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3631 process. This fills IT->overlay_strings with strings, and sets
3632 IT->n_overlay_strings to the total number of strings to process.
3633 IT->pos.overlay_string_index has to be set temporarily to zero
3634 because load_overlay_strings needs this; it must be set to -1
3635 when no overlay strings are found because a zero value would
3636 indicate a position in the first overlay string. */
3637 it->current.overlay_string_index = 0;
3638 load_overlay_strings (it, charpos);
3639
3640 /* If we found overlay strings, set up IT to deliver display
3641 elements from the first one. Otherwise set up IT to deliver
3642 from current_buffer. */
3643 if (it->n_overlay_strings)
3644 {
3645 /* Make sure we know settings in current_buffer, so that we can
3646 restore meaningful values when we're done with the overlay
3647 strings. */
3648 compute_stop_pos (it);
3649 xassert (it->face_id >= 0);
3650
3651 /* Save IT's settings. They are restored after all overlay
3652 strings have been processed. */
3653 xassert (it->sp == 0);
3654 push_it (it);
3655
3656 /* Set up IT to deliver display elements from the first overlay
3657 string. */
3658 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3659 it->string = it->overlay_strings[0];
3660 it->stop_charpos = 0;
3661 it->end_charpos = XSTRING (it->string)->size;
3662 it->multibyte_p = STRING_MULTIBYTE (it->string);
3663 xassert (STRINGP (it->string));
3664 it->method = next_element_from_string;
3665 }
3666 else
3667 {
3668 it->string = Qnil;
3669 it->current.overlay_string_index = -1;
3670 it->method = next_element_from_buffer;
3671 }
3672
3673 CHECK_IT (it);
3674
3675 /* Value is non-zero if we found at least one overlay string. */
3676 return STRINGP (it->string);
3677 }
3678
3679
3680 \f
3681 /***********************************************************************
3682 Saving and restoring state
3683 ***********************************************************************/
3684
3685 /* Save current settings of IT on IT->stack. Called, for example,
3686 before setting up IT for an overlay string, to be able to restore
3687 IT's settings to what they were after the overlay string has been
3688 processed. */
3689
3690 static void
3691 push_it (it)
3692 struct it *it;
3693 {
3694 struct iterator_stack_entry *p;
3695
3696 xassert (it->sp < 2);
3697 p = it->stack + it->sp;
3698
3699 p->stop_charpos = it->stop_charpos;
3700 xassert (it->face_id >= 0);
3701 p->face_id = it->face_id;
3702 p->string = it->string;
3703 p->pos = it->current;
3704 p->end_charpos = it->end_charpos;
3705 p->string_nchars = it->string_nchars;
3706 p->area = it->area;
3707 p->multibyte_p = it->multibyte_p;
3708 p->space_width = it->space_width;
3709 p->font_height = it->font_height;
3710 p->voffset = it->voffset;
3711 p->string_from_display_prop_p = it->string_from_display_prop_p;
3712 p->display_ellipsis_p = 0;
3713 ++it->sp;
3714 }
3715
3716
3717 /* Restore IT's settings from IT->stack. Called, for example, when no
3718 more overlay strings must be processed, and we return to delivering
3719 display elements from a buffer, or when the end of a string from a
3720 `display' property is reached and we return to delivering display
3721 elements from an overlay string, or from a buffer. */
3722
3723 static void
3724 pop_it (it)
3725 struct it *it;
3726 {
3727 struct iterator_stack_entry *p;
3728
3729 xassert (it->sp > 0);
3730 --it->sp;
3731 p = it->stack + it->sp;
3732 it->stop_charpos = p->stop_charpos;
3733 it->face_id = p->face_id;
3734 it->string = p->string;
3735 it->current = p->pos;
3736 it->end_charpos = p->end_charpos;
3737 it->string_nchars = p->string_nchars;
3738 it->area = p->area;
3739 it->multibyte_p = p->multibyte_p;
3740 it->space_width = p->space_width;
3741 it->font_height = p->font_height;
3742 it->voffset = p->voffset;
3743 it->string_from_display_prop_p = p->string_from_display_prop_p;
3744 }
3745
3746
3747 \f
3748 /***********************************************************************
3749 Moving over lines
3750 ***********************************************************************/
3751
3752 /* Set IT's current position to the previous line start. */
3753
3754 static void
3755 back_to_previous_line_start (it)
3756 struct it *it;
3757 {
3758 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3759 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3760 }
3761
3762
3763 /* Move IT to the next line start.
3764
3765 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3766 we skipped over part of the text (as opposed to moving the iterator
3767 continuously over the text). Otherwise, don't change the value
3768 of *SKIPPED_P.
3769
3770 Newlines may come from buffer text, overlay strings, or strings
3771 displayed via the `display' property. That's the reason we can't
3772 simply use find_next_newline_no_quit.
3773
3774 Note that this function may not skip over invisible text that is so
3775 because of text properties and immediately follows a newline. If
3776 it would, function reseat_at_next_visible_line_start, when called
3777 from set_iterator_to_next, would effectively make invisible
3778 characters following a newline part of the wrong glyph row, which
3779 leads to wrong cursor motion. */
3780
3781 static int
3782 forward_to_next_line_start (it, skipped_p)
3783 struct it *it;
3784 int *skipped_p;
3785 {
3786 int old_selective, newline_found_p, n;
3787 const int MAX_NEWLINE_DISTANCE = 500;
3788
3789 /* If already on a newline, just consume it to avoid unintended
3790 skipping over invisible text below. */
3791 if (it->what == IT_CHARACTER
3792 && it->c == '\n'
3793 && CHARPOS (it->position) == IT_CHARPOS (*it))
3794 {
3795 set_iterator_to_next (it, 0);
3796 it->c = 0;
3797 return 1;
3798 }
3799
3800 /* Don't handle selective display in the following. It's (a)
3801 unnecessary because it's done by the caller, and (b) leads to an
3802 infinite recursion because next_element_from_ellipsis indirectly
3803 calls this function. */
3804 old_selective = it->selective;
3805 it->selective = 0;
3806
3807 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3808 from buffer text. */
3809 for (n = newline_found_p = 0;
3810 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3811 n += STRINGP (it->string) ? 0 : 1)
3812 {
3813 if (!get_next_display_element (it))
3814 break;
3815 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3816 set_iterator_to_next (it, 0);
3817 }
3818
3819 /* If we didn't find a newline near enough, see if we can use a
3820 short-cut. */
3821 if (n == MAX_NEWLINE_DISTANCE)
3822 {
3823 int start = IT_CHARPOS (*it);
3824 int limit = find_next_newline_no_quit (start, 1);
3825 Lisp_Object pos;
3826
3827 xassert (!STRINGP (it->string));
3828
3829 /* If there isn't any `display' property in sight, and no
3830 overlays, we can just use the position of the newline in
3831 buffer text. */
3832 if (it->stop_charpos >= limit
3833 || ((pos = Fnext_single_property_change (make_number (start),
3834 Qdisplay,
3835 Qnil, make_number (limit)),
3836 NILP (pos))
3837 && next_overlay_change (start) == ZV))
3838 {
3839 IT_CHARPOS (*it) = limit;
3840 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3841 *skipped_p = newline_found_p = 1;
3842 }
3843 else
3844 {
3845 while (get_next_display_element (it)
3846 && !newline_found_p)
3847 {
3848 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3849 set_iterator_to_next (it, 0);
3850 }
3851 }
3852 }
3853
3854 it->selective = old_selective;
3855 return newline_found_p;
3856 }
3857
3858
3859 /* Set IT's current position to the previous visible line start. Skip
3860 invisible text that is so either due to text properties or due to
3861 selective display. Caution: this does not change IT->current_x and
3862 IT->hpos. */
3863
3864 static void
3865 back_to_previous_visible_line_start (it)
3866 struct it *it;
3867 {
3868 int visible_p = 0;
3869
3870 /* Go back one newline if not on BEGV already. */
3871 if (IT_CHARPOS (*it) > BEGV)
3872 back_to_previous_line_start (it);
3873
3874 /* Move over lines that are invisible because of selective display
3875 or text properties. */
3876 while (IT_CHARPOS (*it) > BEGV
3877 && !visible_p)
3878 {
3879 visible_p = 1;
3880
3881 /* If selective > 0, then lines indented more than that values
3882 are invisible. */
3883 if (it->selective > 0
3884 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3885 it->selective))
3886 visible_p = 0;
3887 else
3888 {
3889 Lisp_Object prop;
3890
3891 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3892 Qinvisible, it->window);
3893 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3894 visible_p = 0;
3895 }
3896
3897 /* Back one more newline if the current one is invisible. */
3898 if (!visible_p)
3899 back_to_previous_line_start (it);
3900 }
3901
3902 xassert (IT_CHARPOS (*it) >= BEGV);
3903 xassert (IT_CHARPOS (*it) == BEGV
3904 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3905 CHECK_IT (it);
3906 }
3907
3908
3909 /* Reseat iterator IT at the previous visible line start. Skip
3910 invisible text that is so either due to text properties or due to
3911 selective display. At the end, update IT's overlay information,
3912 face information etc. */
3913
3914 static void
3915 reseat_at_previous_visible_line_start (it)
3916 struct it *it;
3917 {
3918 back_to_previous_visible_line_start (it);
3919 reseat (it, it->current.pos, 1);
3920 CHECK_IT (it);
3921 }
3922
3923
3924 /* Reseat iterator IT on the next visible line start in the current
3925 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3926 preceding the line start. Skip over invisible text that is so
3927 because of selective display. Compute faces, overlays etc at the
3928 new position. Note that this function does not skip over text that
3929 is invisible because of text properties. */
3930
3931 static void
3932 reseat_at_next_visible_line_start (it, on_newline_p)
3933 struct it *it;
3934 int on_newline_p;
3935 {
3936 int newline_found_p, skipped_p = 0;
3937
3938 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3939
3940 /* Skip over lines that are invisible because they are indented
3941 more than the value of IT->selective. */
3942 if (it->selective > 0)
3943 while (IT_CHARPOS (*it) < ZV
3944 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3945 it->selective))
3946 {
3947 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3948 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3949 }
3950
3951 /* Position on the newline if that's what's requested. */
3952 if (on_newline_p && newline_found_p)
3953 {
3954 if (STRINGP (it->string))
3955 {
3956 if (IT_STRING_CHARPOS (*it) > 0)
3957 {
3958 --IT_STRING_CHARPOS (*it);
3959 --IT_STRING_BYTEPOS (*it);
3960 }
3961 }
3962 else if (IT_CHARPOS (*it) > BEGV)
3963 {
3964 --IT_CHARPOS (*it);
3965 --IT_BYTEPOS (*it);
3966 reseat (it, it->current.pos, 0);
3967 }
3968 }
3969 else if (skipped_p)
3970 reseat (it, it->current.pos, 0);
3971
3972 CHECK_IT (it);
3973 }
3974
3975
3976 \f
3977 /***********************************************************************
3978 Changing an iterator's position
3979 ***********************************************************************/
3980
3981 /* Change IT's current position to POS in current_buffer. If FORCE_P
3982 is non-zero, always check for text properties at the new position.
3983 Otherwise, text properties are only looked up if POS >=
3984 IT->check_charpos of a property. */
3985
3986 static void
3987 reseat (it, pos, force_p)
3988 struct it *it;
3989 struct text_pos pos;
3990 int force_p;
3991 {
3992 int original_pos = IT_CHARPOS (*it);
3993
3994 reseat_1 (it, pos, 0);
3995
3996 /* Determine where to check text properties. Avoid doing it
3997 where possible because text property lookup is very expensive. */
3998 if (force_p
3999 || CHARPOS (pos) > it->stop_charpos
4000 || CHARPOS (pos) < original_pos)
4001 handle_stop (it);
4002
4003 CHECK_IT (it);
4004 }
4005
4006
4007 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4008 IT->stop_pos to POS, also. */
4009
4010 static void
4011 reseat_1 (it, pos, set_stop_p)
4012 struct it *it;
4013 struct text_pos pos;
4014 int set_stop_p;
4015 {
4016 /* Don't call this function when scanning a C string. */
4017 xassert (it->s == NULL);
4018
4019 /* POS must be a reasonable value. */
4020 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4021
4022 it->current.pos = it->position = pos;
4023 XSETBUFFER (it->object, current_buffer);
4024 it->end_charpos = ZV;
4025 it->dpvec = NULL;
4026 it->current.dpvec_index = -1;
4027 it->current.overlay_string_index = -1;
4028 IT_STRING_CHARPOS (*it) = -1;
4029 IT_STRING_BYTEPOS (*it) = -1;
4030 it->string = Qnil;
4031 it->method = next_element_from_buffer;
4032 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4033 it->sp = 0;
4034 it->face_before_selective_p = 0;
4035
4036 if (set_stop_p)
4037 it->stop_charpos = CHARPOS (pos);
4038 }
4039
4040
4041 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4042 If S is non-null, it is a C string to iterate over. Otherwise,
4043 STRING gives a Lisp string to iterate over.
4044
4045 If PRECISION > 0, don't return more then PRECISION number of
4046 characters from the string.
4047
4048 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4049 characters have been returned. FIELD_WIDTH < 0 means an infinite
4050 field width.
4051
4052 MULTIBYTE = 0 means disable processing of multibyte characters,
4053 MULTIBYTE > 0 means enable it,
4054 MULTIBYTE < 0 means use IT->multibyte_p.
4055
4056 IT must be initialized via a prior call to init_iterator before
4057 calling this function. */
4058
4059 static void
4060 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4061 struct it *it;
4062 unsigned char *s;
4063 Lisp_Object string;
4064 int charpos;
4065 int precision, field_width, multibyte;
4066 {
4067 /* No region in strings. */
4068 it->region_beg_charpos = it->region_end_charpos = -1;
4069
4070 /* No text property checks performed by default, but see below. */
4071 it->stop_charpos = -1;
4072
4073 /* Set iterator position and end position. */
4074 bzero (&it->current, sizeof it->current);
4075 it->current.overlay_string_index = -1;
4076 it->current.dpvec_index = -1;
4077 xassert (charpos >= 0);
4078
4079 /* If STRING is specified, use its multibyteness, otherwise use the
4080 setting of MULTIBYTE, if specified. */
4081 if (multibyte >= 0)
4082 it->multibyte_p = multibyte > 0;
4083
4084 if (s == NULL)
4085 {
4086 xassert (STRINGP (string));
4087 it->string = string;
4088 it->s = NULL;
4089 it->end_charpos = it->string_nchars = XSTRING (string)->size;
4090 it->method = next_element_from_string;
4091 it->current.string_pos = string_pos (charpos, string);
4092 }
4093 else
4094 {
4095 it->s = s;
4096 it->string = Qnil;
4097
4098 /* Note that we use IT->current.pos, not it->current.string_pos,
4099 for displaying C strings. */
4100 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4101 if (it->multibyte_p)
4102 {
4103 it->current.pos = c_string_pos (charpos, s, 1);
4104 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4105 }
4106 else
4107 {
4108 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4109 it->end_charpos = it->string_nchars = strlen (s);
4110 }
4111
4112 it->method = next_element_from_c_string;
4113 }
4114
4115 /* PRECISION > 0 means don't return more than PRECISION characters
4116 from the string. */
4117 if (precision > 0 && it->end_charpos - charpos > precision)
4118 it->end_charpos = it->string_nchars = charpos + precision;
4119
4120 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4121 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4122 FIELD_WIDTH < 0 means infinite field width. This is useful for
4123 padding with `-' at the end of a mode line. */
4124 if (field_width < 0)
4125 field_width = INFINITY;
4126 if (field_width > it->end_charpos - charpos)
4127 it->end_charpos = charpos + field_width;
4128
4129 /* Use the standard display table for displaying strings. */
4130 if (DISP_TABLE_P (Vstandard_display_table))
4131 it->dp = XCHAR_TABLE (Vstandard_display_table);
4132
4133 it->stop_charpos = charpos;
4134 CHECK_IT (it);
4135 }
4136
4137
4138 \f
4139 /***********************************************************************
4140 Iteration
4141 ***********************************************************************/
4142
4143 /* Load IT's display element fields with information about the next
4144 display element from the current position of IT. Value is zero if
4145 end of buffer (or C string) is reached. */
4146
4147 int
4148 get_next_display_element (it)
4149 struct it *it;
4150 {
4151 /* Non-zero means that we found an display element. Zero means that
4152 we hit the end of what we iterate over. Performance note: the
4153 function pointer `method' used here turns out to be faster than
4154 using a sequence of if-statements. */
4155 int success_p = (*it->method) (it);
4156
4157 if (it->what == IT_CHARACTER)
4158 {
4159 /* Map via display table or translate control characters.
4160 IT->c, IT->len etc. have been set to the next character by
4161 the function call above. If we have a display table, and it
4162 contains an entry for IT->c, translate it. Don't do this if
4163 IT->c itself comes from a display table, otherwise we could
4164 end up in an infinite recursion. (An alternative could be to
4165 count the recursion depth of this function and signal an
4166 error when a certain maximum depth is reached.) Is it worth
4167 it? */
4168 if (success_p && it->dpvec == NULL)
4169 {
4170 Lisp_Object dv;
4171
4172 if (it->dp
4173 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4174 VECTORP (dv)))
4175 {
4176 struct Lisp_Vector *v = XVECTOR (dv);
4177
4178 /* Return the first character from the display table
4179 entry, if not empty. If empty, don't display the
4180 current character. */
4181 if (v->size)
4182 {
4183 it->dpvec_char_len = it->len;
4184 it->dpvec = v->contents;
4185 it->dpend = v->contents + v->size;
4186 it->current.dpvec_index = 0;
4187 it->method = next_element_from_display_vector;
4188 success_p = get_next_display_element (it);
4189 }
4190 else
4191 {
4192 set_iterator_to_next (it, 0);
4193 success_p = get_next_display_element (it);
4194 }
4195 }
4196
4197 /* Translate control characters into `\003' or `^C' form.
4198 Control characters coming from a display table entry are
4199 currently not translated because we use IT->dpvec to hold
4200 the translation. This could easily be changed but I
4201 don't believe that it is worth doing.
4202
4203 Non-printable multibyte characters are also translated
4204 octal form. */
4205 else if ((it->c < ' '
4206 && (it->area != TEXT_AREA
4207 || (it->c != '\n' && it->c != '\t')))
4208 || (it->c >= 127
4209 && it->len == 1)
4210 || !CHAR_PRINTABLE_P (it->c))
4211 {
4212 /* IT->c is a control character which must be displayed
4213 either as '\003' or as `^C' where the '\\' and '^'
4214 can be defined in the display table. Fill
4215 IT->ctl_chars with glyphs for what we have to
4216 display. Then, set IT->dpvec to these glyphs. */
4217 GLYPH g;
4218
4219 if (it->c < 128 && it->ctl_arrow_p)
4220 {
4221 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4222 if (it->dp
4223 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4224 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4225 g = XINT (DISP_CTRL_GLYPH (it->dp));
4226 else
4227 g = FAST_MAKE_GLYPH ('^', 0);
4228 XSETINT (it->ctl_chars[0], g);
4229
4230 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4231 XSETINT (it->ctl_chars[1], g);
4232
4233 /* Set up IT->dpvec and return first character from it. */
4234 it->dpvec_char_len = it->len;
4235 it->dpvec = it->ctl_chars;
4236 it->dpend = it->dpvec + 2;
4237 it->current.dpvec_index = 0;
4238 it->method = next_element_from_display_vector;
4239 get_next_display_element (it);
4240 }
4241 else
4242 {
4243 unsigned char str[MAX_MULTIBYTE_LENGTH];
4244 int len;
4245 int i;
4246 GLYPH escape_glyph;
4247
4248 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4249 if (it->dp
4250 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4251 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4252 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4253 else
4254 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4255
4256 if (SINGLE_BYTE_CHAR_P (it->c))
4257 str[0] = it->c, len = 1;
4258 else
4259 {
4260 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4261 if (len < 0)
4262 {
4263 /* It's an invalid character, which
4264 shouldn't happen actually, but due to
4265 bugs it may happen. Let's print the char
4266 as is, there's not much meaningful we can
4267 do with it. */
4268 str[0] = it->c;
4269 str[1] = it->c >> 8;
4270 str[2] = it->c >> 16;
4271 str[3] = it->c >> 24;
4272 len = 4;
4273 }
4274 }
4275
4276 for (i = 0; i < len; i++)
4277 {
4278 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4279 /* Insert three more glyphs into IT->ctl_chars for
4280 the octal display of the character. */
4281 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4282 XSETINT (it->ctl_chars[i * 4 + 1], g);
4283 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4284 XSETINT (it->ctl_chars[i * 4 + 2], g);
4285 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4286 XSETINT (it->ctl_chars[i * 4 + 3], g);
4287 }
4288
4289 /* Set up IT->dpvec and return the first character
4290 from it. */
4291 it->dpvec_char_len = it->len;
4292 it->dpvec = it->ctl_chars;
4293 it->dpend = it->dpvec + len * 4;
4294 it->current.dpvec_index = 0;
4295 it->method = next_element_from_display_vector;
4296 get_next_display_element (it);
4297 }
4298 }
4299 }
4300
4301 /* Adjust face id for a multibyte character. There are no
4302 multibyte character in unibyte text. */
4303 if (it->multibyte_p
4304 && success_p
4305 && FRAME_WINDOW_P (it->f))
4306 {
4307 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4308 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4309 }
4310 }
4311
4312 /* Is this character the last one of a run of characters with
4313 box? If yes, set IT->end_of_box_run_p to 1. */
4314 if (it->face_box_p
4315 && it->s == NULL)
4316 {
4317 int face_id;
4318 struct face *face;
4319
4320 it->end_of_box_run_p
4321 = ((face_id = face_after_it_pos (it),
4322 face_id != it->face_id)
4323 && (face = FACE_FROM_ID (it->f, face_id),
4324 face->box == FACE_NO_BOX));
4325 }
4326
4327 /* Value is 0 if end of buffer or string reached. */
4328 return success_p;
4329 }
4330
4331
4332 /* Move IT to the next display element.
4333
4334 RESEAT_P non-zero means if called on a newline in buffer text,
4335 skip to the next visible line start.
4336
4337 Functions get_next_display_element and set_iterator_to_next are
4338 separate because I find this arrangement easier to handle than a
4339 get_next_display_element function that also increments IT's
4340 position. The way it is we can first look at an iterator's current
4341 display element, decide whether it fits on a line, and if it does,
4342 increment the iterator position. The other way around we probably
4343 would either need a flag indicating whether the iterator has to be
4344 incremented the next time, or we would have to implement a
4345 decrement position function which would not be easy to write. */
4346
4347 void
4348 set_iterator_to_next (it, reseat_p)
4349 struct it *it;
4350 int reseat_p;
4351 {
4352 /* Reset flags indicating start and end of a sequence of characters
4353 with box. Reset them at the start of this function because
4354 moving the iterator to a new position might set them. */
4355 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4356
4357 if (it->method == next_element_from_buffer)
4358 {
4359 /* The current display element of IT is a character from
4360 current_buffer. Advance in the buffer, and maybe skip over
4361 invisible lines that are so because of selective display. */
4362 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4363 reseat_at_next_visible_line_start (it, 0);
4364 else
4365 {
4366 xassert (it->len != 0);
4367 IT_BYTEPOS (*it) += it->len;
4368 IT_CHARPOS (*it) += 1;
4369 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4370 }
4371 }
4372 else if (it->method == next_element_from_composition)
4373 {
4374 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4375 if (STRINGP (it->string))
4376 {
4377 IT_STRING_BYTEPOS (*it) += it->len;
4378 IT_STRING_CHARPOS (*it) += it->cmp_len;
4379 it->method = next_element_from_string;
4380 goto consider_string_end;
4381 }
4382 else
4383 {
4384 IT_BYTEPOS (*it) += it->len;
4385 IT_CHARPOS (*it) += it->cmp_len;
4386 it->method = next_element_from_buffer;
4387 }
4388 }
4389 else if (it->method == next_element_from_c_string)
4390 {
4391 /* Current display element of IT is from a C string. */
4392 IT_BYTEPOS (*it) += it->len;
4393 IT_CHARPOS (*it) += 1;
4394 }
4395 else if (it->method == next_element_from_display_vector)
4396 {
4397 /* Current display element of IT is from a display table entry.
4398 Advance in the display table definition. Reset it to null if
4399 end reached, and continue with characters from buffers/
4400 strings. */
4401 ++it->current.dpvec_index;
4402
4403 /* Restore face of the iterator to what they were before the
4404 display vector entry (these entries may contain faces). */
4405 it->face_id = it->saved_face_id;
4406
4407 if (it->dpvec + it->current.dpvec_index == it->dpend)
4408 {
4409 if (it->s)
4410 it->method = next_element_from_c_string;
4411 else if (STRINGP (it->string))
4412 it->method = next_element_from_string;
4413 else
4414 it->method = next_element_from_buffer;
4415
4416 it->dpvec = NULL;
4417 it->current.dpvec_index = -1;
4418
4419 /* Skip over characters which were displayed via IT->dpvec. */
4420 if (it->dpvec_char_len < 0)
4421 reseat_at_next_visible_line_start (it, 1);
4422 else if (it->dpvec_char_len > 0)
4423 {
4424 it->len = it->dpvec_char_len;
4425 set_iterator_to_next (it, reseat_p);
4426 }
4427 }
4428 }
4429 else if (it->method == next_element_from_string)
4430 {
4431 /* Current display element is a character from a Lisp string. */
4432 xassert (it->s == NULL && STRINGP (it->string));
4433 IT_STRING_BYTEPOS (*it) += it->len;
4434 IT_STRING_CHARPOS (*it) += 1;
4435
4436 consider_string_end:
4437
4438 if (it->current.overlay_string_index >= 0)
4439 {
4440 /* IT->string is an overlay string. Advance to the
4441 next, if there is one. */
4442 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4443 next_overlay_string (it);
4444 }
4445 else
4446 {
4447 /* IT->string is not an overlay string. If we reached
4448 its end, and there is something on IT->stack, proceed
4449 with what is on the stack. This can be either another
4450 string, this time an overlay string, or a buffer. */
4451 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4452 && it->sp > 0)
4453 {
4454 pop_it (it);
4455 if (!STRINGP (it->string))
4456 it->method = next_element_from_buffer;
4457 else
4458 goto consider_string_end;
4459 }
4460 }
4461 }
4462 else if (it->method == next_element_from_image
4463 || it->method == next_element_from_stretch)
4464 {
4465 /* The position etc with which we have to proceed are on
4466 the stack. The position may be at the end of a string,
4467 if the `display' property takes up the whole string. */
4468 pop_it (it);
4469 it->image_id = 0;
4470 if (STRINGP (it->string))
4471 {
4472 it->method = next_element_from_string;
4473 goto consider_string_end;
4474 }
4475 else
4476 it->method = next_element_from_buffer;
4477 }
4478 else
4479 /* There are no other methods defined, so this should be a bug. */
4480 abort ();
4481
4482 xassert (it->method != next_element_from_string
4483 || (STRINGP (it->string)
4484 && IT_STRING_CHARPOS (*it) >= 0));
4485 }
4486
4487
4488 /* Load IT's display element fields with information about the next
4489 display element which comes from a display table entry or from the
4490 result of translating a control character to one of the forms `^C'
4491 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4492
4493 static int
4494 next_element_from_display_vector (it)
4495 struct it *it;
4496 {
4497 /* Precondition. */
4498 xassert (it->dpvec && it->current.dpvec_index >= 0);
4499
4500 /* Remember the current face id in case glyphs specify faces.
4501 IT's face is restored in set_iterator_to_next. */
4502 it->saved_face_id = it->face_id;
4503
4504 if (INTEGERP (*it->dpvec)
4505 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4506 {
4507 int lface_id;
4508 GLYPH g;
4509
4510 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4511 it->c = FAST_GLYPH_CHAR (g);
4512 it->len = CHAR_BYTES (it->c);
4513
4514 /* The entry may contain a face id to use. Such a face id is
4515 the id of a Lisp face, not a realized face. A face id of
4516 zero means no face is specified. */
4517 lface_id = FAST_GLYPH_FACE (g);
4518 if (lface_id)
4519 {
4520 /* The function returns -1 if lface_id is invalid. */
4521 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4522 if (face_id >= 0)
4523 it->face_id = face_id;
4524 }
4525 }
4526 else
4527 /* Display table entry is invalid. Return a space. */
4528 it->c = ' ', it->len = 1;
4529
4530 /* Don't change position and object of the iterator here. They are
4531 still the values of the character that had this display table
4532 entry or was translated, and that's what we want. */
4533 it->what = IT_CHARACTER;
4534 return 1;
4535 }
4536
4537
4538 /* Load IT with the next display element from Lisp string IT->string.
4539 IT->current.string_pos is the current position within the string.
4540 If IT->current.overlay_string_index >= 0, the Lisp string is an
4541 overlay string. */
4542
4543 static int
4544 next_element_from_string (it)
4545 struct it *it;
4546 {
4547 struct text_pos position;
4548
4549 xassert (STRINGP (it->string));
4550 xassert (IT_STRING_CHARPOS (*it) >= 0);
4551 position = it->current.string_pos;
4552
4553 /* Time to check for invisible text? */
4554 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4555 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4556 {
4557 handle_stop (it);
4558
4559 /* Since a handler may have changed IT->method, we must
4560 recurse here. */
4561 return get_next_display_element (it);
4562 }
4563
4564 if (it->current.overlay_string_index >= 0)
4565 {
4566 /* Get the next character from an overlay string. In overlay
4567 strings, There is no field width or padding with spaces to
4568 do. */
4569 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4570 {
4571 it->what = IT_EOB;
4572 return 0;
4573 }
4574 else if (STRING_MULTIBYTE (it->string))
4575 {
4576 int remaining = (STRING_BYTES (XSTRING (it->string))
4577 - IT_STRING_BYTEPOS (*it));
4578 unsigned char *s = (XSTRING (it->string)->data
4579 + IT_STRING_BYTEPOS (*it));
4580 it->c = string_char_and_length (s, remaining, &it->len);
4581 }
4582 else
4583 {
4584 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4585 it->len = 1;
4586 }
4587 }
4588 else
4589 {
4590 /* Get the next character from a Lisp string that is not an
4591 overlay string. Such strings come from the mode line, for
4592 example. We may have to pad with spaces, or truncate the
4593 string. See also next_element_from_c_string. */
4594 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4595 {
4596 it->what = IT_EOB;
4597 return 0;
4598 }
4599 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4600 {
4601 /* Pad with spaces. */
4602 it->c = ' ', it->len = 1;
4603 CHARPOS (position) = BYTEPOS (position) = -1;
4604 }
4605 else if (STRING_MULTIBYTE (it->string))
4606 {
4607 int maxlen = (STRING_BYTES (XSTRING (it->string))
4608 - IT_STRING_BYTEPOS (*it));
4609 unsigned char *s = (XSTRING (it->string)->data
4610 + IT_STRING_BYTEPOS (*it));
4611 it->c = string_char_and_length (s, maxlen, &it->len);
4612 }
4613 else
4614 {
4615 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4616 it->len = 1;
4617 }
4618 }
4619
4620 /* Record what we have and where it came from. Note that we store a
4621 buffer position in IT->position although it could arguably be a
4622 string position. */
4623 it->what = IT_CHARACTER;
4624 it->object = it->string;
4625 it->position = position;
4626 return 1;
4627 }
4628
4629
4630 /* Load IT with next display element from C string IT->s.
4631 IT->string_nchars is the maximum number of characters to return
4632 from the string. IT->end_charpos may be greater than
4633 IT->string_nchars when this function is called, in which case we
4634 may have to return padding spaces. Value is zero if end of string
4635 reached, including padding spaces. */
4636
4637 static int
4638 next_element_from_c_string (it)
4639 struct it *it;
4640 {
4641 int success_p = 1;
4642
4643 xassert (it->s);
4644 it->what = IT_CHARACTER;
4645 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4646 it->object = Qnil;
4647
4648 /* IT's position can be greater IT->string_nchars in case a field
4649 width or precision has been specified when the iterator was
4650 initialized. */
4651 if (IT_CHARPOS (*it) >= it->end_charpos)
4652 {
4653 /* End of the game. */
4654 it->what = IT_EOB;
4655 success_p = 0;
4656 }
4657 else if (IT_CHARPOS (*it) >= it->string_nchars)
4658 {
4659 /* Pad with spaces. */
4660 it->c = ' ', it->len = 1;
4661 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4662 }
4663 else if (it->multibyte_p)
4664 {
4665 /* Implementation note: The calls to strlen apparently aren't a
4666 performance problem because there is no noticeable performance
4667 difference between Emacs running in unibyte or multibyte mode. */
4668 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4669 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4670 maxlen, &it->len);
4671 }
4672 else
4673 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4674
4675 return success_p;
4676 }
4677
4678
4679 /* Set up IT to return characters from an ellipsis, if appropriate.
4680 The definition of the ellipsis glyphs may come from a display table
4681 entry. This function Fills IT with the first glyph from the
4682 ellipsis if an ellipsis is to be displayed. */
4683
4684 static int
4685 next_element_from_ellipsis (it)
4686 struct it *it;
4687 {
4688 if (it->selective_display_ellipsis_p)
4689 {
4690 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4691 {
4692 /* Use the display table definition for `...'. Invalid glyphs
4693 will be handled by the method returning elements from dpvec. */
4694 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4695 it->dpvec_char_len = it->len;
4696 it->dpvec = v->contents;
4697 it->dpend = v->contents + v->size;
4698 it->current.dpvec_index = 0;
4699 it->method = next_element_from_display_vector;
4700 }
4701 else
4702 {
4703 /* Use default `...' which is stored in default_invis_vector. */
4704 it->dpvec_char_len = it->len;
4705 it->dpvec = default_invis_vector;
4706 it->dpend = default_invis_vector + 3;
4707 it->current.dpvec_index = 0;
4708 it->method = next_element_from_display_vector;
4709 }
4710 }
4711 else
4712 {
4713 /* The face at the current position may be different from the
4714 face we find after the invisible text. Remember what it
4715 was in IT->saved_face_id, and signal that it's there by
4716 setting face_before_selective_p. */
4717 it->saved_face_id = it->face_id;
4718 it->method = next_element_from_buffer;
4719 reseat_at_next_visible_line_start (it, 1);
4720 it->face_before_selective_p = 1;
4721 }
4722
4723 return get_next_display_element (it);
4724 }
4725
4726
4727 /* Deliver an image display element. The iterator IT is already
4728 filled with image information (done in handle_display_prop). Value
4729 is always 1. */
4730
4731
4732 static int
4733 next_element_from_image (it)
4734 struct it *it;
4735 {
4736 it->what = IT_IMAGE;
4737 return 1;
4738 }
4739
4740
4741 /* Fill iterator IT with next display element from a stretch glyph
4742 property. IT->object is the value of the text property. Value is
4743 always 1. */
4744
4745 static int
4746 next_element_from_stretch (it)
4747 struct it *it;
4748 {
4749 it->what = IT_STRETCH;
4750 return 1;
4751 }
4752
4753
4754 /* Load IT with the next display element from current_buffer. Value
4755 is zero if end of buffer reached. IT->stop_charpos is the next
4756 position at which to stop and check for text properties or buffer
4757 end. */
4758
4759 static int
4760 next_element_from_buffer (it)
4761 struct it *it;
4762 {
4763 int success_p = 1;
4764
4765 /* Check this assumption, otherwise, we would never enter the
4766 if-statement, below. */
4767 xassert (IT_CHARPOS (*it) >= BEGV
4768 && IT_CHARPOS (*it) <= it->stop_charpos);
4769
4770 if (IT_CHARPOS (*it) >= it->stop_charpos)
4771 {
4772 if (IT_CHARPOS (*it) >= it->end_charpos)
4773 {
4774 int overlay_strings_follow_p;
4775
4776 /* End of the game, except when overlay strings follow that
4777 haven't been returned yet. */
4778 if (it->overlay_strings_at_end_processed_p)
4779 overlay_strings_follow_p = 0;
4780 else
4781 {
4782 it->overlay_strings_at_end_processed_p = 1;
4783 overlay_strings_follow_p = get_overlay_strings (it, 0);
4784 }
4785
4786 if (overlay_strings_follow_p)
4787 success_p = get_next_display_element (it);
4788 else
4789 {
4790 it->what = IT_EOB;
4791 it->position = it->current.pos;
4792 success_p = 0;
4793 }
4794 }
4795 else
4796 {
4797 handle_stop (it);
4798 return get_next_display_element (it);
4799 }
4800 }
4801 else
4802 {
4803 /* No face changes, overlays etc. in sight, so just return a
4804 character from current_buffer. */
4805 unsigned char *p;
4806
4807 /* Maybe run the redisplay end trigger hook. Performance note:
4808 This doesn't seem to cost measurable time. */
4809 if (it->redisplay_end_trigger_charpos
4810 && it->glyph_row
4811 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4812 run_redisplay_end_trigger_hook (it);
4813
4814 /* Get the next character, maybe multibyte. */
4815 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4816 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4817 {
4818 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4819 - IT_BYTEPOS (*it));
4820 it->c = string_char_and_length (p, maxlen, &it->len);
4821 }
4822 else
4823 it->c = *p, it->len = 1;
4824
4825 /* Record what we have and where it came from. */
4826 it->what = IT_CHARACTER;;
4827 it->object = it->w->buffer;
4828 it->position = it->current.pos;
4829
4830 /* Normally we return the character found above, except when we
4831 really want to return an ellipsis for selective display. */
4832 if (it->selective)
4833 {
4834 if (it->c == '\n')
4835 {
4836 /* A value of selective > 0 means hide lines indented more
4837 than that number of columns. */
4838 if (it->selective > 0
4839 && IT_CHARPOS (*it) + 1 < ZV
4840 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4841 IT_BYTEPOS (*it) + 1,
4842 it->selective))
4843 {
4844 success_p = next_element_from_ellipsis (it);
4845 it->dpvec_char_len = -1;
4846 }
4847 }
4848 else if (it->c == '\r' && it->selective == -1)
4849 {
4850 /* A value of selective == -1 means that everything from the
4851 CR to the end of the line is invisible, with maybe an
4852 ellipsis displayed for it. */
4853 success_p = next_element_from_ellipsis (it);
4854 it->dpvec_char_len = -1;
4855 }
4856 }
4857 }
4858
4859 /* Value is zero if end of buffer reached. */
4860 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4861 return success_p;
4862 }
4863
4864
4865 /* Run the redisplay end trigger hook for IT. */
4866
4867 static void
4868 run_redisplay_end_trigger_hook (it)
4869 struct it *it;
4870 {
4871 Lisp_Object args[3];
4872
4873 /* IT->glyph_row should be non-null, i.e. we should be actually
4874 displaying something, or otherwise we should not run the hook. */
4875 xassert (it->glyph_row);
4876
4877 /* Set up hook arguments. */
4878 args[0] = Qredisplay_end_trigger_functions;
4879 args[1] = it->window;
4880 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4881 it->redisplay_end_trigger_charpos = 0;
4882
4883 /* Since we are *trying* to run these functions, don't try to run
4884 them again, even if they get an error. */
4885 it->w->redisplay_end_trigger = Qnil;
4886 Frun_hook_with_args (3, args);
4887
4888 /* Notice if it changed the face of the character we are on. */
4889 handle_face_prop (it);
4890 }
4891
4892
4893 /* Deliver a composition display element. The iterator IT is already
4894 filled with composition information (done in
4895 handle_composition_prop). Value is always 1. */
4896
4897 static int
4898 next_element_from_composition (it)
4899 struct it *it;
4900 {
4901 it->what = IT_COMPOSITION;
4902 it->position = (STRINGP (it->string)
4903 ? it->current.string_pos
4904 : it->current.pos);
4905 return 1;
4906 }
4907
4908
4909 \f
4910 /***********************************************************************
4911 Moving an iterator without producing glyphs
4912 ***********************************************************************/
4913
4914 /* Move iterator IT to a specified buffer or X position within one
4915 line on the display without producing glyphs.
4916
4917 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4918 whichever is reached first.
4919
4920 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4921
4922 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4923 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4924 TO_X includes the amount by which a window is horizontally
4925 scrolled.
4926
4927 Value is
4928
4929 MOVE_POS_MATCH_OR_ZV
4930 - when TO_POS or ZV was reached.
4931
4932 MOVE_X_REACHED
4933 -when TO_X was reached before TO_POS or ZV were reached.
4934
4935 MOVE_LINE_CONTINUED
4936 - when we reached the end of the display area and the line must
4937 be continued.
4938
4939 MOVE_LINE_TRUNCATED
4940 - when we reached the end of the display area and the line is
4941 truncated.
4942
4943 MOVE_NEWLINE_OR_CR
4944 - when we stopped at a line end, i.e. a newline or a CR and selective
4945 display is on. */
4946
4947 static enum move_it_result
4948 move_it_in_display_line_to (it, to_charpos, to_x, op)
4949 struct it *it;
4950 int to_charpos, to_x, op;
4951 {
4952 enum move_it_result result = MOVE_UNDEFINED;
4953 struct glyph_row *saved_glyph_row;
4954
4955 /* Don't produce glyphs in produce_glyphs. */
4956 saved_glyph_row = it->glyph_row;
4957 it->glyph_row = NULL;
4958
4959 while (1)
4960 {
4961 int x, i, ascent = 0, descent = 0;
4962
4963 /* Stop when ZV or TO_CHARPOS reached. */
4964 if (!get_next_display_element (it)
4965 || ((op & MOVE_TO_POS) != 0
4966 && BUFFERP (it->object)
4967 && IT_CHARPOS (*it) >= to_charpos))
4968 {
4969 result = MOVE_POS_MATCH_OR_ZV;
4970 break;
4971 }
4972
4973 /* The call to produce_glyphs will get the metrics of the
4974 display element IT is loaded with. We record in x the
4975 x-position before this display element in case it does not
4976 fit on the line. */
4977 x = it->current_x;
4978
4979 /* Remember the line height so far in case the next element doesn't
4980 fit on the line. */
4981 if (!it->truncate_lines_p)
4982 {
4983 ascent = it->max_ascent;
4984 descent = it->max_descent;
4985 }
4986
4987 PRODUCE_GLYPHS (it);
4988
4989 if (it->area != TEXT_AREA)
4990 {
4991 set_iterator_to_next (it, 1);
4992 continue;
4993 }
4994
4995 /* The number of glyphs we get back in IT->nglyphs will normally
4996 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4997 character on a terminal frame, or (iii) a line end. For the
4998 second case, IT->nglyphs - 1 padding glyphs will be present
4999 (on X frames, there is only one glyph produced for a
5000 composite character.
5001
5002 The behavior implemented below means, for continuation lines,
5003 that as many spaces of a TAB as fit on the current line are
5004 displayed there. For terminal frames, as many glyphs of a
5005 multi-glyph character are displayed in the current line, too.
5006 This is what the old redisplay code did, and we keep it that
5007 way. Under X, the whole shape of a complex character must
5008 fit on the line or it will be completely displayed in the
5009 next line.
5010
5011 Note that both for tabs and padding glyphs, all glyphs have
5012 the same width. */
5013 if (it->nglyphs)
5014 {
5015 /* More than one glyph or glyph doesn't fit on line. All
5016 glyphs have the same width. */
5017 int single_glyph_width = it->pixel_width / it->nglyphs;
5018 int new_x;
5019
5020 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5021 {
5022 new_x = x + single_glyph_width;
5023
5024 /* We want to leave anything reaching TO_X to the caller. */
5025 if ((op & MOVE_TO_X) && new_x > to_x)
5026 {
5027 it->current_x = x;
5028 result = MOVE_X_REACHED;
5029 break;
5030 }
5031 else if (/* Lines are continued. */
5032 !it->truncate_lines_p
5033 && (/* And glyph doesn't fit on the line. */
5034 new_x > it->last_visible_x
5035 /* Or it fits exactly and we're on a window
5036 system frame. */
5037 || (new_x == it->last_visible_x
5038 && FRAME_WINDOW_P (it->f))))
5039 {
5040 if (/* IT->hpos == 0 means the very first glyph
5041 doesn't fit on the line, e.g. a wide image. */
5042 it->hpos == 0
5043 || (new_x == it->last_visible_x
5044 && FRAME_WINDOW_P (it->f)))
5045 {
5046 ++it->hpos;
5047 it->current_x = new_x;
5048 if (i == it->nglyphs - 1)
5049 set_iterator_to_next (it, 1);
5050 }
5051 else
5052 {
5053 it->current_x = x;
5054 it->max_ascent = ascent;
5055 it->max_descent = descent;
5056 }
5057
5058 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5059 IT_CHARPOS (*it)));
5060 result = MOVE_LINE_CONTINUED;
5061 break;
5062 }
5063 else if (new_x > it->first_visible_x)
5064 {
5065 /* Glyph is visible. Increment number of glyphs that
5066 would be displayed. */
5067 ++it->hpos;
5068 }
5069 else
5070 {
5071 /* Glyph is completely off the left margin of the display
5072 area. Nothing to do. */
5073 }
5074 }
5075
5076 if (result != MOVE_UNDEFINED)
5077 break;
5078 }
5079 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5080 {
5081 /* Stop when TO_X specified and reached. This check is
5082 necessary here because of lines consisting of a line end,
5083 only. The line end will not produce any glyphs and we
5084 would never get MOVE_X_REACHED. */
5085 xassert (it->nglyphs == 0);
5086 result = MOVE_X_REACHED;
5087 break;
5088 }
5089
5090 /* Is this a line end? If yes, we're done. */
5091 if (ITERATOR_AT_END_OF_LINE_P (it))
5092 {
5093 result = MOVE_NEWLINE_OR_CR;
5094 break;
5095 }
5096
5097 /* The current display element has been consumed. Advance
5098 to the next. */
5099 set_iterator_to_next (it, 1);
5100
5101 /* Stop if lines are truncated and IT's current x-position is
5102 past the right edge of the window now. */
5103 if (it->truncate_lines_p
5104 && it->current_x >= it->last_visible_x)
5105 {
5106 result = MOVE_LINE_TRUNCATED;
5107 break;
5108 }
5109 }
5110
5111 /* Restore the iterator settings altered at the beginning of this
5112 function. */
5113 it->glyph_row = saved_glyph_row;
5114 return result;
5115 }
5116
5117
5118 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5119 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5120 the description of enum move_operation_enum.
5121
5122 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5123 screen line, this function will set IT to the next position >
5124 TO_CHARPOS. */
5125
5126 void
5127 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5128 struct it *it;
5129 int to_charpos, to_x, to_y, to_vpos;
5130 int op;
5131 {
5132 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5133 int line_height;
5134 int reached = 0;
5135
5136 for (;;)
5137 {
5138 if (op & MOVE_TO_VPOS)
5139 {
5140 /* If no TO_CHARPOS and no TO_X specified, stop at the
5141 start of the line TO_VPOS. */
5142 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5143 {
5144 if (it->vpos == to_vpos)
5145 {
5146 reached = 1;
5147 break;
5148 }
5149 else
5150 skip = move_it_in_display_line_to (it, -1, -1, 0);
5151 }
5152 else
5153 {
5154 /* TO_VPOS >= 0 means stop at TO_X in the line at
5155 TO_VPOS, or at TO_POS, whichever comes first. */
5156 if (it->vpos == to_vpos)
5157 {
5158 reached = 2;
5159 break;
5160 }
5161
5162 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5163
5164 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5165 {
5166 reached = 3;
5167 break;
5168 }
5169 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5170 {
5171 /* We have reached TO_X but not in the line we want. */
5172 skip = move_it_in_display_line_to (it, to_charpos,
5173 -1, MOVE_TO_POS);
5174 if (skip == MOVE_POS_MATCH_OR_ZV)
5175 {
5176 reached = 4;
5177 break;
5178 }
5179 }
5180 }
5181 }
5182 else if (op & MOVE_TO_Y)
5183 {
5184 struct it it_backup;
5185
5186 /* TO_Y specified means stop at TO_X in the line containing
5187 TO_Y---or at TO_CHARPOS if this is reached first. The
5188 problem is that we can't really tell whether the line
5189 contains TO_Y before we have completely scanned it, and
5190 this may skip past TO_X. What we do is to first scan to
5191 TO_X.
5192
5193 If TO_X is not specified, use a TO_X of zero. The reason
5194 is to make the outcome of this function more predictable.
5195 If we didn't use TO_X == 0, we would stop at the end of
5196 the line which is probably not what a caller would expect
5197 to happen. */
5198 skip = move_it_in_display_line_to (it, to_charpos,
5199 ((op & MOVE_TO_X)
5200 ? to_x : 0),
5201 (MOVE_TO_X
5202 | (op & MOVE_TO_POS)));
5203
5204 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5205 if (skip == MOVE_POS_MATCH_OR_ZV)
5206 {
5207 reached = 5;
5208 break;
5209 }
5210
5211 /* If TO_X was reached, we would like to know whether TO_Y
5212 is in the line. This can only be said if we know the
5213 total line height which requires us to scan the rest of
5214 the line. */
5215 if (skip == MOVE_X_REACHED)
5216 {
5217 it_backup = *it;
5218 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5219 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5220 op & MOVE_TO_POS);
5221 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5222 }
5223
5224 /* Now, decide whether TO_Y is in this line. */
5225 line_height = it->max_ascent + it->max_descent;
5226 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5227
5228 if (to_y >= it->current_y
5229 && to_y < it->current_y + line_height)
5230 {
5231 if (skip == MOVE_X_REACHED)
5232 /* If TO_Y is in this line and TO_X was reached above,
5233 we scanned too far. We have to restore IT's settings
5234 to the ones before skipping. */
5235 *it = it_backup;
5236 reached = 6;
5237 }
5238 else if (skip == MOVE_X_REACHED)
5239 {
5240 skip = skip2;
5241 if (skip == MOVE_POS_MATCH_OR_ZV)
5242 reached = 7;
5243 }
5244
5245 if (reached)
5246 break;
5247 }
5248 else
5249 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5250
5251 switch (skip)
5252 {
5253 case MOVE_POS_MATCH_OR_ZV:
5254 reached = 8;
5255 goto out;
5256
5257 case MOVE_NEWLINE_OR_CR:
5258 set_iterator_to_next (it, 1);
5259 it->continuation_lines_width = 0;
5260 break;
5261
5262 case MOVE_LINE_TRUNCATED:
5263 it->continuation_lines_width = 0;
5264 reseat_at_next_visible_line_start (it, 0);
5265 if ((op & MOVE_TO_POS) != 0
5266 && IT_CHARPOS (*it) > to_charpos)
5267 {
5268 reached = 9;
5269 goto out;
5270 }
5271 break;
5272
5273 case MOVE_LINE_CONTINUED:
5274 it->continuation_lines_width += it->current_x;
5275 break;
5276
5277 default:
5278 abort ();
5279 }
5280
5281 /* Reset/increment for the next run. */
5282 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5283 it->current_x = it->hpos = 0;
5284 it->current_y += it->max_ascent + it->max_descent;
5285 ++it->vpos;
5286 last_height = it->max_ascent + it->max_descent;
5287 last_max_ascent = it->max_ascent;
5288 it->max_ascent = it->max_descent = 0;
5289 }
5290
5291 out:
5292
5293 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5294 }
5295
5296
5297 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5298
5299 If DY > 0, move IT backward at least that many pixels. DY = 0
5300 means move IT backward to the preceding line start or BEGV. This
5301 function may move over more than DY pixels if IT->current_y - DY
5302 ends up in the middle of a line; in this case IT->current_y will be
5303 set to the top of the line moved to. */
5304
5305 void
5306 move_it_vertically_backward (it, dy)
5307 struct it *it;
5308 int dy;
5309 {
5310 int nlines, h, line_height;
5311 struct it it2;
5312 int start_pos = IT_CHARPOS (*it);
5313
5314 xassert (dy >= 0);
5315
5316 /* Estimate how many newlines we must move back. */
5317 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5318
5319 /* Set the iterator's position that many lines back. */
5320 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5321 back_to_previous_visible_line_start (it);
5322
5323 /* Reseat the iterator here. When moving backward, we don't want
5324 reseat to skip forward over invisible text, set up the iterator
5325 to deliver from overlay strings at the new position etc. So,
5326 use reseat_1 here. */
5327 reseat_1 (it, it->current.pos, 1);
5328
5329 /* We are now surely at a line start. */
5330 it->current_x = it->hpos = 0;
5331
5332 /* Move forward and see what y-distance we moved. First move to the
5333 start of the next line so that we get its height. We need this
5334 height to be able to tell whether we reached the specified
5335 y-distance. */
5336 it2 = *it;
5337 it2.max_ascent = it2.max_descent = 0;
5338 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5339 MOVE_TO_POS | MOVE_TO_VPOS);
5340 xassert (IT_CHARPOS (*it) >= BEGV);
5341 line_height = it2.max_ascent + it2.max_descent;
5342
5343 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5344 xassert (IT_CHARPOS (*it) >= BEGV);
5345 h = it2.current_y - it->current_y;
5346 nlines = it2.vpos - it->vpos;
5347
5348 /* Correct IT's y and vpos position. */
5349 it->vpos -= nlines;
5350 it->current_y -= h;
5351
5352 if (dy == 0)
5353 {
5354 /* DY == 0 means move to the start of the screen line. The
5355 value of nlines is > 0 if continuation lines were involved. */
5356 if (nlines > 0)
5357 move_it_by_lines (it, nlines, 1);
5358 xassert (IT_CHARPOS (*it) <= start_pos);
5359 }
5360 else if (nlines)
5361 {
5362 /* The y-position we try to reach. Note that h has been
5363 subtracted in front of the if-statement. */
5364 int target_y = it->current_y + h - dy;
5365
5366 /* If we did not reach target_y, try to move further backward if
5367 we can. If we moved too far backward, try to move forward. */
5368 if (target_y < it->current_y
5369 && IT_CHARPOS (*it) > BEGV)
5370 {
5371 move_it_vertically (it, target_y - it->current_y);
5372 xassert (IT_CHARPOS (*it) >= BEGV);
5373 }
5374 else if (target_y >= it->current_y + line_height
5375 && IT_CHARPOS (*it) < ZV)
5376 {
5377 move_it_vertically (it, target_y - (it->current_y + line_height));
5378 xassert (IT_CHARPOS (*it) >= BEGV);
5379 }
5380 }
5381 }
5382
5383
5384 /* Move IT by a specified amount of pixel lines DY. DY negative means
5385 move backwards. DY = 0 means move to start of screen line. At the
5386 end, IT will be on the start of a screen line. */
5387
5388 void
5389 move_it_vertically (it, dy)
5390 struct it *it;
5391 int dy;
5392 {
5393 if (dy <= 0)
5394 move_it_vertically_backward (it, -dy);
5395 else if (dy > 0)
5396 {
5397 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5398 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5399 MOVE_TO_POS | MOVE_TO_Y);
5400 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5401
5402 /* If buffer ends in ZV without a newline, move to the start of
5403 the line to satisfy the post-condition. */
5404 if (IT_CHARPOS (*it) == ZV
5405 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5406 move_it_by_lines (it, 0, 0);
5407 }
5408 }
5409
5410
5411 /* Move iterator IT past the end of the text line it is in. */
5412
5413 void
5414 move_it_past_eol (it)
5415 struct it *it;
5416 {
5417 enum move_it_result rc;
5418
5419 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5420 if (rc == MOVE_NEWLINE_OR_CR)
5421 set_iterator_to_next (it, 0);
5422 }
5423
5424
5425 #if 0 /* Currently not used. */
5426
5427 /* Return non-zero if some text between buffer positions START_CHARPOS
5428 and END_CHARPOS is invisible. IT->window is the window for text
5429 property lookup. */
5430
5431 static int
5432 invisible_text_between_p (it, start_charpos, end_charpos)
5433 struct it *it;
5434 int start_charpos, end_charpos;
5435 {
5436 Lisp_Object prop, limit;
5437 int invisible_found_p;
5438
5439 xassert (it != NULL && start_charpos <= end_charpos);
5440
5441 /* Is text at START invisible? */
5442 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5443 it->window);
5444 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5445 invisible_found_p = 1;
5446 else
5447 {
5448 limit = Fnext_single_char_property_change (make_number (start_charpos),
5449 Qinvisible, Qnil,
5450 make_number (end_charpos));
5451 invisible_found_p = XFASTINT (limit) < end_charpos;
5452 }
5453
5454 return invisible_found_p;
5455 }
5456
5457 #endif /* 0 */
5458
5459
5460 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5461 negative means move up. DVPOS == 0 means move to the start of the
5462 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5463 NEED_Y_P is zero, IT->current_y will be left unchanged.
5464
5465 Further optimization ideas: If we would know that IT->f doesn't use
5466 a face with proportional font, we could be faster for
5467 truncate-lines nil. */
5468
5469 void
5470 move_it_by_lines (it, dvpos, need_y_p)
5471 struct it *it;
5472 int dvpos, need_y_p;
5473 {
5474 struct position pos;
5475
5476 if (!FRAME_WINDOW_P (it->f))
5477 {
5478 struct text_pos textpos;
5479
5480 /* We can use vmotion on frames without proportional fonts. */
5481 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5482 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5483 reseat (it, textpos, 1);
5484 it->vpos += pos.vpos;
5485 it->current_y += pos.vpos;
5486 }
5487 else if (dvpos == 0)
5488 {
5489 /* DVPOS == 0 means move to the start of the screen line. */
5490 move_it_vertically_backward (it, 0);
5491 xassert (it->current_x == 0 && it->hpos == 0);
5492 }
5493 else if (dvpos > 0)
5494 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5495 else
5496 {
5497 struct it it2;
5498 int start_charpos, i;
5499
5500 /* Start at the beginning of the screen line containing IT's
5501 position. */
5502 move_it_vertically_backward (it, 0);
5503
5504 /* Go back -DVPOS visible lines and reseat the iterator there. */
5505 start_charpos = IT_CHARPOS (*it);
5506 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5507 back_to_previous_visible_line_start (it);
5508 reseat (it, it->current.pos, 1);
5509 it->current_x = it->hpos = 0;
5510
5511 /* Above call may have moved too far if continuation lines
5512 are involved. Scan forward and see if it did. */
5513 it2 = *it;
5514 it2.vpos = it2.current_y = 0;
5515 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5516 it->vpos -= it2.vpos;
5517 it->current_y -= it2.current_y;
5518 it->current_x = it->hpos = 0;
5519
5520 /* If we moved too far, move IT some lines forward. */
5521 if (it2.vpos > -dvpos)
5522 {
5523 int delta = it2.vpos + dvpos;
5524 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5525 }
5526 }
5527 }
5528
5529
5530 \f
5531 /***********************************************************************
5532 Messages
5533 ***********************************************************************/
5534
5535
5536 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5537 to *Messages*. */
5538
5539 void
5540 add_to_log (format, arg1, arg2)
5541 char *format;
5542 Lisp_Object arg1, arg2;
5543 {
5544 Lisp_Object args[3];
5545 Lisp_Object msg, fmt;
5546 char *buffer;
5547 int len;
5548 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5549
5550 /* Do nothing if called asynchronously. Inserting text into
5551 a buffer may call after-change-functions and alike and
5552 that would means running Lisp asynchronously. */
5553 if (handling_signal)
5554 return;
5555
5556 fmt = msg = Qnil;
5557 GCPRO4 (fmt, msg, arg1, arg2);
5558
5559 args[0] = fmt = build_string (format);
5560 args[1] = arg1;
5561 args[2] = arg2;
5562 msg = Fformat (3, args);
5563
5564 len = STRING_BYTES (XSTRING (msg)) + 1;
5565 buffer = (char *) alloca (len);
5566 bcopy (XSTRING (msg)->data, buffer, len);
5567
5568 message_dolog (buffer, len - 1, 1, 0);
5569 UNGCPRO;
5570 }
5571
5572
5573 /* Output a newline in the *Messages* buffer if "needs" one. */
5574
5575 void
5576 message_log_maybe_newline ()
5577 {
5578 if (message_log_need_newline)
5579 message_dolog ("", 0, 1, 0);
5580 }
5581
5582
5583 /* Add a string M of length NBYTES to the message log, optionally
5584 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5585 nonzero, means interpret the contents of M as multibyte. This
5586 function calls low-level routines in order to bypass text property
5587 hooks, etc. which might not be safe to run. */
5588
5589 void
5590 message_dolog (m, nbytes, nlflag, multibyte)
5591 char *m;
5592 int nbytes, nlflag, multibyte;
5593 {
5594 if (!NILP (Vmessage_log_max))
5595 {
5596 struct buffer *oldbuf;
5597 Lisp_Object oldpoint, oldbegv, oldzv;
5598 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5599 int point_at_end = 0;
5600 int zv_at_end = 0;
5601 Lisp_Object old_deactivate_mark, tem;
5602 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5603
5604 old_deactivate_mark = Vdeactivate_mark;
5605 oldbuf = current_buffer;
5606 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5607 current_buffer->undo_list = Qt;
5608
5609 oldpoint = Fpoint_marker ();
5610 oldbegv = Fpoint_min_marker ();
5611 oldzv = Fpoint_max_marker ();
5612 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5613
5614 if (PT == Z)
5615 point_at_end = 1;
5616 if (ZV == Z)
5617 zv_at_end = 1;
5618
5619 BEGV = BEG;
5620 BEGV_BYTE = BEG_BYTE;
5621 ZV = Z;
5622 ZV_BYTE = Z_BYTE;
5623 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5624
5625 /* Insert the string--maybe converting multibyte to single byte
5626 or vice versa, so that all the text fits the buffer. */
5627 if (multibyte
5628 && NILP (current_buffer->enable_multibyte_characters))
5629 {
5630 int i, c, char_bytes;
5631 unsigned char work[1];
5632
5633 /* Convert a multibyte string to single-byte
5634 for the *Message* buffer. */
5635 for (i = 0; i < nbytes; i += nbytes)
5636 {
5637 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5638 work[0] = (SINGLE_BYTE_CHAR_P (c)
5639 ? c
5640 : multibyte_char_to_unibyte (c, Qnil));
5641 insert_1_both (work, 1, 1, 1, 0, 0);
5642 }
5643 }
5644 else if (! multibyte
5645 && ! NILP (current_buffer->enable_multibyte_characters))
5646 {
5647 int i, c, char_bytes;
5648 unsigned char *msg = (unsigned char *) m;
5649 unsigned char str[MAX_MULTIBYTE_LENGTH];
5650 /* Convert a single-byte string to multibyte
5651 for the *Message* buffer. */
5652 for (i = 0; i < nbytes; i++)
5653 {
5654 c = unibyte_char_to_multibyte (msg[i]);
5655 char_bytes = CHAR_STRING (c, str);
5656 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5657 }
5658 }
5659 else if (nbytes)
5660 insert_1 (m, nbytes, 1, 0, 0);
5661
5662 if (nlflag)
5663 {
5664 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5665 insert_1 ("\n", 1, 1, 0, 0);
5666
5667 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5668 this_bol = PT;
5669 this_bol_byte = PT_BYTE;
5670
5671 if (this_bol > BEG)
5672 {
5673 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5674 prev_bol = PT;
5675 prev_bol_byte = PT_BYTE;
5676
5677 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5678 this_bol, this_bol_byte);
5679 if (dup)
5680 {
5681 del_range_both (prev_bol, prev_bol_byte,
5682 this_bol, this_bol_byte, 0);
5683 if (dup > 1)
5684 {
5685 char dupstr[40];
5686 int duplen;
5687
5688 /* If you change this format, don't forget to also
5689 change message_log_check_duplicate. */
5690 sprintf (dupstr, " [%d times]", dup);
5691 duplen = strlen (dupstr);
5692 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5693 insert_1 (dupstr, duplen, 1, 0, 1);
5694 }
5695 }
5696 }
5697
5698 if (NATNUMP (Vmessage_log_max))
5699 {
5700 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5701 -XFASTINT (Vmessage_log_max) - 1, 0);
5702 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5703 }
5704 }
5705 BEGV = XMARKER (oldbegv)->charpos;
5706 BEGV_BYTE = marker_byte_position (oldbegv);
5707
5708 if (zv_at_end)
5709 {
5710 ZV = Z;
5711 ZV_BYTE = Z_BYTE;
5712 }
5713 else
5714 {
5715 ZV = XMARKER (oldzv)->charpos;
5716 ZV_BYTE = marker_byte_position (oldzv);
5717 }
5718
5719 if (point_at_end)
5720 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5721 else
5722 /* We can't do Fgoto_char (oldpoint) because it will run some
5723 Lisp code. */
5724 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5725 XMARKER (oldpoint)->bytepos);
5726
5727 UNGCPRO;
5728 free_marker (oldpoint);
5729 free_marker (oldbegv);
5730 free_marker (oldzv);
5731
5732 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5733 set_buffer_internal (oldbuf);
5734 if (NILP (tem))
5735 windows_or_buffers_changed = old_windows_or_buffers_changed;
5736 message_log_need_newline = !nlflag;
5737 Vdeactivate_mark = old_deactivate_mark;
5738 }
5739 }
5740
5741
5742 /* We are at the end of the buffer after just having inserted a newline.
5743 (Note: We depend on the fact we won't be crossing the gap.)
5744 Check to see if the most recent message looks a lot like the previous one.
5745 Return 0 if different, 1 if the new one should just replace it, or a
5746 value N > 1 if we should also append " [N times]". */
5747
5748 static int
5749 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5750 int prev_bol, this_bol;
5751 int prev_bol_byte, this_bol_byte;
5752 {
5753 int i;
5754 int len = Z_BYTE - 1 - this_bol_byte;
5755 int seen_dots = 0;
5756 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5757 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5758
5759 for (i = 0; i < len; i++)
5760 {
5761 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5762 seen_dots = 1;
5763 if (p1[i] != p2[i])
5764 return seen_dots;
5765 }
5766 p1 += len;
5767 if (*p1 == '\n')
5768 return 2;
5769 if (*p1++ == ' ' && *p1++ == '[')
5770 {
5771 int n = 0;
5772 while (*p1 >= '0' && *p1 <= '9')
5773 n = n * 10 + *p1++ - '0';
5774 if (strncmp (p1, " times]\n", 8) == 0)
5775 return n+1;
5776 }
5777 return 0;
5778 }
5779
5780
5781 /* Display an echo area message M with a specified length of NBYTES
5782 bytes. The string may include null characters. If M is 0, clear
5783 out any existing message, and let the mini-buffer text show
5784 through.
5785
5786 The buffer M must continue to exist until after the echo area gets
5787 cleared or some other message gets displayed there. This means do
5788 not pass text that is stored in a Lisp string; do not pass text in
5789 a buffer that was alloca'd. */
5790
5791 void
5792 message2 (m, nbytes, multibyte)
5793 char *m;
5794 int nbytes;
5795 int multibyte;
5796 {
5797 /* First flush out any partial line written with print. */
5798 message_log_maybe_newline ();
5799 if (m)
5800 message_dolog (m, nbytes, 1, multibyte);
5801 message2_nolog (m, nbytes, multibyte);
5802 }
5803
5804
5805 /* The non-logging counterpart of message2. */
5806
5807 void
5808 message2_nolog (m, nbytes, multibyte)
5809 char *m;
5810 int nbytes;
5811 {
5812 struct frame *sf = SELECTED_FRAME ();
5813 message_enable_multibyte = multibyte;
5814
5815 if (noninteractive)
5816 {
5817 if (noninteractive_need_newline)
5818 putc ('\n', stderr);
5819 noninteractive_need_newline = 0;
5820 if (m)
5821 fwrite (m, nbytes, 1, stderr);
5822 if (cursor_in_echo_area == 0)
5823 fprintf (stderr, "\n");
5824 fflush (stderr);
5825 }
5826 /* A null message buffer means that the frame hasn't really been
5827 initialized yet. Error messages get reported properly by
5828 cmd_error, so this must be just an informative message; toss it. */
5829 else if (INTERACTIVE
5830 && sf->glyphs_initialized_p
5831 && FRAME_MESSAGE_BUF (sf))
5832 {
5833 Lisp_Object mini_window;
5834 struct frame *f;
5835
5836 /* Get the frame containing the mini-buffer
5837 that the selected frame is using. */
5838 mini_window = FRAME_MINIBUF_WINDOW (sf);
5839 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5840
5841 FRAME_SAMPLE_VISIBILITY (f);
5842 if (FRAME_VISIBLE_P (sf)
5843 && ! FRAME_VISIBLE_P (f))
5844 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5845
5846 if (m)
5847 {
5848 set_message (m, Qnil, nbytes, multibyte);
5849 if (minibuffer_auto_raise)
5850 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5851 }
5852 else
5853 clear_message (1, 1);
5854
5855 do_pending_window_change (0);
5856 echo_area_display (1);
5857 do_pending_window_change (0);
5858 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5859 (*frame_up_to_date_hook) (f);
5860 }
5861 }
5862
5863
5864 /* Display an echo area message M with a specified length of NBYTES
5865 bytes. The string may include null characters. If M is not a
5866 string, clear out any existing message, and let the mini-buffer
5867 text show through. */
5868
5869 void
5870 message3 (m, nbytes, multibyte)
5871 Lisp_Object m;
5872 int nbytes;
5873 int multibyte;
5874 {
5875 struct gcpro gcpro1;
5876
5877 GCPRO1 (m);
5878
5879 /* First flush out any partial line written with print. */
5880 message_log_maybe_newline ();
5881 if (STRINGP (m))
5882 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5883 message3_nolog (m, nbytes, multibyte);
5884
5885 UNGCPRO;
5886 }
5887
5888
5889 /* The non-logging version of message3. */
5890
5891 void
5892 message3_nolog (m, nbytes, multibyte)
5893 Lisp_Object m;
5894 int nbytes, multibyte;
5895 {
5896 struct frame *sf = SELECTED_FRAME ();
5897 message_enable_multibyte = multibyte;
5898
5899 if (noninteractive)
5900 {
5901 if (noninteractive_need_newline)
5902 putc ('\n', stderr);
5903 noninteractive_need_newline = 0;
5904 if (STRINGP (m))
5905 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5906 if (cursor_in_echo_area == 0)
5907 fprintf (stderr, "\n");
5908 fflush (stderr);
5909 }
5910 /* A null message buffer means that the frame hasn't really been
5911 initialized yet. Error messages get reported properly by
5912 cmd_error, so this must be just an informative message; toss it. */
5913 else if (INTERACTIVE
5914 && sf->glyphs_initialized_p
5915 && FRAME_MESSAGE_BUF (sf))
5916 {
5917 Lisp_Object mini_window;
5918 Lisp_Object frame;
5919 struct frame *f;
5920
5921 /* Get the frame containing the mini-buffer
5922 that the selected frame is using. */
5923 mini_window = FRAME_MINIBUF_WINDOW (sf);
5924 frame = XWINDOW (mini_window)->frame;
5925 f = XFRAME (frame);
5926
5927 FRAME_SAMPLE_VISIBILITY (f);
5928 if (FRAME_VISIBLE_P (sf)
5929 && !FRAME_VISIBLE_P (f))
5930 Fmake_frame_visible (frame);
5931
5932 if (STRINGP (m) && XSTRING (m)->size)
5933 {
5934 set_message (NULL, m, nbytes, multibyte);
5935 if (minibuffer_auto_raise)
5936 Fraise_frame (frame);
5937 }
5938 else
5939 clear_message (1, 1);
5940
5941 do_pending_window_change (0);
5942 echo_area_display (1);
5943 do_pending_window_change (0);
5944 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5945 (*frame_up_to_date_hook) (f);
5946 }
5947 }
5948
5949
5950 /* Display a null-terminated echo area message M. If M is 0, clear
5951 out any existing message, and let the mini-buffer text show through.
5952
5953 The buffer M must continue to exist until after the echo area gets
5954 cleared or some other message gets displayed there. Do not pass
5955 text that is stored in a Lisp string. Do not pass text in a buffer
5956 that was alloca'd. */
5957
5958 void
5959 message1 (m)
5960 char *m;
5961 {
5962 message2 (m, (m ? strlen (m) : 0), 0);
5963 }
5964
5965
5966 /* The non-logging counterpart of message1. */
5967
5968 void
5969 message1_nolog (m)
5970 char *m;
5971 {
5972 message2_nolog (m, (m ? strlen (m) : 0), 0);
5973 }
5974
5975 /* Display a message M which contains a single %s
5976 which gets replaced with STRING. */
5977
5978 void
5979 message_with_string (m, string, log)
5980 char *m;
5981 Lisp_Object string;
5982 int log;
5983 {
5984 if (noninteractive)
5985 {
5986 if (m)
5987 {
5988 if (noninteractive_need_newline)
5989 putc ('\n', stderr);
5990 noninteractive_need_newline = 0;
5991 fprintf (stderr, m, XSTRING (string)->data);
5992 if (cursor_in_echo_area == 0)
5993 fprintf (stderr, "\n");
5994 fflush (stderr);
5995 }
5996 }
5997 else if (INTERACTIVE)
5998 {
5999 /* The frame whose minibuffer we're going to display the message on.
6000 It may be larger than the selected frame, so we need
6001 to use its buffer, not the selected frame's buffer. */
6002 Lisp_Object mini_window;
6003 struct frame *f, *sf = SELECTED_FRAME ();
6004
6005 /* Get the frame containing the minibuffer
6006 that the selected frame is using. */
6007 mini_window = FRAME_MINIBUF_WINDOW (sf);
6008 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6009
6010 /* A null message buffer means that the frame hasn't really been
6011 initialized yet. Error messages get reported properly by
6012 cmd_error, so this must be just an informative message; toss it. */
6013 if (FRAME_MESSAGE_BUF (f))
6014 {
6015 int len;
6016 char *a[1];
6017 a[0] = (char *) XSTRING (string)->data;
6018
6019 len = doprnt (FRAME_MESSAGE_BUF (f),
6020 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6021
6022 if (log)
6023 message2 (FRAME_MESSAGE_BUF (f), len,
6024 STRING_MULTIBYTE (string));
6025 else
6026 message2_nolog (FRAME_MESSAGE_BUF (f), len,
6027 STRING_MULTIBYTE (string));
6028
6029 /* Print should start at the beginning of the message
6030 buffer next time. */
6031 message_buf_print = 0;
6032 }
6033 }
6034 }
6035
6036
6037 /* Dump an informative message to the minibuf. If M is 0, clear out
6038 any existing message, and let the mini-buffer text show through. */
6039
6040 /* VARARGS 1 */
6041 void
6042 message (m, a1, a2, a3)
6043 char *m;
6044 EMACS_INT a1, a2, a3;
6045 {
6046 if (noninteractive)
6047 {
6048 if (m)
6049 {
6050 if (noninteractive_need_newline)
6051 putc ('\n', stderr);
6052 noninteractive_need_newline = 0;
6053 fprintf (stderr, m, a1, a2, a3);
6054 if (cursor_in_echo_area == 0)
6055 fprintf (stderr, "\n");
6056 fflush (stderr);
6057 }
6058 }
6059 else if (INTERACTIVE)
6060 {
6061 /* The frame whose mini-buffer we're going to display the message
6062 on. It may be larger than the selected frame, so we need to
6063 use its buffer, not the selected frame's buffer. */
6064 Lisp_Object mini_window;
6065 struct frame *f, *sf = SELECTED_FRAME ();
6066
6067 /* Get the frame containing the mini-buffer
6068 that the selected frame is using. */
6069 mini_window = FRAME_MINIBUF_WINDOW (sf);
6070 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6071
6072 /* A null message buffer means that the frame hasn't really been
6073 initialized yet. Error messages get reported properly by
6074 cmd_error, so this must be just an informative message; toss
6075 it. */
6076 if (FRAME_MESSAGE_BUF (f))
6077 {
6078 if (m)
6079 {
6080 int len;
6081 #ifdef NO_ARG_ARRAY
6082 char *a[3];
6083 a[0] = (char *) a1;
6084 a[1] = (char *) a2;
6085 a[2] = (char *) a3;
6086
6087 len = doprnt (FRAME_MESSAGE_BUF (f),
6088 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6089 #else
6090 len = doprnt (FRAME_MESSAGE_BUF (f),
6091 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6092 (char **) &a1);
6093 #endif /* NO_ARG_ARRAY */
6094
6095 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6096 }
6097 else
6098 message1 (0);
6099
6100 /* Print should start at the beginning of the message
6101 buffer next time. */
6102 message_buf_print = 0;
6103 }
6104 }
6105 }
6106
6107
6108 /* The non-logging version of message. */
6109
6110 void
6111 message_nolog (m, a1, a2, a3)
6112 char *m;
6113 EMACS_INT a1, a2, a3;
6114 {
6115 Lisp_Object old_log_max;
6116 old_log_max = Vmessage_log_max;
6117 Vmessage_log_max = Qnil;
6118 message (m, a1, a2, a3);
6119 Vmessage_log_max = old_log_max;
6120 }
6121
6122
6123 /* Display the current message in the current mini-buffer. This is
6124 only called from error handlers in process.c, and is not time
6125 critical. */
6126
6127 void
6128 update_echo_area ()
6129 {
6130 if (!NILP (echo_area_buffer[0]))
6131 {
6132 Lisp_Object string;
6133 string = Fcurrent_message ();
6134 message3 (string, XSTRING (string)->size,
6135 !NILP (current_buffer->enable_multibyte_characters));
6136 }
6137 }
6138
6139
6140 /* Make sure echo area buffers in echo_buffers[] are life. If they
6141 aren't, make new ones. */
6142
6143 static void
6144 ensure_echo_area_buffers ()
6145 {
6146 int i;
6147
6148 for (i = 0; i < 2; ++i)
6149 if (!BUFFERP (echo_buffer[i])
6150 || NILP (XBUFFER (echo_buffer[i])->name))
6151 {
6152 char name[30];
6153 Lisp_Object old_buffer;
6154 int j;
6155
6156 old_buffer = echo_buffer[i];
6157 sprintf (name, " *Echo Area %d*", i);
6158 echo_buffer[i] = Fget_buffer_create (build_string (name));
6159 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6160
6161 for (j = 0; j < 2; ++j)
6162 if (EQ (old_buffer, echo_area_buffer[j]))
6163 echo_area_buffer[j] = echo_buffer[i];
6164 }
6165 }
6166
6167
6168 /* Call FN with args A1..A4 with either the current or last displayed
6169 echo_area_buffer as current buffer.
6170
6171 WHICH zero means use the current message buffer
6172 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6173 from echo_buffer[] and clear it.
6174
6175 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6176 suitable buffer from echo_buffer[] and clear it.
6177
6178 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6179 that the current message becomes the last displayed one, make
6180 choose a suitable buffer for echo_area_buffer[0], and clear it.
6181
6182 Value is what FN returns. */
6183
6184 static int
6185 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6186 struct window *w;
6187 int which;
6188 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6189 EMACS_INT a1;
6190 Lisp_Object a2;
6191 EMACS_INT a3, a4;
6192 {
6193 Lisp_Object buffer;
6194 int this_one, the_other, clear_buffer_p, rc;
6195 int count = BINDING_STACK_SIZE ();
6196
6197 /* If buffers aren't life, make new ones. */
6198 ensure_echo_area_buffers ();
6199
6200 clear_buffer_p = 0;
6201
6202 if (which == 0)
6203 this_one = 0, the_other = 1;
6204 else if (which > 0)
6205 this_one = 1, the_other = 0;
6206 else
6207 {
6208 this_one = 0, the_other = 1;
6209 clear_buffer_p = 1;
6210
6211 /* We need a fresh one in case the current echo buffer equals
6212 the one containing the last displayed echo area message. */
6213 if (!NILP (echo_area_buffer[this_one])
6214 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6215 echo_area_buffer[this_one] = Qnil;
6216 }
6217
6218 /* Choose a suitable buffer from echo_buffer[] is we don't
6219 have one. */
6220 if (NILP (echo_area_buffer[this_one]))
6221 {
6222 echo_area_buffer[this_one]
6223 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6224 ? echo_buffer[the_other]
6225 : echo_buffer[this_one]);
6226 clear_buffer_p = 1;
6227 }
6228
6229 buffer = echo_area_buffer[this_one];
6230
6231 /* Don't get confused by reusing the buffer used for echoing
6232 for a different purpose. */
6233 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6234 cancel_echoing ();
6235
6236 record_unwind_protect (unwind_with_echo_area_buffer,
6237 with_echo_area_buffer_unwind_data (w));
6238
6239 /* Make the echo area buffer current. Note that for display
6240 purposes, it is not necessary that the displayed window's buffer
6241 == current_buffer, except for text property lookup. So, let's
6242 only set that buffer temporarily here without doing a full
6243 Fset_window_buffer. We must also change w->pointm, though,
6244 because otherwise an assertions in unshow_buffer fails, and Emacs
6245 aborts. */
6246 set_buffer_internal_1 (XBUFFER (buffer));
6247 if (w)
6248 {
6249 w->buffer = buffer;
6250 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6251 }
6252
6253 current_buffer->undo_list = Qt;
6254 current_buffer->read_only = Qnil;
6255 specbind (Qinhibit_read_only, Qt);
6256 specbind (Qinhibit_modification_hooks, Qt);
6257
6258 if (clear_buffer_p && Z > BEG)
6259 del_range (BEG, Z);
6260
6261 xassert (BEGV >= BEG);
6262 xassert (ZV <= Z && ZV >= BEGV);
6263
6264 rc = fn (a1, a2, a3, a4);
6265
6266 xassert (BEGV >= BEG);
6267 xassert (ZV <= Z && ZV >= BEGV);
6268
6269 unbind_to (count, Qnil);
6270 return rc;
6271 }
6272
6273
6274 /* Save state that should be preserved around the call to the function
6275 FN called in with_echo_area_buffer. */
6276
6277 static Lisp_Object
6278 with_echo_area_buffer_unwind_data (w)
6279 struct window *w;
6280 {
6281 int i = 0;
6282 Lisp_Object vector;
6283
6284 /* Reduce consing by keeping one vector in
6285 Vwith_echo_area_save_vector. */
6286 vector = Vwith_echo_area_save_vector;
6287 Vwith_echo_area_save_vector = Qnil;
6288
6289 if (NILP (vector))
6290 vector = Fmake_vector (make_number (7), Qnil);
6291
6292 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6293 AREF (vector, i) = Vdeactivate_mark, ++i;
6294 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6295
6296 if (w)
6297 {
6298 XSETWINDOW (AREF (vector, i), w); ++i;
6299 AREF (vector, i) = w->buffer; ++i;
6300 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6301 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6302 }
6303 else
6304 {
6305 int end = i + 4;
6306 for (; i < end; ++i)
6307 AREF (vector, i) = Qnil;
6308 }
6309
6310 xassert (i == ASIZE (vector));
6311 return vector;
6312 }
6313
6314
6315 /* Restore global state from VECTOR which was created by
6316 with_echo_area_buffer_unwind_data. */
6317
6318 static Lisp_Object
6319 unwind_with_echo_area_buffer (vector)
6320 Lisp_Object vector;
6321 {
6322 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6323 Vdeactivate_mark = AREF (vector, 1);
6324 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6325
6326 if (WINDOWP (AREF (vector, 3)))
6327 {
6328 struct window *w;
6329 Lisp_Object buffer, charpos, bytepos;
6330
6331 w = XWINDOW (AREF (vector, 3));
6332 buffer = AREF (vector, 4);
6333 charpos = AREF (vector, 5);
6334 bytepos = AREF (vector, 6);
6335
6336 w->buffer = buffer;
6337 set_marker_both (w->pointm, buffer,
6338 XFASTINT (charpos), XFASTINT (bytepos));
6339 }
6340
6341 Vwith_echo_area_save_vector = vector;
6342 return Qnil;
6343 }
6344
6345
6346 /* Set up the echo area for use by print functions. MULTIBYTE_P
6347 non-zero means we will print multibyte. */
6348
6349 void
6350 setup_echo_area_for_printing (multibyte_p)
6351 int multibyte_p;
6352 {
6353 ensure_echo_area_buffers ();
6354
6355 if (!message_buf_print)
6356 {
6357 /* A message has been output since the last time we printed.
6358 Choose a fresh echo area buffer. */
6359 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6360 echo_area_buffer[0] = echo_buffer[1];
6361 else
6362 echo_area_buffer[0] = echo_buffer[0];
6363
6364 /* Switch to that buffer and clear it. */
6365 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6366 current_buffer->truncate_lines = Qnil;
6367
6368 if (Z > BEG)
6369 {
6370 int count = BINDING_STACK_SIZE ();
6371 specbind (Qinhibit_read_only, Qt);
6372 del_range (BEG, Z);
6373 unbind_to (count, Qnil);
6374 }
6375 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6376
6377 /* Set up the buffer for the multibyteness we need. */
6378 if (multibyte_p
6379 != !NILP (current_buffer->enable_multibyte_characters))
6380 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6381
6382 /* Raise the frame containing the echo area. */
6383 if (minibuffer_auto_raise)
6384 {
6385 struct frame *sf = SELECTED_FRAME ();
6386 Lisp_Object mini_window;
6387 mini_window = FRAME_MINIBUF_WINDOW (sf);
6388 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6389 }
6390
6391 message_log_maybe_newline ();
6392 message_buf_print = 1;
6393 }
6394 else
6395 {
6396 if (NILP (echo_area_buffer[0]))
6397 {
6398 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6399 echo_area_buffer[0] = echo_buffer[1];
6400 else
6401 echo_area_buffer[0] = echo_buffer[0];
6402 }
6403
6404 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6405 {
6406 /* Someone switched buffers between print requests. */
6407 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6408 current_buffer->truncate_lines = Qnil;
6409 }
6410 }
6411 }
6412
6413
6414 /* Display an echo area message in window W. Value is non-zero if W's
6415 height is changed. If display_last_displayed_message_p is
6416 non-zero, display the message that was last displayed, otherwise
6417 display the current message. */
6418
6419 static int
6420 display_echo_area (w)
6421 struct window *w;
6422 {
6423 int i, no_message_p, window_height_changed_p, count;
6424
6425 /* Temporarily disable garbage collections while displaying the echo
6426 area. This is done because a GC can print a message itself.
6427 That message would modify the echo area buffer's contents while a
6428 redisplay of the buffer is going on, and seriously confuse
6429 redisplay. */
6430 count = inhibit_garbage_collection ();
6431
6432 /* If there is no message, we must call display_echo_area_1
6433 nevertheless because it resizes the window. But we will have to
6434 reset the echo_area_buffer in question to nil at the end because
6435 with_echo_area_buffer will sets it to an empty buffer. */
6436 i = display_last_displayed_message_p ? 1 : 0;
6437 no_message_p = NILP (echo_area_buffer[i]);
6438
6439 window_height_changed_p
6440 = with_echo_area_buffer (w, display_last_displayed_message_p,
6441 display_echo_area_1,
6442 (EMACS_INT) w, Qnil, 0, 0);
6443
6444 if (no_message_p)
6445 echo_area_buffer[i] = Qnil;
6446
6447 unbind_to (count, Qnil);
6448 return window_height_changed_p;
6449 }
6450
6451
6452 /* Helper for display_echo_area. Display the current buffer which
6453 contains the current echo area message in window W, a mini-window,
6454 a pointer to which is passed in A1. A2..A4 are currently not used.
6455 Change the height of W so that all of the message is displayed.
6456 Value is non-zero if height of W was changed. */
6457
6458 static int
6459 display_echo_area_1 (a1, a2, a3, a4)
6460 EMACS_INT a1;
6461 Lisp_Object a2;
6462 EMACS_INT a3, a4;
6463 {
6464 struct window *w = (struct window *) a1;
6465 Lisp_Object window;
6466 struct text_pos start;
6467 int window_height_changed_p = 0;
6468
6469 /* Do this before displaying, so that we have a large enough glyph
6470 matrix for the display. */
6471 window_height_changed_p = resize_mini_window (w, 0);
6472
6473 /* Display. */
6474 clear_glyph_matrix (w->desired_matrix);
6475 XSETWINDOW (window, w);
6476 SET_TEXT_POS (start, BEG, BEG_BYTE);
6477 try_window (window, start);
6478
6479 return window_height_changed_p;
6480 }
6481
6482
6483 /* Resize the echo area window to exactly the size needed for the
6484 currently displayed message, if there is one. If a mini-buffer
6485 is active, don't shrink it. */
6486
6487 void
6488 resize_echo_area_exactly ()
6489 {
6490 if (BUFFERP (echo_area_buffer[0])
6491 && WINDOWP (echo_area_window))
6492 {
6493 struct window *w = XWINDOW (echo_area_window);
6494 int resized_p;
6495 Lisp_Object resize_exactly;
6496
6497 if (minibuf_level == 0)
6498 resize_exactly = Qt;
6499 else
6500 resize_exactly = Qnil;
6501
6502 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6503 (EMACS_INT) w, resize_exactly, 0, 0);
6504 if (resized_p)
6505 {
6506 ++windows_or_buffers_changed;
6507 ++update_mode_lines;
6508 redisplay_internal (0);
6509 }
6510 }
6511 }
6512
6513
6514 /* Callback function for with_echo_area_buffer, when used from
6515 resize_echo_area_exactly. A1 contains a pointer to the window to
6516 resize, EXACTLY non-nil means resize the mini-window exactly to the
6517 size of the text displayed. A3 and A4 are not used. Value is what
6518 resize_mini_window returns. */
6519
6520 static int
6521 resize_mini_window_1 (a1, exactly, a3, a4)
6522 EMACS_INT a1;
6523 Lisp_Object exactly;
6524 EMACS_INT a3, a4;
6525 {
6526 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6527 }
6528
6529
6530 /* Resize mini-window W to fit the size of its contents. EXACT:P
6531 means size the window exactly to the size needed. Otherwise, it's
6532 only enlarged until W's buffer is empty. Value is non-zero if
6533 the window height has been changed. */
6534
6535 int
6536 resize_mini_window (w, exact_p)
6537 struct window *w;
6538 int exact_p;
6539 {
6540 struct frame *f = XFRAME (w->frame);
6541 int window_height_changed_p = 0;
6542
6543 xassert (MINI_WINDOW_P (w));
6544
6545 /* Don't resize windows while redisplaying a window; it would
6546 confuse redisplay functions when the size of the window they are
6547 displaying changes from under them. Such a resizing can happen,
6548 for instance, when which-func prints a long message while
6549 we are running fontification-functions. We're running these
6550 functions with safe_call which binds inhibit-redisplay to t. */
6551 if (!NILP (Vinhibit_redisplay))
6552 return 0;
6553
6554 /* Nil means don't try to resize. */
6555 if (NILP (Vresize_mini_windows)
6556 || (FRAME_X_P (f) && f->output_data.x == NULL))
6557 return 0;
6558
6559 if (!FRAME_MINIBUF_ONLY_P (f))
6560 {
6561 struct it it;
6562 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6563 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6564 int height, max_height;
6565 int unit = CANON_Y_UNIT (f);
6566 struct text_pos start;
6567 struct buffer *old_current_buffer = NULL;
6568
6569 if (current_buffer != XBUFFER (w->buffer))
6570 {
6571 old_current_buffer = current_buffer;
6572 set_buffer_internal (XBUFFER (w->buffer));
6573 }
6574
6575 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6576
6577 /* Compute the max. number of lines specified by the user. */
6578 if (FLOATP (Vmax_mini_window_height))
6579 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6580 else if (INTEGERP (Vmax_mini_window_height))
6581 max_height = XINT (Vmax_mini_window_height);
6582 else
6583 max_height = total_height / 4;
6584
6585 /* Correct that max. height if it's bogus. */
6586 max_height = max (1, max_height);
6587 max_height = min (total_height, max_height);
6588
6589 /* Find out the height of the text in the window. */
6590 if (it.truncate_lines_p)
6591 height = 1;
6592 else
6593 {
6594 last_height = 0;
6595 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6596 if (it.max_ascent == 0 && it.max_descent == 0)
6597 height = it.current_y + last_height;
6598 else
6599 height = it.current_y + it.max_ascent + it.max_descent;
6600 height -= it.extra_line_spacing;
6601 height = (height + unit - 1) / unit;
6602 }
6603
6604 /* Compute a suitable window start. */
6605 if (height > max_height)
6606 {
6607 height = max_height;
6608 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6609 move_it_vertically_backward (&it, (height - 1) * unit);
6610 start = it.current.pos;
6611 }
6612 else
6613 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6614 SET_MARKER_FROM_TEXT_POS (w->start, start);
6615
6616 if (EQ (Vresize_mini_windows, Qgrow_only))
6617 {
6618 /* Let it grow only, until we display an empty message, in which
6619 case the window shrinks again. */
6620 if (height > XFASTINT (w->height))
6621 {
6622 int old_height = XFASTINT (w->height);
6623 freeze_window_starts (f, 1);
6624 grow_mini_window (w, height - XFASTINT (w->height));
6625 window_height_changed_p = XFASTINT (w->height) != old_height;
6626 }
6627 else if (height < XFASTINT (w->height)
6628 && (exact_p || BEGV == ZV))
6629 {
6630 int old_height = XFASTINT (w->height);
6631 freeze_window_starts (f, 0);
6632 shrink_mini_window (w);
6633 window_height_changed_p = XFASTINT (w->height) != old_height;
6634 }
6635 }
6636 else
6637 {
6638 /* Always resize to exact size needed. */
6639 if (height > XFASTINT (w->height))
6640 {
6641 int old_height = XFASTINT (w->height);
6642 freeze_window_starts (f, 1);
6643 grow_mini_window (w, height - XFASTINT (w->height));
6644 window_height_changed_p = XFASTINT (w->height) != old_height;
6645 }
6646 else if (height < XFASTINT (w->height))
6647 {
6648 int old_height = XFASTINT (w->height);
6649 freeze_window_starts (f, 0);
6650 shrink_mini_window (w);
6651
6652 if (height)
6653 {
6654 freeze_window_starts (f, 1);
6655 grow_mini_window (w, height - XFASTINT (w->height));
6656 }
6657
6658 window_height_changed_p = XFASTINT (w->height) != old_height;
6659 }
6660 }
6661
6662 if (old_current_buffer)
6663 set_buffer_internal (old_current_buffer);
6664 }
6665
6666 return window_height_changed_p;
6667 }
6668
6669
6670 /* Value is the current message, a string, or nil if there is no
6671 current message. */
6672
6673 Lisp_Object
6674 current_message ()
6675 {
6676 Lisp_Object msg;
6677
6678 if (NILP (echo_area_buffer[0]))
6679 msg = Qnil;
6680 else
6681 {
6682 with_echo_area_buffer (0, 0, current_message_1,
6683 (EMACS_INT) &msg, Qnil, 0, 0);
6684 if (NILP (msg))
6685 echo_area_buffer[0] = Qnil;
6686 }
6687
6688 return msg;
6689 }
6690
6691
6692 static int
6693 current_message_1 (a1, a2, a3, a4)
6694 EMACS_INT a1;
6695 Lisp_Object a2;
6696 EMACS_INT a3, a4;
6697 {
6698 Lisp_Object *msg = (Lisp_Object *) a1;
6699
6700 if (Z > BEG)
6701 *msg = make_buffer_string (BEG, Z, 1);
6702 else
6703 *msg = Qnil;
6704 return 0;
6705 }
6706
6707
6708 /* Push the current message on Vmessage_stack for later restauration
6709 by restore_message. Value is non-zero if the current message isn't
6710 empty. This is a relatively infrequent operation, so it's not
6711 worth optimizing. */
6712
6713 int
6714 push_message ()
6715 {
6716 Lisp_Object msg;
6717 msg = current_message ();
6718 Vmessage_stack = Fcons (msg, Vmessage_stack);
6719 return STRINGP (msg);
6720 }
6721
6722
6723 /* Handler for record_unwind_protect calling pop_message. */
6724
6725 Lisp_Object
6726 push_message_unwind (dummy)
6727 Lisp_Object dummy;
6728 {
6729 pop_message ();
6730 return Qnil;
6731 }
6732
6733
6734 /* Restore message display from the top of Vmessage_stack. */
6735
6736 void
6737 restore_message ()
6738 {
6739 Lisp_Object msg;
6740
6741 xassert (CONSP (Vmessage_stack));
6742 msg = XCAR (Vmessage_stack);
6743 if (STRINGP (msg))
6744 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6745 else
6746 message3_nolog (msg, 0, 0);
6747 }
6748
6749
6750 /* Pop the top-most entry off Vmessage_stack. */
6751
6752 void
6753 pop_message ()
6754 {
6755 xassert (CONSP (Vmessage_stack));
6756 Vmessage_stack = XCDR (Vmessage_stack);
6757 }
6758
6759
6760 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6761 exits. If the stack is not empty, we have a missing pop_message
6762 somewhere. */
6763
6764 void
6765 check_message_stack ()
6766 {
6767 if (!NILP (Vmessage_stack))
6768 abort ();
6769 }
6770
6771
6772 /* Truncate to NCHARS what will be displayed in the echo area the next
6773 time we display it---but don't redisplay it now. */
6774
6775 void
6776 truncate_echo_area (nchars)
6777 int nchars;
6778 {
6779 if (nchars == 0)
6780 echo_area_buffer[0] = Qnil;
6781 /* A null message buffer means that the frame hasn't really been
6782 initialized yet. Error messages get reported properly by
6783 cmd_error, so this must be just an informative message; toss it. */
6784 else if (!noninteractive
6785 && INTERACTIVE
6786 && !NILP (echo_area_buffer[0]))
6787 {
6788 struct frame *sf = SELECTED_FRAME ();
6789 if (FRAME_MESSAGE_BUF (sf))
6790 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6791 }
6792 }
6793
6794
6795 /* Helper function for truncate_echo_area. Truncate the current
6796 message to at most NCHARS characters. */
6797
6798 static int
6799 truncate_message_1 (nchars, a2, a3, a4)
6800 EMACS_INT nchars;
6801 Lisp_Object a2;
6802 EMACS_INT a3, a4;
6803 {
6804 if (BEG + nchars < Z)
6805 del_range (BEG + nchars, Z);
6806 if (Z == BEG)
6807 echo_area_buffer[0] = Qnil;
6808 return 0;
6809 }
6810
6811
6812 /* Set the current message to a substring of S or STRING.
6813
6814 If STRING is a Lisp string, set the message to the first NBYTES
6815 bytes from STRING. NBYTES zero means use the whole string. If
6816 STRING is multibyte, the message will be displayed multibyte.
6817
6818 If S is not null, set the message to the first LEN bytes of S. LEN
6819 zero means use the whole string. MULTIBYTE_P non-zero means S is
6820 multibyte. Display the message multibyte in that case. */
6821
6822 void
6823 set_message (s, string, nbytes, multibyte_p)
6824 char *s;
6825 Lisp_Object string;
6826 int nbytes;
6827 {
6828 message_enable_multibyte
6829 = ((s && multibyte_p)
6830 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6831
6832 with_echo_area_buffer (0, -1, set_message_1,
6833 (EMACS_INT) s, string, nbytes, multibyte_p);
6834 message_buf_print = 0;
6835 help_echo_showing_p = 0;
6836 }
6837
6838
6839 /* Helper function for set_message. Arguments have the same meaning
6840 as there, with A1 corresponding to S and A2 corresponding to STRING
6841 This function is called with the echo area buffer being
6842 current. */
6843
6844 static int
6845 set_message_1 (a1, a2, nbytes, multibyte_p)
6846 EMACS_INT a1;
6847 Lisp_Object a2;
6848 EMACS_INT nbytes, multibyte_p;
6849 {
6850 char *s = (char *) a1;
6851 Lisp_Object string = a2;
6852
6853 xassert (BEG == Z);
6854
6855 /* Change multibyteness of the echo buffer appropriately. */
6856 if (message_enable_multibyte
6857 != !NILP (current_buffer->enable_multibyte_characters))
6858 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6859
6860 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6861
6862 /* Insert new message at BEG. */
6863 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6864
6865 if (STRINGP (string))
6866 {
6867 int nchars;
6868
6869 if (nbytes == 0)
6870 nbytes = XSTRING (string)->size_byte;
6871 nchars = string_byte_to_char (string, nbytes);
6872
6873 /* This function takes care of single/multibyte conversion. We
6874 just have to ensure that the echo area buffer has the right
6875 setting of enable_multibyte_characters. */
6876 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6877 }
6878 else if (s)
6879 {
6880 if (nbytes == 0)
6881 nbytes = strlen (s);
6882
6883 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6884 {
6885 /* Convert from multi-byte to single-byte. */
6886 int i, c, n;
6887 unsigned char work[1];
6888
6889 /* Convert a multibyte string to single-byte. */
6890 for (i = 0; i < nbytes; i += n)
6891 {
6892 c = string_char_and_length (s + i, nbytes - i, &n);
6893 work[0] = (SINGLE_BYTE_CHAR_P (c)
6894 ? c
6895 : multibyte_char_to_unibyte (c, Qnil));
6896 insert_1_both (work, 1, 1, 1, 0, 0);
6897 }
6898 }
6899 else if (!multibyte_p
6900 && !NILP (current_buffer->enable_multibyte_characters))
6901 {
6902 /* Convert from single-byte to multi-byte. */
6903 int i, c, n;
6904 unsigned char *msg = (unsigned char *) s;
6905 unsigned char str[MAX_MULTIBYTE_LENGTH];
6906
6907 /* Convert a single-byte string to multibyte. */
6908 for (i = 0; i < nbytes; i++)
6909 {
6910 c = unibyte_char_to_multibyte (msg[i]);
6911 n = CHAR_STRING (c, str);
6912 insert_1_both (str, 1, n, 1, 0, 0);
6913 }
6914 }
6915 else
6916 insert_1 (s, nbytes, 1, 0, 0);
6917 }
6918
6919 return 0;
6920 }
6921
6922
6923 /* Clear messages. CURRENT_P non-zero means clear the current
6924 message. LAST_DISPLAYED_P non-zero means clear the message
6925 last displayed. */
6926
6927 void
6928 clear_message (current_p, last_displayed_p)
6929 int current_p, last_displayed_p;
6930 {
6931 if (current_p)
6932 {
6933 echo_area_buffer[0] = Qnil;
6934 message_cleared_p = 1;
6935 }
6936
6937 if (last_displayed_p)
6938 echo_area_buffer[1] = Qnil;
6939
6940 message_buf_print = 0;
6941 }
6942
6943 /* Clear garbaged frames.
6944
6945 This function is used where the old redisplay called
6946 redraw_garbaged_frames which in turn called redraw_frame which in
6947 turn called clear_frame. The call to clear_frame was a source of
6948 flickering. I believe a clear_frame is not necessary. It should
6949 suffice in the new redisplay to invalidate all current matrices,
6950 and ensure a complete redisplay of all windows. */
6951
6952 static void
6953 clear_garbaged_frames ()
6954 {
6955 if (frame_garbaged)
6956 {
6957 Lisp_Object tail, frame;
6958
6959 FOR_EACH_FRAME (tail, frame)
6960 {
6961 struct frame *f = XFRAME (frame);
6962
6963 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6964 {
6965 clear_current_matrices (f);
6966 f->garbaged = 0;
6967 }
6968 }
6969
6970 frame_garbaged = 0;
6971 ++windows_or_buffers_changed;
6972 }
6973 }
6974
6975
6976 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6977 is non-zero update selected_frame. Value is non-zero if the
6978 mini-windows height has been changed. */
6979
6980 static int
6981 echo_area_display (update_frame_p)
6982 int update_frame_p;
6983 {
6984 Lisp_Object mini_window;
6985 struct window *w;
6986 struct frame *f;
6987 int window_height_changed_p = 0;
6988 struct frame *sf = SELECTED_FRAME ();
6989
6990 mini_window = FRAME_MINIBUF_WINDOW (sf);
6991 w = XWINDOW (mini_window);
6992 f = XFRAME (WINDOW_FRAME (w));
6993
6994 /* Don't display if frame is invisible or not yet initialized. */
6995 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6996 return 0;
6997
6998 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6999 #ifndef macintosh
7000 #ifdef HAVE_WINDOW_SYSTEM
7001 /* When Emacs starts, selected_frame may be a visible terminal
7002 frame, even if we run under a window system. If we let this
7003 through, a message would be displayed on the terminal. */
7004 if (EQ (selected_frame, Vterminal_frame)
7005 && !NILP (Vwindow_system))
7006 return 0;
7007 #endif /* HAVE_WINDOW_SYSTEM */
7008 #endif
7009
7010 /* Redraw garbaged frames. */
7011 if (frame_garbaged)
7012 clear_garbaged_frames ();
7013
7014 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7015 {
7016 echo_area_window = mini_window;
7017 window_height_changed_p = display_echo_area (w);
7018 w->must_be_updated_p = 1;
7019
7020 /* Update the display, unless called from redisplay_internal.
7021 Also don't update the screen during redisplay itself. The
7022 update will happen at the end of redisplay, and an update
7023 here could cause confusion. */
7024 if (update_frame_p && !redisplaying_p)
7025 {
7026 int n = 0;
7027
7028 /* If the display update has been interrupted by pending
7029 input, update mode lines in the frame. Due to the
7030 pending input, it might have been that redisplay hasn't
7031 been called, so that mode lines above the echo area are
7032 garbaged. This looks odd, so we prevent it here. */
7033 if (!display_completed)
7034 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7035
7036 if (window_height_changed_p
7037 /* Don't do this if Emacs is shutting down. Redisplay
7038 needs to run hooks. */
7039 && !NILP (Vrun_hooks))
7040 {
7041 /* Must update other windows. Likewise as in other
7042 cases, don't let this update be interrupted by
7043 pending input. */
7044 int count = BINDING_STACK_SIZE ();
7045 specbind (Qredisplay_dont_pause, Qt);
7046 windows_or_buffers_changed = 1;
7047 redisplay_internal (0);
7048 unbind_to (count, Qnil);
7049 }
7050 else if (FRAME_WINDOW_P (f) && n == 0)
7051 {
7052 /* Window configuration is the same as before.
7053 Can do with a display update of the echo area,
7054 unless we displayed some mode lines. */
7055 update_single_window (w, 1);
7056 rif->flush_display (f);
7057 }
7058 else
7059 update_frame (f, 1, 1);
7060
7061 /* If cursor is in the echo area, make sure that the next
7062 redisplay displays the minibuffer, so that the cursor will
7063 be replaced with what the minibuffer wants. */
7064 if (cursor_in_echo_area)
7065 ++windows_or_buffers_changed;
7066 }
7067 }
7068 else if (!EQ (mini_window, selected_window))
7069 windows_or_buffers_changed++;
7070
7071 /* Last displayed message is now the current message. */
7072 echo_area_buffer[1] = echo_area_buffer[0];
7073
7074 /* Prevent redisplay optimization in redisplay_internal by resetting
7075 this_line_start_pos. This is done because the mini-buffer now
7076 displays the message instead of its buffer text. */
7077 if (EQ (mini_window, selected_window))
7078 CHARPOS (this_line_start_pos) = 0;
7079
7080 return window_height_changed_p;
7081 }
7082
7083
7084 \f
7085 /***********************************************************************
7086 Frame Titles
7087 ***********************************************************************/
7088
7089
7090 #ifdef HAVE_WINDOW_SYSTEM
7091
7092 /* A buffer for constructing frame titles in it; allocated from the
7093 heap in init_xdisp and resized as needed in store_frame_title_char. */
7094
7095 static char *frame_title_buf;
7096
7097 /* The buffer's end, and a current output position in it. */
7098
7099 static char *frame_title_buf_end;
7100 static char *frame_title_ptr;
7101
7102
7103 /* Store a single character C for the frame title in frame_title_buf.
7104 Re-allocate frame_title_buf if necessary. */
7105
7106 static void
7107 store_frame_title_char (c)
7108 char c;
7109 {
7110 /* If output position has reached the end of the allocated buffer,
7111 double the buffer's size. */
7112 if (frame_title_ptr == frame_title_buf_end)
7113 {
7114 int len = frame_title_ptr - frame_title_buf;
7115 int new_size = 2 * len * sizeof *frame_title_buf;
7116 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7117 frame_title_buf_end = frame_title_buf + new_size;
7118 frame_title_ptr = frame_title_buf + len;
7119 }
7120
7121 *frame_title_ptr++ = c;
7122 }
7123
7124
7125 /* Store part of a frame title in frame_title_buf, beginning at
7126 frame_title_ptr. STR is the string to store. Do not copy
7127 characters that yield more columns than PRECISION; PRECISION <= 0
7128 means copy the whole string. Pad with spaces until FIELD_WIDTH
7129 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7130 pad. Called from display_mode_element when it is used to build a
7131 frame title. */
7132
7133 static int
7134 store_frame_title (str, field_width, precision)
7135 unsigned char *str;
7136 int field_width, precision;
7137 {
7138 int n = 0;
7139 int dummy, nbytes, width;
7140
7141 /* Copy at most PRECISION chars from STR. */
7142 nbytes = strlen (str);
7143 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7144 while (nbytes--)
7145 store_frame_title_char (*str++);
7146
7147 /* Fill up with spaces until FIELD_WIDTH reached. */
7148 while (field_width > 0
7149 && n < field_width)
7150 {
7151 store_frame_title_char (' ');
7152 ++n;
7153 }
7154
7155 return n;
7156 }
7157
7158
7159 /* Set the title of FRAME, if it has changed. The title format is
7160 Vicon_title_format if FRAME is iconified, otherwise it is
7161 frame_title_format. */
7162
7163 static void
7164 x_consider_frame_title (frame)
7165 Lisp_Object frame;
7166 {
7167 struct frame *f = XFRAME (frame);
7168
7169 if (FRAME_WINDOW_P (f)
7170 || FRAME_MINIBUF_ONLY_P (f)
7171 || f->explicit_name)
7172 {
7173 /* Do we have more than one visible frame on this X display? */
7174 Lisp_Object tail;
7175 Lisp_Object fmt;
7176 struct buffer *obuf;
7177 int len;
7178 struct it it;
7179
7180 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7181 {
7182 struct frame *tf = XFRAME (XCAR (tail));
7183
7184 if (tf != f
7185 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7186 && !FRAME_MINIBUF_ONLY_P (tf)
7187 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7188 break;
7189 }
7190
7191 /* Set global variable indicating that multiple frames exist. */
7192 multiple_frames = CONSP (tail);
7193
7194 /* Switch to the buffer of selected window of the frame. Set up
7195 frame_title_ptr so that display_mode_element will output into it;
7196 then display the title. */
7197 obuf = current_buffer;
7198 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7199 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7200 frame_title_ptr = frame_title_buf;
7201 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7202 NULL, DEFAULT_FACE_ID);
7203 display_mode_element (&it, 0, -1, -1, fmt);
7204 len = frame_title_ptr - frame_title_buf;
7205 frame_title_ptr = NULL;
7206 set_buffer_internal (obuf);
7207
7208 /* Set the title only if it's changed. This avoids consing in
7209 the common case where it hasn't. (If it turns out that we've
7210 already wasted too much time by walking through the list with
7211 display_mode_element, then we might need to optimize at a
7212 higher level than this.) */
7213 if (! STRINGP (f->name)
7214 || STRING_BYTES (XSTRING (f->name)) != len
7215 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7216 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7217 }
7218 }
7219
7220 #else /* not HAVE_WINDOW_SYSTEM */
7221
7222 #define frame_title_ptr ((char *)0)
7223 #define store_frame_title(str, mincol, maxcol) 0
7224
7225 #endif /* not HAVE_WINDOW_SYSTEM */
7226
7227
7228
7229 \f
7230 /***********************************************************************
7231 Menu Bars
7232 ***********************************************************************/
7233
7234
7235 /* Prepare for redisplay by updating menu-bar item lists when
7236 appropriate. This can call eval. */
7237
7238 void
7239 prepare_menu_bars ()
7240 {
7241 int all_windows;
7242 struct gcpro gcpro1, gcpro2;
7243 struct frame *f;
7244 Lisp_Object tooltip_frame;
7245
7246 #ifdef HAVE_X_WINDOWS
7247 tooltip_frame = tip_frame;
7248 #else
7249 tooltip_frame = Qnil;
7250 #endif
7251
7252 /* Update all frame titles based on their buffer names, etc. We do
7253 this before the menu bars so that the buffer-menu will show the
7254 up-to-date frame titles. */
7255 #ifdef HAVE_WINDOW_SYSTEM
7256 if (windows_or_buffers_changed || update_mode_lines)
7257 {
7258 Lisp_Object tail, frame;
7259
7260 FOR_EACH_FRAME (tail, frame)
7261 {
7262 f = XFRAME (frame);
7263 if (!EQ (frame, tooltip_frame)
7264 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7265 x_consider_frame_title (frame);
7266 }
7267 }
7268 #endif /* HAVE_WINDOW_SYSTEM */
7269
7270 /* Update the menu bar item lists, if appropriate. This has to be
7271 done before any actual redisplay or generation of display lines. */
7272 all_windows = (update_mode_lines
7273 || buffer_shared > 1
7274 || windows_or_buffers_changed);
7275 if (all_windows)
7276 {
7277 Lisp_Object tail, frame;
7278 int count = BINDING_STACK_SIZE ();
7279
7280 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7281
7282 FOR_EACH_FRAME (tail, frame)
7283 {
7284 f = XFRAME (frame);
7285
7286 /* Ignore tooltip frame. */
7287 if (EQ (frame, tooltip_frame))
7288 continue;
7289
7290 /* If a window on this frame changed size, report that to
7291 the user and clear the size-change flag. */
7292 if (FRAME_WINDOW_SIZES_CHANGED (f))
7293 {
7294 Lisp_Object functions;
7295
7296 /* Clear flag first in case we get an error below. */
7297 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7298 functions = Vwindow_size_change_functions;
7299 GCPRO2 (tail, functions);
7300
7301 while (CONSP (functions))
7302 {
7303 call1 (XCAR (functions), frame);
7304 functions = XCDR (functions);
7305 }
7306 UNGCPRO;
7307 }
7308
7309 GCPRO1 (tail);
7310 update_menu_bar (f, 0);
7311 #ifdef HAVE_WINDOW_SYSTEM
7312 update_tool_bar (f, 0);
7313 #endif
7314 UNGCPRO;
7315 }
7316
7317 unbind_to (count, Qnil);
7318 }
7319 else
7320 {
7321 struct frame *sf = SELECTED_FRAME ();
7322 update_menu_bar (sf, 1);
7323 #ifdef HAVE_WINDOW_SYSTEM
7324 update_tool_bar (sf, 1);
7325 #endif
7326 }
7327
7328 /* Motif needs this. See comment in xmenu.c. Turn it off when
7329 pending_menu_activation is not defined. */
7330 #ifdef USE_X_TOOLKIT
7331 pending_menu_activation = 0;
7332 #endif
7333 }
7334
7335
7336 /* Update the menu bar item list for frame F. This has to be done
7337 before we start to fill in any display lines, because it can call
7338 eval.
7339
7340 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7341
7342 static void
7343 update_menu_bar (f, save_match_data)
7344 struct frame *f;
7345 int save_match_data;
7346 {
7347 Lisp_Object window;
7348 register struct window *w;
7349
7350 /* If called recursively during a menu update, do nothing. This can
7351 happen when, for instance, an activate-menubar-hook causes a
7352 redisplay. */
7353 if (inhibit_menubar_update)
7354 return;
7355
7356 window = FRAME_SELECTED_WINDOW (f);
7357 w = XWINDOW (window);
7358
7359 if (update_mode_lines)
7360 w->update_mode_line = Qt;
7361
7362 if (FRAME_WINDOW_P (f)
7363 ?
7364 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7365 FRAME_EXTERNAL_MENU_BAR (f)
7366 #else
7367 FRAME_MENU_BAR_LINES (f) > 0
7368 #endif
7369 : FRAME_MENU_BAR_LINES (f) > 0)
7370 {
7371 /* If the user has switched buffers or windows, we need to
7372 recompute to reflect the new bindings. But we'll
7373 recompute when update_mode_lines is set too; that means
7374 that people can use force-mode-line-update to request
7375 that the menu bar be recomputed. The adverse effect on
7376 the rest of the redisplay algorithm is about the same as
7377 windows_or_buffers_changed anyway. */
7378 if (windows_or_buffers_changed
7379 || !NILP (w->update_mode_line)
7380 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7381 < BUF_MODIFF (XBUFFER (w->buffer)))
7382 != !NILP (w->last_had_star))
7383 || ((!NILP (Vtransient_mark_mode)
7384 && !NILP (XBUFFER (w->buffer)->mark_active))
7385 != !NILP (w->region_showing)))
7386 {
7387 struct buffer *prev = current_buffer;
7388 int count = BINDING_STACK_SIZE ();
7389
7390 specbind (Qinhibit_menubar_update, Qt);
7391
7392 set_buffer_internal_1 (XBUFFER (w->buffer));
7393 if (save_match_data)
7394 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7395 if (NILP (Voverriding_local_map_menu_flag))
7396 {
7397 specbind (Qoverriding_terminal_local_map, Qnil);
7398 specbind (Qoverriding_local_map, Qnil);
7399 }
7400
7401 /* Run the Lucid hook. */
7402 safe_run_hooks (Qactivate_menubar_hook);
7403
7404 /* If it has changed current-menubar from previous value,
7405 really recompute the menu-bar from the value. */
7406 if (! NILP (Vlucid_menu_bar_dirty_flag))
7407 call0 (Qrecompute_lucid_menubar);
7408
7409 safe_run_hooks (Qmenu_bar_update_hook);
7410 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7411
7412 /* Redisplay the menu bar in case we changed it. */
7413 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7414 if (FRAME_WINDOW_P (f)
7415 #if defined (macintosh)
7416 /* All frames on Mac OS share the same menubar. So only the
7417 selected frame should be allowed to set it. */
7418 && f == SELECTED_FRAME ()
7419 #endif
7420 )
7421 set_frame_menubar (f, 0, 0);
7422 else
7423 /* On a terminal screen, the menu bar is an ordinary screen
7424 line, and this makes it get updated. */
7425 w->update_mode_line = Qt;
7426 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7427 /* In the non-toolkit version, the menu bar is an ordinary screen
7428 line, and this makes it get updated. */
7429 w->update_mode_line = Qt;
7430 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7431
7432 unbind_to (count, Qnil);
7433 set_buffer_internal_1 (prev);
7434 }
7435 }
7436 }
7437
7438
7439 \f
7440 /***********************************************************************
7441 Tool-bars
7442 ***********************************************************************/
7443
7444 #ifdef HAVE_WINDOW_SYSTEM
7445
7446 /* Update the tool-bar item list for frame F. This has to be done
7447 before we start to fill in any display lines. Called from
7448 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7449 and restore it here. */
7450
7451 static void
7452 update_tool_bar (f, save_match_data)
7453 struct frame *f;
7454 int save_match_data;
7455 {
7456 if (WINDOWP (f->tool_bar_window)
7457 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7458 {
7459 Lisp_Object window;
7460 struct window *w;
7461
7462 window = FRAME_SELECTED_WINDOW (f);
7463 w = XWINDOW (window);
7464
7465 /* If the user has switched buffers or windows, we need to
7466 recompute to reflect the new bindings. But we'll
7467 recompute when update_mode_lines is set too; that means
7468 that people can use force-mode-line-update to request
7469 that the menu bar be recomputed. The adverse effect on
7470 the rest of the redisplay algorithm is about the same as
7471 windows_or_buffers_changed anyway. */
7472 if (windows_or_buffers_changed
7473 || !NILP (w->update_mode_line)
7474 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7475 < BUF_MODIFF (XBUFFER (w->buffer)))
7476 != !NILP (w->last_had_star))
7477 || ((!NILP (Vtransient_mark_mode)
7478 && !NILP (XBUFFER (w->buffer)->mark_active))
7479 != !NILP (w->region_showing)))
7480 {
7481 struct buffer *prev = current_buffer;
7482 int count = BINDING_STACK_SIZE ();
7483
7484 /* Set current_buffer to the buffer of the selected
7485 window of the frame, so that we get the right local
7486 keymaps. */
7487 set_buffer_internal_1 (XBUFFER (w->buffer));
7488
7489 /* Save match data, if we must. */
7490 if (save_match_data)
7491 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7492
7493 /* Make sure that we don't accidentally use bogus keymaps. */
7494 if (NILP (Voverriding_local_map_menu_flag))
7495 {
7496 specbind (Qoverriding_terminal_local_map, Qnil);
7497 specbind (Qoverriding_local_map, Qnil);
7498 }
7499
7500 /* Build desired tool-bar items from keymaps. */
7501 f->tool_bar_items
7502 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7503
7504 /* Redisplay the tool-bar in case we changed it. */
7505 w->update_mode_line = Qt;
7506
7507 unbind_to (count, Qnil);
7508 set_buffer_internal_1 (prev);
7509 }
7510 }
7511 }
7512
7513
7514 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7515 F's desired tool-bar contents. F->tool_bar_items must have
7516 been set up previously by calling prepare_menu_bars. */
7517
7518 static void
7519 build_desired_tool_bar_string (f)
7520 struct frame *f;
7521 {
7522 int i, size, size_needed;
7523 struct gcpro gcpro1, gcpro2, gcpro3;
7524 Lisp_Object image, plist, props;
7525
7526 image = plist = props = Qnil;
7527 GCPRO3 (image, plist, props);
7528
7529 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7530 Otherwise, make a new string. */
7531
7532 /* The size of the string we might be able to reuse. */
7533 size = (STRINGP (f->desired_tool_bar_string)
7534 ? XSTRING (f->desired_tool_bar_string)->size
7535 : 0);
7536
7537 /* We need one space in the string for each image. */
7538 size_needed = f->n_tool_bar_items;
7539
7540 /* Reuse f->desired_tool_bar_string, if possible. */
7541 if (size < size_needed || NILP (f->desired_tool_bar_string))
7542 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7543 make_number (' '));
7544 else
7545 {
7546 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7547 Fremove_text_properties (make_number (0), make_number (size),
7548 props, f->desired_tool_bar_string);
7549 }
7550
7551 /* Put a `display' property on the string for the images to display,
7552 put a `menu_item' property on tool-bar items with a value that
7553 is the index of the item in F's tool-bar item vector. */
7554 for (i = 0; i < f->n_tool_bar_items; ++i)
7555 {
7556 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7557
7558 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7559 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7560 int hmargin, vmargin, relief, idx, end;
7561 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7562 extern Lisp_Object Qlaplace;
7563
7564 /* If image is a vector, choose the image according to the
7565 button state. */
7566 image = PROP (TOOL_BAR_ITEM_IMAGES);
7567 if (VECTORP (image))
7568 {
7569 if (enabled_p)
7570 idx = (selected_p
7571 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7572 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7573 else
7574 idx = (selected_p
7575 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7576 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7577
7578 xassert (ASIZE (image) >= idx);
7579 image = AREF (image, idx);
7580 }
7581 else
7582 idx = -1;
7583
7584 /* Ignore invalid image specifications. */
7585 if (!valid_image_p (image))
7586 continue;
7587
7588 /* Display the tool-bar button pressed, or depressed. */
7589 plist = Fcopy_sequence (XCDR (image));
7590
7591 /* Compute margin and relief to draw. */
7592 relief = (tool_bar_button_relief > 0
7593 ? tool_bar_button_relief
7594 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7595 hmargin = vmargin = relief;
7596
7597 if (INTEGERP (Vtool_bar_button_margin)
7598 && XINT (Vtool_bar_button_margin) > 0)
7599 {
7600 hmargin += XFASTINT (Vtool_bar_button_margin);
7601 vmargin += XFASTINT (Vtool_bar_button_margin);
7602 }
7603 else if (CONSP (Vtool_bar_button_margin))
7604 {
7605 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7606 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7607 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7608
7609 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7610 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7611 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7612 }
7613
7614 if (auto_raise_tool_bar_buttons_p)
7615 {
7616 /* Add a `:relief' property to the image spec if the item is
7617 selected. */
7618 if (selected_p)
7619 {
7620 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7621 hmargin -= relief;
7622 vmargin -= relief;
7623 }
7624 }
7625 else
7626 {
7627 /* If image is selected, display it pressed, i.e. with a
7628 negative relief. If it's not selected, display it with a
7629 raised relief. */
7630 plist = Fplist_put (plist, QCrelief,
7631 (selected_p
7632 ? make_number (-relief)
7633 : make_number (relief)));
7634 hmargin -= relief;
7635 vmargin -= relief;
7636 }
7637
7638 /* Put a margin around the image. */
7639 if (hmargin || vmargin)
7640 {
7641 if (hmargin == vmargin)
7642 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7643 else
7644 plist = Fplist_put (plist, QCmargin,
7645 Fcons (make_number (hmargin),
7646 make_number (vmargin)));
7647 }
7648
7649 /* If button is not enabled, and we don't have special images
7650 for the disabled state, make the image appear disabled by
7651 applying an appropriate algorithm to it. */
7652 if (!enabled_p && idx < 0)
7653 plist = Fplist_put (plist, QCconversion, Qdisabled);
7654
7655 /* Put a `display' text property on the string for the image to
7656 display. Put a `menu-item' property on the string that gives
7657 the start of this item's properties in the tool-bar items
7658 vector. */
7659 image = Fcons (Qimage, plist);
7660 props = list4 (Qdisplay, image,
7661 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7662
7663 /* Let the last image hide all remaining spaces in the tool bar
7664 string. The string can be longer than needed when we reuse a
7665 previous string. */
7666 if (i + 1 == f->n_tool_bar_items)
7667 end = XSTRING (f->desired_tool_bar_string)->size;
7668 else
7669 end = i + 1;
7670 Fadd_text_properties (make_number (i), make_number (end),
7671 props, f->desired_tool_bar_string);
7672 #undef PROP
7673 }
7674
7675 UNGCPRO;
7676 }
7677
7678
7679 /* Display one line of the tool-bar of frame IT->f. */
7680
7681 static void
7682 display_tool_bar_line (it)
7683 struct it *it;
7684 {
7685 struct glyph_row *row = it->glyph_row;
7686 int max_x = it->last_visible_x;
7687 struct glyph *last;
7688
7689 prepare_desired_row (row);
7690 row->y = it->current_y;
7691
7692 /* Note that this isn't made use of if the face hasn't a box,
7693 so there's no need to check the face here. */
7694 it->start_of_box_run_p = 1;
7695
7696 while (it->current_x < max_x)
7697 {
7698 int x_before, x, n_glyphs_before, i, nglyphs;
7699
7700 /* Get the next display element. */
7701 if (!get_next_display_element (it))
7702 break;
7703
7704 /* Produce glyphs. */
7705 x_before = it->current_x;
7706 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7707 PRODUCE_GLYPHS (it);
7708
7709 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7710 i = 0;
7711 x = x_before;
7712 while (i < nglyphs)
7713 {
7714 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7715
7716 if (x + glyph->pixel_width > max_x)
7717 {
7718 /* Glyph doesn't fit on line. */
7719 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7720 it->current_x = x;
7721 goto out;
7722 }
7723
7724 ++it->hpos;
7725 x += glyph->pixel_width;
7726 ++i;
7727 }
7728
7729 /* Stop at line ends. */
7730 if (ITERATOR_AT_END_OF_LINE_P (it))
7731 break;
7732
7733 set_iterator_to_next (it, 1);
7734 }
7735
7736 out:;
7737
7738 row->displays_text_p = row->used[TEXT_AREA] != 0;
7739 extend_face_to_end_of_line (it);
7740 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7741 last->right_box_line_p = 1;
7742 if (last == row->glyphs[TEXT_AREA])
7743 last->left_box_line_p = 1;
7744 compute_line_metrics (it);
7745
7746 /* If line is empty, make it occupy the rest of the tool-bar. */
7747 if (!row->displays_text_p)
7748 {
7749 row->height = row->phys_height = it->last_visible_y - row->y;
7750 row->ascent = row->phys_ascent = 0;
7751 }
7752
7753 row->full_width_p = 1;
7754 row->continued_p = 0;
7755 row->truncated_on_left_p = 0;
7756 row->truncated_on_right_p = 0;
7757
7758 it->current_x = it->hpos = 0;
7759 it->current_y += row->height;
7760 ++it->vpos;
7761 ++it->glyph_row;
7762 }
7763
7764
7765 /* Value is the number of screen lines needed to make all tool-bar
7766 items of frame F visible. */
7767
7768 static int
7769 tool_bar_lines_needed (f)
7770 struct frame *f;
7771 {
7772 struct window *w = XWINDOW (f->tool_bar_window);
7773 struct it it;
7774
7775 /* Initialize an iterator for iteration over
7776 F->desired_tool_bar_string in the tool-bar window of frame F. */
7777 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7778 it.first_visible_x = 0;
7779 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7780 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7781
7782 while (!ITERATOR_AT_END_P (&it))
7783 {
7784 it.glyph_row = w->desired_matrix->rows;
7785 clear_glyph_row (it.glyph_row);
7786 display_tool_bar_line (&it);
7787 }
7788
7789 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7790 }
7791
7792
7793 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7794 0, 1, 0,
7795 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7796 (frame)
7797 Lisp_Object frame;
7798 {
7799 struct frame *f;
7800 struct window *w;
7801 int nlines = 0;
7802
7803 if (NILP (frame))
7804 frame = selected_frame;
7805 else
7806 CHECK_FRAME (frame, 0);
7807 f = XFRAME (frame);
7808
7809 if (WINDOWP (f->tool_bar_window)
7810 || (w = XWINDOW (f->tool_bar_window),
7811 XFASTINT (w->height) > 0))
7812 {
7813 update_tool_bar (f, 1);
7814 if (f->n_tool_bar_items)
7815 {
7816 build_desired_tool_bar_string (f);
7817 nlines = tool_bar_lines_needed (f);
7818 }
7819 }
7820
7821 return make_number (nlines);
7822 }
7823
7824
7825 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7826 height should be changed. */
7827
7828 static int
7829 redisplay_tool_bar (f)
7830 struct frame *f;
7831 {
7832 struct window *w;
7833 struct it it;
7834 struct glyph_row *row;
7835 int change_height_p = 0;
7836
7837 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7838 do anything. This means you must start with tool-bar-lines
7839 non-zero to get the auto-sizing effect. Or in other words, you
7840 can turn off tool-bars by specifying tool-bar-lines zero. */
7841 if (!WINDOWP (f->tool_bar_window)
7842 || (w = XWINDOW (f->tool_bar_window),
7843 XFASTINT (w->height) == 0))
7844 return 0;
7845
7846 /* Set up an iterator for the tool-bar window. */
7847 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7848 it.first_visible_x = 0;
7849 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7850 row = it.glyph_row;
7851
7852 /* Build a string that represents the contents of the tool-bar. */
7853 build_desired_tool_bar_string (f);
7854 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7855
7856 /* Display as many lines as needed to display all tool-bar items. */
7857 while (it.current_y < it.last_visible_y)
7858 display_tool_bar_line (&it);
7859
7860 /* It doesn't make much sense to try scrolling in the tool-bar
7861 window, so don't do it. */
7862 w->desired_matrix->no_scrolling_p = 1;
7863 w->must_be_updated_p = 1;
7864
7865 if (auto_resize_tool_bars_p)
7866 {
7867 int nlines;
7868
7869 /* If we couldn't display everything, change the tool-bar's
7870 height. */
7871 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7872 change_height_p = 1;
7873
7874 /* If there are blank lines at the end, except for a partially
7875 visible blank line at the end that is smaller than
7876 CANON_Y_UNIT, change the tool-bar's height. */
7877 row = it.glyph_row - 1;
7878 if (!row->displays_text_p
7879 && row->height >= CANON_Y_UNIT (f))
7880 change_height_p = 1;
7881
7882 /* If row displays tool-bar items, but is partially visible,
7883 change the tool-bar's height. */
7884 if (row->displays_text_p
7885 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7886 change_height_p = 1;
7887
7888 /* Resize windows as needed by changing the `tool-bar-lines'
7889 frame parameter. */
7890 if (change_height_p
7891 && (nlines = tool_bar_lines_needed (f),
7892 nlines != XFASTINT (w->height)))
7893 {
7894 extern Lisp_Object Qtool_bar_lines;
7895 Lisp_Object frame;
7896 int old_height = XFASTINT (w->height);
7897
7898 XSETFRAME (frame, f);
7899 clear_glyph_matrix (w->desired_matrix);
7900 Fmodify_frame_parameters (frame,
7901 Fcons (Fcons (Qtool_bar_lines,
7902 make_number (nlines)),
7903 Qnil));
7904 if (XFASTINT (w->height) != old_height)
7905 fonts_changed_p = 1;
7906 }
7907 }
7908
7909 return change_height_p;
7910 }
7911
7912
7913 /* Get information about the tool-bar item which is displayed in GLYPH
7914 on frame F. Return in *PROP_IDX the index where tool-bar item
7915 properties start in F->tool_bar_items. Value is zero if
7916 GLYPH doesn't display a tool-bar item. */
7917
7918 int
7919 tool_bar_item_info (f, glyph, prop_idx)
7920 struct frame *f;
7921 struct glyph *glyph;
7922 int *prop_idx;
7923 {
7924 Lisp_Object prop;
7925 int success_p;
7926
7927 /* Get the text property `menu-item' at pos. The value of that
7928 property is the start index of this item's properties in
7929 F->tool_bar_items. */
7930 prop = Fget_text_property (make_number (glyph->charpos),
7931 Qmenu_item, f->current_tool_bar_string);
7932 if (INTEGERP (prop))
7933 {
7934 *prop_idx = XINT (prop);
7935 success_p = 1;
7936 }
7937 else
7938 success_p = 0;
7939
7940 return success_p;
7941 }
7942
7943 #endif /* HAVE_WINDOW_SYSTEM */
7944
7945
7946 \f
7947 /************************************************************************
7948 Horizontal scrolling
7949 ************************************************************************/
7950
7951 static int hscroll_window_tree P_ ((Lisp_Object));
7952 static int hscroll_windows P_ ((Lisp_Object));
7953
7954 /* For all leaf windows in the window tree rooted at WINDOW, set their
7955 hscroll value so that PT is (i) visible in the window, and (ii) so
7956 that it is not within a certain margin at the window's left and
7957 right border. Value is non-zero if any window's hscroll has been
7958 changed. */
7959
7960 static int
7961 hscroll_window_tree (window)
7962 Lisp_Object window;
7963 {
7964 int hscrolled_p = 0;
7965
7966 while (WINDOWP (window))
7967 {
7968 struct window *w = XWINDOW (window);
7969
7970 if (WINDOWP (w->hchild))
7971 hscrolled_p |= hscroll_window_tree (w->hchild);
7972 else if (WINDOWP (w->vchild))
7973 hscrolled_p |= hscroll_window_tree (w->vchild);
7974 else if (w->cursor.vpos >= 0)
7975 {
7976 int hscroll_margin, text_area_x, text_area_y;
7977 int text_area_width, text_area_height;
7978 struct glyph_row *current_cursor_row
7979 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7980 struct glyph_row *desired_cursor_row
7981 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7982 struct glyph_row *cursor_row
7983 = (desired_cursor_row->enabled_p
7984 ? desired_cursor_row
7985 : current_cursor_row);
7986
7987 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7988 &text_area_width, &text_area_height);
7989
7990 /* Scroll when cursor is inside this scroll margin. */
7991 /* Shouldn't we export this `5' for customization ? -stef */
7992 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7993
7994 if ((XFASTINT (w->hscroll)
7995 && w->cursor.x < hscroll_margin)
7996 || (cursor_row->enabled_p
7997 && cursor_row->truncated_on_right_p
7998 && (w->cursor.x > text_area_width - hscroll_margin)))
7999 {
8000 struct it it;
8001 int hscroll;
8002 struct buffer *saved_current_buffer;
8003 int pt;
8004
8005 /* Find point in a display of infinite width. */
8006 saved_current_buffer = current_buffer;
8007 current_buffer = XBUFFER (w->buffer);
8008
8009 if (w == XWINDOW (selected_window))
8010 pt = BUF_PT (current_buffer);
8011 else
8012 {
8013 pt = marker_position (w->pointm);
8014 pt = max (BEGV, pt);
8015 pt = min (ZV, pt);
8016 }
8017
8018 /* Move iterator to pt starting at cursor_row->start in
8019 a line with infinite width. */
8020 init_to_row_start (&it, w, cursor_row);
8021 it.last_visible_x = INFINITY;
8022 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8023 current_buffer = saved_current_buffer;
8024
8025 /* Center cursor in window. */
8026 hscroll = (max (0, it.current_x - text_area_width / 2)
8027 / CANON_X_UNIT (it.f));
8028 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8029
8030 /* Don't call Fset_window_hscroll if value hasn't
8031 changed because it will prevent redisplay
8032 optimizations. */
8033 if (XFASTINT (w->hscroll) != hscroll)
8034 {
8035 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8036 w->hscroll = make_number (hscroll);
8037 hscrolled_p = 1;
8038 }
8039 }
8040 }
8041
8042 window = w->next;
8043 }
8044
8045 /* Value is non-zero if hscroll of any leaf window has been changed. */
8046 return hscrolled_p;
8047 }
8048
8049
8050 /* Set hscroll so that cursor is visible and not inside horizontal
8051 scroll margins for all windows in the tree rooted at WINDOW. See
8052 also hscroll_window_tree above. Value is non-zero if any window's
8053 hscroll has been changed. If it has, desired matrices on the frame
8054 of WINDOW are cleared. */
8055
8056 static int
8057 hscroll_windows (window)
8058 Lisp_Object window;
8059 {
8060 int hscrolled_p;
8061
8062 if (automatic_hscrolling_p)
8063 {
8064 hscrolled_p = hscroll_window_tree (window);
8065 if (hscrolled_p)
8066 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8067 }
8068 else
8069 hscrolled_p = 0;
8070 return hscrolled_p;
8071 }
8072
8073
8074 \f
8075 /************************************************************************
8076 Redisplay
8077 ************************************************************************/
8078
8079 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8080 to a non-zero value. This is sometimes handy to have in a debugger
8081 session. */
8082
8083 #if GLYPH_DEBUG
8084
8085 /* First and last unchanged row for try_window_id. */
8086
8087 int debug_first_unchanged_at_end_vpos;
8088 int debug_last_unchanged_at_beg_vpos;
8089
8090 /* Delta vpos and y. */
8091
8092 int debug_dvpos, debug_dy;
8093
8094 /* Delta in characters and bytes for try_window_id. */
8095
8096 int debug_delta, debug_delta_bytes;
8097
8098 /* Values of window_end_pos and window_end_vpos at the end of
8099 try_window_id. */
8100
8101 int debug_end_pos, debug_end_vpos;
8102
8103 /* Append a string to W->desired_matrix->method. FMT is a printf
8104 format string. A1...A9 are a supplement for a variable-length
8105 argument list. If trace_redisplay_p is non-zero also printf the
8106 resulting string to stderr. */
8107
8108 static void
8109 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8110 struct window *w;
8111 char *fmt;
8112 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8113 {
8114 char buffer[512];
8115 char *method = w->desired_matrix->method;
8116 int len = strlen (method);
8117 int size = sizeof w->desired_matrix->method;
8118 int remaining = size - len - 1;
8119
8120 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8121 if (len && remaining)
8122 {
8123 method[len] = '|';
8124 --remaining, ++len;
8125 }
8126
8127 strncpy (method + len, buffer, remaining);
8128
8129 if (trace_redisplay_p)
8130 fprintf (stderr, "%p (%s): %s\n",
8131 w,
8132 ((BUFFERP (w->buffer)
8133 && STRINGP (XBUFFER (w->buffer)->name))
8134 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
8135 : "no buffer"),
8136 buffer);
8137 }
8138
8139 #endif /* GLYPH_DEBUG */
8140
8141
8142 /* This counter is used to clear the face cache every once in a while
8143 in redisplay_internal. It is incremented for each redisplay.
8144 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
8145 cleared. */
8146
8147 #define CLEAR_FACE_CACHE_COUNT 10000
8148 static int clear_face_cache_count;
8149
8150 /* Record the previous terminal frame we displayed. */
8151
8152 static struct frame *previous_terminal_frame;
8153
8154 /* Non-zero while redisplay_internal is in progress. */
8155
8156 int redisplaying_p;
8157
8158
8159 /* Value is non-zero if all changes in window W, which displays
8160 current_buffer, are in the text between START and END. START is a
8161 buffer position, END is given as a distance from Z. Used in
8162 redisplay_internal for display optimization. */
8163
8164 static INLINE int
8165 text_outside_line_unchanged_p (w, start, end)
8166 struct window *w;
8167 int start, end;
8168 {
8169 int unchanged_p = 1;
8170
8171 /* If text or overlays have changed, see where. */
8172 if (XFASTINT (w->last_modified) < MODIFF
8173 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8174 {
8175 /* Gap in the line? */
8176 if (GPT < start || Z - GPT < end)
8177 unchanged_p = 0;
8178
8179 /* Changes start in front of the line, or end after it? */
8180 if (unchanged_p
8181 && (BEG_UNCHANGED < start - 1
8182 || END_UNCHANGED < end))
8183 unchanged_p = 0;
8184
8185 /* If selective display, can't optimize if changes start at the
8186 beginning of the line. */
8187 if (unchanged_p
8188 && INTEGERP (current_buffer->selective_display)
8189 && XINT (current_buffer->selective_display) > 0
8190 && (BEG_UNCHANGED < start || GPT <= start))
8191 unchanged_p = 0;
8192
8193 /* If there are overlays at the start or end of the line, these
8194 may have overlay strings with newlines in them. A change at
8195 START, for instance, may actually concern the display of such
8196 overlay strings as well, and they are displayed on different
8197 lines. So, quickly rule out this case. (For the future, it
8198 might be desirable to implement something more telling than
8199 just BEG/END_UNCHANGED.) */
8200 if (unchanged_p)
8201 {
8202 if (BEG + BEG_UNCHANGED == start
8203 && overlay_touches_p (start))
8204 unchanged_p = 0;
8205 if (END_UNCHANGED == end
8206 && overlay_touches_p (Z - end))
8207 unchanged_p = 0;
8208 }
8209 }
8210
8211 return unchanged_p;
8212 }
8213
8214
8215 /* Do a frame update, taking possible shortcuts into account. This is
8216 the main external entry point for redisplay.
8217
8218 If the last redisplay displayed an echo area message and that message
8219 is no longer requested, we clear the echo area or bring back the
8220 mini-buffer if that is in use. */
8221
8222 void
8223 redisplay ()
8224 {
8225 redisplay_internal (0);
8226 }
8227
8228
8229 /* Return 1 if point moved out of or into a composition. Otherwise
8230 return 0. PREV_BUF and PREV_PT are the last point buffer and
8231 position. BUF and PT are the current point buffer and position. */
8232
8233 int
8234 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8235 struct buffer *prev_buf, *buf;
8236 int prev_pt, pt;
8237 {
8238 int start, end;
8239 Lisp_Object prop;
8240 Lisp_Object buffer;
8241
8242 XSETBUFFER (buffer, buf);
8243 /* Check a composition at the last point if point moved within the
8244 same buffer. */
8245 if (prev_buf == buf)
8246 {
8247 if (prev_pt == pt)
8248 /* Point didn't move. */
8249 return 0;
8250
8251 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8252 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8253 && COMPOSITION_VALID_P (start, end, prop)
8254 && start < prev_pt && end > prev_pt)
8255 /* The last point was within the composition. Return 1 iff
8256 point moved out of the composition. */
8257 return (pt <= start || pt >= end);
8258 }
8259
8260 /* Check a composition at the current point. */
8261 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8262 && find_composition (pt, -1, &start, &end, &prop, buffer)
8263 && COMPOSITION_VALID_P (start, end, prop)
8264 && start < pt && end > pt);
8265 }
8266
8267
8268 /* Reconsider the setting of B->clip_changed which is displayed
8269 in window W. */
8270
8271 static INLINE void
8272 reconsider_clip_changes (w, b)
8273 struct window *w;
8274 struct buffer *b;
8275 {
8276 if (b->prevent_redisplay_optimizations_p)
8277 b->clip_changed = 1;
8278 else if (b->clip_changed
8279 && !NILP (w->window_end_valid)
8280 && w->current_matrix->buffer == b
8281 && w->current_matrix->zv == BUF_ZV (b)
8282 && w->current_matrix->begv == BUF_BEGV (b))
8283 b->clip_changed = 0;
8284
8285 /* If display wasn't paused, and W is not a tool bar window, see if
8286 point has been moved into or out of a composition. In that case,
8287 we set b->clip_changed to 1 to force updating the screen. If
8288 b->clip_changed has already been set to 1, we can skip this
8289 check. */
8290 if (!b->clip_changed
8291 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8292 {
8293 int pt;
8294
8295 if (w == XWINDOW (selected_window))
8296 pt = BUF_PT (current_buffer);
8297 else
8298 pt = marker_position (w->pointm);
8299
8300 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8301 || pt != XINT (w->last_point))
8302 && check_point_in_composition (w->current_matrix->buffer,
8303 XINT (w->last_point),
8304 XBUFFER (w->buffer), pt))
8305 b->clip_changed = 1;
8306 }
8307 }
8308
8309
8310 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8311 response to any user action; therefore, we should preserve the echo
8312 area. (Actually, our caller does that job.) Perhaps in the future
8313 avoid recentering windows if it is not necessary; currently that
8314 causes some problems. */
8315
8316 static void
8317 redisplay_internal (preserve_echo_area)
8318 int preserve_echo_area;
8319 {
8320 struct window *w = XWINDOW (selected_window);
8321 struct frame *f = XFRAME (w->frame);
8322 int pause;
8323 int must_finish = 0;
8324 struct text_pos tlbufpos, tlendpos;
8325 int number_of_visible_frames;
8326 int count;
8327 struct frame *sf = SELECTED_FRAME ();
8328
8329 /* Non-zero means redisplay has to consider all windows on all
8330 frames. Zero means, only selected_window is considered. */
8331 int consider_all_windows_p;
8332
8333 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8334
8335 /* No redisplay if running in batch mode or frame is not yet fully
8336 initialized, or redisplay is explicitly turned off by setting
8337 Vinhibit_redisplay. */
8338 if (noninteractive
8339 || !NILP (Vinhibit_redisplay)
8340 || !f->glyphs_initialized_p)
8341 return;
8342
8343 /* The flag redisplay_performed_directly_p is set by
8344 direct_output_for_insert when it already did the whole screen
8345 update necessary. */
8346 if (redisplay_performed_directly_p)
8347 {
8348 redisplay_performed_directly_p = 0;
8349 if (!hscroll_windows (selected_window))
8350 return;
8351 }
8352
8353 #ifdef USE_X_TOOLKIT
8354 if (popup_activated ())
8355 return;
8356 #endif
8357
8358 /* I don't think this happens but let's be paranoid. */
8359 if (redisplaying_p)
8360 return;
8361
8362 /* Record a function that resets redisplaying_p to its old value
8363 when we leave this function. */
8364 count = BINDING_STACK_SIZE ();
8365 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8366 ++redisplaying_p;
8367
8368 retry:
8369 pause = 0;
8370 reconsider_clip_changes (w, current_buffer);
8371
8372 /* If new fonts have been loaded that make a glyph matrix adjustment
8373 necessary, do it. */
8374 if (fonts_changed_p)
8375 {
8376 adjust_glyphs (NULL);
8377 ++windows_or_buffers_changed;
8378 fonts_changed_p = 0;
8379 }
8380
8381 /* If face_change_count is non-zero, init_iterator will free all
8382 realized faces, which includes the faces referenced from current
8383 matrices. So, we can't reuse current matrices in this case. */
8384 if (face_change_count)
8385 ++windows_or_buffers_changed;
8386
8387 if (! FRAME_WINDOW_P (sf)
8388 && previous_terminal_frame != sf)
8389 {
8390 /* Since frames on an ASCII terminal share the same display
8391 area, displaying a different frame means redisplay the whole
8392 thing. */
8393 windows_or_buffers_changed++;
8394 SET_FRAME_GARBAGED (sf);
8395 XSETFRAME (Vterminal_frame, sf);
8396 }
8397 previous_terminal_frame = sf;
8398
8399 /* Set the visible flags for all frames. Do this before checking
8400 for resized or garbaged frames; they want to know if their frames
8401 are visible. See the comment in frame.h for
8402 FRAME_SAMPLE_VISIBILITY. */
8403 {
8404 Lisp_Object tail, frame;
8405
8406 number_of_visible_frames = 0;
8407
8408 FOR_EACH_FRAME (tail, frame)
8409 {
8410 struct frame *f = XFRAME (frame);
8411
8412 FRAME_SAMPLE_VISIBILITY (f);
8413 if (FRAME_VISIBLE_P (f))
8414 ++number_of_visible_frames;
8415 clear_desired_matrices (f);
8416 }
8417 }
8418
8419 /* Notice any pending interrupt request to change frame size. */
8420 do_pending_window_change (1);
8421
8422 /* Clear frames marked as garbaged. */
8423 if (frame_garbaged)
8424 clear_garbaged_frames ();
8425
8426 /* Build menubar and tool-bar items. */
8427 prepare_menu_bars ();
8428
8429 if (windows_or_buffers_changed)
8430 update_mode_lines++;
8431
8432 /* Detect case that we need to write or remove a star in the mode line. */
8433 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8434 {
8435 w->update_mode_line = Qt;
8436 if (buffer_shared > 1)
8437 update_mode_lines++;
8438 }
8439
8440 /* If %c is in the mode line, update it if needed. */
8441 if (!NILP (w->column_number_displayed)
8442 /* This alternative quickly identifies a common case
8443 where no change is needed. */
8444 && !(PT == XFASTINT (w->last_point)
8445 && XFASTINT (w->last_modified) >= MODIFF
8446 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8447 && XFASTINT (w->column_number_displayed) != current_column ())
8448 w->update_mode_line = Qt;
8449
8450 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8451
8452 /* The variable buffer_shared is set in redisplay_window and
8453 indicates that we redisplay a buffer in different windows. See
8454 there. */
8455 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
8456
8457 /* If specs for an arrow have changed, do thorough redisplay
8458 to ensure we remove any arrow that should no longer exist. */
8459 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8460 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8461 consider_all_windows_p = windows_or_buffers_changed = 1;
8462
8463 /* Normally the message* functions will have already displayed and
8464 updated the echo area, but the frame may have been trashed, or
8465 the update may have been preempted, so display the echo area
8466 again here. Checking message_cleared_p captures the case that
8467 the echo area should be cleared. */
8468 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8469 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8470 || (message_cleared_p && minibuf_level == 0))
8471 {
8472 int window_height_changed_p = echo_area_display (0);
8473 must_finish = 1;
8474
8475 /* If we don't display the current message, don't clear the
8476 message_cleared_p flag, because, if we did, we wouldn't clear
8477 the echo area in the next redisplay which doesn't preserve
8478 the echo area. */
8479 if (!display_last_displayed_message_p)
8480 message_cleared_p = 0;
8481
8482 if (fonts_changed_p)
8483 goto retry;
8484 else if (window_height_changed_p)
8485 {
8486 consider_all_windows_p = 1;
8487 ++update_mode_lines;
8488 ++windows_or_buffers_changed;
8489
8490 /* If window configuration was changed, frames may have been
8491 marked garbaged. Clear them or we will experience
8492 surprises wrt scrolling. */
8493 if (frame_garbaged)
8494 clear_garbaged_frames ();
8495 }
8496 }
8497 else if (EQ (selected_window, minibuf_window)
8498 && (current_buffer->clip_changed
8499 || XFASTINT (w->last_modified) < MODIFF
8500 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8501 && resize_mini_window (w, 0))
8502 {
8503 /* Resized active mini-window to fit the size of what it is
8504 showing if its contents might have changed. */
8505 must_finish = 1;
8506 consider_all_windows_p = 1;
8507 ++windows_or_buffers_changed;
8508 ++update_mode_lines;
8509
8510 /* If window configuration was changed, frames may have been
8511 marked garbaged. Clear them or we will experience
8512 surprises wrt scrolling. */
8513 if (frame_garbaged)
8514 clear_garbaged_frames ();
8515 }
8516
8517
8518 /* If showing the region, and mark has changed, we must redisplay
8519 the whole window. The assignment to this_line_start_pos prevents
8520 the optimization directly below this if-statement. */
8521 if (((!NILP (Vtransient_mark_mode)
8522 && !NILP (XBUFFER (w->buffer)->mark_active))
8523 != !NILP (w->region_showing))
8524 || (!NILP (w->region_showing)
8525 && !EQ (w->region_showing,
8526 Fmarker_position (XBUFFER (w->buffer)->mark))))
8527 CHARPOS (this_line_start_pos) = 0;
8528
8529 /* Optimize the case that only the line containing the cursor in the
8530 selected window has changed. Variables starting with this_ are
8531 set in display_line and record information about the line
8532 containing the cursor. */
8533 tlbufpos = this_line_start_pos;
8534 tlendpos = this_line_end_pos;
8535 if (!consider_all_windows_p
8536 && CHARPOS (tlbufpos) > 0
8537 && NILP (w->update_mode_line)
8538 && !current_buffer->clip_changed
8539 && FRAME_VISIBLE_P (XFRAME (w->frame))
8540 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8541 /* Make sure recorded data applies to current buffer, etc. */
8542 && this_line_buffer == current_buffer
8543 && current_buffer == XBUFFER (w->buffer)
8544 && NILP (w->force_start)
8545 /* Point must be on the line that we have info recorded about. */
8546 && PT >= CHARPOS (tlbufpos)
8547 && PT <= Z - CHARPOS (tlendpos)
8548 /* All text outside that line, including its final newline,
8549 must be unchanged */
8550 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8551 CHARPOS (tlendpos)))
8552 {
8553 if (CHARPOS (tlbufpos) > BEGV
8554 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8555 && (CHARPOS (tlbufpos) == ZV
8556 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8557 /* Former continuation line has disappeared by becoming empty */
8558 goto cancel;
8559 else if (XFASTINT (w->last_modified) < MODIFF
8560 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8561 || MINI_WINDOW_P (w))
8562 {
8563 /* We have to handle the case of continuation around a
8564 wide-column character (See the comment in indent.c around
8565 line 885).
8566
8567 For instance, in the following case:
8568
8569 -------- Insert --------
8570 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8571 J_I_ ==> J_I_ `^^' are cursors.
8572 ^^ ^^
8573 -------- --------
8574
8575 As we have to redraw the line above, we should goto cancel. */
8576
8577 struct it it;
8578 int line_height_before = this_line_pixel_height;
8579
8580 /* Note that start_display will handle the case that the
8581 line starting at tlbufpos is a continuation lines. */
8582 start_display (&it, w, tlbufpos);
8583
8584 /* Implementation note: It this still necessary? */
8585 if (it.current_x != this_line_start_x)
8586 goto cancel;
8587
8588 TRACE ((stderr, "trying display optimization 1\n"));
8589 w->cursor.vpos = -1;
8590 overlay_arrow_seen = 0;
8591 it.vpos = this_line_vpos;
8592 it.current_y = this_line_y;
8593 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8594 display_line (&it);
8595
8596 /* If line contains point, is not continued,
8597 and ends at same distance from eob as before, we win */
8598 if (w->cursor.vpos >= 0
8599 /* Line is not continued, otherwise this_line_start_pos
8600 would have been set to 0 in display_line. */
8601 && CHARPOS (this_line_start_pos)
8602 /* Line ends as before. */
8603 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8604 /* Line has same height as before. Otherwise other lines
8605 would have to be shifted up or down. */
8606 && this_line_pixel_height == line_height_before)
8607 {
8608 /* If this is not the window's last line, we must adjust
8609 the charstarts of the lines below. */
8610 if (it.current_y < it.last_visible_y)
8611 {
8612 struct glyph_row *row
8613 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8614 int delta, delta_bytes;
8615
8616 if (Z - CHARPOS (tlendpos) == ZV)
8617 {
8618 /* This line ends at end of (accessible part of)
8619 buffer. There is no newline to count. */
8620 delta = (Z
8621 - CHARPOS (tlendpos)
8622 - MATRIX_ROW_START_CHARPOS (row));
8623 delta_bytes = (Z_BYTE
8624 - BYTEPOS (tlendpos)
8625 - MATRIX_ROW_START_BYTEPOS (row));
8626 }
8627 else
8628 {
8629 /* This line ends in a newline. Must take
8630 account of the newline and the rest of the
8631 text that follows. */
8632 delta = (Z
8633 - CHARPOS (tlendpos)
8634 - MATRIX_ROW_START_CHARPOS (row));
8635 delta_bytes = (Z_BYTE
8636 - BYTEPOS (tlendpos)
8637 - MATRIX_ROW_START_BYTEPOS (row));
8638 }
8639
8640 increment_matrix_positions (w->current_matrix,
8641 this_line_vpos + 1,
8642 w->current_matrix->nrows,
8643 delta, delta_bytes);
8644 }
8645
8646 /* If this row displays text now but previously didn't,
8647 or vice versa, w->window_end_vpos may have to be
8648 adjusted. */
8649 if ((it.glyph_row - 1)->displays_text_p)
8650 {
8651 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8652 XSETINT (w->window_end_vpos, this_line_vpos);
8653 }
8654 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8655 && this_line_vpos > 0)
8656 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8657 w->window_end_valid = Qnil;
8658
8659 /* Update hint: No need to try to scroll in update_window. */
8660 w->desired_matrix->no_scrolling_p = 1;
8661
8662 #if GLYPH_DEBUG
8663 *w->desired_matrix->method = 0;
8664 debug_method_add (w, "optimization 1");
8665 #endif
8666 goto update;
8667 }
8668 else
8669 goto cancel;
8670 }
8671 else if (/* Cursor position hasn't changed. */
8672 PT == XFASTINT (w->last_point)
8673 /* Make sure the cursor was last displayed
8674 in this window. Otherwise we have to reposition it. */
8675 && 0 <= w->cursor.vpos
8676 && XINT (w->height) > w->cursor.vpos)
8677 {
8678 if (!must_finish)
8679 {
8680 do_pending_window_change (1);
8681
8682 /* We used to always goto end_of_redisplay here, but this
8683 isn't enough if we have a blinking cursor. */
8684 if (w->cursor_off_p == w->last_cursor_off_p)
8685 goto end_of_redisplay;
8686 }
8687 goto update;
8688 }
8689 /* If highlighting the region, or if the cursor is in the echo area,
8690 then we can't just move the cursor. */
8691 else if (! (!NILP (Vtransient_mark_mode)
8692 && !NILP (current_buffer->mark_active))
8693 && (EQ (selected_window, current_buffer->last_selected_window)
8694 || highlight_nonselected_windows)
8695 && NILP (w->region_showing)
8696 && NILP (Vshow_trailing_whitespace)
8697 && !cursor_in_echo_area)
8698 {
8699 struct it it;
8700 struct glyph_row *row;
8701
8702 /* Skip from tlbufpos to PT and see where it is. Note that
8703 PT may be in invisible text. If so, we will end at the
8704 next visible position. */
8705 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8706 NULL, DEFAULT_FACE_ID);
8707 it.current_x = this_line_start_x;
8708 it.current_y = this_line_y;
8709 it.vpos = this_line_vpos;
8710
8711 /* The call to move_it_to stops in front of PT, but
8712 moves over before-strings. */
8713 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8714
8715 if (it.vpos == this_line_vpos
8716 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8717 row->enabled_p))
8718 {
8719 xassert (this_line_vpos == it.vpos);
8720 xassert (this_line_y == it.current_y);
8721 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8722 #if GLYPH_DEBUG
8723 *w->desired_matrix->method = 0;
8724 debug_method_add (w, "optimization 3");
8725 #endif
8726 goto update;
8727 }
8728 else
8729 goto cancel;
8730 }
8731
8732 cancel:
8733 /* Text changed drastically or point moved off of line. */
8734 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8735 }
8736
8737 CHARPOS (this_line_start_pos) = 0;
8738 consider_all_windows_p |= buffer_shared > 1;
8739 ++clear_face_cache_count;
8740
8741
8742 /* Build desired matrices, and update the display. If
8743 consider_all_windows_p is non-zero, do it for all windows on all
8744 frames. Otherwise do it for selected_window, only. */
8745
8746 if (consider_all_windows_p)
8747 {
8748 Lisp_Object tail, frame;
8749 int i, n = 0, size = 50;
8750 struct frame **updated
8751 = (struct frame **) alloca (size * sizeof *updated);
8752
8753 /* Clear the face cache eventually. */
8754 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8755 {
8756 clear_face_cache (0);
8757 clear_face_cache_count = 0;
8758 }
8759
8760 /* Recompute # windows showing selected buffer. This will be
8761 incremented each time such a window is displayed. */
8762 buffer_shared = 0;
8763
8764 FOR_EACH_FRAME (tail, frame)
8765 {
8766 struct frame *f = XFRAME (frame);
8767
8768 if (FRAME_WINDOW_P (f) || f == sf)
8769 {
8770 /* Mark all the scroll bars to be removed; we'll redeem
8771 the ones we want when we redisplay their windows. */
8772 if (condemn_scroll_bars_hook)
8773 condemn_scroll_bars_hook (f);
8774
8775 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8776 redisplay_windows (FRAME_ROOT_WINDOW (f));
8777
8778 /* Any scroll bars which redisplay_windows should have
8779 nuked should now go away. */
8780 if (judge_scroll_bars_hook)
8781 judge_scroll_bars_hook (f);
8782
8783 /* If fonts changed, display again. */
8784 if (fonts_changed_p)
8785 goto retry;
8786
8787 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8788 {
8789 /* See if we have to hscroll. */
8790 if (hscroll_windows (f->root_window))
8791 goto retry;
8792
8793 /* Prevent various kinds of signals during display
8794 update. stdio is not robust about handling
8795 signals, which can cause an apparent I/O
8796 error. */
8797 if (interrupt_input)
8798 unrequest_sigio ();
8799 stop_polling ();
8800
8801 /* Update the display. */
8802 set_window_update_flags (XWINDOW (f->root_window), 1);
8803 pause |= update_frame (f, 0, 0);
8804 if (pause)
8805 break;
8806
8807 if (n == size)
8808 {
8809 int nbytes = size * sizeof *updated;
8810 struct frame **p = (struct frame **) alloca (2 * nbytes);
8811 bcopy (updated, p, nbytes);
8812 size *= 2;
8813 }
8814
8815 updated[n++] = f;
8816 }
8817 }
8818 }
8819
8820 /* Do the mark_window_display_accurate after all windows have
8821 been redisplayed because this call resets flags in buffers
8822 which are needed for proper redisplay. */
8823 for (i = 0; i < n; ++i)
8824 {
8825 struct frame *f = updated[i];
8826 mark_window_display_accurate (f->root_window, 1);
8827 if (frame_up_to_date_hook)
8828 frame_up_to_date_hook (f);
8829 }
8830 }
8831 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8832 {
8833 Lisp_Object mini_window;
8834 struct frame *mini_frame;
8835
8836 redisplay_window (selected_window, 1);
8837
8838 /* Compare desired and current matrices, perform output. */
8839 update:
8840
8841 /* If fonts changed, display again. */
8842 if (fonts_changed_p)
8843 goto retry;
8844
8845 /* Prevent various kinds of signals during display update.
8846 stdio is not robust about handling signals,
8847 which can cause an apparent I/O error. */
8848 if (interrupt_input)
8849 unrequest_sigio ();
8850 stop_polling ();
8851
8852 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8853 {
8854 if (hscroll_windows (selected_window))
8855 goto retry;
8856
8857 XWINDOW (selected_window)->must_be_updated_p = 1;
8858 pause = update_frame (sf, 0, 0);
8859 }
8860
8861 /* We may have called echo_area_display at the top of this
8862 function. If the echo area is on another frame, that may
8863 have put text on a frame other than the selected one, so the
8864 above call to update_frame would not have caught it. Catch
8865 it here. */
8866 mini_window = FRAME_MINIBUF_WINDOW (sf);
8867 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8868
8869 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8870 {
8871 XWINDOW (mini_window)->must_be_updated_p = 1;
8872 pause |= update_frame (mini_frame, 0, 0);
8873 if (!pause && hscroll_windows (mini_window))
8874 goto retry;
8875 }
8876 }
8877
8878 /* If display was paused because of pending input, make sure we do a
8879 thorough update the next time. */
8880 if (pause)
8881 {
8882 /* Prevent the optimization at the beginning of
8883 redisplay_internal that tries a single-line update of the
8884 line containing the cursor in the selected window. */
8885 CHARPOS (this_line_start_pos) = 0;
8886
8887 /* Let the overlay arrow be updated the next time. */
8888 if (!NILP (last_arrow_position))
8889 {
8890 last_arrow_position = Qt;
8891 last_arrow_string = Qt;
8892 }
8893
8894 /* If we pause after scrolling, some rows in the current
8895 matrices of some windows are not valid. */
8896 if (!WINDOW_FULL_WIDTH_P (w)
8897 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8898 update_mode_lines = 1;
8899 }
8900 else
8901 {
8902 if (!consider_all_windows_p)
8903 {
8904 /* This has already been done above if
8905 consider_all_windows_p is set. */
8906 mark_window_display_accurate_1 (w, 1);
8907
8908 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8909 last_arrow_string = Voverlay_arrow_string;
8910
8911 if (frame_up_to_date_hook != 0)
8912 frame_up_to_date_hook (sf);
8913 }
8914
8915 update_mode_lines = 0;
8916 windows_or_buffers_changed = 0;
8917 }
8918
8919 /* Start SIGIO interrupts coming again. Having them off during the
8920 code above makes it less likely one will discard output, but not
8921 impossible, since there might be stuff in the system buffer here.
8922 But it is much hairier to try to do anything about that. */
8923 if (interrupt_input)
8924 request_sigio ();
8925 start_polling ();
8926
8927 /* If a frame has become visible which was not before, redisplay
8928 again, so that we display it. Expose events for such a frame
8929 (which it gets when becoming visible) don't call the parts of
8930 redisplay constructing glyphs, so simply exposing a frame won't
8931 display anything in this case. So, we have to display these
8932 frames here explicitly. */
8933 if (!pause)
8934 {
8935 Lisp_Object tail, frame;
8936 int new_count = 0;
8937
8938 FOR_EACH_FRAME (tail, frame)
8939 {
8940 int this_is_visible = 0;
8941
8942 if (XFRAME (frame)->visible)
8943 this_is_visible = 1;
8944 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8945 if (XFRAME (frame)->visible)
8946 this_is_visible = 1;
8947
8948 if (this_is_visible)
8949 new_count++;
8950 }
8951
8952 if (new_count != number_of_visible_frames)
8953 windows_or_buffers_changed++;
8954 }
8955
8956 /* Change frame size now if a change is pending. */
8957 do_pending_window_change (1);
8958
8959 /* If we just did a pending size change, or have additional
8960 visible frames, redisplay again. */
8961 if (windows_or_buffers_changed && !pause)
8962 goto retry;
8963
8964 end_of_redisplay:;
8965
8966 unbind_to (count, Qnil);
8967 }
8968
8969
8970 /* Redisplay, but leave alone any recent echo area message unless
8971 another message has been requested in its place.
8972
8973 This is useful in situations where you need to redisplay but no
8974 user action has occurred, making it inappropriate for the message
8975 area to be cleared. See tracking_off and
8976 wait_reading_process_input for examples of these situations.
8977
8978 FROM_WHERE is an integer saying from where this function was
8979 called. This is useful for debugging. */
8980
8981 void
8982 redisplay_preserve_echo_area (from_where)
8983 int from_where;
8984 {
8985 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8986
8987 if (!NILP (echo_area_buffer[1]))
8988 {
8989 /* We have a previously displayed message, but no current
8990 message. Redisplay the previous message. */
8991 display_last_displayed_message_p = 1;
8992 redisplay_internal (1);
8993 display_last_displayed_message_p = 0;
8994 }
8995 else
8996 redisplay_internal (1);
8997 }
8998
8999
9000 /* Function registered with record_unwind_protect in
9001 redisplay_internal. Clears the flag indicating that a redisplay is
9002 in progress. */
9003
9004 static Lisp_Object
9005 unwind_redisplay (old_redisplaying_p)
9006 Lisp_Object old_redisplaying_p;
9007 {
9008 redisplaying_p = XFASTINT (old_redisplaying_p);
9009 return Qnil;
9010 }
9011
9012
9013 /* Mark the display of window W as accurate or inaccurate. If
9014 ACCURATE_P is non-zero mark display of W as accurate. If
9015 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9016 redisplay_internal is called. */
9017
9018 static void
9019 mark_window_display_accurate_1 (w, accurate_p)
9020 struct window *w;
9021 int accurate_p;
9022 {
9023 if (BUFFERP (w->buffer))
9024 {
9025 struct buffer *b = XBUFFER (w->buffer);
9026
9027 w->last_modified
9028 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9029 w->last_overlay_modified
9030 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9031 w->last_had_star
9032 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9033
9034 if (accurate_p)
9035 {
9036 b->clip_changed = 0;
9037 b->prevent_redisplay_optimizations_p = 0;
9038
9039 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9040 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9041 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9042 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9043
9044 w->current_matrix->buffer = b;
9045 w->current_matrix->begv = BUF_BEGV (b);
9046 w->current_matrix->zv = BUF_ZV (b);
9047
9048 w->last_cursor = w->cursor;
9049 w->last_cursor_off_p = w->cursor_off_p;
9050
9051 if (w == XWINDOW (selected_window))
9052 w->last_point = make_number (BUF_PT (b));
9053 else
9054 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9055 }
9056 }
9057
9058 if (accurate_p)
9059 {
9060 w->window_end_valid = w->buffer;
9061 #if 0 /* This is incorrect with variable-height lines. */
9062 xassert (XINT (w->window_end_vpos)
9063 < (XINT (w->height)
9064 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9065 #endif
9066 w->update_mode_line = Qnil;
9067 }
9068 }
9069
9070
9071 /* Mark the display of windows in the window tree rooted at WINDOW as
9072 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9073 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9074 be redisplayed the next time redisplay_internal is called. */
9075
9076 void
9077 mark_window_display_accurate (window, accurate_p)
9078 Lisp_Object window;
9079 int accurate_p;
9080 {
9081 struct window *w;
9082
9083 for (; !NILP (window); window = w->next)
9084 {
9085 w = XWINDOW (window);
9086 mark_window_display_accurate_1 (w, accurate_p);
9087
9088 if (!NILP (w->vchild))
9089 mark_window_display_accurate (w->vchild, accurate_p);
9090 if (!NILP (w->hchild))
9091 mark_window_display_accurate (w->hchild, accurate_p);
9092 }
9093
9094 if (accurate_p)
9095 {
9096 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9097 last_arrow_string = Voverlay_arrow_string;
9098 }
9099 else
9100 {
9101 /* Force a thorough redisplay the next time by setting
9102 last_arrow_position and last_arrow_string to t, which is
9103 unequal to any useful value of Voverlay_arrow_... */
9104 last_arrow_position = Qt;
9105 last_arrow_string = Qt;
9106 }
9107 }
9108
9109
9110 /* Return value in display table DP (Lisp_Char_Table *) for character
9111 C. Since a display table doesn't have any parent, we don't have to
9112 follow parent. Do not call this function directly but use the
9113 macro DISP_CHAR_VECTOR. */
9114
9115 Lisp_Object
9116 disp_char_vector (dp, c)
9117 struct Lisp_Char_Table *dp;
9118 int c;
9119 {
9120 int code[4], i;
9121 Lisp_Object val;
9122
9123 if (SINGLE_BYTE_CHAR_P (c))
9124 return (dp->contents[c]);
9125
9126 SPLIT_CHAR (c, code[0], code[1], code[2]);
9127 if (code[1] < 32)
9128 code[1] = -1;
9129 else if (code[2] < 32)
9130 code[2] = -1;
9131
9132 /* Here, the possible range of code[0] (== charset ID) is
9133 128..max_charset. Since the top level char table contains data
9134 for multibyte characters after 256th element, we must increment
9135 code[0] by 128 to get a correct index. */
9136 code[0] += 128;
9137 code[3] = -1; /* anchor */
9138
9139 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9140 {
9141 val = dp->contents[code[i]];
9142 if (!SUB_CHAR_TABLE_P (val))
9143 return (NILP (val) ? dp->defalt : val);
9144 }
9145
9146 /* Here, val is a sub char table. We return the default value of
9147 it. */
9148 return (dp->defalt);
9149 }
9150
9151
9152 \f
9153 /***********************************************************************
9154 Window Redisplay
9155 ***********************************************************************/
9156
9157 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9158
9159 static void
9160 redisplay_windows (window)
9161 Lisp_Object window;
9162 {
9163 while (!NILP (window))
9164 {
9165 struct window *w = XWINDOW (window);
9166
9167 if (!NILP (w->hchild))
9168 redisplay_windows (w->hchild);
9169 else if (!NILP (w->vchild))
9170 redisplay_windows (w->vchild);
9171 else
9172 redisplay_window (window, 0);
9173
9174 window = w->next;
9175 }
9176 }
9177
9178
9179 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9180 DELTA is the number of bytes by which positions recorded in ROW
9181 differ from current buffer positions. */
9182
9183 void
9184 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9185 struct window *w;
9186 struct glyph_row *row;
9187 struct glyph_matrix *matrix;
9188 int delta, delta_bytes, dy, dvpos;
9189 {
9190 struct glyph *glyph = row->glyphs[TEXT_AREA];
9191 struct glyph *end = glyph + row->used[TEXT_AREA];
9192 int x = row->x;
9193 int pt_old = PT - delta;
9194
9195 /* Skip over glyphs not having an object at the start of the row.
9196 These are special glyphs like truncation marks on terminal
9197 frames. */
9198 if (row->displays_text_p)
9199 while (glyph < end
9200 && INTEGERP (glyph->object)
9201 && glyph->charpos < 0)
9202 {
9203 x += glyph->pixel_width;
9204 ++glyph;
9205 }
9206
9207 while (glyph < end
9208 && !INTEGERP (glyph->object)
9209 && (!BUFFERP (glyph->object)
9210 || glyph->charpos < pt_old))
9211 {
9212 x += glyph->pixel_width;
9213 ++glyph;
9214 }
9215
9216 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9217 w->cursor.x = x;
9218 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9219 w->cursor.y = row->y + dy;
9220
9221 if (w == XWINDOW (selected_window))
9222 {
9223 if (!row->continued_p
9224 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9225 && row->x == 0)
9226 {
9227 this_line_buffer = XBUFFER (w->buffer);
9228
9229 CHARPOS (this_line_start_pos)
9230 = MATRIX_ROW_START_CHARPOS (row) + delta;
9231 BYTEPOS (this_line_start_pos)
9232 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9233
9234 CHARPOS (this_line_end_pos)
9235 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9236 BYTEPOS (this_line_end_pos)
9237 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9238
9239 this_line_y = w->cursor.y;
9240 this_line_pixel_height = row->height;
9241 this_line_vpos = w->cursor.vpos;
9242 this_line_start_x = row->x;
9243 }
9244 else
9245 CHARPOS (this_line_start_pos) = 0;
9246 }
9247 }
9248
9249
9250 /* Run window scroll functions, if any, for WINDOW with new window
9251 start STARTP. Sets the window start of WINDOW to that position.
9252
9253 We assume that the window's buffer is really current. */
9254
9255 static INLINE struct text_pos
9256 run_window_scroll_functions (window, startp)
9257 Lisp_Object window;
9258 struct text_pos startp;
9259 {
9260 struct window *w = XWINDOW (window);
9261 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9262
9263 if (current_buffer != XBUFFER (w->buffer))
9264 abort ();
9265
9266 if (!NILP (Vwindow_scroll_functions))
9267 {
9268 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9269 make_number (CHARPOS (startp)));
9270 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9271 /* In case the hook functions switch buffers. */
9272 if (current_buffer != XBUFFER (w->buffer))
9273 set_buffer_internal_1 (XBUFFER (w->buffer));
9274 }
9275
9276 return startp;
9277 }
9278
9279
9280 /* Modify the desired matrix of window W and W->vscroll so that the
9281 line containing the cursor is fully visible. If this requires
9282 larger matrices than are allocated, set fonts_changed_p and return
9283 0. */
9284
9285 static int
9286 make_cursor_line_fully_visible (w)
9287 struct window *w;
9288 {
9289 struct glyph_matrix *matrix;
9290 struct glyph_row *row;
9291 int window_height;
9292
9293 /* It's not always possible to find the cursor, e.g, when a window
9294 is full of overlay strings. Don't do anything in that case. */
9295 if (w->cursor.vpos < 0)
9296 return 1;
9297
9298 matrix = w->desired_matrix;
9299 row = MATRIX_ROW (matrix, w->cursor.vpos);
9300
9301 /* If the cursor row is not partially visible, there's nothing
9302 to do. */
9303 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9304 return 1;
9305
9306 /* If the row the cursor is in is taller than the window's height,
9307 it's not clear what to do, so do nothing. */
9308 window_height = window_box_height (w);
9309 if (row->height >= window_height)
9310 return 1;
9311
9312 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9313 {
9314 int dy = row->height - row->visible_height;
9315 w->vscroll = 0;
9316 w->cursor.y += dy;
9317 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9318 }
9319 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9320 {
9321 int dy = - (row->height - row->visible_height);
9322 w->vscroll = dy;
9323 w->cursor.y += dy;
9324 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9325 }
9326
9327 /* When we change the cursor y-position of the selected window,
9328 change this_line_y as well so that the display optimization for
9329 the cursor line of the selected window in redisplay_internal uses
9330 the correct y-position. */
9331 if (w == XWINDOW (selected_window))
9332 this_line_y = w->cursor.y;
9333
9334 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9335 redisplay with larger matrices. */
9336 if (matrix->nrows < required_matrix_height (w))
9337 {
9338 fonts_changed_p = 1;
9339 return 0;
9340 }
9341
9342 return 1;
9343 }
9344
9345
9346 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9347 non-zero means only WINDOW is redisplayed in redisplay_internal.
9348 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9349 in redisplay_window to bring a partially visible line into view in
9350 the case that only the cursor has moved.
9351
9352 Value is
9353
9354 1 if scrolling succeeded
9355
9356 0 if scrolling didn't find point.
9357
9358 -1 if new fonts have been loaded so that we must interrupt
9359 redisplay, adjust glyph matrices, and try again. */
9360
9361 enum
9362 {
9363 SCROLLING_SUCCESS,
9364 SCROLLING_FAILED,
9365 SCROLLING_NEED_LARGER_MATRICES
9366 };
9367
9368 static int
9369 try_scrolling (window, just_this_one_p, scroll_conservatively,
9370 scroll_step, temp_scroll_step)
9371 Lisp_Object window;
9372 int just_this_one_p;
9373 int scroll_conservatively, scroll_step;
9374 int temp_scroll_step;
9375 {
9376 struct window *w = XWINDOW (window);
9377 struct frame *f = XFRAME (w->frame);
9378 struct text_pos scroll_margin_pos;
9379 struct text_pos pos;
9380 struct text_pos startp;
9381 struct it it;
9382 Lisp_Object window_end;
9383 int this_scroll_margin;
9384 int dy = 0;
9385 int scroll_max;
9386 int rc;
9387 int amount_to_scroll = 0;
9388 Lisp_Object aggressive;
9389 int height;
9390
9391 #if GLYPH_DEBUG
9392 debug_method_add (w, "try_scrolling");
9393 #endif
9394
9395 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9396
9397 /* Compute scroll margin height in pixels. We scroll when point is
9398 within this distance from the top or bottom of the window. */
9399 if (scroll_margin > 0)
9400 {
9401 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9402 this_scroll_margin *= CANON_Y_UNIT (f);
9403 }
9404 else
9405 this_scroll_margin = 0;
9406
9407 /* Compute how much we should try to scroll maximally to bring point
9408 into view. */
9409 if (scroll_step || scroll_conservatively || temp_scroll_step)
9410 scroll_max = max (scroll_step,
9411 max (scroll_conservatively, temp_scroll_step));
9412 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9413 || NUMBERP (current_buffer->scroll_up_aggressively))
9414 /* We're trying to scroll because of aggressive scrolling
9415 but no scroll_step is set. Choose an arbitrary one. Maybe
9416 there should be a variable for this. */
9417 scroll_max = 10;
9418 else
9419 scroll_max = 0;
9420 scroll_max *= CANON_Y_UNIT (f);
9421
9422 /* Decide whether we have to scroll down. Start at the window end
9423 and move this_scroll_margin up to find the position of the scroll
9424 margin. */
9425 window_end = Fwindow_end (window, Qt);
9426 CHARPOS (scroll_margin_pos) = XINT (window_end);
9427 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9428 if (this_scroll_margin)
9429 {
9430 start_display (&it, w, scroll_margin_pos);
9431 move_it_vertically (&it, - this_scroll_margin);
9432 scroll_margin_pos = it.current.pos;
9433 }
9434
9435 if (PT >= CHARPOS (scroll_margin_pos))
9436 {
9437 int y0;
9438
9439 /* Point is in the scroll margin at the bottom of the window, or
9440 below. Compute a new window start that makes point visible. */
9441
9442 /* Compute the distance from the scroll margin to PT.
9443 Give up if the distance is greater than scroll_max. */
9444 start_display (&it, w, scroll_margin_pos);
9445 y0 = it.current_y;
9446 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9447 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9448
9449 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9450 end, which is one line below the window. The iterator's
9451 current_y will be same as y0 in that case, but we have to
9452 scroll a line to make PT visible. That's the reason why 1 is
9453 added below. */
9454 dy = 1 + it.current_y - y0;
9455
9456 if (dy > scroll_max)
9457 return SCROLLING_FAILED;
9458
9459 /* Move the window start down. If scrolling conservatively,
9460 move it just enough down to make point visible. If
9461 scroll_step is set, move it down by scroll_step. */
9462 start_display (&it, w, startp);
9463
9464 if (scroll_conservatively)
9465 amount_to_scroll
9466 = max (max (dy, CANON_Y_UNIT (f)),
9467 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9468 else if (scroll_step || temp_scroll_step)
9469 amount_to_scroll = scroll_max;
9470 else
9471 {
9472 aggressive = current_buffer->scroll_down_aggressively;
9473 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9474 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9475 if (NUMBERP (aggressive))
9476 amount_to_scroll = XFLOATINT (aggressive) * height;
9477 }
9478
9479 if (amount_to_scroll <= 0)
9480 return SCROLLING_FAILED;
9481
9482 move_it_vertically (&it, amount_to_scroll);
9483 startp = it.current.pos;
9484 }
9485 else
9486 {
9487 /* See if point is inside the scroll margin at the top of the
9488 window. */
9489 scroll_margin_pos = startp;
9490 if (this_scroll_margin)
9491 {
9492 start_display (&it, w, startp);
9493 move_it_vertically (&it, this_scroll_margin);
9494 scroll_margin_pos = it.current.pos;
9495 }
9496
9497 if (PT < CHARPOS (scroll_margin_pos))
9498 {
9499 /* Point is in the scroll margin at the top of the window or
9500 above what is displayed in the window. */
9501 int y0;
9502
9503 /* Compute the vertical distance from PT to the scroll
9504 margin position. Give up if distance is greater than
9505 scroll_max. */
9506 SET_TEXT_POS (pos, PT, PT_BYTE);
9507 start_display (&it, w, pos);
9508 y0 = it.current_y;
9509 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9510 it.last_visible_y, -1,
9511 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9512 dy = it.current_y - y0;
9513 if (dy > scroll_max)
9514 return SCROLLING_FAILED;
9515
9516 /* Compute new window start. */
9517 start_display (&it, w, startp);
9518
9519 if (scroll_conservatively)
9520 amount_to_scroll =
9521 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9522 else if (scroll_step || temp_scroll_step)
9523 amount_to_scroll = scroll_max;
9524 else
9525 {
9526 aggressive = current_buffer->scroll_up_aggressively;
9527 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9528 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9529 if (NUMBERP (aggressive))
9530 amount_to_scroll = XFLOATINT (aggressive) * height;
9531 }
9532
9533 if (amount_to_scroll <= 0)
9534 return SCROLLING_FAILED;
9535
9536 move_it_vertically (&it, - amount_to_scroll);
9537 startp = it.current.pos;
9538 }
9539 }
9540
9541 /* Run window scroll functions. */
9542 startp = run_window_scroll_functions (window, startp);
9543
9544 /* Display the window. Give up if new fonts are loaded, or if point
9545 doesn't appear. */
9546 if (!try_window (window, startp))
9547 rc = SCROLLING_NEED_LARGER_MATRICES;
9548 else if (w->cursor.vpos < 0)
9549 {
9550 clear_glyph_matrix (w->desired_matrix);
9551 rc = SCROLLING_FAILED;
9552 }
9553 else
9554 {
9555 /* Maybe forget recorded base line for line number display. */
9556 if (!just_this_one_p
9557 || current_buffer->clip_changed
9558 || BEG_UNCHANGED < CHARPOS (startp))
9559 w->base_line_number = Qnil;
9560
9561 /* If cursor ends up on a partially visible line, shift display
9562 lines up or down. If that fails because we need larger
9563 matrices, give up. */
9564 if (!make_cursor_line_fully_visible (w))
9565 rc = SCROLLING_NEED_LARGER_MATRICES;
9566 else
9567 rc = SCROLLING_SUCCESS;
9568 }
9569
9570 return rc;
9571 }
9572
9573
9574 /* Compute a suitable window start for window W if display of W starts
9575 on a continuation line. Value is non-zero if a new window start
9576 was computed.
9577
9578 The new window start will be computed, based on W's width, starting
9579 from the start of the continued line. It is the start of the
9580 screen line with the minimum distance from the old start W->start. */
9581
9582 static int
9583 compute_window_start_on_continuation_line (w)
9584 struct window *w;
9585 {
9586 struct text_pos pos, start_pos;
9587 int window_start_changed_p = 0;
9588
9589 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9590
9591 /* If window start is on a continuation line... Window start may be
9592 < BEGV in case there's invisible text at the start of the
9593 buffer (M-x rmail, for example). */
9594 if (CHARPOS (start_pos) > BEGV
9595 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9596 {
9597 struct it it;
9598 struct glyph_row *row;
9599
9600 /* Handle the case that the window start is out of range. */
9601 if (CHARPOS (start_pos) < BEGV)
9602 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9603 else if (CHARPOS (start_pos) > ZV)
9604 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9605
9606 /* Find the start of the continued line. This should be fast
9607 because scan_buffer is fast (newline cache). */
9608 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9609 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9610 row, DEFAULT_FACE_ID);
9611 reseat_at_previous_visible_line_start (&it);
9612
9613 /* If the line start is "too far" away from the window start,
9614 say it takes too much time to compute a new window start. */
9615 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9616 < XFASTINT (w->height) * XFASTINT (w->width))
9617 {
9618 int min_distance, distance;
9619
9620 /* Move forward by display lines to find the new window
9621 start. If window width was enlarged, the new start can
9622 be expected to be > the old start. If window width was
9623 decreased, the new window start will be < the old start.
9624 So, we're looking for the display line start with the
9625 minimum distance from the old window start. */
9626 pos = it.current.pos;
9627 min_distance = INFINITY;
9628 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9629 distance < min_distance)
9630 {
9631 min_distance = distance;
9632 pos = it.current.pos;
9633 move_it_by_lines (&it, 1, 0);
9634 }
9635
9636 /* Set the window start there. */
9637 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9638 window_start_changed_p = 1;
9639 }
9640 }
9641
9642 return window_start_changed_p;
9643 }
9644
9645
9646 /* Try cursor movement in case text has not changes in window WINDOW,
9647 with window start STARTP. Value is
9648
9649 CURSOR_MOVEMENT_SUCCESS if successful
9650
9651 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
9652
9653 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
9654 display. *SCROLL_STEP is set to 1, under certain circumstances, if
9655 we want to scroll as if scroll-step were set to 1. See the code.
9656
9657 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
9658 which case we have to abort this redisplay, and adjust matrices
9659 first. */
9660
9661 enum
9662 {
9663 CURSOR_MOVEMENT_SUCCESS,
9664 CURSOR_MOVEMENT_CANNOT_BE_USED,
9665 CURSOR_MOVEMENT_MUST_SCROLL,
9666 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
9667 };
9668
9669 static int
9670 try_cursor_movement (window, startp, scroll_step)
9671 Lisp_Object window;
9672 struct text_pos startp;
9673 int *scroll_step;
9674 {
9675 struct window *w = XWINDOW (window);
9676 struct frame *f = XFRAME (w->frame);
9677 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
9678
9679 /* Handle case where text has not changed, only point, and it has
9680 not moved off the frame. */
9681 if (/* Point may be in this window. */
9682 PT >= CHARPOS (startp)
9683 /* Selective display hasn't changed. */
9684 && !current_buffer->clip_changed
9685 /* Function force-mode-line-update is used to force a thorough
9686 redisplay. It sets either windows_or_buffers_changed or
9687 update_mode_lines. So don't take a shortcut here for these
9688 cases. */
9689 && !update_mode_lines
9690 && !windows_or_buffers_changed
9691 /* Can't use this case if highlighting a region. When a
9692 region exists, cursor movement has to do more than just
9693 set the cursor. */
9694 && !(!NILP (Vtransient_mark_mode)
9695 && !NILP (current_buffer->mark_active))
9696 && NILP (w->region_showing)
9697 && NILP (Vshow_trailing_whitespace)
9698 /* Right after splitting windows, last_point may be nil. */
9699 && INTEGERP (w->last_point)
9700 /* This code is not used for mini-buffer for the sake of the case
9701 of redisplaying to replace an echo area message; since in
9702 that case the mini-buffer contents per se are usually
9703 unchanged. This code is of no real use in the mini-buffer
9704 since the handling of this_line_start_pos, etc., in redisplay
9705 handles the same cases. */
9706 && !EQ (window, minibuf_window)
9707 /* When splitting windows or for new windows, it happens that
9708 redisplay is called with a nil window_end_vpos or one being
9709 larger than the window. This should really be fixed in
9710 window.c. I don't have this on my list, now, so we do
9711 approximately the same as the old redisplay code. --gerd. */
9712 && INTEGERP (w->window_end_vpos)
9713 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9714 && (FRAME_WINDOW_P (f)
9715 || !MARKERP (Voverlay_arrow_position)
9716 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9717 {
9718 int this_scroll_margin;
9719 struct glyph_row *row = NULL;
9720
9721 #if GLYPH_DEBUG
9722 debug_method_add (w, "cursor movement");
9723 #endif
9724
9725 /* Scroll if point within this distance from the top or bottom
9726 of the window. This is a pixel value. */
9727 this_scroll_margin = max (0, scroll_margin);
9728 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9729 this_scroll_margin *= CANON_Y_UNIT (f);
9730
9731 /* Start with the row the cursor was displayed during the last
9732 not paused redisplay. Give up if that row is not valid. */
9733 if (w->last_cursor.vpos < 0
9734 || w->last_cursor.vpos >= w->current_matrix->nrows)
9735 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9736 else
9737 {
9738 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9739 if (row->mode_line_p)
9740 ++row;
9741 if (!row->enabled_p)
9742 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9743 }
9744
9745 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
9746 {
9747 int scroll_p = 0;
9748 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9749
9750 if (PT > XFASTINT (w->last_point))
9751 {
9752 /* Point has moved forward. */
9753 while (MATRIX_ROW_END_CHARPOS (row) < PT
9754 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9755 {
9756 xassert (row->enabled_p);
9757 ++row;
9758 }
9759
9760 /* The end position of a row equals the start position
9761 of the next row. If PT is there, we would rather
9762 display it in the next line. */
9763 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9764 && MATRIX_ROW_END_CHARPOS (row) == PT
9765 && !cursor_row_p (w, row))
9766 ++row;
9767
9768 /* If within the scroll margin, scroll. Note that
9769 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9770 the next line would be drawn, and that
9771 this_scroll_margin can be zero. */
9772 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9773 || PT > MATRIX_ROW_END_CHARPOS (row)
9774 /* Line is completely visible last line in window
9775 and PT is to be set in the next line. */
9776 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9777 && PT == MATRIX_ROW_END_CHARPOS (row)
9778 && !row->ends_at_zv_p
9779 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9780 scroll_p = 1;
9781 }
9782 else if (PT < XFASTINT (w->last_point))
9783 {
9784 /* Cursor has to be moved backward. Note that PT >=
9785 CHARPOS (startp) because of the outer
9786 if-statement. */
9787 while (!row->mode_line_p
9788 && (MATRIX_ROW_START_CHARPOS (row) > PT
9789 || (MATRIX_ROW_START_CHARPOS (row) == PT
9790 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9791 && (row->y > this_scroll_margin
9792 || CHARPOS (startp) == BEGV))
9793 {
9794 xassert (row->enabled_p);
9795 --row;
9796 }
9797
9798 /* Consider the following case: Window starts at BEGV,
9799 there is invisible, intangible text at BEGV, so that
9800 display starts at some point START > BEGV. It can
9801 happen that we are called with PT somewhere between
9802 BEGV and START. Try to handle that case. */
9803 if (row < w->current_matrix->rows
9804 || row->mode_line_p)
9805 {
9806 row = w->current_matrix->rows;
9807 if (row->mode_line_p)
9808 ++row;
9809 }
9810
9811 /* Due to newlines in overlay strings, we may have to
9812 skip forward over overlay strings. */
9813 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9814 && MATRIX_ROW_END_CHARPOS (row) == PT
9815 && !cursor_row_p (w, row))
9816 ++row;
9817
9818 /* If within the scroll margin, scroll. */
9819 if (row->y < this_scroll_margin
9820 && CHARPOS (startp) != BEGV)
9821 scroll_p = 1;
9822 }
9823
9824 if (PT < MATRIX_ROW_START_CHARPOS (row)
9825 || PT > MATRIX_ROW_END_CHARPOS (row))
9826 {
9827 /* if PT is not in the glyph row, give up. */
9828 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9829 }
9830 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9831 {
9832 if (PT == MATRIX_ROW_END_CHARPOS (row)
9833 && !row->ends_at_zv_p
9834 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
9835 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9836 else if (row->height > window_box_height (w))
9837 {
9838 /* If we end up in a partially visible line, let's
9839 make it fully visible, except when it's taller
9840 than the window, in which case we can't do much
9841 about it. */
9842 *scroll_step = 1;
9843 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9844 }
9845 else
9846 {
9847 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9848 try_window (window, startp);
9849 if (!make_cursor_line_fully_visible (w))
9850 rc = CURSOR_MOVEMENT_NEED_LARGER_MATRICES;
9851 else
9852 rc = CURSOR_MOVEMENT_SUCCESS;
9853 }
9854 }
9855 else if (scroll_p)
9856 rc = CURSOR_MOVEMENT_MUST_SCROLL;
9857 else
9858 {
9859 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9860 rc = CURSOR_MOVEMENT_SUCCESS;
9861 }
9862 }
9863 }
9864
9865 return rc;
9866 }
9867
9868
9869 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9870 selected_window is redisplayed. */
9871
9872 static void
9873 redisplay_window (window, just_this_one_p)
9874 Lisp_Object window;
9875 int just_this_one_p;
9876 {
9877 struct window *w = XWINDOW (window);
9878 struct frame *f = XFRAME (w->frame);
9879 struct buffer *buffer = XBUFFER (w->buffer);
9880 struct buffer *old = current_buffer;
9881 struct text_pos lpoint, opoint, startp;
9882 int update_mode_line;
9883 int tem;
9884 struct it it;
9885 /* Record it now because it's overwritten. */
9886 int current_matrix_up_to_date_p = 0;
9887 int temp_scroll_step = 0;
9888 int count = BINDING_STACK_SIZE ();
9889 int rc;
9890
9891 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9892 opoint = lpoint;
9893
9894 /* W must be a leaf window here. */
9895 xassert (!NILP (w->buffer));
9896 #if GLYPH_DEBUG
9897 *w->desired_matrix->method = 0;
9898 #endif
9899
9900 specbind (Qinhibit_point_motion_hooks, Qt);
9901
9902 reconsider_clip_changes (w, buffer);
9903
9904 /* Has the mode line to be updated? */
9905 update_mode_line = (!NILP (w->update_mode_line)
9906 || update_mode_lines
9907 || buffer->clip_changed);
9908
9909 if (MINI_WINDOW_P (w))
9910 {
9911 if (w == XWINDOW (echo_area_window)
9912 && !NILP (echo_area_buffer[0]))
9913 {
9914 if (update_mode_line)
9915 /* We may have to update a tty frame's menu bar or a
9916 tool-bar. Example `M-x C-h C-h C-g'. */
9917 goto finish_menu_bars;
9918 else
9919 /* We've already displayed the echo area glyphs in this window. */
9920 goto finish_scroll_bars;
9921 }
9922 else if (w != XWINDOW (minibuf_window))
9923 {
9924 /* W is a mini-buffer window, but it's not the currently
9925 active one, so clear it. */
9926 int yb = window_text_bottom_y (w);
9927 struct glyph_row *row;
9928 int y;
9929
9930 for (y = 0, row = w->desired_matrix->rows;
9931 y < yb;
9932 y += row->height, ++row)
9933 blank_row (w, row, y);
9934 goto finish_scroll_bars;
9935 }
9936
9937 clear_glyph_matrix (w->desired_matrix);
9938 }
9939
9940 /* Otherwise set up data on this window; select its buffer and point
9941 value. */
9942 /* Really select the buffer, for the sake of buffer-local
9943 variables. */
9944 set_buffer_internal_1 (XBUFFER (w->buffer));
9945 SET_TEXT_POS (opoint, PT, PT_BYTE);
9946
9947 current_matrix_up_to_date_p
9948 = (!NILP (w->window_end_valid)
9949 && !current_buffer->clip_changed
9950 && XFASTINT (w->last_modified) >= MODIFF
9951 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9952
9953 /* When windows_or_buffers_changed is non-zero, we can't rely on
9954 the window end being valid, so set it to nil there. */
9955 if (windows_or_buffers_changed)
9956 {
9957 /* If window starts on a continuation line, maybe adjust the
9958 window start in case the window's width changed. */
9959 if (XMARKER (w->start)->buffer == current_buffer)
9960 compute_window_start_on_continuation_line (w);
9961
9962 w->window_end_valid = Qnil;
9963 }
9964
9965 /* Some sanity checks. */
9966 CHECK_WINDOW_END (w);
9967 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9968 abort ();
9969 if (BYTEPOS (opoint) < CHARPOS (opoint))
9970 abort ();
9971
9972 /* If %c is in mode line, update it if needed. */
9973 if (!NILP (w->column_number_displayed)
9974 /* This alternative quickly identifies a common case
9975 where no change is needed. */
9976 && !(PT == XFASTINT (w->last_point)
9977 && XFASTINT (w->last_modified) >= MODIFF
9978 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9979 && XFASTINT (w->column_number_displayed) != current_column ())
9980 update_mode_line = 1;
9981
9982 /* Count number of windows showing the selected buffer. An indirect
9983 buffer counts as its base buffer. */
9984 if (!just_this_one_p)
9985 {
9986 struct buffer *current_base, *window_base;
9987 current_base = current_buffer;
9988 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9989 if (current_base->base_buffer)
9990 current_base = current_base->base_buffer;
9991 if (window_base->base_buffer)
9992 window_base = window_base->base_buffer;
9993 if (current_base == window_base)
9994 buffer_shared++;
9995 }
9996
9997 /* Point refers normally to the selected window. For any other
9998 window, set up appropriate value. */
9999 if (!EQ (window, selected_window))
10000 {
10001 int new_pt = XMARKER (w->pointm)->charpos;
10002 int new_pt_byte = marker_byte_position (w->pointm);
10003 if (new_pt < BEGV)
10004 {
10005 new_pt = BEGV;
10006 new_pt_byte = BEGV_BYTE;
10007 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10008 }
10009 else if (new_pt > (ZV - 1))
10010 {
10011 new_pt = ZV;
10012 new_pt_byte = ZV_BYTE;
10013 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10014 }
10015
10016 /* We don't use SET_PT so that the point-motion hooks don't run. */
10017 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10018 }
10019
10020 /* If any of the character widths specified in the display table
10021 have changed, invalidate the width run cache. It's true that
10022 this may be a bit late to catch such changes, but the rest of
10023 redisplay goes (non-fatally) haywire when the display table is
10024 changed, so why should we worry about doing any better? */
10025 if (current_buffer->width_run_cache)
10026 {
10027 struct Lisp_Char_Table *disptab = buffer_display_table ();
10028
10029 if (! disptab_matches_widthtab (disptab,
10030 XVECTOR (current_buffer->width_table)))
10031 {
10032 invalidate_region_cache (current_buffer,
10033 current_buffer->width_run_cache,
10034 BEG, Z);
10035 recompute_width_table (current_buffer, disptab);
10036 }
10037 }
10038
10039 /* If window-start is screwed up, choose a new one. */
10040 if (XMARKER (w->start)->buffer != current_buffer)
10041 goto recenter;
10042
10043 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10044
10045 /* If someone specified a new starting point but did not insist,
10046 check whether it can be used. */
10047 if (!NILP (w->optional_new_start)
10048 && CHARPOS (startp) >= BEGV
10049 && CHARPOS (startp) <= ZV)
10050 {
10051 w->optional_new_start = Qnil;
10052 start_display (&it, w, startp);
10053 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10054 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10055 if (IT_CHARPOS (it) == PT)
10056 w->force_start = Qt;
10057 }
10058
10059 /* Handle case where place to start displaying has been specified,
10060 unless the specified location is outside the accessible range. */
10061 if (!NILP (w->force_start)
10062 || w->frozen_window_start_p)
10063 {
10064 w->force_start = Qnil;
10065 w->vscroll = 0;
10066 w->window_end_valid = Qnil;
10067
10068 /* Forget any recorded base line for line number display. */
10069 if (!current_matrix_up_to_date_p
10070 || current_buffer->clip_changed)
10071 w->base_line_number = Qnil;
10072
10073 /* Redisplay the mode line. Select the buffer properly for that.
10074 Also, run the hook window-scroll-functions
10075 because we have scrolled. */
10076 /* Note, we do this after clearing force_start because
10077 if there's an error, it is better to forget about force_start
10078 than to get into an infinite loop calling the hook functions
10079 and having them get more errors. */
10080 if (!update_mode_line
10081 || ! NILP (Vwindow_scroll_functions))
10082 {
10083 update_mode_line = 1;
10084 w->update_mode_line = Qt;
10085 startp = run_window_scroll_functions (window, startp);
10086 }
10087
10088 w->last_modified = make_number (0);
10089 w->last_overlay_modified = make_number (0);
10090 if (CHARPOS (startp) < BEGV)
10091 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10092 else if (CHARPOS (startp) > ZV)
10093 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10094
10095 /* Redisplay, then check if cursor has been set during the
10096 redisplay. Give up if new fonts were loaded. */
10097 if (!try_window (window, startp))
10098 {
10099 w->force_start = Qt;
10100 clear_glyph_matrix (w->desired_matrix);
10101 goto finish_scroll_bars;
10102 }
10103
10104 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10105 {
10106 /* If point does not appear, try to move point so it does
10107 appear. The desired matrix has been built above, so we
10108 can use it here. */
10109 int window_height;
10110 struct glyph_row *row;
10111
10112 window_height = window_box_height (w) / 2;
10113 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10114 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
10115 ++row;
10116
10117 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10118 MATRIX_ROW_START_BYTEPOS (row));
10119
10120 if (w != XWINDOW (selected_window))
10121 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10122 else if (current_buffer == old)
10123 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10124
10125 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10126
10127 /* If we are highlighting the region, then we just changed
10128 the region, so redisplay to show it. */
10129 if (!NILP (Vtransient_mark_mode)
10130 && !NILP (current_buffer->mark_active))
10131 {
10132 clear_glyph_matrix (w->desired_matrix);
10133 if (!try_window (window, startp))
10134 goto need_larger_matrices;
10135 }
10136 }
10137
10138 if (!make_cursor_line_fully_visible (w))
10139 goto need_larger_matrices;
10140 #if GLYPH_DEBUG
10141 debug_method_add (w, "forced window start");
10142 #endif
10143 goto done;
10144 }
10145
10146 /* Handle case where text has not changed, only point, and it has
10147 not moved off the frame. */
10148 if (current_matrix_up_to_date_p
10149 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10150 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10151 {
10152 switch (rc)
10153 {
10154 case CURSOR_MOVEMENT_SUCCESS:
10155 goto done;
10156
10157 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10158 goto need_larger_matrices;
10159
10160 case CURSOR_MOVEMENT_MUST_SCROLL:
10161 goto try_to_scroll;
10162
10163 default:
10164 abort ();
10165 }
10166 }
10167 /* If current starting point was originally the beginning of a line
10168 but no longer is, find a new starting point. */
10169 else if (!NILP (w->start_at_line_beg)
10170 && !(CHARPOS (startp) <= BEGV
10171 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10172 {
10173 #if GLYPH_DEBUG
10174 debug_method_add (w, "recenter 1");
10175 #endif
10176 goto recenter;
10177 }
10178
10179 /* Try scrolling with try_window_id. Value is > 0 if update has
10180 been done, it is -1 if we know that the same window start will
10181 not work. It is 0 if unsuccessful for some other reason. */
10182 else if ((tem = try_window_id (w)) != 0)
10183 {
10184 #if GLYPH_DEBUG
10185 debug_method_add (w, "try_window_id %d", tem);
10186 #endif
10187
10188 if (fonts_changed_p)
10189 goto need_larger_matrices;
10190 if (tem > 0)
10191 goto done;
10192
10193 /* Otherwise try_window_id has returned -1 which means that we
10194 don't want the alternative below this comment to execute. */
10195 }
10196 else if (CHARPOS (startp) >= BEGV
10197 && CHARPOS (startp) <= ZV
10198 && PT >= CHARPOS (startp)
10199 && (CHARPOS (startp) < ZV
10200 /* Avoid starting at end of buffer. */
10201 || CHARPOS (startp) == BEGV
10202 || (XFASTINT (w->last_modified) >= MODIFF
10203 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10204 {
10205 #if GLYPH_DEBUG
10206 debug_method_add (w, "same window start");
10207 #endif
10208
10209 /* Try to redisplay starting at same place as before.
10210 If point has not moved off frame, accept the results. */
10211 if (!current_matrix_up_to_date_p
10212 /* Don't use try_window_reusing_current_matrix in this case
10213 because a window scroll function can have changed the
10214 buffer. */
10215 || !NILP (Vwindow_scroll_functions)
10216 || MINI_WINDOW_P (w)
10217 || !try_window_reusing_current_matrix (w))
10218 {
10219 IF_DEBUG (debug_method_add (w, "1"));
10220 try_window (window, startp);
10221 }
10222
10223 if (fonts_changed_p)
10224 goto need_larger_matrices;
10225
10226 if (w->cursor.vpos >= 0)
10227 {
10228 if (!just_this_one_p
10229 || current_buffer->clip_changed
10230 || BEG_UNCHANGED < CHARPOS (startp))
10231 /* Forget any recorded base line for line number display. */
10232 w->base_line_number = Qnil;
10233
10234 if (!make_cursor_line_fully_visible (w))
10235 goto need_larger_matrices;
10236 goto done;
10237 }
10238 else
10239 clear_glyph_matrix (w->desired_matrix);
10240 }
10241
10242 try_to_scroll:
10243
10244 w->last_modified = make_number (0);
10245 w->last_overlay_modified = make_number (0);
10246
10247 /* Redisplay the mode line. Select the buffer properly for that. */
10248 if (!update_mode_line)
10249 {
10250 update_mode_line = 1;
10251 w->update_mode_line = Qt;
10252 }
10253
10254 /* Try to scroll by specified few lines. */
10255 if ((scroll_conservatively
10256 || scroll_step
10257 || temp_scroll_step
10258 || NUMBERP (current_buffer->scroll_up_aggressively)
10259 || NUMBERP (current_buffer->scroll_down_aggressively))
10260 && !current_buffer->clip_changed
10261 && CHARPOS (startp) >= BEGV
10262 && CHARPOS (startp) <= ZV)
10263 {
10264 /* The function returns -1 if new fonts were loaded, 1 if
10265 successful, 0 if not successful. */
10266 int rc = try_scrolling (window, just_this_one_p,
10267 scroll_conservatively,
10268 scroll_step,
10269 temp_scroll_step);
10270 switch (rc)
10271 {
10272 case SCROLLING_SUCCESS:
10273 goto done;
10274
10275 case SCROLLING_NEED_LARGER_MATRICES:
10276 goto need_larger_matrices;
10277
10278 case SCROLLING_FAILED:
10279 break;
10280
10281 default:
10282 abort ();
10283 }
10284 }
10285
10286 /* Finally, just choose place to start which centers point */
10287
10288 recenter:
10289
10290 #if GLYPH_DEBUG
10291 debug_method_add (w, "recenter");
10292 #endif
10293
10294 /* w->vscroll = 0; */
10295
10296 /* Forget any previously recorded base line for line number display. */
10297 if (!current_matrix_up_to_date_p
10298 || current_buffer->clip_changed)
10299 w->base_line_number = Qnil;
10300
10301 /* Move backward half the height of the window. */
10302 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10303 it.current_y = it.last_visible_y;
10304 move_it_vertically_backward (&it, window_box_height (w) / 2);
10305 xassert (IT_CHARPOS (it) >= BEGV);
10306
10307 /* The function move_it_vertically_backward may move over more
10308 than the specified y-distance. If it->w is small, e.g. a
10309 mini-buffer window, we may end up in front of the window's
10310 display area. Start displaying at the start of the line
10311 containing PT in this case. */
10312 if (it.current_y <= 0)
10313 {
10314 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10315 move_it_vertically (&it, 0);
10316 xassert (IT_CHARPOS (it) <= PT);
10317 it.current_y = 0;
10318 }
10319
10320 it.current_x = it.hpos = 0;
10321
10322 /* Set startp here explicitly in case that helps avoid an infinite loop
10323 in case the window-scroll-functions functions get errors. */
10324 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10325
10326 /* Run scroll hooks. */
10327 startp = run_window_scroll_functions (window, it.current.pos);
10328
10329 /* Redisplay the window. */
10330 if (!current_matrix_up_to_date_p
10331 || windows_or_buffers_changed
10332 /* Don't use try_window_reusing_current_matrix in this case
10333 because it can have changed the buffer. */
10334 || !NILP (Vwindow_scroll_functions)
10335 || !just_this_one_p
10336 || MINI_WINDOW_P (w)
10337 || !try_window_reusing_current_matrix (w))
10338 try_window (window, startp);
10339
10340 /* If new fonts have been loaded (due to fontsets), give up. We
10341 have to start a new redisplay since we need to re-adjust glyph
10342 matrices. */
10343 if (fonts_changed_p)
10344 goto need_larger_matrices;
10345
10346 /* If cursor did not appear assume that the middle of the window is
10347 in the first line of the window. Do it again with the next line.
10348 (Imagine a window of height 100, displaying two lines of height
10349 60. Moving back 50 from it->last_visible_y will end in the first
10350 line.) */
10351 if (w->cursor.vpos < 0)
10352 {
10353 if (!NILP (w->window_end_valid)
10354 && PT >= Z - XFASTINT (w->window_end_pos))
10355 {
10356 clear_glyph_matrix (w->desired_matrix);
10357 move_it_by_lines (&it, 1, 0);
10358 try_window (window, it.current.pos);
10359 }
10360 else if (PT < IT_CHARPOS (it))
10361 {
10362 clear_glyph_matrix (w->desired_matrix);
10363 move_it_by_lines (&it, -1, 0);
10364 try_window (window, it.current.pos);
10365 }
10366 else
10367 {
10368 /* Not much we can do about it. */
10369 }
10370 }
10371
10372 /* Consider the following case: Window starts at BEGV, there is
10373 invisible, intangible text at BEGV, so that display starts at
10374 some point START > BEGV. It can happen that we are called with
10375 PT somewhere between BEGV and START. Try to handle that case. */
10376 if (w->cursor.vpos < 0)
10377 {
10378 struct glyph_row *row = w->current_matrix->rows;
10379 if (row->mode_line_p)
10380 ++row;
10381 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10382 }
10383
10384 if (!make_cursor_line_fully_visible (w))
10385 goto need_larger_matrices;
10386
10387 done:
10388
10389 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10390 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10391 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10392 ? Qt : Qnil);
10393
10394 /* Display the mode line, if we must. */
10395 if ((update_mode_line
10396 /* If window not full width, must redo its mode line
10397 if (a) the window to its side is being redone and
10398 (b) we do a frame-based redisplay. This is a consequence
10399 of how inverted lines are drawn in frame-based redisplay. */
10400 || (!just_this_one_p
10401 && !FRAME_WINDOW_P (f)
10402 && !WINDOW_FULL_WIDTH_P (w))
10403 /* Line number to display. */
10404 || INTEGERP (w->base_line_pos)
10405 /* Column number is displayed and different from the one displayed. */
10406 || (!NILP (w->column_number_displayed)
10407 && XFASTINT (w->column_number_displayed) != current_column ()))
10408 /* This means that the window has a mode line. */
10409 && (WINDOW_WANTS_MODELINE_P (w)
10410 || WINDOW_WANTS_HEADER_LINE_P (w)))
10411 {
10412 display_mode_lines (w);
10413
10414 /* If mode line height has changed, arrange for a thorough
10415 immediate redisplay using the correct mode line height. */
10416 if (WINDOW_WANTS_MODELINE_P (w)
10417 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
10418 {
10419 fonts_changed_p = 1;
10420 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10421 = DESIRED_MODE_LINE_HEIGHT (w);
10422 }
10423
10424 /* If top line height has changed, arrange for a thorough
10425 immediate redisplay using the correct mode line height. */
10426 if (WINDOW_WANTS_HEADER_LINE_P (w)
10427 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
10428 {
10429 fonts_changed_p = 1;
10430 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10431 = DESIRED_HEADER_LINE_HEIGHT (w);
10432 }
10433
10434 if (fonts_changed_p)
10435 goto need_larger_matrices;
10436 }
10437
10438 if (!line_number_displayed
10439 && !BUFFERP (w->base_line_pos))
10440 {
10441 w->base_line_pos = Qnil;
10442 w->base_line_number = Qnil;
10443 }
10444
10445 finish_menu_bars:
10446
10447 /* When we reach a frame's selected window, redo the frame's menu bar. */
10448 if (update_mode_line
10449 && EQ (FRAME_SELECTED_WINDOW (f), window))
10450 {
10451 int redisplay_menu_p = 0;
10452
10453 if (FRAME_WINDOW_P (f))
10454 {
10455 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
10456 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
10457 #else
10458 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10459 #endif
10460 }
10461 else
10462 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10463
10464 if (redisplay_menu_p)
10465 display_menu_bar (w);
10466
10467 #ifdef HAVE_WINDOW_SYSTEM
10468 if (WINDOWP (f->tool_bar_window)
10469 && (FRAME_TOOL_BAR_LINES (f) > 0
10470 || auto_resize_tool_bars_p))
10471 redisplay_tool_bar (f);
10472 #endif
10473 }
10474
10475 need_larger_matrices:
10476 ;
10477 finish_scroll_bars:
10478
10479 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
10480 {
10481 int start, end, whole;
10482
10483 /* Calculate the start and end positions for the current window.
10484 At some point, it would be nice to choose between scrollbars
10485 which reflect the whole buffer size, with special markers
10486 indicating narrowing, and scrollbars which reflect only the
10487 visible region.
10488
10489 Note that mini-buffers sometimes aren't displaying any text. */
10490 if (!MINI_WINDOW_P (w)
10491 || (w == XWINDOW (minibuf_window)
10492 && NILP (echo_area_buffer[0])))
10493 {
10494 whole = ZV - BEGV;
10495 start = marker_position (w->start) - BEGV;
10496 /* I don't think this is guaranteed to be right. For the
10497 moment, we'll pretend it is. */
10498 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
10499
10500 if (end < start)
10501 end = start;
10502 if (whole < (end - start))
10503 whole = end - start;
10504 }
10505 else
10506 start = end = whole = 0;
10507
10508 /* Indicate what this scroll bar ought to be displaying now. */
10509 set_vertical_scroll_bar_hook (w, end - start, whole, start);
10510
10511 /* Note that we actually used the scroll bar attached to this
10512 window, so it shouldn't be deleted at the end of redisplay. */
10513 redeem_scroll_bar_hook (w);
10514 }
10515
10516 /* Restore current_buffer and value of point in it. */
10517 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
10518 set_buffer_internal_1 (old);
10519 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
10520
10521 unbind_to (count, Qnil);
10522 }
10523
10524
10525 /* Build the complete desired matrix of WINDOW with a window start
10526 buffer position POS. Value is non-zero if successful. It is zero
10527 if fonts were loaded during redisplay which makes re-adjusting
10528 glyph matrices necessary. */
10529
10530 int
10531 try_window (window, pos)
10532 Lisp_Object window;
10533 struct text_pos pos;
10534 {
10535 struct window *w = XWINDOW (window);
10536 struct it it;
10537 struct glyph_row *last_text_row = NULL;
10538
10539 /* Make POS the new window start. */
10540 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10541
10542 /* Mark cursor position as unknown. No overlay arrow seen. */
10543 w->cursor.vpos = -1;
10544 overlay_arrow_seen = 0;
10545
10546 /* Initialize iterator and info to start at POS. */
10547 start_display (&it, w, pos);
10548
10549 /* Display all lines of W. */
10550 while (it.current_y < it.last_visible_y)
10551 {
10552 if (display_line (&it))
10553 last_text_row = it.glyph_row - 1;
10554 if (fonts_changed_p)
10555 return 0;
10556 }
10557
10558 /* If bottom moved off end of frame, change mode line percentage. */
10559 if (XFASTINT (w->window_end_pos) <= 0
10560 && Z != IT_CHARPOS (it))
10561 w->update_mode_line = Qt;
10562
10563 /* Set window_end_pos to the offset of the last character displayed
10564 on the window from the end of current_buffer. Set
10565 window_end_vpos to its row number. */
10566 if (last_text_row)
10567 {
10568 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10569 w->window_end_bytepos
10570 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10571 w->window_end_pos
10572 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10573 w->window_end_vpos
10574 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10575 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10576 ->displays_text_p);
10577 }
10578 else
10579 {
10580 w->window_end_bytepos = 0;
10581 w->window_end_pos = w->window_end_vpos = make_number (0);
10582 }
10583
10584 /* But that is not valid info until redisplay finishes. */
10585 w->window_end_valid = Qnil;
10586 return 1;
10587 }
10588
10589
10590 \f
10591 /************************************************************************
10592 Window redisplay reusing current matrix when buffer has not changed
10593 ************************************************************************/
10594
10595 /* Try redisplay of window W showing an unchanged buffer with a
10596 different window start than the last time it was displayed by
10597 reusing its current matrix. Value is non-zero if successful.
10598 W->start is the new window start. */
10599
10600 static int
10601 try_window_reusing_current_matrix (w)
10602 struct window *w;
10603 {
10604 struct frame *f = XFRAME (w->frame);
10605 struct glyph_row *row, *bottom_row;
10606 struct it it;
10607 struct run run;
10608 struct text_pos start, new_start;
10609 int nrows_scrolled, i;
10610 struct glyph_row *last_text_row;
10611 struct glyph_row *last_reused_text_row;
10612 struct glyph_row *start_row;
10613 int start_vpos, min_y, max_y;
10614
10615 if (/* This function doesn't handle terminal frames. */
10616 !FRAME_WINDOW_P (f)
10617 /* Don't try to reuse the display if windows have been split
10618 or such. */
10619 || windows_or_buffers_changed)
10620 return 0;
10621
10622 /* Can't do this if region may have changed. */
10623 if ((!NILP (Vtransient_mark_mode)
10624 && !NILP (current_buffer->mark_active))
10625 || !NILP (w->region_showing)
10626 || !NILP (Vshow_trailing_whitespace))
10627 return 0;
10628
10629 /* If top-line visibility has changed, give up. */
10630 if (WINDOW_WANTS_HEADER_LINE_P (w)
10631 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10632 return 0;
10633
10634 /* Give up if old or new display is scrolled vertically. We could
10635 make this function handle this, but right now it doesn't. */
10636 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10637 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10638 return 0;
10639
10640 /* The variable new_start now holds the new window start. The old
10641 start `start' can be determined from the current matrix. */
10642 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10643 start = start_row->start.pos;
10644 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10645
10646 /* Clear the desired matrix for the display below. */
10647 clear_glyph_matrix (w->desired_matrix);
10648
10649 if (CHARPOS (new_start) <= CHARPOS (start))
10650 {
10651 int first_row_y;
10652
10653 /* Don't use this method if the display starts with an ellipsis
10654 displayed for invisible text. It's not easy to handle that case
10655 below, and it's certainly not worth the effort since this is
10656 not a frequent case. */
10657 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10658 return 0;
10659
10660 IF_DEBUG (debug_method_add (w, "twu1"));
10661
10662 /* Display up to a row that can be reused. The variable
10663 last_text_row is set to the last row displayed that displays
10664 text. Note that it.vpos == 0 if or if not there is a
10665 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10666 start_display (&it, w, new_start);
10667 first_row_y = it.current_y;
10668 w->cursor.vpos = -1;
10669 last_text_row = last_reused_text_row = NULL;
10670
10671 while (it.current_y < it.last_visible_y
10672 && IT_CHARPOS (it) < CHARPOS (start)
10673 && !fonts_changed_p)
10674 if (display_line (&it))
10675 last_text_row = it.glyph_row - 1;
10676
10677 /* A value of current_y < last_visible_y means that we stopped
10678 at the previous window start, which in turn means that we
10679 have at least one reusable row. */
10680 if (it.current_y < it.last_visible_y)
10681 {
10682 /* IT.vpos always starts from 0; it counts text lines. */
10683 nrows_scrolled = it.vpos;
10684
10685 /* Find PT if not already found in the lines displayed. */
10686 if (w->cursor.vpos < 0)
10687 {
10688 int dy = it.current_y - first_row_y;
10689
10690 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10691 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10692 {
10693 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10694 && PT < MATRIX_ROW_END_CHARPOS (row))
10695 {
10696 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10697 dy, nrows_scrolled);
10698 break;
10699 }
10700
10701 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10702 break;
10703
10704 ++row;
10705 }
10706
10707 /* Give up if point was not found. This shouldn't
10708 happen often; not more often than with try_window
10709 itself. */
10710 if (w->cursor.vpos < 0)
10711 {
10712 clear_glyph_matrix (w->desired_matrix);
10713 return 0;
10714 }
10715 }
10716
10717 /* Scroll the display. Do it before the current matrix is
10718 changed. The problem here is that update has not yet
10719 run, i.e. part of the current matrix is not up to date.
10720 scroll_run_hook will clear the cursor, and use the
10721 current matrix to get the height of the row the cursor is
10722 in. */
10723 run.current_y = first_row_y;
10724 run.desired_y = it.current_y;
10725 run.height = it.last_visible_y - it.current_y;
10726
10727 if (run.height > 0 && run.current_y != run.desired_y)
10728 {
10729 update_begin (f);
10730 rif->update_window_begin_hook (w);
10731 rif->clear_mouse_face (w);
10732 rif->scroll_run_hook (w, &run);
10733 rif->update_window_end_hook (w, 0, 0);
10734 update_end (f);
10735 }
10736
10737 /* Shift current matrix down by nrows_scrolled lines. */
10738 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10739 rotate_matrix (w->current_matrix,
10740 start_vpos,
10741 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10742 nrows_scrolled);
10743
10744 /* Disable lines that must be updated. */
10745 for (i = 0; i < it.vpos; ++i)
10746 (start_row + i)->enabled_p = 0;
10747
10748 /* Re-compute Y positions. */
10749 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10750 max_y = it.last_visible_y;
10751 for (row = start_row + nrows_scrolled;
10752 row < bottom_row;
10753 ++row)
10754 {
10755 row->y = it.current_y;
10756 row->visible_height = row->height;
10757
10758 if (row->y < min_y)
10759 row->visible_height -= min_y - row->y;
10760 if (row->y + row->height > max_y)
10761 row->visible_height -= row->y + row->height - max_y;
10762
10763 it.current_y += row->height;
10764
10765 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10766 last_reused_text_row = row;
10767 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10768 break;
10769 }
10770
10771 /* Disable lines in the current matrix which are now
10772 below the window. */
10773 for (++row; row < bottom_row; ++row)
10774 row->enabled_p = 0;
10775 }
10776
10777 /* Update window_end_pos etc.; last_reused_text_row is the last
10778 reused row from the current matrix containing text, if any.
10779 The value of last_text_row is the last displayed line
10780 containing text. */
10781 if (last_reused_text_row)
10782 {
10783 w->window_end_bytepos
10784 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10785 w->window_end_pos
10786 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10787 w->window_end_vpos
10788 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10789 w->current_matrix));
10790 }
10791 else if (last_text_row)
10792 {
10793 w->window_end_bytepos
10794 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10795 w->window_end_pos
10796 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10797 w->window_end_vpos
10798 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10799 }
10800 else
10801 {
10802 /* This window must be completely empty. */
10803 w->window_end_bytepos = 0;
10804 w->window_end_pos = w->window_end_vpos = make_number (0);
10805 }
10806 w->window_end_valid = Qnil;
10807
10808 /* Update hint: don't try scrolling again in update_window. */
10809 w->desired_matrix->no_scrolling_p = 1;
10810
10811 #if GLYPH_DEBUG
10812 debug_method_add (w, "try_window_reusing_current_matrix 1");
10813 #endif
10814 return 1;
10815 }
10816 else if (CHARPOS (new_start) > CHARPOS (start))
10817 {
10818 struct glyph_row *pt_row, *row;
10819 struct glyph_row *first_reusable_row;
10820 struct glyph_row *first_row_to_display;
10821 int dy;
10822 int yb = window_text_bottom_y (w);
10823
10824 /* Find the row starting at new_start, if there is one. Don't
10825 reuse a partially visible line at the end. */
10826 first_reusable_row = start_row;
10827 while (first_reusable_row->enabled_p
10828 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10829 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10830 < CHARPOS (new_start)))
10831 ++first_reusable_row;
10832
10833 /* Give up if there is no row to reuse. */
10834 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10835 || !first_reusable_row->enabled_p
10836 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10837 != CHARPOS (new_start)))
10838 return 0;
10839
10840 /* We can reuse fully visible rows beginning with
10841 first_reusable_row to the end of the window. Set
10842 first_row_to_display to the first row that cannot be reused.
10843 Set pt_row to the row containing point, if there is any. */
10844 pt_row = NULL;
10845 for (first_row_to_display = first_reusable_row;
10846 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10847 ++first_row_to_display)
10848 {
10849 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10850 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10851 pt_row = first_row_to_display;
10852 }
10853
10854 /* Start displaying at the start of first_row_to_display. */
10855 xassert (first_row_to_display->y < yb);
10856 init_to_row_start (&it, w, first_row_to_display);
10857
10858 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10859 - start_vpos);
10860 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10861 - nrows_scrolled);
10862 it.current_y = (first_row_to_display->y - first_reusable_row->y
10863 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10864
10865 /* Display lines beginning with first_row_to_display in the
10866 desired matrix. Set last_text_row to the last row displayed
10867 that displays text. */
10868 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10869 if (pt_row == NULL)
10870 w->cursor.vpos = -1;
10871 last_text_row = NULL;
10872 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10873 if (display_line (&it))
10874 last_text_row = it.glyph_row - 1;
10875
10876 /* Give up If point isn't in a row displayed or reused. */
10877 if (w->cursor.vpos < 0)
10878 {
10879 clear_glyph_matrix (w->desired_matrix);
10880 return 0;
10881 }
10882
10883 /* If point is in a reused row, adjust y and vpos of the cursor
10884 position. */
10885 if (pt_row)
10886 {
10887 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10888 w->current_matrix);
10889 w->cursor.y -= first_reusable_row->y;
10890 }
10891
10892 /* Scroll the display. */
10893 run.current_y = first_reusable_row->y;
10894 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10895 run.height = it.last_visible_y - run.current_y;
10896 dy = run.current_y - run.desired_y;
10897
10898 if (run.height)
10899 {
10900 struct frame *f = XFRAME (WINDOW_FRAME (w));
10901 update_begin (f);
10902 rif->update_window_begin_hook (w);
10903 rif->clear_mouse_face (w);
10904 rif->scroll_run_hook (w, &run);
10905 rif->update_window_end_hook (w, 0, 0);
10906 update_end (f);
10907 }
10908
10909 /* Adjust Y positions of reused rows. */
10910 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10911 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10912 max_y = it.last_visible_y;
10913 for (row = first_reusable_row; row < first_row_to_display; ++row)
10914 {
10915 row->y -= dy;
10916 row->visible_height = row->height;
10917 if (row->y < min_y)
10918 row->visible_height -= min_y - row->y;
10919 if (row->y + row->height > max_y)
10920 row->visible_height -= row->y + row->height - max_y;
10921 }
10922
10923 /* Scroll the current matrix. */
10924 xassert (nrows_scrolled > 0);
10925 rotate_matrix (w->current_matrix,
10926 start_vpos,
10927 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10928 -nrows_scrolled);
10929
10930 /* Disable rows not reused. */
10931 for (row -= nrows_scrolled; row < bottom_row; ++row)
10932 row->enabled_p = 0;
10933
10934 /* Adjust window end. A null value of last_text_row means that
10935 the window end is in reused rows which in turn means that
10936 only its vpos can have changed. */
10937 if (last_text_row)
10938 {
10939 w->window_end_bytepos
10940 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10941 w->window_end_pos
10942 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10943 w->window_end_vpos
10944 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10945 }
10946 else
10947 {
10948 w->window_end_vpos
10949 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
10950 }
10951
10952 w->window_end_valid = Qnil;
10953 w->desired_matrix->no_scrolling_p = 1;
10954
10955 #if GLYPH_DEBUG
10956 debug_method_add (w, "try_window_reusing_current_matrix 2");
10957 #endif
10958 return 1;
10959 }
10960
10961 return 0;
10962 }
10963
10964
10965 \f
10966 /************************************************************************
10967 Window redisplay reusing current matrix when buffer has changed
10968 ************************************************************************/
10969
10970 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10971 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10972 int *, int *));
10973 static struct glyph_row *
10974 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10975 struct glyph_row *));
10976
10977
10978 /* Return the last row in MATRIX displaying text. If row START is
10979 non-null, start searching with that row. IT gives the dimensions
10980 of the display. Value is null if matrix is empty; otherwise it is
10981 a pointer to the row found. */
10982
10983 static struct glyph_row *
10984 find_last_row_displaying_text (matrix, it, start)
10985 struct glyph_matrix *matrix;
10986 struct it *it;
10987 struct glyph_row *start;
10988 {
10989 struct glyph_row *row, *row_found;
10990
10991 /* Set row_found to the last row in IT->w's current matrix
10992 displaying text. The loop looks funny but think of partially
10993 visible lines. */
10994 row_found = NULL;
10995 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10996 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10997 {
10998 xassert (row->enabled_p);
10999 row_found = row;
11000 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11001 break;
11002 ++row;
11003 }
11004
11005 return row_found;
11006 }
11007
11008
11009 /* Return the last row in the current matrix of W that is not affected
11010 by changes at the start of current_buffer that occurred since W's
11011 current matrix was built. Value is null if no such row exists.
11012
11013 BEG_UNCHANGED us the number of characters unchanged at the start of
11014 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11015 first changed character in current_buffer. Characters at positions <
11016 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11017 when the current matrix was built. */
11018
11019 static struct glyph_row *
11020 find_last_unchanged_at_beg_row (w)
11021 struct window *w;
11022 {
11023 int first_changed_pos = BEG + BEG_UNCHANGED;
11024 struct glyph_row *row;
11025 struct glyph_row *row_found = NULL;
11026 int yb = window_text_bottom_y (w);
11027
11028 /* Find the last row displaying unchanged text. */
11029 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11030 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11031 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11032 {
11033 if (/* If row ends before first_changed_pos, it is unchanged,
11034 except in some case. */
11035 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11036 /* When row ends in ZV and we write at ZV it is not
11037 unchanged. */
11038 && !row->ends_at_zv_p
11039 /* When first_changed_pos is the end of a continued line,
11040 row is not unchanged because it may be no longer
11041 continued. */
11042 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11043 && row->continued_p))
11044 row_found = row;
11045
11046 /* Stop if last visible row. */
11047 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11048 break;
11049
11050 ++row;
11051 }
11052
11053 return row_found;
11054 }
11055
11056
11057 /* Find the first glyph row in the current matrix of W that is not
11058 affected by changes at the end of current_buffer since the
11059 time W's current matrix was built.
11060
11061 Return in *DELTA the number of chars by which buffer positions in
11062 unchanged text at the end of current_buffer must be adjusted.
11063
11064 Return in *DELTA_BYTES the corresponding number of bytes.
11065
11066 Value is null if no such row exists, i.e. all rows are affected by
11067 changes. */
11068
11069 static struct glyph_row *
11070 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11071 struct window *w;
11072 int *delta, *delta_bytes;
11073 {
11074 struct glyph_row *row;
11075 struct glyph_row *row_found = NULL;
11076
11077 *delta = *delta_bytes = 0;
11078
11079 /* Display must not have been paused, otherwise the current matrix
11080 is not up to date. */
11081 if (NILP (w->window_end_valid))
11082 abort ();
11083
11084 /* A value of window_end_pos >= END_UNCHANGED means that the window
11085 end is in the range of changed text. If so, there is no
11086 unchanged row at the end of W's current matrix. */
11087 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11088 return NULL;
11089
11090 /* Set row to the last row in W's current matrix displaying text. */
11091 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11092
11093 /* If matrix is entirely empty, no unchanged row exists. */
11094 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11095 {
11096 /* The value of row is the last glyph row in the matrix having a
11097 meaningful buffer position in it. The end position of row
11098 corresponds to window_end_pos. This allows us to translate
11099 buffer positions in the current matrix to current buffer
11100 positions for characters not in changed text. */
11101 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11102 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11103 int last_unchanged_pos, last_unchanged_pos_old;
11104 struct glyph_row *first_text_row
11105 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11106
11107 *delta = Z - Z_old;
11108 *delta_bytes = Z_BYTE - Z_BYTE_old;
11109
11110 /* Set last_unchanged_pos to the buffer position of the last
11111 character in the buffer that has not been changed. Z is the
11112 index + 1 of the last character in current_buffer, i.e. by
11113 subtracting END_UNCHANGED we get the index of the last
11114 unchanged character, and we have to add BEG to get its buffer
11115 position. */
11116 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11117 last_unchanged_pos_old = last_unchanged_pos - *delta;
11118
11119 /* Search backward from ROW for a row displaying a line that
11120 starts at a minimum position >= last_unchanged_pos_old. */
11121 for (; row > first_text_row; --row)
11122 {
11123 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11124 abort ();
11125
11126 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11127 row_found = row;
11128 }
11129 }
11130
11131 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11132 abort ();
11133
11134 return row_found;
11135 }
11136
11137
11138 /* Make sure that glyph rows in the current matrix of window W
11139 reference the same glyph memory as corresponding rows in the
11140 frame's frame matrix. This function is called after scrolling W's
11141 current matrix on a terminal frame in try_window_id and
11142 try_window_reusing_current_matrix. */
11143
11144 static void
11145 sync_frame_with_window_matrix_rows (w)
11146 struct window *w;
11147 {
11148 struct frame *f = XFRAME (w->frame);
11149 struct glyph_row *window_row, *window_row_end, *frame_row;
11150
11151 /* Preconditions: W must be a leaf window and full-width. Its frame
11152 must have a frame matrix. */
11153 xassert (NILP (w->hchild) && NILP (w->vchild));
11154 xassert (WINDOW_FULL_WIDTH_P (w));
11155 xassert (!FRAME_WINDOW_P (f));
11156
11157 /* If W is a full-width window, glyph pointers in W's current matrix
11158 have, by definition, to be the same as glyph pointers in the
11159 corresponding frame matrix. */
11160 window_row = w->current_matrix->rows;
11161 window_row_end = window_row + w->current_matrix->nrows;
11162 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11163 while (window_row < window_row_end)
11164 {
11165 int area;
11166
11167 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
11168 frame_row->glyphs[area] = window_row->glyphs[area];
11169
11170 /* Disable frame rows whose corresponding window rows have
11171 been disabled in try_window_id. */
11172 if (!window_row->enabled_p)
11173 frame_row->enabled_p = 0;
11174
11175 ++window_row, ++frame_row;
11176 }
11177 }
11178
11179
11180 /* Find the glyph row in window W containing CHARPOS. Consider all
11181 rows between START and END (not inclusive). END null means search
11182 all rows to the end of the display area of W. Value is the row
11183 containing CHARPOS or null. */
11184
11185 struct glyph_row *
11186 row_containing_pos (w, charpos, start, end)
11187 struct window *w;
11188 int charpos;
11189 struct glyph_row *start, *end;
11190 {
11191 struct glyph_row *row = start;
11192 int last_y;
11193
11194 /* If we happen to start on a header-line, skip that. */
11195 if (row->mode_line_p)
11196 ++row;
11197
11198 if ((end && row >= end) || !row->enabled_p)
11199 return NULL;
11200
11201 last_y = window_text_bottom_y (w);
11202
11203 while ((end == NULL || row < end)
11204 && (MATRIX_ROW_END_CHARPOS (row) < charpos
11205 /* The end position of a row equals the start
11206 position of the next row. If CHARPOS is there, we
11207 would rather display it in the next line, except
11208 when this line ends in ZV. */
11209 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11210 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11211 || !row->ends_at_zv_p)))
11212 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11213 ++row;
11214
11215 /* Give up if CHARPOS not found. */
11216 if ((end && row >= end)
11217 || charpos < MATRIX_ROW_START_CHARPOS (row)
11218 || charpos > MATRIX_ROW_END_CHARPOS (row))
11219 row = NULL;
11220
11221 return row;
11222 }
11223
11224
11225 /* Try to redisplay window W by reusing its existing display. W's
11226 current matrix must be up to date when this function is called,
11227 i.e. window_end_valid must not be nil.
11228
11229 Value is
11230
11231 1 if display has been updated
11232 0 if otherwise unsuccessful
11233 -1 if redisplay with same window start is known not to succeed
11234
11235 The following steps are performed:
11236
11237 1. Find the last row in the current matrix of W that is not
11238 affected by changes at the start of current_buffer. If no such row
11239 is found, give up.
11240
11241 2. Find the first row in W's current matrix that is not affected by
11242 changes at the end of current_buffer. Maybe there is no such row.
11243
11244 3. Display lines beginning with the row + 1 found in step 1 to the
11245 row found in step 2 or, if step 2 didn't find a row, to the end of
11246 the window.
11247
11248 4. If cursor is not known to appear on the window, give up.
11249
11250 5. If display stopped at the row found in step 2, scroll the
11251 display and current matrix as needed.
11252
11253 6. Maybe display some lines at the end of W, if we must. This can
11254 happen under various circumstances, like a partially visible line
11255 becoming fully visible, or because newly displayed lines are displayed
11256 in smaller font sizes.
11257
11258 7. Update W's window end information. */
11259
11260 static int
11261 try_window_id (w)
11262 struct window *w;
11263 {
11264 struct frame *f = XFRAME (w->frame);
11265 struct glyph_matrix *current_matrix = w->current_matrix;
11266 struct glyph_matrix *desired_matrix = w->desired_matrix;
11267 struct glyph_row *last_unchanged_at_beg_row;
11268 struct glyph_row *first_unchanged_at_end_row;
11269 struct glyph_row *row;
11270 struct glyph_row *bottom_row;
11271 int bottom_vpos;
11272 struct it it;
11273 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11274 struct text_pos start_pos;
11275 struct run run;
11276 int first_unchanged_at_end_vpos = 0;
11277 struct glyph_row *last_text_row, *last_text_row_at_end;
11278 struct text_pos start;
11279 int first_changed_charpos, last_changed_charpos;
11280
11281 /* This is handy for debugging. */
11282 #if 0
11283 #define GIVE_UP(X) \
11284 do { \
11285 fprintf (stderr, "try_window_id give up %d\n", (X)); \
11286 return 0; \
11287 } while (0)
11288 #else
11289 #define GIVE_UP(X) return 0
11290 #endif
11291
11292 SET_TEXT_POS_FROM_MARKER (start, w->start);
11293
11294 /* Don't use this for mini-windows because these can show
11295 messages and mini-buffers, and we don't handle that here. */
11296 if (MINI_WINDOW_P (w))
11297 GIVE_UP (1);
11298
11299 /* This flag is used to prevent redisplay optimizations. */
11300 if (windows_or_buffers_changed)
11301 GIVE_UP (2);
11302
11303 /* Verify that narrowing has not changed. This flag is also set to prevent
11304 redisplay optimizations. It would be nice to further
11305 reduce the number of cases where this prevents try_window_id. */
11306 if (current_buffer->clip_changed)
11307 GIVE_UP (3);
11308
11309 /* Window must either use window-based redisplay or be full width. */
11310 if (!FRAME_WINDOW_P (f)
11311 && (!line_ins_del_ok
11312 || !WINDOW_FULL_WIDTH_P (w)))
11313 GIVE_UP (4);
11314
11315 /* Give up if point is not known NOT to appear in W. */
11316 if (PT < CHARPOS (start))
11317 GIVE_UP (5);
11318
11319 /* Another way to prevent redisplay optimizations. */
11320 if (XFASTINT (w->last_modified) == 0)
11321 GIVE_UP (6);
11322
11323 /* Verify that window is not hscrolled. */
11324 if (XFASTINT (w->hscroll) != 0)
11325 GIVE_UP (7);
11326
11327 /* Verify that display wasn't paused. */
11328 if (NILP (w->window_end_valid))
11329 GIVE_UP (8);
11330
11331 /* Can't use this if highlighting a region because a cursor movement
11332 will do more than just set the cursor. */
11333 if (!NILP (Vtransient_mark_mode)
11334 && !NILP (current_buffer->mark_active))
11335 GIVE_UP (9);
11336
11337 /* Likewise if highlighting trailing whitespace. */
11338 if (!NILP (Vshow_trailing_whitespace))
11339 GIVE_UP (11);
11340
11341 /* Likewise if showing a region. */
11342 if (!NILP (w->region_showing))
11343 GIVE_UP (10);
11344
11345 /* Can use this if overlay arrow position and or string have changed. */
11346 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
11347 || !EQ (last_arrow_string, Voverlay_arrow_string))
11348 GIVE_UP (12);
11349
11350
11351 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11352 only if buffer has really changed. The reason is that the gap is
11353 initially at Z for freshly visited files. The code below would
11354 set end_unchanged to 0 in that case. */
11355 if (MODIFF > SAVE_MODIFF
11356 /* This seems to happen sometimes after saving a buffer. */
11357 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
11358 {
11359 if (GPT - BEG < BEG_UNCHANGED)
11360 BEG_UNCHANGED = GPT - BEG;
11361 if (Z - GPT < END_UNCHANGED)
11362 END_UNCHANGED = Z - GPT;
11363 }
11364
11365 /* The position of the first and last character that has been changed. */
11366 first_changed_charpos = BEG + BEG_UNCHANGED;
11367 last_changed_charpos = Z - END_UNCHANGED;
11368
11369 /* If window starts after a line end, and the last change is in
11370 front of that newline, then changes don't affect the display.
11371 This case happens with stealth-fontification. Note that although
11372 the display is unchanged, glyph positions in the matrix have to
11373 be adjusted, of course. */
11374 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11375 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11376 && ((last_changed_charpos < CHARPOS (start)
11377 && CHARPOS (start) == BEGV)
11378 || (last_changed_charpos < CHARPOS (start) - 1
11379 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
11380 {
11381 int Z_old, delta, Z_BYTE_old, delta_bytes;
11382 struct glyph_row *r0;
11383
11384 /* Compute how many chars/bytes have been added to or removed
11385 from the buffer. */
11386 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11387 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11388 delta = Z - Z_old;
11389 delta_bytes = Z_BYTE - Z_BYTE_old;
11390
11391 /* Give up if PT is not in the window. Note that it already has
11392 been checked at the start of try_window_id that PT is not in
11393 front of the window start. */
11394 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
11395 GIVE_UP (13);
11396
11397 /* If window start is unchanged, we can reuse the whole matrix
11398 as is, after adjusting glyph positions. No need to compute
11399 the window end again, since its offset from Z hasn't changed. */
11400 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11401 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
11402 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
11403 {
11404 /* Adjust positions in the glyph matrix. */
11405 if (delta || delta_bytes)
11406 {
11407 struct glyph_row *r1
11408 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11409 increment_matrix_positions (w->current_matrix,
11410 MATRIX_ROW_VPOS (r0, current_matrix),
11411 MATRIX_ROW_VPOS (r1, current_matrix),
11412 delta, delta_bytes);
11413 }
11414
11415 /* Set the cursor. */
11416 row = row_containing_pos (w, PT, r0, NULL);
11417 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11418 return 1;
11419 }
11420 }
11421
11422 /* Handle the case that changes are all below what is displayed in
11423 the window, and that PT is in the window. This shortcut cannot
11424 be taken if ZV is visible in the window, and text has been added
11425 there that is visible in the window. */
11426 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
11427 /* ZV is not visible in the window, or there are no
11428 changes at ZV, actually. */
11429 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
11430 || first_changed_charpos == last_changed_charpos))
11431 {
11432 struct glyph_row *r0;
11433
11434 /* Give up if PT is not in the window. Note that it already has
11435 been checked at the start of try_window_id that PT is not in
11436 front of the window start. */
11437 if (PT >= MATRIX_ROW_END_CHARPOS (row))
11438 GIVE_UP (14);
11439
11440 /* If window start is unchanged, we can reuse the whole matrix
11441 as is, without changing glyph positions since no text has
11442 been added/removed in front of the window end. */
11443 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11444 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
11445 {
11446 /* We have to compute the window end anew since text
11447 can have been added/removed after it. */
11448 w->window_end_pos
11449 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11450 w->window_end_bytepos
11451 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11452
11453 /* Set the cursor. */
11454 row = row_containing_pos (w, PT, r0, NULL);
11455 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
11456 return 2;
11457 }
11458 }
11459
11460 /* Give up if window start is in the changed area.
11461
11462 The condition used to read
11463
11464 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
11465
11466 but why that was tested escapes me at the moment. */
11467 if (CHARPOS (start) >= first_changed_charpos
11468 && CHARPOS (start) <= last_changed_charpos)
11469 GIVE_UP (15);
11470
11471 /* Check that window start agrees with the start of the first glyph
11472 row in its current matrix. Check this after we know the window
11473 start is not in changed text, otherwise positions would not be
11474 comparable. */
11475 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
11476 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11477 GIVE_UP (16);
11478
11479 /* Compute the position at which we have to start displaying new
11480 lines. Some of the lines at the top of the window might be
11481 reusable because they are not displaying changed text. Find the
11482 last row in W's current matrix not affected by changes at the
11483 start of current_buffer. Value is null if changes start in the
11484 first line of window. */
11485 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
11486 if (last_unchanged_at_beg_row)
11487 {
11488 /* Avoid starting to display in the moddle of a character, a TAB
11489 for instance. This is easier than to set up the iterator
11490 exactly, and it's not a frequent case, so the additional
11491 effort wouldn't really pay off. */
11492 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11493 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
11494 && last_unchanged_at_beg_row > w->current_matrix->rows)
11495 --last_unchanged_at_beg_row;
11496
11497 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11498 GIVE_UP (17);
11499
11500 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
11501 GIVE_UP (18);
11502 start_pos = it.current.pos;
11503
11504 /* Start displaying new lines in the desired matrix at the same
11505 vpos we would use in the current matrix, i.e. below
11506 last_unchanged_at_beg_row. */
11507 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11508 current_matrix);
11509 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11510 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11511
11512 xassert (it.hpos == 0 && it.current_x == 0);
11513 }
11514 else
11515 {
11516 /* There are no reusable lines at the start of the window.
11517 Start displaying in the first line. */
11518 start_display (&it, w, start);
11519 start_pos = it.current.pos;
11520 }
11521
11522 /* Find the first row that is not affected by changes at the end of
11523 the buffer. Value will be null if there is no unchanged row, in
11524 which case we must redisplay to the end of the window. delta
11525 will be set to the value by which buffer positions beginning with
11526 first_unchanged_at_end_row have to be adjusted due to text
11527 changes. */
11528 first_unchanged_at_end_row
11529 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
11530 IF_DEBUG (debug_delta = delta);
11531 IF_DEBUG (debug_delta_bytes = delta_bytes);
11532
11533 /* Set stop_pos to the buffer position up to which we will have to
11534 display new lines. If first_unchanged_at_end_row != NULL, this
11535 is the buffer position of the start of the line displayed in that
11536 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11537 that we don't stop at a buffer position. */
11538 stop_pos = 0;
11539 if (first_unchanged_at_end_row)
11540 {
11541 #if GLYPH_DEBUG
11542 xassert (last_unchanged_at_beg_row == NULL
11543 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11544 #else
11545 /* This is for the release of 21.1 only, and should be removed
11546 after the release.
11547
11548 This case means that unchanged information is probably bogus,
11549 which leads to being unable to compute a correct
11550 first_unchanged_at_end_row. At least that was the case in
11551 one debugging session. I've fixed a bug that can lead to
11552 wrong unchanged info, but didn't find a way to reproduce this
11553 case. 2001-09-18 gerd. */
11554 if (last_unchanged_at_beg_row
11555 && first_unchanged_at_end_row < last_unchanged_at_beg_row)
11556 GIVE_UP (20);
11557 #endif
11558
11559 /* If this is a continuation line, move forward to the next one
11560 that isn't. Changes in lines above affect this line.
11561 Caution: this may move first_unchanged_at_end_row to a row
11562 not displaying text. */
11563 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11564 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11565 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11566 < it.last_visible_y))
11567 ++first_unchanged_at_end_row;
11568
11569 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11570 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11571 >= it.last_visible_y))
11572 first_unchanged_at_end_row = NULL;
11573 else
11574 {
11575 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11576 + delta);
11577 first_unchanged_at_end_vpos
11578 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
11579 xassert (stop_pos >= Z - END_UNCHANGED);
11580 }
11581 }
11582 else if (last_unchanged_at_beg_row == NULL)
11583 GIVE_UP (18);
11584
11585
11586 #if GLYPH_DEBUG
11587
11588 /* Either there is no unchanged row at the end, or the one we have
11589 now displays text. This is a necessary condition for the window
11590 end pos calculation at the end of this function. */
11591 xassert (first_unchanged_at_end_row == NULL
11592 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11593
11594 debug_last_unchanged_at_beg_vpos
11595 = (last_unchanged_at_beg_row
11596 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11597 : -1);
11598 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11599
11600 #endif /* GLYPH_DEBUG != 0 */
11601
11602
11603 /* Display new lines. Set last_text_row to the last new line
11604 displayed which has text on it, i.e. might end up as being the
11605 line where the window_end_vpos is. */
11606 w->cursor.vpos = -1;
11607 last_text_row = NULL;
11608 overlay_arrow_seen = 0;
11609 while (it.current_y < it.last_visible_y
11610 && !fonts_changed_p
11611 && (first_unchanged_at_end_row == NULL
11612 || IT_CHARPOS (it) < stop_pos))
11613 {
11614 if (display_line (&it))
11615 last_text_row = it.glyph_row - 1;
11616 }
11617
11618 if (fonts_changed_p)
11619 return -1;
11620
11621
11622 /* Compute differences in buffer positions, y-positions etc. for
11623 lines reused at the bottom of the window. Compute what we can
11624 scroll. */
11625 if (first_unchanged_at_end_row
11626 /* No lines reused because we displayed everything up to the
11627 bottom of the window. */
11628 && it.current_y < it.last_visible_y)
11629 {
11630 dvpos = (it.vpos
11631 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11632 current_matrix));
11633 dy = it.current_y - first_unchanged_at_end_row->y;
11634 run.current_y = first_unchanged_at_end_row->y;
11635 run.desired_y = run.current_y + dy;
11636 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11637 }
11638 else
11639 {
11640 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11641 first_unchanged_at_end_row = NULL;
11642 }
11643 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11644
11645
11646 /* Find the cursor if not already found. We have to decide whether
11647 PT will appear on this window (it sometimes doesn't, but this is
11648 not a very frequent case.) This decision has to be made before
11649 the current matrix is altered. A value of cursor.vpos < 0 means
11650 that PT is either in one of the lines beginning at
11651 first_unchanged_at_end_row or below the window. Don't care for
11652 lines that might be displayed later at the window end; as
11653 mentioned, this is not a frequent case. */
11654 if (w->cursor.vpos < 0)
11655 {
11656 /* Cursor in unchanged rows at the top? */
11657 if (PT < CHARPOS (start_pos)
11658 && last_unchanged_at_beg_row)
11659 {
11660 row = row_containing_pos (w, PT,
11661 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11662 last_unchanged_at_beg_row + 1);
11663 if (row)
11664 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11665 }
11666
11667 /* Start from first_unchanged_at_end_row looking for PT. */
11668 else if (first_unchanged_at_end_row)
11669 {
11670 row = row_containing_pos (w, PT - delta,
11671 first_unchanged_at_end_row, NULL);
11672 if (row)
11673 set_cursor_from_row (w, row, w->current_matrix, delta,
11674 delta_bytes, dy, dvpos);
11675 }
11676
11677 /* Give up if cursor was not found. */
11678 if (w->cursor.vpos < 0)
11679 {
11680 clear_glyph_matrix (w->desired_matrix);
11681 return -1;
11682 }
11683 }
11684
11685 /* Don't let the cursor end in the scroll margins. */
11686 {
11687 int this_scroll_margin, cursor_height;
11688
11689 this_scroll_margin = max (0, scroll_margin);
11690 this_scroll_margin = min (this_scroll_margin,
11691 XFASTINT (w->height) / 4);
11692 this_scroll_margin *= CANON_Y_UNIT (it.f);
11693 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11694
11695 if ((w->cursor.y < this_scroll_margin
11696 && CHARPOS (start) > BEGV)
11697 /* Don't take scroll margin into account at the bottom because
11698 old redisplay didn't do it either. */
11699 || w->cursor.y + cursor_height > it.last_visible_y)
11700 {
11701 w->cursor.vpos = -1;
11702 clear_glyph_matrix (w->desired_matrix);
11703 return -1;
11704 }
11705 }
11706
11707 /* Scroll the display. Do it before changing the current matrix so
11708 that xterm.c doesn't get confused about where the cursor glyph is
11709 found. */
11710 if (dy && run.height)
11711 {
11712 update_begin (f);
11713
11714 if (FRAME_WINDOW_P (f))
11715 {
11716 rif->update_window_begin_hook (w);
11717 rif->clear_mouse_face (w);
11718 rif->scroll_run_hook (w, &run);
11719 rif->update_window_end_hook (w, 0, 0);
11720 }
11721 else
11722 {
11723 /* Terminal frame. In this case, dvpos gives the number of
11724 lines to scroll by; dvpos < 0 means scroll up. */
11725 int first_unchanged_at_end_vpos
11726 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11727 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11728 int end = (XFASTINT (w->top)
11729 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
11730 + window_internal_height (w));
11731
11732 /* Perform the operation on the screen. */
11733 if (dvpos > 0)
11734 {
11735 /* Scroll last_unchanged_at_beg_row to the end of the
11736 window down dvpos lines. */
11737 set_terminal_window (end);
11738
11739 /* On dumb terminals delete dvpos lines at the end
11740 before inserting dvpos empty lines. */
11741 if (!scroll_region_ok)
11742 ins_del_lines (end - dvpos, -dvpos);
11743
11744 /* Insert dvpos empty lines in front of
11745 last_unchanged_at_beg_row. */
11746 ins_del_lines (from, dvpos);
11747 }
11748 else if (dvpos < 0)
11749 {
11750 /* Scroll up last_unchanged_at_beg_vpos to the end of
11751 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11752 set_terminal_window (end);
11753
11754 /* Delete dvpos lines in front of
11755 last_unchanged_at_beg_vpos. ins_del_lines will set
11756 the cursor to the given vpos and emit |dvpos| delete
11757 line sequences. */
11758 ins_del_lines (from + dvpos, dvpos);
11759
11760 /* On a dumb terminal insert dvpos empty lines at the
11761 end. */
11762 if (!scroll_region_ok)
11763 ins_del_lines (end + dvpos, -dvpos);
11764 }
11765
11766 set_terminal_window (0);
11767 }
11768
11769 update_end (f);
11770 }
11771
11772 /* Shift reused rows of the current matrix to the right position.
11773 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11774 text. */
11775 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11776 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11777 if (dvpos < 0)
11778 {
11779 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11780 bottom_vpos, dvpos);
11781 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11782 bottom_vpos, 0);
11783 }
11784 else if (dvpos > 0)
11785 {
11786 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11787 bottom_vpos, dvpos);
11788 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11789 first_unchanged_at_end_vpos + dvpos, 0);
11790 }
11791
11792 /* For frame-based redisplay, make sure that current frame and window
11793 matrix are in sync with respect to glyph memory. */
11794 if (!FRAME_WINDOW_P (f))
11795 sync_frame_with_window_matrix_rows (w);
11796
11797 /* Adjust buffer positions in reused rows. */
11798 if (delta)
11799 increment_matrix_positions (current_matrix,
11800 first_unchanged_at_end_vpos + dvpos,
11801 bottom_vpos, delta, delta_bytes);
11802
11803 /* Adjust Y positions. */
11804 if (dy)
11805 shift_glyph_matrix (w, current_matrix,
11806 first_unchanged_at_end_vpos + dvpos,
11807 bottom_vpos, dy);
11808
11809 if (first_unchanged_at_end_row)
11810 first_unchanged_at_end_row += dvpos;
11811
11812 /* If scrolling up, there may be some lines to display at the end of
11813 the window. */
11814 last_text_row_at_end = NULL;
11815 if (dy < 0)
11816 {
11817 /* Set last_row to the glyph row in the current matrix where the
11818 window end line is found. It has been moved up or down in
11819 the matrix by dvpos. */
11820 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11821 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11822
11823 /* If last_row is the window end line, it should display text. */
11824 xassert (last_row->displays_text_p);
11825
11826 /* If window end line was partially visible before, begin
11827 displaying at that line. Otherwise begin displaying with the
11828 line following it. */
11829 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11830 {
11831 init_to_row_start (&it, w, last_row);
11832 it.vpos = last_vpos;
11833 it.current_y = last_row->y;
11834 }
11835 else
11836 {
11837 init_to_row_end (&it, w, last_row);
11838 it.vpos = 1 + last_vpos;
11839 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11840 ++last_row;
11841 }
11842
11843 /* We may start in a continuation line. If so, we have to get
11844 the right continuation_lines_width and current_x. */
11845 it.continuation_lines_width = last_row->continuation_lines_width;
11846 it.hpos = it.current_x = 0;
11847
11848 /* Display the rest of the lines at the window end. */
11849 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11850 while (it.current_y < it.last_visible_y
11851 && !fonts_changed_p)
11852 {
11853 /* Is it always sure that the display agrees with lines in
11854 the current matrix? I don't think so, so we mark rows
11855 displayed invalid in the current matrix by setting their
11856 enabled_p flag to zero. */
11857 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11858 if (display_line (&it))
11859 last_text_row_at_end = it.glyph_row - 1;
11860 }
11861 }
11862
11863 /* Update window_end_pos and window_end_vpos. */
11864 if (first_unchanged_at_end_row
11865 && first_unchanged_at_end_row->y < it.last_visible_y
11866 && !last_text_row_at_end)
11867 {
11868 /* Window end line if one of the preserved rows from the current
11869 matrix. Set row to the last row displaying text in current
11870 matrix starting at first_unchanged_at_end_row, after
11871 scrolling. */
11872 xassert (first_unchanged_at_end_row->displays_text_p);
11873 row = find_last_row_displaying_text (w->current_matrix, &it,
11874 first_unchanged_at_end_row);
11875 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11876
11877 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11878 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11879 w->window_end_vpos
11880 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
11881 xassert (w->window_end_bytepos >= 0);
11882 IF_DEBUG (debug_method_add (w, "A"));
11883 }
11884 else if (last_text_row_at_end)
11885 {
11886 w->window_end_pos
11887 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11888 w->window_end_bytepos
11889 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11890 w->window_end_vpos
11891 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11892 xassert (w->window_end_bytepos >= 0);
11893 IF_DEBUG (debug_method_add (w, "B"));
11894 }
11895 else if (last_text_row)
11896 {
11897 /* We have displayed either to the end of the window or at the
11898 end of the window, i.e. the last row with text is to be found
11899 in the desired matrix. */
11900 w->window_end_pos
11901 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11902 w->window_end_bytepos
11903 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11904 w->window_end_vpos
11905 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11906 xassert (w->window_end_bytepos >= 0);
11907 }
11908 else if (first_unchanged_at_end_row == NULL
11909 && last_text_row == NULL
11910 && last_text_row_at_end == NULL)
11911 {
11912 /* Displayed to end of window, but no line containing text was
11913 displayed. Lines were deleted at the end of the window. */
11914 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11915 int vpos = XFASTINT (w->window_end_vpos);
11916 struct glyph_row *current_row = current_matrix->rows + vpos;
11917 struct glyph_row *desired_row = desired_matrix->rows + vpos;
11918
11919 for (row = NULL;
11920 row == NULL && vpos >= first_vpos;
11921 --vpos, --current_row, --desired_row)
11922 {
11923 if (desired_row->enabled_p)
11924 {
11925 if (desired_row->displays_text_p)
11926 row = desired_row;
11927 }
11928 else if (current_row->displays_text_p)
11929 row = current_row;
11930 }
11931
11932 xassert (row != NULL);
11933 w->window_end_vpos = make_number (vpos + 1);
11934 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11935 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11936 xassert (w->window_end_bytepos >= 0);
11937 IF_DEBUG (debug_method_add (w, "C"));
11938 }
11939 else
11940 abort ();
11941
11942 #if 0 /* This leads to problems, for instance when the cursor is
11943 at ZV, and the cursor line displays no text. */
11944 /* Disable rows below what's displayed in the window. This makes
11945 debugging easier. */
11946 enable_glyph_matrix_rows (current_matrix,
11947 XFASTINT (w->window_end_vpos) + 1,
11948 bottom_vpos, 0);
11949 #endif
11950
11951 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11952 debug_end_vpos = XFASTINT (w->window_end_vpos));
11953
11954 /* Record that display has not been completed. */
11955 w->window_end_valid = Qnil;
11956 w->desired_matrix->no_scrolling_p = 1;
11957 return 3;
11958
11959 #undef GIVE_UP
11960 }
11961
11962
11963 \f
11964 /***********************************************************************
11965 More debugging support
11966 ***********************************************************************/
11967
11968 #if GLYPH_DEBUG
11969
11970 void dump_glyph_row P_ ((struct glyph_row *, int, int));
11971 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11972 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
11973
11974
11975 /* Dump the contents of glyph matrix MATRIX on stderr.
11976
11977 GLYPHS 0 means don't show glyph contents.
11978 GLYPHS 1 means show glyphs in short form
11979 GLYPHS > 1 means show glyphs in long form. */
11980
11981 void
11982 dump_glyph_matrix (matrix, glyphs)
11983 struct glyph_matrix *matrix;
11984 int glyphs;
11985 {
11986 int i;
11987 for (i = 0; i < matrix->nrows; ++i)
11988 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
11989 }
11990
11991
11992 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11993 the glyph row and area where the glyph comes from. */
11994
11995 void
11996 dump_glyph (row, glyph, area)
11997 struct glyph_row *row;
11998 struct glyph *glyph;
11999 int area;
12000 {
12001 if (glyph->type == CHAR_GLYPH)
12002 {
12003 fprintf (stderr,
12004 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12005 glyph - row->glyphs[TEXT_AREA],
12006 'C',
12007 glyph->charpos,
12008 (BUFFERP (glyph->object)
12009 ? 'B'
12010 : (STRINGP (glyph->object)
12011 ? 'S'
12012 : '-')),
12013 glyph->pixel_width,
12014 glyph->u.ch,
12015 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12016 ? glyph->u.ch
12017 : '.'),
12018 glyph->face_id,
12019 glyph->left_box_line_p,
12020 glyph->right_box_line_p);
12021 }
12022 else if (glyph->type == STRETCH_GLYPH)
12023 {
12024 fprintf (stderr,
12025 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12026 glyph - row->glyphs[TEXT_AREA],
12027 'S',
12028 glyph->charpos,
12029 (BUFFERP (glyph->object)
12030 ? 'B'
12031 : (STRINGP (glyph->object)
12032 ? 'S'
12033 : '-')),
12034 glyph->pixel_width,
12035 0,
12036 '.',
12037 glyph->face_id,
12038 glyph->left_box_line_p,
12039 glyph->right_box_line_p);
12040 }
12041 else if (glyph->type == IMAGE_GLYPH)
12042 {
12043 fprintf (stderr,
12044 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12045 glyph - row->glyphs[TEXT_AREA],
12046 'I',
12047 glyph->charpos,
12048 (BUFFERP (glyph->object)
12049 ? 'B'
12050 : (STRINGP (glyph->object)
12051 ? 'S'
12052 : '-')),
12053 glyph->pixel_width,
12054 glyph->u.img_id,
12055 '.',
12056 glyph->face_id,
12057 glyph->left_box_line_p,
12058 glyph->right_box_line_p);
12059 }
12060 }
12061
12062
12063 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12064 GLYPHS 0 means don't show glyph contents.
12065 GLYPHS 1 means show glyphs in short form
12066 GLYPHS > 1 means show glyphs in long form. */
12067
12068 void
12069 dump_glyph_row (row, vpos, glyphs)
12070 struct glyph_row *row;
12071 int vpos, glyphs;
12072 {
12073 if (glyphs != 1)
12074 {
12075 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12076 fprintf (stderr, "=======================================================================\n");
12077
12078 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
12079 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12080 vpos,
12081 MATRIX_ROW_START_CHARPOS (row),
12082 MATRIX_ROW_END_CHARPOS (row),
12083 row->used[TEXT_AREA],
12084 row->contains_overlapping_glyphs_p,
12085 row->enabled_p,
12086 row->inverse_p,
12087 row->truncated_on_left_p,
12088 row->truncated_on_right_p,
12089 row->overlay_arrow_p,
12090 row->continued_p,
12091 MATRIX_ROW_CONTINUATION_LINE_P (row),
12092 row->displays_text_p,
12093 row->ends_at_zv_p,
12094 row->fill_line_p,
12095 row->ends_in_middle_of_char_p,
12096 row->starts_in_middle_of_char_p,
12097 row->mouse_face_p,
12098 row->x,
12099 row->y,
12100 row->pixel_width,
12101 row->height,
12102 row->visible_height,
12103 row->ascent,
12104 row->phys_ascent);
12105 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12106 row->end.overlay_string_index,
12107 row->continuation_lines_width);
12108 fprintf (stderr, "%9d %5d\n",
12109 CHARPOS (row->start.string_pos),
12110 CHARPOS (row->end.string_pos));
12111 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12112 row->end.dpvec_index);
12113 }
12114
12115 if (glyphs > 1)
12116 {
12117 int area;
12118
12119 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12120 {
12121 struct glyph *glyph = row->glyphs[area];
12122 struct glyph *glyph_end = glyph + row->used[area];
12123
12124 /* Glyph for a line end in text. */
12125 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12126 ++glyph_end;
12127
12128 if (glyph < glyph_end)
12129 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12130
12131 for (; glyph < glyph_end; ++glyph)
12132 dump_glyph (row, glyph, area);
12133 }
12134 }
12135 else if (glyphs == 1)
12136 {
12137 int area;
12138
12139 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12140 {
12141 char *s = (char *) alloca (row->used[area] + 1);
12142 int i;
12143
12144 for (i = 0; i < row->used[area]; ++i)
12145 {
12146 struct glyph *glyph = row->glyphs[area] + i;
12147 if (glyph->type == CHAR_GLYPH
12148 && glyph->u.ch < 0x80
12149 && glyph->u.ch >= ' ')
12150 s[i] = glyph->u.ch;
12151 else
12152 s[i] = '.';
12153 }
12154
12155 s[i] = '\0';
12156 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12157 }
12158 }
12159 }
12160
12161
12162 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12163 Sdump_glyph_matrix, 0, 1, "p",
12164 doc: /* Dump the current matrix of the selected window to stderr.
12165 Shows contents of glyph row structures. With non-nil
12166 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12167 glyphs in short form, otherwise show glyphs in long form. */)
12168 (glyphs)
12169 Lisp_Object glyphs;
12170 {
12171 struct window *w = XWINDOW (selected_window);
12172 struct buffer *buffer = XBUFFER (w->buffer);
12173
12174 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12175 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12176 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12177 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12178 fprintf (stderr, "=============================================\n");
12179 dump_glyph_matrix (w->current_matrix,
12180 NILP (glyphs) ? 0 : XINT (glyphs));
12181 return Qnil;
12182 }
12183
12184
12185 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12186 doc: /* Dump glyph row ROW to stderr.
12187 GLYPH 0 means don't dump glyphs.
12188 GLYPH 1 means dump glyphs in short form.
12189 GLYPH > 1 or omitted means dump glyphs in long form. */)
12190 (row, glyphs)
12191 Lisp_Object row, glyphs;
12192 {
12193 struct glyph_matrix *matrix;
12194 int vpos;
12195
12196 CHECK_NUMBER (row, 0);
12197 matrix = XWINDOW (selected_window)->current_matrix;
12198 vpos = XINT (row);
12199 if (vpos >= 0 && vpos < matrix->nrows)
12200 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12201 vpos,
12202 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12203 return Qnil;
12204 }
12205
12206
12207 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12208 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12209 GLYPH 0 means don't dump glyphs.
12210 GLYPH 1 means dump glyphs in short form.
12211 GLYPH > 1 or omitted means dump glyphs in long form. */)
12212 (row, glyphs)
12213 Lisp_Object row, glyphs;
12214 {
12215 struct frame *sf = SELECTED_FRAME ();
12216 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12217 int vpos;
12218
12219 CHECK_NUMBER (row, 0);
12220 vpos = XINT (row);
12221 if (vpos >= 0 && vpos < m->nrows)
12222 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12223 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12224 return Qnil;
12225 }
12226
12227
12228 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12229 doc: /* Toggle tracing of redisplay.
12230 With ARG, turn tracing on if and only if ARG is positive. */)
12231 (arg)
12232 Lisp_Object arg;
12233 {
12234 if (NILP (arg))
12235 trace_redisplay_p = !trace_redisplay_p;
12236 else
12237 {
12238 arg = Fprefix_numeric_value (arg);
12239 trace_redisplay_p = XINT (arg) > 0;
12240 }
12241
12242 return Qnil;
12243 }
12244
12245
12246 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
12247 doc: /* Like `format', but print result to stderr. */)
12248 (nargs, args)
12249 int nargs;
12250 Lisp_Object *args;
12251 {
12252 Lisp_Object s = Fformat (nargs, args);
12253 fprintf (stderr, "%s", XSTRING (s)->data);
12254 return Qnil;
12255 }
12256
12257 #endif /* GLYPH_DEBUG */
12258
12259
12260 \f
12261 /***********************************************************************
12262 Building Desired Matrix Rows
12263 ***********************************************************************/
12264
12265 /* Return a temporary glyph row holding the glyphs of an overlay
12266 arrow. Only used for non-window-redisplay windows. */
12267
12268 static struct glyph_row *
12269 get_overlay_arrow_glyph_row (w)
12270 struct window *w;
12271 {
12272 struct frame *f = XFRAME (WINDOW_FRAME (w));
12273 struct buffer *buffer = XBUFFER (w->buffer);
12274 struct buffer *old = current_buffer;
12275 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
12276 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
12277 unsigned char *arrow_end = arrow_string + arrow_len;
12278 unsigned char *p;
12279 struct it it;
12280 int multibyte_p;
12281 int n_glyphs_before;
12282
12283 set_buffer_temp (buffer);
12284 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
12285 it.glyph_row->used[TEXT_AREA] = 0;
12286 SET_TEXT_POS (it.position, 0, 0);
12287
12288 multibyte_p = !NILP (buffer->enable_multibyte_characters);
12289 p = arrow_string;
12290 while (p < arrow_end)
12291 {
12292 Lisp_Object face, ilisp;
12293
12294 /* Get the next character. */
12295 if (multibyte_p)
12296 it.c = string_char_and_length (p, arrow_len, &it.len);
12297 else
12298 it.c = *p, it.len = 1;
12299 p += it.len;
12300
12301 /* Get its face. */
12302 ilisp = make_number (p - arrow_string);
12303 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
12304 it.face_id = compute_char_face (f, it.c, face);
12305
12306 /* Compute its width, get its glyphs. */
12307 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
12308 SET_TEXT_POS (it.position, -1, -1);
12309 PRODUCE_GLYPHS (&it);
12310
12311 /* If this character doesn't fit any more in the line, we have
12312 to remove some glyphs. */
12313 if (it.current_x > it.last_visible_x)
12314 {
12315 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
12316 break;
12317 }
12318 }
12319
12320 set_buffer_temp (old);
12321 return it.glyph_row;
12322 }
12323
12324
12325 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
12326 glyphs are only inserted for terminal frames since we can't really
12327 win with truncation glyphs when partially visible glyphs are
12328 involved. Which glyphs to insert is determined by
12329 produce_special_glyphs. */
12330
12331 static void
12332 insert_left_trunc_glyphs (it)
12333 struct it *it;
12334 {
12335 struct it truncate_it;
12336 struct glyph *from, *end, *to, *toend;
12337
12338 xassert (!FRAME_WINDOW_P (it->f));
12339
12340 /* Get the truncation glyphs. */
12341 truncate_it = *it;
12342 truncate_it.current_x = 0;
12343 truncate_it.face_id = DEFAULT_FACE_ID;
12344 truncate_it.glyph_row = &scratch_glyph_row;
12345 truncate_it.glyph_row->used[TEXT_AREA] = 0;
12346 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
12347 truncate_it.object = make_number (0);
12348 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
12349
12350 /* Overwrite glyphs from IT with truncation glyphs. */
12351 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12352 end = from + truncate_it.glyph_row->used[TEXT_AREA];
12353 to = it->glyph_row->glyphs[TEXT_AREA];
12354 toend = to + it->glyph_row->used[TEXT_AREA];
12355
12356 while (from < end)
12357 *to++ = *from++;
12358
12359 /* There may be padding glyphs left over. Overwrite them too. */
12360 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
12361 {
12362 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
12363 while (from < end)
12364 *to++ = *from++;
12365 }
12366
12367 if (to > toend)
12368 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
12369 }
12370
12371
12372 /* Compute the pixel height and width of IT->glyph_row.
12373
12374 Most of the time, ascent and height of a display line will be equal
12375 to the max_ascent and max_height values of the display iterator
12376 structure. This is not the case if
12377
12378 1. We hit ZV without displaying anything. In this case, max_ascent
12379 and max_height will be zero.
12380
12381 2. We have some glyphs that don't contribute to the line height.
12382 (The glyph row flag contributes_to_line_height_p is for future
12383 pixmap extensions).
12384
12385 The first case is easily covered by using default values because in
12386 these cases, the line height does not really matter, except that it
12387 must not be zero. */
12388
12389 static void
12390 compute_line_metrics (it)
12391 struct it *it;
12392 {
12393 struct glyph_row *row = it->glyph_row;
12394 int area, i;
12395
12396 if (FRAME_WINDOW_P (it->f))
12397 {
12398 int i, min_y, max_y;
12399
12400 /* The line may consist of one space only, that was added to
12401 place the cursor on it. If so, the row's height hasn't been
12402 computed yet. */
12403 if (row->height == 0)
12404 {
12405 if (it->max_ascent + it->max_descent == 0)
12406 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
12407 row->ascent = it->max_ascent;
12408 row->height = it->max_ascent + it->max_descent;
12409 row->phys_ascent = it->max_phys_ascent;
12410 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12411 }
12412
12413 /* Compute the width of this line. */
12414 row->pixel_width = row->x;
12415 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12416 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12417
12418 xassert (row->pixel_width >= 0);
12419 xassert (row->ascent >= 0 && row->height > 0);
12420
12421 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12422 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12423
12424 /* If first line's physical ascent is larger than its logical
12425 ascent, use the physical ascent, and make the row taller.
12426 This makes accented characters fully visible. */
12427 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
12428 && row->phys_ascent > row->ascent)
12429 {
12430 row->height += row->phys_ascent - row->ascent;
12431 row->ascent = row->phys_ascent;
12432 }
12433
12434 /* Compute how much of the line is visible. */
12435 row->visible_height = row->height;
12436
12437 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12438 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12439
12440 if (row->y < min_y)
12441 row->visible_height -= min_y - row->y;
12442 if (row->y + row->height > max_y)
12443 row->visible_height -= row->y + row->height - max_y;
12444 }
12445 else
12446 {
12447 row->pixel_width = row->used[TEXT_AREA];
12448 if (row->continued_p)
12449 row->pixel_width -= it->continuation_pixel_width;
12450 else if (row->truncated_on_right_p)
12451 row->pixel_width -= it->truncation_pixel_width;
12452 row->ascent = row->phys_ascent = 0;
12453 row->height = row->phys_height = row->visible_height = 1;
12454 }
12455
12456 /* Compute a hash code for this row. */
12457 row->hash = 0;
12458 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12459 for (i = 0; i < row->used[area]; ++i)
12460 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12461 + row->glyphs[area][i].u.val
12462 + row->glyphs[area][i].face_id
12463 + row->glyphs[area][i].padding_p
12464 + (row->glyphs[area][i].type << 2));
12465
12466 it->max_ascent = it->max_descent = 0;
12467 it->max_phys_ascent = it->max_phys_descent = 0;
12468 }
12469
12470
12471 /* Append one space to the glyph row of iterator IT if doing a
12472 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12473 space have the default face, otherwise let it have the same face as
12474 IT->face_id. Value is non-zero if a space was added.
12475
12476 This function is called to make sure that there is always one glyph
12477 at the end of a glyph row that the cursor can be set on under
12478 window-systems. (If there weren't such a glyph we would not know
12479 how wide and tall a box cursor should be displayed).
12480
12481 At the same time this space let's a nicely handle clearing to the
12482 end of the line if the row ends in italic text. */
12483
12484 static int
12485 append_space (it, default_face_p)
12486 struct it *it;
12487 int default_face_p;
12488 {
12489 if (FRAME_WINDOW_P (it->f))
12490 {
12491 int n = it->glyph_row->used[TEXT_AREA];
12492
12493 if (it->glyph_row->glyphs[TEXT_AREA] + n
12494 < it->glyph_row->glyphs[1 + TEXT_AREA])
12495 {
12496 /* Save some values that must not be changed.
12497 Must save IT->c and IT->len because otherwise
12498 ITERATOR_AT_END_P wouldn't work anymore after
12499 append_space has been called. */
12500 enum display_element_type saved_what = it->what;
12501 int saved_c = it->c, saved_len = it->len;
12502 int saved_x = it->current_x;
12503 int saved_face_id = it->face_id;
12504 struct text_pos saved_pos;
12505 Lisp_Object saved_object;
12506 struct face *face;
12507
12508 saved_object = it->object;
12509 saved_pos = it->position;
12510
12511 it->what = IT_CHARACTER;
12512 bzero (&it->position, sizeof it->position);
12513 it->object = make_number (0);
12514 it->c = ' ';
12515 it->len = 1;
12516
12517 if (default_face_p)
12518 it->face_id = DEFAULT_FACE_ID;
12519 else if (it->face_before_selective_p)
12520 it->face_id = it->saved_face_id;
12521 face = FACE_FROM_ID (it->f, it->face_id);
12522 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
12523
12524 PRODUCE_GLYPHS (it);
12525
12526 it->current_x = saved_x;
12527 it->object = saved_object;
12528 it->position = saved_pos;
12529 it->what = saved_what;
12530 it->face_id = saved_face_id;
12531 it->len = saved_len;
12532 it->c = saved_c;
12533 return 1;
12534 }
12535 }
12536
12537 return 0;
12538 }
12539
12540
12541 /* Extend the face of the last glyph in the text area of IT->glyph_row
12542 to the end of the display line. Called from display_line.
12543 If the glyph row is empty, add a space glyph to it so that we
12544 know the face to draw. Set the glyph row flag fill_line_p. */
12545
12546 static void
12547 extend_face_to_end_of_line (it)
12548 struct it *it;
12549 {
12550 struct face *face;
12551 struct frame *f = it->f;
12552
12553 /* If line is already filled, do nothing. */
12554 if (it->current_x >= it->last_visible_x)
12555 return;
12556
12557 /* Face extension extends the background and box of IT->face_id
12558 to the end of the line. If the background equals the background
12559 of the frame, we don't have to do anything. */
12560 if (it->face_before_selective_p)
12561 face = FACE_FROM_ID (it->f, it->saved_face_id);
12562 else
12563 face = FACE_FROM_ID (f, it->face_id);
12564
12565 if (FRAME_WINDOW_P (f)
12566 && face->box == FACE_NO_BOX
12567 && face->background == FRAME_BACKGROUND_PIXEL (f)
12568 && !face->stipple)
12569 return;
12570
12571 /* Set the glyph row flag indicating that the face of the last glyph
12572 in the text area has to be drawn to the end of the text area. */
12573 it->glyph_row->fill_line_p = 1;
12574
12575 /* If current character of IT is not ASCII, make sure we have the
12576 ASCII face. This will be automatically undone the next time
12577 get_next_display_element returns a multibyte character. Note
12578 that the character will always be single byte in unibyte text. */
12579 if (!SINGLE_BYTE_CHAR_P (it->c))
12580 {
12581 it->face_id = FACE_FOR_CHAR (f, face, 0);
12582 }
12583
12584 if (FRAME_WINDOW_P (f))
12585 {
12586 /* If the row is empty, add a space with the current face of IT,
12587 so that we know which face to draw. */
12588 if (it->glyph_row->used[TEXT_AREA] == 0)
12589 {
12590 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
12591 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
12592 it->glyph_row->used[TEXT_AREA] = 1;
12593 }
12594 }
12595 else
12596 {
12597 /* Save some values that must not be changed. */
12598 int saved_x = it->current_x;
12599 struct text_pos saved_pos;
12600 Lisp_Object saved_object;
12601 enum display_element_type saved_what = it->what;
12602 int saved_face_id = it->face_id;
12603
12604 saved_object = it->object;
12605 saved_pos = it->position;
12606
12607 it->what = IT_CHARACTER;
12608 bzero (&it->position, sizeof it->position);
12609 it->object = make_number (0);
12610 it->c = ' ';
12611 it->len = 1;
12612 it->face_id = face->id;
12613
12614 PRODUCE_GLYPHS (it);
12615
12616 while (it->current_x <= it->last_visible_x)
12617 PRODUCE_GLYPHS (it);
12618
12619 /* Don't count these blanks really. It would let us insert a left
12620 truncation glyph below and make us set the cursor on them, maybe. */
12621 it->current_x = saved_x;
12622 it->object = saved_object;
12623 it->position = saved_pos;
12624 it->what = saved_what;
12625 it->face_id = saved_face_id;
12626 }
12627 }
12628
12629
12630 /* Value is non-zero if text starting at CHARPOS in current_buffer is
12631 trailing whitespace. */
12632
12633 static int
12634 trailing_whitespace_p (charpos)
12635 int charpos;
12636 {
12637 int bytepos = CHAR_TO_BYTE (charpos);
12638 int c = 0;
12639
12640 while (bytepos < ZV_BYTE
12641 && (c = FETCH_CHAR (bytepos),
12642 c == ' ' || c == '\t'))
12643 ++bytepos;
12644
12645 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12646 {
12647 if (bytepos != PT_BYTE)
12648 return 1;
12649 }
12650 return 0;
12651 }
12652
12653
12654 /* Highlight trailing whitespace, if any, in ROW. */
12655
12656 void
12657 highlight_trailing_whitespace (f, row)
12658 struct frame *f;
12659 struct glyph_row *row;
12660 {
12661 int used = row->used[TEXT_AREA];
12662
12663 if (used)
12664 {
12665 struct glyph *start = row->glyphs[TEXT_AREA];
12666 struct glyph *glyph = start + used - 1;
12667
12668 /* Skip over glyphs inserted to display the cursor at the
12669 end of a line, for extending the face of the last glyph
12670 to the end of the line on terminals, and for truncation
12671 and continuation glyphs. */
12672 while (glyph >= start
12673 && glyph->type == CHAR_GLYPH
12674 && INTEGERP (glyph->object))
12675 --glyph;
12676
12677 /* If last glyph is a space or stretch, and it's trailing
12678 whitespace, set the face of all trailing whitespace glyphs in
12679 IT->glyph_row to `trailing-whitespace'. */
12680 if (glyph >= start
12681 && BUFFERP (glyph->object)
12682 && (glyph->type == STRETCH_GLYPH
12683 || (glyph->type == CHAR_GLYPH
12684 && glyph->u.ch == ' '))
12685 && trailing_whitespace_p (glyph->charpos))
12686 {
12687 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
12688
12689 while (glyph >= start
12690 && BUFFERP (glyph->object)
12691 && (glyph->type == STRETCH_GLYPH
12692 || (glyph->type == CHAR_GLYPH
12693 && glyph->u.ch == ' ')))
12694 (glyph--)->face_id = face_id;
12695 }
12696 }
12697 }
12698
12699
12700 /* Value is non-zero if glyph row ROW in window W should be
12701 used to hold the cursor. */
12702
12703 static int
12704 cursor_row_p (w, row)
12705 struct window *w;
12706 struct glyph_row *row;
12707 {
12708 int cursor_row_p = 1;
12709
12710 if (PT == MATRIX_ROW_END_CHARPOS (row))
12711 {
12712 /* If the row ends with a newline from a string, we don't want
12713 the cursor there (if the row is continued it doesn't end in a
12714 newline). */
12715 if (CHARPOS (row->end.string_pos) >= 0
12716 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12717 cursor_row_p = row->continued_p;
12718
12719 /* If the row ends at ZV, display the cursor at the end of that
12720 row instead of at the start of the row below. */
12721 else if (row->ends_at_zv_p)
12722 cursor_row_p = 1;
12723 else
12724 cursor_row_p = 0;
12725 }
12726
12727 return cursor_row_p;
12728 }
12729
12730
12731 /* Construct the glyph row IT->glyph_row in the desired matrix of
12732 IT->w from text at the current position of IT. See dispextern.h
12733 for an overview of struct it. Value is non-zero if
12734 IT->glyph_row displays text, as opposed to a line displaying ZV
12735 only. */
12736
12737 static int
12738 display_line (it)
12739 struct it *it;
12740 {
12741 struct glyph_row *row = it->glyph_row;
12742
12743 /* We always start displaying at hpos zero even if hscrolled. */
12744 xassert (it->hpos == 0 && it->current_x == 0);
12745
12746 /* We must not display in a row that's not a text row. */
12747 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12748 < it->w->desired_matrix->nrows);
12749
12750 /* Is IT->w showing the region? */
12751 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12752
12753 /* Clear the result glyph row and enable it. */
12754 prepare_desired_row (row);
12755
12756 row->y = it->current_y;
12757 row->start = it->current;
12758 row->continuation_lines_width = it->continuation_lines_width;
12759 row->displays_text_p = 1;
12760 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12761 it->starts_in_middle_of_char_p = 0;
12762
12763 /* Arrange the overlays nicely for our purposes. Usually, we call
12764 display_line on only one line at a time, in which case this
12765 can't really hurt too much, or we call it on lines which appear
12766 one after another in the buffer, in which case all calls to
12767 recenter_overlay_lists but the first will be pretty cheap. */
12768 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12769
12770 /* Move over display elements that are not visible because we are
12771 hscrolled. This may stop at an x-position < IT->first_visible_x
12772 if the first glyph is partially visible or if we hit a line end. */
12773 if (it->current_x < it->first_visible_x)
12774 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12775 MOVE_TO_POS | MOVE_TO_X);
12776
12777 /* Get the initial row height. This is either the height of the
12778 text hscrolled, if there is any, or zero. */
12779 row->ascent = it->max_ascent;
12780 row->height = it->max_ascent + it->max_descent;
12781 row->phys_ascent = it->max_phys_ascent;
12782 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12783
12784 /* Loop generating characters. The loop is left with IT on the next
12785 character to display. */
12786 while (1)
12787 {
12788 int n_glyphs_before, hpos_before, x_before;
12789 int x, i, nglyphs;
12790 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12791
12792 /* Retrieve the next thing to display. Value is zero if end of
12793 buffer reached. */
12794 if (!get_next_display_element (it))
12795 {
12796 /* Maybe add a space at the end of this line that is used to
12797 display the cursor there under X. Set the charpos of the
12798 first glyph of blank lines not corresponding to any text
12799 to -1. */
12800 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12801 || row->used[TEXT_AREA] == 0)
12802 {
12803 row->glyphs[TEXT_AREA]->charpos = -1;
12804 row->displays_text_p = 0;
12805
12806 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
12807 && !MINI_WINDOW_P (it->w))
12808 row->indicate_empty_line_p = 1;
12809 }
12810
12811 it->continuation_lines_width = 0;
12812 row->ends_at_zv_p = 1;
12813 break;
12814 }
12815
12816 /* Now, get the metrics of what we want to display. This also
12817 generates glyphs in `row' (which is IT->glyph_row). */
12818 n_glyphs_before = row->used[TEXT_AREA];
12819 x = it->current_x;
12820
12821 /* Remember the line height so far in case the next element doesn't
12822 fit on the line. */
12823 if (!it->truncate_lines_p)
12824 {
12825 ascent = it->max_ascent;
12826 descent = it->max_descent;
12827 phys_ascent = it->max_phys_ascent;
12828 phys_descent = it->max_phys_descent;
12829 }
12830
12831 PRODUCE_GLYPHS (it);
12832
12833 /* If this display element was in marginal areas, continue with
12834 the next one. */
12835 if (it->area != TEXT_AREA)
12836 {
12837 row->ascent = max (row->ascent, it->max_ascent);
12838 row->height = max (row->height, it->max_ascent + it->max_descent);
12839 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12840 row->phys_height = max (row->phys_height,
12841 it->max_phys_ascent + it->max_phys_descent);
12842 set_iterator_to_next (it, 1);
12843 continue;
12844 }
12845
12846 /* Does the display element fit on the line? If we truncate
12847 lines, we should draw past the right edge of the window. If
12848 we don't truncate, we want to stop so that we can display the
12849 continuation glyph before the right margin. If lines are
12850 continued, there are two possible strategies for characters
12851 resulting in more than 1 glyph (e.g. tabs): Display as many
12852 glyphs as possible in this line and leave the rest for the
12853 continuation line, or display the whole element in the next
12854 line. Original redisplay did the former, so we do it also. */
12855 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12856 hpos_before = it->hpos;
12857 x_before = x;
12858
12859 if (/* Not a newline. */
12860 nglyphs > 0
12861 /* Glyphs produced fit entirely in the line. */
12862 && it->current_x < it->last_visible_x)
12863 {
12864 it->hpos += nglyphs;
12865 row->ascent = max (row->ascent, it->max_ascent);
12866 row->height = max (row->height, it->max_ascent + it->max_descent);
12867 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12868 row->phys_height = max (row->phys_height,
12869 it->max_phys_ascent + it->max_phys_descent);
12870 if (it->current_x - it->pixel_width < it->first_visible_x)
12871 row->x = x - it->first_visible_x;
12872 }
12873 else
12874 {
12875 int new_x;
12876 struct glyph *glyph;
12877
12878 for (i = 0; i < nglyphs; ++i, x = new_x)
12879 {
12880 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12881 new_x = x + glyph->pixel_width;
12882
12883 if (/* Lines are continued. */
12884 !it->truncate_lines_p
12885 && (/* Glyph doesn't fit on the line. */
12886 new_x > it->last_visible_x
12887 /* Or it fits exactly on a window system frame. */
12888 || (new_x == it->last_visible_x
12889 && FRAME_WINDOW_P (it->f))))
12890 {
12891 /* End of a continued line. */
12892
12893 if (it->hpos == 0
12894 || (new_x == it->last_visible_x
12895 && FRAME_WINDOW_P (it->f)))
12896 {
12897 /* Current glyph is the only one on the line or
12898 fits exactly on the line. We must continue
12899 the line because we can't draw the cursor
12900 after the glyph. */
12901 row->continued_p = 1;
12902 it->current_x = new_x;
12903 it->continuation_lines_width += new_x;
12904 ++it->hpos;
12905 if (i == nglyphs - 1)
12906 set_iterator_to_next (it, 1);
12907 }
12908 else if (CHAR_GLYPH_PADDING_P (*glyph)
12909 && !FRAME_WINDOW_P (it->f))
12910 {
12911 /* A padding glyph that doesn't fit on this line.
12912 This means the whole character doesn't fit
12913 on the line. */
12914 row->used[TEXT_AREA] = n_glyphs_before;
12915
12916 /* Fill the rest of the row with continuation
12917 glyphs like in 20.x. */
12918 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12919 < row->glyphs[1 + TEXT_AREA])
12920 produce_special_glyphs (it, IT_CONTINUATION);
12921
12922 row->continued_p = 1;
12923 it->current_x = x_before;
12924 it->continuation_lines_width += x_before;
12925
12926 /* Restore the height to what it was before the
12927 element not fitting on the line. */
12928 it->max_ascent = ascent;
12929 it->max_descent = descent;
12930 it->max_phys_ascent = phys_ascent;
12931 it->max_phys_descent = phys_descent;
12932 }
12933 else
12934 {
12935 /* Display element draws past the right edge of
12936 the window. Restore positions to values
12937 before the element. The next line starts
12938 with current_x before the glyph that could
12939 not be displayed, so that TAB works right. */
12940 row->used[TEXT_AREA] = n_glyphs_before + i;
12941
12942 /* Display continuation glyphs. */
12943 if (!FRAME_WINDOW_P (it->f))
12944 produce_special_glyphs (it, IT_CONTINUATION);
12945 row->continued_p = 1;
12946
12947 it->current_x = x;
12948 it->continuation_lines_width += x;
12949 if (nglyphs > 1 && i > 0)
12950 {
12951 row->ends_in_middle_of_char_p = 1;
12952 it->starts_in_middle_of_char_p = 1;
12953 }
12954
12955 /* Restore the height to what it was before the
12956 element not fitting on the line. */
12957 it->max_ascent = ascent;
12958 it->max_descent = descent;
12959 it->max_phys_ascent = phys_ascent;
12960 it->max_phys_descent = phys_descent;
12961 }
12962
12963 break;
12964 }
12965 else if (new_x > it->first_visible_x)
12966 {
12967 /* Increment number of glyphs actually displayed. */
12968 ++it->hpos;
12969
12970 if (x < it->first_visible_x)
12971 /* Glyph is partially visible, i.e. row starts at
12972 negative X position. */
12973 row->x = x - it->first_visible_x;
12974 }
12975 else
12976 {
12977 /* Glyph is completely off the left margin of the
12978 window. This should not happen because of the
12979 move_it_in_display_line at the start of
12980 this function. */
12981 abort ();
12982 }
12983 }
12984
12985 row->ascent = max (row->ascent, it->max_ascent);
12986 row->height = max (row->height, it->max_ascent + it->max_descent);
12987 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12988 row->phys_height = max (row->phys_height,
12989 it->max_phys_ascent + it->max_phys_descent);
12990
12991 /* End of this display line if row is continued. */
12992 if (row->continued_p)
12993 break;
12994 }
12995
12996 /* Is this a line end? If yes, we're also done, after making
12997 sure that a non-default face is extended up to the right
12998 margin of the window. */
12999 if (ITERATOR_AT_END_OF_LINE_P (it))
13000 {
13001 int used_before = row->used[TEXT_AREA];
13002
13003 row->ends_in_newline_from_string_p = STRINGP (it->object);
13004
13005 /* Add a space at the end of the line that is used to
13006 display the cursor there. */
13007 append_space (it, 0);
13008
13009 /* Extend the face to the end of the line. */
13010 extend_face_to_end_of_line (it);
13011
13012 /* Make sure we have the position. */
13013 if (used_before == 0)
13014 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13015
13016 /* Consume the line end. This skips over invisible lines. */
13017 set_iterator_to_next (it, 1);
13018 it->continuation_lines_width = 0;
13019 break;
13020 }
13021
13022 /* Proceed with next display element. Note that this skips
13023 over lines invisible because of selective display. */
13024 set_iterator_to_next (it, 1);
13025
13026 /* If we truncate lines, we are done when the last displayed
13027 glyphs reach past the right margin of the window. */
13028 if (it->truncate_lines_p
13029 && (FRAME_WINDOW_P (it->f)
13030 ? (it->current_x >= it->last_visible_x)
13031 : (it->current_x > it->last_visible_x)))
13032 {
13033 /* Maybe add truncation glyphs. */
13034 if (!FRAME_WINDOW_P (it->f))
13035 {
13036 int i, n;
13037
13038 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13039 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13040 break;
13041
13042 for (n = row->used[TEXT_AREA]; i < n; ++i)
13043 {
13044 row->used[TEXT_AREA] = i;
13045 produce_special_glyphs (it, IT_TRUNCATION);
13046 }
13047 }
13048
13049 row->truncated_on_right_p = 1;
13050 it->continuation_lines_width = 0;
13051 reseat_at_next_visible_line_start (it, 0);
13052 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13053 it->hpos = hpos_before;
13054 it->current_x = x_before;
13055 break;
13056 }
13057 }
13058
13059 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13060 at the left window margin. */
13061 if (it->first_visible_x
13062 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13063 {
13064 if (!FRAME_WINDOW_P (it->f))
13065 insert_left_trunc_glyphs (it);
13066 row->truncated_on_left_p = 1;
13067 }
13068
13069 /* If the start of this line is the overlay arrow-position, then
13070 mark this glyph row as the one containing the overlay arrow.
13071 This is clearly a mess with variable size fonts. It would be
13072 better to let it be displayed like cursors under X. */
13073 if (MARKERP (Voverlay_arrow_position)
13074 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13075 && (MATRIX_ROW_START_CHARPOS (row)
13076 == marker_position (Voverlay_arrow_position))
13077 && STRINGP (Voverlay_arrow_string)
13078 && ! overlay_arrow_seen)
13079 {
13080 /* Overlay arrow in window redisplay is a bitmap. */
13081 if (!FRAME_WINDOW_P (it->f))
13082 {
13083 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13084 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13085 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13086 struct glyph *p = row->glyphs[TEXT_AREA];
13087 struct glyph *p2, *end;
13088
13089 /* Copy the arrow glyphs. */
13090 while (glyph < arrow_end)
13091 *p++ = *glyph++;
13092
13093 /* Throw away padding glyphs. */
13094 p2 = p;
13095 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13096 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13097 ++p2;
13098 if (p2 > p)
13099 {
13100 while (p2 < end)
13101 *p++ = *p2++;
13102 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13103 }
13104 }
13105
13106 overlay_arrow_seen = 1;
13107 row->overlay_arrow_p = 1;
13108 }
13109
13110 /* Compute pixel dimensions of this line. */
13111 compute_line_metrics (it);
13112
13113 /* Remember the position at which this line ends. */
13114 row->end = it->current;
13115
13116 /* Maybe set the cursor. */
13117 if (it->w->cursor.vpos < 0
13118 && PT >= MATRIX_ROW_START_CHARPOS (row)
13119 && PT <= MATRIX_ROW_END_CHARPOS (row)
13120 && cursor_row_p (it->w, row))
13121 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13122
13123 /* Highlight trailing whitespace. */
13124 if (!NILP (Vshow_trailing_whitespace))
13125 highlight_trailing_whitespace (it->f, it->glyph_row);
13126
13127 /* Prepare for the next line. This line starts horizontally at (X
13128 HPOS) = (0 0). Vertical positions are incremented. As a
13129 convenience for the caller, IT->glyph_row is set to the next
13130 row to be used. */
13131 it->current_x = it->hpos = 0;
13132 it->current_y += row->height;
13133 ++it->vpos;
13134 ++it->glyph_row;
13135 return row->displays_text_p;
13136 }
13137
13138
13139 \f
13140 /***********************************************************************
13141 Menu Bar
13142 ***********************************************************************/
13143
13144 /* Redisplay the menu bar in the frame for window W.
13145
13146 The menu bar of X frames that don't have X toolkit support is
13147 displayed in a special window W->frame->menu_bar_window.
13148
13149 The menu bar of terminal frames is treated specially as far as
13150 glyph matrices are concerned. Menu bar lines are not part of
13151 windows, so the update is done directly on the frame matrix rows
13152 for the menu bar. */
13153
13154 static void
13155 display_menu_bar (w)
13156 struct window *w;
13157 {
13158 struct frame *f = XFRAME (WINDOW_FRAME (w));
13159 struct it it;
13160 Lisp_Object items;
13161 int i;
13162
13163 /* Don't do all this for graphical frames. */
13164 #ifdef HAVE_NTGUI
13165 if (!NILP (Vwindow_system))
13166 return;
13167 #endif
13168 #ifdef USE_X_TOOLKIT
13169 if (FRAME_X_P (f))
13170 return;
13171 #endif
13172 #ifdef macintosh
13173 if (FRAME_MAC_P (f))
13174 return;
13175 #endif
13176
13177 #ifdef USE_X_TOOLKIT
13178 xassert (!FRAME_WINDOW_P (f));
13179 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13180 it.first_visible_x = 0;
13181 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13182 #else /* not USE_X_TOOLKIT */
13183 if (FRAME_WINDOW_P (f))
13184 {
13185 /* Menu bar lines are displayed in the desired matrix of the
13186 dummy window menu_bar_window. */
13187 struct window *menu_w;
13188 xassert (WINDOWP (f->menu_bar_window));
13189 menu_w = XWINDOW (f->menu_bar_window);
13190 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13191 MENU_FACE_ID);
13192 it.first_visible_x = 0;
13193 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13194 }
13195 else
13196 {
13197 /* This is a TTY frame, i.e. character hpos/vpos are used as
13198 pixel x/y. */
13199 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13200 MENU_FACE_ID);
13201 it.first_visible_x = 0;
13202 it.last_visible_x = FRAME_WIDTH (f);
13203 }
13204 #endif /* not USE_X_TOOLKIT */
13205
13206 if (! mode_line_inverse_video)
13207 /* Force the menu-bar to be displayed in the default face. */
13208 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13209
13210 /* Clear all rows of the menu bar. */
13211 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13212 {
13213 struct glyph_row *row = it.glyph_row + i;
13214 clear_glyph_row (row);
13215 row->enabled_p = 1;
13216 row->full_width_p = 1;
13217 }
13218
13219 /* Display all items of the menu bar. */
13220 items = FRAME_MENU_BAR_ITEMS (it.f);
13221 for (i = 0; i < XVECTOR (items)->size; i += 4)
13222 {
13223 Lisp_Object string;
13224
13225 /* Stop at nil string. */
13226 string = AREF (items, i + 1);
13227 if (NILP (string))
13228 break;
13229
13230 /* Remember where item was displayed. */
13231 AREF (items, i + 3) = make_number (it.hpos);
13232
13233 /* Display the item, pad with one space. */
13234 if (it.current_x < it.last_visible_x)
13235 display_string (NULL, string, Qnil, 0, 0, &it,
13236 XSTRING (string)->size + 1, 0, 0, -1);
13237 }
13238
13239 /* Fill out the line with spaces. */
13240 if (it.current_x < it.last_visible_x)
13241 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
13242
13243 /* Compute the total height of the lines. */
13244 compute_line_metrics (&it);
13245 }
13246
13247
13248 \f
13249 /***********************************************************************
13250 Mode Line
13251 ***********************************************************************/
13252
13253 /* Redisplay mode lines in the window tree whose root is WINDOW. If
13254 FORCE is non-zero, redisplay mode lines unconditionally.
13255 Otherwise, redisplay only mode lines that are garbaged. Value is
13256 the number of windows whose mode lines were redisplayed. */
13257
13258 static int
13259 redisplay_mode_lines (window, force)
13260 Lisp_Object window;
13261 int force;
13262 {
13263 int nwindows = 0;
13264
13265 while (!NILP (window))
13266 {
13267 struct window *w = XWINDOW (window);
13268
13269 if (WINDOWP (w->hchild))
13270 nwindows += redisplay_mode_lines (w->hchild, force);
13271 else if (WINDOWP (w->vchild))
13272 nwindows += redisplay_mode_lines (w->vchild, force);
13273 else if (force
13274 || FRAME_GARBAGED_P (XFRAME (w->frame))
13275 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
13276 {
13277 struct text_pos lpoint;
13278 struct buffer *old = current_buffer;
13279
13280 /* Set the window's buffer for the mode line display. */
13281 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13282 set_buffer_internal_1 (XBUFFER (w->buffer));
13283
13284 /* Point refers normally to the selected window. For any
13285 other window, set up appropriate value. */
13286 if (!EQ (window, selected_window))
13287 {
13288 struct text_pos pt;
13289
13290 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
13291 if (CHARPOS (pt) < BEGV)
13292 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13293 else if (CHARPOS (pt) > (ZV - 1))
13294 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
13295 else
13296 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
13297 }
13298
13299 /* Display mode lines. */
13300 clear_glyph_matrix (w->desired_matrix);
13301 if (display_mode_lines (w))
13302 {
13303 ++nwindows;
13304 w->must_be_updated_p = 1;
13305 }
13306
13307 /* Restore old settings. */
13308 set_buffer_internal_1 (old);
13309 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13310 }
13311
13312 window = w->next;
13313 }
13314
13315 return nwindows;
13316 }
13317
13318
13319 /* Display the mode and/or top line of window W. Value is the number
13320 of mode lines displayed. */
13321
13322 static int
13323 display_mode_lines (w)
13324 struct window *w;
13325 {
13326 Lisp_Object old_selected_window, old_selected_frame;
13327 int n = 0;
13328
13329 old_selected_frame = selected_frame;
13330 selected_frame = w->frame;
13331 old_selected_window = selected_window;
13332 XSETWINDOW (selected_window, w);
13333
13334 /* These will be set while the mode line specs are processed. */
13335 line_number_displayed = 0;
13336 w->column_number_displayed = Qnil;
13337
13338 if (WINDOW_WANTS_MODELINE_P (w))
13339 {
13340 display_mode_line (w, MODE_LINE_FACE_ID,
13341 current_buffer->mode_line_format);
13342 ++n;
13343 }
13344
13345 if (WINDOW_WANTS_HEADER_LINE_P (w))
13346 {
13347 display_mode_line (w, HEADER_LINE_FACE_ID,
13348 current_buffer->header_line_format);
13349 ++n;
13350 }
13351
13352 selected_frame = old_selected_frame;
13353 selected_window = old_selected_window;
13354 return n;
13355 }
13356
13357
13358 /* Display mode or top line of window W. FACE_ID specifies which line
13359 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
13360 FORMAT is the mode line format to display. Value is the pixel
13361 height of the mode line displayed. */
13362
13363 static int
13364 display_mode_line (w, face_id, format)
13365 struct window *w;
13366 enum face_id face_id;
13367 Lisp_Object format;
13368 {
13369 struct it it;
13370 struct face *face;
13371
13372 init_iterator (&it, w, -1, -1, NULL, face_id);
13373 prepare_desired_row (it.glyph_row);
13374
13375 if (! mode_line_inverse_video)
13376 /* Force the mode-line to be displayed in the default face. */
13377 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13378
13379 /* Temporarily make frame's keyboard the current kboard so that
13380 kboard-local variables in the mode_line_format will get the right
13381 values. */
13382 push_frame_kboard (it.f);
13383 display_mode_element (&it, 0, 0, 0, format);
13384 pop_frame_kboard ();
13385
13386 /* Fill up with spaces. */
13387 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13388
13389 compute_line_metrics (&it);
13390 it.glyph_row->full_width_p = 1;
13391 it.glyph_row->mode_line_p = 1;
13392 it.glyph_row->inverse_p = 0;
13393 it.glyph_row->continued_p = 0;
13394 it.glyph_row->truncated_on_left_p = 0;
13395 it.glyph_row->truncated_on_right_p = 0;
13396
13397 /* Make a 3D mode-line have a shadow at its right end. */
13398 face = FACE_FROM_ID (it.f, face_id);
13399 extend_face_to_end_of_line (&it);
13400 if (face->box != FACE_NO_BOX)
13401 {
13402 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13403 + it.glyph_row->used[TEXT_AREA] - 1);
13404 last->right_box_line_p = 1;
13405 }
13406
13407 return it.glyph_row->height;
13408 }
13409
13410
13411 /* Contribute ELT to the mode line for window IT->w. How it
13412 translates into text depends on its data type.
13413
13414 IT describes the display environment in which we display, as usual.
13415
13416 DEPTH is the depth in recursion. It is used to prevent
13417 infinite recursion here.
13418
13419 FIELD_WIDTH is the number of characters the display of ELT should
13420 occupy in the mode line, and PRECISION is the maximum number of
13421 characters to display from ELT's representation. See
13422 display_string for details. *
13423
13424 Returns the hpos of the end of the text generated by ELT. */
13425
13426 static int
13427 display_mode_element (it, depth, field_width, precision, elt)
13428 struct it *it;
13429 int depth;
13430 int field_width, precision;
13431 Lisp_Object elt;
13432 {
13433 int n = 0, field, prec;
13434
13435 tail_recurse:
13436 if (depth > 10)
13437 goto invalid;
13438
13439 depth++;
13440
13441 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
13442 {
13443 case Lisp_String:
13444 {
13445 /* A string: output it and check for %-constructs within it. */
13446 unsigned char c;
13447 unsigned char *this = XSTRING (elt)->data;
13448 unsigned char *lisp_string = this;
13449
13450 while ((precision <= 0 || n < precision)
13451 && *this
13452 && (frame_title_ptr
13453 || it->current_x < it->last_visible_x))
13454 {
13455 unsigned char *last = this;
13456
13457 /* Advance to end of string or next format specifier. */
13458 while ((c = *this++) != '\0' && c != '%')
13459 ;
13460
13461 if (this - 1 != last)
13462 {
13463 /* Output to end of string or up to '%'. Field width
13464 is length of string. Don't output more than
13465 PRECISION allows us. */
13466 --this;
13467
13468 prec = chars_in_text (last, this - last);
13469 if (precision > 0 && prec > precision - n)
13470 prec = precision - n;
13471
13472 if (frame_title_ptr)
13473 n += store_frame_title (last, 0, prec);
13474 else
13475 {
13476 int bytepos = last - lisp_string;
13477 int charpos = string_byte_to_char (elt, bytepos);
13478 n += display_string (NULL, elt, Qnil, 0, charpos,
13479 it, 0, prec, 0,
13480 STRING_MULTIBYTE (elt));
13481 }
13482 }
13483 else /* c == '%' */
13484 {
13485 unsigned char *percent_position = this;
13486
13487 /* Get the specified minimum width. Zero means
13488 don't pad. */
13489 field = 0;
13490 while ((c = *this++) >= '0' && c <= '9')
13491 field = field * 10 + c - '0';
13492
13493 /* Don't pad beyond the total padding allowed. */
13494 if (field_width - n > 0 && field > field_width - n)
13495 field = field_width - n;
13496
13497 /* Note that either PRECISION <= 0 or N < PRECISION. */
13498 prec = precision - n;
13499
13500 if (c == 'M')
13501 n += display_mode_element (it, depth, field, prec,
13502 Vglobal_mode_string);
13503 else if (c != 0)
13504 {
13505 int multibyte;
13506 unsigned char *spec
13507 = decode_mode_spec (it->w, c, field, prec, &multibyte);
13508
13509 if (frame_title_ptr)
13510 n += store_frame_title (spec, field, prec);
13511 else
13512 {
13513 int nglyphs_before, bytepos, charpos, nwritten;
13514
13515 nglyphs_before = it->glyph_row->used[TEXT_AREA];
13516 bytepos = percent_position - XSTRING (elt)->data;
13517 charpos = (multibyte
13518 ? string_byte_to_char (elt, bytepos)
13519 : bytepos);
13520 nwritten = display_string (spec, Qnil, elt,
13521 charpos, 0, it,
13522 field, prec, 0,
13523 multibyte);
13524
13525 /* Assign to the glyphs written above the
13526 string where the `%x' came from, position
13527 of the `%'. */
13528 if (nwritten > 0)
13529 {
13530 struct glyph *glyph
13531 = (it->glyph_row->glyphs[TEXT_AREA]
13532 + nglyphs_before);
13533 int i;
13534
13535 for (i = 0; i < nwritten; ++i)
13536 {
13537 glyph[i].object = elt;
13538 glyph[i].charpos = charpos;
13539 }
13540
13541 n += nwritten;
13542 }
13543 }
13544 }
13545 }
13546 }
13547 }
13548 break;
13549
13550 case Lisp_Symbol:
13551 /* A symbol: process the value of the symbol recursively
13552 as if it appeared here directly. Avoid error if symbol void.
13553 Special case: if value of symbol is a string, output the string
13554 literally. */
13555 {
13556 register Lisp_Object tem;
13557 tem = Fboundp (elt);
13558 if (!NILP (tem))
13559 {
13560 tem = Fsymbol_value (elt);
13561 /* If value is a string, output that string literally:
13562 don't check for % within it. */
13563 if (STRINGP (tem))
13564 {
13565 prec = precision - n;
13566 if (frame_title_ptr)
13567 n += store_frame_title (XSTRING (tem)->data, -1, prec);
13568 else
13569 n += display_string (NULL, tem, Qnil, 0, 0, it,
13570 0, prec, 0, STRING_MULTIBYTE (tem));
13571 }
13572 else if (!EQ (tem, elt))
13573 {
13574 /* Give up right away for nil or t. */
13575 elt = tem;
13576 goto tail_recurse;
13577 }
13578 }
13579 }
13580 break;
13581
13582 case Lisp_Cons:
13583 {
13584 register Lisp_Object car, tem;
13585
13586 /* A cons cell: three distinct cases.
13587 If first element is a string or a cons, process all the elements
13588 and effectively concatenate them.
13589 If first element is a negative number, truncate displaying cdr to
13590 at most that many characters. If positive, pad (with spaces)
13591 to at least that many characters.
13592 If first element is a symbol, process the cadr or caddr recursively
13593 according to whether the symbol's value is non-nil or nil. */
13594 car = XCAR (elt);
13595 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13596 {
13597 /* An element of the form (:eval FORM) means evaluate FORM
13598 and use the result as mode line elements. */
13599 struct gcpro gcpro1;
13600 Lisp_Object spec;
13601
13602 spec = safe_eval (XCAR (XCDR (elt)));
13603 GCPRO1 (spec);
13604 n += display_mode_element (it, depth, field_width - n,
13605 precision - n, spec);
13606 UNGCPRO;
13607 }
13608 else if (SYMBOLP (car))
13609 {
13610 tem = Fboundp (car);
13611 elt = XCDR (elt);
13612 if (!CONSP (elt))
13613 goto invalid;
13614 /* elt is now the cdr, and we know it is a cons cell.
13615 Use its car if CAR has a non-nil value. */
13616 if (!NILP (tem))
13617 {
13618 tem = Fsymbol_value (car);
13619 if (!NILP (tem))
13620 {
13621 elt = XCAR (elt);
13622 goto tail_recurse;
13623 }
13624 }
13625 /* Symbol's value is nil (or symbol is unbound)
13626 Get the cddr of the original list
13627 and if possible find the caddr and use that. */
13628 elt = XCDR (elt);
13629 if (NILP (elt))
13630 break;
13631 else if (!CONSP (elt))
13632 goto invalid;
13633 elt = XCAR (elt);
13634 goto tail_recurse;
13635 }
13636 else if (INTEGERP (car))
13637 {
13638 register int lim = XINT (car);
13639 elt = XCDR (elt);
13640 if (lim < 0)
13641 {
13642 /* Negative int means reduce maximum width. */
13643 if (precision <= 0)
13644 precision = -lim;
13645 else
13646 precision = min (precision, -lim);
13647 }
13648 else if (lim > 0)
13649 {
13650 /* Padding specified. Don't let it be more than
13651 current maximum. */
13652 if (precision > 0)
13653 lim = min (precision, lim);
13654
13655 /* If that's more padding than already wanted, queue it.
13656 But don't reduce padding already specified even if
13657 that is beyond the current truncation point. */
13658 field_width = max (lim, field_width);
13659 }
13660 goto tail_recurse;
13661 }
13662 else if (STRINGP (car) || CONSP (car))
13663 {
13664 register int limit = 50;
13665 /* Limit is to protect against circular lists. */
13666 while (CONSP (elt)
13667 && --limit > 0
13668 && (precision <= 0 || n < precision))
13669 {
13670 n += display_mode_element (it, depth, field_width - n,
13671 precision - n, XCAR (elt));
13672 elt = XCDR (elt);
13673 }
13674 }
13675 }
13676 break;
13677
13678 default:
13679 invalid:
13680 if (frame_title_ptr)
13681 n += store_frame_title ("*invalid*", 0, precision - n);
13682 else
13683 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13684 precision - n, 0, 0);
13685 return n;
13686 }
13687
13688 /* Pad to FIELD_WIDTH. */
13689 if (field_width > 0 && n < field_width)
13690 {
13691 if (frame_title_ptr)
13692 n += store_frame_title ("", field_width - n, 0);
13693 else
13694 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13695 0, 0, 0);
13696 }
13697
13698 return n;
13699 }
13700
13701
13702 /* Write a null-terminated, right justified decimal representation of
13703 the positive integer D to BUF using a minimal field width WIDTH. */
13704
13705 static void
13706 pint2str (buf, width, d)
13707 register char *buf;
13708 register int width;
13709 register int d;
13710 {
13711 register char *p = buf;
13712
13713 if (d <= 0)
13714 *p++ = '0';
13715 else
13716 {
13717 while (d > 0)
13718 {
13719 *p++ = d % 10 + '0';
13720 d /= 10;
13721 }
13722 }
13723
13724 for (width -= (int) (p - buf); width > 0; --width)
13725 *p++ = ' ';
13726 *p-- = '\0';
13727 while (p > buf)
13728 {
13729 d = *buf;
13730 *buf++ = *p;
13731 *p-- = d;
13732 }
13733 }
13734
13735 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
13736 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13737 type of CODING_SYSTEM. Return updated pointer into BUF. */
13738
13739 static unsigned char invalid_eol_type[] = "(*invalid*)";
13740
13741 static char *
13742 decode_mode_spec_coding (coding_system, buf, eol_flag)
13743 Lisp_Object coding_system;
13744 register char *buf;
13745 int eol_flag;
13746 {
13747 Lisp_Object val;
13748 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
13749 unsigned char *eol_str;
13750 int eol_str_len;
13751 /* The EOL conversion we are using. */
13752 Lisp_Object eoltype;
13753
13754 val = Fget (coding_system, Qcoding_system);
13755 eoltype = Qnil;
13756
13757 if (!VECTORP (val)) /* Not yet decided. */
13758 {
13759 if (multibyte)
13760 *buf++ = '-';
13761 if (eol_flag)
13762 eoltype = eol_mnemonic_undecided;
13763 /* Don't mention EOL conversion if it isn't decided. */
13764 }
13765 else
13766 {
13767 Lisp_Object eolvalue;
13768
13769 eolvalue = Fget (coding_system, Qeol_type);
13770
13771 if (multibyte)
13772 *buf++ = XFASTINT (AREF (val, 1));
13773
13774 if (eol_flag)
13775 {
13776 /* The EOL conversion that is normal on this system. */
13777
13778 if (NILP (eolvalue)) /* Not yet decided. */
13779 eoltype = eol_mnemonic_undecided;
13780 else if (VECTORP (eolvalue)) /* Not yet decided. */
13781 eoltype = eol_mnemonic_undecided;
13782 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13783 eoltype = (XFASTINT (eolvalue) == 0
13784 ? eol_mnemonic_unix
13785 : (XFASTINT (eolvalue) == 1
13786 ? eol_mnemonic_dos : eol_mnemonic_mac));
13787 }
13788 }
13789
13790 if (eol_flag)
13791 {
13792 /* Mention the EOL conversion if it is not the usual one. */
13793 if (STRINGP (eoltype))
13794 {
13795 eol_str = XSTRING (eoltype)->data;
13796 eol_str_len = XSTRING (eoltype)->size;
13797 }
13798 else if (INTEGERP (eoltype)
13799 && CHAR_VALID_P (XINT (eoltype), 0))
13800 {
13801 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13802 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13803 }
13804 else
13805 {
13806 eol_str = invalid_eol_type;
13807 eol_str_len = sizeof (invalid_eol_type) - 1;
13808 }
13809 bcopy (eol_str, buf, eol_str_len);
13810 buf += eol_str_len;
13811 }
13812
13813 return buf;
13814 }
13815
13816 /* Return a string for the output of a mode line %-spec for window W,
13817 generated by character C. PRECISION >= 0 means don't return a
13818 string longer than that value. FIELD_WIDTH > 0 means pad the
13819 string returned with spaces to that value. Return 1 in *MULTIBYTE
13820 if the result is multibyte text. */
13821
13822 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13823
13824 static char *
13825 decode_mode_spec (w, c, field_width, precision, multibyte)
13826 struct window *w;
13827 register int c;
13828 int field_width, precision;
13829 int *multibyte;
13830 {
13831 Lisp_Object obj;
13832 struct frame *f = XFRAME (WINDOW_FRAME (w));
13833 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13834 struct buffer *b = XBUFFER (w->buffer);
13835
13836 obj = Qnil;
13837 *multibyte = 0;
13838
13839 switch (c)
13840 {
13841 case '*':
13842 if (!NILP (b->read_only))
13843 return "%";
13844 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13845 return "*";
13846 return "-";
13847
13848 case '+':
13849 /* This differs from %* only for a modified read-only buffer. */
13850 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13851 return "*";
13852 if (!NILP (b->read_only))
13853 return "%";
13854 return "-";
13855
13856 case '&':
13857 /* This differs from %* in ignoring read-only-ness. */
13858 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13859 return "*";
13860 return "-";
13861
13862 case '%':
13863 return "%";
13864
13865 case '[':
13866 {
13867 int i;
13868 char *p;
13869
13870 if (command_loop_level > 5)
13871 return "[[[... ";
13872 p = decode_mode_spec_buf;
13873 for (i = 0; i < command_loop_level; i++)
13874 *p++ = '[';
13875 *p = 0;
13876 return decode_mode_spec_buf;
13877 }
13878
13879 case ']':
13880 {
13881 int i;
13882 char *p;
13883
13884 if (command_loop_level > 5)
13885 return " ...]]]";
13886 p = decode_mode_spec_buf;
13887 for (i = 0; i < command_loop_level; i++)
13888 *p++ = ']';
13889 *p = 0;
13890 return decode_mode_spec_buf;
13891 }
13892
13893 case '-':
13894 {
13895 register int i;
13896
13897 /* Let lots_of_dashes be a string of infinite length. */
13898 if (field_width <= 0
13899 || field_width > sizeof (lots_of_dashes))
13900 {
13901 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13902 decode_mode_spec_buf[i] = '-';
13903 decode_mode_spec_buf[i] = '\0';
13904 return decode_mode_spec_buf;
13905 }
13906 else
13907 return lots_of_dashes;
13908 }
13909
13910 case 'b':
13911 obj = b->name;
13912 break;
13913
13914 case 'c':
13915 {
13916 int col = current_column ();
13917 w->column_number_displayed = make_number (col);
13918 pint2str (decode_mode_spec_buf, field_width, col);
13919 return decode_mode_spec_buf;
13920 }
13921
13922 case 'F':
13923 /* %F displays the frame name. */
13924 if (!NILP (f->title))
13925 return (char *) XSTRING (f->title)->data;
13926 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13927 return (char *) XSTRING (f->name)->data;
13928 return "Emacs";
13929
13930 case 'f':
13931 obj = b->filename;
13932 break;
13933
13934 case 'l':
13935 {
13936 int startpos = XMARKER (w->start)->charpos;
13937 int startpos_byte = marker_byte_position (w->start);
13938 int line, linepos, linepos_byte, topline;
13939 int nlines, junk;
13940 int height = XFASTINT (w->height);
13941
13942 /* If we decided that this buffer isn't suitable for line numbers,
13943 don't forget that too fast. */
13944 if (EQ (w->base_line_pos, w->buffer))
13945 goto no_value;
13946 /* But do forget it, if the window shows a different buffer now. */
13947 else if (BUFFERP (w->base_line_pos))
13948 w->base_line_pos = Qnil;
13949
13950 /* If the buffer is very big, don't waste time. */
13951 if (INTEGERP (Vline_number_display_limit)
13952 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13953 {
13954 w->base_line_pos = Qnil;
13955 w->base_line_number = Qnil;
13956 goto no_value;
13957 }
13958
13959 if (!NILP (w->base_line_number)
13960 && !NILP (w->base_line_pos)
13961 && XFASTINT (w->base_line_pos) <= startpos)
13962 {
13963 line = XFASTINT (w->base_line_number);
13964 linepos = XFASTINT (w->base_line_pos);
13965 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13966 }
13967 else
13968 {
13969 line = 1;
13970 linepos = BUF_BEGV (b);
13971 linepos_byte = BUF_BEGV_BYTE (b);
13972 }
13973
13974 /* Count lines from base line to window start position. */
13975 nlines = display_count_lines (linepos, linepos_byte,
13976 startpos_byte,
13977 startpos, &junk);
13978
13979 topline = nlines + line;
13980
13981 /* Determine a new base line, if the old one is too close
13982 or too far away, or if we did not have one.
13983 "Too close" means it's plausible a scroll-down would
13984 go back past it. */
13985 if (startpos == BUF_BEGV (b))
13986 {
13987 w->base_line_number = make_number (topline);
13988 w->base_line_pos = make_number (BUF_BEGV (b));
13989 }
13990 else if (nlines < height + 25 || nlines > height * 3 + 50
13991 || linepos == BUF_BEGV (b))
13992 {
13993 int limit = BUF_BEGV (b);
13994 int limit_byte = BUF_BEGV_BYTE (b);
13995 int position;
13996 int distance = (height * 2 + 30) * line_number_display_limit_width;
13997
13998 if (startpos - distance > limit)
13999 {
14000 limit = startpos - distance;
14001 limit_byte = CHAR_TO_BYTE (limit);
14002 }
14003
14004 nlines = display_count_lines (startpos, startpos_byte,
14005 limit_byte,
14006 - (height * 2 + 30),
14007 &position);
14008 /* If we couldn't find the lines we wanted within
14009 line_number_display_limit_width chars per line,
14010 give up on line numbers for this window. */
14011 if (position == limit_byte && limit == startpos - distance)
14012 {
14013 w->base_line_pos = w->buffer;
14014 w->base_line_number = Qnil;
14015 goto no_value;
14016 }
14017
14018 w->base_line_number = make_number (topline - nlines);
14019 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
14020 }
14021
14022 /* Now count lines from the start pos to point. */
14023 nlines = display_count_lines (startpos, startpos_byte,
14024 PT_BYTE, PT, &junk);
14025
14026 /* Record that we did display the line number. */
14027 line_number_displayed = 1;
14028
14029 /* Make the string to show. */
14030 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
14031 return decode_mode_spec_buf;
14032 no_value:
14033 {
14034 char* p = decode_mode_spec_buf;
14035 int pad = field_width - 2;
14036 while (pad-- > 0)
14037 *p++ = ' ';
14038 *p++ = '?';
14039 *p++ = '?';
14040 *p = '\0';
14041 return decode_mode_spec_buf;
14042 }
14043 }
14044 break;
14045
14046 case 'm':
14047 obj = b->mode_name;
14048 break;
14049
14050 case 'n':
14051 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
14052 return " Narrow";
14053 break;
14054
14055 case 'p':
14056 {
14057 int pos = marker_position (w->start);
14058 int total = BUF_ZV (b) - BUF_BEGV (b);
14059
14060 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
14061 {
14062 if (pos <= BUF_BEGV (b))
14063 return "All";
14064 else
14065 return "Bottom";
14066 }
14067 else if (pos <= BUF_BEGV (b))
14068 return "Top";
14069 else
14070 {
14071 if (total > 1000000)
14072 /* Do it differently for a large value, to avoid overflow. */
14073 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14074 else
14075 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
14076 /* We can't normally display a 3-digit number,
14077 so get us a 2-digit number that is close. */
14078 if (total == 100)
14079 total = 99;
14080 sprintf (decode_mode_spec_buf, "%2d%%", total);
14081 return decode_mode_spec_buf;
14082 }
14083 }
14084
14085 /* Display percentage of size above the bottom of the screen. */
14086 case 'P':
14087 {
14088 int toppos = marker_position (w->start);
14089 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
14090 int total = BUF_ZV (b) - BUF_BEGV (b);
14091
14092 if (botpos >= BUF_ZV (b))
14093 {
14094 if (toppos <= BUF_BEGV (b))
14095 return "All";
14096 else
14097 return "Bottom";
14098 }
14099 else
14100 {
14101 if (total > 1000000)
14102 /* Do it differently for a large value, to avoid overflow. */
14103 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
14104 else
14105 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
14106 /* We can't normally display a 3-digit number,
14107 so get us a 2-digit number that is close. */
14108 if (total == 100)
14109 total = 99;
14110 if (toppos <= BUF_BEGV (b))
14111 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
14112 else
14113 sprintf (decode_mode_spec_buf, "%2d%%", total);
14114 return decode_mode_spec_buf;
14115 }
14116 }
14117
14118 case 's':
14119 /* status of process */
14120 obj = Fget_buffer_process (w->buffer);
14121 if (NILP (obj))
14122 return "no process";
14123 #ifdef subprocesses
14124 obj = Fsymbol_name (Fprocess_status (obj));
14125 #endif
14126 break;
14127
14128 case 't': /* indicate TEXT or BINARY */
14129 #ifdef MODE_LINE_BINARY_TEXT
14130 return MODE_LINE_BINARY_TEXT (b);
14131 #else
14132 return "T";
14133 #endif
14134
14135 case 'z':
14136 /* coding-system (not including end-of-line format) */
14137 case 'Z':
14138 /* coding-system (including end-of-line type) */
14139 {
14140 int eol_flag = (c == 'Z');
14141 char *p = decode_mode_spec_buf;
14142
14143 if (! FRAME_WINDOW_P (f))
14144 {
14145 /* No need to mention EOL here--the terminal never needs
14146 to do EOL conversion. */
14147 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
14148 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
14149 }
14150 p = decode_mode_spec_coding (b->buffer_file_coding_system,
14151 p, eol_flag);
14152
14153 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
14154 #ifdef subprocesses
14155 obj = Fget_buffer_process (Fcurrent_buffer ());
14156 if (PROCESSP (obj))
14157 {
14158 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
14159 p, eol_flag);
14160 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
14161 p, eol_flag);
14162 }
14163 #endif /* subprocesses */
14164 #endif /* 0 */
14165 *p = 0;
14166 return decode_mode_spec_buf;
14167 }
14168 }
14169
14170 if (STRINGP (obj))
14171 {
14172 *multibyte = STRING_MULTIBYTE (obj);
14173 return (char *) XSTRING (obj)->data;
14174 }
14175 else
14176 return "";
14177 }
14178
14179
14180 /* Count up to COUNT lines starting from START / START_BYTE.
14181 But don't go beyond LIMIT_BYTE.
14182 Return the number of lines thus found (always nonnegative).
14183
14184 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
14185
14186 static int
14187 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
14188 int start, start_byte, limit_byte, count;
14189 int *byte_pos_ptr;
14190 {
14191 register unsigned char *cursor;
14192 unsigned char *base;
14193
14194 register int ceiling;
14195 register unsigned char *ceiling_addr;
14196 int orig_count = count;
14197
14198 /* If we are not in selective display mode,
14199 check only for newlines. */
14200 int selective_display = (!NILP (current_buffer->selective_display)
14201 && !INTEGERP (current_buffer->selective_display));
14202
14203 if (count > 0)
14204 {
14205 while (start_byte < limit_byte)
14206 {
14207 ceiling = BUFFER_CEILING_OF (start_byte);
14208 ceiling = min (limit_byte - 1, ceiling);
14209 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
14210 base = (cursor = BYTE_POS_ADDR (start_byte));
14211 while (1)
14212 {
14213 if (selective_display)
14214 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
14215 ;
14216 else
14217 while (*cursor != '\n' && ++cursor != ceiling_addr)
14218 ;
14219
14220 if (cursor != ceiling_addr)
14221 {
14222 if (--count == 0)
14223 {
14224 start_byte += cursor - base + 1;
14225 *byte_pos_ptr = start_byte;
14226 return orig_count;
14227 }
14228 else
14229 if (++cursor == ceiling_addr)
14230 break;
14231 }
14232 else
14233 break;
14234 }
14235 start_byte += cursor - base;
14236 }
14237 }
14238 else
14239 {
14240 while (start_byte > limit_byte)
14241 {
14242 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
14243 ceiling = max (limit_byte, ceiling);
14244 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
14245 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
14246 while (1)
14247 {
14248 if (selective_display)
14249 while (--cursor != ceiling_addr
14250 && *cursor != '\n' && *cursor != 015)
14251 ;
14252 else
14253 while (--cursor != ceiling_addr && *cursor != '\n')
14254 ;
14255
14256 if (cursor != ceiling_addr)
14257 {
14258 if (++count == 0)
14259 {
14260 start_byte += cursor - base + 1;
14261 *byte_pos_ptr = start_byte;
14262 /* When scanning backwards, we should
14263 not count the newline posterior to which we stop. */
14264 return - orig_count - 1;
14265 }
14266 }
14267 else
14268 break;
14269 }
14270 /* Here we add 1 to compensate for the last decrement
14271 of CURSOR, which took it past the valid range. */
14272 start_byte += cursor - base + 1;
14273 }
14274 }
14275
14276 *byte_pos_ptr = limit_byte;
14277
14278 if (count < 0)
14279 return - orig_count + count;
14280 return orig_count - count;
14281
14282 }
14283
14284
14285 \f
14286 /***********************************************************************
14287 Displaying strings
14288 ***********************************************************************/
14289
14290 /* Display a NUL-terminated string, starting with index START.
14291
14292 If STRING is non-null, display that C string. Otherwise, the Lisp
14293 string LISP_STRING is displayed.
14294
14295 If FACE_STRING is not nil, FACE_STRING_POS is a position in
14296 FACE_STRING. Display STRING or LISP_STRING with the face at
14297 FACE_STRING_POS in FACE_STRING:
14298
14299 Display the string in the environment given by IT, but use the
14300 standard display table, temporarily.
14301
14302 FIELD_WIDTH is the minimum number of output glyphs to produce.
14303 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14304 with spaces. If STRING has more characters, more than FIELD_WIDTH
14305 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
14306
14307 PRECISION is the maximum number of characters to output from
14308 STRING. PRECISION < 0 means don't truncate the string.
14309
14310 This is roughly equivalent to printf format specifiers:
14311
14312 FIELD_WIDTH PRECISION PRINTF
14313 ----------------------------------------
14314 -1 -1 %s
14315 -1 10 %.10s
14316 10 -1 %10s
14317 20 10 %20.10s
14318
14319 MULTIBYTE zero means do not display multibyte chars, > 0 means do
14320 display them, and < 0 means obey the current buffer's value of
14321 enable_multibyte_characters.
14322
14323 Value is the number of glyphs produced. */
14324
14325 static int
14326 display_string (string, lisp_string, face_string, face_string_pos,
14327 start, it, field_width, precision, max_x, multibyte)
14328 unsigned char *string;
14329 Lisp_Object lisp_string;
14330 Lisp_Object face_string;
14331 int face_string_pos;
14332 int start;
14333 struct it *it;
14334 int field_width, precision, max_x;
14335 int multibyte;
14336 {
14337 int hpos_at_start = it->hpos;
14338 int saved_face_id = it->face_id;
14339 struct glyph_row *row = it->glyph_row;
14340
14341 /* Initialize the iterator IT for iteration over STRING beginning
14342 with index START. */
14343 reseat_to_string (it, string, lisp_string, start,
14344 precision, field_width, multibyte);
14345
14346 /* If displaying STRING, set up the face of the iterator
14347 from LISP_STRING, if that's given. */
14348 if (STRINGP (face_string))
14349 {
14350 int endptr;
14351 struct face *face;
14352
14353 it->face_id
14354 = face_at_string_position (it->w, face_string, face_string_pos,
14355 0, it->region_beg_charpos,
14356 it->region_end_charpos,
14357 &endptr, it->base_face_id, 0);
14358 face = FACE_FROM_ID (it->f, it->face_id);
14359 it->face_box_p = face->box != FACE_NO_BOX;
14360 }
14361
14362 /* Set max_x to the maximum allowed X position. Don't let it go
14363 beyond the right edge of the window. */
14364 if (max_x <= 0)
14365 max_x = it->last_visible_x;
14366 else
14367 max_x = min (max_x, it->last_visible_x);
14368
14369 /* Skip over display elements that are not visible. because IT->w is
14370 hscrolled. */
14371 if (it->current_x < it->first_visible_x)
14372 move_it_in_display_line_to (it, 100000, it->first_visible_x,
14373 MOVE_TO_POS | MOVE_TO_X);
14374
14375 row->ascent = it->max_ascent;
14376 row->height = it->max_ascent + it->max_descent;
14377 row->phys_ascent = it->max_phys_ascent;
14378 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14379
14380 /* This condition is for the case that we are called with current_x
14381 past last_visible_x. */
14382 while (it->current_x < max_x)
14383 {
14384 int x_before, x, n_glyphs_before, i, nglyphs;
14385
14386 /* Get the next display element. */
14387 if (!get_next_display_element (it))
14388 break;
14389
14390 /* Produce glyphs. */
14391 x_before = it->current_x;
14392 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
14393 PRODUCE_GLYPHS (it);
14394
14395 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14396 i = 0;
14397 x = x_before;
14398 while (i < nglyphs)
14399 {
14400 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14401
14402 if (!it->truncate_lines_p
14403 && x + glyph->pixel_width > max_x)
14404 {
14405 /* End of continued line or max_x reached. */
14406 if (CHAR_GLYPH_PADDING_P (*glyph))
14407 {
14408 /* A wide character is unbreakable. */
14409 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14410 it->current_x = x_before;
14411 }
14412 else
14413 {
14414 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14415 it->current_x = x;
14416 }
14417 break;
14418 }
14419 else if (x + glyph->pixel_width > it->first_visible_x)
14420 {
14421 /* Glyph is at least partially visible. */
14422 ++it->hpos;
14423 if (x < it->first_visible_x)
14424 it->glyph_row->x = x - it->first_visible_x;
14425 }
14426 else
14427 {
14428 /* Glyph is off the left margin of the display area.
14429 Should not happen. */
14430 abort ();
14431 }
14432
14433 row->ascent = max (row->ascent, it->max_ascent);
14434 row->height = max (row->height, it->max_ascent + it->max_descent);
14435 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14436 row->phys_height = max (row->phys_height,
14437 it->max_phys_ascent + it->max_phys_descent);
14438 x += glyph->pixel_width;
14439 ++i;
14440 }
14441
14442 /* Stop if max_x reached. */
14443 if (i < nglyphs)
14444 break;
14445
14446 /* Stop at line ends. */
14447 if (ITERATOR_AT_END_OF_LINE_P (it))
14448 {
14449 it->continuation_lines_width = 0;
14450 break;
14451 }
14452
14453 set_iterator_to_next (it, 1);
14454
14455 /* Stop if truncating at the right edge. */
14456 if (it->truncate_lines_p
14457 && it->current_x >= it->last_visible_x)
14458 {
14459 /* Add truncation mark, but don't do it if the line is
14460 truncated at a padding space. */
14461 if (IT_CHARPOS (*it) < it->string_nchars)
14462 {
14463 if (!FRAME_WINDOW_P (it->f))
14464 {
14465 int i, n;
14466
14467 if (it->current_x > it->last_visible_x)
14468 {
14469 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14470 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14471 break;
14472 for (n = row->used[TEXT_AREA]; i < n; ++i)
14473 {
14474 row->used[TEXT_AREA] = i;
14475 produce_special_glyphs (it, IT_TRUNCATION);
14476 }
14477 }
14478 produce_special_glyphs (it, IT_TRUNCATION);
14479 }
14480 it->glyph_row->truncated_on_right_p = 1;
14481 }
14482 break;
14483 }
14484 }
14485
14486 /* Maybe insert a truncation at the left. */
14487 if (it->first_visible_x
14488 && IT_CHARPOS (*it) > 0)
14489 {
14490 if (!FRAME_WINDOW_P (it->f))
14491 insert_left_trunc_glyphs (it);
14492 it->glyph_row->truncated_on_left_p = 1;
14493 }
14494
14495 it->face_id = saved_face_id;
14496
14497 /* Value is number of columns displayed. */
14498 return it->hpos - hpos_at_start;
14499 }
14500
14501
14502 \f
14503 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
14504 appears as an element of LIST or as the car of an element of LIST.
14505 If PROPVAL is a list, compare each element against LIST in that
14506 way, and return 1/2 if any element of PROPVAL is found in LIST.
14507 Otherwise return 0. This function cannot quit.
14508 The return value is 2 if the text is invisible but with an ellipsis
14509 and 1 if it's invisible and without an ellipsis. */
14510
14511 int
14512 invisible_p (propval, list)
14513 register Lisp_Object propval;
14514 Lisp_Object list;
14515 {
14516 register Lisp_Object tail, proptail;
14517
14518 for (tail = list; CONSP (tail); tail = XCDR (tail))
14519 {
14520 register Lisp_Object tem;
14521 tem = XCAR (tail);
14522 if (EQ (propval, tem))
14523 return 1;
14524 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14525 return NILP (XCDR (tem)) ? 1 : 2;
14526 }
14527
14528 if (CONSP (propval))
14529 {
14530 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14531 {
14532 Lisp_Object propelt;
14533 propelt = XCAR (proptail);
14534 for (tail = list; CONSP (tail); tail = XCDR (tail))
14535 {
14536 register Lisp_Object tem;
14537 tem = XCAR (tail);
14538 if (EQ (propelt, tem))
14539 return 1;
14540 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14541 return NILP (XCDR (tem)) ? 1 : 2;
14542 }
14543 }
14544 }
14545
14546 return 0;
14547 }
14548
14549 \f
14550 /***********************************************************************
14551 Initialization
14552 ***********************************************************************/
14553
14554 void
14555 syms_of_xdisp ()
14556 {
14557 Vwith_echo_area_save_vector = Qnil;
14558 staticpro (&Vwith_echo_area_save_vector);
14559
14560 Vmessage_stack = Qnil;
14561 staticpro (&Vmessage_stack);
14562
14563 Qinhibit_redisplay = intern ("inhibit-redisplay");
14564 staticpro (&Qinhibit_redisplay);
14565
14566 #if GLYPH_DEBUG
14567 defsubr (&Sdump_glyph_matrix);
14568 defsubr (&Sdump_glyph_row);
14569 defsubr (&Sdump_tool_bar_row);
14570 defsubr (&Strace_redisplay);
14571 defsubr (&Strace_to_stderr);
14572 #endif
14573 #ifdef HAVE_WINDOW_SYSTEM
14574 defsubr (&Stool_bar_lines_needed);
14575 #endif
14576
14577 staticpro (&Qmenu_bar_update_hook);
14578 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14579
14580 staticpro (&Qoverriding_terminal_local_map);
14581 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
14582
14583 staticpro (&Qoverriding_local_map);
14584 Qoverriding_local_map = intern ("overriding-local-map");
14585
14586 staticpro (&Qwindow_scroll_functions);
14587 Qwindow_scroll_functions = intern ("window-scroll-functions");
14588
14589 staticpro (&Qredisplay_end_trigger_functions);
14590 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
14591
14592 staticpro (&Qinhibit_point_motion_hooks);
14593 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14594
14595 QCdata = intern (":data");
14596 staticpro (&QCdata);
14597 Qdisplay = intern ("display");
14598 staticpro (&Qdisplay);
14599 Qspace_width = intern ("space-width");
14600 staticpro (&Qspace_width);
14601 Qraise = intern ("raise");
14602 staticpro (&Qraise);
14603 Qspace = intern ("space");
14604 staticpro (&Qspace);
14605 Qmargin = intern ("margin");
14606 staticpro (&Qmargin);
14607 Qleft_margin = intern ("left-margin");
14608 staticpro (&Qleft_margin);
14609 Qright_margin = intern ("right-margin");
14610 staticpro (&Qright_margin);
14611 Qalign_to = intern ("align-to");
14612 staticpro (&Qalign_to);
14613 QCalign_to = intern (":align-to");
14614 staticpro (&QCalign_to);
14615 Qrelative_width = intern ("relative-width");
14616 staticpro (&Qrelative_width);
14617 QCrelative_width = intern (":relative-width");
14618 staticpro (&QCrelative_width);
14619 QCrelative_height = intern (":relative-height");
14620 staticpro (&QCrelative_height);
14621 QCeval = intern (":eval");
14622 staticpro (&QCeval);
14623 Qwhen = intern ("when");
14624 staticpro (&Qwhen);
14625 QCfile = intern (":file");
14626 staticpro (&QCfile);
14627 Qfontified = intern ("fontified");
14628 staticpro (&Qfontified);
14629 Qfontification_functions = intern ("fontification-functions");
14630 staticpro (&Qfontification_functions);
14631 Qtrailing_whitespace = intern ("trailing-whitespace");
14632 staticpro (&Qtrailing_whitespace);
14633 Qimage = intern ("image");
14634 staticpro (&Qimage);
14635 Qmessage_truncate_lines = intern ("message-truncate-lines");
14636 staticpro (&Qmessage_truncate_lines);
14637 Qgrow_only = intern ("grow-only");
14638 staticpro (&Qgrow_only);
14639 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14640 staticpro (&Qinhibit_menubar_update);
14641 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
14642 staticpro (&Qinhibit_eval_during_redisplay);
14643 Qposition = intern ("position");
14644 staticpro (&Qposition);
14645 Qbuffer_position = intern ("buffer-position");
14646 staticpro (&Qbuffer_position);
14647 Qobject = intern ("object");
14648 staticpro (&Qobject);
14649
14650 last_arrow_position = Qnil;
14651 last_arrow_string = Qnil;
14652 staticpro (&last_arrow_position);
14653 staticpro (&last_arrow_string);
14654
14655 echo_buffer[0] = echo_buffer[1] = Qnil;
14656 staticpro (&echo_buffer[0]);
14657 staticpro (&echo_buffer[1]);
14658
14659 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14660 staticpro (&echo_area_buffer[0]);
14661 staticpro (&echo_area_buffer[1]);
14662
14663 Vmessages_buffer_name = build_string ("*Messages*");
14664 staticpro (&Vmessages_buffer_name);
14665
14666 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14667 doc: /* Non-nil means highlight trailing whitespace.
14668 The face used for trailing whitespace is `trailing-whitespace'. */);
14669 Vshow_trailing_whitespace = Qnil;
14670
14671 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14672 doc: /* Non-nil means don't actually do any redisplay.
14673 This is used for internal purposes. */);
14674 Vinhibit_redisplay = Qnil;
14675
14676 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
14677 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
14678 Vglobal_mode_string = Qnil;
14679
14680 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14681 doc: /* Marker for where to display an arrow on top of the buffer text.
14682 This must be the beginning of a line in order to work.
14683 See also `overlay-arrow-string'. */);
14684 Voverlay_arrow_position = Qnil;
14685
14686 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14687 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
14688 Voverlay_arrow_string = Qnil;
14689
14690 DEFVAR_INT ("scroll-step", &scroll_step,
14691 doc: /* *The number of lines to try scrolling a window by when point moves out.
14692 If that fails to bring point back on frame, point is centered instead.
14693 If this is zero, point is always centered after it moves off frame.
14694 If you want scrolling to always be a line at a time, you should set
14695 `scroll-conservatively' to a large value rather than set this to 1. */);
14696
14697 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
14698 doc: /* *Scroll up to this many lines, to bring point back on screen.
14699 A value of zero means to scroll the text to center point vertically
14700 in the window. */);
14701 scroll_conservatively = 0;
14702
14703 DEFVAR_INT ("scroll-margin", &scroll_margin,
14704 doc: /* *Number of lines of margin at the top and bottom of a window.
14705 Recenter the window whenever point gets within this many lines
14706 of the top or bottom of the window. */);
14707 scroll_margin = 0;
14708
14709 #if GLYPH_DEBUG
14710 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
14711 #endif
14712
14713 DEFVAR_BOOL ("truncate-partial-width-windows",
14714 &truncate_partial_width_windows,
14715 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
14716 truncate_partial_width_windows = 1;
14717
14718 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
14719 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
14720 Any other value means to use the appropriate face, `mode-line',
14721 `header-line', or `menu' respectively.
14722
14723 This variable is deprecated; please change the above faces instead. */);
14724 mode_line_inverse_video = 1;
14725
14726 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
14727 doc: /* *Maximum buffer size for which line number should be displayed.
14728 If the buffer is bigger than this, the line number does not appear
14729 in the mode line. A value of nil means no limit. */);
14730 Vline_number_display_limit = Qnil;
14731
14732 DEFVAR_INT ("line-number-display-limit-width",
14733 &line_number_display_limit_width,
14734 doc: /* *Maximum line width (in characters) for line number display.
14735 If the average length of the lines near point is bigger than this, then the
14736 line number may be omitted from the mode line. */);
14737 line_number_display_limit_width = 200;
14738
14739 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14740 doc: /* *Non-nil means highlight region even in nonselected windows. */);
14741 highlight_nonselected_windows = 0;
14742
14743 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
14744 doc: /* Non-nil if more than one frame is visible on this display.
14745 Minibuffer-only frames don't count, but iconified frames do.
14746 This variable is not guaranteed to be accurate except while processing
14747 `frame-title-format' and `icon-title-format'. */);
14748
14749 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
14750 doc: /* Template for displaying the title bar of visible frames.
14751 \(Assuming the window manager supports this feature.)
14752 This variable has the same structure as `mode-line-format' (which see),
14753 and is used only on frames for which no explicit name has been set
14754 \(see `modify-frame-parameters'). */);
14755 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
14756 doc: /* Template for displaying the title bar of an iconified frame.
14757 \(Assuming the window manager supports this feature.)
14758 This variable has the same structure as `mode-line-format' (which see),
14759 and is used only on frames for which no explicit name has been set
14760 \(see `modify-frame-parameters'). */);
14761 Vicon_title_format
14762 = Vframe_title_format
14763 = Fcons (intern ("multiple-frames"),
14764 Fcons (build_string ("%b"),
14765 Fcons (Fcons (empty_string,
14766 Fcons (intern ("invocation-name"),
14767 Fcons (build_string ("@"),
14768 Fcons (intern ("system-name"),
14769 Qnil)))),
14770 Qnil)));
14771
14772 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14773 doc: /* Maximum number of lines to keep in the message log buffer.
14774 If nil, disable message logging. If t, log messages but don't truncate
14775 the buffer when it becomes large. */);
14776 Vmessage_log_max = make_number (50);
14777
14778 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14779 doc: /* Functions called before redisplay, if window sizes have changed.
14780 The value should be a list of functions that take one argument.
14781 Just before redisplay, for each frame, if any of its windows have changed
14782 size since the last redisplay, or have been split or deleted,
14783 all the functions in the list are called, with the frame as argument. */);
14784 Vwindow_size_change_functions = Qnil;
14785
14786 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
14787 doc: /* List of Functions to call before redisplaying a window with scrolling.
14788 Each function is called with two arguments, the window
14789 and its new display-start position. Note that the value of `window-end'
14790 is not valid when these functions are called. */);
14791 Vwindow_scroll_functions = Qnil;
14792
14793 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14794 doc: /* *Non-nil means automatically resize tool-bars.
14795 This increases a tool-bar's height if not all tool-bar items are visible.
14796 It decreases a tool-bar's height when it would display blank lines
14797 otherwise. */);
14798 auto_resize_tool_bars_p = 1;
14799
14800 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14801 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
14802 auto_raise_tool_bar_buttons_p = 1;
14803
14804 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14805 doc: /* *Margin around tool-bar buttons in pixels.
14806 If an integer, use that for both horizontal and vertical margins.
14807 Otherwise, value should be a pair of integers `(HORZ : VERT)' with
14808 HORZ specifying the horizontal margin, and VERT specifying the
14809 vertical margin. */);
14810 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
14811
14812 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14813 doc: /* Relief thickness of tool-bar buttons. */);
14814 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
14815
14816 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14817 doc: /* List of functions to call to fontify regions of text.
14818 Each function is called with one argument POS. Functions must
14819 fontify a region starting at POS in the current buffer, and give
14820 fontified regions the property `fontified'. */);
14821 Vfontification_functions = Qnil;
14822 Fmake_variable_buffer_local (Qfontification_functions);
14823
14824 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14825 &unibyte_display_via_language_environment,
14826 doc: /* *Non-nil means display unibyte text according to language environment.
14827 Specifically this means that unibyte non-ASCII characters
14828 are displayed by converting them to the equivalent multibyte characters
14829 according to the current language environment. As a result, they are
14830 displayed according to the current fontset. */);
14831 unibyte_display_via_language_environment = 0;
14832
14833 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14834 doc: /* *Maximum height for resizing mini-windows.
14835 If a float, it specifies a fraction of the mini-window frame's height.
14836 If an integer, it specifies a number of lines. */);
14837 Vmax_mini_window_height = make_float (0.25);
14838
14839 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14840 doc: /* *How to resize mini-windows.
14841 A value of nil means don't automatically resize mini-windows.
14842 A value of t means resize them to fit the text displayed in them.
14843 A value of `grow-only', the default, means let mini-windows grow
14844 only, until their display becomes empty, at which point the windows
14845 go back to their normal size. */);
14846 Vresize_mini_windows = Qgrow_only;
14847
14848 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14849 &cursor_in_non_selected_windows,
14850 doc: /* *Non-nil means display a hollow cursor in non-selected windows.
14851 Nil means don't display a cursor there. */);
14852 cursor_in_non_selected_windows = 1;
14853
14854 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14855 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
14856 automatic_hscrolling_p = 1;
14857
14858 DEFVAR_LISP ("image-types", &Vimage_types,
14859 doc: /* List of supported image types.
14860 Each element of the list is a symbol for a supported image type. */);
14861 Vimage_types = Qnil;
14862
14863 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14864 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
14865 Bind this around calls to `message' to let it take effect. */);
14866 message_truncate_lines = 0;
14867
14868 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14869 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
14870 Can be used to update submenus whose contents should vary. */);
14871 Vmenu_bar_update_hook = Qnil;
14872
14873 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14874 doc: /* Non-nil means don't update menu bars. Internal use only. */);
14875 inhibit_menubar_update = 0;
14876
14877 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
14878 doc: /* Non-nil means don't eval Lisp during redisplay. */);
14879 inhibit_eval_during_redisplay = 0;
14880 }
14881
14882
14883 /* Initialize this module when Emacs starts. */
14884
14885 void
14886 init_xdisp ()
14887 {
14888 Lisp_Object root_window;
14889 struct window *mini_w;
14890
14891 current_header_line_height = current_mode_line_height = -1;
14892
14893 CHARPOS (this_line_start_pos) = 0;
14894
14895 mini_w = XWINDOW (minibuf_window);
14896 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14897
14898 if (!noninteractive)
14899 {
14900 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14901 int i;
14902
14903 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
14904 set_window_height (root_window,
14905 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14906 0);
14907 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
14908 set_window_height (minibuf_window, 1, 0);
14909
14910 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14911 mini_w->width = make_number (FRAME_WIDTH (f));
14912
14913 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14914 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14915 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14916
14917 /* The default ellipsis glyphs `...'. */
14918 for (i = 0; i < 3; ++i)
14919 default_invis_vector[i] = make_number ('.');
14920 }
14921
14922 #ifdef HAVE_WINDOW_SYSTEM
14923 {
14924 /* Allocate the buffer for frame titles. */
14925 int size = 100;
14926 frame_title_buf = (char *) xmalloc (size);
14927 frame_title_buf_end = frame_title_buf + size;
14928 frame_title_ptr = NULL;
14929 }
14930 #endif /* HAVE_WINDOW_SYSTEM */
14931
14932 help_echo_showing_p = 0;
14933 }
14934
14935