]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(prepare_menu_bars): Changes for tip_frame being a
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the the description of direct update
37 operations, below.).
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay() +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking a iterator structure (struct it)
124 argument.
125
126 Iteration over things to be be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator
128 (or init_string_iterator for that matter). Calls to
129 get_next_display_element fill the iterator structure with relevant
130 information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "macros.h"
183 #include "disptab.h"
184 #include "termhooks.h"
185 #include "intervals.h"
186 #include "coding.h"
187 #include "process.h"
188 #include "region-cache.h"
189 #include "fontset.h"
190
191 #ifdef HAVE_X_WINDOWS
192 #include "xterm.h"
193 #endif
194 #ifdef WINDOWSNT
195 #include "w32term.h"
196 #endif
197 #ifdef macintosh
198 #include "macterm.h"
199 #endif
200
201 #define min(a, b) ((a) < (b) ? (a) : (b))
202 #define max(a, b) ((a) > (b) ? (a) : (b))
203
204 #define INFINITY 10000000
205
206 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
207 extern void set_frame_menubar P_ ((struct frame *f, int, int));
208 extern int pending_menu_activation;
209 #endif
210
211 extern int interrupt_input;
212 extern int command_loop_level;
213
214 extern int minibuffer_auto_raise;
215
216 extern Lisp_Object Qface;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221
222 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
223 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
224 Lisp_Object Qredisplay_end_trigger_functions;
225 Lisp_Object Qinhibit_point_motion_hooks;
226 Lisp_Object QCeval, Qwhen, QCfile, QCdata;
227 Lisp_Object Qfontified;
228 Lisp_Object Qgrow_only;
229
230 /* Functions called to fontify regions of text. */
231
232 Lisp_Object Vfontification_functions;
233 Lisp_Object Qfontification_functions;
234
235 /* Non-zero means draw tool bar buttons raised when the mouse moves
236 over them. */
237
238 int auto_raise_tool_bar_buttons_p;
239
240 /* Margin around tool bar buttons in pixels. */
241
242 int tool_bar_button_margin;
243
244 /* Thickness of shadow to draw around tool bar buttons. */
245
246 int tool_bar_button_relief;
247
248 /* Non-zero means automatically resize tool-bars so that all tool-bar
249 items are visible, and no blank lines remain. */
250
251 int auto_resize_tool_bars_p;
252
253 /* Non-nil means don't actually do any redisplay. */
254
255 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
256
257 /* Names of text properties relevant for redisplay. */
258
259 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
260 extern Lisp_Object Qface, Qinvisible, Qimage, Qwidth;
261
262 /* Symbols used in text property values. */
263
264 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
265 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
266 Lisp_Object Qmargin;
267 extern Lisp_Object Qheight;
268
269 /* Non-nil means highlight trailing whitespace. */
270
271 Lisp_Object Vshow_trailing_whitespace;
272
273 /* Name of the face used to highlight trailing whitespace. */
274
275 Lisp_Object Qtrailing_whitespace;
276
277 /* The symbol `image' which is the car of the lists used to represent
278 images in Lisp. */
279
280 Lisp_Object Qimage;
281
282 /* Non-zero means print newline to stdout before next mini-buffer
283 message. */
284
285 int noninteractive_need_newline;
286
287 /* Non-zero means print newline to message log before next message. */
288
289 static int message_log_need_newline;
290
291 \f
292 /* The buffer position of the first character appearing entirely or
293 partially on the line of the selected window which contains the
294 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
295 redisplay optimization in redisplay_internal. */
296
297 static struct text_pos this_line_start_pos;
298
299 /* Number of characters past the end of the line above, including the
300 terminating newline. */
301
302 static struct text_pos this_line_end_pos;
303
304 /* The vertical positions and the height of this line. */
305
306 static int this_line_vpos;
307 static int this_line_y;
308 static int this_line_pixel_height;
309
310 /* X position at which this display line starts. Usually zero;
311 negative if first character is partially visible. */
312
313 static int this_line_start_x;
314
315 /* Buffer that this_line_.* variables are referring to. */
316
317 static struct buffer *this_line_buffer;
318
319 /* Nonzero means truncate lines in all windows less wide than the
320 frame. */
321
322 int truncate_partial_width_windows;
323
324 /* A flag to control how to display unibyte 8-bit character. */
325
326 int unibyte_display_via_language_environment;
327
328 /* Nonzero means we have more than one non-mini-buffer-only frame.
329 Not guaranteed to be accurate except while parsing
330 frame-title-format. */
331
332 int multiple_frames;
333
334 Lisp_Object Vglobal_mode_string;
335
336 /* Marker for where to display an arrow on top of the buffer text. */
337
338 Lisp_Object Voverlay_arrow_position;
339
340 /* String to display for the arrow. Only used on terminal frames. */
341
342 Lisp_Object Voverlay_arrow_string;
343
344 /* Values of those variables at last redisplay. However, if
345 Voverlay_arrow_position is a marker, last_arrow_position is its
346 numerical position. */
347
348 static Lisp_Object last_arrow_position, last_arrow_string;
349
350 /* Like mode-line-format, but for the title bar on a visible frame. */
351
352 Lisp_Object Vframe_title_format;
353
354 /* Like mode-line-format, but for the title bar on an iconified frame. */
355
356 Lisp_Object Vicon_title_format;
357
358 /* List of functions to call when a window's size changes. These
359 functions get one arg, a frame on which one or more windows' sizes
360 have changed. */
361
362 static Lisp_Object Vwindow_size_change_functions;
363
364 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
365
366 /* Nonzero if overlay arrow has been displayed once in this window. */
367
368 static int overlay_arrow_seen;
369
370 /* Nonzero means highlight the region even in nonselected windows. */
371
372 int highlight_nonselected_windows;
373
374 /* If cursor motion alone moves point off frame, try scrolling this
375 many lines up or down if that will bring it back. */
376
377 static int scroll_step;
378
379 /* Non-0 means scroll just far enough to bring point back on the
380 screen, when appropriate. */
381
382 static int scroll_conservatively;
383
384 /* Recenter the window whenever point gets within this many lines of
385 the top or bottom of the window. This value is translated into a
386 pixel value by multiplying it with CANON_Y_UNIT, which means that
387 there is really a fixed pixel height scroll margin. */
388
389 int scroll_margin;
390
391 /* Number of windows showing the buffer of the selected window (or
392 another buffer with the same base buffer). keyboard.c refers to
393 this. */
394
395 int buffer_shared;
396
397 /* Vector containing glyphs for an ellipsis `...'. */
398
399 static Lisp_Object default_invis_vector[3];
400
401 /* Zero means display the mode-line/header-line/menu-bar in the default face
402 (this slightly odd definition is for compatibility with previous versions
403 of emacs), non-zero means display them using their respective faces.
404
405 This variable is deprecated. */
406
407 int mode_line_inverse_video;
408
409 /* Prompt to display in front of the mini-buffer contents. */
410
411 Lisp_Object minibuf_prompt;
412
413 /* Width of current mini-buffer prompt. Only set after display_line
414 of the line that contains the prompt. */
415
416 int minibuf_prompt_width;
417 int minibuf_prompt_pixel_width;
418
419 /* This is the window where the echo area message was displayed. It
420 is always a mini-buffer window, but it may not be the same window
421 currently active as a mini-buffer. */
422
423 Lisp_Object echo_area_window;
424
425 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
426 pushes the current message and the value of
427 message_enable_multibyte on the stack, the function restore_message
428 pops the stack and displays MESSAGE again. */
429
430 Lisp_Object Vmessage_stack;
431
432 /* Nonzero means multibyte characters were enabled when the echo area
433 message was specified. */
434
435 int message_enable_multibyte;
436
437 /* True if we should redraw the mode lines on the next redisplay. */
438
439 int update_mode_lines;
440
441 /* Nonzero if window sizes or contents have changed since last
442 redisplay that finished */
443
444 int windows_or_buffers_changed;
445
446 /* Nonzero after display_mode_line if %l was used and it displayed a
447 line number. */
448
449 int line_number_displayed;
450
451 /* Maximum buffer size for which to display line numbers. */
452
453 Lisp_Object Vline_number_display_limit;
454
455 /* line width to consider when repostioning for line number display */
456
457 static int line_number_display_limit_width;
458
459 /* Number of lines to keep in the message log buffer. t means
460 infinite. nil means don't log at all. */
461
462 Lisp_Object Vmessage_log_max;
463
464 /* The name of the *Messages* buffer, a string. */
465
466 static Lisp_Object Vmessages_buffer_name;
467
468 /* Current, index 0, and last displayed echo area message. Either
469 buffers from echo_buffers, or nil to indicate no message. */
470
471 Lisp_Object echo_area_buffer[2];
472
473 /* The buffers referenced from echo_area_buffer. */
474
475 static Lisp_Object echo_buffer[2];
476
477 /* A vector saved used in with_area_buffer to reduce consing. */
478
479 static Lisp_Object Vwith_echo_area_save_vector;
480
481 /* Non-zero means display_echo_area should display the last echo area
482 message again. Set by redisplay_preserve_echo_area. */
483
484 static int display_last_displayed_message_p;
485
486 /* Nonzero if echo area is being used by print; zero if being used by
487 message. */
488
489 int message_buf_print;
490
491 /* Maximum height for resizing mini-windows. Either a float
492 specifying a fraction of the available height, or an integer
493 specifying a number of lines. */
494
495 Lisp_Object Vmax_mini_window_height;
496
497 /* Non-zero means messages should be displayed with truncated
498 lines instead of being continued. */
499
500 int message_truncate_lines;
501 Lisp_Object Qmessage_truncate_lines;
502
503 /* Non-zero means we want a hollow cursor in windows that are not
504 selected. Zero means there's no cursor in such windows. */
505
506 int cursor_in_non_selected_windows;
507
508 /* A scratch glyph row with contents used for generating truncation
509 glyphs. Also used in direct_output_for_insert. */
510
511 #define MAX_SCRATCH_GLYPHS 100
512 struct glyph_row scratch_glyph_row;
513 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
514
515 /* Ascent and height of the last line processed by move_it_to. */
516
517 static int last_max_ascent, last_height;
518
519 /* Non-zero if there's a help-echo in the echo area. */
520
521 int help_echo_showing_p;
522
523 /* If >= 0, computed, exact values of mode-line and header-line height
524 to use in the macros CURRENT_MODE_LINE_HEIGHT and
525 CURRENT_HEADER_LINE_HEIGHT. */
526
527 int current_mode_line_height, current_header_line_height;
528
529 /* The maximum distance to look ahead for text properties. Values
530 that are too small let us call compute_char_face and similar
531 functions too often which is expensive. Values that are too large
532 let us call compute_char_face and alike too often because we
533 might not be interested in text properties that far away. */
534
535 #define TEXT_PROP_DISTANCE_LIMIT 100
536
537 #if GLYPH_DEBUG
538
539 /* Non-zero means print traces of redisplay if compiled with
540 GLYPH_DEBUG != 0. */
541
542 int trace_redisplay_p;
543
544 #endif /* GLYPH_DEBUG */
545
546 #ifdef DEBUG_TRACE_MOVE
547 /* Non-zero means trace with TRACE_MOVE to stderr. */
548 int trace_move;
549
550 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
551 #else
552 #define TRACE_MOVE(x) (void) 0
553 #endif
554
555 /* Non-zero means automatically scroll windows horizontally to make
556 point visible. */
557
558 int automatic_hscrolling_p;
559
560 /* A list of symbols, one for each supported image type. */
561
562 Lisp_Object Vimage_types;
563
564 /* The variable `resize-mini-windows'. If nil, don't resize
565 mini-windows. If t, always resize them to fit the text they
566 display. If `grow-only', let mini-windows grow only until they
567 become empty. */
568
569 Lisp_Object Vresize_mini_windows;
570
571 /* Value returned from text property handlers (see below). */
572
573 enum prop_handled
574 {
575 HANDLED_NORMALLY,
576 HANDLED_RECOMPUTE_PROPS,
577 HANDLED_OVERLAY_STRING_CONSUMED,
578 HANDLED_RETURN
579 };
580
581 /* A description of text properties that redisplay is interested
582 in. */
583
584 struct props
585 {
586 /* The name of the property. */
587 Lisp_Object *name;
588
589 /* A unique index for the property. */
590 enum prop_idx idx;
591
592 /* A handler function called to set up iterator IT from the property
593 at IT's current position. Value is used to steer handle_stop. */
594 enum prop_handled (*handler) P_ ((struct it *it));
595 };
596
597 static enum prop_handled handle_face_prop P_ ((struct it *));
598 static enum prop_handled handle_invisible_prop P_ ((struct it *));
599 static enum prop_handled handle_display_prop P_ ((struct it *));
600 static enum prop_handled handle_composition_prop P_ ((struct it *));
601 static enum prop_handled handle_overlay_change P_ ((struct it *));
602 static enum prop_handled handle_fontified_prop P_ ((struct it *));
603
604 /* Properties handled by iterators. */
605
606 static struct props it_props[] =
607 {
608 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
609 /* Handle `face' before `display' because some sub-properties of
610 `display' need to know the face. */
611 {&Qface, FACE_PROP_IDX, handle_face_prop},
612 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
613 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
614 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
615 {NULL, 0, NULL}
616 };
617
618 /* Value is the position described by X. If X is a marker, value is
619 the marker_position of X. Otherwise, value is X. */
620
621 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
622
623 /* Enumeration returned by some move_it_.* functions internally. */
624
625 enum move_it_result
626 {
627 /* Not used. Undefined value. */
628 MOVE_UNDEFINED,
629
630 /* Move ended at the requested buffer position or ZV. */
631 MOVE_POS_MATCH_OR_ZV,
632
633 /* Move ended at the requested X pixel position. */
634 MOVE_X_REACHED,
635
636 /* Move within a line ended at the end of a line that must be
637 continued. */
638 MOVE_LINE_CONTINUED,
639
640 /* Move within a line ended at the end of a line that would
641 be displayed truncated. */
642 MOVE_LINE_TRUNCATED,
643
644 /* Move within a line ended at a line end. */
645 MOVE_NEWLINE_OR_CR
646 };
647
648
649 \f
650 /* Function prototypes. */
651
652 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
653 static int redisplay_mode_lines P_ ((Lisp_Object, int));
654 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
655 static int invisible_text_between_p P_ ((struct it *, int, int));
656 static int next_element_from_ellipsis P_ ((struct it *));
657 static void pint2str P_ ((char *, int, int));
658 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
659 struct text_pos));
660 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
661 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
662 static void store_frame_title_char P_ ((char));
663 static int store_frame_title P_ ((unsigned char *, int, int));
664 static void x_consider_frame_title P_ ((Lisp_Object));
665 static void handle_stop P_ ((struct it *));
666 static int tool_bar_lines_needed P_ ((struct frame *));
667 static int single_display_prop_intangible_p P_ ((Lisp_Object));
668 static void ensure_echo_area_buffers P_ ((void));
669 static struct glyph_row *row_containing_pos P_ ((struct window *, int,
670 struct glyph_row *,
671 struct glyph_row *));
672 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
673 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
674 static int with_echo_area_buffer P_ ((struct window *, int,
675 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
676 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
677 static void clear_garbaged_frames P_ ((void));
678 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
679 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
680 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
681 static int display_echo_area P_ ((struct window *));
682 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
683 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
684 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
685 static int string_char_and_length P_ ((unsigned char *, int, int *));
686 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
687 struct text_pos));
688 static int compute_window_start_on_continuation_line P_ ((struct window *));
689 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
690 static void insert_left_trunc_glyphs P_ ((struct it *));
691 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
692 static void extend_face_to_end_of_line P_ ((struct it *));
693 static int append_space P_ ((struct it *, int));
694 static void make_cursor_line_fully_visible P_ ((struct window *));
695 static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
696 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
697 static int trailing_whitespace_p P_ ((int));
698 static int message_log_check_duplicate P_ ((int, int, int, int));
699 int invisible_p P_ ((Lisp_Object, Lisp_Object));
700 int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
701 static void push_it P_ ((struct it *));
702 static void pop_it P_ ((struct it *));
703 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
704 static void redisplay_internal P_ ((int));
705 static int echo_area_display P_ ((int));
706 static void redisplay_windows P_ ((Lisp_Object));
707 static void redisplay_window P_ ((Lisp_Object, int));
708 static void update_menu_bar P_ ((struct frame *, int));
709 static int try_window_reusing_current_matrix P_ ((struct window *));
710 static int try_window_id P_ ((struct window *));
711 static int display_line P_ ((struct it *));
712 static int display_mode_lines P_ ((struct window *));
713 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
714 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
715 static char *decode_mode_spec P_ ((struct window *, int, int, int));
716 static void display_menu_bar P_ ((struct window *));
717 static int display_count_lines P_ ((int, int, int, int, int *));
718 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
719 int, int, struct it *, int, int, int, int));
720 static void compute_line_metrics P_ ((struct it *));
721 static void run_redisplay_end_trigger_hook P_ ((struct it *));
722 static int get_overlay_strings P_ ((struct it *));
723 static void next_overlay_string P_ ((struct it *));
724 static void reseat P_ ((struct it *, struct text_pos, int));
725 static void reseat_1 P_ ((struct it *, struct text_pos, int));
726 static void back_to_previous_visible_line_start P_ ((struct it *));
727 static void reseat_at_previous_visible_line_start P_ ((struct it *));
728 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
729 static int next_element_from_display_vector P_ ((struct it *));
730 static int next_element_from_string P_ ((struct it *));
731 static int next_element_from_c_string P_ ((struct it *));
732 static int next_element_from_buffer P_ ((struct it *));
733 static int next_element_from_composition P_ ((struct it *));
734 static int next_element_from_image P_ ((struct it *));
735 static int next_element_from_stretch P_ ((struct it *));
736 static void load_overlay_strings P_ ((struct it *));
737 static void init_from_display_pos P_ ((struct it *, struct window *,
738 struct display_pos *));
739 static void reseat_to_string P_ ((struct it *, unsigned char *,
740 Lisp_Object, int, int, int, int));
741 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
742 int, int, int));
743 void move_it_vertically_backward P_ ((struct it *, int));
744 static void init_to_row_start P_ ((struct it *, struct window *,
745 struct glyph_row *));
746 static void init_to_row_end P_ ((struct it *, struct window *,
747 struct glyph_row *));
748 static void back_to_previous_line_start P_ ((struct it *));
749 static int forward_to_next_line_start P_ ((struct it *, int *));
750 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
751 Lisp_Object, int));
752 static struct text_pos string_pos P_ ((int, Lisp_Object));
753 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
754 static int number_of_chars P_ ((unsigned char *, int));
755 static void compute_stop_pos P_ ((struct it *));
756 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
757 Lisp_Object));
758 static int face_before_or_after_it_pos P_ ((struct it *, int));
759 static int next_overlay_change P_ ((int));
760 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
761 Lisp_Object, struct text_pos *));
762 static int underlying_face_id P_ ((struct it *));
763
764 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
765 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
766
767 #ifdef HAVE_WINDOW_SYSTEM
768
769 static void update_tool_bar P_ ((struct frame *, int));
770 static void build_desired_tool_bar_string P_ ((struct frame *f));
771 static int redisplay_tool_bar P_ ((struct frame *));
772 static void display_tool_bar_line P_ ((struct it *));
773
774 #endif /* HAVE_WINDOW_SYSTEM */
775
776 \f
777 /***********************************************************************
778 Window display dimensions
779 ***********************************************************************/
780
781 /* Return the window-relative maximum y + 1 for glyph rows displaying
782 text in window W. This is the height of W minus the height of a
783 mode line, if any. */
784
785 INLINE int
786 window_text_bottom_y (w)
787 struct window *w;
788 {
789 struct frame *f = XFRAME (w->frame);
790 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
791
792 if (WINDOW_WANTS_MODELINE_P (w))
793 height -= CURRENT_MODE_LINE_HEIGHT (w);
794 return height;
795 }
796
797
798 /* Return the pixel width of display area AREA of window W. AREA < 0
799 means return the total width of W, not including bitmap areas to
800 the left and right of the window. */
801
802 INLINE int
803 window_box_width (w, area)
804 struct window *w;
805 int area;
806 {
807 struct frame *f = XFRAME (w->frame);
808 int width = XFASTINT (w->width);
809
810 if (!w->pseudo_window_p)
811 {
812 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
813
814 if (area == TEXT_AREA)
815 {
816 if (INTEGERP (w->left_margin_width))
817 width -= XFASTINT (w->left_margin_width);
818 if (INTEGERP (w->right_margin_width))
819 width -= XFASTINT (w->right_margin_width);
820 }
821 else if (area == LEFT_MARGIN_AREA)
822 width = (INTEGERP (w->left_margin_width)
823 ? XFASTINT (w->left_margin_width) : 0);
824 else if (area == RIGHT_MARGIN_AREA)
825 width = (INTEGERP (w->right_margin_width)
826 ? XFASTINT (w->right_margin_width) : 0);
827 }
828
829 return width * CANON_X_UNIT (f);
830 }
831
832
833 /* Return the pixel height of the display area of window W, not
834 including mode lines of W, if any.. */
835
836 INLINE int
837 window_box_height (w)
838 struct window *w;
839 {
840 struct frame *f = XFRAME (w->frame);
841 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
842
843 xassert (height >= 0);
844
845 if (WINDOW_WANTS_MODELINE_P (w))
846 height -= CURRENT_MODE_LINE_HEIGHT (w);
847
848 if (WINDOW_WANTS_HEADER_LINE_P (w))
849 height -= CURRENT_HEADER_LINE_HEIGHT (w);
850
851 return height;
852 }
853
854
855 /* Return the frame-relative coordinate of the left edge of display
856 area AREA of window W. AREA < 0 means return the left edge of the
857 whole window, to the right of any bitmap area at the left side of
858 W. */
859
860 INLINE int
861 window_box_left (w, area)
862 struct window *w;
863 int area;
864 {
865 struct frame *f = XFRAME (w->frame);
866 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
867
868 if (!w->pseudo_window_p)
869 {
870 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
871 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
872
873 if (area == TEXT_AREA)
874 x += window_box_width (w, LEFT_MARGIN_AREA);
875 else if (area == RIGHT_MARGIN_AREA)
876 x += (window_box_width (w, LEFT_MARGIN_AREA)
877 + window_box_width (w, TEXT_AREA));
878 }
879
880 return x;
881 }
882
883
884 /* Return the frame-relative coordinate of the right edge of display
885 area AREA of window W. AREA < 0 means return the left edge of the
886 whole window, to the left of any bitmap area at the right side of
887 W. */
888
889 INLINE int
890 window_box_right (w, area)
891 struct window *w;
892 int area;
893 {
894 return window_box_left (w, area) + window_box_width (w, area);
895 }
896
897
898 /* Get the bounding box of the display area AREA of window W, without
899 mode lines, in frame-relative coordinates. AREA < 0 means the
900 whole window, not including bitmap areas to the left and right of
901 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
902 coordinates of the upper-left corner of the box. Return in
903 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
904
905 INLINE void
906 window_box (w, area, box_x, box_y, box_width, box_height)
907 struct window *w;
908 int area;
909 int *box_x, *box_y, *box_width, *box_height;
910 {
911 struct frame *f = XFRAME (w->frame);
912
913 *box_width = window_box_width (w, area);
914 *box_height = window_box_height (w);
915 *box_x = window_box_left (w, area);
916 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
917 + XFASTINT (w->top) * CANON_Y_UNIT (f));
918 if (WINDOW_WANTS_HEADER_LINE_P (w))
919 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
920 }
921
922
923 /* Get the bounding box of the display area AREA of window W, without
924 mode lines. AREA < 0 means the whole window, not including bitmap
925 areas to the left and right of the window. Return in *TOP_LEFT_X
926 and TOP_LEFT_Y the frame-relative pixel coordinates of the
927 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
928 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
929 box. */
930
931 INLINE void
932 window_box_edges (w, area, top_left_x, top_left_y,
933 bottom_right_x, bottom_right_y)
934 struct window *w;
935 int area;
936 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
937 {
938 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
939 bottom_right_y);
940 *bottom_right_x += *top_left_x;
941 *bottom_right_y += *top_left_y;
942 }
943
944
945 \f
946 /***********************************************************************
947 Utilities
948 ***********************************************************************/
949
950 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
951 1 if POS is visible and the line containing POS is fully visible.
952 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
953 and header-lines heights. */
954
955 int
956 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
957 struct window *w;
958 int charpos, *fully, exact_mode_line_heights_p;
959 {
960 struct it it;
961 struct text_pos top;
962 int visible_p;
963 struct buffer *old_buffer = NULL;
964
965 if (XBUFFER (w->buffer) != current_buffer)
966 {
967 old_buffer = current_buffer;
968 set_buffer_internal_1 (XBUFFER (w->buffer));
969 }
970
971 *fully = visible_p = 0;
972 SET_TEXT_POS_FROM_MARKER (top, w->start);
973
974 /* Compute exact mode line heights, if requested. */
975 if (exact_mode_line_heights_p)
976 {
977 if (WINDOW_WANTS_MODELINE_P (w))
978 current_mode_line_height
979 = display_mode_line (w, MODE_LINE_FACE_ID,
980 current_buffer->mode_line_format);
981
982 if (WINDOW_WANTS_HEADER_LINE_P (w))
983 current_header_line_height
984 = display_mode_line (w, HEADER_LINE_FACE_ID,
985 current_buffer->header_line_format);
986 }
987
988 start_display (&it, w, top);
989 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
990 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
991
992 if (IT_CHARPOS (it) == charpos)
993 {
994 int line_height, line_bottom_y;
995 int line_top_y = it.current_y;
996 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
997
998 line_height = it.max_ascent + it.max_descent;
999 if (line_height == 0)
1000 {
1001 if (last_height)
1002 line_height = last_height;
1003 else if (charpos < ZV)
1004 {
1005 move_it_by_lines (&it, 1, 1);
1006 line_height = (it.max_ascent || it.max_descent
1007 ? it.max_ascent + it.max_descent
1008 : last_height);
1009 }
1010 else
1011 {
1012 /* Use the default character height. */
1013 it.what = IT_CHARACTER;
1014 it.c = ' ';
1015 it.len = 1;
1016 PRODUCE_GLYPHS (&it);
1017 line_height = it.ascent + it.descent;
1018 }
1019 }
1020 line_bottom_y = line_top_y + line_height;
1021
1022 if (line_top_y < window_top_y)
1023 visible_p = line_bottom_y > window_top_y;
1024 else if (line_top_y < it.last_visible_y)
1025 {
1026 visible_p = 1;
1027 *fully = line_bottom_y <= it.last_visible_y;
1028 }
1029 }
1030 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1031 {
1032 move_it_by_lines (&it, 1, 0);
1033 if (charpos < IT_CHARPOS (it))
1034 {
1035 visible_p = 1;
1036 *fully = 0;
1037 }
1038 }
1039
1040 if (old_buffer)
1041 set_buffer_internal_1 (old_buffer);
1042
1043 current_header_line_height = current_mode_line_height = -1;
1044 return visible_p;
1045 }
1046
1047
1048 /* Return the next character from STR which is MAXLEN bytes long.
1049 Return in *LEN the length of the character. This is like
1050 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1051 we find one, we return a `?', but with the length of the invalid
1052 character. */
1053
1054 static INLINE int
1055 string_char_and_length (str, maxlen, len)
1056 unsigned char *str;
1057 int maxlen, *len;
1058 {
1059 int c;
1060
1061 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1062 if (!CHAR_VALID_P (c, 1))
1063 /* We may not change the length here because other places in Emacs
1064 don't use this function, i.e. they silently accept invalid
1065 characters. */
1066 c = '?';
1067
1068 return c;
1069 }
1070
1071
1072
1073 /* Given a position POS containing a valid character and byte position
1074 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1075
1076 static struct text_pos
1077 string_pos_nchars_ahead (pos, string, nchars)
1078 struct text_pos pos;
1079 Lisp_Object string;
1080 int nchars;
1081 {
1082 xassert (STRINGP (string) && nchars >= 0);
1083
1084 if (STRING_MULTIBYTE (string))
1085 {
1086 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1087 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1088 int len;
1089
1090 while (nchars--)
1091 {
1092 string_char_and_length (p, rest, &len);
1093 p += len, rest -= len;
1094 xassert (rest >= 0);
1095 CHARPOS (pos) += 1;
1096 BYTEPOS (pos) += len;
1097 }
1098 }
1099 else
1100 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1101
1102 return pos;
1103 }
1104
1105
1106 /* Value is the text position, i.e. character and byte position,
1107 for character position CHARPOS in STRING. */
1108
1109 static INLINE struct text_pos
1110 string_pos (charpos, string)
1111 int charpos;
1112 Lisp_Object string;
1113 {
1114 struct text_pos pos;
1115 xassert (STRINGP (string));
1116 xassert (charpos >= 0);
1117 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1118 return pos;
1119 }
1120
1121
1122 /* Value is a text position, i.e. character and byte position, for
1123 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1124 means recognize multibyte characters. */
1125
1126 static struct text_pos
1127 c_string_pos (charpos, s, multibyte_p)
1128 int charpos;
1129 unsigned char *s;
1130 int multibyte_p;
1131 {
1132 struct text_pos pos;
1133
1134 xassert (s != NULL);
1135 xassert (charpos >= 0);
1136
1137 if (multibyte_p)
1138 {
1139 int rest = strlen (s), len;
1140
1141 SET_TEXT_POS (pos, 0, 0);
1142 while (charpos--)
1143 {
1144 string_char_and_length (s, rest, &len);
1145 s += len, rest -= len;
1146 xassert (rest >= 0);
1147 CHARPOS (pos) += 1;
1148 BYTEPOS (pos) += len;
1149 }
1150 }
1151 else
1152 SET_TEXT_POS (pos, charpos, charpos);
1153
1154 return pos;
1155 }
1156
1157
1158 /* Value is the number of characters in C string S. MULTIBYTE_P
1159 non-zero means recognize multibyte characters. */
1160
1161 static int
1162 number_of_chars (s, multibyte_p)
1163 unsigned char *s;
1164 int multibyte_p;
1165 {
1166 int nchars;
1167
1168 if (multibyte_p)
1169 {
1170 int rest = strlen (s), len;
1171 unsigned char *p = (unsigned char *) s;
1172
1173 for (nchars = 0; rest > 0; ++nchars)
1174 {
1175 string_char_and_length (p, rest, &len);
1176 rest -= len, p += len;
1177 }
1178 }
1179 else
1180 nchars = strlen (s);
1181
1182 return nchars;
1183 }
1184
1185
1186 /* Compute byte position NEWPOS->bytepos corresponding to
1187 NEWPOS->charpos. POS is a known position in string STRING.
1188 NEWPOS->charpos must be >= POS.charpos. */
1189
1190 static void
1191 compute_string_pos (newpos, pos, string)
1192 struct text_pos *newpos, pos;
1193 Lisp_Object string;
1194 {
1195 xassert (STRINGP (string));
1196 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1197
1198 if (STRING_MULTIBYTE (string))
1199 *newpos = string_pos_nchars_ahead (pos, string,
1200 CHARPOS (*newpos) - CHARPOS (pos));
1201 else
1202 BYTEPOS (*newpos) = CHARPOS (*newpos);
1203 }
1204
1205
1206 \f
1207 /***********************************************************************
1208 Lisp form evaluation
1209 ***********************************************************************/
1210
1211 /* Error handler for safe_eval and safe_call. */
1212
1213 static Lisp_Object
1214 safe_eval_handler (arg)
1215 Lisp_Object arg;
1216 {
1217 add_to_log ("Error during redisplay: %s", arg, Qnil);
1218 return Qnil;
1219 }
1220
1221
1222 /* Evaluate SEXPR and return the result, or nil if something went
1223 wrong. */
1224
1225 Lisp_Object
1226 safe_eval (sexpr)
1227 Lisp_Object sexpr;
1228 {
1229 int count = BINDING_STACK_SIZE ();
1230 struct gcpro gcpro1;
1231 Lisp_Object val;
1232
1233 GCPRO1 (sexpr);
1234 specbind (Qinhibit_redisplay, Qt);
1235 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
1236 UNGCPRO;
1237 return unbind_to (count, val);
1238 }
1239
1240
1241 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1242 Return the result, or nil if something went wrong. */
1243
1244 Lisp_Object
1245 safe_call (nargs, args)
1246 int nargs;
1247 Lisp_Object *args;
1248 {
1249 int count = BINDING_STACK_SIZE ();
1250 Lisp_Object val;
1251 struct gcpro gcpro1;
1252
1253 GCPRO1 (args[0]);
1254 gcpro1.nvars = nargs;
1255 specbind (Qinhibit_redisplay, Qt);
1256 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
1257 safe_eval_handler);
1258 UNGCPRO;
1259 return unbind_to (count, val);
1260 }
1261
1262
1263 /* Call function FN with one argument ARG.
1264 Return the result, or nil if something went wrong. */
1265
1266 Lisp_Object
1267 safe_call1 (fn, arg)
1268 Lisp_Object fn, arg;
1269 {
1270 Lisp_Object args[2];
1271 args[0] = fn;
1272 args[1] = arg;
1273 return safe_call (2, args);
1274 }
1275
1276
1277 \f
1278 /***********************************************************************
1279 Debugging
1280 ***********************************************************************/
1281
1282 #if 0
1283
1284 /* Define CHECK_IT to perform sanity checks on iterators.
1285 This is for debugging. It is too slow to do unconditionally. */
1286
1287 static void
1288 check_it (it)
1289 struct it *it;
1290 {
1291 if (it->method == next_element_from_string)
1292 {
1293 xassert (STRINGP (it->string));
1294 xassert (IT_STRING_CHARPOS (*it) >= 0);
1295 }
1296 else if (it->method == next_element_from_buffer)
1297 {
1298 /* Check that character and byte positions agree. */
1299 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1300 }
1301
1302 if (it->dpvec)
1303 xassert (it->current.dpvec_index >= 0);
1304 else
1305 xassert (it->current.dpvec_index < 0);
1306 }
1307
1308 #define CHECK_IT(IT) check_it ((IT))
1309
1310 #else /* not 0 */
1311
1312 #define CHECK_IT(IT) (void) 0
1313
1314 #endif /* not 0 */
1315
1316
1317 #if GLYPH_DEBUG
1318
1319 /* Check that the window end of window W is what we expect it
1320 to be---the last row in the current matrix displaying text. */
1321
1322 static void
1323 check_window_end (w)
1324 struct window *w;
1325 {
1326 if (!MINI_WINDOW_P (w)
1327 && !NILP (w->window_end_valid))
1328 {
1329 struct glyph_row *row;
1330 xassert ((row = MATRIX_ROW (w->current_matrix,
1331 XFASTINT (w->window_end_vpos)),
1332 !row->enabled_p
1333 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1334 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1335 }
1336 }
1337
1338 #define CHECK_WINDOW_END(W) check_window_end ((W))
1339
1340 #else /* not GLYPH_DEBUG */
1341
1342 #define CHECK_WINDOW_END(W) (void) 0
1343
1344 #endif /* not GLYPH_DEBUG */
1345
1346
1347 \f
1348 /***********************************************************************
1349 Iterator initialization
1350 ***********************************************************************/
1351
1352 /* Initialize IT for displaying current_buffer in window W, starting
1353 at character position CHARPOS. CHARPOS < 0 means that no buffer
1354 position is specified which is useful when the iterator is assigned
1355 a position later. BYTEPOS is the byte position corresponding to
1356 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1357
1358 If ROW is not null, calls to produce_glyphs with IT as parameter
1359 will produce glyphs in that row.
1360
1361 BASE_FACE_ID is the id of a base face to use. It must be one of
1362 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
1363 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
1364 displaying the tool-bar.
1365
1366 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
1367 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
1368 corresponding mode line glyph row of the desired matrix of W. */
1369
1370 void
1371 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1372 struct it *it;
1373 struct window *w;
1374 int charpos, bytepos;
1375 struct glyph_row *row;
1376 enum face_id base_face_id;
1377 {
1378 int highlight_region_p;
1379
1380 /* Some precondition checks. */
1381 xassert (w != NULL && it != NULL);
1382 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1383
1384 /* If face attributes have been changed since the last redisplay,
1385 free realized faces now because they depend on face definitions
1386 that might have changed. */
1387 if (face_change_count)
1388 {
1389 face_change_count = 0;
1390 free_all_realized_faces (Qnil);
1391 }
1392
1393 /* Use one of the mode line rows of W's desired matrix if
1394 appropriate. */
1395 if (row == NULL)
1396 {
1397 if (base_face_id == MODE_LINE_FACE_ID)
1398 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1399 else if (base_face_id == HEADER_LINE_FACE_ID)
1400 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1401 }
1402
1403 /* Clear IT. */
1404 bzero (it, sizeof *it);
1405 it->current.overlay_string_index = -1;
1406 it->current.dpvec_index = -1;
1407 it->base_face_id = base_face_id;
1408
1409 /* The window in which we iterate over current_buffer: */
1410 XSETWINDOW (it->window, w);
1411 it->w = w;
1412 it->f = XFRAME (w->frame);
1413
1414 /* Extra space between lines (on window systems only). */
1415 if (base_face_id == DEFAULT_FACE_ID
1416 && FRAME_WINDOW_P (it->f))
1417 {
1418 if (NATNUMP (current_buffer->extra_line_spacing))
1419 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1420 else if (it->f->extra_line_spacing > 0)
1421 it->extra_line_spacing = it->f->extra_line_spacing;
1422 }
1423
1424 /* If realized faces have been removed, e.g. because of face
1425 attribute changes of named faces, recompute them. */
1426 if (FRAME_FACE_CACHE (it->f)->used == 0)
1427 recompute_basic_faces (it->f);
1428
1429 /* Current value of the `space-width', and 'height' properties. */
1430 it->space_width = Qnil;
1431 it->font_height = Qnil;
1432
1433 /* Are control characters displayed as `^C'? */
1434 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1435
1436 /* -1 means everything between a CR and the following line end
1437 is invisible. >0 means lines indented more than this value are
1438 invisible. */
1439 it->selective = (INTEGERP (current_buffer->selective_display)
1440 ? XFASTINT (current_buffer->selective_display)
1441 : (!NILP (current_buffer->selective_display)
1442 ? -1 : 0));
1443 it->selective_display_ellipsis_p
1444 = !NILP (current_buffer->selective_display_ellipses);
1445
1446 /* Display table to use. */
1447 it->dp = window_display_table (w);
1448
1449 /* Are multibyte characters enabled in current_buffer? */
1450 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1451
1452 /* Non-zero if we should highlight the region. */
1453 highlight_region_p
1454 = (!NILP (Vtransient_mark_mode)
1455 && !NILP (current_buffer->mark_active)
1456 && XMARKER (current_buffer->mark)->buffer != 0);
1457
1458 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1459 start and end of a visible region in window IT->w. Set both to
1460 -1 to indicate no region. */
1461 if (highlight_region_p
1462 /* Maybe highlight only in selected window. */
1463 && (/* Either show region everywhere. */
1464 highlight_nonselected_windows
1465 /* Or show region in the selected window. */
1466 || w == XWINDOW (selected_window)
1467 /* Or show the region if we are in the mini-buffer and W is
1468 the window the mini-buffer refers to. */
1469 || (MINI_WINDOW_P (XWINDOW (selected_window))
1470 && w == XWINDOW (Vminibuf_scroll_window))))
1471 {
1472 int charpos = marker_position (current_buffer->mark);
1473 it->region_beg_charpos = min (PT, charpos);
1474 it->region_end_charpos = max (PT, charpos);
1475 }
1476 else
1477 it->region_beg_charpos = it->region_end_charpos = -1;
1478
1479 /* Get the position at which the redisplay_end_trigger hook should
1480 be run, if it is to be run at all. */
1481 if (MARKERP (w->redisplay_end_trigger)
1482 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1483 it->redisplay_end_trigger_charpos
1484 = marker_position (w->redisplay_end_trigger);
1485 else if (INTEGERP (w->redisplay_end_trigger))
1486 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1487
1488 /* Correct bogus values of tab_width. */
1489 it->tab_width = XINT (current_buffer->tab_width);
1490 if (it->tab_width <= 0 || it->tab_width > 1000)
1491 it->tab_width = 8;
1492
1493 /* Are lines in the display truncated? */
1494 it->truncate_lines_p
1495 = (base_face_id != DEFAULT_FACE_ID
1496 || XINT (it->w->hscroll)
1497 || (truncate_partial_width_windows
1498 && !WINDOW_FULL_WIDTH_P (it->w))
1499 || !NILP (current_buffer->truncate_lines));
1500
1501 /* Get dimensions of truncation and continuation glyphs. These are
1502 displayed as bitmaps under X, so we don't need them for such
1503 frames. */
1504 if (!FRAME_WINDOW_P (it->f))
1505 {
1506 if (it->truncate_lines_p)
1507 {
1508 /* We will need the truncation glyph. */
1509 xassert (it->glyph_row == NULL);
1510 produce_special_glyphs (it, IT_TRUNCATION);
1511 it->truncation_pixel_width = it->pixel_width;
1512 }
1513 else
1514 {
1515 /* We will need the continuation glyph. */
1516 xassert (it->glyph_row == NULL);
1517 produce_special_glyphs (it, IT_CONTINUATION);
1518 it->continuation_pixel_width = it->pixel_width;
1519 }
1520
1521 /* Reset these values to zero becaue the produce_special_glyphs
1522 above has changed them. */
1523 it->pixel_width = it->ascent = it->descent = 0;
1524 it->phys_ascent = it->phys_descent = 0;
1525 }
1526
1527 /* Set this after getting the dimensions of truncation and
1528 continuation glyphs, so that we don't produce glyphs when calling
1529 produce_special_glyphs, above. */
1530 it->glyph_row = row;
1531 it->area = TEXT_AREA;
1532
1533 /* Get the dimensions of the display area. The display area
1534 consists of the visible window area plus a horizontally scrolled
1535 part to the left of the window. All x-values are relative to the
1536 start of this total display area. */
1537 if (base_face_id != DEFAULT_FACE_ID)
1538 {
1539 /* Mode lines, menu bar in terminal frames. */
1540 it->first_visible_x = 0;
1541 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1542 }
1543 else
1544 {
1545 it->first_visible_x
1546 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1547 it->last_visible_x = (it->first_visible_x
1548 + window_box_width (w, TEXT_AREA));
1549
1550 /* If we truncate lines, leave room for the truncator glyph(s) at
1551 the right margin. Otherwise, leave room for the continuation
1552 glyph(s). Truncation and continuation glyphs are not inserted
1553 for window-based redisplay. */
1554 if (!FRAME_WINDOW_P (it->f))
1555 {
1556 if (it->truncate_lines_p)
1557 it->last_visible_x -= it->truncation_pixel_width;
1558 else
1559 it->last_visible_x -= it->continuation_pixel_width;
1560 }
1561
1562 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1563 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1564 }
1565
1566 /* Leave room for a border glyph. */
1567 if (!FRAME_WINDOW_P (it->f)
1568 && !WINDOW_RIGHTMOST_P (it->w))
1569 it->last_visible_x -= 1;
1570
1571 it->last_visible_y = window_text_bottom_y (w);
1572
1573 /* For mode lines and alike, arrange for the first glyph having a
1574 left box line if the face specifies a box. */
1575 if (base_face_id != DEFAULT_FACE_ID)
1576 {
1577 struct face *face;
1578
1579 it->face_id = base_face_id;
1580
1581 /* If we have a boxed mode line, make the first character appear
1582 with a left box line. */
1583 face = FACE_FROM_ID (it->f, base_face_id);
1584 if (face->box != FACE_NO_BOX)
1585 it->start_of_box_run_p = 1;
1586 }
1587
1588 /* If a buffer position was specified, set the iterator there,
1589 getting overlays and face properties from that position. */
1590 if (charpos > 0)
1591 {
1592 it->end_charpos = ZV;
1593 it->face_id = -1;
1594 IT_CHARPOS (*it) = charpos;
1595
1596 /* Compute byte position if not specified. */
1597 if (bytepos <= 0)
1598 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1599 else
1600 IT_BYTEPOS (*it) = bytepos;
1601
1602 /* Compute faces etc. */
1603 reseat (it, it->current.pos, 1);
1604 }
1605
1606 CHECK_IT (it);
1607 }
1608
1609
1610 /* Initialize IT for the display of window W with window start POS. */
1611
1612 void
1613 start_display (it, w, pos)
1614 struct it *it;
1615 struct window *w;
1616 struct text_pos pos;
1617 {
1618 int start_at_line_beg_p;
1619 struct glyph_row *row;
1620 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1621 int first_y;
1622
1623 row = w->desired_matrix->rows + first_vpos;
1624 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1625 first_y = it->current_y;
1626
1627 /* If window start is not at a line start, move back to the line
1628 start. This makes sure that we take continuation lines into
1629 account. */
1630 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1631 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1632 if (!start_at_line_beg_p)
1633 reseat_at_previous_visible_line_start (it);
1634
1635 /* If window start is not at a line start, skip forward to POS to
1636 get the correct continuation_lines_width and current_x. */
1637 if (!start_at_line_beg_p)
1638 {
1639 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1640
1641 /* If lines are continued, this line may end in the middle of a
1642 multi-glyph character (e.g. a control character displayed as
1643 \003, or in the middle of an overlay string). In this case
1644 move_it_to above will not have taken us to the start of
1645 the continuation line but to the end of the continued line. */
1646 if (!it->truncate_lines_p)
1647 {
1648 if (it->current_x > 0)
1649 {
1650 if (it->current.dpvec_index >= 0
1651 || it->current.overlay_string_index >= 0)
1652 {
1653 set_iterator_to_next (it, 1);
1654 move_it_in_display_line_to (it, -1, -1, 0);
1655 }
1656
1657 it->continuation_lines_width += it->current_x;
1658 }
1659
1660 /* We're starting a new display line, not affected by the
1661 height of the continued line, so clear the appropriate
1662 fields in the iterator structure. */
1663 it->max_ascent = it->max_descent = 0;
1664 it->max_phys_ascent = it->max_phys_descent = 0;
1665 }
1666
1667 it->current_y = first_y;
1668 it->vpos = 0;
1669 it->current_x = it->hpos = 0;
1670 }
1671
1672 #if 0 /* Don't assert the following because start_display is sometimes
1673 called intentionally with a window start that is not at a
1674 line start. Please leave this code in as a comment. */
1675
1676 /* Window start should be on a line start, now. */
1677 xassert (it->continuation_lines_width
1678 || IT_CHARPOS (it) == BEGV
1679 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1680 #endif /* 0 */
1681 }
1682
1683
1684 /* Initialize IT for stepping through current_buffer in window W,
1685 starting at position POS that includes overlay string and display
1686 vector/ control character translation position information. */
1687
1688 static void
1689 init_from_display_pos (it, w, pos)
1690 struct it *it;
1691 struct window *w;
1692 struct display_pos *pos;
1693 {
1694 /* Keep in mind: the call to reseat in init_iterator skips invisible
1695 text, so we might end up at a position different from POS. This
1696 is only a problem when POS is a row start after a newline and an
1697 overlay starts there with an after-string, and the overlay has an
1698 invisible property. Since we don't skip invisible text in
1699 display_line and elsewhere immediately after consuming the
1700 newline before the row start, such a POS will not be in a string,
1701 but the call to init_iterator below will move us to the
1702 after-string. */
1703 init_iterator (it, w, CHARPOS (pos->pos), BYTEPOS (pos->pos),
1704 NULL, DEFAULT_FACE_ID);
1705
1706 /* If position is within an overlay string, set up IT to
1707 the right overlay string. */
1708 if (pos->overlay_string_index >= 0)
1709 {
1710 int relative_index;
1711
1712 /* We already have the first chunk of overlay strings in
1713 IT->overlay_strings. Load more until the one for
1714 pos->overlay_string_index is in IT->overlay_strings. */
1715 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1716 {
1717 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1718 it->current.overlay_string_index = 0;
1719 while (n--)
1720 {
1721 load_overlay_strings (it);
1722 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1723 }
1724 }
1725
1726 it->current.overlay_string_index = pos->overlay_string_index;
1727 relative_index = (it->current.overlay_string_index
1728 % OVERLAY_STRING_CHUNK_SIZE);
1729 it->string = it->overlay_strings[relative_index];
1730 xassert (STRINGP (it->string));
1731 it->current.string_pos = pos->string_pos;
1732 it->method = next_element_from_string;
1733 }
1734 else if (it->current.overlay_string_index >= 0)
1735 {
1736 /* If POS says we're already after an overlay string ending at
1737 POS, make sure to pop the iterator because it will be in
1738 front of that overlay string. When POS is ZV, we've thereby
1739 also ``processed'' overlay strings at ZV. */
1740 pop_it (it);
1741 it->current.overlay_string_index = -1;
1742 it->method = next_element_from_buffer;
1743 if (CHARPOS (pos->pos) == ZV)
1744 it->overlay_strings_at_end_processed_p = 1;
1745 }
1746
1747 if (CHARPOS (pos->string_pos) >= 0)
1748 {
1749 /* Recorded position is not in an overlay string, but in another
1750 string. This can only be a string from a `display' property.
1751 IT should already be filled with that string. */
1752 it->current.string_pos = pos->string_pos;
1753 xassert (STRINGP (it->string));
1754 }
1755
1756 /* Restore position in display vector translations or control
1757 character translations. */
1758 if (pos->dpvec_index >= 0)
1759 {
1760 /* This fills IT->dpvec. */
1761 get_next_display_element (it);
1762 xassert (it->dpvec && it->current.dpvec_index == 0);
1763 it->current.dpvec_index = pos->dpvec_index;
1764 }
1765
1766 CHECK_IT (it);
1767 }
1768
1769
1770 /* Initialize IT for stepping through current_buffer in window W
1771 starting at ROW->start. */
1772
1773 static void
1774 init_to_row_start (it, w, row)
1775 struct it *it;
1776 struct window *w;
1777 struct glyph_row *row;
1778 {
1779 init_from_display_pos (it, w, &row->start);
1780 it->continuation_lines_width = row->continuation_lines_width;
1781 CHECK_IT (it);
1782 }
1783
1784
1785 /* Initialize IT for stepping through current_buffer in window W
1786 starting in the line following ROW, i.e. starting at ROW->end. */
1787
1788 static void
1789 init_to_row_end (it, w, row)
1790 struct it *it;
1791 struct window *w;
1792 struct glyph_row *row;
1793 {
1794 init_from_display_pos (it, w, &row->end);
1795
1796 if (row->continued_p)
1797 it->continuation_lines_width = (row->continuation_lines_width
1798 + row->pixel_width);
1799 CHECK_IT (it);
1800 }
1801
1802
1803
1804 \f
1805 /***********************************************************************
1806 Text properties
1807 ***********************************************************************/
1808
1809 /* Called when IT reaches IT->stop_charpos. Handle text property and
1810 overlay changes. Set IT->stop_charpos to the next position where
1811 to stop. */
1812
1813 static void
1814 handle_stop (it)
1815 struct it *it;
1816 {
1817 enum prop_handled handled;
1818 int handle_overlay_change_p = 1;
1819 struct props *p;
1820
1821 it->dpvec = NULL;
1822 it->current.dpvec_index = -1;
1823
1824 do
1825 {
1826 handled = HANDLED_NORMALLY;
1827
1828 /* Call text property handlers. */
1829 for (p = it_props; p->handler; ++p)
1830 {
1831 handled = p->handler (it);
1832
1833 if (handled == HANDLED_RECOMPUTE_PROPS)
1834 break;
1835 else if (handled == HANDLED_RETURN)
1836 return;
1837 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1838 handle_overlay_change_p = 0;
1839 }
1840
1841 if (handled != HANDLED_RECOMPUTE_PROPS)
1842 {
1843 /* Don't check for overlay strings below when set to deliver
1844 characters from a display vector. */
1845 if (it->method == next_element_from_display_vector)
1846 handle_overlay_change_p = 0;
1847
1848 /* Handle overlay changes. */
1849 if (handle_overlay_change_p)
1850 handled = handle_overlay_change (it);
1851
1852 /* Determine where to stop next. */
1853 if (handled == HANDLED_NORMALLY)
1854 compute_stop_pos (it);
1855 }
1856 }
1857 while (handled == HANDLED_RECOMPUTE_PROPS);
1858 }
1859
1860
1861 /* Compute IT->stop_charpos from text property and overlay change
1862 information for IT's current position. */
1863
1864 static void
1865 compute_stop_pos (it)
1866 struct it *it;
1867 {
1868 register INTERVAL iv, next_iv;
1869 Lisp_Object object, limit, position;
1870
1871 /* If nowhere else, stop at the end. */
1872 it->stop_charpos = it->end_charpos;
1873
1874 if (STRINGP (it->string))
1875 {
1876 /* Strings are usually short, so don't limit the search for
1877 properties. */
1878 object = it->string;
1879 limit = Qnil;
1880 XSETFASTINT (position, IT_STRING_CHARPOS (*it));
1881 }
1882 else
1883 {
1884 int charpos;
1885
1886 /* If next overlay change is in front of the current stop pos
1887 (which is IT->end_charpos), stop there. Note: value of
1888 next_overlay_change is point-max if no overlay change
1889 follows. */
1890 charpos = next_overlay_change (IT_CHARPOS (*it));
1891 if (charpos < it->stop_charpos)
1892 it->stop_charpos = charpos;
1893
1894 /* If showing the region, we have to stop at the region
1895 start or end because the face might change there. */
1896 if (it->region_beg_charpos > 0)
1897 {
1898 if (IT_CHARPOS (*it) < it->region_beg_charpos)
1899 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
1900 else if (IT_CHARPOS (*it) < it->region_end_charpos)
1901 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
1902 }
1903
1904 /* Set up variables for computing the stop position from text
1905 property changes. */
1906 XSETBUFFER (object, current_buffer);
1907 XSETFASTINT (limit, IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
1908 XSETFASTINT (position, IT_CHARPOS (*it));
1909
1910 }
1911
1912 /* Get the interval containing IT's position. Value is a null
1913 interval if there isn't such an interval. */
1914 iv = validate_interval_range (object, &position, &position, 0);
1915 if (!NULL_INTERVAL_P (iv))
1916 {
1917 Lisp_Object values_here[LAST_PROP_IDX];
1918 struct props *p;
1919
1920 /* Get properties here. */
1921 for (p = it_props; p->handler; ++p)
1922 values_here[p->idx] = textget (iv->plist, *p->name);
1923
1924 /* Look for an interval following iv that has different
1925 properties. */
1926 for (next_iv = next_interval (iv);
1927 (!NULL_INTERVAL_P (next_iv)
1928 && (NILP (limit)
1929 || XFASTINT (limit) > next_iv->position));
1930 next_iv = next_interval (next_iv))
1931 {
1932 for (p = it_props; p->handler; ++p)
1933 {
1934 Lisp_Object new_value;
1935
1936 new_value = textget (next_iv->plist, *p->name);
1937 if (!EQ (values_here[p->idx], new_value))
1938 break;
1939 }
1940
1941 if (p->handler)
1942 break;
1943 }
1944
1945 if (!NULL_INTERVAL_P (next_iv))
1946 {
1947 if (INTEGERP (limit)
1948 && next_iv->position >= XFASTINT (limit))
1949 /* No text property change up to limit. */
1950 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
1951 else
1952 /* Text properties change in next_iv. */
1953 it->stop_charpos = min (it->stop_charpos, next_iv->position);
1954 }
1955 }
1956
1957 xassert (STRINGP (it->string)
1958 || (it->stop_charpos >= BEGV
1959 && it->stop_charpos >= IT_CHARPOS (*it)));
1960 }
1961
1962
1963 /* Return the position of the next overlay change after POS in
1964 current_buffer. Value is point-max if no overlay change
1965 follows. This is like `next-overlay-change' but doesn't use
1966 xmalloc. */
1967
1968 static int
1969 next_overlay_change (pos)
1970 int pos;
1971 {
1972 int noverlays;
1973 int endpos;
1974 Lisp_Object *overlays;
1975 int len;
1976 int i;
1977
1978 /* Get all overlays at the given position. */
1979 len = 10;
1980 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1981 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1982 if (noverlays > len)
1983 {
1984 len = noverlays;
1985 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
1986 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
1987 }
1988
1989 /* If any of these overlays ends before endpos,
1990 use its ending point instead. */
1991 for (i = 0; i < noverlays; ++i)
1992 {
1993 Lisp_Object oend;
1994 int oendpos;
1995
1996 oend = OVERLAY_END (overlays[i]);
1997 oendpos = OVERLAY_POSITION (oend);
1998 endpos = min (endpos, oendpos);
1999 }
2000
2001 return endpos;
2002 }
2003
2004
2005 \f
2006 /***********************************************************************
2007 Fontification
2008 ***********************************************************************/
2009
2010 /* Handle changes in the `fontified' property of the current buffer by
2011 calling hook functions from Qfontification_functions to fontify
2012 regions of text. */
2013
2014 static enum prop_handled
2015 handle_fontified_prop (it)
2016 struct it *it;
2017 {
2018 Lisp_Object prop, pos;
2019 enum prop_handled handled = HANDLED_NORMALLY;
2020
2021 /* Get the value of the `fontified' property at IT's current buffer
2022 position. (The `fontified' property doesn't have a special
2023 meaning in strings.) If the value is nil, call functions from
2024 Qfontification_functions. */
2025 if (!STRINGP (it->string)
2026 && it->s == NULL
2027 && !NILP (Vfontification_functions)
2028 && !NILP (Vrun_hooks)
2029 && (pos = make_number (IT_CHARPOS (*it)),
2030 prop = Fget_char_property (pos, Qfontified, Qnil),
2031 NILP (prop)))
2032 {
2033 int count = BINDING_STACK_SIZE ();
2034 Lisp_Object val;
2035
2036 val = Vfontification_functions;
2037 specbind (Qfontification_functions, Qnil);
2038 specbind (Qafter_change_functions, Qnil);
2039
2040 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2041 safe_call1 (val, pos);
2042 else
2043 {
2044 Lisp_Object globals, fn;
2045 struct gcpro gcpro1, gcpro2;
2046
2047 globals = Qnil;
2048 GCPRO2 (val, globals);
2049
2050 for (; CONSP (val); val = XCDR (val))
2051 {
2052 fn = XCAR (val);
2053
2054 if (EQ (fn, Qt))
2055 {
2056 /* A value of t indicates this hook has a local
2057 binding; it means to run the global binding too.
2058 In a global value, t should not occur. If it
2059 does, we must ignore it to avoid an endless
2060 loop. */
2061 for (globals = Fdefault_value (Qfontification_functions);
2062 CONSP (globals);
2063 globals = XCDR (globals))
2064 {
2065 fn = XCAR (globals);
2066 if (!EQ (fn, Qt))
2067 safe_call1 (fn, pos);
2068 }
2069 }
2070 else
2071 safe_call1 (fn, pos);
2072 }
2073
2074 UNGCPRO;
2075 }
2076
2077 unbind_to (count, Qnil);
2078
2079 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2080 something. This avoids an endless loop if they failed to
2081 fontify the text for which reason ever. */
2082 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2083 handled = HANDLED_RECOMPUTE_PROPS;
2084 }
2085
2086 return handled;
2087 }
2088
2089
2090 \f
2091 /***********************************************************************
2092 Faces
2093 ***********************************************************************/
2094
2095 /* Set up iterator IT from face properties at its current position.
2096 Called from handle_stop. */
2097
2098 static enum prop_handled
2099 handle_face_prop (it)
2100 struct it *it;
2101 {
2102 int new_face_id, next_stop;
2103
2104 if (!STRINGP (it->string))
2105 {
2106 new_face_id
2107 = face_at_buffer_position (it->w,
2108 IT_CHARPOS (*it),
2109 it->region_beg_charpos,
2110 it->region_end_charpos,
2111 &next_stop,
2112 (IT_CHARPOS (*it)
2113 + TEXT_PROP_DISTANCE_LIMIT),
2114 0);
2115
2116 /* Is this a start of a run of characters with box face?
2117 Caveat: this can be called for a freshly initialized
2118 iterator; face_id is -1 is this case. We know that the new
2119 face will not change until limit, i.e. if the new face has a
2120 box, all characters up to limit will have one. But, as
2121 usual, we don't know whether limit is really the end. */
2122 if (new_face_id != it->face_id)
2123 {
2124 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2125
2126 /* If new face has a box but old face has not, this is
2127 the start of a run of characters with box, i.e. it has
2128 a shadow on the left side. The value of face_id of the
2129 iterator will be -1 if this is the initial call that gets
2130 the face. In this case, we have to look in front of IT's
2131 position and see whether there is a face != new_face_id. */
2132 it->start_of_box_run_p
2133 = (new_face->box != FACE_NO_BOX
2134 && (it->face_id >= 0
2135 || IT_CHARPOS (*it) == BEG
2136 || new_face_id != face_before_it_pos (it)));
2137 it->face_box_p = new_face->box != FACE_NO_BOX;
2138 }
2139 }
2140 else
2141 {
2142 int base_face_id, bufpos;
2143
2144 if (it->current.overlay_string_index >= 0)
2145 bufpos = IT_CHARPOS (*it);
2146 else
2147 bufpos = 0;
2148
2149 /* For strings from a buffer, i.e. overlay strings or strings
2150 from a `display' property, use the face at IT's current
2151 buffer position as the base face to merge with, so that
2152 overlay strings appear in the same face as surrounding
2153 text, unless they specify their own faces. */
2154 base_face_id = underlying_face_id (it);
2155
2156 new_face_id = face_at_string_position (it->w,
2157 it->string,
2158 IT_STRING_CHARPOS (*it),
2159 bufpos,
2160 it->region_beg_charpos,
2161 it->region_end_charpos,
2162 &next_stop,
2163 base_face_id);
2164
2165 #if 0 /* This shouldn't be neccessary. Let's check it. */
2166 /* If IT is used to display a mode line we would really like to
2167 use the mode line face instead of the frame's default face. */
2168 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2169 && new_face_id == DEFAULT_FACE_ID)
2170 new_face_id = MODE_LINE_FACE_ID;
2171 #endif
2172
2173 /* Is this a start of a run of characters with box? Caveat:
2174 this can be called for a freshly allocated iterator; face_id
2175 is -1 is this case. We know that the new face will not
2176 change until the next check pos, i.e. if the new face has a
2177 box, all characters up to that position will have a
2178 box. But, as usual, we don't know whether that position
2179 is really the end. */
2180 if (new_face_id != it->face_id)
2181 {
2182 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2183 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2184
2185 /* If new face has a box but old face hasn't, this is the
2186 start of a run of characters with box, i.e. it has a
2187 shadow on the left side. */
2188 it->start_of_box_run_p
2189 = new_face->box && (old_face == NULL || !old_face->box);
2190 it->face_box_p = new_face->box != FACE_NO_BOX;
2191 }
2192 }
2193
2194 it->face_id = new_face_id;
2195 return HANDLED_NORMALLY;
2196 }
2197
2198
2199 /* Return the ID of the face ``underlying'' IT's current position,
2200 which is in a string. If the iterator is associated with a
2201 buffer, return the face at IT's current buffer position.
2202 Otherwise, use the iterator's base_face_id. */
2203
2204 static int
2205 underlying_face_id (it)
2206 struct it *it;
2207 {
2208 int face_id = it->base_face_id, i;
2209
2210 xassert (STRINGP (it->string));
2211
2212 for (i = it->sp - 1; i >= 0; --i)
2213 if (NILP (it->stack[i].string))
2214 face_id = it->stack[i].face_id;
2215
2216 return face_id;
2217 }
2218
2219
2220 /* Compute the face one character before or after the current position
2221 of IT. BEFORE_P non-zero means get the face in front of IT's
2222 position. Value is the id of the face. */
2223
2224 static int
2225 face_before_or_after_it_pos (it, before_p)
2226 struct it *it;
2227 int before_p;
2228 {
2229 int face_id, limit;
2230 int next_check_charpos;
2231 struct text_pos pos;
2232
2233 xassert (it->s == NULL);
2234
2235 if (STRINGP (it->string))
2236 {
2237 int bufpos, base_face_id;
2238
2239 /* No face change past the end of the string (for the case
2240 we are padding with spaces). No face change before the
2241 string start. */
2242 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2243 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2244 return it->face_id;
2245
2246 /* Set pos to the position before or after IT's current position. */
2247 if (before_p)
2248 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2249 else
2250 /* For composition, we must check the character after the
2251 composition. */
2252 pos = (it->what == IT_COMPOSITION
2253 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2254 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2255
2256 if (it->current.overlay_string_index >= 0)
2257 bufpos = IT_CHARPOS (*it);
2258 else
2259 bufpos = 0;
2260
2261 base_face_id = underlying_face_id (it);
2262
2263 /* Get the face for ASCII, or unibyte. */
2264 face_id = face_at_string_position (it->w,
2265 it->string,
2266 CHARPOS (pos),
2267 bufpos,
2268 it->region_beg_charpos,
2269 it->region_end_charpos,
2270 &next_check_charpos,
2271 base_face_id);
2272
2273 /* Correct the face for charsets different from ASCII. Do it
2274 for the multibyte case only. The face returned above is
2275 suitable for unibyte text if IT->string is unibyte. */
2276 if (STRING_MULTIBYTE (it->string))
2277 {
2278 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2279 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
2280 int c, len;
2281 struct face *face = FACE_FROM_ID (it->f, face_id);
2282
2283 c = string_char_and_length (p, rest, &len);
2284 face_id = FACE_FOR_CHAR (it->f, face, c);
2285 }
2286 }
2287 else
2288 {
2289 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2290 || (IT_CHARPOS (*it) <= BEGV && before_p))
2291 return it->face_id;
2292
2293 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2294 pos = it->current.pos;
2295
2296 if (before_p)
2297 DEC_TEXT_POS (pos, it->multibyte_p);
2298 else
2299 {
2300 if (it->what == IT_COMPOSITION)
2301 /* For composition, we must check the position after the
2302 composition. */
2303 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2304 else
2305 INC_TEXT_POS (pos, it->multibyte_p);
2306 }
2307
2308 /* Determine face for CHARSET_ASCII, or unibyte. */
2309 face_id = face_at_buffer_position (it->w,
2310 CHARPOS (pos),
2311 it->region_beg_charpos,
2312 it->region_end_charpos,
2313 &next_check_charpos,
2314 limit, 0);
2315
2316 /* Correct the face for charsets different from ASCII. Do it
2317 for the multibyte case only. The face returned above is
2318 suitable for unibyte text if current_buffer is unibyte. */
2319 if (it->multibyte_p)
2320 {
2321 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2322 struct face *face = FACE_FROM_ID (it->f, face_id);
2323 face_id = FACE_FOR_CHAR (it->f, face, c);
2324 }
2325 }
2326
2327 return face_id;
2328 }
2329
2330
2331 \f
2332 /***********************************************************************
2333 Invisible text
2334 ***********************************************************************/
2335
2336 /* Set up iterator IT from invisible properties at its current
2337 position. Called from handle_stop. */
2338
2339 static enum prop_handled
2340 handle_invisible_prop (it)
2341 struct it *it;
2342 {
2343 enum prop_handled handled = HANDLED_NORMALLY;
2344
2345 if (STRINGP (it->string))
2346 {
2347 extern Lisp_Object Qinvisible;
2348 Lisp_Object prop, end_charpos, limit, charpos;
2349
2350 /* Get the value of the invisible text property at the
2351 current position. Value will be nil if there is no such
2352 property. */
2353 XSETFASTINT (charpos, IT_STRING_CHARPOS (*it));
2354 prop = Fget_text_property (charpos, Qinvisible, it->string);
2355
2356 if (!NILP (prop)
2357 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2358 {
2359 handled = HANDLED_RECOMPUTE_PROPS;
2360
2361 /* Get the position at which the next change of the
2362 invisible text property can be found in IT->string.
2363 Value will be nil if the property value is the same for
2364 all the rest of IT->string. */
2365 XSETINT (limit, XSTRING (it->string)->size);
2366 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2367 it->string, limit);
2368
2369 /* Text at current position is invisible. The next
2370 change in the property is at position end_charpos.
2371 Move IT's current position to that position. */
2372 if (INTEGERP (end_charpos)
2373 && XFASTINT (end_charpos) < XFASTINT (limit))
2374 {
2375 struct text_pos old;
2376 old = it->current.string_pos;
2377 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2378 compute_string_pos (&it->current.string_pos, old, it->string);
2379 }
2380 else
2381 {
2382 /* The rest of the string is invisible. If this is an
2383 overlay string, proceed with the next overlay string
2384 or whatever comes and return a character from there. */
2385 if (it->current.overlay_string_index >= 0)
2386 {
2387 next_overlay_string (it);
2388 /* Don't check for overlay strings when we just
2389 finished processing them. */
2390 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2391 }
2392 else
2393 {
2394 struct Lisp_String *s = XSTRING (it->string);
2395 IT_STRING_CHARPOS (*it) = s->size;
2396 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2397 }
2398 }
2399 }
2400 }
2401 else
2402 {
2403 int visible_p, newpos, next_stop;
2404 Lisp_Object pos, prop;
2405
2406 /* First of all, is there invisible text at this position? */
2407 XSETFASTINT (pos, IT_CHARPOS (*it));
2408 prop = Fget_char_property (pos, Qinvisible, it->window);
2409
2410 /* If we are on invisible text, skip over it. */
2411 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2412 && IT_CHARPOS (*it) < it->end_charpos)
2413 {
2414 /* Record whether we have to display an ellipsis for the
2415 invisible text. */
2416 int display_ellipsis_p
2417 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2418
2419 handled = HANDLED_RECOMPUTE_PROPS;
2420
2421 /* Loop skipping over invisible text. The loop is left at
2422 ZV or with IT on the first char being visible again. */
2423 do
2424 {
2425 /* Try to skip some invisible text. Return value is the
2426 position reached which can be equal to IT's position
2427 if there is nothing invisible here. This skips both
2428 over invisible text properties and overlays with
2429 invisible property. */
2430 newpos = skip_invisible (IT_CHARPOS (*it),
2431 &next_stop, ZV, it->window);
2432
2433 /* If we skipped nothing at all we weren't at invisible
2434 text in the first place. If everything to the end of
2435 the buffer was skipped, end the loop. */
2436 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2437 visible_p = 1;
2438 else
2439 {
2440 /* We skipped some characters but not necessarily
2441 all there are. Check if we ended up on visible
2442 text. Fget_char_property returns the property of
2443 the char before the given position, i.e. if we
2444 get visible_p = 1, this means that the char at
2445 newpos is visible. */
2446 XSETFASTINT (pos, newpos);
2447 prop = Fget_char_property (pos, Qinvisible, it->window);
2448 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2449 }
2450
2451 /* If we ended up on invisible text, proceed to
2452 skip starting with next_stop. */
2453 if (!visible_p)
2454 IT_CHARPOS (*it) = next_stop;
2455 }
2456 while (!visible_p);
2457
2458 /* The position newpos is now either ZV or on visible text. */
2459 IT_CHARPOS (*it) = newpos;
2460 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2461
2462 /* Maybe return `...' next for the end of the invisible text. */
2463 if (display_ellipsis_p)
2464 {
2465 if (it->dp
2466 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2467 {
2468 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2469 it->dpvec = v->contents;
2470 it->dpend = v->contents + v->size;
2471 }
2472 else
2473 {
2474 /* Default `...'. */
2475 it->dpvec = default_invis_vector;
2476 it->dpend = default_invis_vector + 3;
2477 }
2478
2479 /* The ellipsis display does not replace the display of
2480 the character at the new position. Indicate this by
2481 setting IT->dpvec_char_len to zero. */
2482 it->dpvec_char_len = 0;
2483
2484 it->current.dpvec_index = 0;
2485 it->method = next_element_from_display_vector;
2486 }
2487 }
2488 }
2489
2490 return handled;
2491 }
2492
2493
2494 \f
2495 /***********************************************************************
2496 'display' property
2497 ***********************************************************************/
2498
2499 /* Set up iterator IT from `display' property at its current position.
2500 Called from handle_stop. */
2501
2502 static enum prop_handled
2503 handle_display_prop (it)
2504 struct it *it;
2505 {
2506 Lisp_Object prop, object;
2507 struct text_pos *position;
2508 int space_or_image_found_p;
2509
2510 if (STRINGP (it->string))
2511 {
2512 object = it->string;
2513 position = &it->current.string_pos;
2514 }
2515 else
2516 {
2517 object = Qnil;
2518 position = &it->current.pos;
2519 }
2520
2521 /* Reset those iterator values set from display property values. */
2522 it->font_height = Qnil;
2523 it->space_width = Qnil;
2524 it->voffset = 0;
2525
2526 /* We don't support recursive `display' properties, i.e. string
2527 values that have a string `display' property, that have a string
2528 `display' property etc. */
2529 if (!it->string_from_display_prop_p)
2530 it->area = TEXT_AREA;
2531
2532 prop = Fget_char_property (make_number (position->charpos),
2533 Qdisplay, object);
2534 if (NILP (prop))
2535 return HANDLED_NORMALLY;
2536
2537 space_or_image_found_p = 0;
2538 if (CONSP (prop)
2539 && CONSP (XCAR (prop))
2540 && !EQ (Qmargin, XCAR (XCAR (prop))))
2541 {
2542 /* A list of sub-properties. */
2543 while (CONSP (prop))
2544 {
2545 if (handle_single_display_prop (it, XCAR (prop), object, position))
2546 space_or_image_found_p = 1;
2547 prop = XCDR (prop);
2548 }
2549 }
2550 else if (VECTORP (prop))
2551 {
2552 int i;
2553 for (i = 0; i < XVECTOR (prop)->size; ++i)
2554 if (handle_single_display_prop (it, XVECTOR (prop)->contents[i],
2555 object, position))
2556 space_or_image_found_p = 1;
2557 }
2558 else
2559 {
2560 if (handle_single_display_prop (it, prop, object, position))
2561 space_or_image_found_p = 1;
2562 }
2563
2564 return space_or_image_found_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2565 }
2566
2567
2568 /* Value is the position of the end of the `display' property starting
2569 at START_POS in OBJECT. */
2570
2571 static struct text_pos
2572 display_prop_end (it, object, start_pos)
2573 struct it *it;
2574 Lisp_Object object;
2575 struct text_pos start_pos;
2576 {
2577 Lisp_Object end;
2578 struct text_pos end_pos;
2579
2580 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2581 Qdisplay, object, Qnil);
2582 CHARPOS (end_pos) = XFASTINT (end);
2583 if (STRINGP (object))
2584 compute_string_pos (&end_pos, start_pos, it->string);
2585 else
2586 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2587
2588 return end_pos;
2589 }
2590
2591
2592 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2593 is the object in which the `display' property was found. *POSITION
2594 is the position at which it was found.
2595
2596 If PROP is a `space' or `image' sub-property, set *POSITION to the
2597 end position of the `display' property.
2598
2599 Value is non-zero if a `space' or `image' property value was found. */
2600
2601 static int
2602 handle_single_display_prop (it, prop, object, position)
2603 struct it *it;
2604 Lisp_Object prop;
2605 Lisp_Object object;
2606 struct text_pos *position;
2607 {
2608 Lisp_Object value;
2609 int space_or_image_found_p = 0;
2610 Lisp_Object form;
2611
2612 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2613 evaluated. If the result is nil, VALUE is ignored. */
2614 form = Qt;
2615 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2616 {
2617 prop = XCDR (prop);
2618 if (!CONSP (prop))
2619 return 0;
2620 form = XCAR (prop);
2621 prop = XCDR (prop);
2622 }
2623
2624 if (!NILP (form) && !EQ (form, Qt))
2625 {
2626 struct gcpro gcpro1;
2627 struct text_pos end_pos, pt;
2628
2629 GCPRO1 (form);
2630 end_pos = display_prop_end (it, object, *position);
2631
2632 /* Temporarily set point to the end position, and then evaluate
2633 the form. This makes `(eolp)' work as FORM. */
2634 if (BUFFERP (object))
2635 {
2636 CHARPOS (pt) = PT;
2637 BYTEPOS (pt) = PT_BYTE;
2638 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2639 }
2640
2641 form = safe_eval (form);
2642
2643 if (BUFFERP (object))
2644 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
2645 UNGCPRO;
2646 }
2647
2648 if (NILP (form))
2649 return 0;
2650
2651 if (CONSP (prop)
2652 && EQ (XCAR (prop), Qheight)
2653 && CONSP (XCDR (prop)))
2654 {
2655 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2656 return 0;
2657
2658 /* `(height HEIGHT)'. */
2659 it->font_height = XCAR (XCDR (prop));
2660 if (!NILP (it->font_height))
2661 {
2662 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2663 int new_height = -1;
2664
2665 if (CONSP (it->font_height)
2666 && (EQ (XCAR (it->font_height), Qplus)
2667 || EQ (XCAR (it->font_height), Qminus))
2668 && CONSP (XCDR (it->font_height))
2669 && INTEGERP (XCAR (XCDR (it->font_height))))
2670 {
2671 /* `(+ N)' or `(- N)' where N is an integer. */
2672 int steps = XINT (XCAR (XCDR (it->font_height)));
2673 if (EQ (XCAR (it->font_height), Qplus))
2674 steps = - steps;
2675 it->face_id = smaller_face (it->f, it->face_id, steps);
2676 }
2677 else if (FUNCTIONP (it->font_height))
2678 {
2679 /* Call function with current height as argument.
2680 Value is the new height. */
2681 Lisp_Object height;
2682 height = safe_call1 (it->font_height,
2683 face->lface[LFACE_HEIGHT_INDEX]);
2684 if (NUMBERP (height))
2685 new_height = XFLOATINT (height);
2686 }
2687 else if (NUMBERP (it->font_height))
2688 {
2689 /* Value is a multiple of the canonical char height. */
2690 struct face *face;
2691
2692 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2693 new_height = (XFLOATINT (it->font_height)
2694 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2695 }
2696 else
2697 {
2698 /* Evaluate IT->font_height with `height' bound to the
2699 current specified height to get the new height. */
2700 Lisp_Object value;
2701 int count = BINDING_STACK_SIZE ();
2702
2703 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2704 value = safe_eval (it->font_height);
2705 unbind_to (count, Qnil);
2706
2707 if (NUMBERP (value))
2708 new_height = XFLOATINT (value);
2709 }
2710
2711 if (new_height > 0)
2712 it->face_id = face_with_height (it->f, it->face_id, new_height);
2713 }
2714 }
2715 else if (CONSP (prop)
2716 && EQ (XCAR (prop), Qspace_width)
2717 && CONSP (XCDR (prop)))
2718 {
2719 /* `(space_width WIDTH)'. */
2720 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2721 return 0;
2722
2723 value = XCAR (XCDR (prop));
2724 if (NUMBERP (value) && XFLOATINT (value) > 0)
2725 it->space_width = value;
2726 }
2727 else if (CONSP (prop)
2728 && EQ (XCAR (prop), Qraise)
2729 && CONSP (XCDR (prop)))
2730 {
2731 /* `(raise FACTOR)'. */
2732 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2733 return 0;
2734
2735 #ifdef HAVE_WINDOW_SYSTEM
2736 value = XCAR (XCDR (prop));
2737 if (NUMBERP (value))
2738 {
2739 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2740 it->voffset = - (XFLOATINT (value)
2741 * (FONT_HEIGHT (face->font)));
2742 }
2743 #endif /* HAVE_WINDOW_SYSTEM */
2744 }
2745 else if (!it->string_from_display_prop_p)
2746 {
2747 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2748 VALUE) or `((margin nil) VALUE)' or VALUE. */
2749 Lisp_Object location, value;
2750 struct text_pos start_pos;
2751 int valid_p;
2752
2753 /* Characters having this form of property are not displayed, so
2754 we have to find the end of the property. */
2755 start_pos = *position;
2756 *position = display_prop_end (it, object, start_pos);
2757 value = Qnil;
2758
2759 /* Let's stop at the new position and assume that all
2760 text properties change there. */
2761 it->stop_charpos = position->charpos;
2762
2763 location = Qunbound;
2764 if (CONSP (prop) && CONSP (XCAR (prop)))
2765 {
2766 Lisp_Object tem;
2767
2768 value = XCDR (prop);
2769 if (CONSP (value))
2770 value = XCAR (value);
2771
2772 tem = XCAR (prop);
2773 if (EQ (XCAR (tem), Qmargin)
2774 && (tem = XCDR (tem),
2775 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2776 (NILP (tem)
2777 || EQ (tem, Qleft_margin)
2778 || EQ (tem, Qright_margin))))
2779 location = tem;
2780 }
2781
2782 if (EQ (location, Qunbound))
2783 {
2784 location = Qnil;
2785 value = prop;
2786 }
2787
2788 #ifdef HAVE_WINDOW_SYSTEM
2789 if (FRAME_TERMCAP_P (it->f))
2790 valid_p = STRINGP (value);
2791 else
2792 valid_p = (STRINGP (value)
2793 || (CONSP (value) && EQ (XCAR (value), Qspace))
2794 || valid_image_p (value));
2795 #else /* not HAVE_WINDOW_SYSTEM */
2796 valid_p = STRINGP (value);
2797 #endif /* not HAVE_WINDOW_SYSTEM */
2798
2799 if ((EQ (location, Qleft_margin)
2800 || EQ (location, Qright_margin)
2801 || NILP (location))
2802 && valid_p)
2803 {
2804 space_or_image_found_p = 1;
2805
2806 /* Save current settings of IT so that we can restore them
2807 when we are finished with the glyph property value. */
2808 push_it (it);
2809
2810 if (NILP (location))
2811 it->area = TEXT_AREA;
2812 else if (EQ (location, Qleft_margin))
2813 it->area = LEFT_MARGIN_AREA;
2814 else
2815 it->area = RIGHT_MARGIN_AREA;
2816
2817 if (STRINGP (value))
2818 {
2819 it->string = value;
2820 it->multibyte_p = STRING_MULTIBYTE (it->string);
2821 it->current.overlay_string_index = -1;
2822 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2823 it->end_charpos = it->string_nchars
2824 = XSTRING (it->string)->size;
2825 it->method = next_element_from_string;
2826 it->stop_charpos = 0;
2827 it->string_from_display_prop_p = 1;
2828 }
2829 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2830 {
2831 it->method = next_element_from_stretch;
2832 it->object = value;
2833 it->current.pos = it->position = start_pos;
2834 }
2835 #ifdef HAVE_WINDOW_SYSTEM
2836 else
2837 {
2838 it->what = IT_IMAGE;
2839 it->image_id = lookup_image (it->f, value);
2840 it->position = start_pos;
2841 it->object = NILP (object) ? it->w->buffer : object;
2842 it->method = next_element_from_image;
2843
2844 /* Say that we haven't consumed the characters with
2845 `display' property yet. The call to pop_it in
2846 set_iterator_to_next will clean this up. */
2847 *position = start_pos;
2848 }
2849 #endif /* HAVE_WINDOW_SYSTEM */
2850 }
2851 else
2852 /* Invalid property or property not supported. Restore
2853 the position to what it was before. */
2854 *position = start_pos;
2855 }
2856
2857 return space_or_image_found_p;
2858 }
2859
2860
2861 /* Check if PROP is a display sub-property value whose text should be
2862 treated as intangible. */
2863
2864 static int
2865 single_display_prop_intangible_p (prop)
2866 Lisp_Object prop;
2867 {
2868 /* Skip over `when FORM'. */
2869 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2870 {
2871 prop = XCDR (prop);
2872 if (!CONSP (prop))
2873 return 0;
2874 prop = XCDR (prop);
2875 }
2876
2877 if (!CONSP (prop))
2878 return 0;
2879
2880 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
2881 we don't need to treat text as intangible. */
2882 if (EQ (XCAR (prop), Qmargin))
2883 {
2884 prop = XCDR (prop);
2885 if (!CONSP (prop))
2886 return 0;
2887
2888 prop = XCDR (prop);
2889 if (!CONSP (prop)
2890 || EQ (XCAR (prop), Qleft_margin)
2891 || EQ (XCAR (prop), Qright_margin))
2892 return 0;
2893 }
2894
2895 return CONSP (prop) && EQ (XCAR (prop), Qimage);
2896 }
2897
2898
2899 /* Check if PROP is a display property value whose text should be
2900 treated as intangible. */
2901
2902 int
2903 display_prop_intangible_p (prop)
2904 Lisp_Object prop;
2905 {
2906 if (CONSP (prop)
2907 && CONSP (XCAR (prop))
2908 && !EQ (Qmargin, XCAR (XCAR (prop))))
2909 {
2910 /* A list of sub-properties. */
2911 while (CONSP (prop))
2912 {
2913 if (single_display_prop_intangible_p (XCAR (prop)))
2914 return 1;
2915 prop = XCDR (prop);
2916 }
2917 }
2918 else if (VECTORP (prop))
2919 {
2920 /* A vector of sub-properties. */
2921 int i;
2922 for (i = 0; i < XVECTOR (prop)->size; ++i)
2923 if (single_display_prop_intangible_p (XVECTOR (prop)->contents[i]))
2924 return 1;
2925 }
2926 else
2927 return single_display_prop_intangible_p (prop);
2928
2929 return 0;
2930 }
2931
2932 \f
2933 /***********************************************************************
2934 `composition' property
2935 ***********************************************************************/
2936
2937 /* Set up iterator IT from `composition' property at its current
2938 position. Called from handle_stop. */
2939
2940 static enum prop_handled
2941 handle_composition_prop (it)
2942 struct it *it;
2943 {
2944 Lisp_Object prop, string;
2945 int pos, pos_byte, end;
2946 enum prop_handled handled = HANDLED_NORMALLY;
2947
2948 if (STRINGP (it->string))
2949 {
2950 pos = IT_STRING_CHARPOS (*it);
2951 pos_byte = IT_STRING_BYTEPOS (*it);
2952 string = it->string;
2953 }
2954 else
2955 {
2956 pos = IT_CHARPOS (*it);
2957 pos_byte = IT_BYTEPOS (*it);
2958 string = Qnil;
2959 }
2960
2961 /* If there's a valid composition and point is not inside of the
2962 composition (in the case that the composition is from the current
2963 buffer), draw a glyph composed from the composition components. */
2964 if (find_composition (pos, -1, &pos, &end, &prop, string)
2965 && COMPOSITION_VALID_P (pos, end, prop)
2966 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
2967 {
2968 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
2969
2970 if (id >= 0)
2971 {
2972 it->method = next_element_from_composition;
2973 it->cmp_id = id;
2974 it->cmp_len = COMPOSITION_LENGTH (prop);
2975 /* For a terminal, draw only the first character of the
2976 components. */
2977 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
2978 it->len = (STRINGP (it->string)
2979 ? string_char_to_byte (it->string, end)
2980 : CHAR_TO_BYTE (end)) - pos_byte;
2981 it->stop_charpos = end;
2982 handled = HANDLED_RETURN;
2983 }
2984 }
2985
2986 return handled;
2987 }
2988
2989
2990 \f
2991 /***********************************************************************
2992 Overlay strings
2993 ***********************************************************************/
2994
2995 /* The following structure is used to record overlay strings for
2996 later sorting in load_overlay_strings. */
2997
2998 struct overlay_entry
2999 {
3000 Lisp_Object overlay;
3001 Lisp_Object string;
3002 int priority;
3003 int after_string_p;
3004 };
3005
3006
3007 /* Set up iterator IT from overlay strings at its current position.
3008 Called from handle_stop. */
3009
3010 static enum prop_handled
3011 handle_overlay_change (it)
3012 struct it *it;
3013 {
3014 if (!STRINGP (it->string) && get_overlay_strings (it))
3015 return HANDLED_RECOMPUTE_PROPS;
3016 else
3017 return HANDLED_NORMALLY;
3018 }
3019
3020
3021 /* Set up the next overlay string for delivery by IT, if there is an
3022 overlay string to deliver. Called by set_iterator_to_next when the
3023 end of the current overlay string is reached. If there are more
3024 overlay strings to display, IT->string and
3025 IT->current.overlay_string_index are set appropriately here.
3026 Otherwise IT->string is set to nil. */
3027
3028 static void
3029 next_overlay_string (it)
3030 struct it *it;
3031 {
3032 ++it->current.overlay_string_index;
3033 if (it->current.overlay_string_index == it->n_overlay_strings)
3034 {
3035 /* No more overlay strings. Restore IT's settings to what
3036 they were before overlay strings were processed, and
3037 continue to deliver from current_buffer. */
3038 pop_it (it);
3039 xassert (it->stop_charpos >= BEGV
3040 && it->stop_charpos <= it->end_charpos);
3041 it->string = Qnil;
3042 it->current.overlay_string_index = -1;
3043 SET_TEXT_POS (it->current.string_pos, -1, -1);
3044 it->n_overlay_strings = 0;
3045 it->method = next_element_from_buffer;
3046
3047 /* If we're at the end of the buffer, record that we have
3048 processed the overlay strings there already, so that
3049 next_element_from_buffer doesn't try it again. */
3050 if (IT_CHARPOS (*it) >= it->end_charpos)
3051 it->overlay_strings_at_end_processed_p = 1;
3052 }
3053 else
3054 {
3055 /* There are more overlay strings to process. If
3056 IT->current.overlay_string_index has advanced to a position
3057 where we must load IT->overlay_strings with more strings, do
3058 it. */
3059 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3060
3061 if (it->current.overlay_string_index && i == 0)
3062 load_overlay_strings (it);
3063
3064 /* Initialize IT to deliver display elements from the overlay
3065 string. */
3066 it->string = it->overlay_strings[i];
3067 it->multibyte_p = STRING_MULTIBYTE (it->string);
3068 SET_TEXT_POS (it->current.string_pos, 0, 0);
3069 it->method = next_element_from_string;
3070 it->stop_charpos = 0;
3071 }
3072
3073 CHECK_IT (it);
3074 }
3075
3076
3077 /* Compare two overlay_entry structures E1 and E2. Used as a
3078 comparison function for qsort in load_overlay_strings. Overlay
3079 strings for the same position are sorted so that
3080
3081 1. All after-strings come in front of before-strings, except
3082 when they come from the same overlay.
3083
3084 2. Within after-strings, strings are sorted so that overlay strings
3085 from overlays with higher priorities come first.
3086
3087 2. Within before-strings, strings are sorted so that overlay
3088 strings from overlays with higher priorities come last.
3089
3090 Value is analogous to strcmp. */
3091
3092
3093 static int
3094 compare_overlay_entries (e1, e2)
3095 void *e1, *e2;
3096 {
3097 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3098 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3099 int result;
3100
3101 if (entry1->after_string_p != entry2->after_string_p)
3102 {
3103 /* Let after-strings appear in front of before-strings if
3104 they come from different overlays. */
3105 if (EQ (entry1->overlay, entry2->overlay))
3106 result = entry1->after_string_p ? 1 : -1;
3107 else
3108 result = entry1->after_string_p ? -1 : 1;
3109 }
3110 else if (entry1->after_string_p)
3111 /* After-strings sorted in order of decreasing priority. */
3112 result = entry2->priority - entry1->priority;
3113 else
3114 /* Before-strings sorted in order of increasing priority. */
3115 result = entry1->priority - entry2->priority;
3116
3117 return result;
3118 }
3119
3120
3121 /* Load the vector IT->overlay_strings with overlay strings from IT's
3122 current buffer position. Set IT->n_overlays to the total number of
3123 overlay strings found.
3124
3125 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3126 a time. On entry into load_overlay_strings,
3127 IT->current.overlay_string_index gives the number of overlay
3128 strings that have already been loaded by previous calls to this
3129 function.
3130
3131 IT->add_overlay_start contains an additional overlay start
3132 position to consider for taking overlay strings from, if non-zero.
3133 This position comes into play when the overlay has an `invisible'
3134 property, and both before and after-strings. When we've skipped to
3135 the end of the overlay, because of its `invisible' property, we
3136 nevertheless want its before-string to appear.
3137 IT->add_overlay_start will contain the overlay start position
3138 in this case.
3139
3140 Overlay strings are sorted so that after-string strings come in
3141 front of before-string strings. Within before and after-strings,
3142 strings are sorted by overlay priority. See also function
3143 compare_overlay_entries. */
3144
3145 static void
3146 load_overlay_strings (it)
3147 struct it *it;
3148 {
3149 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3150 Lisp_Object ov, overlay, window, str, invisible;
3151 int start, end;
3152 int size = 20;
3153 int n = 0, i, j, invis_p;
3154 struct overlay_entry *entries
3155 = (struct overlay_entry *) alloca (size * sizeof *entries);
3156 int charpos = IT_CHARPOS (*it);
3157
3158 /* Append the overlay string STRING of overlay OVERLAY to vector
3159 `entries' which has size `size' and currently contains `n'
3160 elements. AFTER_P non-zero means STRING is an after-string of
3161 OVERLAY. */
3162 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3163 do \
3164 { \
3165 Lisp_Object priority; \
3166 \
3167 if (n == size) \
3168 { \
3169 int new_size = 2 * size; \
3170 struct overlay_entry *old = entries; \
3171 entries = \
3172 (struct overlay_entry *) alloca (new_size \
3173 * sizeof *entries); \
3174 bcopy (old, entries, size * sizeof *entries); \
3175 size = new_size; \
3176 } \
3177 \
3178 entries[n].string = (STRING); \
3179 entries[n].overlay = (OVERLAY); \
3180 priority = Foverlay_get ((OVERLAY), Qpriority); \
3181 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3182 entries[n].after_string_p = (AFTER_P); \
3183 ++n; \
3184 } \
3185 while (0)
3186
3187 /* Process overlay before the overlay center. */
3188 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3189 {
3190 overlay = XCAR (ov);
3191 xassert (OVERLAYP (overlay));
3192 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3193 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3194
3195 if (end < charpos)
3196 break;
3197
3198 /* Skip this overlay if it doesn't start or end at IT's current
3199 position. */
3200 if (end != charpos && start != charpos)
3201 continue;
3202
3203 /* Skip this overlay if it doesn't apply to IT->w. */
3204 window = Foverlay_get (overlay, Qwindow);
3205 if (WINDOWP (window) && XWINDOW (window) != it->w)
3206 continue;
3207
3208 /* If the text ``under'' the overlay is invisible, both before-
3209 and after-strings from this overlay are visible; start and
3210 end position are indistinguishable. */
3211 invisible = Foverlay_get (overlay, Qinvisible);
3212 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3213
3214 /* If overlay has a non-empty before-string, record it. */
3215 if ((start == charpos || (end == charpos && invis_p))
3216 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3217 && XSTRING (str)->size)
3218 RECORD_OVERLAY_STRING (overlay, str, 0);
3219
3220 /* If overlay has a non-empty after-string, record it. */
3221 if ((end == charpos || (start == charpos && invis_p))
3222 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3223 && XSTRING (str)->size)
3224 RECORD_OVERLAY_STRING (overlay, str, 1);
3225 }
3226
3227 /* Process overlays after the overlay center. */
3228 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3229 {
3230 overlay = XCAR (ov);
3231 xassert (OVERLAYP (overlay));
3232 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3233 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3234
3235 if (start > charpos)
3236 break;
3237
3238 /* Skip this overlay if it doesn't start or end at IT's current
3239 position. */
3240 if (end != charpos && start != charpos)
3241 continue;
3242
3243 /* Skip this overlay if it doesn't apply to IT->w. */
3244 window = Foverlay_get (overlay, Qwindow);
3245 if (WINDOWP (window) && XWINDOW (window) != it->w)
3246 continue;
3247
3248 /* If the text ``under'' the overlay is invisible, it has a zero
3249 dimension, and both before- and after-strings apply. */
3250 invisible = Foverlay_get (overlay, Qinvisible);
3251 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3252
3253 /* If overlay has a non-empty before-string, record it. */
3254 if ((start == charpos || (end == charpos && invis_p))
3255 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3256 && XSTRING (str)->size)
3257 RECORD_OVERLAY_STRING (overlay, str, 0);
3258
3259 /* If overlay has a non-empty after-string, record it. */
3260 if ((end == charpos || (start == charpos && invis_p))
3261 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3262 && XSTRING (str)->size)
3263 RECORD_OVERLAY_STRING (overlay, str, 1);
3264 }
3265
3266 #undef RECORD_OVERLAY_STRING
3267
3268 /* Sort entries. */
3269 if (n > 1)
3270 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3271
3272 /* Record the total number of strings to process. */
3273 it->n_overlay_strings = n;
3274
3275 /* IT->current.overlay_string_index is the number of overlay strings
3276 that have already been consumed by IT. Copy some of the
3277 remaining overlay strings to IT->overlay_strings. */
3278 i = 0;
3279 j = it->current.overlay_string_index;
3280 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3281 it->overlay_strings[i++] = entries[j++].string;
3282
3283 CHECK_IT (it);
3284 }
3285
3286
3287 /* Get the first chunk of overlay strings at IT's current buffer
3288 position. Value is non-zero if at least one overlay string was
3289 found. */
3290
3291 static int
3292 get_overlay_strings (it)
3293 struct it *it;
3294 {
3295 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3296 process. This fills IT->overlay_strings with strings, and sets
3297 IT->n_overlay_strings to the total number of strings to process.
3298 IT->pos.overlay_string_index has to be set temporarily to zero
3299 because load_overlay_strings needs this; it must be set to -1
3300 when no overlay strings are found because a zero value would
3301 indicate a position in the first overlay string. */
3302 it->current.overlay_string_index = 0;
3303 load_overlay_strings (it);
3304
3305 /* If we found overlay strings, set up IT to deliver display
3306 elements from the first one. Otherwise set up IT to deliver
3307 from current_buffer. */
3308 if (it->n_overlay_strings)
3309 {
3310 /* Make sure we know settings in current_buffer, so that we can
3311 restore meaningful values when we're done with the overlay
3312 strings. */
3313 compute_stop_pos (it);
3314 xassert (it->face_id >= 0);
3315
3316 /* Save IT's settings. They are restored after all overlay
3317 strings have been processed. */
3318 xassert (it->sp == 0);
3319 push_it (it);
3320
3321 /* Set up IT to deliver display elements from the first overlay
3322 string. */
3323 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3324 it->stop_charpos = 0;
3325 it->string = it->overlay_strings[0];
3326 it->multibyte_p = STRING_MULTIBYTE (it->string);
3327 xassert (STRINGP (it->string));
3328 it->method = next_element_from_string;
3329 }
3330 else
3331 {
3332 it->string = Qnil;
3333 it->current.overlay_string_index = -1;
3334 it->method = next_element_from_buffer;
3335 }
3336
3337 CHECK_IT (it);
3338
3339 /* Value is non-zero if we found at least one overlay string. */
3340 return STRINGP (it->string);
3341 }
3342
3343
3344 \f
3345 /***********************************************************************
3346 Saving and restoring state
3347 ***********************************************************************/
3348
3349 /* Save current settings of IT on IT->stack. Called, for example,
3350 before setting up IT for an overlay string, to be able to restore
3351 IT's settings to what they were after the overlay string has been
3352 processed. */
3353
3354 static void
3355 push_it (it)
3356 struct it *it;
3357 {
3358 struct iterator_stack_entry *p;
3359
3360 xassert (it->sp < 2);
3361 p = it->stack + it->sp;
3362
3363 p->stop_charpos = it->stop_charpos;
3364 xassert (it->face_id >= 0);
3365 p->face_id = it->face_id;
3366 p->string = it->string;
3367 p->pos = it->current;
3368 p->end_charpos = it->end_charpos;
3369 p->string_nchars = it->string_nchars;
3370 p->area = it->area;
3371 p->multibyte_p = it->multibyte_p;
3372 p->space_width = it->space_width;
3373 p->font_height = it->font_height;
3374 p->voffset = it->voffset;
3375 p->string_from_display_prop_p = it->string_from_display_prop_p;
3376 ++it->sp;
3377 }
3378
3379
3380 /* Restore IT's settings from IT->stack. Called, for example, when no
3381 more overlay strings must be processed, and we return to delivering
3382 display elements from a buffer, or when the end of a string from a
3383 `display' property is reached and we return to delivering display
3384 elements from an overlay string, or from a buffer. */
3385
3386 static void
3387 pop_it (it)
3388 struct it *it;
3389 {
3390 struct iterator_stack_entry *p;
3391
3392 xassert (it->sp > 0);
3393 --it->sp;
3394 p = it->stack + it->sp;
3395 it->stop_charpos = p->stop_charpos;
3396 it->face_id = p->face_id;
3397 it->string = p->string;
3398 it->current = p->pos;
3399 it->end_charpos = p->end_charpos;
3400 it->string_nchars = p->string_nchars;
3401 it->area = p->area;
3402 it->multibyte_p = p->multibyte_p;
3403 it->space_width = p->space_width;
3404 it->font_height = p->font_height;
3405 it->voffset = p->voffset;
3406 it->string_from_display_prop_p = p->string_from_display_prop_p;
3407 }
3408
3409
3410 \f
3411 /***********************************************************************
3412 Moving over lines
3413 ***********************************************************************/
3414
3415 /* Set IT's current position to the previous line start. */
3416
3417 static void
3418 back_to_previous_line_start (it)
3419 struct it *it;
3420 {
3421 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3422 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3423 }
3424
3425
3426 /* Move IT to the next line start.
3427
3428 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3429 we skipped over part of the text (as opposed to moving the iterator
3430 continuously over the text). Otherwise, don't change the value
3431 of *SKIPPED_P.
3432
3433 Newlines may come from buffer text, overlay strings, or strings
3434 displayed via the `display' property. That's the reason we can't
3435 simply use find_next_newline_no_quit.
3436
3437 Note that this function may not skip over invisible text that is so
3438 because of text properties and immediately follows a newline. If
3439 it would, function reseat_at_next_visible_line_start, when called
3440 from set_iterator_to_next, would effectively make invisible
3441 characters following a newline part of the wrong glyph row, which
3442 leads to wrong cursor motion. */
3443
3444 static int
3445 forward_to_next_line_start (it, skipped_p)
3446 struct it *it;
3447 int *skipped_p;
3448 {
3449 int old_selective, newline_found_p, n;
3450 const int MAX_NEWLINE_DISTANCE = 500;
3451
3452 /* If already on a newline, just consume it to avoid unintended
3453 skipping over invisible text below. */
3454 if (it->what == IT_CHARACTER && it->c == '\n')
3455 {
3456 set_iterator_to_next (it, 0);
3457 return 1;
3458 }
3459
3460 /* Don't handle selective display in the following. It's (a)
3461 unnecessary because it's done by the caller, and (b) leads to an
3462 infinite recursion because next_element_from_ellipsis indirectly
3463 calls this function. */
3464 old_selective = it->selective;
3465 it->selective = 0;
3466
3467 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3468 from buffer text. */
3469 n = newline_found_p = 0;
3470 while (n < MAX_NEWLINE_DISTANCE
3471 && get_next_display_element (it)
3472 && !newline_found_p)
3473 {
3474 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3475 set_iterator_to_next (it, 0);
3476 if (!STRINGP (it->string))
3477 ++n;
3478 }
3479
3480 /* If we didn't find a newline near enough, see if we can use a
3481 short-cut. */
3482 if (!newline_found_p && n == MAX_NEWLINE_DISTANCE)
3483 {
3484 int start = IT_CHARPOS (*it);
3485 int limit = find_next_newline_no_quit (start, 1);
3486 Lisp_Object pos;
3487
3488 xassert (!STRINGP (it->string));
3489
3490 /* If there isn't any `display' property in sight, and no
3491 overlays, we can just use the position of the newline in
3492 buffer text. */
3493 if (it->stop_charpos >= limit
3494 || ((pos = Fnext_single_property_change (make_number (start),
3495 Qdisplay,
3496 Qnil, make_number (limit)),
3497 NILP (pos))
3498 && next_overlay_change (start) == ZV))
3499 {
3500 IT_CHARPOS (*it) = limit;
3501 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3502 *skipped_p = newline_found_p = 1;
3503 }
3504 else
3505 {
3506 while (get_next_display_element (it)
3507 && !newline_found_p)
3508 {
3509 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3510 set_iterator_to_next (it, 0);
3511 }
3512 }
3513 }
3514
3515 it->selective = old_selective;
3516 return newline_found_p;
3517 }
3518
3519
3520 /* Set IT's current position to the previous visible line start. Skip
3521 invisible text that is so either due to text properties or due to
3522 selective display. Caution: this does not change IT->current_x and
3523 IT->hpos. */
3524
3525 static void
3526 back_to_previous_visible_line_start (it)
3527 struct it *it;
3528 {
3529 int visible_p = 0;
3530
3531 /* Go back one newline if not on BEGV already. */
3532 if (IT_CHARPOS (*it) > BEGV)
3533 back_to_previous_line_start (it);
3534
3535 /* Move over lines that are invisible because of selective display
3536 or text properties. */
3537 while (IT_CHARPOS (*it) > BEGV
3538 && !visible_p)
3539 {
3540 visible_p = 1;
3541
3542 /* If selective > 0, then lines indented more than that values
3543 are invisible. */
3544 if (it->selective > 0
3545 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3546 it->selective))
3547 visible_p = 0;
3548 else
3549 {
3550 Lisp_Object prop;
3551
3552 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3553 Qinvisible, it->window);
3554 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3555 visible_p = 0;
3556 }
3557
3558 /* Back one more newline if the current one is invisible. */
3559 if (!visible_p)
3560 back_to_previous_line_start (it);
3561 }
3562
3563 xassert (IT_CHARPOS (*it) >= BEGV);
3564 xassert (IT_CHARPOS (*it) == BEGV
3565 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3566 CHECK_IT (it);
3567 }
3568
3569
3570 /* Reseat iterator IT at the previous visible line start. Skip
3571 invisible text that is so either due to text properties or due to
3572 selective display. At the end, update IT's overlay information,
3573 face information etc. */
3574
3575 static void
3576 reseat_at_previous_visible_line_start (it)
3577 struct it *it;
3578 {
3579 back_to_previous_visible_line_start (it);
3580 reseat (it, it->current.pos, 1);
3581 CHECK_IT (it);
3582 }
3583
3584
3585 /* Reseat iterator IT on the next visible line start in the current
3586 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3587 preceding the line start. Skip over invisible text that is so
3588 because of selective display. Compute faces, overlays etc at the
3589 new position. Note that this function does not skip over text that
3590 is invisible because of text properties. */
3591
3592 static void
3593 reseat_at_next_visible_line_start (it, on_newline_p)
3594 struct it *it;
3595 int on_newline_p;
3596 {
3597 int newline_found_p, skipped_p = 0;
3598
3599 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3600
3601 /* Skip over lines that are invisible because they are indented
3602 more than the value of IT->selective. */
3603 if (it->selective > 0)
3604 while (IT_CHARPOS (*it) < ZV
3605 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3606 it->selective))
3607 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3608
3609 /* Position on the newline if that's what's requested. */
3610 if (on_newline_p && newline_found_p)
3611 {
3612 if (STRINGP (it->string))
3613 {
3614 if (IT_STRING_CHARPOS (*it) > 0)
3615 {
3616 --IT_STRING_CHARPOS (*it);
3617 --IT_STRING_BYTEPOS (*it);
3618 }
3619 }
3620 else if (IT_CHARPOS (*it) > BEGV)
3621 {
3622 --IT_CHARPOS (*it);
3623 --IT_BYTEPOS (*it);
3624 reseat (it, it->current.pos, 0);
3625 }
3626 }
3627 else if (skipped_p)
3628 reseat (it, it->current.pos, 0);
3629
3630 CHECK_IT (it);
3631 }
3632
3633
3634 \f
3635 /***********************************************************************
3636 Changing an iterator's position
3637 ***********************************************************************/
3638
3639 /* Change IT's current position to POS in current_buffer. If FORCE_P
3640 is non-zero, always check for text properties at the new position.
3641 Otherwise, text properties are only looked up if POS >=
3642 IT->check_charpos of a property. */
3643
3644 static void
3645 reseat (it, pos, force_p)
3646 struct it *it;
3647 struct text_pos pos;
3648 int force_p;
3649 {
3650 int original_pos = IT_CHARPOS (*it);
3651
3652 reseat_1 (it, pos, 0);
3653
3654 /* Determine where to check text properties. Avoid doing it
3655 where possible because text property lookup is very expensive. */
3656 if (force_p
3657 || CHARPOS (pos) > it->stop_charpos
3658 || CHARPOS (pos) < original_pos)
3659 handle_stop (it);
3660
3661 CHECK_IT (it);
3662 }
3663
3664
3665 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3666 IT->stop_pos to POS, also. */
3667
3668 static void
3669 reseat_1 (it, pos, set_stop_p)
3670 struct it *it;
3671 struct text_pos pos;
3672 int set_stop_p;
3673 {
3674 /* Don't call this function when scanning a C string. */
3675 xassert (it->s == NULL);
3676
3677 /* POS must be a reasonable value. */
3678 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3679
3680 it->current.pos = it->position = pos;
3681 XSETBUFFER (it->object, current_buffer);
3682 it->dpvec = NULL;
3683 it->current.dpvec_index = -1;
3684 it->current.overlay_string_index = -1;
3685 IT_STRING_CHARPOS (*it) = -1;
3686 IT_STRING_BYTEPOS (*it) = -1;
3687 it->string = Qnil;
3688 it->method = next_element_from_buffer;
3689 it->sp = 0;
3690 it->face_before_selective_p = 0;
3691
3692 if (set_stop_p)
3693 it->stop_charpos = CHARPOS (pos);
3694 }
3695
3696
3697 /* Set up IT for displaying a string, starting at CHARPOS in window W.
3698 If S is non-null, it is a C string to iterate over. Otherwise,
3699 STRING gives a Lisp string to iterate over.
3700
3701 If PRECISION > 0, don't return more then PRECISION number of
3702 characters from the string.
3703
3704 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3705 characters have been returned. FIELD_WIDTH < 0 means an infinite
3706 field width.
3707
3708 MULTIBYTE = 0 means disable processing of multibyte characters,
3709 MULTIBYTE > 0 means enable it,
3710 MULTIBYTE < 0 means use IT->multibyte_p.
3711
3712 IT must be initialized via a prior call to init_iterator before
3713 calling this function. */
3714
3715 static void
3716 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3717 struct it *it;
3718 unsigned char *s;
3719 Lisp_Object string;
3720 int charpos;
3721 int precision, field_width, multibyte;
3722 {
3723 /* No region in strings. */
3724 it->region_beg_charpos = it->region_end_charpos = -1;
3725
3726 /* No text property checks performed by default, but see below. */
3727 it->stop_charpos = -1;
3728
3729 /* Set iterator position and end position. */
3730 bzero (&it->current, sizeof it->current);
3731 it->current.overlay_string_index = -1;
3732 it->current.dpvec_index = -1;
3733 xassert (charpos >= 0);
3734
3735 /* Use the setting of MULTIBYTE if specified. */
3736 if (multibyte >= 0)
3737 it->multibyte_p = multibyte > 0;
3738
3739 if (s == NULL)
3740 {
3741 xassert (STRINGP (string));
3742 it->string = string;
3743 it->s = NULL;
3744 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3745 it->method = next_element_from_string;
3746 it->current.string_pos = string_pos (charpos, string);
3747 }
3748 else
3749 {
3750 it->s = s;
3751 it->string = Qnil;
3752
3753 /* Note that we use IT->current.pos, not it->current.string_pos,
3754 for displaying C strings. */
3755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
3756 if (it->multibyte_p)
3757 {
3758 it->current.pos = c_string_pos (charpos, s, 1);
3759 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
3760 }
3761 else
3762 {
3763 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
3764 it->end_charpos = it->string_nchars = strlen (s);
3765 }
3766
3767 it->method = next_element_from_c_string;
3768 }
3769
3770 /* PRECISION > 0 means don't return more than PRECISION characters
3771 from the string. */
3772 if (precision > 0 && it->end_charpos - charpos > precision)
3773 it->end_charpos = it->string_nchars = charpos + precision;
3774
3775 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
3776 characters have been returned. FIELD_WIDTH == 0 means don't pad,
3777 FIELD_WIDTH < 0 means infinite field width. This is useful for
3778 padding with `-' at the end of a mode line. */
3779 if (field_width < 0)
3780 field_width = INFINITY;
3781 if (field_width > it->end_charpos - charpos)
3782 it->end_charpos = charpos + field_width;
3783
3784 /* Use the standard display table for displaying strings. */
3785 if (DISP_TABLE_P (Vstandard_display_table))
3786 it->dp = XCHAR_TABLE (Vstandard_display_table);
3787
3788 it->stop_charpos = charpos;
3789 CHECK_IT (it);
3790 }
3791
3792
3793 \f
3794 /***********************************************************************
3795 Iteration
3796 ***********************************************************************/
3797
3798 /* Load IT's display element fields with information about the next
3799 display element from the current position of IT. Value is zero if
3800 end of buffer (or C string) is reached. */
3801
3802 int
3803 get_next_display_element (it)
3804 struct it *it;
3805 {
3806 /* Non-zero means that we found an display element. Zero means that
3807 we hit the end of what we iterate over. Performance note: the
3808 function pointer `method' used here turns out to be faster than
3809 using a sequence of if-statements. */
3810 int success_p = (*it->method) (it);
3811
3812 if (it->what == IT_CHARACTER)
3813 {
3814 /* Map via display table or translate control characters.
3815 IT->c, IT->len etc. have been set to the next character by
3816 the function call above. If we have a display table, and it
3817 contains an entry for IT->c, translate it. Don't do this if
3818 IT->c itself comes from a display table, otherwise we could
3819 end up in an infinite recursion. (An alternative could be to
3820 count the recursion depth of this function and signal an
3821 error when a certain maximum depth is reached.) Is it worth
3822 it? */
3823 if (success_p && it->dpvec == NULL)
3824 {
3825 Lisp_Object dv;
3826
3827 if (it->dp
3828 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
3829 VECTORP (dv)))
3830 {
3831 struct Lisp_Vector *v = XVECTOR (dv);
3832
3833 /* Return the first character from the display table
3834 entry, if not empty. If empty, don't display the
3835 current character. */
3836 if (v->size)
3837 {
3838 it->dpvec_char_len = it->len;
3839 it->dpvec = v->contents;
3840 it->dpend = v->contents + v->size;
3841 it->current.dpvec_index = 0;
3842 it->method = next_element_from_display_vector;
3843 }
3844
3845 success_p = get_next_display_element (it);
3846 }
3847
3848 /* Translate control characters into `\003' or `^C' form.
3849 Control characters coming from a display table entry are
3850 currently not translated because we use IT->dpvec to hold
3851 the translation. This could easily be changed but I
3852 don't believe that it is worth doing.
3853
3854 Non-printable multibyte characters are also translated
3855 octal form. */
3856 else if ((it->c < ' '
3857 && (it->area != TEXT_AREA
3858 || (it->c != '\n' && it->c != '\t')))
3859 || (it->c >= 127
3860 && it->len == 1)
3861 || !CHAR_PRINTABLE_P (it->c))
3862 {
3863 /* IT->c is a control character which must be displayed
3864 either as '\003' or as `^C' where the '\\' and '^'
3865 can be defined in the display table. Fill
3866 IT->ctl_chars with glyphs for what we have to
3867 display. Then, set IT->dpvec to these glyphs. */
3868 GLYPH g;
3869
3870 if (it->c < 128 && it->ctl_arrow_p)
3871 {
3872 /* Set IT->ctl_chars[0] to the glyph for `^'. */
3873 if (it->dp
3874 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
3875 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
3876 g = XINT (DISP_CTRL_GLYPH (it->dp));
3877 else
3878 g = FAST_MAKE_GLYPH ('^', 0);
3879 XSETINT (it->ctl_chars[0], g);
3880
3881 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
3882 XSETINT (it->ctl_chars[1], g);
3883
3884 /* Set up IT->dpvec and return first character from it. */
3885 it->dpvec_char_len = it->len;
3886 it->dpvec = it->ctl_chars;
3887 it->dpend = it->dpvec + 2;
3888 it->current.dpvec_index = 0;
3889 it->method = next_element_from_display_vector;
3890 get_next_display_element (it);
3891 }
3892 else
3893 {
3894 unsigned char str[MAX_MULTIBYTE_LENGTH];
3895 int len;
3896 int i;
3897 GLYPH escape_glyph;
3898
3899 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
3900 if (it->dp
3901 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
3902 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
3903 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
3904 else
3905 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
3906
3907 if (SINGLE_BYTE_CHAR_P (it->c))
3908 str[0] = it->c, len = 1;
3909 else
3910 len = CHAR_STRING (it->c, str);
3911
3912 for (i = 0; i < len; i++)
3913 {
3914 XSETINT (it->ctl_chars[i * 4], escape_glyph);
3915 /* Insert three more glyphs into IT->ctl_chars for
3916 the octal display of the character. */
3917 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
3918 XSETINT (it->ctl_chars[i * 4 + 1], g);
3919 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
3920 XSETINT (it->ctl_chars[i * 4 + 2], g);
3921 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
3922 XSETINT (it->ctl_chars[i * 4 + 3], g);
3923 }
3924
3925 /* Set up IT->dpvec and return the first character
3926 from it. */
3927 it->dpvec_char_len = it->len;
3928 it->dpvec = it->ctl_chars;
3929 it->dpend = it->dpvec + len * 4;
3930 it->current.dpvec_index = 0;
3931 it->method = next_element_from_display_vector;
3932 get_next_display_element (it);
3933 }
3934 }
3935 }
3936
3937 /* Adjust face id for a multibyte character. There are no
3938 multibyte character in unibyte text. */
3939 if (it->multibyte_p
3940 && success_p
3941 && FRAME_WINDOW_P (it->f))
3942 {
3943 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3944 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
3945 }
3946 }
3947
3948 /* Is this character the last one of a run of characters with
3949 box? If yes, set IT->end_of_box_run_p to 1. */
3950 if (it->face_box_p
3951 && it->s == NULL)
3952 {
3953 int face_id;
3954 struct face *face;
3955
3956 it->end_of_box_run_p
3957 = ((face_id = face_after_it_pos (it),
3958 face_id != it->face_id)
3959 && (face = FACE_FROM_ID (it->f, face_id),
3960 face->box == FACE_NO_BOX));
3961 }
3962
3963 /* Value is 0 if end of buffer or string reached. */
3964 return success_p;
3965 }
3966
3967
3968 /* Move IT to the next display element.
3969
3970 RESEAT_P non-zero means if called on a newline in buffer text,
3971 skip to the next visible line start.
3972
3973 Functions get_next_display_element and set_iterator_to_next are
3974 separate because I find this arrangement easier to handle than a
3975 get_next_display_element function that also increments IT's
3976 position. The way it is we can first look at an iterator's current
3977 display element, decide whether it fits on a line, and if it does,
3978 increment the iterator position. The other way around we probably
3979 would either need a flag indicating whether the iterator has to be
3980 incremented the next time, or we would have to implement a
3981 decrement position function which would not be easy to write. */
3982
3983 void
3984 set_iterator_to_next (it, reseat_p)
3985 struct it *it;
3986 int reseat_p;
3987 {
3988 /* Reset flags indicating start and end of a sequence of characters
3989 with box. Reset them at the start of this function because
3990 moving the iterator to a new position might set them. */
3991 it->start_of_box_run_p = it->end_of_box_run_p = 0;
3992
3993 if (it->method == next_element_from_buffer)
3994 {
3995 /* The current display element of IT is a character from
3996 current_buffer. Advance in the buffer, and maybe skip over
3997 invisible lines that are so because of selective display. */
3998 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
3999 reseat_at_next_visible_line_start (it, 0);
4000 else
4001 {
4002 xassert (it->len != 0);
4003 IT_BYTEPOS (*it) += it->len;
4004 IT_CHARPOS (*it) += 1;
4005 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4006 }
4007 }
4008 else if (it->method == next_element_from_composition)
4009 {
4010 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4011 if (STRINGP (it->string))
4012 {
4013 IT_STRING_BYTEPOS (*it) += it->len;
4014 IT_STRING_CHARPOS (*it) += it->cmp_len;
4015 it->method = next_element_from_string;
4016 goto consider_string_end;
4017 }
4018 else
4019 {
4020 IT_BYTEPOS (*it) += it->len;
4021 IT_CHARPOS (*it) += it->cmp_len;
4022 it->method = next_element_from_buffer;
4023 }
4024 }
4025 else if (it->method == next_element_from_c_string)
4026 {
4027 /* Current display element of IT is from a C string. */
4028 IT_BYTEPOS (*it) += it->len;
4029 IT_CHARPOS (*it) += 1;
4030 }
4031 else if (it->method == next_element_from_display_vector)
4032 {
4033 /* Current display element of IT is from a display table entry.
4034 Advance in the display table definition. Reset it to null if
4035 end reached, and continue with characters from buffers/
4036 strings. */
4037 ++it->current.dpvec_index;
4038
4039 /* Restore face of the iterator to what they were before the
4040 display vector entry (these entries may contain faces). */
4041 it->face_id = it->saved_face_id;
4042
4043 if (it->dpvec + it->current.dpvec_index == it->dpend)
4044 {
4045 if (it->s)
4046 it->method = next_element_from_c_string;
4047 else if (STRINGP (it->string))
4048 it->method = next_element_from_string;
4049 else
4050 it->method = next_element_from_buffer;
4051
4052 it->dpvec = NULL;
4053 it->current.dpvec_index = -1;
4054
4055 /* Skip over characters which were displayed via IT->dpvec. */
4056 if (it->dpvec_char_len < 0)
4057 reseat_at_next_visible_line_start (it, 1);
4058 else if (it->dpvec_char_len > 0)
4059 {
4060 it->len = it->dpvec_char_len;
4061 set_iterator_to_next (it, reseat_p);
4062 }
4063 }
4064 }
4065 else if (it->method == next_element_from_string)
4066 {
4067 /* Current display element is a character from a Lisp string. */
4068 xassert (it->s == NULL && STRINGP (it->string));
4069 IT_STRING_BYTEPOS (*it) += it->len;
4070 IT_STRING_CHARPOS (*it) += 1;
4071
4072 consider_string_end:
4073
4074 if (it->current.overlay_string_index >= 0)
4075 {
4076 /* IT->string is an overlay string. Advance to the
4077 next, if there is one. */
4078 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4079 next_overlay_string (it);
4080 }
4081 else
4082 {
4083 /* IT->string is not an overlay string. If we reached
4084 its end, and there is something on IT->stack, proceed
4085 with what is on the stack. This can be either another
4086 string, this time an overlay string, or a buffer. */
4087 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4088 && it->sp > 0)
4089 {
4090 pop_it (it);
4091 if (!STRINGP (it->string))
4092 it->method = next_element_from_buffer;
4093 }
4094 }
4095 }
4096 else if (it->method == next_element_from_image
4097 || it->method == next_element_from_stretch)
4098 {
4099 /* The position etc with which we have to proceed are on
4100 the stack. The position may be at the end of a string,
4101 if the `display' property takes up the whole string. */
4102 pop_it (it);
4103 it->image_id = 0;
4104 if (STRINGP (it->string))
4105 {
4106 it->method = next_element_from_string;
4107 goto consider_string_end;
4108 }
4109 else
4110 it->method = next_element_from_buffer;
4111 }
4112 else
4113 /* There are no other methods defined, so this should be a bug. */
4114 abort ();
4115
4116 xassert (it->method != next_element_from_string
4117 || (STRINGP (it->string)
4118 && IT_STRING_CHARPOS (*it) >= 0));
4119 }
4120
4121
4122 /* Load IT's display element fields with information about the next
4123 display element which comes from a display table entry or from the
4124 result of translating a control character to one of the forms `^C'
4125 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4126
4127 static int
4128 next_element_from_display_vector (it)
4129 struct it *it;
4130 {
4131 /* Precondition. */
4132 xassert (it->dpvec && it->current.dpvec_index >= 0);
4133
4134 /* Remember the current face id in case glyphs specify faces.
4135 IT's face is restored in set_iterator_to_next. */
4136 it->saved_face_id = it->face_id;
4137
4138 if (INTEGERP (*it->dpvec)
4139 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4140 {
4141 int lface_id;
4142 GLYPH g;
4143
4144 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4145 it->c = FAST_GLYPH_CHAR (g);
4146 it->len = CHAR_BYTES (it->c);
4147
4148 /* The entry may contain a face id to use. Such a face id is
4149 the id of a Lisp face, not a realized face. A face id of
4150 zero means no face is specified. */
4151 lface_id = FAST_GLYPH_FACE (g);
4152 if (lface_id)
4153 {
4154 /* The function returns -1 if lface_id is invalid. */
4155 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4156 if (face_id >= 0)
4157 it->face_id = face_id;
4158 }
4159 }
4160 else
4161 /* Display table entry is invalid. Return a space. */
4162 it->c = ' ', it->len = 1;
4163
4164 /* Don't change position and object of the iterator here. They are
4165 still the values of the character that had this display table
4166 entry or was translated, and that's what we want. */
4167 it->what = IT_CHARACTER;
4168 return 1;
4169 }
4170
4171
4172 /* Load IT with the next display element from Lisp string IT->string.
4173 IT->current.string_pos is the current position within the string.
4174 If IT->current.overlay_string_index >= 0, the Lisp string is an
4175 overlay string. */
4176
4177 static int
4178 next_element_from_string (it)
4179 struct it *it;
4180 {
4181 struct text_pos position;
4182
4183 xassert (STRINGP (it->string));
4184 xassert (IT_STRING_CHARPOS (*it) >= 0);
4185 position = it->current.string_pos;
4186
4187 /* Time to check for invisible text? */
4188 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4189 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4190 {
4191 handle_stop (it);
4192
4193 /* Since a handler may have changed IT->method, we must
4194 recurse here. */
4195 return get_next_display_element (it);
4196 }
4197
4198 if (it->current.overlay_string_index >= 0)
4199 {
4200 /* Get the next character from an overlay string. In overlay
4201 strings, There is no field width or padding with spaces to
4202 do. */
4203 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4204 {
4205 it->what = IT_EOB;
4206 return 0;
4207 }
4208 else if (STRING_MULTIBYTE (it->string))
4209 {
4210 int remaining = (STRING_BYTES (XSTRING (it->string))
4211 - IT_STRING_BYTEPOS (*it));
4212 unsigned char *s = (XSTRING (it->string)->data
4213 + IT_STRING_BYTEPOS (*it));
4214 it->c = string_char_and_length (s, remaining, &it->len);
4215 }
4216 else
4217 {
4218 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4219 it->len = 1;
4220 }
4221 }
4222 else
4223 {
4224 /* Get the next character from a Lisp string that is not an
4225 overlay string. Such strings come from the mode line, for
4226 example. We may have to pad with spaces, or truncate the
4227 string. See also next_element_from_c_string. */
4228 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4229 {
4230 it->what = IT_EOB;
4231 return 0;
4232 }
4233 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4234 {
4235 /* Pad with spaces. */
4236 it->c = ' ', it->len = 1;
4237 CHARPOS (position) = BYTEPOS (position) = -1;
4238 }
4239 else if (STRING_MULTIBYTE (it->string))
4240 {
4241 int maxlen = (STRING_BYTES (XSTRING (it->string))
4242 - IT_STRING_BYTEPOS (*it));
4243 unsigned char *s = (XSTRING (it->string)->data
4244 + IT_STRING_BYTEPOS (*it));
4245 it->c = string_char_and_length (s, maxlen, &it->len);
4246 }
4247 else
4248 {
4249 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4250 it->len = 1;
4251 }
4252 }
4253
4254 /* Record what we have and where it came from. Note that we store a
4255 buffer position in IT->position although it could arguably be a
4256 string position. */
4257 it->what = IT_CHARACTER;
4258 it->object = it->string;
4259 it->position = position;
4260 return 1;
4261 }
4262
4263
4264 /* Load IT with next display element from C string IT->s.
4265 IT->string_nchars is the maximum number of characters to return
4266 from the string. IT->end_charpos may be greater than
4267 IT->string_nchars when this function is called, in which case we
4268 may have to return padding spaces. Value is zero if end of string
4269 reached, including padding spaces. */
4270
4271 static int
4272 next_element_from_c_string (it)
4273 struct it *it;
4274 {
4275 int success_p = 1;
4276
4277 xassert (it->s);
4278 it->what = IT_CHARACTER;
4279 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4280 it->object = Qnil;
4281
4282 /* IT's position can be greater IT->string_nchars in case a field
4283 width or precision has been specified when the iterator was
4284 initialized. */
4285 if (IT_CHARPOS (*it) >= it->end_charpos)
4286 {
4287 /* End of the game. */
4288 it->what = IT_EOB;
4289 success_p = 0;
4290 }
4291 else if (IT_CHARPOS (*it) >= it->string_nchars)
4292 {
4293 /* Pad with spaces. */
4294 it->c = ' ', it->len = 1;
4295 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4296 }
4297 else if (it->multibyte_p)
4298 {
4299 /* Implementation note: The calls to strlen apparently aren't a
4300 performance problem because there is no noticeable performance
4301 difference between Emacs running in unibyte or multibyte mode. */
4302 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4303 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4304 maxlen, &it->len);
4305 }
4306 else
4307 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4308
4309 return success_p;
4310 }
4311
4312
4313 /* Set up IT to return characters from an ellipsis, if appropriate.
4314 The definition of the ellipsis glyphs may come from a display table
4315 entry. This function Fills IT with the first glyph from the
4316 ellipsis if an ellipsis is to be displayed. */
4317
4318 static int
4319 next_element_from_ellipsis (it)
4320 struct it *it;
4321 {
4322 if (it->selective_display_ellipsis_p)
4323 {
4324 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4325 {
4326 /* Use the display table definition for `...'. Invalid glyphs
4327 will be handled by the method returning elements from dpvec. */
4328 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4329 it->dpvec_char_len = it->len;
4330 it->dpvec = v->contents;
4331 it->dpend = v->contents + v->size;
4332 it->current.dpvec_index = 0;
4333 it->method = next_element_from_display_vector;
4334 }
4335 else
4336 {
4337 /* Use default `...' which is stored in default_invis_vector. */
4338 it->dpvec_char_len = it->len;
4339 it->dpvec = default_invis_vector;
4340 it->dpend = default_invis_vector + 3;
4341 it->current.dpvec_index = 0;
4342 it->method = next_element_from_display_vector;
4343 }
4344 }
4345 else
4346 {
4347 /* The face at the current position may be different from the
4348 face we find after the invisible text. Remember what it
4349 was in IT->saved_face_id, and signal that it's there by
4350 setting face_before_selective_p. */
4351 it->saved_face_id = it->face_id;
4352 it->method = next_element_from_buffer;
4353 reseat_at_next_visible_line_start (it, 1);
4354 it->face_before_selective_p = 1;
4355 }
4356
4357 return get_next_display_element (it);
4358 }
4359
4360
4361 /* Deliver an image display element. The iterator IT is already
4362 filled with image information (done in handle_display_prop). Value
4363 is always 1. */
4364
4365
4366 static int
4367 next_element_from_image (it)
4368 struct it *it;
4369 {
4370 it->what = IT_IMAGE;
4371 return 1;
4372 }
4373
4374
4375 /* Fill iterator IT with next display element from a stretch glyph
4376 property. IT->object is the value of the text property. Value is
4377 always 1. */
4378
4379 static int
4380 next_element_from_stretch (it)
4381 struct it *it;
4382 {
4383 it->what = IT_STRETCH;
4384 return 1;
4385 }
4386
4387
4388 /* Load IT with the next display element from current_buffer. Value
4389 is zero if end of buffer reached. IT->stop_charpos is the next
4390 position at which to stop and check for text properties or buffer
4391 end. */
4392
4393 static int
4394 next_element_from_buffer (it)
4395 struct it *it;
4396 {
4397 int success_p = 1;
4398
4399 /* Check this assumption, otherwise, we would never enter the
4400 if-statement, below. */
4401 xassert (IT_CHARPOS (*it) >= BEGV
4402 && IT_CHARPOS (*it) <= it->stop_charpos);
4403
4404 if (IT_CHARPOS (*it) >= it->stop_charpos)
4405 {
4406 if (IT_CHARPOS (*it) >= it->end_charpos)
4407 {
4408 int overlay_strings_follow_p;
4409
4410 /* End of the game, except when overlay strings follow that
4411 haven't been returned yet. */
4412 if (it->overlay_strings_at_end_processed_p)
4413 overlay_strings_follow_p = 0;
4414 else
4415 {
4416 it->overlay_strings_at_end_processed_p = 1;
4417 overlay_strings_follow_p = get_overlay_strings (it);
4418 }
4419
4420 if (overlay_strings_follow_p)
4421 success_p = get_next_display_element (it);
4422 else
4423 {
4424 it->what = IT_EOB;
4425 it->position = it->current.pos;
4426 success_p = 0;
4427 }
4428 }
4429 else
4430 {
4431 handle_stop (it);
4432 return get_next_display_element (it);
4433 }
4434 }
4435 else
4436 {
4437 /* No face changes, overlays etc. in sight, so just return a
4438 character from current_buffer. */
4439 unsigned char *p;
4440
4441 /* Maybe run the redisplay end trigger hook. Performance note:
4442 This doesn't seem to cost measurable time. */
4443 if (it->redisplay_end_trigger_charpos
4444 && it->glyph_row
4445 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4446 run_redisplay_end_trigger_hook (it);
4447
4448 /* Get the next character, maybe multibyte. */
4449 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4450 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4451 {
4452 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4453 - IT_BYTEPOS (*it));
4454 it->c = string_char_and_length (p, maxlen, &it->len);
4455 }
4456 else
4457 it->c = *p, it->len = 1;
4458
4459 /* Record what we have and where it came from. */
4460 it->what = IT_CHARACTER;;
4461 it->object = it->w->buffer;
4462 it->position = it->current.pos;
4463
4464 /* Normally we return the character found above, except when we
4465 really want to return an ellipsis for selective display. */
4466 if (it->selective)
4467 {
4468 if (it->c == '\n')
4469 {
4470 /* A value of selective > 0 means hide lines indented more
4471 than that number of columns. */
4472 if (it->selective > 0
4473 && IT_CHARPOS (*it) + 1 < ZV
4474 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4475 IT_BYTEPOS (*it) + 1,
4476 it->selective))
4477 {
4478 success_p = next_element_from_ellipsis (it);
4479 it->dpvec_char_len = -1;
4480 }
4481 }
4482 else if (it->c == '\r' && it->selective == -1)
4483 {
4484 /* A value of selective == -1 means that everything from the
4485 CR to the end of the line is invisible, with maybe an
4486 ellipsis displayed for it. */
4487 success_p = next_element_from_ellipsis (it);
4488 it->dpvec_char_len = -1;
4489 }
4490 }
4491 }
4492
4493 /* Value is zero if end of buffer reached. */
4494 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4495 return success_p;
4496 }
4497
4498
4499 /* Run the redisplay end trigger hook for IT. */
4500
4501 static void
4502 run_redisplay_end_trigger_hook (it)
4503 struct it *it;
4504 {
4505 Lisp_Object args[3];
4506
4507 /* IT->glyph_row should be non-null, i.e. we should be actually
4508 displaying something, or otherwise we should not run the hook. */
4509 xassert (it->glyph_row);
4510
4511 /* Set up hook arguments. */
4512 args[0] = Qredisplay_end_trigger_functions;
4513 args[1] = it->window;
4514 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4515 it->redisplay_end_trigger_charpos = 0;
4516
4517 /* Since we are *trying* to run these functions, don't try to run
4518 them again, even if they get an error. */
4519 it->w->redisplay_end_trigger = Qnil;
4520 Frun_hook_with_args (3, args);
4521
4522 /* Notice if it changed the face of the character we are on. */
4523 handle_face_prop (it);
4524 }
4525
4526
4527 /* Deliver a composition display element. The iterator IT is already
4528 filled with composition information (done in
4529 handle_composition_prop). Value is always 1. */
4530
4531 static int
4532 next_element_from_composition (it)
4533 struct it *it;
4534 {
4535 it->what = IT_COMPOSITION;
4536 it->position = (STRINGP (it->string)
4537 ? it->current.string_pos
4538 : it->current.pos);
4539 return 1;
4540 }
4541
4542
4543 \f
4544 /***********************************************************************
4545 Moving an iterator without producing glyphs
4546 ***********************************************************************/
4547
4548 /* Move iterator IT to a specified buffer or X position within one
4549 line on the display without producing glyphs.
4550
4551 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4552 whichever is reached first.
4553
4554 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4555
4556 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4557 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4558 TO_X includes the amount by which a window is horizontally
4559 scrolled.
4560
4561 Value is
4562
4563 MOVE_POS_MATCH_OR_ZV
4564 - when TO_POS or ZV was reached.
4565
4566 MOVE_X_REACHED
4567 -when TO_X was reached before TO_POS or ZV were reached.
4568
4569 MOVE_LINE_CONTINUED
4570 - when we reached the end of the display area and the line must
4571 be continued.
4572
4573 MOVE_LINE_TRUNCATED
4574 - when we reached the end of the display area and the line is
4575 truncated.
4576
4577 MOVE_NEWLINE_OR_CR
4578 - when we stopped at a line end, i.e. a newline or a CR and selective
4579 display is on. */
4580
4581 static enum move_it_result
4582 move_it_in_display_line_to (it, to_charpos, to_x, op)
4583 struct it *it;
4584 int to_charpos, to_x, op;
4585 {
4586 enum move_it_result result = MOVE_UNDEFINED;
4587 struct glyph_row *saved_glyph_row;
4588
4589 /* Don't produce glyphs in produce_glyphs. */
4590 saved_glyph_row = it->glyph_row;
4591 it->glyph_row = NULL;
4592
4593 while (1)
4594 {
4595 int x, i, ascent = 0, descent = 0;
4596
4597 /* Stop when ZV or TO_CHARPOS reached. */
4598 if (!get_next_display_element (it)
4599 || ((op & MOVE_TO_POS) != 0
4600 && BUFFERP (it->object)
4601 && IT_CHARPOS (*it) >= to_charpos))
4602 {
4603 result = MOVE_POS_MATCH_OR_ZV;
4604 break;
4605 }
4606
4607 /* The call to produce_glyphs will get the metrics of the
4608 display element IT is loaded with. We record in x the
4609 x-position before this display element in case it does not
4610 fit on the line. */
4611 x = it->current_x;
4612
4613 /* Remember the line height so far in case the next element doesn't
4614 fit on the line. */
4615 if (!it->truncate_lines_p)
4616 {
4617 ascent = it->max_ascent;
4618 descent = it->max_descent;
4619 }
4620
4621 PRODUCE_GLYPHS (it);
4622
4623 if (it->area != TEXT_AREA)
4624 {
4625 set_iterator_to_next (it, 1);
4626 continue;
4627 }
4628
4629 /* The number of glyphs we get back in IT->nglyphs will normally
4630 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4631 character on a terminal frame, or (iii) a line end. For the
4632 second case, IT->nglyphs - 1 padding glyphs will be present
4633 (on X frames, there is only one glyph produced for a
4634 composite character.
4635
4636 The behavior implemented below means, for continuation lines,
4637 that as many spaces of a TAB as fit on the current line are
4638 displayed there. For terminal frames, as many glyphs of a
4639 multi-glyph character are displayed in the current line, too.
4640 This is what the old redisplay code did, and we keep it that
4641 way. Under X, the whole shape of a complex character must
4642 fit on the line or it will be completely displayed in the
4643 next line.
4644
4645 Note that both for tabs and padding glyphs, all glyphs have
4646 the same width. */
4647 if (it->nglyphs)
4648 {
4649 /* More than one glyph or glyph doesn't fit on line. All
4650 glyphs have the same width. */
4651 int single_glyph_width = it->pixel_width / it->nglyphs;
4652 int new_x;
4653
4654 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4655 {
4656 new_x = x + single_glyph_width;
4657
4658 /* We want to leave anything reaching TO_X to the caller. */
4659 if ((op & MOVE_TO_X) && new_x > to_x)
4660 {
4661 it->current_x = x;
4662 result = MOVE_X_REACHED;
4663 break;
4664 }
4665 else if (/* Lines are continued. */
4666 !it->truncate_lines_p
4667 && (/* And glyph doesn't fit on the line. */
4668 new_x > it->last_visible_x
4669 /* Or it fits exactly and we're on a window
4670 system frame. */
4671 || (new_x == it->last_visible_x
4672 && FRAME_WINDOW_P (it->f))))
4673 {
4674 if (/* IT->hpos == 0 means the very first glyph
4675 doesn't fit on the line, e.g. a wide image. */
4676 it->hpos == 0
4677 || (new_x == it->last_visible_x
4678 && FRAME_WINDOW_P (it->f)))
4679 {
4680 ++it->hpos;
4681 it->current_x = new_x;
4682 if (i == it->nglyphs - 1)
4683 set_iterator_to_next (it, 1);
4684 }
4685 else
4686 {
4687 it->current_x = x;
4688 it->max_ascent = ascent;
4689 it->max_descent = descent;
4690 }
4691
4692 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4693 IT_CHARPOS (*it)));
4694 result = MOVE_LINE_CONTINUED;
4695 break;
4696 }
4697 else if (new_x > it->first_visible_x)
4698 {
4699 /* Glyph is visible. Increment number of glyphs that
4700 would be displayed. */
4701 ++it->hpos;
4702 }
4703 else
4704 {
4705 /* Glyph is completely off the left margin of the display
4706 area. Nothing to do. */
4707 }
4708 }
4709
4710 if (result != MOVE_UNDEFINED)
4711 break;
4712 }
4713 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4714 {
4715 /* Stop when TO_X specified and reached. This check is
4716 necessary here because of lines consisting of a line end,
4717 only. The line end will not produce any glyphs and we
4718 would never get MOVE_X_REACHED. */
4719 xassert (it->nglyphs == 0);
4720 result = MOVE_X_REACHED;
4721 break;
4722 }
4723
4724 /* Is this a line end? If yes, we're done. */
4725 if (ITERATOR_AT_END_OF_LINE_P (it))
4726 {
4727 result = MOVE_NEWLINE_OR_CR;
4728 break;
4729 }
4730
4731 /* The current display element has been consumed. Advance
4732 to the next. */
4733 set_iterator_to_next (it, 1);
4734
4735 /* Stop if lines are truncated and IT's current x-position is
4736 past the right edge of the window now. */
4737 if (it->truncate_lines_p
4738 && it->current_x >= it->last_visible_x)
4739 {
4740 result = MOVE_LINE_TRUNCATED;
4741 break;
4742 }
4743 }
4744
4745 /* Restore the iterator settings altered at the beginning of this
4746 function. */
4747 it->glyph_row = saved_glyph_row;
4748 return result;
4749 }
4750
4751
4752 /* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
4753 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
4754 the description of enum move_operation_enum.
4755
4756 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
4757 screen line, this function will set IT to the next position >
4758 TO_CHARPOS. */
4759
4760 void
4761 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
4762 struct it *it;
4763 int to_charpos, to_x, to_y, to_vpos;
4764 int op;
4765 {
4766 enum move_it_result skip, skip2 = MOVE_X_REACHED;
4767 int line_height;
4768 int reached = 0;
4769
4770 for (;;)
4771 {
4772 if (op & MOVE_TO_VPOS)
4773 {
4774 /* If no TO_CHARPOS and no TO_X specified, stop at the
4775 start of the line TO_VPOS. */
4776 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
4777 {
4778 if (it->vpos == to_vpos)
4779 {
4780 reached = 1;
4781 break;
4782 }
4783 else
4784 skip = move_it_in_display_line_to (it, -1, -1, 0);
4785 }
4786 else
4787 {
4788 /* TO_VPOS >= 0 means stop at TO_X in the line at
4789 TO_VPOS, or at TO_POS, whichever comes first. */
4790 if (it->vpos == to_vpos)
4791 {
4792 reached = 2;
4793 break;
4794 }
4795
4796 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
4797
4798 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
4799 {
4800 reached = 3;
4801 break;
4802 }
4803 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
4804 {
4805 /* We have reached TO_X but not in the line we want. */
4806 skip = move_it_in_display_line_to (it, to_charpos,
4807 -1, MOVE_TO_POS);
4808 if (skip == MOVE_POS_MATCH_OR_ZV)
4809 {
4810 reached = 4;
4811 break;
4812 }
4813 }
4814 }
4815 }
4816 else if (op & MOVE_TO_Y)
4817 {
4818 struct it it_backup;
4819
4820 /* TO_Y specified means stop at TO_X in the line containing
4821 TO_Y---or at TO_CHARPOS if this is reached first. The
4822 problem is that we can't really tell whether the line
4823 contains TO_Y before we have completely scanned it, and
4824 this may skip past TO_X. What we do is to first scan to
4825 TO_X.
4826
4827 If TO_X is not specified, use a TO_X of zero. The reason
4828 is to make the outcome of this function more predictable.
4829 If we didn't use TO_X == 0, we would stop at the end of
4830 the line which is probably not what a caller would expect
4831 to happen. */
4832 skip = move_it_in_display_line_to (it, to_charpos,
4833 ((op & MOVE_TO_X)
4834 ? to_x : 0),
4835 (MOVE_TO_X
4836 | (op & MOVE_TO_POS)));
4837
4838 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
4839 if (skip == MOVE_POS_MATCH_OR_ZV)
4840 {
4841 reached = 5;
4842 break;
4843 }
4844
4845 /* If TO_X was reached, we would like to know whether TO_Y
4846 is in the line. This can only be said if we know the
4847 total line height which requires us to scan the rest of
4848 the line. */
4849 if (skip == MOVE_X_REACHED)
4850 {
4851 it_backup = *it;
4852 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
4853 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
4854 op & MOVE_TO_POS);
4855 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
4856 }
4857
4858 /* Now, decide whether TO_Y is in this line. */
4859 line_height = it->max_ascent + it->max_descent;
4860 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
4861
4862 if (to_y >= it->current_y
4863 && to_y < it->current_y + line_height)
4864 {
4865 if (skip == MOVE_X_REACHED)
4866 /* If TO_Y is in this line and TO_X was reached above,
4867 we scanned too far. We have to restore IT's settings
4868 to the ones before skipping. */
4869 *it = it_backup;
4870 reached = 6;
4871 }
4872 else if (skip == MOVE_X_REACHED)
4873 {
4874 skip = skip2;
4875 if (skip == MOVE_POS_MATCH_OR_ZV)
4876 reached = 7;
4877 }
4878
4879 if (reached)
4880 break;
4881 }
4882 else
4883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
4884
4885 switch (skip)
4886 {
4887 case MOVE_POS_MATCH_OR_ZV:
4888 reached = 8;
4889 goto out;
4890
4891 case MOVE_NEWLINE_OR_CR:
4892 set_iterator_to_next (it, 1);
4893 it->continuation_lines_width = 0;
4894 break;
4895
4896 case MOVE_LINE_TRUNCATED:
4897 it->continuation_lines_width = 0;
4898 reseat_at_next_visible_line_start (it, 0);
4899 if ((op & MOVE_TO_POS) != 0
4900 && IT_CHARPOS (*it) > to_charpos)
4901 {
4902 reached = 9;
4903 goto out;
4904 }
4905 break;
4906
4907 case MOVE_LINE_CONTINUED:
4908 it->continuation_lines_width += it->current_x;
4909 break;
4910
4911 default:
4912 abort ();
4913 }
4914
4915 /* Reset/increment for the next run. */
4916 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
4917 it->current_x = it->hpos = 0;
4918 it->current_y += it->max_ascent + it->max_descent;
4919 ++it->vpos;
4920 last_height = it->max_ascent + it->max_descent;
4921 last_max_ascent = it->max_ascent;
4922 it->max_ascent = it->max_descent = 0;
4923 }
4924
4925 out:
4926
4927 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
4928 }
4929
4930
4931 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
4932
4933 If DY > 0, move IT backward at least that many pixels. DY = 0
4934 means move IT backward to the preceding line start or BEGV. This
4935 function may move over more than DY pixels if IT->current_y - DY
4936 ends up in the middle of a line; in this case IT->current_y will be
4937 set to the top of the line moved to. */
4938
4939 void
4940 move_it_vertically_backward (it, dy)
4941 struct it *it;
4942 int dy;
4943 {
4944 int nlines, h, line_height;
4945 struct it it2;
4946 int start_pos = IT_CHARPOS (*it);
4947
4948 xassert (dy >= 0);
4949
4950 /* Estimate how many newlines we must move back. */
4951 nlines = max (1, dy / CANON_Y_UNIT (it->f));
4952
4953 /* Set the iterator's position that many lines back. */
4954 while (nlines-- && IT_CHARPOS (*it) > BEGV)
4955 back_to_previous_visible_line_start (it);
4956
4957 /* Reseat the iterator here. When moving backward, we don't want
4958 reseat to skip forward over invisible text, set up the iterator
4959 to deliver from overlay strings at the new position etc. So,
4960 use reseat_1 here. */
4961 reseat_1 (it, it->current.pos, 1);
4962
4963 /* We are now surely at a line start. */
4964 it->current_x = it->hpos = 0;
4965
4966 /* Move forward and see what y-distance we moved. First move to the
4967 start of the next line so that we get its height. We need this
4968 height to be able to tell whether we reached the specified
4969 y-distance. */
4970 it2 = *it;
4971 it2.max_ascent = it2.max_descent = 0;
4972 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
4973 MOVE_TO_POS | MOVE_TO_VPOS);
4974 xassert (IT_CHARPOS (*it) >= BEGV);
4975 line_height = it2.max_ascent + it2.max_descent;
4976
4977 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
4978 xassert (IT_CHARPOS (*it) >= BEGV);
4979 h = it2.current_y - it->current_y;
4980 nlines = it2.vpos - it->vpos;
4981
4982 /* Correct IT's y and vpos position. */
4983 it->vpos -= nlines;
4984 it->current_y -= h;
4985
4986 if (dy == 0)
4987 {
4988 /* DY == 0 means move to the start of the screen line. The
4989 value of nlines is > 0 if continuation lines were involved. */
4990 if (nlines > 0)
4991 move_it_by_lines (it, nlines, 1);
4992 xassert (IT_CHARPOS (*it) <= start_pos);
4993 }
4994 else if (nlines)
4995 {
4996 /* The y-position we try to reach. Note that h has been
4997 subtracted in front of the if-statement. */
4998 int target_y = it->current_y + h - dy;
4999
5000 /* If we did not reach target_y, try to move further backward if
5001 we can. If we moved too far backward, try to move forward. */
5002 if (target_y < it->current_y
5003 && IT_CHARPOS (*it) > BEGV)
5004 {
5005 move_it_vertically (it, target_y - it->current_y);
5006 xassert (IT_CHARPOS (*it) >= BEGV);
5007 }
5008 else if (target_y >= it->current_y + line_height
5009 && IT_CHARPOS (*it) < ZV)
5010 {
5011 move_it_vertically (it, target_y - (it->current_y + line_height));
5012 xassert (IT_CHARPOS (*it) >= BEGV);
5013 }
5014 }
5015 }
5016
5017
5018 /* Move IT by a specified amount of pixel lines DY. DY negative means
5019 move backwards. DY = 0 means move to start of screen line. At the
5020 end, IT will be on the start of a screen line. */
5021
5022 void
5023 move_it_vertically (it, dy)
5024 struct it *it;
5025 int dy;
5026 {
5027 if (dy <= 0)
5028 move_it_vertically_backward (it, -dy);
5029 else if (dy > 0)
5030 {
5031 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5032 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5033 MOVE_TO_POS | MOVE_TO_Y);
5034 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5035
5036 /* If buffer ends in ZV without a newline, move to the start of
5037 the line to satisfy the post-condition. */
5038 if (IT_CHARPOS (*it) == ZV
5039 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5040 move_it_by_lines (it, 0, 0);
5041 }
5042 }
5043
5044
5045 /* Return non-zero if some text between buffer positions START_CHARPOS
5046 and END_CHARPOS is invisible. IT->window is the window for text
5047 property lookup. */
5048
5049 static int
5050 invisible_text_between_p (it, start_charpos, end_charpos)
5051 struct it *it;
5052 int start_charpos, end_charpos;
5053 {
5054 Lisp_Object prop, limit;
5055 int invisible_found_p;
5056
5057 xassert (it != NULL && start_charpos <= end_charpos);
5058
5059 /* Is text at START invisible? */
5060 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5061 it->window);
5062 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5063 invisible_found_p = 1;
5064 else
5065 {
5066 limit = Fnext_single_char_property_change (make_number (start_charpos),
5067 Qinvisible, Qnil,
5068 make_number (end_charpos));
5069 invisible_found_p = XFASTINT (limit) < end_charpos;
5070 }
5071
5072 return invisible_found_p;
5073 }
5074
5075
5076 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5077 negative means move up. DVPOS == 0 means move to the start of the
5078 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5079 NEED_Y_P is zero, IT->current_y will be left unchanged.
5080
5081 Further optimization ideas: If we would know that IT->f doesn't use
5082 a face with proportional font, we could be faster for
5083 truncate-lines nil. */
5084
5085 void
5086 move_it_by_lines (it, dvpos, need_y_p)
5087 struct it *it;
5088 int dvpos, need_y_p;
5089 {
5090 struct position pos;
5091
5092 if (!FRAME_WINDOW_P (it->f))
5093 {
5094 struct text_pos textpos;
5095
5096 /* We can use vmotion on frames without proportional fonts. */
5097 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5098 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5099 reseat (it, textpos, 1);
5100 it->vpos += pos.vpos;
5101 it->current_y += pos.vpos;
5102 }
5103 else if (dvpos == 0)
5104 {
5105 /* DVPOS == 0 means move to the start of the screen line. */
5106 move_it_vertically_backward (it, 0);
5107 xassert (it->current_x == 0 && it->hpos == 0);
5108 }
5109 else if (dvpos > 0)
5110 {
5111 /* If there are no continuation lines, and if there is no
5112 selective display, try the simple method of moving forward
5113 DVPOS newlines, then see where we are. */
5114 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5115 {
5116 int shortage = 0, charpos;
5117
5118 if (FETCH_BYTE (IT_BYTEPOS (*it)) == '\n')
5119 charpos = IT_CHARPOS (*it) + 1;
5120 else
5121 charpos = scan_buffer ('\n', IT_CHARPOS (*it), 0, dvpos,
5122 &shortage, 0);
5123
5124 if (!invisible_text_between_p (it, IT_CHARPOS (*it), charpos))
5125 {
5126 struct text_pos pos;
5127 CHARPOS (pos) = charpos;
5128 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5129 reseat (it, pos, 1);
5130 it->vpos += dvpos - shortage;
5131 it->hpos = it->current_x = 0;
5132 return;
5133 }
5134 }
5135
5136 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5137 }
5138 else
5139 {
5140 struct it it2;
5141 int start_charpos, i;
5142
5143 /* If there are no continuation lines, and if there is no
5144 selective display, try the simple method of moving backward
5145 -DVPOS newlines. */
5146 if (!need_y_p && it->truncate_lines_p && it->selective == 0)
5147 {
5148 int shortage;
5149 int charpos = IT_CHARPOS (*it);
5150 int bytepos = IT_BYTEPOS (*it);
5151
5152 /* If in the middle of a line, go to its start. */
5153 if (charpos > BEGV && FETCH_BYTE (bytepos - 1) != '\n')
5154 {
5155 charpos = find_next_newline_no_quit (charpos, -1);
5156 bytepos = CHAR_TO_BYTE (charpos);
5157 }
5158
5159 if (charpos == BEGV)
5160 {
5161 struct text_pos pos;
5162 CHARPOS (pos) = charpos;
5163 BYTEPOS (pos) = bytepos;
5164 reseat (it, pos, 1);
5165 it->hpos = it->current_x = 0;
5166 return;
5167 }
5168 else
5169 {
5170 charpos = scan_buffer ('\n', charpos - 1, 0, dvpos, &shortage, 0);
5171 if (!invisible_text_between_p (it, charpos, IT_CHARPOS (*it)))
5172 {
5173 struct text_pos pos;
5174 CHARPOS (pos) = charpos;
5175 BYTEPOS (pos) = CHAR_TO_BYTE (charpos);
5176 reseat (it, pos, 1);
5177 it->vpos += dvpos + (shortage ? shortage - 1 : 0);
5178 it->hpos = it->current_x = 0;
5179 return;
5180 }
5181 }
5182 }
5183
5184 /* Go back -DVPOS visible lines and reseat the iterator there. */
5185 start_charpos = IT_CHARPOS (*it);
5186 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5187 back_to_previous_visible_line_start (it);
5188 reseat (it, it->current.pos, 1);
5189 it->current_x = it->hpos = 0;
5190
5191 /* Above call may have moved too far if continuation lines
5192 are involved. Scan forward and see if it did. */
5193 it2 = *it;
5194 it2.vpos = it2.current_y = 0;
5195 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5196 it->vpos -= it2.vpos;
5197 it->current_y -= it2.current_y;
5198 it->current_x = it->hpos = 0;
5199
5200 /* If we moved too far, move IT some lines forward. */
5201 if (it2.vpos > -dvpos)
5202 {
5203 int delta = it2.vpos + dvpos;
5204 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5205 }
5206 }
5207 }
5208
5209
5210 \f
5211 /***********************************************************************
5212 Messages
5213 ***********************************************************************/
5214
5215
5216 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5217 to *Messages*. */
5218
5219 void
5220 add_to_log (format, arg1, arg2)
5221 char *format;
5222 Lisp_Object arg1, arg2;
5223 {
5224 Lisp_Object args[3];
5225 Lisp_Object msg, fmt;
5226 char *buffer;
5227 int len;
5228 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5229
5230 fmt = msg = Qnil;
5231 GCPRO4 (fmt, msg, arg1, arg2);
5232
5233 args[0] = fmt = build_string (format);
5234 args[1] = arg1;
5235 args[2] = arg2;
5236 msg = Fformat (3, args);
5237
5238 len = STRING_BYTES (XSTRING (msg)) + 1;
5239 buffer = (char *) alloca (len);
5240 strcpy (buffer, XSTRING (msg)->data);
5241
5242 message_dolog (buffer, len - 1, 1, 0);
5243 UNGCPRO;
5244 }
5245
5246
5247 /* Output a newline in the *Messages* buffer if "needs" one. */
5248
5249 void
5250 message_log_maybe_newline ()
5251 {
5252 if (message_log_need_newline)
5253 message_dolog ("", 0, 1, 0);
5254 }
5255
5256
5257 /* Add a string M of length LEN to the message log, optionally
5258 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5259 nonzero, means interpret the contents of M as multibyte. This
5260 function calls low-level routines in order to bypass text property
5261 hooks, etc. which might not be safe to run. */
5262
5263 void
5264 message_dolog (m, len, nlflag, multibyte)
5265 char *m;
5266 int len, nlflag, multibyte;
5267 {
5268 if (!NILP (Vmessage_log_max))
5269 {
5270 struct buffer *oldbuf;
5271 Lisp_Object oldpoint, oldbegv, oldzv;
5272 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5273 int point_at_end = 0;
5274 int zv_at_end = 0;
5275 Lisp_Object old_deactivate_mark, tem;
5276 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5277
5278 old_deactivate_mark = Vdeactivate_mark;
5279 oldbuf = current_buffer;
5280 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5281 current_buffer->undo_list = Qt;
5282
5283 oldpoint = Fpoint_marker ();
5284 oldbegv = Fpoint_min_marker ();
5285 oldzv = Fpoint_max_marker ();
5286 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5287
5288 if (PT == Z)
5289 point_at_end = 1;
5290 if (ZV == Z)
5291 zv_at_end = 1;
5292
5293 BEGV = BEG;
5294 BEGV_BYTE = BEG_BYTE;
5295 ZV = Z;
5296 ZV_BYTE = Z_BYTE;
5297 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5298
5299 /* Insert the string--maybe converting multibyte to single byte
5300 or vice versa, so that all the text fits the buffer. */
5301 if (multibyte
5302 && NILP (current_buffer->enable_multibyte_characters))
5303 {
5304 int i, c, nbytes;
5305 unsigned char work[1];
5306
5307 /* Convert a multibyte string to single-byte
5308 for the *Message* buffer. */
5309 for (i = 0; i < len; i += nbytes)
5310 {
5311 c = string_char_and_length (m + i, len - i, &nbytes);
5312 work[0] = (SINGLE_BYTE_CHAR_P (c)
5313 ? c
5314 : multibyte_char_to_unibyte (c, Qnil));
5315 insert_1_both (work, 1, 1, 1, 0, 0);
5316 }
5317 }
5318 else if (! multibyte
5319 && ! NILP (current_buffer->enable_multibyte_characters))
5320 {
5321 int i, c, nbytes;
5322 unsigned char *msg = (unsigned char *) m;
5323 unsigned char str[MAX_MULTIBYTE_LENGTH];
5324 /* Convert a single-byte string to multibyte
5325 for the *Message* buffer. */
5326 for (i = 0; i < len; i++)
5327 {
5328 c = unibyte_char_to_multibyte (msg[i]);
5329 nbytes = CHAR_STRING (c, str);
5330 insert_1_both (str, 1, nbytes, 1, 0, 0);
5331 }
5332 }
5333 else if (len)
5334 insert_1 (m, len, 1, 0, 0);
5335
5336 if (nlflag)
5337 {
5338 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5339 insert_1 ("\n", 1, 1, 0, 0);
5340
5341 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5342 this_bol = PT;
5343 this_bol_byte = PT_BYTE;
5344
5345 if (this_bol > BEG)
5346 {
5347 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5348 prev_bol = PT;
5349 prev_bol_byte = PT_BYTE;
5350
5351 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5352 this_bol, this_bol_byte);
5353 if (dup)
5354 {
5355 del_range_both (prev_bol, prev_bol_byte,
5356 this_bol, this_bol_byte, 0);
5357 if (dup > 1)
5358 {
5359 char dupstr[40];
5360 int duplen;
5361
5362 /* If you change this format, don't forget to also
5363 change message_log_check_duplicate. */
5364 sprintf (dupstr, " [%d times]", dup);
5365 duplen = strlen (dupstr);
5366 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5367 insert_1 (dupstr, duplen, 1, 0, 1);
5368 }
5369 }
5370 }
5371
5372 if (NATNUMP (Vmessage_log_max))
5373 {
5374 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5375 -XFASTINT (Vmessage_log_max) - 1, 0);
5376 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5377 }
5378 }
5379 BEGV = XMARKER (oldbegv)->charpos;
5380 BEGV_BYTE = marker_byte_position (oldbegv);
5381
5382 if (zv_at_end)
5383 {
5384 ZV = Z;
5385 ZV_BYTE = Z_BYTE;
5386 }
5387 else
5388 {
5389 ZV = XMARKER (oldzv)->charpos;
5390 ZV_BYTE = marker_byte_position (oldzv);
5391 }
5392
5393 if (point_at_end)
5394 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5395 else
5396 /* We can't do Fgoto_char (oldpoint) because it will run some
5397 Lisp code. */
5398 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5399 XMARKER (oldpoint)->bytepos);
5400
5401 UNGCPRO;
5402 free_marker (oldpoint);
5403 free_marker (oldbegv);
5404 free_marker (oldzv);
5405
5406 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5407 set_buffer_internal (oldbuf);
5408 if (NILP (tem))
5409 windows_or_buffers_changed = old_windows_or_buffers_changed;
5410 message_log_need_newline = !nlflag;
5411 Vdeactivate_mark = old_deactivate_mark;
5412 }
5413 }
5414
5415
5416 /* We are at the end of the buffer after just having inserted a newline.
5417 (Note: We depend on the fact we won't be crossing the gap.)
5418 Check to see if the most recent message looks a lot like the previous one.
5419 Return 0 if different, 1 if the new one should just replace it, or a
5420 value N > 1 if we should also append " [N times]". */
5421
5422 static int
5423 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5424 int prev_bol, this_bol;
5425 int prev_bol_byte, this_bol_byte;
5426 {
5427 int i;
5428 int len = Z_BYTE - 1 - this_bol_byte;
5429 int seen_dots = 0;
5430 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5431 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5432
5433 for (i = 0; i < len; i++)
5434 {
5435 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5436 seen_dots = 1;
5437 if (p1[i] != p2[i])
5438 return seen_dots;
5439 }
5440 p1 += len;
5441 if (*p1 == '\n')
5442 return 2;
5443 if (*p1++ == ' ' && *p1++ == '[')
5444 {
5445 int n = 0;
5446 while (*p1 >= '0' && *p1 <= '9')
5447 n = n * 10 + *p1++ - '0';
5448 if (strncmp (p1, " times]\n", 8) == 0)
5449 return n+1;
5450 }
5451 return 0;
5452 }
5453
5454
5455 /* Display an echo area message M with a specified length of LEN
5456 chars. The string may include null characters. If M is 0, clear
5457 out any existing message, and let the mini-buffer text show through.
5458
5459 The buffer M must continue to exist until after the echo area gets
5460 cleared or some other message gets displayed there. This means do
5461 not pass text that is stored in a Lisp string; do not pass text in
5462 a buffer that was alloca'd. */
5463
5464 void
5465 message2 (m, len, multibyte)
5466 char *m;
5467 int len;
5468 int multibyte;
5469 {
5470 /* First flush out any partial line written with print. */
5471 message_log_maybe_newline ();
5472 if (m)
5473 message_dolog (m, len, 1, multibyte);
5474 message2_nolog (m, len, multibyte);
5475 }
5476
5477
5478 /* The non-logging counterpart of message2. */
5479
5480 void
5481 message2_nolog (m, len, multibyte)
5482 char *m;
5483 int len;
5484 {
5485 struct frame *sf = SELECTED_FRAME ();
5486 message_enable_multibyte = multibyte;
5487
5488 if (noninteractive)
5489 {
5490 if (noninteractive_need_newline)
5491 putc ('\n', stderr);
5492 noninteractive_need_newline = 0;
5493 if (m)
5494 fwrite (m, len, 1, stderr);
5495 if (cursor_in_echo_area == 0)
5496 fprintf (stderr, "\n");
5497 fflush (stderr);
5498 }
5499 /* A null message buffer means that the frame hasn't really been
5500 initialized yet. Error messages get reported properly by
5501 cmd_error, so this must be just an informative message; toss it. */
5502 else if (INTERACTIVE
5503 && sf->glyphs_initialized_p
5504 && FRAME_MESSAGE_BUF (sf))
5505 {
5506 Lisp_Object mini_window;
5507 struct frame *f;
5508
5509 /* Get the frame containing the mini-buffer
5510 that the selected frame is using. */
5511 mini_window = FRAME_MINIBUF_WINDOW (sf);
5512 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5513
5514 FRAME_SAMPLE_VISIBILITY (f);
5515 if (FRAME_VISIBLE_P (sf)
5516 && ! FRAME_VISIBLE_P (f))
5517 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5518
5519 if (m)
5520 {
5521 set_message (m, Qnil, len, multibyte);
5522 if (minibuffer_auto_raise)
5523 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5524 }
5525 else
5526 clear_message (1, 1);
5527
5528 do_pending_window_change (0);
5529 echo_area_display (1);
5530 do_pending_window_change (0);
5531 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5532 (*frame_up_to_date_hook) (f);
5533 }
5534 }
5535
5536
5537 /* Display an echo area message M with a specified length of NBYTES
5538 bytes. The string may include null characters. If M is not a
5539 string, clear out any existing message, and let the mini-buffer
5540 text show through. */
5541
5542 void
5543 message3 (m, nbytes, multibyte)
5544 Lisp_Object m;
5545 int nbytes;
5546 int multibyte;
5547 {
5548 struct gcpro gcpro1;
5549
5550 GCPRO1 (m);
5551
5552 /* First flush out any partial line written with print. */
5553 message_log_maybe_newline ();
5554 if (STRINGP (m))
5555 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5556 message3_nolog (m, nbytes, multibyte);
5557
5558 UNGCPRO;
5559 }
5560
5561
5562 /* The non-logging version of message3. */
5563
5564 void
5565 message3_nolog (m, nbytes, multibyte)
5566 Lisp_Object m;
5567 int nbytes, multibyte;
5568 {
5569 struct frame *sf = SELECTED_FRAME ();
5570 message_enable_multibyte = multibyte;
5571
5572 if (noninteractive)
5573 {
5574 if (noninteractive_need_newline)
5575 putc ('\n', stderr);
5576 noninteractive_need_newline = 0;
5577 if (STRINGP (m))
5578 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5579 if (cursor_in_echo_area == 0)
5580 fprintf (stderr, "\n");
5581 fflush (stderr);
5582 }
5583 /* A null message buffer means that the frame hasn't really been
5584 initialized yet. Error messages get reported properly by
5585 cmd_error, so this must be just an informative message; toss it. */
5586 else if (INTERACTIVE
5587 && sf->glyphs_initialized_p
5588 && FRAME_MESSAGE_BUF (sf))
5589 {
5590 Lisp_Object mini_window;
5591 Lisp_Object frame;
5592 struct frame *f;
5593
5594 /* Get the frame containing the mini-buffer
5595 that the selected frame is using. */
5596 mini_window = FRAME_MINIBUF_WINDOW (sf);
5597 frame = XWINDOW (mini_window)->frame;
5598 f = XFRAME (frame);
5599
5600 FRAME_SAMPLE_VISIBILITY (f);
5601 if (FRAME_VISIBLE_P (sf)
5602 && !FRAME_VISIBLE_P (f))
5603 Fmake_frame_visible (frame);
5604
5605 if (STRINGP (m) && XSTRING (m)->size)
5606 {
5607 set_message (NULL, m, nbytes, multibyte);
5608 if (minibuffer_auto_raise)
5609 Fraise_frame (frame);
5610 }
5611 else
5612 clear_message (1, 1);
5613
5614 do_pending_window_change (0);
5615 echo_area_display (1);
5616 do_pending_window_change (0);
5617 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5618 (*frame_up_to_date_hook) (f);
5619 }
5620 }
5621
5622
5623 /* Display a null-terminated echo area message M. If M is 0, clear
5624 out any existing message, and let the mini-buffer text show through.
5625
5626 The buffer M must continue to exist until after the echo area gets
5627 cleared or some other message gets displayed there. Do not pass
5628 text that is stored in a Lisp string. Do not pass text in a buffer
5629 that was alloca'd. */
5630
5631 void
5632 message1 (m)
5633 char *m;
5634 {
5635 message2 (m, (m ? strlen (m) : 0), 0);
5636 }
5637
5638
5639 /* The non-logging counterpart of message1. */
5640
5641 void
5642 message1_nolog (m)
5643 char *m;
5644 {
5645 message2_nolog (m, (m ? strlen (m) : 0), 0);
5646 }
5647
5648 /* Display a message M which contains a single %s
5649 which gets replaced with STRING. */
5650
5651 void
5652 message_with_string (m, string, log)
5653 char *m;
5654 Lisp_Object string;
5655 int log;
5656 {
5657 if (noninteractive)
5658 {
5659 if (m)
5660 {
5661 if (noninteractive_need_newline)
5662 putc ('\n', stderr);
5663 noninteractive_need_newline = 0;
5664 fprintf (stderr, m, XSTRING (string)->data);
5665 if (cursor_in_echo_area == 0)
5666 fprintf (stderr, "\n");
5667 fflush (stderr);
5668 }
5669 }
5670 else if (INTERACTIVE)
5671 {
5672 /* The frame whose minibuffer we're going to display the message on.
5673 It may be larger than the selected frame, so we need
5674 to use its buffer, not the selected frame's buffer. */
5675 Lisp_Object mini_window;
5676 struct frame *f, *sf = SELECTED_FRAME ();
5677
5678 /* Get the frame containing the minibuffer
5679 that the selected frame is using. */
5680 mini_window = FRAME_MINIBUF_WINDOW (sf);
5681 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5682
5683 /* A null message buffer means that the frame hasn't really been
5684 initialized yet. Error messages get reported properly by
5685 cmd_error, so this must be just an informative message; toss it. */
5686 if (FRAME_MESSAGE_BUF (f))
5687 {
5688 int len;
5689 char *a[1];
5690 a[0] = (char *) XSTRING (string)->data;
5691
5692 len = doprnt (FRAME_MESSAGE_BUF (f),
5693 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5694
5695 if (log)
5696 message2 (FRAME_MESSAGE_BUF (f), len,
5697 STRING_MULTIBYTE (string));
5698 else
5699 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5700 STRING_MULTIBYTE (string));
5701
5702 /* Print should start at the beginning of the message
5703 buffer next time. */
5704 message_buf_print = 0;
5705 }
5706 }
5707 }
5708
5709
5710 /* Dump an informative message to the minibuf. If M is 0, clear out
5711 any existing message, and let the mini-buffer text show through. */
5712
5713 /* VARARGS 1 */
5714 void
5715 message (m, a1, a2, a3)
5716 char *m;
5717 EMACS_INT a1, a2, a3;
5718 {
5719 if (noninteractive)
5720 {
5721 if (m)
5722 {
5723 if (noninteractive_need_newline)
5724 putc ('\n', stderr);
5725 noninteractive_need_newline = 0;
5726 fprintf (stderr, m, a1, a2, a3);
5727 if (cursor_in_echo_area == 0)
5728 fprintf (stderr, "\n");
5729 fflush (stderr);
5730 }
5731 }
5732 else if (INTERACTIVE)
5733 {
5734 /* The frame whose mini-buffer we're going to display the message
5735 on. It may be larger than the selected frame, so we need to
5736 use its buffer, not the selected frame's buffer. */
5737 Lisp_Object mini_window;
5738 struct frame *f, *sf = SELECTED_FRAME ();
5739
5740 /* Get the frame containing the mini-buffer
5741 that the selected frame is using. */
5742 mini_window = FRAME_MINIBUF_WINDOW (sf);
5743 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5744
5745 /* A null message buffer means that the frame hasn't really been
5746 initialized yet. Error messages get reported properly by
5747 cmd_error, so this must be just an informative message; toss
5748 it. */
5749 if (FRAME_MESSAGE_BUF (f))
5750 {
5751 if (m)
5752 {
5753 int len;
5754 #ifdef NO_ARG_ARRAY
5755 char *a[3];
5756 a[0] = (char *) a1;
5757 a[1] = (char *) a2;
5758 a[2] = (char *) a3;
5759
5760 len = doprnt (FRAME_MESSAGE_BUF (f),
5761 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5762 #else
5763 len = doprnt (FRAME_MESSAGE_BUF (f),
5764 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5765 (char **) &a1);
5766 #endif /* NO_ARG_ARRAY */
5767
5768 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5769 }
5770 else
5771 message1 (0);
5772
5773 /* Print should start at the beginning of the message
5774 buffer next time. */
5775 message_buf_print = 0;
5776 }
5777 }
5778 }
5779
5780
5781 /* The non-logging version of message. */
5782
5783 void
5784 message_nolog (m, a1, a2, a3)
5785 char *m;
5786 EMACS_INT a1, a2, a3;
5787 {
5788 Lisp_Object old_log_max;
5789 old_log_max = Vmessage_log_max;
5790 Vmessage_log_max = Qnil;
5791 message (m, a1, a2, a3);
5792 Vmessage_log_max = old_log_max;
5793 }
5794
5795
5796 /* Display the current message in the current mini-buffer. This is
5797 only called from error handlers in process.c, and is not time
5798 critical. */
5799
5800 void
5801 update_echo_area ()
5802 {
5803 if (!NILP (echo_area_buffer[0]))
5804 {
5805 Lisp_Object string;
5806 string = Fcurrent_message ();
5807 message3 (string, XSTRING (string)->size,
5808 !NILP (current_buffer->enable_multibyte_characters));
5809 }
5810 }
5811
5812
5813 /* Make sure echo area buffers in echo_buffers[] are life. If they
5814 aren't, make new ones. */
5815
5816 static void
5817 ensure_echo_area_buffers ()
5818 {
5819 int i;
5820
5821 for (i = 0; i < 2; ++i)
5822 if (!BUFFERP (echo_buffer[i])
5823 || NILP (XBUFFER (echo_buffer[i])->name))
5824 {
5825 char name[30];
5826 Lisp_Object old_buffer;
5827 int j;
5828
5829 old_buffer = echo_buffer[i];
5830 sprintf (name, " *Echo Area %d*", i);
5831 echo_buffer[i] = Fget_buffer_create (build_string (name));
5832 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
5833
5834 for (j = 0; j < 2; ++j)
5835 if (EQ (old_buffer, echo_area_buffer[j]))
5836 echo_area_buffer[j] = echo_buffer[i];
5837 }
5838 }
5839
5840
5841 /* Call FN with args A1..A4 with either the current or last displayed
5842 echo_area_buffer as current buffer.
5843
5844 WHICH zero means use the current message buffer
5845 echo_area_buffer[0]. If that is nil, choose a suitable buffer
5846 from echo_buffer[] and clear it.
5847
5848 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
5849 suitable buffer from echo_buffer[] and clear it.
5850
5851 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
5852 that the current message becomes the last displayed one, make
5853 choose a suitable buffer for echo_area_buffer[0], and clear it.
5854
5855 Value is what FN returns. */
5856
5857 static int
5858 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
5859 struct window *w;
5860 int which;
5861 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
5862 EMACS_INT a1;
5863 Lisp_Object a2;
5864 EMACS_INT a3, a4;
5865 {
5866 Lisp_Object buffer;
5867 int this_one, the_other, clear_buffer_p, rc;
5868 int count = BINDING_STACK_SIZE ();
5869
5870 /* If buffers aren't life, make new ones. */
5871 ensure_echo_area_buffers ();
5872
5873 clear_buffer_p = 0;
5874
5875 if (which == 0)
5876 this_one = 0, the_other = 1;
5877 else if (which > 0)
5878 this_one = 1, the_other = 0;
5879 else
5880 {
5881 this_one = 0, the_other = 1;
5882 clear_buffer_p = 1;
5883
5884 /* We need a fresh one in case the current echo buffer equals
5885 the one containing the last displayed echo area message. */
5886 if (!NILP (echo_area_buffer[this_one])
5887 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
5888 echo_area_buffer[this_one] = Qnil;
5889 }
5890
5891 /* Choose a suitable buffer from echo_buffer[] is we don't
5892 have one. */
5893 if (NILP (echo_area_buffer[this_one]))
5894 {
5895 echo_area_buffer[this_one]
5896 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
5897 ? echo_buffer[the_other]
5898 : echo_buffer[this_one]);
5899 clear_buffer_p = 1;
5900 }
5901
5902 buffer = echo_area_buffer[this_one];
5903
5904 record_unwind_protect (unwind_with_echo_area_buffer,
5905 with_echo_area_buffer_unwind_data (w));
5906
5907 /* Make the echo area buffer current. Note that for display
5908 purposes, it is not necessary that the displayed window's buffer
5909 == current_buffer, except for text property lookup. So, let's
5910 only set that buffer temporarily here without doing a full
5911 Fset_window_buffer. We must also change w->pointm, though,
5912 because otherwise an assertions in unshow_buffer fails, and Emacs
5913 aborts. */
5914 set_buffer_internal_1 (XBUFFER (buffer));
5915 if (w)
5916 {
5917 w->buffer = buffer;
5918 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
5919 }
5920
5921 current_buffer->undo_list = Qt;
5922 current_buffer->read_only = Qnil;
5923
5924 if (clear_buffer_p && Z > BEG)
5925 del_range (BEG, Z);
5926
5927 xassert (BEGV >= BEG);
5928 xassert (ZV <= Z && ZV >= BEGV);
5929
5930 rc = fn (a1, a2, a3, a4);
5931
5932 xassert (BEGV >= BEG);
5933 xassert (ZV <= Z && ZV >= BEGV);
5934
5935 unbind_to (count, Qnil);
5936 return rc;
5937 }
5938
5939
5940 /* Save state that should be preserved around the call to the function
5941 FN called in with_echo_area_buffer. */
5942
5943 static Lisp_Object
5944 with_echo_area_buffer_unwind_data (w)
5945 struct window *w;
5946 {
5947 int i = 0;
5948 Lisp_Object vector;
5949
5950 /* Reduce consing by keeping one vector in
5951 Vwith_echo_area_save_vector. */
5952 vector = Vwith_echo_area_save_vector;
5953 Vwith_echo_area_save_vector = Qnil;
5954
5955 if (NILP (vector))
5956 vector = Fmake_vector (make_number (7), Qnil);
5957
5958 XSETBUFFER (XVECTOR (vector)->contents[i], current_buffer); ++i;
5959 XVECTOR (vector)->contents[i++] = Vdeactivate_mark;
5960 XVECTOR (vector)->contents[i++] = make_number (windows_or_buffers_changed);
5961
5962 if (w)
5963 {
5964 XSETWINDOW (XVECTOR (vector)->contents[i], w); ++i;
5965 XVECTOR (vector)->contents[i++] = w->buffer;
5966 XVECTOR (vector)->contents[i++]
5967 = make_number (XMARKER (w->pointm)->charpos);
5968 XVECTOR (vector)->contents[i++]
5969 = make_number (XMARKER (w->pointm)->bytepos);
5970 }
5971 else
5972 {
5973 int end = i + 4;
5974 while (i < end)
5975 XVECTOR (vector)->contents[i++] = Qnil;
5976 }
5977
5978 xassert (i == XVECTOR (vector)->size);
5979 return vector;
5980 }
5981
5982
5983 /* Restore global state from VECTOR which was created by
5984 with_echo_area_buffer_unwind_data. */
5985
5986 static Lisp_Object
5987 unwind_with_echo_area_buffer (vector)
5988 Lisp_Object vector;
5989 {
5990 int i = 0;
5991
5992 set_buffer_internal_1 (XBUFFER (XVECTOR (vector)->contents[i])); ++i;
5993 Vdeactivate_mark = XVECTOR (vector)->contents[i]; ++i;
5994 windows_or_buffers_changed = XFASTINT (XVECTOR (vector)->contents[i]); ++i;
5995
5996 if (WINDOWP (XVECTOR (vector)->contents[i]))
5997 {
5998 struct window *w;
5999 Lisp_Object buffer, charpos, bytepos;
6000
6001 w = XWINDOW (XVECTOR (vector)->contents[i]); ++i;
6002 buffer = XVECTOR (vector)->contents[i]; ++i;
6003 charpos = XVECTOR (vector)->contents[i]; ++i;
6004 bytepos = XVECTOR (vector)->contents[i]; ++i;
6005
6006 w->buffer = buffer;
6007 set_marker_both (w->pointm, buffer,
6008 XFASTINT (charpos), XFASTINT (bytepos));
6009 }
6010
6011 Vwith_echo_area_save_vector = vector;
6012 return Qnil;
6013 }
6014
6015
6016 /* Set up the echo area for use by print functions. MULTIBYTE_P
6017 non-zero means we will print multibyte. */
6018
6019 void
6020 setup_echo_area_for_printing (multibyte_p)
6021 int multibyte_p;
6022 {
6023 ensure_echo_area_buffers ();
6024
6025 if (!message_buf_print)
6026 {
6027 /* A message has been output since the last time we printed.
6028 Choose a fresh echo area buffer. */
6029 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6030 echo_area_buffer[0] = echo_buffer[1];
6031 else
6032 echo_area_buffer[0] = echo_buffer[0];
6033
6034 /* Switch to that buffer and clear it. */
6035 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6036 if (Z > BEG)
6037 del_range (BEG, Z);
6038 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6039
6040 /* Set up the buffer for the multibyteness we need. */
6041 if (multibyte_p
6042 != !NILP (current_buffer->enable_multibyte_characters))
6043 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6044
6045 /* Raise the frame containing the echo area. */
6046 if (minibuffer_auto_raise)
6047 {
6048 struct frame *sf = SELECTED_FRAME ();
6049 Lisp_Object mini_window;
6050 mini_window = FRAME_MINIBUF_WINDOW (sf);
6051 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6052 }
6053
6054 message_log_maybe_newline ();
6055 message_buf_print = 1;
6056 }
6057 else
6058 {
6059 if (NILP (echo_area_buffer[0]))
6060 {
6061 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6062 echo_area_buffer[0] = echo_buffer[1];
6063 else
6064 echo_area_buffer[0] = echo_buffer[0];
6065 }
6066
6067 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6068 /* Someone switched buffers between print requests. */
6069 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6070 }
6071 }
6072
6073
6074 /* Display an echo area message in window W. Value is non-zero if W's
6075 height is changed. If display_last_displayed_message_p is
6076 non-zero, display the message that was last displayed, otherwise
6077 display the current message. */
6078
6079 static int
6080 display_echo_area (w)
6081 struct window *w;
6082 {
6083 int i, no_message_p, window_height_changed_p, count;
6084
6085 /* Temporarily disable garbage collections while displaying the echo
6086 area. This is done because a GC can print a message itself.
6087 That message would modify the echo area buffer's contents while a
6088 redisplay of the buffer is going on, and seriously confuse
6089 redisplay. */
6090 count = inhibit_garbage_collection ();
6091
6092 /* If there is no message, we must call display_echo_area_1
6093 nevertheless because it resizes the window. But we will have to
6094 reset the echo_area_buffer in question to nil at the end because
6095 with_echo_area_buffer will sets it to an empty buffer. */
6096 i = display_last_displayed_message_p ? 1 : 0;
6097 no_message_p = NILP (echo_area_buffer[i]);
6098
6099 window_height_changed_p
6100 = with_echo_area_buffer (w, display_last_displayed_message_p,
6101 display_echo_area_1,
6102 (EMACS_INT) w, Qnil, 0, 0);
6103
6104 if (no_message_p)
6105 echo_area_buffer[i] = Qnil;
6106
6107 unbind_to (count, Qnil);
6108 return window_height_changed_p;
6109 }
6110
6111
6112 /* Helper for display_echo_area. Display the current buffer which
6113 contains the current echo area message in window W, a mini-window,
6114 a pointer to which is passed in A1. A2..A4 are currently not used.
6115 Change the height of W so that all of the message is displayed.
6116 Value is non-zero if height of W was changed. */
6117
6118 static int
6119 display_echo_area_1 (a1, a2, a3, a4)
6120 EMACS_INT a1;
6121 Lisp_Object a2;
6122 EMACS_INT a3, a4;
6123 {
6124 struct window *w = (struct window *) a1;
6125 Lisp_Object window;
6126 struct text_pos start;
6127 int window_height_changed_p = 0;
6128
6129 /* Do this before displaying, so that we have a large enough glyph
6130 matrix for the display. */
6131 window_height_changed_p = resize_mini_window (w, 0);
6132
6133 /* Display. */
6134 clear_glyph_matrix (w->desired_matrix);
6135 XSETWINDOW (window, w);
6136 SET_TEXT_POS (start, BEG, BEG_BYTE);
6137 try_window (window, start);
6138
6139 return window_height_changed_p;
6140 }
6141
6142
6143 /* Resize the echo area window to exactly the size needed for the
6144 currently displayed message, if there is one. */
6145
6146 void
6147 resize_echo_area_axactly ()
6148 {
6149 if (BUFFERP (echo_area_buffer[0])
6150 && WINDOWP (echo_area_window))
6151 {
6152 struct window *w = XWINDOW (echo_area_window);
6153 int resized_p;
6154
6155 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6156 (EMACS_INT) w, Qnil, 0, 0);
6157 if (resized_p)
6158 {
6159 ++windows_or_buffers_changed;
6160 ++update_mode_lines;
6161 redisplay_internal (0);
6162 }
6163 }
6164 }
6165
6166
6167 /* Callback function for with_echo_area_buffer, when used from
6168 resize_echo_area_axactly. A1 contains a pointer to the window to
6169 resize, A2 to A4 are not used. Value is what resize_mini_window
6170 returns. */
6171
6172 static int
6173 resize_mini_window_1 (a1, a2, a3, a4)
6174 EMACS_INT a1;
6175 Lisp_Object a2;
6176 EMACS_INT a3, a4;
6177 {
6178 return resize_mini_window ((struct window *) a1, 1);
6179 }
6180
6181
6182 /* Resize mini-window W to fit the size of its contents. EXACT:P
6183 means size the window exactly to the size needed. Otherwise, it's
6184 only enlarged until W's buffer is empty. Value is non-zero if
6185 the window height has been changed. */
6186
6187 int
6188 resize_mini_window (w, exact_p)
6189 struct window *w;
6190 int exact_p;
6191 {
6192 struct frame *f = XFRAME (w->frame);
6193 int window_height_changed_p = 0;
6194
6195 xassert (MINI_WINDOW_P (w));
6196
6197 /* Nil means don't try to resize. */
6198 if (NILP (Vresize_mini_windows)
6199 || (FRAME_X_P (f) && f->output_data.x == NULL))
6200 return 0;
6201
6202 if (!FRAME_MINIBUF_ONLY_P (f))
6203 {
6204 struct it it;
6205 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6206 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6207 int height, max_height;
6208 int unit = CANON_Y_UNIT (f);
6209 struct text_pos start;
6210 struct buffer *old_current_buffer = NULL;
6211
6212 if (current_buffer != XBUFFER (w->buffer))
6213 {
6214 old_current_buffer = current_buffer;
6215 set_buffer_internal (XBUFFER (w->buffer));
6216 }
6217
6218 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6219
6220 /* Compute the max. number of lines specified by the user. */
6221 if (FLOATP (Vmax_mini_window_height))
6222 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6223 else if (INTEGERP (Vmax_mini_window_height))
6224 max_height = XINT (Vmax_mini_window_height);
6225 else
6226 max_height = total_height / 4;
6227
6228 /* Correct that max. height if it's bogus. */
6229 max_height = max (1, max_height);
6230 max_height = min (total_height, max_height);
6231
6232 /* Find out the height of the text in the window. */
6233 if (it.truncate_lines_p)
6234 height = 1;
6235 else
6236 {
6237 last_height = 0;
6238 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6239 if (it.max_ascent == 0 && it.max_descent == 0)
6240 height = it.current_y + last_height;
6241 else
6242 height = it.current_y + it.max_ascent + it.max_descent;
6243 height -= it.extra_line_spacing;
6244 height = (height + unit - 1) / unit;
6245 }
6246
6247 /* Compute a suitable window start. */
6248 if (height > max_height)
6249 {
6250 height = max_height;
6251 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6252 move_it_vertically_backward (&it, (height - 1) * unit);
6253 start = it.current.pos;
6254 }
6255 else
6256 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6257 SET_MARKER_FROM_TEXT_POS (w->start, start);
6258
6259 if (EQ (Vresize_mini_windows, Qgrow_only))
6260 {
6261 /* Let it grow only, until we display an empty message, in which
6262 case the window shrinks again. */
6263 if (height > XFASTINT (w->height))
6264 {
6265 int old_height = XFASTINT (w->height);
6266 freeze_window_starts (f, 1);
6267 grow_mini_window (w, height - XFASTINT (w->height));
6268 window_height_changed_p = XFASTINT (w->height) != old_height;
6269 }
6270 else if (height < XFASTINT (w->height)
6271 && (exact_p || BEGV == ZV))
6272 {
6273 int old_height = XFASTINT (w->height);
6274 freeze_window_starts (f, 0);
6275 shrink_mini_window (w);
6276 window_height_changed_p = XFASTINT (w->height) != old_height;
6277 }
6278 }
6279 else
6280 {
6281 /* Always resize to exact size needed. */
6282 if (height > XFASTINT (w->height))
6283 {
6284 int old_height = XFASTINT (w->height);
6285 freeze_window_starts (f, 1);
6286 grow_mini_window (w, height - XFASTINT (w->height));
6287 window_height_changed_p = XFASTINT (w->height) != old_height;
6288 }
6289 else if (height < XFASTINT (w->height))
6290 {
6291 int old_height = XFASTINT (w->height);
6292 freeze_window_starts (f, 0);
6293 shrink_mini_window (w);
6294
6295 if (height)
6296 {
6297 freeze_window_starts (f, 1);
6298 grow_mini_window (w, height - XFASTINT (w->height));
6299 }
6300
6301 window_height_changed_p = XFASTINT (w->height) != old_height;
6302 }
6303 }
6304
6305 if (old_current_buffer)
6306 set_buffer_internal (old_current_buffer);
6307 }
6308
6309 return window_height_changed_p;
6310 }
6311
6312
6313 /* Value is the current message, a string, or nil if there is no
6314 current message. */
6315
6316 Lisp_Object
6317 current_message ()
6318 {
6319 Lisp_Object msg;
6320
6321 if (NILP (echo_area_buffer[0]))
6322 msg = Qnil;
6323 else
6324 {
6325 with_echo_area_buffer (0, 0, current_message_1,
6326 (EMACS_INT) &msg, Qnil, 0, 0);
6327 if (NILP (msg))
6328 echo_area_buffer[0] = Qnil;
6329 }
6330
6331 return msg;
6332 }
6333
6334
6335 static int
6336 current_message_1 (a1, a2, a3, a4)
6337 EMACS_INT a1;
6338 Lisp_Object a2;
6339 EMACS_INT a3, a4;
6340 {
6341 Lisp_Object *msg = (Lisp_Object *) a1;
6342
6343 if (Z > BEG)
6344 *msg = make_buffer_string (BEG, Z, 1);
6345 else
6346 *msg = Qnil;
6347 return 0;
6348 }
6349
6350
6351 /* Push the current message on Vmessage_stack for later restauration
6352 by restore_message. Value is non-zero if the current message isn't
6353 empty. This is a relatively infrequent operation, so it's not
6354 worth optimizing. */
6355
6356 int
6357 push_message ()
6358 {
6359 Lisp_Object msg;
6360 msg = current_message ();
6361 Vmessage_stack = Fcons (msg, Vmessage_stack);
6362 return STRINGP (msg);
6363 }
6364
6365
6366 /* Restore message display from the top of Vmessage_stack. */
6367
6368 void
6369 restore_message ()
6370 {
6371 Lisp_Object msg;
6372
6373 xassert (CONSP (Vmessage_stack));
6374 msg = XCAR (Vmessage_stack);
6375 if (STRINGP (msg))
6376 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6377 else
6378 message3_nolog (msg, 0, 0);
6379 }
6380
6381
6382 /* Pop the top-most entry off Vmessage_stack. */
6383
6384 void
6385 pop_message ()
6386 {
6387 xassert (CONSP (Vmessage_stack));
6388 Vmessage_stack = XCDR (Vmessage_stack);
6389 }
6390
6391
6392 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6393 exits. If the stack is not empty, we have a missing pop_message
6394 somewhere. */
6395
6396 void
6397 check_message_stack ()
6398 {
6399 if (!NILP (Vmessage_stack))
6400 abort ();
6401 }
6402
6403
6404 /* Truncate to NCHARS what will be displayed in the echo area the next
6405 time we display it---but don't redisplay it now. */
6406
6407 void
6408 truncate_echo_area (nchars)
6409 int nchars;
6410 {
6411 if (nchars == 0)
6412 echo_area_buffer[0] = Qnil;
6413 /* A null message buffer means that the frame hasn't really been
6414 initialized yet. Error messages get reported properly by
6415 cmd_error, so this must be just an informative message; toss it. */
6416 else if (!noninteractive
6417 && INTERACTIVE
6418 && !NILP (echo_area_buffer[0]))
6419 {
6420 struct frame *sf = SELECTED_FRAME ();
6421 if (FRAME_MESSAGE_BUF (sf))
6422 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6423 }
6424 }
6425
6426
6427 /* Helper function for truncate_echo_area. Truncate the current
6428 message to at most NCHARS characters. */
6429
6430 static int
6431 truncate_message_1 (nchars, a2, a3, a4)
6432 EMACS_INT nchars;
6433 Lisp_Object a2;
6434 EMACS_INT a3, a4;
6435 {
6436 if (BEG + nchars < Z)
6437 del_range (BEG + nchars, Z);
6438 if (Z == BEG)
6439 echo_area_buffer[0] = Qnil;
6440 return 0;
6441 }
6442
6443
6444 /* Set the current message to a substring of S or STRING.
6445
6446 If STRING is a Lisp string, set the message to the first NBYTES
6447 bytes from STRING. NBYTES zero means use the whole string. If
6448 STRING is multibyte, the message will be displayed multibyte.
6449
6450 If S is not null, set the message to the first LEN bytes of S. LEN
6451 zero means use the whole string. MULTIBYTE_P non-zero means S is
6452 multibyte. Display the message multibyte in that case. */
6453
6454 void
6455 set_message (s, string, nbytes, multibyte_p)
6456 char *s;
6457 Lisp_Object string;
6458 int nbytes;
6459 {
6460 message_enable_multibyte
6461 = ((s && multibyte_p)
6462 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6463
6464 with_echo_area_buffer (0, -1, set_message_1,
6465 (EMACS_INT) s, string, nbytes, multibyte_p);
6466 message_buf_print = 0;
6467 help_echo_showing_p = 0;
6468 }
6469
6470
6471 /* Helper function for set_message. Arguments have the same meaning
6472 as there, with A1 corresponding to S and A2 corresponding to STRING
6473 This function is called with the echo area buffer being
6474 current. */
6475
6476 static int
6477 set_message_1 (a1, a2, nbytes, multibyte_p)
6478 EMACS_INT a1;
6479 Lisp_Object a2;
6480 EMACS_INT nbytes, multibyte_p;
6481 {
6482 char *s = (char *) a1;
6483 Lisp_Object string = a2;
6484
6485 xassert (BEG == Z);
6486
6487 /* Change multibyteness of the echo buffer appropriately. */
6488 if (message_enable_multibyte
6489 != !NILP (current_buffer->enable_multibyte_characters))
6490 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6491
6492 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6493
6494 /* Insert new message at BEG. */
6495 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6496
6497 if (STRINGP (string))
6498 {
6499 int nchars;
6500
6501 if (nbytes == 0)
6502 nbytes = XSTRING (string)->size_byte;
6503 nchars = string_byte_to_char (string, nbytes);
6504
6505 /* This function takes care of single/multibyte conversion. We
6506 just have to ensure that the echo area buffer has the right
6507 setting of enable_multibyte_characters. */
6508 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6509 }
6510 else if (s)
6511 {
6512 if (nbytes == 0)
6513 nbytes = strlen (s);
6514
6515 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6516 {
6517 /* Convert from multi-byte to single-byte. */
6518 int i, c, n;
6519 unsigned char work[1];
6520
6521 /* Convert a multibyte string to single-byte. */
6522 for (i = 0; i < nbytes; i += n)
6523 {
6524 c = string_char_and_length (s + i, nbytes - i, &n);
6525 work[0] = (SINGLE_BYTE_CHAR_P (c)
6526 ? c
6527 : multibyte_char_to_unibyte (c, Qnil));
6528 insert_1_both (work, 1, 1, 1, 0, 0);
6529 }
6530 }
6531 else if (!multibyte_p
6532 && !NILP (current_buffer->enable_multibyte_characters))
6533 {
6534 /* Convert from single-byte to multi-byte. */
6535 int i, c, n;
6536 unsigned char *msg = (unsigned char *) s;
6537 unsigned char str[MAX_MULTIBYTE_LENGTH];
6538
6539 /* Convert a single-byte string to multibyte. */
6540 for (i = 0; i < nbytes; i++)
6541 {
6542 c = unibyte_char_to_multibyte (msg[i]);
6543 n = CHAR_STRING (c, str);
6544 insert_1_both (str, 1, n, 1, 0, 0);
6545 }
6546 }
6547 else
6548 insert_1 (s, nbytes, 1, 0, 0);
6549 }
6550
6551 return 0;
6552 }
6553
6554
6555 /* Clear messages. CURRENT_P non-zero means clear the current
6556 message. LAST_DISPLAYED_P non-zero means clear the message
6557 last displayed. */
6558
6559 void
6560 clear_message (current_p, last_displayed_p)
6561 int current_p, last_displayed_p;
6562 {
6563 if (current_p)
6564 echo_area_buffer[0] = Qnil;
6565
6566 if (last_displayed_p)
6567 echo_area_buffer[1] = Qnil;
6568
6569 message_buf_print = 0;
6570 }
6571
6572 /* Clear garbaged frames.
6573
6574 This function is used where the old redisplay called
6575 redraw_garbaged_frames which in turn called redraw_frame which in
6576 turn called clear_frame. The call to clear_frame was a source of
6577 flickering. I believe a clear_frame is not necessary. It should
6578 suffice in the new redisplay to invalidate all current matrices,
6579 and ensure a complete redisplay of all windows. */
6580
6581 static void
6582 clear_garbaged_frames ()
6583 {
6584 if (frame_garbaged)
6585 {
6586 Lisp_Object tail, frame;
6587
6588 FOR_EACH_FRAME (tail, frame)
6589 {
6590 struct frame *f = XFRAME (frame);
6591
6592 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6593 {
6594 clear_current_matrices (f);
6595 f->garbaged = 0;
6596 }
6597 }
6598
6599 frame_garbaged = 0;
6600 ++windows_or_buffers_changed;
6601 }
6602 }
6603
6604
6605 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6606 is non-zero update selected_frame. Value is non-zero if the
6607 mini-windows height has been changed. */
6608
6609 static int
6610 echo_area_display (update_frame_p)
6611 int update_frame_p;
6612 {
6613 Lisp_Object mini_window;
6614 struct window *w;
6615 struct frame *f;
6616 int window_height_changed_p = 0;
6617 struct frame *sf = SELECTED_FRAME ();
6618
6619 mini_window = FRAME_MINIBUF_WINDOW (sf);
6620 w = XWINDOW (mini_window);
6621 f = XFRAME (WINDOW_FRAME (w));
6622
6623 /* Don't display if frame is invisible or not yet initialized. */
6624 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6625 return 0;
6626
6627 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
6628 #ifndef macintosh
6629 #ifdef HAVE_WINDOW_SYSTEM
6630 /* When Emacs starts, selected_frame may be a visible terminal
6631 frame, even if we run under a window system. If we let this
6632 through, a message would be displayed on the terminal. */
6633 if (EQ (selected_frame, Vterminal_frame)
6634 && !NILP (Vwindow_system))
6635 return 0;
6636 #endif /* HAVE_WINDOW_SYSTEM */
6637 #endif
6638
6639 /* Redraw garbaged frames. */
6640 if (frame_garbaged)
6641 clear_garbaged_frames ();
6642
6643 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6644 {
6645 echo_area_window = mini_window;
6646 window_height_changed_p = display_echo_area (w);
6647 w->must_be_updated_p = 1;
6648
6649 /* Update the display, unless called from redisplay_internal.
6650 Also don't update the screen during redisplay itself. The
6651 update will happen at the end of redisplay, and an update
6652 here could cause confusion. */
6653 if (update_frame_p && !redisplaying_p)
6654 {
6655 int n = 0;
6656
6657 /* If the display update has been interrupted by pending
6658 input, update mode lines in the frame. Due to the
6659 pending input, it might have been that redisplay hasn't
6660 been called, so that mode lines above the echo area are
6661 garbaged. This looks odd, so we prevent it here. */
6662 if (!display_completed)
6663 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6664
6665 if (window_height_changed_p)
6666 {
6667 /* Must update other windows. */
6668 windows_or_buffers_changed = 1;
6669 redisplay_internal (0);
6670 }
6671 else if (FRAME_WINDOW_P (f) && n == 0)
6672 {
6673 /* Window configuration is the same as before.
6674 Can do with a display update of the echo area,
6675 unless we displayed some mode lines. */
6676 update_single_window (w, 1);
6677 rif->flush_display (f);
6678 }
6679 else
6680 update_frame (f, 1, 1);
6681
6682 /* If cursor is in the echo area, make sure that the next
6683 redisplay displays the minibuffer, so that the cursor will
6684 be replaced with what the minibuffer wants. */
6685 if (cursor_in_echo_area)
6686 ++windows_or_buffers_changed;
6687 }
6688 }
6689 else if (!EQ (mini_window, selected_window))
6690 windows_or_buffers_changed++;
6691
6692 /* Last displayed message is now the current message. */
6693 echo_area_buffer[1] = echo_area_buffer[0];
6694
6695 /* Prevent redisplay optimization in redisplay_internal by resetting
6696 this_line_start_pos. This is done because the mini-buffer now
6697 displays the message instead of its buffer text. */
6698 if (EQ (mini_window, selected_window))
6699 CHARPOS (this_line_start_pos) = 0;
6700
6701 return window_height_changed_p;
6702 }
6703
6704
6705 \f
6706 /***********************************************************************
6707 Frame Titles
6708 ***********************************************************************/
6709
6710
6711 #ifdef HAVE_WINDOW_SYSTEM
6712
6713 /* A buffer for constructing frame titles in it; allocated from the
6714 heap in init_xdisp and resized as needed in store_frame_title_char. */
6715
6716 static char *frame_title_buf;
6717
6718 /* The buffer's end, and a current output position in it. */
6719
6720 static char *frame_title_buf_end;
6721 static char *frame_title_ptr;
6722
6723
6724 /* Store a single character C for the frame title in frame_title_buf.
6725 Re-allocate frame_title_buf if necessary. */
6726
6727 static void
6728 store_frame_title_char (c)
6729 char c;
6730 {
6731 /* If output position has reached the end of the allocated buffer,
6732 double the buffer's size. */
6733 if (frame_title_ptr == frame_title_buf_end)
6734 {
6735 int len = frame_title_ptr - frame_title_buf;
6736 int new_size = 2 * len * sizeof *frame_title_buf;
6737 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6738 frame_title_buf_end = frame_title_buf + new_size;
6739 frame_title_ptr = frame_title_buf + len;
6740 }
6741
6742 *frame_title_ptr++ = c;
6743 }
6744
6745
6746 /* Store part of a frame title in frame_title_buf, beginning at
6747 frame_title_ptr. STR is the string to store. Do not copy more
6748 than PRECISION number of bytes from STR; PRECISION <= 0 means copy
6749 the whole string. Pad with spaces until FIELD_WIDTH number of
6750 characters have been copied; FIELD_WIDTH <= 0 means don't pad.
6751 Called from display_mode_element when it is used to build a frame
6752 title. */
6753
6754 static int
6755 store_frame_title (str, field_width, precision)
6756 unsigned char *str;
6757 int field_width, precision;
6758 {
6759 int n = 0;
6760
6761 /* Copy at most PRECISION chars from STR. */
6762 while ((precision <= 0 || n < precision)
6763 && *str)
6764 {
6765 store_frame_title_char (*str++);
6766 ++n;
6767 }
6768
6769 /* Fill up with spaces until FIELD_WIDTH reached. */
6770 while (field_width > 0
6771 && n < field_width)
6772 {
6773 store_frame_title_char (' ');
6774 ++n;
6775 }
6776
6777 return n;
6778 }
6779
6780
6781 /* Set the title of FRAME, if it has changed. The title format is
6782 Vicon_title_format if FRAME is iconified, otherwise it is
6783 frame_title_format. */
6784
6785 static void
6786 x_consider_frame_title (frame)
6787 Lisp_Object frame;
6788 {
6789 struct frame *f = XFRAME (frame);
6790
6791 if (FRAME_WINDOW_P (f)
6792 || FRAME_MINIBUF_ONLY_P (f)
6793 || f->explicit_name)
6794 {
6795 /* Do we have more than one visible frame on this X display? */
6796 Lisp_Object tail;
6797 Lisp_Object fmt;
6798 struct buffer *obuf;
6799 int len;
6800 struct it it;
6801
6802 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
6803 {
6804 struct frame *tf = XFRAME (XCAR (tail));
6805
6806 if (tf != f
6807 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
6808 && !FRAME_MINIBUF_ONLY_P (tf)
6809 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
6810 break;
6811 }
6812
6813 /* Set global variable indicating that multiple frames exist. */
6814 multiple_frames = CONSP (tail);
6815
6816 /* Switch to the buffer of selected window of the frame. Set up
6817 frame_title_ptr so that display_mode_element will output into it;
6818 then display the title. */
6819 obuf = current_buffer;
6820 Fset_buffer (XWINDOW (f->selected_window)->buffer);
6821 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
6822 frame_title_ptr = frame_title_buf;
6823 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
6824 NULL, DEFAULT_FACE_ID);
6825 len = display_mode_element (&it, 0, -1, -1, fmt);
6826 frame_title_ptr = NULL;
6827 set_buffer_internal (obuf);
6828
6829 /* Set the title only if it's changed. This avoids consing in
6830 the common case where it hasn't. (If it turns out that we've
6831 already wasted too much time by walking through the list with
6832 display_mode_element, then we might need to optimize at a
6833 higher level than this.) */
6834 if (! STRINGP (f->name)
6835 || STRING_BYTES (XSTRING (f->name)) != len
6836 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
6837 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
6838 }
6839 }
6840
6841 #else /* not HAVE_WINDOW_SYSTEM */
6842
6843 #define frame_title_ptr ((char *)0)
6844 #define store_frame_title(str, mincol, maxcol) 0
6845
6846 #endif /* not HAVE_WINDOW_SYSTEM */
6847
6848
6849
6850 \f
6851 /***********************************************************************
6852 Menu Bars
6853 ***********************************************************************/
6854
6855
6856 /* Prepare for redisplay by updating menu-bar item lists when
6857 appropriate. This can call eval. */
6858
6859 void
6860 prepare_menu_bars ()
6861 {
6862 int all_windows;
6863 struct gcpro gcpro1, gcpro2;
6864 struct frame *f;
6865 Lisp_Object tooltip_frame;
6866
6867 #ifdef HAVE_X_WINDOWS
6868 tooltip_frame = tip_frame;
6869 #else
6870 tooltip_frame = Qnil;
6871 #endif
6872
6873 /* Update all frame titles based on their buffer names, etc. We do
6874 this before the menu bars so that the buffer-menu will show the
6875 up-to-date frame titles. */
6876 #ifdef HAVE_WINDOW_SYSTEM
6877 if (windows_or_buffers_changed || update_mode_lines)
6878 {
6879 Lisp_Object tail, frame;
6880
6881 FOR_EACH_FRAME (tail, frame)
6882 {
6883 f = XFRAME (frame);
6884 if (!EQ (frame, tooltip_frame)
6885 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
6886 x_consider_frame_title (frame);
6887 }
6888 }
6889 #endif /* HAVE_WINDOW_SYSTEM */
6890
6891 /* Update the menu bar item lists, if appropriate. This has to be
6892 done before any actual redisplay or generation of display lines. */
6893 all_windows = (update_mode_lines
6894 || buffer_shared > 1
6895 || windows_or_buffers_changed);
6896 if (all_windows)
6897 {
6898 Lisp_Object tail, frame;
6899 int count = BINDING_STACK_SIZE ();
6900
6901 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
6902
6903 FOR_EACH_FRAME (tail, frame)
6904 {
6905 f = XFRAME (frame);
6906
6907 /* Ignore tooltip frame. */
6908 if (EQ (frame, tooltip_frame))
6909 continue;
6910
6911 /* If a window on this frame changed size, report that to
6912 the user and clear the size-change flag. */
6913 if (FRAME_WINDOW_SIZES_CHANGED (f))
6914 {
6915 Lisp_Object functions;
6916
6917 /* Clear flag first in case we get an error below. */
6918 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
6919 functions = Vwindow_size_change_functions;
6920 GCPRO2 (tail, functions);
6921
6922 while (CONSP (functions))
6923 {
6924 call1 (XCAR (functions), frame);
6925 functions = XCDR (functions);
6926 }
6927 UNGCPRO;
6928 }
6929
6930 GCPRO1 (tail);
6931 update_menu_bar (f, 0);
6932 #ifdef HAVE_WINDOW_SYSTEM
6933 update_tool_bar (f, 0);
6934 #endif
6935 UNGCPRO;
6936 }
6937
6938 unbind_to (count, Qnil);
6939 }
6940 else
6941 {
6942 struct frame *sf = SELECTED_FRAME ();
6943 update_menu_bar (sf, 1);
6944 #ifdef HAVE_WINDOW_SYSTEM
6945 update_tool_bar (sf, 1);
6946 #endif
6947 }
6948
6949 /* Motif needs this. See comment in xmenu.c. Turn it off when
6950 pending_menu_activation is not defined. */
6951 #ifdef USE_X_TOOLKIT
6952 pending_menu_activation = 0;
6953 #endif
6954 }
6955
6956
6957 /* Update the menu bar item list for frame F. This has to be done
6958 before we start to fill in any display lines, because it can call
6959 eval.
6960
6961 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
6962
6963 static void
6964 update_menu_bar (f, save_match_data)
6965 struct frame *f;
6966 int save_match_data;
6967 {
6968 Lisp_Object window;
6969 register struct window *w;
6970
6971 window = FRAME_SELECTED_WINDOW (f);
6972 w = XWINDOW (window);
6973
6974 if (update_mode_lines)
6975 w->update_mode_line = Qt;
6976
6977 if (FRAME_WINDOW_P (f)
6978 ?
6979 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
6980 FRAME_EXTERNAL_MENU_BAR (f)
6981 #else
6982 FRAME_MENU_BAR_LINES (f) > 0
6983 #endif
6984 : FRAME_MENU_BAR_LINES (f) > 0)
6985 {
6986 /* If the user has switched buffers or windows, we need to
6987 recompute to reflect the new bindings. But we'll
6988 recompute when update_mode_lines is set too; that means
6989 that people can use force-mode-line-update to request
6990 that the menu bar be recomputed. The adverse effect on
6991 the rest of the redisplay algorithm is about the same as
6992 windows_or_buffers_changed anyway. */
6993 if (windows_or_buffers_changed
6994 || !NILP (w->update_mode_line)
6995 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
6996 < BUF_MODIFF (XBUFFER (w->buffer)))
6997 != !NILP (w->last_had_star))
6998 || ((!NILP (Vtransient_mark_mode)
6999 && !NILP (XBUFFER (w->buffer)->mark_active))
7000 != !NILP (w->region_showing)))
7001 {
7002 struct buffer *prev = current_buffer;
7003 int count = BINDING_STACK_SIZE ();
7004
7005 set_buffer_internal_1 (XBUFFER (w->buffer));
7006 if (save_match_data)
7007 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7008 if (NILP (Voverriding_local_map_menu_flag))
7009 {
7010 specbind (Qoverriding_terminal_local_map, Qnil);
7011 specbind (Qoverriding_local_map, Qnil);
7012 }
7013
7014 /* Run the Lucid hook. */
7015 call1 (Vrun_hooks, Qactivate_menubar_hook);
7016
7017 /* If it has changed current-menubar from previous value,
7018 really recompute the menu-bar from the value. */
7019 if (! NILP (Vlucid_menu_bar_dirty_flag))
7020 call0 (Qrecompute_lucid_menubar);
7021
7022 safe_run_hooks (Qmenu_bar_update_hook);
7023 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7024
7025 /* Redisplay the menu bar in case we changed it. */
7026 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7027 if (FRAME_WINDOW_P (f)
7028 #if defined (macintosh)
7029 /* All frames on Mac OS share the same menubar. So only the
7030 selected frame should be allowed to set it. */
7031 && f == SELECTED_FRAME ()
7032 #endif
7033 )
7034 set_frame_menubar (f, 0, 0);
7035 else
7036 /* On a terminal screen, the menu bar is an ordinary screen
7037 line, and this makes it get updated. */
7038 w->update_mode_line = Qt;
7039 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7040 /* In the non-toolkit version, the menu bar is an ordinary screen
7041 line, and this makes it get updated. */
7042 w->update_mode_line = Qt;
7043 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7044
7045 unbind_to (count, Qnil);
7046 set_buffer_internal_1 (prev);
7047 }
7048 }
7049 }
7050
7051
7052 \f
7053 /***********************************************************************
7054 Tool-bars
7055 ***********************************************************************/
7056
7057 #ifdef HAVE_WINDOW_SYSTEM
7058
7059 /* Update the tool-bar item list for frame F. This has to be done
7060 before we start to fill in any display lines. Called from
7061 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7062 and restore it here. */
7063
7064 static void
7065 update_tool_bar (f, save_match_data)
7066 struct frame *f;
7067 int save_match_data;
7068 {
7069 if (WINDOWP (f->tool_bar_window)
7070 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
7071 {
7072 Lisp_Object window;
7073 struct window *w;
7074
7075 window = FRAME_SELECTED_WINDOW (f);
7076 w = XWINDOW (window);
7077
7078 /* If the user has switched buffers or windows, we need to
7079 recompute to reflect the new bindings. But we'll
7080 recompute when update_mode_lines is set too; that means
7081 that people can use force-mode-line-update to request
7082 that the menu bar be recomputed. The adverse effect on
7083 the rest of the redisplay algorithm is about the same as
7084 windows_or_buffers_changed anyway. */
7085 if (windows_or_buffers_changed
7086 || !NILP (w->update_mode_line)
7087 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7088 < BUF_MODIFF (XBUFFER (w->buffer)))
7089 != !NILP (w->last_had_star))
7090 || ((!NILP (Vtransient_mark_mode)
7091 && !NILP (XBUFFER (w->buffer)->mark_active))
7092 != !NILP (w->region_showing)))
7093 {
7094 struct buffer *prev = current_buffer;
7095 int count = BINDING_STACK_SIZE ();
7096
7097 /* Set current_buffer to the buffer of the selected
7098 window of the frame, so that we get the right local
7099 keymaps. */
7100 set_buffer_internal_1 (XBUFFER (w->buffer));
7101
7102 /* Save match data, if we must. */
7103 if (save_match_data)
7104 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7105
7106 /* Make sure that we don't accidentally use bogus keymaps. */
7107 if (NILP (Voverriding_local_map_menu_flag))
7108 {
7109 specbind (Qoverriding_terminal_local_map, Qnil);
7110 specbind (Qoverriding_local_map, Qnil);
7111 }
7112
7113 /* Build desired tool-bar items from keymaps. */
7114 f->tool_bar_items
7115 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7116
7117 /* Redisplay the tool-bar in case we changed it. */
7118 w->update_mode_line = Qt;
7119
7120 unbind_to (count, Qnil);
7121 set_buffer_internal_1 (prev);
7122 }
7123 }
7124 }
7125
7126
7127 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7128 F's desired tool-bar contents. F->tool_bar_items must have
7129 been set up previously by calling prepare_menu_bars. */
7130
7131 static void
7132 build_desired_tool_bar_string (f)
7133 struct frame *f;
7134 {
7135 int i, size, size_needed, string_idx;
7136 struct gcpro gcpro1, gcpro2, gcpro3;
7137 Lisp_Object image, plist, props;
7138
7139 image = plist = props = Qnil;
7140 GCPRO3 (image, plist, props);
7141
7142 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7143 Otherwise, make a new string. */
7144
7145 /* The size of the string we might be able to reuse. */
7146 size = (STRINGP (f->desired_tool_bar_string)
7147 ? XSTRING (f->desired_tool_bar_string)->size
7148 : 0);
7149
7150 /* Each image in the string we build is preceded by a space,
7151 and there is a space at the end. */
7152 size_needed = f->n_tool_bar_items + 1;
7153
7154 /* Reuse f->desired_tool_bar_string, if possible. */
7155 if (size < size_needed)
7156 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7157 make_number (' '));
7158 else
7159 {
7160 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7161 Fremove_text_properties (make_number (0), make_number (size),
7162 props, f->desired_tool_bar_string);
7163 }
7164
7165 /* Put a `display' property on the string for the images to display,
7166 put a `menu_item' property on tool-bar items with a value that
7167 is the index of the item in F's tool-bar item vector. */
7168 for (i = 0, string_idx = 0;
7169 i < f->n_tool_bar_items;
7170 ++i, string_idx += 1)
7171 {
7172 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7173
7174 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7175 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7176 int margin, relief, idx;
7177 extern Lisp_Object QCrelief, QCmargin, QCalgorithm, Qimage;
7178 extern Lisp_Object Qlaplace;
7179
7180 /* If image is a vector, choose the image according to the
7181 button state. */
7182 image = PROP (TOOL_BAR_ITEM_IMAGES);
7183 if (VECTORP (image))
7184 {
7185 if (enabled_p)
7186 idx = (selected_p
7187 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7188 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7189 else
7190 idx = (selected_p
7191 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7192 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7193
7194 xassert (ASIZE (image) >= idx);
7195 image = AREF (image, idx);
7196 }
7197 else
7198 idx = -1;
7199
7200 /* Ignore invalid image specifications. */
7201 if (!valid_image_p (image))
7202 continue;
7203
7204 /* Display the tool-bar button pressed, or depressed. */
7205 plist = Fcopy_sequence (XCDR (image));
7206
7207 /* Compute margin and relief to draw. */
7208 relief = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
7209 margin = relief + max (0, tool_bar_button_margin);
7210
7211 if (auto_raise_tool_bar_buttons_p)
7212 {
7213 /* Add a `:relief' property to the image spec if the item is
7214 selected. */
7215 if (selected_p)
7216 {
7217 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7218 margin -= relief;
7219 }
7220 }
7221 else
7222 {
7223 /* If image is selected, display it pressed, i.e. with a
7224 negative relief. If it's not selected, display it with a
7225 raised relief. */
7226 plist = Fplist_put (plist, QCrelief,
7227 (selected_p
7228 ? make_number (-relief)
7229 : make_number (relief)));
7230 margin -= relief;
7231 }
7232
7233 /* Put a margin around the image. */
7234 if (margin)
7235 plist = Fplist_put (plist, QCmargin, make_number (margin));
7236
7237 /* If button is not enabled, and we don't have special images
7238 for the disabled state, make the image appear disabled by
7239 applying an appropriate algorithm to it. */
7240 if (!enabled_p && idx < 0)
7241 plist = Fplist_put (plist, QCalgorithm, Qdisabled);
7242
7243 /* Put a `display' text property on the string for the image to
7244 display. Put a `menu-item' property on the string that gives
7245 the start of this item's properties in the tool-bar items
7246 vector. */
7247 image = Fcons (Qimage, plist);
7248 props = list4 (Qdisplay, image,
7249 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS)),
7250 Fadd_text_properties (make_number (string_idx),
7251 make_number (string_idx + 1),
7252 props, f->desired_tool_bar_string);
7253 #undef PROP
7254 }
7255
7256 UNGCPRO;
7257 }
7258
7259
7260 /* Display one line of the tool-bar of frame IT->f. */
7261
7262 static void
7263 display_tool_bar_line (it)
7264 struct it *it;
7265 {
7266 struct glyph_row *row = it->glyph_row;
7267 int max_x = it->last_visible_x;
7268 struct glyph *last;
7269
7270 prepare_desired_row (row);
7271 row->y = it->current_y;
7272
7273 while (it->current_x < max_x)
7274 {
7275 int x_before, x, n_glyphs_before, i, nglyphs;
7276
7277 /* Get the next display element. */
7278 if (!get_next_display_element (it))
7279 break;
7280
7281 /* Produce glyphs. */
7282 x_before = it->current_x;
7283 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7284 PRODUCE_GLYPHS (it);
7285
7286 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7287 i = 0;
7288 x = x_before;
7289 while (i < nglyphs)
7290 {
7291 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7292
7293 if (x + glyph->pixel_width > max_x)
7294 {
7295 /* Glyph doesn't fit on line. */
7296 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7297 it->current_x = x;
7298 goto out;
7299 }
7300
7301 ++it->hpos;
7302 x += glyph->pixel_width;
7303 ++i;
7304 }
7305
7306 /* Stop at line ends. */
7307 if (ITERATOR_AT_END_OF_LINE_P (it))
7308 break;
7309
7310 set_iterator_to_next (it, 1);
7311 }
7312
7313 out:;
7314
7315 row->displays_text_p = row->used[TEXT_AREA] != 0;
7316 extend_face_to_end_of_line (it);
7317 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7318 last->right_box_line_p = 1;
7319 compute_line_metrics (it);
7320
7321 /* If line is empty, make it occupy the rest of the tool-bar. */
7322 if (!row->displays_text_p)
7323 {
7324 row->height = row->phys_height = it->last_visible_y - row->y;
7325 row->ascent = row->phys_ascent = 0;
7326 }
7327
7328 row->full_width_p = 1;
7329 row->continued_p = 0;
7330 row->truncated_on_left_p = 0;
7331 row->truncated_on_right_p = 0;
7332
7333 it->current_x = it->hpos = 0;
7334 it->current_y += row->height;
7335 ++it->vpos;
7336 ++it->glyph_row;
7337 }
7338
7339
7340 /* Value is the number of screen lines needed to make all tool-bar
7341 items of frame F visible. */
7342
7343 static int
7344 tool_bar_lines_needed (f)
7345 struct frame *f;
7346 {
7347 struct window *w = XWINDOW (f->tool_bar_window);
7348 struct it it;
7349
7350 /* Initialize an iterator for iteration over
7351 F->desired_tool_bar_string in the tool-bar window of frame F. */
7352 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7353 it.first_visible_x = 0;
7354 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7355 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7356
7357 while (!ITERATOR_AT_END_P (&it))
7358 {
7359 it.glyph_row = w->desired_matrix->rows;
7360 clear_glyph_row (it.glyph_row);
7361 display_tool_bar_line (&it);
7362 }
7363
7364 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7365 }
7366
7367
7368 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
7369 height should be changed. */
7370
7371 static int
7372 redisplay_tool_bar (f)
7373 struct frame *f;
7374 {
7375 struct window *w;
7376 struct it it;
7377 struct glyph_row *row;
7378 int change_height_p = 0;
7379
7380 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7381 do anything. This means you must start with tool-bar-lines
7382 non-zero to get the auto-sizing effect. Or in other words, you
7383 can turn off tool-bars by specifying tool-bar-lines zero. */
7384 if (!WINDOWP (f->tool_bar_window)
7385 || (w = XWINDOW (f->tool_bar_window),
7386 XFASTINT (w->height) == 0))
7387 return 0;
7388
7389 /* Set up an iterator for the tool-bar window. */
7390 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7391 it.first_visible_x = 0;
7392 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7393 row = it.glyph_row;
7394
7395 /* Build a string that represents the contents of the tool-bar. */
7396 build_desired_tool_bar_string (f);
7397 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7398
7399 /* Display as many lines as needed to display all tool-bar items. */
7400 while (it.current_y < it.last_visible_y)
7401 display_tool_bar_line (&it);
7402
7403 /* It doesn't make much sense to try scrolling in the tool-bar
7404 window, so don't do it. */
7405 w->desired_matrix->no_scrolling_p = 1;
7406 w->must_be_updated_p = 1;
7407
7408 if (auto_resize_tool_bars_p)
7409 {
7410 int nlines;
7411
7412 /* If there are blank lines at the end, except for a partially
7413 visible blank line at the end that is smaller than
7414 CANON_Y_UNIT, change the tool-bar's height. */
7415 row = it.glyph_row - 1;
7416 if (!row->displays_text_p
7417 && row->height >= CANON_Y_UNIT (f))
7418 change_height_p = 1;
7419
7420 /* If row displays tool-bar items, but is partially visible,
7421 change the tool-bar's height. */
7422 if (row->displays_text_p
7423 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7424 change_height_p = 1;
7425
7426 /* Resize windows as needed by changing the `tool-bar-lines'
7427 frame parameter. */
7428 if (change_height_p
7429 && (nlines = tool_bar_lines_needed (f),
7430 nlines != XFASTINT (w->height)))
7431 {
7432 extern Lisp_Object Qtool_bar_lines;
7433 Lisp_Object frame;
7434 int old_height = XFASTINT (w->height);
7435
7436 XSETFRAME (frame, f);
7437 clear_glyph_matrix (w->desired_matrix);
7438 Fmodify_frame_parameters (frame,
7439 Fcons (Fcons (Qtool_bar_lines,
7440 make_number (nlines)),
7441 Qnil));
7442 if (XFASTINT (w->height) != old_height)
7443 fonts_changed_p = 1;
7444 }
7445 }
7446
7447 return change_height_p;
7448 }
7449
7450
7451 /* Get information about the tool-bar item which is displayed in GLYPH
7452 on frame F. Return in *PROP_IDX the index where tool-bar item
7453 properties start in F->tool_bar_items. Value is zero if
7454 GLYPH doesn't display a tool-bar item. */
7455
7456 int
7457 tool_bar_item_info (f, glyph, prop_idx)
7458 struct frame *f;
7459 struct glyph *glyph;
7460 int *prop_idx;
7461 {
7462 Lisp_Object prop;
7463 int success_p;
7464
7465 /* Get the text property `menu-item' at pos. The value of that
7466 property is the start index of this item's properties in
7467 F->tool_bar_items. */
7468 prop = Fget_text_property (make_number (glyph->charpos),
7469 Qmenu_item, f->current_tool_bar_string);
7470 if (INTEGERP (prop))
7471 {
7472 *prop_idx = XINT (prop);
7473 success_p = 1;
7474 }
7475 else
7476 success_p = 0;
7477
7478 return success_p;
7479 }
7480
7481 #endif /* HAVE_WINDOW_SYSTEM */
7482
7483
7484 \f
7485 /************************************************************************
7486 Horizontal scrolling
7487 ************************************************************************/
7488
7489 static int hscroll_window_tree P_ ((Lisp_Object));
7490 static int hscroll_windows P_ ((Lisp_Object));
7491
7492 /* For all leaf windows in the window tree rooted at WINDOW, set their
7493 hscroll value so that PT is (i) visible in the window, and (ii) so
7494 that it is not within a certain margin at the window's left and
7495 right border. Value is non-zero if any window's hscroll has been
7496 changed. */
7497
7498 static int
7499 hscroll_window_tree (window)
7500 Lisp_Object window;
7501 {
7502 int hscrolled_p = 0;
7503
7504 while (WINDOWP (window))
7505 {
7506 struct window *w = XWINDOW (window);
7507
7508 if (WINDOWP (w->hchild))
7509 hscrolled_p |= hscroll_window_tree (w->hchild);
7510 else if (WINDOWP (w->vchild))
7511 hscrolled_p |= hscroll_window_tree (w->vchild);
7512 else if (w->cursor.vpos >= 0)
7513 {
7514 int hscroll_margin, text_area_x, text_area_y;
7515 int text_area_width, text_area_height;
7516 struct glyph_row *current_cursor_row
7517 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7518 struct glyph_row *desired_cursor_row
7519 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7520 struct glyph_row *cursor_row
7521 = (desired_cursor_row->enabled_p
7522 ? desired_cursor_row
7523 : current_cursor_row);
7524
7525 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7526 &text_area_width, &text_area_height);
7527
7528 /* Scroll when cursor is inside this scroll margin. */
7529 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7530
7531 if ((XFASTINT (w->hscroll)
7532 && w->cursor.x < hscroll_margin)
7533 || (cursor_row->enabled_p
7534 && cursor_row->truncated_on_right_p
7535 && (w->cursor.x > text_area_width - hscroll_margin)))
7536 {
7537 struct it it;
7538 int hscroll;
7539 struct buffer *saved_current_buffer;
7540 int pt;
7541
7542 /* Find point in a display of infinite width. */
7543 saved_current_buffer = current_buffer;
7544 current_buffer = XBUFFER (w->buffer);
7545
7546 if (w == XWINDOW (selected_window))
7547 pt = BUF_PT (current_buffer);
7548 else
7549 {
7550 pt = marker_position (w->pointm);
7551 pt = max (BEGV, pt);
7552 pt = min (ZV, pt);
7553 }
7554
7555 /* Move iterator to pt starting at cursor_row->start in
7556 a line with infinite width. */
7557 init_to_row_start (&it, w, cursor_row);
7558 it.last_visible_x = INFINITY;
7559 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7560 current_buffer = saved_current_buffer;
7561
7562 /* Center cursor in window. */
7563 hscroll = (max (0, it.current_x - text_area_width / 2)
7564 / CANON_X_UNIT (it.f));
7565
7566 /* Don't call Fset_window_hscroll if value hasn't
7567 changed because it will prevent redisplay
7568 optimizations. */
7569 if (XFASTINT (w->hscroll) != hscroll)
7570 {
7571 Fset_window_hscroll (window, make_number (hscroll));
7572 hscrolled_p = 1;
7573 }
7574 }
7575 }
7576
7577 window = w->next;
7578 }
7579
7580 /* Value is non-zero if hscroll of any leaf window has been changed. */
7581 return hscrolled_p;
7582 }
7583
7584
7585 /* Set hscroll so that cursor is visible and not inside horizontal
7586 scroll margins for all windows in the tree rooted at WINDOW. See
7587 also hscroll_window_tree above. Value is non-zero if any window's
7588 hscroll has been changed. If it has, desired matrices on the frame
7589 of WINDOW are cleared. */
7590
7591 static int
7592 hscroll_windows (window)
7593 Lisp_Object window;
7594 {
7595 int hscrolled_p;
7596
7597 if (automatic_hscrolling_p)
7598 {
7599 hscrolled_p = hscroll_window_tree (window);
7600 if (hscrolled_p)
7601 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7602 }
7603 else
7604 hscrolled_p = 0;
7605 return hscrolled_p;
7606 }
7607
7608
7609 \f
7610 /************************************************************************
7611 Redisplay
7612 ************************************************************************/
7613
7614 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7615 to a non-zero value. This is sometimes handy to have in a debugger
7616 session. */
7617
7618 #if GLYPH_DEBUG
7619
7620 /* First and last unchanged row for try_window_id. */
7621
7622 int debug_first_unchanged_at_end_vpos;
7623 int debug_last_unchanged_at_beg_vpos;
7624
7625 /* Delta vpos and y. */
7626
7627 int debug_dvpos, debug_dy;
7628
7629 /* Delta in characters and bytes for try_window_id. */
7630
7631 int debug_delta, debug_delta_bytes;
7632
7633 /* Values of window_end_pos and window_end_vpos at the end of
7634 try_window_id. */
7635
7636 int debug_end_pos, debug_end_vpos;
7637
7638 /* Append a string to W->desired_matrix->method. FMT is a printf
7639 format string. A1...A9 are a supplement for a variable-length
7640 argument list. If trace_redisplay_p is non-zero also printf the
7641 resulting string to stderr. */
7642
7643 static void
7644 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7645 struct window *w;
7646 char *fmt;
7647 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7648 {
7649 char buffer[512];
7650 char *method = w->desired_matrix->method;
7651 int len = strlen (method);
7652 int size = sizeof w->desired_matrix->method;
7653 int remaining = size - len - 1;
7654
7655 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7656 if (len && remaining)
7657 {
7658 method[len] = '|';
7659 --remaining, ++len;
7660 }
7661
7662 strncpy (method + len, buffer, remaining);
7663
7664 if (trace_redisplay_p)
7665 fprintf (stderr, "%p (%s): %s\n",
7666 w,
7667 ((BUFFERP (w->buffer)
7668 && STRINGP (XBUFFER (w->buffer)->name))
7669 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7670 : "no buffer"),
7671 buffer);
7672 }
7673
7674 #endif /* GLYPH_DEBUG */
7675
7676
7677 /* This counter is used to clear the face cache every once in a while
7678 in redisplay_internal. It is incremented for each redisplay.
7679 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7680 cleared. */
7681
7682 #define CLEAR_FACE_CACHE_COUNT 10000
7683 static int clear_face_cache_count;
7684
7685 /* Record the previous terminal frame we displayed. */
7686
7687 static struct frame *previous_terminal_frame;
7688
7689 /* Non-zero while redisplay_internal is in progress. */
7690
7691 int redisplaying_p;
7692
7693
7694 /* Value is non-zero if all changes in window W, which displays
7695 current_buffer, are in the text between START and END. START is a
7696 buffer position, END is given as a distance from Z. Used in
7697 redisplay_internal for display optimization. */
7698
7699 static INLINE int
7700 text_outside_line_unchanged_p (w, start, end)
7701 struct window *w;
7702 int start, end;
7703 {
7704 int unchanged_p = 1;
7705
7706 /* If text or overlays have changed, see where. */
7707 if (XFASTINT (w->last_modified) < MODIFF
7708 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
7709 {
7710 /* Gap in the line? */
7711 if (GPT < start || Z - GPT < end)
7712 unchanged_p = 0;
7713
7714 /* Changes start in front of the line, or end after it? */
7715 if (unchanged_p
7716 && (BEG_UNCHANGED < start - 1
7717 || END_UNCHANGED < end))
7718 unchanged_p = 0;
7719
7720 /* If selective display, can't optimize if changes start at the
7721 beginning of the line. */
7722 if (unchanged_p
7723 && INTEGERP (current_buffer->selective_display)
7724 && XINT (current_buffer->selective_display) > 0
7725 && (BEG_UNCHANGED < start || GPT <= start))
7726 unchanged_p = 0;
7727 }
7728
7729 return unchanged_p;
7730 }
7731
7732
7733 /* Do a frame update, taking possible shortcuts into account. This is
7734 the main external entry point for redisplay.
7735
7736 If the last redisplay displayed an echo area message and that message
7737 is no longer requested, we clear the echo area or bring back the
7738 mini-buffer if that is in use. */
7739
7740 void
7741 redisplay ()
7742 {
7743 redisplay_internal (0);
7744 }
7745
7746 /* Return 1 if point moved out of or into a composition. Otherwise
7747 return 0. PREV_BUF and PREV_PT are the last point buffer and
7748 position. BUF and PT are the current point buffer and position. */
7749
7750 int
7751 check_point_in_composition (prev_buf, prev_pt, buf, pt)
7752 struct buffer *prev_buf, *buf;
7753 int prev_pt, pt;
7754 {
7755 int start, end;
7756 Lisp_Object prop;
7757 Lisp_Object buffer;
7758
7759 XSETBUFFER (buffer, buf);
7760 /* Check a composition at the last point if point moved within the
7761 same buffer. */
7762 if (prev_buf == buf)
7763 {
7764 if (prev_pt == pt)
7765 /* Point didn't move. */
7766 return 0;
7767
7768 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
7769 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
7770 && COMPOSITION_VALID_P (start, end, prop)
7771 && start < prev_pt && end > prev_pt)
7772 /* The last point was within the composition. Return 1 iff
7773 point moved out of the composition. */
7774 return (pt <= start || pt >= end);
7775 }
7776
7777 /* Check a composition at the current point. */
7778 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
7779 && find_composition (pt, -1, &start, &end, &prop, buffer)
7780 && COMPOSITION_VALID_P (start, end, prop)
7781 && start < pt && end > pt);
7782 }
7783
7784 /* Reconsider the setting of B->clip_changed which is displayed
7785 in window W. */
7786
7787 static INLINE void
7788 reconsider_clip_changes (w, b)
7789 struct window *w;
7790 struct buffer *b;
7791 {
7792 if (b->prevent_redisplay_optimizations_p)
7793 b->clip_changed = 1;
7794 else if (b->clip_changed
7795 && !NILP (w->window_end_valid)
7796 && w->current_matrix->buffer == b
7797 && w->current_matrix->zv == BUF_ZV (b)
7798 && w->current_matrix->begv == BUF_BEGV (b))
7799 b->clip_changed = 0;
7800
7801 /* If display wasn't paused, and W is not a tool bar window, see if
7802 point has been moved into or out of a composition. In that case,
7803 we set b->clip_changed to 1 to force updating the screen. If
7804 b->clip_changed has already been set to 1, we can skip this
7805 check. */
7806 if (!b->clip_changed
7807 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
7808 {
7809 int pt;
7810
7811 if (w == XWINDOW (selected_window))
7812 pt = BUF_PT (current_buffer);
7813 else
7814 pt = marker_position (w->pointm);
7815
7816 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
7817 || pt != XINT (w->last_point))
7818 && check_point_in_composition (w->current_matrix->buffer,
7819 XINT (w->last_point),
7820 XBUFFER (w->buffer), pt))
7821 b->clip_changed = 1;
7822 }
7823 }
7824
7825
7826 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
7827 response to any user action; therefore, we should preserve the echo
7828 area. (Actually, our caller does that job.) Perhaps in the future
7829 avoid recentering windows if it is not necessary; currently that
7830 causes some problems. */
7831
7832 static void
7833 redisplay_internal (preserve_echo_area)
7834 int preserve_echo_area;
7835 {
7836 struct window *w = XWINDOW (selected_window);
7837 struct frame *f = XFRAME (w->frame);
7838 int pause;
7839 int must_finish = 0;
7840 struct text_pos tlbufpos, tlendpos;
7841 int number_of_visible_frames;
7842 int count;
7843 struct frame *sf = SELECTED_FRAME ();
7844
7845 /* Non-zero means redisplay has to consider all windows on all
7846 frames. Zero means, only selected_window is considered. */
7847 int consider_all_windows_p;
7848
7849 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
7850
7851 /* No redisplay if running in batch mode or frame is not yet fully
7852 initialized, or redisplay is explicitly turned off by setting
7853 Vinhibit_redisplay. */
7854 if (noninteractive
7855 || !NILP (Vinhibit_redisplay)
7856 || !f->glyphs_initialized_p)
7857 return;
7858
7859 /* The flag redisplay_performed_directly_p is set by
7860 direct_output_for_insert when it already did the whole screen
7861 update necessary. */
7862 if (redisplay_performed_directly_p)
7863 {
7864 redisplay_performed_directly_p = 0;
7865 if (!hscroll_windows (selected_window))
7866 return;
7867 }
7868
7869 #ifdef USE_X_TOOLKIT
7870 if (popup_activated ())
7871 return;
7872 #endif
7873
7874 /* I don't think this happens but let's be paranoid. */
7875 if (redisplaying_p)
7876 return;
7877
7878 /* Record a function that resets redisplaying_p to its old value
7879 when we leave this function. */
7880 count = BINDING_STACK_SIZE ();
7881 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
7882 ++redisplaying_p;
7883
7884 retry:
7885 pause = 0;
7886 reconsider_clip_changes (w, current_buffer);
7887
7888 /* If new fonts have been loaded that make a glyph matrix adjustment
7889 necessary, do it. */
7890 if (fonts_changed_p)
7891 {
7892 adjust_glyphs (NULL);
7893 ++windows_or_buffers_changed;
7894 fonts_changed_p = 0;
7895 }
7896
7897 if (! FRAME_WINDOW_P (sf)
7898 && previous_terminal_frame != sf)
7899 {
7900 /* Since frames on an ASCII terminal share the same display
7901 area, displaying a different frame means redisplay the whole
7902 thing. */
7903 windows_or_buffers_changed++;
7904 SET_FRAME_GARBAGED (sf);
7905 XSETFRAME (Vterminal_frame, sf);
7906 }
7907 previous_terminal_frame = sf;
7908
7909 /* Set the visible flags for all frames. Do this before checking
7910 for resized or garbaged frames; they want to know if their frames
7911 are visible. See the comment in frame.h for
7912 FRAME_SAMPLE_VISIBILITY. */
7913 {
7914 Lisp_Object tail, frame;
7915
7916 number_of_visible_frames = 0;
7917
7918 FOR_EACH_FRAME (tail, frame)
7919 {
7920 struct frame *f = XFRAME (frame);
7921
7922 FRAME_SAMPLE_VISIBILITY (f);
7923 if (FRAME_VISIBLE_P (f))
7924 ++number_of_visible_frames;
7925 clear_desired_matrices (f);
7926 }
7927 }
7928
7929 /* Notice any pending interrupt request to change frame size. */
7930 do_pending_window_change (1);
7931
7932 /* Clear frames marked as garbaged. */
7933 if (frame_garbaged)
7934 clear_garbaged_frames ();
7935
7936 /* Build menubar and tool-bar items. */
7937 prepare_menu_bars ();
7938
7939 if (windows_or_buffers_changed)
7940 update_mode_lines++;
7941
7942 /* Detect case that we need to write or remove a star in the mode line. */
7943 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
7944 {
7945 w->update_mode_line = Qt;
7946 if (buffer_shared > 1)
7947 update_mode_lines++;
7948 }
7949
7950 /* If %c is in the mode line, update it if needed. */
7951 if (!NILP (w->column_number_displayed)
7952 /* This alternative quickly identifies a common case
7953 where no change is needed. */
7954 && !(PT == XFASTINT (w->last_point)
7955 && XFASTINT (w->last_modified) >= MODIFF
7956 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
7957 && XFASTINT (w->column_number_displayed) != current_column ())
7958 w->update_mode_line = Qt;
7959
7960 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
7961
7962 /* The variable buffer_shared is set in redisplay_window and
7963 indicates that we redisplay a buffer in different windows. See
7964 there. */
7965 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
7966
7967 /* If specs for an arrow have changed, do thorough redisplay
7968 to ensure we remove any arrow that should no longer exist. */
7969 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
7970 || ! EQ (Voverlay_arrow_string, last_arrow_string))
7971 consider_all_windows_p = windows_or_buffers_changed = 1;
7972
7973 /* Normally the message* functions will have already displayed and
7974 updated the echo area, but the frame may have been trashed, or
7975 the update may have been preempted, so display the echo area
7976 again here. Checking both message buffers captures the case that
7977 the echo area should be cleared. */
7978 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
7979 {
7980 int window_height_changed_p = echo_area_display (0);
7981 must_finish = 1;
7982
7983 if (fonts_changed_p)
7984 goto retry;
7985 else if (window_height_changed_p)
7986 {
7987 consider_all_windows_p = 1;
7988 ++update_mode_lines;
7989 ++windows_or_buffers_changed;
7990
7991 /* If window configuration was changed, frames may have been
7992 marked garbaged. Clear them or we will experience
7993 surprises wrt scrolling. */
7994 if (frame_garbaged)
7995 clear_garbaged_frames ();
7996 }
7997 }
7998 else if (EQ (selected_window, minibuf_window)
7999 && (current_buffer->clip_changed
8000 || XFASTINT (w->last_modified) < MODIFF
8001 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8002 && resize_mini_window (w, 0))
8003 {
8004 /* Resized active mini-window to fit the size of what it is
8005 showing if its contents might have changed. */
8006 must_finish = 1;
8007 consider_all_windows_p = 1;
8008 ++windows_or_buffers_changed;
8009 ++update_mode_lines;
8010
8011 /* If window configuration was changed, frames may have been
8012 marked garbaged. Clear them or we will experience
8013 surprises wrt scrolling. */
8014 if (frame_garbaged)
8015 clear_garbaged_frames ();
8016 }
8017
8018
8019 /* If showing the region, and mark has changed, we must redisplay
8020 the whole window. The assignment to this_line_start_pos prevents
8021 the optimization directly below this if-statement. */
8022 if (((!NILP (Vtransient_mark_mode)
8023 && !NILP (XBUFFER (w->buffer)->mark_active))
8024 != !NILP (w->region_showing))
8025 || (!NILP (w->region_showing)
8026 && !EQ (w->region_showing,
8027 Fmarker_position (XBUFFER (w->buffer)->mark))))
8028 CHARPOS (this_line_start_pos) = 0;
8029
8030 /* Optimize the case that only the line containing the cursor in the
8031 selected window has changed. Variables starting with this_ are
8032 set in display_line and record information about the line
8033 containing the cursor. */
8034 tlbufpos = this_line_start_pos;
8035 tlendpos = this_line_end_pos;
8036 if (!consider_all_windows_p
8037 && CHARPOS (tlbufpos) > 0
8038 && NILP (w->update_mode_line)
8039 && !current_buffer->clip_changed
8040 && FRAME_VISIBLE_P (XFRAME (w->frame))
8041 && !FRAME_OBSCURED_P (XFRAME (w->frame))
8042 /* Make sure recorded data applies to current buffer, etc. */
8043 && this_line_buffer == current_buffer
8044 && current_buffer == XBUFFER (w->buffer)
8045 && NILP (w->force_start)
8046 /* Point must be on the line that we have info recorded about. */
8047 && PT >= CHARPOS (tlbufpos)
8048 && PT <= Z - CHARPOS (tlendpos)
8049 /* All text outside that line, including its final newline,
8050 must be unchanged */
8051 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8052 CHARPOS (tlendpos)))
8053 {
8054 if (CHARPOS (tlbufpos) > BEGV
8055 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8056 && (CHARPOS (tlbufpos) == ZV
8057 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
8058 /* Former continuation line has disappeared by becoming empty */
8059 goto cancel;
8060 else if (XFASTINT (w->last_modified) < MODIFF
8061 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
8062 || MINI_WINDOW_P (w))
8063 {
8064 /* We have to handle the case of continuation around a
8065 wide-column character (See the comment in indent.c around
8066 line 885).
8067
8068 For instance, in the following case:
8069
8070 -------- Insert --------
8071 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8072 J_I_ ==> J_I_ `^^' are cursors.
8073 ^^ ^^
8074 -------- --------
8075
8076 As we have to redraw the line above, we should goto cancel. */
8077
8078 struct it it;
8079 int line_height_before = this_line_pixel_height;
8080
8081 /* Note that start_display will handle the case that the
8082 line starting at tlbufpos is a continuation lines. */
8083 start_display (&it, w, tlbufpos);
8084
8085 /* Implementation note: It this still necessary? */
8086 if (it.current_x != this_line_start_x)
8087 goto cancel;
8088
8089 TRACE ((stderr, "trying display optimization 1\n"));
8090 w->cursor.vpos = -1;
8091 overlay_arrow_seen = 0;
8092 it.vpos = this_line_vpos;
8093 it.current_y = this_line_y;
8094 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8095 display_line (&it);
8096
8097 /* If line contains point, is not continued,
8098 and ends at same distance from eob as before, we win */
8099 if (w->cursor.vpos >= 0
8100 /* Line is not continued, otherwise this_line_start_pos
8101 would have been set to 0 in display_line. */
8102 && CHARPOS (this_line_start_pos)
8103 /* Line ends as before. */
8104 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8105 /* Line has same height as before. Otherwise other lines
8106 would have to be shifted up or down. */
8107 && this_line_pixel_height == line_height_before)
8108 {
8109 /* If this is not the window's last line, we must adjust
8110 the charstarts of the lines below. */
8111 if (it.current_y < it.last_visible_y)
8112 {
8113 struct glyph_row *row
8114 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8115 int delta, delta_bytes;
8116
8117 if (Z - CHARPOS (tlendpos) == ZV)
8118 {
8119 /* This line ends at end of (accessible part of)
8120 buffer. There is no newline to count. */
8121 delta = (Z
8122 - CHARPOS (tlendpos)
8123 - MATRIX_ROW_START_CHARPOS (row));
8124 delta_bytes = (Z_BYTE
8125 - BYTEPOS (tlendpos)
8126 - MATRIX_ROW_START_BYTEPOS (row));
8127 }
8128 else
8129 {
8130 /* This line ends in a newline. Must take
8131 account of the newline and the rest of the
8132 text that follows. */
8133 delta = (Z
8134 - CHARPOS (tlendpos)
8135 - MATRIX_ROW_START_CHARPOS (row));
8136 delta_bytes = (Z_BYTE
8137 - BYTEPOS (tlendpos)
8138 - MATRIX_ROW_START_BYTEPOS (row));
8139 }
8140
8141 increment_matrix_positions (w->current_matrix,
8142 this_line_vpos + 1,
8143 w->current_matrix->nrows,
8144 delta, delta_bytes);
8145 }
8146
8147 /* If this row displays text now but previously didn't,
8148 or vice versa, w->window_end_vpos may have to be
8149 adjusted. */
8150 if ((it.glyph_row - 1)->displays_text_p)
8151 {
8152 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8153 XSETINT (w->window_end_vpos, this_line_vpos);
8154 }
8155 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8156 && this_line_vpos > 0)
8157 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8158 w->window_end_valid = Qnil;
8159
8160 /* Update hint: No need to try to scroll in update_window. */
8161 w->desired_matrix->no_scrolling_p = 1;
8162
8163 #if GLYPH_DEBUG
8164 *w->desired_matrix->method = 0;
8165 debug_method_add (w, "optimization 1");
8166 #endif
8167 goto update;
8168 }
8169 else
8170 goto cancel;
8171 }
8172 else if (/* Cursor position hasn't changed. */
8173 PT == XFASTINT (w->last_point)
8174 /* Make sure the cursor was last displayed
8175 in this window. Otherwise we have to reposition it. */
8176 && 0 <= w->cursor.vpos
8177 && XINT (w->height) > w->cursor.vpos)
8178 {
8179 if (!must_finish)
8180 {
8181 do_pending_window_change (1);
8182
8183 /* We used to always goto end_of_redisplay here, but this
8184 isn't enough if we have a blinking cursor. */
8185 if (w->cursor_off_p == w->last_cursor_off_p)
8186 goto end_of_redisplay;
8187 }
8188 goto update;
8189 }
8190 /* If highlighting the region, or if the cursor is in the echo area,
8191 then we can't just move the cursor. */
8192 else if (! (!NILP (Vtransient_mark_mode)
8193 && !NILP (current_buffer->mark_active))
8194 && (EQ (selected_window, current_buffer->last_selected_window)
8195 || highlight_nonselected_windows)
8196 && NILP (w->region_showing)
8197 && NILP (Vshow_trailing_whitespace)
8198 && !cursor_in_echo_area)
8199 {
8200 struct it it;
8201 struct glyph_row *row;
8202
8203 /* Skip from tlbufpos to PT and see where it is. Note that
8204 PT may be in invisible text. If so, we will end at the
8205 next visible position. */
8206 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8207 NULL, DEFAULT_FACE_ID);
8208 it.current_x = this_line_start_x;
8209 it.current_y = this_line_y;
8210 it.vpos = this_line_vpos;
8211
8212 /* The call to move_it_to stops in front of PT, but
8213 moves over before-strings. */
8214 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8215
8216 if (it.vpos == this_line_vpos
8217 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8218 row->enabled_p))
8219 {
8220 xassert (this_line_vpos == it.vpos);
8221 xassert (this_line_y == it.current_y);
8222 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
8223 goto update;
8224 }
8225 else
8226 goto cancel;
8227 }
8228
8229 cancel:
8230 /* Text changed drastically or point moved off of line. */
8231 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
8232 }
8233
8234 CHARPOS (this_line_start_pos) = 0;
8235 consider_all_windows_p |= buffer_shared > 1;
8236 ++clear_face_cache_count;
8237
8238
8239 /* Build desired matrices, and update the display. If
8240 consider_all_windows_p is non-zero, do it for all windows on all
8241 frames. Otherwise do it for selected_window, only. */
8242
8243 if (consider_all_windows_p)
8244 {
8245 Lisp_Object tail, frame;
8246
8247 /* Clear the face cache eventually. */
8248 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
8249 {
8250 clear_face_cache (0);
8251 clear_face_cache_count = 0;
8252 }
8253
8254 /* Recompute # windows showing selected buffer. This will be
8255 incremented each time such a window is displayed. */
8256 buffer_shared = 0;
8257
8258 FOR_EACH_FRAME (tail, frame)
8259 {
8260 struct frame *f = XFRAME (frame);
8261
8262 if (FRAME_WINDOW_P (f) || f == sf)
8263 {
8264 /* Mark all the scroll bars to be removed; we'll redeem
8265 the ones we want when we redisplay their windows. */
8266 if (condemn_scroll_bars_hook)
8267 (*condemn_scroll_bars_hook) (f);
8268
8269 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8270 redisplay_windows (FRAME_ROOT_WINDOW (f));
8271
8272 /* Any scroll bars which redisplay_windows should have
8273 nuked should now go away. */
8274 if (judge_scroll_bars_hook)
8275 (*judge_scroll_bars_hook) (f);
8276
8277 /* If fonts changed, display again. */
8278 if (fonts_changed_p)
8279 goto retry;
8280
8281 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8282 {
8283 /* See if we have to hscroll. */
8284 if (hscroll_windows (f->root_window))
8285 goto retry;
8286
8287 /* Prevent various kinds of signals during display
8288 update. stdio is not robust about handling
8289 signals, which can cause an apparent I/O
8290 error. */
8291 if (interrupt_input)
8292 unrequest_sigio ();
8293 stop_polling ();
8294
8295 /* Update the display. */
8296 set_window_update_flags (XWINDOW (f->root_window), 1);
8297 pause |= update_frame (f, 0, 0);
8298 if (pause)
8299 break;
8300
8301 mark_window_display_accurate (f->root_window, 1);
8302 if (frame_up_to_date_hook)
8303 frame_up_to_date_hook (f);
8304 }
8305 }
8306 }
8307 }
8308 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8309 {
8310 Lisp_Object mini_window;
8311 struct frame *mini_frame;
8312
8313 redisplay_window (selected_window, 1);
8314
8315 /* Compare desired and current matrices, perform output. */
8316 update:
8317
8318 /* If fonts changed, display again. */
8319 if (fonts_changed_p)
8320 goto retry;
8321
8322 /* Prevent various kinds of signals during display update.
8323 stdio is not robust about handling signals,
8324 which can cause an apparent I/O error. */
8325 if (interrupt_input)
8326 unrequest_sigio ();
8327 stop_polling ();
8328
8329 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8330 {
8331 if (hscroll_windows (selected_window))
8332 goto retry;
8333
8334 XWINDOW (selected_window)->must_be_updated_p = 1;
8335 pause = update_frame (sf, 0, 0);
8336 }
8337
8338 /* We may have called echo_area_display at the top of this
8339 function. If the echo area is on another frame, that may
8340 have put text on a frame other than the selected one, so the
8341 above call to update_frame would not have caught it. Catch
8342 it here. */
8343 mini_window = FRAME_MINIBUF_WINDOW (sf);
8344 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8345
8346 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8347 {
8348 XWINDOW (mini_window)->must_be_updated_p = 1;
8349 pause |= update_frame (mini_frame, 0, 0);
8350 if (!pause && hscroll_windows (mini_window))
8351 goto retry;
8352 }
8353 }
8354
8355 /* If display was paused because of pending input, make sure we do a
8356 thorough update the next time. */
8357 if (pause)
8358 {
8359 /* Prevent the optimization at the beginning of
8360 redisplay_internal that tries a single-line update of the
8361 line containing the cursor in the selected window. */
8362 CHARPOS (this_line_start_pos) = 0;
8363
8364 /* Let the overlay arrow be updated the next time. */
8365 if (!NILP (last_arrow_position))
8366 {
8367 last_arrow_position = Qt;
8368 last_arrow_string = Qt;
8369 }
8370
8371 /* If we pause after scrolling, some rows in the current
8372 matrices of some windows are not valid. */
8373 if (!WINDOW_FULL_WIDTH_P (w)
8374 && !FRAME_WINDOW_P (XFRAME (w->frame)))
8375 update_mode_lines = 1;
8376 }
8377
8378 /* Now text on frame agrees with windows, so put info into the
8379 windows for partial redisplay to follow. */
8380 if (!pause)
8381 {
8382 register struct buffer *b = XBUFFER (w->buffer);
8383
8384 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8385 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8386 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8387 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
8388
8389 if (consider_all_windows_p)
8390 mark_window_display_accurate (FRAME_ROOT_WINDOW (sf), 1);
8391 else
8392 {
8393 XSETFASTINT (w->last_point, BUF_PT (b));
8394 w->last_cursor = w->cursor;
8395 w->last_cursor_off_p = w->cursor_off_p;
8396
8397 b->clip_changed = 0;
8398 b->prevent_redisplay_optimizations_p = 0;
8399 w->update_mode_line = Qnil;
8400 XSETFASTINT (w->last_modified, BUF_MODIFF (b));
8401 XSETFASTINT (w->last_overlay_modified, BUF_OVERLAY_MODIFF (b));
8402 w->last_had_star
8403 = (BUF_MODIFF (XBUFFER (w->buffer)) > BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8404 ? Qt : Qnil);
8405
8406 /* Record if we are showing a region, so can make sure to
8407 update it fully at next redisplay. */
8408 w->region_showing = (!NILP (Vtransient_mark_mode)
8409 && (EQ (selected_window,
8410 current_buffer->last_selected_window)
8411 || highlight_nonselected_windows)
8412 && !NILP (XBUFFER (w->buffer)->mark_active)
8413 ? Fmarker_position (XBUFFER (w->buffer)->mark)
8414 : Qnil);
8415
8416 w->window_end_valid = w->buffer;
8417 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8418 last_arrow_string = Voverlay_arrow_string;
8419 if (frame_up_to_date_hook != 0)
8420 (*frame_up_to_date_hook) (sf);
8421
8422 w->current_matrix->buffer = b;
8423 w->current_matrix->begv = BUF_BEGV (b);
8424 w->current_matrix->zv = BUF_ZV (b);
8425 }
8426
8427 update_mode_lines = 0;
8428 windows_or_buffers_changed = 0;
8429 }
8430
8431 /* Start SIGIO interrupts coming again. Having them off during the
8432 code above makes it less likely one will discard output, but not
8433 impossible, since there might be stuff in the system buffer here.
8434 But it is much hairier to try to do anything about that. */
8435 if (interrupt_input)
8436 request_sigio ();
8437 start_polling ();
8438
8439 /* If a frame has become visible which was not before, redisplay
8440 again, so that we display it. Expose events for such a frame
8441 (which it gets when becoming visible) don't call the parts of
8442 redisplay constructing glyphs, so simply exposing a frame won't
8443 display anything in this case. So, we have to display these
8444 frames here explicitly. */
8445 if (!pause)
8446 {
8447 Lisp_Object tail, frame;
8448 int new_count = 0;
8449
8450 FOR_EACH_FRAME (tail, frame)
8451 {
8452 int this_is_visible = 0;
8453
8454 if (XFRAME (frame)->visible)
8455 this_is_visible = 1;
8456 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8457 if (XFRAME (frame)->visible)
8458 this_is_visible = 1;
8459
8460 if (this_is_visible)
8461 new_count++;
8462 }
8463
8464 if (new_count != number_of_visible_frames)
8465 windows_or_buffers_changed++;
8466 }
8467
8468 /* Change frame size now if a change is pending. */
8469 do_pending_window_change (1);
8470
8471 /* If we just did a pending size change, or have additional
8472 visible frames, redisplay again. */
8473 if (windows_or_buffers_changed && !pause)
8474 goto retry;
8475
8476 end_of_redisplay:;
8477
8478 unbind_to (count, Qnil);
8479 }
8480
8481
8482 /* Redisplay, but leave alone any recent echo area message unless
8483 another message has been requested in its place.
8484
8485 This is useful in situations where you need to redisplay but no
8486 user action has occurred, making it inappropriate for the message
8487 area to be cleared. See tracking_off and
8488 wait_reading_process_input for examples of these situations. */
8489
8490 void
8491 redisplay_preserve_echo_area ()
8492 {
8493 if (!NILP (echo_area_buffer[1]))
8494 {
8495 /* We have a previously displayed message, but no current
8496 message. Redisplay the previous message. */
8497 display_last_displayed_message_p = 1;
8498 redisplay_internal (1);
8499 display_last_displayed_message_p = 0;
8500 }
8501 else
8502 redisplay_internal (1);
8503 }
8504
8505
8506 /* Function registered with record_unwind_protect in
8507 redisplay_internal. Clears the flag indicating that a redisplay is
8508 in progress. */
8509
8510 static Lisp_Object
8511 unwind_redisplay (old_redisplaying_p)
8512 Lisp_Object old_redisplaying_p;
8513 {
8514 redisplaying_p = XFASTINT (old_redisplaying_p);
8515 return Qnil;
8516 }
8517
8518
8519 /* Mark the display of windows in the window tree rooted at WINDOW as
8520 accurate or inaccurate. If FLAG is non-zero mark display of WINDOW
8521 as accurate. If FLAG is zero arrange for WINDOW to be redisplayed
8522 the next time redisplay_internal is called. */
8523
8524 void
8525 mark_window_display_accurate (window, accurate_p)
8526 Lisp_Object window;
8527 int accurate_p;
8528 {
8529 struct window *w;
8530
8531 for (; !NILP (window); window = w->next)
8532 {
8533 w = XWINDOW (window);
8534
8535 if (BUFFERP (w->buffer))
8536 {
8537 struct buffer *b = XBUFFER (w->buffer);
8538
8539 XSETFASTINT (w->last_modified,
8540 accurate_p ? BUF_MODIFF (b) : 0);
8541 XSETFASTINT (w->last_overlay_modified,
8542 accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8543 w->last_had_star = (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b)
8544 ? Qt : Qnil);
8545
8546 #if 0 /* I don't think this is necessary because display_line does it.
8547 Let's check it. */
8548 /* Record if we are showing a region, so can make sure to
8549 update it fully at next redisplay. */
8550 w->region_showing
8551 = (!NILP (Vtransient_mark_mode)
8552 && (w == XWINDOW (current_buffer->last_selected_window)
8553 || highlight_nonselected_windows)
8554 && (!NILP (b->mark_active)
8555 ? Fmarker_position (b->mark)
8556 : Qnil));
8557 #endif
8558
8559 if (accurate_p)
8560 {
8561 b->clip_changed = 0;
8562 b->prevent_redisplay_optimizations_p = 0;
8563 w->current_matrix->buffer = b;
8564 w->current_matrix->begv = BUF_BEGV (b);
8565 w->current_matrix->zv = BUF_ZV (b);
8566 w->last_cursor = w->cursor;
8567 w->last_cursor_off_p = w->cursor_off_p;
8568 if (w == XWINDOW (selected_window))
8569 w->last_point = make_number (BUF_PT (b));
8570 else
8571 w->last_point = make_number (XMARKER (w->pointm)->charpos);
8572 }
8573 }
8574
8575 w->window_end_valid = w->buffer;
8576 w->update_mode_line = Qnil;
8577
8578 if (!NILP (w->vchild))
8579 mark_window_display_accurate (w->vchild, accurate_p);
8580 if (!NILP (w->hchild))
8581 mark_window_display_accurate (w->hchild, accurate_p);
8582 }
8583
8584 if (accurate_p)
8585 {
8586 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8587 last_arrow_string = Voverlay_arrow_string;
8588 }
8589 else
8590 {
8591 /* Force a thorough redisplay the next time by setting
8592 last_arrow_position and last_arrow_string to t, which is
8593 unequal to any useful value of Voverlay_arrow_... */
8594 last_arrow_position = Qt;
8595 last_arrow_string = Qt;
8596 }
8597 }
8598
8599
8600 /* Return value in display table DP (Lisp_Char_Table *) for character
8601 C. Since a display table doesn't have any parent, we don't have to
8602 follow parent. Do not call this function directly but use the
8603 macro DISP_CHAR_VECTOR. */
8604
8605 Lisp_Object
8606 disp_char_vector (dp, c)
8607 struct Lisp_Char_Table *dp;
8608 int c;
8609 {
8610 int code[4], i;
8611 Lisp_Object val;
8612
8613 if (SINGLE_BYTE_CHAR_P (c))
8614 return (dp->contents[c]);
8615
8616 SPLIT_CHAR (c, code[0], code[1], code[2]);
8617 if (code[1] < 32)
8618 code[1] = -1;
8619 else if (code[2] < 32)
8620 code[2] = -1;
8621
8622 /* Here, the possible range of code[0] (== charset ID) is
8623 128..max_charset. Since the top level char table contains data
8624 for multibyte characters after 256th element, we must increment
8625 code[0] by 128 to get a correct index. */
8626 code[0] += 128;
8627 code[3] = -1; /* anchor */
8628
8629 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8630 {
8631 val = dp->contents[code[i]];
8632 if (!SUB_CHAR_TABLE_P (val))
8633 return (NILP (val) ? dp->defalt : val);
8634 }
8635
8636 /* Here, val is a sub char table. We return the default value of
8637 it. */
8638 return (dp->defalt);
8639 }
8640
8641
8642 \f
8643 /***********************************************************************
8644 Window Redisplay
8645 ***********************************************************************/
8646
8647 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
8648
8649 static void
8650 redisplay_windows (window)
8651 Lisp_Object window;
8652 {
8653 while (!NILP (window))
8654 {
8655 struct window *w = XWINDOW (window);
8656
8657 if (!NILP (w->hchild))
8658 redisplay_windows (w->hchild);
8659 else if (!NILP (w->vchild))
8660 redisplay_windows (w->vchild);
8661 else
8662 redisplay_window (window, 0);
8663
8664 window = w->next;
8665 }
8666 }
8667
8668
8669 /* Set cursor position of W. PT is assumed to be displayed in ROW.
8670 DELTA is the number of bytes by which positions recorded in ROW
8671 differ from current buffer positions. */
8672
8673 void
8674 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
8675 struct window *w;
8676 struct glyph_row *row;
8677 struct glyph_matrix *matrix;
8678 int delta, delta_bytes, dy, dvpos;
8679 {
8680 struct glyph *glyph = row->glyphs[TEXT_AREA];
8681 struct glyph *end = glyph + row->used[TEXT_AREA];
8682 int x = row->x;
8683 int pt_old = PT - delta;
8684
8685 /* Skip over glyphs not having an object at the start of the row.
8686 These are special glyphs like truncation marks on terminal
8687 frames. */
8688 if (row->displays_text_p)
8689 while (glyph < end
8690 && INTEGERP (glyph->object)
8691 && glyph->charpos < 0)
8692 {
8693 x += glyph->pixel_width;
8694 ++glyph;
8695 }
8696
8697 while (glyph < end
8698 && !INTEGERP (glyph->object)
8699 && (!BUFFERP (glyph->object)
8700 || glyph->charpos < pt_old))
8701 {
8702 x += glyph->pixel_width;
8703 ++glyph;
8704 }
8705
8706 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
8707 w->cursor.x = x;
8708 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
8709 w->cursor.y = row->y + dy;
8710
8711 if (w == XWINDOW (selected_window))
8712 {
8713 if (!row->continued_p
8714 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
8715 && row->x == 0)
8716 {
8717 this_line_buffer = XBUFFER (w->buffer);
8718
8719 CHARPOS (this_line_start_pos)
8720 = MATRIX_ROW_START_CHARPOS (row) + delta;
8721 BYTEPOS (this_line_start_pos)
8722 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
8723
8724 CHARPOS (this_line_end_pos)
8725 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
8726 BYTEPOS (this_line_end_pos)
8727 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
8728
8729 this_line_y = w->cursor.y;
8730 this_line_pixel_height = row->height;
8731 this_line_vpos = w->cursor.vpos;
8732 this_line_start_x = row->x;
8733 }
8734 else
8735 CHARPOS (this_line_start_pos) = 0;
8736 }
8737 }
8738
8739
8740 /* Run window scroll functions, if any, for WINDOW with new window
8741 start STARTP. Sets the window start of WINDOW to that position.
8742
8743 We assume that the window's buffer is really current. */
8744
8745 static INLINE struct text_pos
8746 run_window_scroll_functions (window, startp)
8747 Lisp_Object window;
8748 struct text_pos startp;
8749 {
8750 struct window *w = XWINDOW (window);
8751 SET_MARKER_FROM_TEXT_POS (w->start, startp);
8752
8753 if (current_buffer != XBUFFER (w->buffer))
8754 abort ();
8755
8756 if (!NILP (Vwindow_scroll_functions))
8757 {
8758 run_hook_with_args_2 (Qwindow_scroll_functions, window,
8759 make_number (CHARPOS (startp)));
8760 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8761 /* In case the hook functions switch buffers. */
8762 if (current_buffer != XBUFFER (w->buffer))
8763 set_buffer_internal_1 (XBUFFER (w->buffer));
8764 }
8765
8766 return startp;
8767 }
8768
8769
8770 /* Modify the desired matrix of window W and W->vscroll so that the
8771 line containing the cursor is fully visible. */
8772
8773 static void
8774 make_cursor_line_fully_visible (w)
8775 struct window *w;
8776 {
8777 struct glyph_matrix *matrix;
8778 struct glyph_row *row;
8779 int window_height, header_line_height;
8780
8781 /* It's not always possible to find the cursor, e.g, when a window
8782 is full of overlay strings. Don't do anything in that case. */
8783 if (w->cursor.vpos < 0)
8784 return;
8785
8786 matrix = w->desired_matrix;
8787 row = MATRIX_ROW (matrix, w->cursor.vpos);
8788
8789 /* If the cursor row is not partially visible, there's nothing
8790 to do. */
8791 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
8792 return;
8793
8794 /* If the row the cursor is in is taller than the window's height,
8795 it's not clear what to do, so do nothing. */
8796 window_height = window_box_height (w);
8797 if (row->height >= window_height)
8798 return;
8799
8800 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
8801 {
8802 int dy = row->height - row->visible_height;
8803 w->vscroll = 0;
8804 w->cursor.y += dy;
8805 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8806 }
8807 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
8808 {
8809 int dy = - (row->height - row->visible_height);
8810 w->vscroll = dy;
8811 w->cursor.y += dy;
8812 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
8813 }
8814
8815 /* When we change the cursor y-position of the selected window,
8816 change this_line_y as well so that the display optimization for
8817 the cursor line of the selected window in redisplay_internal uses
8818 the correct y-position. */
8819 if (w == XWINDOW (selected_window))
8820 this_line_y = w->cursor.y;
8821 }
8822
8823
8824 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
8825 non-zero means only WINDOW is redisplayed in redisplay_internal.
8826 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
8827 in redisplay_window to bring a partially visible line into view in
8828 the case that only the cursor has moved.
8829
8830 Value is
8831
8832 1 if scrolling succeeded
8833
8834 0 if scrolling didn't find point.
8835
8836 -1 if new fonts have been loaded so that we must interrupt
8837 redisplay, adjust glyph matrices, and try again. */
8838
8839 static int
8840 try_scrolling (window, just_this_one_p, scroll_conservatively,
8841 scroll_step, temp_scroll_step)
8842 Lisp_Object window;
8843 int just_this_one_p;
8844 int scroll_conservatively, scroll_step;
8845 int temp_scroll_step;
8846 {
8847 struct window *w = XWINDOW (window);
8848 struct frame *f = XFRAME (w->frame);
8849 struct text_pos scroll_margin_pos;
8850 struct text_pos pos;
8851 struct text_pos startp;
8852 struct it it;
8853 Lisp_Object window_end;
8854 int this_scroll_margin;
8855 int dy = 0;
8856 int scroll_max;
8857 int rc;
8858 int amount_to_scroll = 0;
8859 Lisp_Object aggressive;
8860 int height;
8861
8862 #if GLYPH_DEBUG
8863 debug_method_add (w, "try_scrolling");
8864 #endif
8865
8866 SET_TEXT_POS_FROM_MARKER (startp, w->start);
8867
8868 /* Compute scroll margin height in pixels. We scroll when point is
8869 within this distance from the top or bottom of the window. */
8870 if (scroll_margin > 0)
8871 {
8872 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
8873 this_scroll_margin *= CANON_Y_UNIT (f);
8874 }
8875 else
8876 this_scroll_margin = 0;
8877
8878 /* Compute how much we should try to scroll maximally to bring point
8879 into view. */
8880 if (scroll_step || scroll_conservatively || temp_scroll_step)
8881 scroll_max = max (scroll_step,
8882 max (scroll_conservatively, temp_scroll_step));
8883 else if (NUMBERP (current_buffer->scroll_down_aggressively)
8884 || NUMBERP (current_buffer->scroll_up_aggressively))
8885 /* We're trying to scroll because of aggressive scrolling
8886 but no scroll_step is set. Choose an arbitrary one. Maybe
8887 there should be a variable for this. */
8888 scroll_max = 10;
8889 else
8890 scroll_max = 0;
8891 scroll_max *= CANON_Y_UNIT (f);
8892
8893 /* Decide whether we have to scroll down. Start at the window end
8894 and move this_scroll_margin up to find the position of the scroll
8895 margin. */
8896 window_end = Fwindow_end (window, Qt);
8897 CHARPOS (scroll_margin_pos) = XINT (window_end);
8898 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
8899 if (this_scroll_margin)
8900 {
8901 start_display (&it, w, scroll_margin_pos);
8902 move_it_vertically (&it, - this_scroll_margin);
8903 scroll_margin_pos = it.current.pos;
8904 }
8905
8906 if (PT >= CHARPOS (scroll_margin_pos))
8907 {
8908 int y0;
8909 #if 0
8910 int line_height;
8911 #endif
8912
8913 /* Point is in the scroll margin at the bottom of the window, or
8914 below. Compute a new window start that makes point visible. */
8915
8916 /* Compute the distance from the scroll margin to PT.
8917 Give up if the distance is greater than scroll_max. */
8918 start_display (&it, w, scroll_margin_pos);
8919 y0 = it.current_y;
8920 move_it_to (&it, PT, 0, it.last_visible_y, -1,
8921 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8922 #if 0 /* Taking the line's height into account here looks wrong. */
8923 line_height = (it.max_ascent + it.max_descent
8924 ? it.max_ascent + it.max_descent
8925 : last_height);
8926 dy = it.current_y + line_height - y0;
8927 #else
8928 /* With a scroll_margin of 0, scroll_margin_pos is at the window
8929 end, which is one line below the window. The iterator's
8930 current_y will be same as y0 in that case, but we have to
8931 scroll a line to make PT visible. That's the reason why 1 is
8932 added below. */
8933 dy = 1 + it.current_y - y0;
8934 #endif
8935
8936 if (dy > scroll_max)
8937 return 0;
8938
8939 /* Move the window start down. If scrolling conservatively,
8940 move it just enough down to make point visible. If
8941 scroll_step is set, move it down by scroll_step. */
8942 start_display (&it, w, startp);
8943
8944 if (scroll_conservatively)
8945 amount_to_scroll =
8946 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
8947 else if (scroll_step || temp_scroll_step)
8948 amount_to_scroll = scroll_max;
8949 else
8950 {
8951 aggressive = current_buffer->scroll_down_aggressively;
8952 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
8953 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
8954 if (NUMBERP (aggressive))
8955 amount_to_scroll = XFLOATINT (aggressive) * height;
8956 }
8957
8958 if (amount_to_scroll <= 0)
8959 return 0;
8960
8961 move_it_vertically (&it, amount_to_scroll);
8962 startp = it.current.pos;
8963 }
8964 else
8965 {
8966 /* See if point is inside the scroll margin at the top of the
8967 window. */
8968 scroll_margin_pos = startp;
8969 if (this_scroll_margin)
8970 {
8971 start_display (&it, w, startp);
8972 move_it_vertically (&it, this_scroll_margin);
8973 scroll_margin_pos = it.current.pos;
8974 }
8975
8976 if (PT < CHARPOS (scroll_margin_pos))
8977 {
8978 /* Point is in the scroll margin at the top of the window or
8979 above what is displayed in the window. */
8980 int y0;
8981
8982 /* Compute the vertical distance from PT to the scroll
8983 margin position. Give up if distance is greater than
8984 scroll_max. */
8985 SET_TEXT_POS (pos, PT, PT_BYTE);
8986 start_display (&it, w, pos);
8987 y0 = it.current_y;
8988 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
8989 it.last_visible_y, -1,
8990 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
8991 dy = it.current_y - y0;
8992 if (dy > scroll_max)
8993 return 0;
8994
8995 /* Compute new window start. */
8996 start_display (&it, w, startp);
8997
8998 if (scroll_conservatively)
8999 amount_to_scroll =
9000 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
9001 else if (scroll_step || temp_scroll_step)
9002 amount_to_scroll = scroll_max;
9003 else
9004 {
9005 aggressive = current_buffer->scroll_up_aggressively;
9006 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
9007 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
9008 if (NUMBERP (aggressive))
9009 amount_to_scroll = XFLOATINT (aggressive) * height;
9010 }
9011
9012 if (amount_to_scroll <= 0)
9013 return 0;
9014
9015 move_it_vertically (&it, - amount_to_scroll);
9016 startp = it.current.pos;
9017 }
9018 }
9019
9020 /* Run window scroll functions. */
9021 startp = run_window_scroll_functions (window, startp);
9022
9023 /* Display the window. Give up if new fonts are loaded, or if point
9024 doesn't appear. */
9025 if (!try_window (window, startp))
9026 rc = -1;
9027 else if (w->cursor.vpos < 0)
9028 {
9029 clear_glyph_matrix (w->desired_matrix);
9030 rc = 0;
9031 }
9032 else
9033 {
9034 /* Maybe forget recorded base line for line number display. */
9035 if (!just_this_one_p
9036 || current_buffer->clip_changed
9037 || BEG_UNCHANGED < CHARPOS (startp))
9038 w->base_line_number = Qnil;
9039
9040 /* If cursor ends up on a partially visible line, shift display
9041 lines up or down. */
9042 make_cursor_line_fully_visible (w);
9043 rc = 1;
9044 }
9045
9046 return rc;
9047 }
9048
9049
9050 /* Compute a suitable window start for window W if display of W starts
9051 on a continuation line. Value is non-zero if a new window start
9052 was computed.
9053
9054 The new window start will be computed, based on W's width, starting
9055 from the start of the continued line. It is the start of the
9056 screen line with the minimum distance from the old start W->start. */
9057
9058 static int
9059 compute_window_start_on_continuation_line (w)
9060 struct window *w;
9061 {
9062 struct text_pos pos, start_pos;
9063 int window_start_changed_p = 0;
9064
9065 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
9066
9067 /* If window start is on a continuation line... Window start may be
9068 < BEGV in case there's invisible text at the start of the
9069 buffer (M-x rmail, for example). */
9070 if (CHARPOS (start_pos) > BEGV
9071 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
9072 {
9073 struct it it;
9074 struct glyph_row *row;
9075
9076 /* Handle the case that the window start is out of range. */
9077 if (CHARPOS (start_pos) < BEGV)
9078 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9079 else if (CHARPOS (start_pos) > ZV)
9080 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
9081
9082 /* Find the start of the continued line. This should be fast
9083 because scan_buffer is fast (newline cache). */
9084 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
9085 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9086 row, DEFAULT_FACE_ID);
9087 reseat_at_previous_visible_line_start (&it);
9088
9089 /* If the line start is "too far" away from the window start,
9090 say it takes too much time to compute a new window start. */
9091 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9092 < XFASTINT (w->height) * XFASTINT (w->width))
9093 {
9094 int min_distance, distance;
9095
9096 /* Move forward by display lines to find the new window
9097 start. If window width was enlarged, the new start can
9098 be expected to be > the old start. If window width was
9099 decreased, the new window start will be < the old start.
9100 So, we're looking for the display line start with the
9101 minimum distance from the old window start. */
9102 pos = it.current.pos;
9103 min_distance = INFINITY;
9104 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9105 distance < min_distance)
9106 {
9107 min_distance = distance;
9108 pos = it.current.pos;
9109 move_it_by_lines (&it, 1, 0);
9110 }
9111
9112 /* Set the window start there. */
9113 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9114 window_start_changed_p = 1;
9115 }
9116 }
9117
9118 return window_start_changed_p;
9119 }
9120
9121
9122 /* Try cursor movement in case text has not changes in window WINDOW,
9123 with window start STARTP. Value is
9124
9125 1 if successful
9126
9127 0 if this method cannot be used
9128
9129 -1 if we know we have to scroll the display. *SCROLL_STEP is
9130 set to 1, under certain circumstances, if we want to scroll as
9131 if scroll-step were set to 1. See the code. */
9132
9133 static int
9134 try_cursor_movement (window, startp, scroll_step)
9135 Lisp_Object window;
9136 struct text_pos startp;
9137 int *scroll_step;
9138 {
9139 struct window *w = XWINDOW (window);
9140 struct frame *f = XFRAME (w->frame);
9141 int rc = 0;
9142
9143 /* Handle case where text has not changed, only point, and it has
9144 not moved off the frame. */
9145 if (/* Point may be in this window. */
9146 PT >= CHARPOS (startp)
9147 /* Selective display hasn't changed. */
9148 && !current_buffer->clip_changed
9149 /* Function force-mode-line-update is used to force a thorough
9150 redisplay. It sets either windows_or_buffers_changed or
9151 update_mode_lines. So don't take a shortcut here for these
9152 cases. */
9153 && !update_mode_lines
9154 && !windows_or_buffers_changed
9155 /* Can't use this case if highlighting a region. When a
9156 region exists, cursor movement has to do more than just
9157 set the cursor. */
9158 && !(!NILP (Vtransient_mark_mode)
9159 && !NILP (current_buffer->mark_active))
9160 && NILP (w->region_showing)
9161 && NILP (Vshow_trailing_whitespace)
9162 /* Right after splitting windows, last_point may be nil. */
9163 && INTEGERP (w->last_point)
9164 /* This code is not used for mini-buffer for the sake of the case
9165 of redisplaying to replace an echo area message; since in
9166 that case the mini-buffer contents per se are usually
9167 unchanged. This code is of no real use in the mini-buffer
9168 since the handling of this_line_start_pos, etc., in redisplay
9169 handles the same cases. */
9170 && !EQ (window, minibuf_window)
9171 /* When splitting windows or for new windows, it happens that
9172 redisplay is called with a nil window_end_vpos or one being
9173 larger than the window. This should really be fixed in
9174 window.c. I don't have this on my list, now, so we do
9175 approximately the same as the old redisplay code. --gerd. */
9176 && INTEGERP (w->window_end_vpos)
9177 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9178 && (FRAME_WINDOW_P (f)
9179 || !MARKERP (Voverlay_arrow_position)
9180 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9181 {
9182 int this_scroll_margin;
9183 struct glyph_row *row;
9184
9185 #if GLYPH_DEBUG
9186 debug_method_add (w, "cursor movement");
9187 #endif
9188
9189 /* Scroll if point within this distance from the top or bottom
9190 of the window. This is a pixel value. */
9191 this_scroll_margin = max (0, scroll_margin);
9192 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9193 this_scroll_margin *= CANON_Y_UNIT (f);
9194
9195 /* Start with the row the cursor was displayed during the last
9196 not paused redisplay. Give up if that row is not valid. */
9197 if (w->last_cursor.vpos < 0
9198 || w->last_cursor.vpos >= w->current_matrix->nrows)
9199 rc = -1;
9200 else
9201 {
9202 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9203 if (row->mode_line_p)
9204 ++row;
9205 if (!row->enabled_p)
9206 rc = -1;
9207 }
9208
9209 if (rc == 0)
9210 {
9211 int scroll_p = 0;
9212 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9213
9214
9215 if (PT > XFASTINT (w->last_point))
9216 {
9217 /* Point has moved forward. */
9218 while (MATRIX_ROW_END_CHARPOS (row) < PT
9219 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9220 {
9221 xassert (row->enabled_p);
9222 ++row;
9223 }
9224
9225 /* The end position of a row equals the start position
9226 of the next row. If PT is there, we would rather
9227 display it in the next line. */
9228 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9229 && MATRIX_ROW_END_CHARPOS (row) == PT
9230 && !cursor_row_p (w, row))
9231 ++row;
9232
9233 /* If within the scroll margin, scroll. Note that
9234 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9235 the next line would be drawn, and that
9236 this_scroll_margin can be zero. */
9237 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9238 || PT > MATRIX_ROW_END_CHARPOS (row)
9239 /* Line is completely visible last line in window
9240 and PT is to be set in the next line. */
9241 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9242 && PT == MATRIX_ROW_END_CHARPOS (row)
9243 && !row->ends_at_zv_p
9244 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9245 scroll_p = 1;
9246 }
9247 else if (PT < XFASTINT (w->last_point))
9248 {
9249 /* Cursor has to be moved backward. Note that PT >=
9250 CHARPOS (startp) because of the outer
9251 if-statement. */
9252 while (!row->mode_line_p
9253 && (MATRIX_ROW_START_CHARPOS (row) > PT
9254 || (MATRIX_ROW_START_CHARPOS (row) == PT
9255 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9256 && (row->y > this_scroll_margin
9257 || CHARPOS (startp) == BEGV))
9258 {
9259 xassert (row->enabled_p);
9260 --row;
9261 }
9262
9263 /* Consider the following case: Window starts at BEGV,
9264 there is invisible, intangible text at BEGV, so that
9265 display starts at some point START > BEGV. It can
9266 happen that we are called with PT somewhere between
9267 BEGV and START. Try to handle that case. */
9268 if (row < w->current_matrix->rows
9269 || row->mode_line_p)
9270 {
9271 row = w->current_matrix->rows;
9272 if (row->mode_line_p)
9273 ++row;
9274 }
9275
9276 /* Due to newlines in overlay strings, we may have to
9277 skip forward over overlay strings. */
9278 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9279 && MATRIX_ROW_END_CHARPOS (row) == PT
9280 && !cursor_row_p (w, row))
9281 ++row;
9282
9283 /* If within the scroll margin, scroll. */
9284 if (row->y < this_scroll_margin
9285 && CHARPOS (startp) != BEGV)
9286 scroll_p = 1;
9287 }
9288
9289 if (PT < MATRIX_ROW_START_CHARPOS (row)
9290 || PT > MATRIX_ROW_END_CHARPOS (row))
9291 {
9292 /* if PT is not in the glyph row, give up. */
9293 rc = -1;
9294 }
9295 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9296 {
9297 /* If we end up in a partially visible line, let's make it
9298 fully visible, except when it's taller than the window,
9299 in which case we can't do much about it. */
9300 if (row->height > window_box_height (w))
9301 {
9302 *scroll_step = 1;
9303 rc = -1;
9304 }
9305 else
9306 {
9307 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9308 try_window (window, startp);
9309 make_cursor_line_fully_visible (w);
9310 rc = 1;
9311 }
9312 }
9313 else if (scroll_p)
9314 rc = -1;
9315 else
9316 {
9317 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9318 rc = 1;
9319 }
9320 }
9321 }
9322
9323 return rc;
9324 }
9325
9326
9327 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9328 selected_window is redisplayed. */
9329
9330 static void
9331 redisplay_window (window, just_this_one_p)
9332 Lisp_Object window;
9333 int just_this_one_p;
9334 {
9335 struct window *w = XWINDOW (window);
9336 struct frame *f = XFRAME (w->frame);
9337 struct buffer *buffer = XBUFFER (w->buffer);
9338 struct buffer *old = current_buffer;
9339 struct text_pos lpoint, opoint, startp;
9340 int update_mode_line;
9341 int tem;
9342 struct it it;
9343 /* Record it now because it's overwritten. */
9344 int current_matrix_up_to_date_p = 0;
9345 int temp_scroll_step = 0;
9346 int count = BINDING_STACK_SIZE ();
9347 int rc;
9348
9349 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9350 opoint = lpoint;
9351
9352 /* W must be a leaf window here. */
9353 xassert (!NILP (w->buffer));
9354 #if GLYPH_DEBUG
9355 *w->desired_matrix->method = 0;
9356 #endif
9357
9358 specbind (Qinhibit_point_motion_hooks, Qt);
9359
9360 reconsider_clip_changes (w, buffer);
9361
9362 /* Has the mode line to be updated? */
9363 update_mode_line = (!NILP (w->update_mode_line)
9364 || update_mode_lines
9365 || buffer->clip_changed);
9366
9367 if (MINI_WINDOW_P (w))
9368 {
9369 if (w == XWINDOW (echo_area_window)
9370 && !NILP (echo_area_buffer[0]))
9371 {
9372 if (update_mode_line)
9373 /* We may have to update a tty frame's menu bar or a
9374 tool-bar. Example `M-x C-h C-h C-g'. */
9375 goto finish_menu_bars;
9376 else
9377 /* We've already displayed the echo area glyphs in this window. */
9378 goto finish_scroll_bars;
9379 }
9380 else if (w != XWINDOW (minibuf_window))
9381 {
9382 /* W is a mini-buffer window, but it's not the currently
9383 active one, so clear it. */
9384 int yb = window_text_bottom_y (w);
9385 struct glyph_row *row;
9386 int y;
9387
9388 for (y = 0, row = w->desired_matrix->rows;
9389 y < yb;
9390 y += row->height, ++row)
9391 blank_row (w, row, y);
9392 goto finish_scroll_bars;
9393 }
9394 }
9395
9396 /* Otherwise set up data on this window; select its buffer and point
9397 value. */
9398 /* Really select the buffer, for the sake of buffer-local
9399 variables. */
9400 set_buffer_internal_1 (XBUFFER (w->buffer));
9401 SET_TEXT_POS (opoint, PT, PT_BYTE);
9402
9403 current_matrix_up_to_date_p
9404 = (!NILP (w->window_end_valid)
9405 && !current_buffer->clip_changed
9406 && XFASTINT (w->last_modified) >= MODIFF
9407 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
9408
9409 /* When windows_or_buffers_changed is non-zero, we can't rely on
9410 the window end being valid, so set it to nil there. */
9411 if (windows_or_buffers_changed)
9412 {
9413 /* If window starts on a continuation line, maybe adjust the
9414 window start in case the window's width changed. */
9415 if (XMARKER (w->start)->buffer == current_buffer)
9416 compute_window_start_on_continuation_line (w);
9417
9418 w->window_end_valid = Qnil;
9419 }
9420
9421 /* Some sanity checks. */
9422 CHECK_WINDOW_END (w);
9423 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
9424 abort ();
9425 if (BYTEPOS (opoint) < CHARPOS (opoint))
9426 abort ();
9427
9428 /* If %c is in mode line, update it if needed. */
9429 if (!NILP (w->column_number_displayed)
9430 /* This alternative quickly identifies a common case
9431 where no change is needed. */
9432 && !(PT == XFASTINT (w->last_point)
9433 && XFASTINT (w->last_modified) >= MODIFF
9434 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9435 && XFASTINT (w->column_number_displayed) != current_column ())
9436 update_mode_line = 1;
9437
9438 /* Count number of windows showing the selected buffer. An indirect
9439 buffer counts as its base buffer. */
9440 if (!just_this_one_p)
9441 {
9442 struct buffer *current_base, *window_base;
9443 current_base = current_buffer;
9444 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9445 if (current_base->base_buffer)
9446 current_base = current_base->base_buffer;
9447 if (window_base->base_buffer)
9448 window_base = window_base->base_buffer;
9449 if (current_base == window_base)
9450 buffer_shared++;
9451 }
9452
9453 /* Point refers normally to the selected window. For any other
9454 window, set up appropriate value. */
9455 if (!EQ (window, selected_window))
9456 {
9457 int new_pt = XMARKER (w->pointm)->charpos;
9458 int new_pt_byte = marker_byte_position (w->pointm);
9459 if (new_pt < BEGV)
9460 {
9461 new_pt = BEGV;
9462 new_pt_byte = BEGV_BYTE;
9463 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
9464 }
9465 else if (new_pt > (ZV - 1))
9466 {
9467 new_pt = ZV;
9468 new_pt_byte = ZV_BYTE;
9469 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
9470 }
9471
9472 /* We don't use SET_PT so that the point-motion hooks don't run. */
9473 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
9474 }
9475
9476 /* If any of the character widths specified in the display table
9477 have changed, invalidate the width run cache. It's true that
9478 this may be a bit late to catch such changes, but the rest of
9479 redisplay goes (non-fatally) haywire when the display table is
9480 changed, so why should we worry about doing any better? */
9481 if (current_buffer->width_run_cache)
9482 {
9483 struct Lisp_Char_Table *disptab = buffer_display_table ();
9484
9485 if (! disptab_matches_widthtab (disptab,
9486 XVECTOR (current_buffer->width_table)))
9487 {
9488 invalidate_region_cache (current_buffer,
9489 current_buffer->width_run_cache,
9490 BEG, Z);
9491 recompute_width_table (current_buffer, disptab);
9492 }
9493 }
9494
9495 /* If window-start is screwed up, choose a new one. */
9496 if (XMARKER (w->start)->buffer != current_buffer)
9497 goto recenter;
9498
9499 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9500
9501 /* If someone specified a new starting point but did not insist,
9502 check whether it can be used. */
9503 if (!NILP (w->optional_new_start)
9504 && CHARPOS (startp) >= BEGV
9505 && CHARPOS (startp) <= ZV)
9506 {
9507 w->optional_new_start = Qnil;
9508 start_display (&it, w, startp);
9509 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9510 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9511 if (IT_CHARPOS (it) == PT)
9512 w->force_start = Qt;
9513 }
9514
9515 /* Handle case where place to start displaying has been specified,
9516 unless the specified location is outside the accessible range. */
9517 if (!NILP (w->force_start)
9518 || w->frozen_window_start_p)
9519 {
9520 w->force_start = Qnil;
9521 w->vscroll = 0;
9522 w->window_end_valid = Qnil;
9523
9524 /* Forget any recorded base line for line number display. */
9525 if (!current_matrix_up_to_date_p
9526 || current_buffer->clip_changed)
9527 w->base_line_number = Qnil;
9528
9529 /* Redisplay the mode line. Select the buffer properly for that.
9530 Also, run the hook window-scroll-functions
9531 because we have scrolled. */
9532 /* Note, we do this after clearing force_start because
9533 if there's an error, it is better to forget about force_start
9534 than to get into an infinite loop calling the hook functions
9535 and having them get more errors. */
9536 if (!update_mode_line
9537 || ! NILP (Vwindow_scroll_functions))
9538 {
9539 update_mode_line = 1;
9540 w->update_mode_line = Qt;
9541 startp = run_window_scroll_functions (window, startp);
9542 }
9543
9544 XSETFASTINT (w->last_modified, 0);
9545 XSETFASTINT (w->last_overlay_modified, 0);
9546 if (CHARPOS (startp) < BEGV)
9547 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9548 else if (CHARPOS (startp) > ZV)
9549 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9550
9551 /* Redisplay, then check if cursor has been set during the
9552 redisplay. Give up if new fonts were loaded. */
9553 if (!try_window (window, startp))
9554 {
9555 w->force_start = Qt;
9556 clear_glyph_matrix (w->desired_matrix);
9557 goto restore_buffers;
9558 }
9559
9560 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
9561 {
9562 /* If point does not appear, try to move point so it does
9563 appear. The desired matrix has been built above, so we
9564 can use it here. */
9565 int window_height;
9566 struct glyph_row *row;
9567
9568 window_height = window_box_height (w) / 2;
9569 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9570 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
9571 ++row;
9572
9573 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9574 MATRIX_ROW_START_BYTEPOS (row));
9575
9576 if (w != XWINDOW (selected_window))
9577 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
9578 else if (current_buffer == old)
9579 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9580
9581 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9582
9583 /* If we are highlighting the region, then we just changed
9584 the region, so redisplay to show it. */
9585 if (!NILP (Vtransient_mark_mode)
9586 && !NILP (current_buffer->mark_active))
9587 {
9588 clear_glyph_matrix (w->desired_matrix);
9589 if (!try_window (window, startp))
9590 goto restore_buffers;
9591 }
9592 }
9593
9594 make_cursor_line_fully_visible (w);
9595 #if GLYPH_DEBUG
9596 debug_method_add (w, "forced window start");
9597 #endif
9598 goto done;
9599 }
9600
9601 /* Handle case where text has not changed, only point, and it has
9602 not moved off the frame. */
9603 if (current_matrix_up_to_date_p
9604 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9605 rc != 0))
9606 {
9607 if (rc == -1)
9608 goto try_to_scroll;
9609 else
9610 goto done;
9611 }
9612 /* If current starting point was originally the beginning of a line
9613 but no longer is, find a new starting point. */
9614 else if (!NILP (w->start_at_line_beg)
9615 && !(CHARPOS (startp) <= BEGV
9616 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
9617 {
9618 #if GLYPH_DEBUG
9619 debug_method_add (w, "recenter 1");
9620 #endif
9621 goto recenter;
9622 }
9623
9624 /* Try scrolling with try_window_id. */
9625 else if (/* Windows and buffers haven't changed. */
9626 !windows_or_buffers_changed
9627 /* Window must be either use window-based redisplay or
9628 be full width. */
9629 && (FRAME_WINDOW_P (f)
9630 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
9631 && !MINI_WINDOW_P (w)
9632 /* Point is not known NOT to appear in window. */
9633 && PT >= CHARPOS (startp)
9634 && XFASTINT (w->last_modified)
9635 /* Window is not hscrolled. */
9636 && XFASTINT (w->hscroll) == 0
9637 /* Selective display has not changed. */
9638 && !current_buffer->clip_changed
9639 /* Current matrix is up to date. */
9640 && !NILP (w->window_end_valid)
9641 /* Can't use this case if highlighting a region because
9642 a cursor movement will do more than just set the cursor. */
9643 && !(!NILP (Vtransient_mark_mode)
9644 && !NILP (current_buffer->mark_active))
9645 && NILP (w->region_showing)
9646 && NILP (Vshow_trailing_whitespace)
9647 /* Overlay arrow position and string not changed. */
9648 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
9649 && EQ (last_arrow_string, Voverlay_arrow_string)
9650 /* Value is > 0 if update has been done, it is -1 if we
9651 know that the same window start will not work. It is 0
9652 if unsuccessful for some other reason. */
9653 && (tem = try_window_id (w)) != 0)
9654 {
9655 #if GLYPH_DEBUG
9656 debug_method_add (w, "try_window_id %d", tem);
9657 #endif
9658
9659 if (fonts_changed_p)
9660 goto restore_buffers;
9661 if (tem > 0)
9662 goto done;
9663
9664 /* Otherwise try_window_id has returned -1 which means that we
9665 don't want the alternative below this comment to execute. */
9666 }
9667 else if (CHARPOS (startp) >= BEGV
9668 && CHARPOS (startp) <= ZV
9669 && PT >= CHARPOS (startp)
9670 && (CHARPOS (startp) < ZV
9671 /* Avoid starting at end of buffer. */
9672 || CHARPOS (startp) == BEGV
9673 || (XFASTINT (w->last_modified) >= MODIFF
9674 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
9675 {
9676 #if GLYPH_DEBUG
9677 debug_method_add (w, "same window start");
9678 #endif
9679
9680 /* Try to redisplay starting at same place as before.
9681 If point has not moved off frame, accept the results. */
9682 if (!current_matrix_up_to_date_p
9683 /* Don't use try_window_reusing_current_matrix in this case
9684 because a window scroll function can have changed the
9685 buffer. */
9686 || !NILP (Vwindow_scroll_functions)
9687 || MINI_WINDOW_P (w)
9688 || !try_window_reusing_current_matrix (w))
9689 {
9690 IF_DEBUG (debug_method_add (w, "1"));
9691 try_window (window, startp);
9692 }
9693
9694 if (fonts_changed_p)
9695 goto restore_buffers;
9696
9697 if (w->cursor.vpos >= 0)
9698 {
9699 if (!just_this_one_p
9700 || current_buffer->clip_changed
9701 || BEG_UNCHANGED < CHARPOS (startp))
9702 /* Forget any recorded base line for line number display. */
9703 w->base_line_number = Qnil;
9704
9705 make_cursor_line_fully_visible (w);
9706 goto done;
9707 }
9708 else
9709 clear_glyph_matrix (w->desired_matrix);
9710 }
9711
9712 try_to_scroll:
9713
9714 XSETFASTINT (w->last_modified, 0);
9715 XSETFASTINT (w->last_overlay_modified, 0);
9716
9717 /* Redisplay the mode line. Select the buffer properly for that. */
9718 if (!update_mode_line)
9719 {
9720 update_mode_line = 1;
9721 w->update_mode_line = Qt;
9722 }
9723
9724 /* Try to scroll by specified few lines. */
9725 if ((scroll_conservatively
9726 || scroll_step
9727 || temp_scroll_step
9728 || NUMBERP (current_buffer->scroll_up_aggressively)
9729 || NUMBERP (current_buffer->scroll_down_aggressively))
9730 && !current_buffer->clip_changed
9731 && CHARPOS (startp) >= BEGV
9732 && CHARPOS (startp) <= ZV)
9733 {
9734 /* The function returns -1 if new fonts were loaded, 1 if
9735 successful, 0 if not successful. */
9736 int rc = try_scrolling (window, just_this_one_p,
9737 scroll_conservatively,
9738 scroll_step,
9739 temp_scroll_step);
9740 if (rc > 0)
9741 goto done;
9742 else if (rc < 0)
9743 goto restore_buffers;
9744 }
9745
9746 /* Finally, just choose place to start which centers point */
9747
9748 recenter:
9749
9750 #if GLYPH_DEBUG
9751 debug_method_add (w, "recenter");
9752 #endif
9753
9754 /* w->vscroll = 0; */
9755
9756 /* Forget any previously recorded base line for line number display. */
9757 if (!current_matrix_up_to_date_p
9758 || current_buffer->clip_changed)
9759 w->base_line_number = Qnil;
9760
9761 /* Move backward half the height of the window. */
9762 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9763 it.current_y = it.last_visible_y;
9764 move_it_vertically_backward (&it, it.last_visible_y / 2);
9765 xassert (IT_CHARPOS (it) >= BEGV);
9766
9767 /* The function move_it_vertically_backward may move over more
9768 than the specified y-distance. If it->w is small, e.g. a
9769 mini-buffer window, we may end up in front of the window's
9770 display area. Start displaying at the start of the line
9771 containing PT in this case. */
9772 if (it.current_y <= 0)
9773 {
9774 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
9775 move_it_vertically (&it, 0);
9776 xassert (IT_CHARPOS (it) <= PT);
9777 it.current_y = 0;
9778 }
9779
9780 it.current_x = it.hpos = 0;
9781
9782 /* Set startp here explicitly in case that helps avoid an infinite loop
9783 in case the window-scroll-functions functions get errors. */
9784 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
9785
9786 /* Run scroll hooks. */
9787 startp = run_window_scroll_functions (window, it.current.pos);
9788
9789 /* Redisplay the window. */
9790 if (!current_matrix_up_to_date_p
9791 || windows_or_buffers_changed
9792 /* Don't use try_window_reusing_current_matrix in this case
9793 because it can have changed the buffer. */
9794 || !NILP (Vwindow_scroll_functions)
9795 || !just_this_one_p
9796 || MINI_WINDOW_P (w)
9797 || !try_window_reusing_current_matrix (w))
9798 try_window (window, startp);
9799
9800 /* If new fonts have been loaded (due to fontsets), give up. We
9801 have to start a new redisplay since we need to re-adjust glyph
9802 matrices. */
9803 if (fonts_changed_p)
9804 goto restore_buffers;
9805
9806 /* If cursor did not appear assume that the middle of the window is
9807 in the first line of the window. Do it again with the next line.
9808 (Imagine a window of height 100, displaying two lines of height
9809 60. Moving back 50 from it->last_visible_y will end in the first
9810 line.) */
9811 if (w->cursor.vpos < 0)
9812 {
9813 if (!NILP (w->window_end_valid)
9814 && PT >= Z - XFASTINT (w->window_end_pos))
9815 {
9816 clear_glyph_matrix (w->desired_matrix);
9817 move_it_by_lines (&it, 1, 0);
9818 try_window (window, it.current.pos);
9819 }
9820 else if (PT < IT_CHARPOS (it))
9821 {
9822 clear_glyph_matrix (w->desired_matrix);
9823 move_it_by_lines (&it, -1, 0);
9824 try_window (window, it.current.pos);
9825 }
9826 else
9827 {
9828 /* Not much we can do about it. */
9829 }
9830 }
9831
9832 /* Consider the following case: Window starts at BEGV, there is
9833 invisible, intangible text at BEGV, so that display starts at
9834 some point START > BEGV. It can happen that we are called with
9835 PT somewhere between BEGV and START. Try to handle that case. */
9836 if (w->cursor.vpos < 0)
9837 {
9838 struct glyph_row *row = w->current_matrix->rows;
9839 if (row->mode_line_p)
9840 ++row;
9841 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9842 }
9843
9844 make_cursor_line_fully_visible (w);
9845
9846 done:
9847
9848 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9849 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
9850 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
9851 ? Qt : Qnil);
9852
9853 /* Display the mode line, if we must. */
9854 if ((update_mode_line
9855 /* If window not full width, must redo its mode line
9856 if (a) the window to its side is being redone and
9857 (b) we do a frame-based redisplay. This is a consequence
9858 of how inverted lines are drawn in frame-based redisplay. */
9859 || (!just_this_one_p
9860 && !FRAME_WINDOW_P (f)
9861 && !WINDOW_FULL_WIDTH_P (w))
9862 /* Line number to display. */
9863 || INTEGERP (w->base_line_pos)
9864 /* Column number is displayed and different from the one displayed. */
9865 || (!NILP (w->column_number_displayed)
9866 && XFASTINT (w->column_number_displayed) != current_column ()))
9867 /* This means that the window has a mode line. */
9868 && (WINDOW_WANTS_MODELINE_P (w)
9869 || WINDOW_WANTS_HEADER_LINE_P (w)))
9870 {
9871 Lisp_Object old_selected_frame;
9872
9873 old_selected_frame = selected_frame;
9874
9875 XSETFRAME (selected_frame, f);
9876 display_mode_lines (w);
9877 selected_frame = old_selected_frame;
9878
9879 /* If mode line height has changed, arrange for a thorough
9880 immediate redisplay using the correct mode line height. */
9881 if (WINDOW_WANTS_MODELINE_P (w)
9882 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
9883 {
9884 fonts_changed_p = 1;
9885 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
9886 = DESIRED_MODE_LINE_HEIGHT (w);
9887 }
9888
9889 /* If top line height has changed, arrange for a thorough
9890 immediate redisplay using the correct mode line height. */
9891 if (WINDOW_WANTS_HEADER_LINE_P (w)
9892 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
9893 {
9894 fonts_changed_p = 1;
9895 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
9896 = DESIRED_HEADER_LINE_HEIGHT (w);
9897 }
9898
9899 if (fonts_changed_p)
9900 goto restore_buffers;
9901 }
9902
9903 if (!line_number_displayed
9904 && !BUFFERP (w->base_line_pos))
9905 {
9906 w->base_line_pos = Qnil;
9907 w->base_line_number = Qnil;
9908 }
9909
9910 finish_menu_bars:
9911
9912 /* When we reach a frame's selected window, redo the frame's menu bar. */
9913 if (update_mode_line
9914 && EQ (FRAME_SELECTED_WINDOW (f), window))
9915 {
9916 int redisplay_menu_p = 0;
9917
9918 if (FRAME_WINDOW_P (f))
9919 {
9920 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
9921 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
9922 #else
9923 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9924 #endif
9925 }
9926 else
9927 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
9928
9929 if (redisplay_menu_p)
9930 display_menu_bar (w);
9931
9932 #ifdef HAVE_WINDOW_SYSTEM
9933 if (WINDOWP (f->tool_bar_window)
9934 && (FRAME_TOOL_BAR_LINES (f) > 0
9935 || auto_resize_tool_bars_p))
9936 redisplay_tool_bar (f);
9937 #endif
9938 }
9939
9940 finish_scroll_bars:
9941
9942 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9943 {
9944 int start, end, whole;
9945
9946 /* Calculate the start and end positions for the current window.
9947 At some point, it would be nice to choose between scrollbars
9948 which reflect the whole buffer size, with special markers
9949 indicating narrowing, and scrollbars which reflect only the
9950 visible region.
9951
9952 Note that mini-buffers sometimes aren't displaying any text. */
9953 if (!MINI_WINDOW_P (w)
9954 || (w == XWINDOW (minibuf_window)
9955 && NILP (echo_area_buffer[0])))
9956 {
9957 whole = ZV - BEGV;
9958 start = marker_position (w->start) - BEGV;
9959 /* I don't think this is guaranteed to be right. For the
9960 moment, we'll pretend it is. */
9961 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
9962
9963 if (end < start)
9964 end = start;
9965 if (whole < (end - start))
9966 whole = end - start;
9967 }
9968 else
9969 start = end = whole = 0;
9970
9971 /* Indicate what this scroll bar ought to be displaying now. */
9972 (*set_vertical_scroll_bar_hook) (w, end - start, whole, start);
9973
9974 /* Note that we actually used the scroll bar attached to this
9975 window, so it shouldn't be deleted at the end of redisplay. */
9976 (*redeem_scroll_bar_hook) (w);
9977 }
9978
9979 restore_buffers:
9980
9981 /* Restore current_buffer and value of point in it. */
9982 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
9983 set_buffer_internal_1 (old);
9984 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
9985
9986 unbind_to (count, Qnil);
9987 }
9988
9989
9990 /* Build the complete desired matrix of WINDOW with a window start
9991 buffer position POS. Value is non-zero if successful. It is zero
9992 if fonts were loaded during redisplay which makes re-adjusting
9993 glyph matrices necessary. */
9994
9995 int
9996 try_window (window, pos)
9997 Lisp_Object window;
9998 struct text_pos pos;
9999 {
10000 struct window *w = XWINDOW (window);
10001 struct it it;
10002 struct glyph_row *last_text_row = NULL;
10003
10004 /* Make POS the new window start. */
10005 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
10006
10007 /* Mark cursor position as unknown. No overlay arrow seen. */
10008 w->cursor.vpos = -1;
10009 overlay_arrow_seen = 0;
10010
10011 /* Initialize iterator and info to start at POS. */
10012 start_display (&it, w, pos);
10013
10014 /* Display all lines of W. */
10015 while (it.current_y < it.last_visible_y)
10016 {
10017 if (display_line (&it))
10018 last_text_row = it.glyph_row - 1;
10019 if (fonts_changed_p)
10020 return 0;
10021 }
10022
10023 /* If bottom moved off end of frame, change mode line percentage. */
10024 if (XFASTINT (w->window_end_pos) <= 0
10025 && Z != IT_CHARPOS (it))
10026 w->update_mode_line = Qt;
10027
10028 /* Set window_end_pos to the offset of the last character displayed
10029 on the window from the end of current_buffer. Set
10030 window_end_vpos to its row number. */
10031 if (last_text_row)
10032 {
10033 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10034 w->window_end_bytepos
10035 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10036 XSETFASTINT (w->window_end_pos,
10037 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10038 XSETFASTINT (w->window_end_vpos,
10039 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10040 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10041 ->displays_text_p);
10042 }
10043 else
10044 {
10045 w->window_end_bytepos = 0;
10046 XSETFASTINT (w->window_end_pos, 0);
10047 XSETFASTINT (w->window_end_vpos, 0);
10048 }
10049
10050 /* But that is not valid info until redisplay finishes. */
10051 w->window_end_valid = Qnil;
10052 return 1;
10053 }
10054
10055
10056 \f
10057 /************************************************************************
10058 Window redisplay reusing current matrix when buffer has not changed
10059 ************************************************************************/
10060
10061 /* Try redisplay of window W showing an unchanged buffer with a
10062 different window start than the last time it was displayed by
10063 reusing its current matrix. Value is non-zero if successful.
10064 W->start is the new window start. */
10065
10066 static int
10067 try_window_reusing_current_matrix (w)
10068 struct window *w;
10069 {
10070 struct frame *f = XFRAME (w->frame);
10071 struct glyph_row *row, *bottom_row;
10072 struct it it;
10073 struct run run;
10074 struct text_pos start, new_start;
10075 int nrows_scrolled, i;
10076 struct glyph_row *last_text_row;
10077 struct glyph_row *last_reused_text_row;
10078 struct glyph_row *start_row;
10079 int start_vpos, min_y, max_y;
10080
10081 if (/* This function doesn't handle terminal frames. */
10082 !FRAME_WINDOW_P (f)
10083 /* Don't try to reuse the display if windows have been split
10084 or such. */
10085 || windows_or_buffers_changed)
10086 return 0;
10087
10088 /* Can't do this if region may have changed. */
10089 if ((!NILP (Vtransient_mark_mode)
10090 && !NILP (current_buffer->mark_active))
10091 || !NILP (w->region_showing)
10092 || !NILP (Vshow_trailing_whitespace))
10093 return 0;
10094
10095 /* If top-line visibility has changed, give up. */
10096 if (WINDOW_WANTS_HEADER_LINE_P (w)
10097 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
10098 return 0;
10099
10100 /* Give up if old or new display is scrolled vertically. We could
10101 make this function handle this, but right now it doesn't. */
10102 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10103 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10104 return 0;
10105
10106 /* The variable new_start now holds the new window start. The old
10107 start `start' can be determined from the current matrix. */
10108 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10109 start = start_row->start.pos;
10110 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
10111
10112 /* Clear the desired matrix for the display below. */
10113 clear_glyph_matrix (w->desired_matrix);
10114
10115 if (CHARPOS (new_start) <= CHARPOS (start))
10116 {
10117 int first_row_y;
10118
10119 IF_DEBUG (debug_method_add (w, "twu1"));
10120
10121 /* Display up to a row that can be reused. The variable
10122 last_text_row is set to the last row displayed that displays
10123 text. Note that it.vpos == 0 if or if not there is a
10124 header-line; it's not the same as the MATRIX_ROW_VPOS! */
10125 start_display (&it, w, new_start);
10126 first_row_y = it.current_y;
10127 w->cursor.vpos = -1;
10128 last_text_row = last_reused_text_row = NULL;
10129
10130 while (it.current_y < it.last_visible_y
10131 && IT_CHARPOS (it) < CHARPOS (start)
10132 && !fonts_changed_p)
10133 if (display_line (&it))
10134 last_text_row = it.glyph_row - 1;
10135
10136 /* A value of current_y < last_visible_y means that we stopped
10137 at the previous window start, which in turn means that we
10138 have at least one reusable row. */
10139 if (it.current_y < it.last_visible_y)
10140 {
10141 /* IT.vpos always starts from 0; it counts text lines. */
10142 nrows_scrolled = it.vpos;
10143
10144 /* Find PT if not already found in the lines displayed. */
10145 if (w->cursor.vpos < 0)
10146 {
10147 int dy = it.current_y - first_row_y;
10148
10149 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10150 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10151 {
10152 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10153 && PT < MATRIX_ROW_END_CHARPOS (row))
10154 {
10155 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10156 dy, nrows_scrolled);
10157 break;
10158 }
10159
10160 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10161 break;
10162
10163 ++row;
10164 }
10165
10166 /* Give up if point was not found. This shouldn't
10167 happen often; not more often than with try_window
10168 itself. */
10169 if (w->cursor.vpos < 0)
10170 {
10171 clear_glyph_matrix (w->desired_matrix);
10172 return 0;
10173 }
10174 }
10175
10176 /* Scroll the display. Do it before the current matrix is
10177 changed. The problem here is that update has not yet
10178 run, i.e. part of the current matrix is not up to date.
10179 scroll_run_hook will clear the cursor, and use the
10180 current matrix to get the height of the row the cursor is
10181 in. */
10182 run.current_y = first_row_y;
10183 run.desired_y = it.current_y;
10184 run.height = it.last_visible_y - it.current_y;
10185
10186 if (run.height > 0 && run.current_y != run.desired_y)
10187 {
10188 update_begin (f);
10189 rif->update_window_begin_hook (w);
10190 rif->clear_mouse_face (w);
10191 rif->scroll_run_hook (w, &run);
10192 rif->update_window_end_hook (w, 0, 0);
10193 update_end (f);
10194 }
10195
10196 /* Shift current matrix down by nrows_scrolled lines. */
10197 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10198 rotate_matrix (w->current_matrix,
10199 start_vpos,
10200 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10201 nrows_scrolled);
10202
10203 /* Disable lines not reused. */
10204 for (i = 0; i < it.vpos; ++i)
10205 (start_row + i)->enabled_p = 0;
10206
10207 /* Re-compute Y positions. */
10208 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10209 max_y = it.last_visible_y;
10210 for (row = start_row + nrows_scrolled;
10211 row < bottom_row;
10212 ++row)
10213 {
10214 row->y = it.current_y;
10215
10216 if (row->y < min_y)
10217 row->visible_height = row->height - (min_y - row->y);
10218 else if (row->y + row->height > max_y)
10219 row->visible_height
10220 = row->height - (row->y + row->height - max_y);
10221 else
10222 row->visible_height = row->height;
10223
10224 it.current_y += row->height;
10225
10226 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10227 last_reused_text_row = row;
10228 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10229 break;
10230 }
10231 }
10232
10233 /* Update window_end_pos etc.; last_reused_text_row is the last
10234 reused row from the current matrix containing text, if any.
10235 The value of last_text_row is the last displayed line
10236 containing text. */
10237 if (last_reused_text_row)
10238 {
10239 w->window_end_bytepos
10240 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
10241 XSETFASTINT (w->window_end_pos,
10242 Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10243 XSETFASTINT (w->window_end_vpos,
10244 MATRIX_ROW_VPOS (last_reused_text_row,
10245 w->current_matrix));
10246 }
10247 else if (last_text_row)
10248 {
10249 w->window_end_bytepos
10250 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10251 XSETFASTINT (w->window_end_pos,
10252 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10253 XSETFASTINT (w->window_end_vpos,
10254 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10255 }
10256 else
10257 {
10258 /* This window must be completely empty. */
10259 w->window_end_bytepos = 0;
10260 XSETFASTINT (w->window_end_pos, 0);
10261 XSETFASTINT (w->window_end_vpos, 0);
10262 }
10263 w->window_end_valid = Qnil;
10264
10265 /* Update hint: don't try scrolling again in update_window. */
10266 w->desired_matrix->no_scrolling_p = 1;
10267
10268 #if GLYPH_DEBUG
10269 debug_method_add (w, "try_window_reusing_current_matrix 1");
10270 #endif
10271 return 1;
10272 }
10273 else if (CHARPOS (new_start) > CHARPOS (start))
10274 {
10275 struct glyph_row *pt_row, *row;
10276 struct glyph_row *first_reusable_row;
10277 struct glyph_row *first_row_to_display;
10278 int dy;
10279 int yb = window_text_bottom_y (w);
10280
10281 IF_DEBUG (debug_method_add (w, "twu2"));
10282
10283 /* Find the row starting at new_start, if there is one. Don't
10284 reuse a partially visible line at the end. */
10285 first_reusable_row = start_row;
10286 while (first_reusable_row->enabled_p
10287 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10288 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10289 < CHARPOS (new_start)))
10290 ++first_reusable_row;
10291
10292 /* Give up if there is no row to reuse. */
10293 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
10294 || !first_reusable_row->enabled_p
10295 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10296 != CHARPOS (new_start)))
10297 return 0;
10298
10299 /* We can reuse fully visible rows beginning with
10300 first_reusable_row to the end of the window. Set
10301 first_row_to_display to the first row that cannot be reused.
10302 Set pt_row to the row containing point, if there is any. */
10303 first_row_to_display = first_reusable_row;
10304 pt_row = NULL;
10305 while (MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb)
10306 {
10307 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10308 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
10309 pt_row = first_row_to_display;
10310
10311 ++first_row_to_display;
10312 }
10313
10314 /* Start displaying at the start of first_row_to_display. */
10315 xassert (first_row_to_display->y < yb);
10316 init_to_row_start (&it, w, first_row_to_display);
10317 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10318 - start_vpos);
10319 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10320 - nrows_scrolled);
10321 it.current_y = (first_row_to_display->y - first_reusable_row->y
10322 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10323
10324 /* Display lines beginning with first_row_to_display in the
10325 desired matrix. Set last_text_row to the last row displayed
10326 that displays text. */
10327 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10328 if (pt_row == NULL)
10329 w->cursor.vpos = -1;
10330 last_text_row = NULL;
10331 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10332 if (display_line (&it))
10333 last_text_row = it.glyph_row - 1;
10334
10335 /* Give up If point isn't in a row displayed or reused. */
10336 if (w->cursor.vpos < 0)
10337 {
10338 clear_glyph_matrix (w->desired_matrix);
10339 return 0;
10340 }
10341
10342 /* If point is in a reused row, adjust y and vpos of the cursor
10343 position. */
10344 if (pt_row)
10345 {
10346 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10347 w->current_matrix);
10348 w->cursor.y -= first_reusable_row->y;
10349 }
10350
10351 /* Scroll the display. */
10352 run.current_y = first_reusable_row->y;
10353 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10354 run.height = it.last_visible_y - run.current_y;
10355 dy = run.current_y - run.desired_y;
10356
10357 if (run.height)
10358 {
10359 struct frame *f = XFRAME (WINDOW_FRAME (w));
10360 update_begin (f);
10361 rif->update_window_begin_hook (w);
10362 rif->clear_mouse_face (w);
10363 rif->scroll_run_hook (w, &run);
10364 rif->update_window_end_hook (w, 0, 0);
10365 update_end (f);
10366 }
10367
10368 /* Adjust Y positions of reused rows. */
10369 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10370 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10371 max_y = it.last_visible_y;
10372 for (row = first_reusable_row; row < first_row_to_display; ++row)
10373 {
10374 row->y -= dy;
10375 if (row->y < min_y)
10376 row->visible_height = row->height - (min_y - row->y);
10377 else if (row->y + row->height > max_y)
10378 row->visible_height
10379 = row->height - (row->y + row->height - max_y);
10380 else
10381 row->visible_height = row->height;
10382 }
10383
10384 /* Disable rows not reused. */
10385 while (row < bottom_row)
10386 {
10387 row->enabled_p = 0;
10388 ++row;
10389 }
10390
10391 /* Scroll the current matrix. */
10392 xassert (nrows_scrolled > 0);
10393 rotate_matrix (w->current_matrix,
10394 start_vpos,
10395 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10396 -nrows_scrolled);
10397
10398 /* Adjust window end. A null value of last_text_row means that
10399 the window end is in reused rows which in turn means that
10400 only its vpos can have changed. */
10401 if (last_text_row)
10402 {
10403 w->window_end_bytepos
10404 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
10405 XSETFASTINT (w->window_end_pos,
10406 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10407 XSETFASTINT (w->window_end_vpos,
10408 MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
10409 }
10410 else
10411 {
10412 XSETFASTINT (w->window_end_vpos,
10413 XFASTINT (w->window_end_vpos) - nrows_scrolled);
10414 }
10415
10416 w->window_end_valid = Qnil;
10417 w->desired_matrix->no_scrolling_p = 1;
10418
10419 #if GLYPH_DEBUG
10420 debug_method_add (w, "try_window_reusing_current_matrix 2");
10421 #endif
10422 return 1;
10423 }
10424
10425 return 0;
10426 }
10427
10428
10429 \f
10430 /************************************************************************
10431 Window redisplay reusing current matrix when buffer has changed
10432 ************************************************************************/
10433
10434 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10435 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
10436 int *, int *));
10437 static struct glyph_row *
10438 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10439 struct glyph_row *));
10440
10441
10442 /* Return the last row in MATRIX displaying text. If row START is
10443 non-null, start searching with that row. IT gives the dimensions
10444 of the display. Value is null if matrix is empty; otherwise it is
10445 a pointer to the row found. */
10446
10447 static struct glyph_row *
10448 find_last_row_displaying_text (matrix, it, start)
10449 struct glyph_matrix *matrix;
10450 struct it *it;
10451 struct glyph_row *start;
10452 {
10453 struct glyph_row *row, *row_found;
10454
10455 /* Set row_found to the last row in IT->w's current matrix
10456 displaying text. The loop looks funny but think of partially
10457 visible lines. */
10458 row_found = NULL;
10459 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10460 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10461 {
10462 xassert (row->enabled_p);
10463 row_found = row;
10464 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10465 break;
10466 ++row;
10467 }
10468
10469 return row_found;
10470 }
10471
10472
10473 /* Return the last row in the current matrix of W that is not affected
10474 by changes at the start of current_buffer that occurred since the
10475 last time W was redisplayed. Value is null if no such row exists.
10476
10477 The global variable beg_unchanged has to contain the number of
10478 bytes unchanged at the start of current_buffer. BEG +
10479 beg_unchanged is the buffer position of the first changed byte in
10480 current_buffer. Characters at positions < BEG + beg_unchanged are
10481 at the same buffer positions as they were when the current matrix
10482 was built. */
10483
10484 static struct glyph_row *
10485 find_last_unchanged_at_beg_row (w)
10486 struct window *w;
10487 {
10488 int first_changed_pos = BEG + BEG_UNCHANGED;
10489 struct glyph_row *row;
10490 struct glyph_row *row_found = NULL;
10491 int yb = window_text_bottom_y (w);
10492
10493 /* Find the last row displaying unchanged text. */
10494 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10495 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10496 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
10497 {
10498 if (/* If row ends before first_changed_pos, it is unchanged,
10499 except in some case. */
10500 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10501 /* When row ends in ZV and we write at ZV it is not
10502 unchanged. */
10503 && !row->ends_at_zv_p
10504 /* When first_changed_pos is the end of a continued line,
10505 row is not unchanged because it may be no longer
10506 continued. */
10507 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10508 && row->continued_p))
10509 row_found = row;
10510
10511 /* Stop if last visible row. */
10512 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10513 break;
10514
10515 ++row;
10516 }
10517
10518 return row_found;
10519 }
10520
10521
10522 /* Find the first glyph row in the current matrix of W that is not
10523 affected by changes at the end of current_buffer since the last
10524 time the window was redisplayed. Return in *DELTA the number of
10525 chars by which buffer positions in unchanged text at the end of
10526 current_buffer must be adjusted. Return in *DELTA_BYTES the
10527 corresponding number of bytes. Value is null if no such row
10528 exists, i.e. all rows are affected by changes. */
10529
10530 static struct glyph_row *
10531 find_first_unchanged_at_end_row (w, delta, delta_bytes)
10532 struct window *w;
10533 int *delta, *delta_bytes;
10534 {
10535 struct glyph_row *row;
10536 struct glyph_row *row_found = NULL;
10537
10538 *delta = *delta_bytes = 0;
10539
10540 /* Display must not have been paused, otherwise the current matrix
10541 is not up to date. */
10542 if (NILP (w->window_end_valid))
10543 abort ();
10544
10545 /* A value of window_end_pos >= END_UNCHANGED means that the window
10546 end is in the range of changed text. If so, there is no
10547 unchanged row at the end of W's current matrix. */
10548 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
10549 return NULL;
10550
10551 /* Set row to the last row in W's current matrix displaying text. */
10552 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10553
10554 /* If matrix is entirely empty, no unchanged row exists. */
10555 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10556 {
10557 /* The value of row is the last glyph row in the matrix having a
10558 meaningful buffer position in it. The end position of row
10559 corresponds to window_end_pos. This allows us to translate
10560 buffer positions in the current matrix to current buffer
10561 positions for characters not in changed text. */
10562 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10563 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10564 int last_unchanged_pos, last_unchanged_pos_old;
10565 struct glyph_row *first_text_row
10566 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10567
10568 *delta = Z - Z_old;
10569 *delta_bytes = Z_BYTE - Z_BYTE_old;
10570
10571 /* Set last_unchanged_pos to the buffer position of the last
10572 character in the buffer that has not been changed. Z is the
10573 index + 1 of the last byte in current_buffer, i.e. by
10574 subtracting end_unchanged we get the index of the last
10575 unchanged character, and we have to add BEG to get its buffer
10576 position. */
10577 last_unchanged_pos = Z - END_UNCHANGED + BEG;
10578 last_unchanged_pos_old = last_unchanged_pos - *delta;
10579
10580 /* Search backward from ROW for a row displaying a line that
10581 starts at a minimum position >= last_unchanged_pos_old. */
10582 for (; row > first_text_row; --row)
10583 {
10584 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10585 abort ();
10586
10587 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10588 row_found = row;
10589 }
10590 }
10591
10592 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10593 abort ();
10594
10595 return row_found;
10596 }
10597
10598
10599 /* Make sure that glyph rows in the current matrix of window W
10600 reference the same glyph memory as corresponding rows in the
10601 frame's frame matrix. This function is called after scrolling W's
10602 current matrix on a terminal frame in try_window_id and
10603 try_window_reusing_current_matrix. */
10604
10605 static void
10606 sync_frame_with_window_matrix_rows (w)
10607 struct window *w;
10608 {
10609 struct frame *f = XFRAME (w->frame);
10610 struct glyph_row *window_row, *window_row_end, *frame_row;
10611
10612 /* Preconditions: W must be a leaf window and full-width. Its frame
10613 must have a frame matrix. */
10614 xassert (NILP (w->hchild) && NILP (w->vchild));
10615 xassert (WINDOW_FULL_WIDTH_P (w));
10616 xassert (!FRAME_WINDOW_P (f));
10617
10618 /* If W is a full-width window, glyph pointers in W's current matrix
10619 have, by definition, to be the same as glyph pointers in the
10620 corresponding frame matrix. */
10621 window_row = w->current_matrix->rows;
10622 window_row_end = window_row + w->current_matrix->nrows;
10623 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10624 while (window_row < window_row_end)
10625 {
10626 int area;
10627
10628 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10629 frame_row->glyphs[area] = window_row->glyphs[area];
10630
10631 /* Disable frame rows whose corresponding window rows have
10632 been disabled in try_window_id. */
10633 if (!window_row->enabled_p)
10634 frame_row->enabled_p = 0;
10635
10636 ++window_row, ++frame_row;
10637 }
10638 }
10639
10640
10641 /* Find the glyph row in window W containing CHARPOS. Consider all
10642 rows between START and END (not inclusive). END null means search
10643 all rows to the end of the display area of W. Value is the row
10644 containing CHARPOS or null. */
10645
10646 static struct glyph_row *
10647 row_containing_pos (w, charpos, start, end)
10648 struct window *w;
10649 int charpos;
10650 struct glyph_row *start, *end;
10651 {
10652 struct glyph_row *row = start;
10653 int last_y;
10654
10655 /* If we happen to start on a header-line, skip that. */
10656 if (row->mode_line_p)
10657 ++row;
10658
10659 if ((end && row >= end) || !row->enabled_p)
10660 return NULL;
10661
10662 last_y = window_text_bottom_y (w);
10663
10664 while ((end == NULL || row < end)
10665 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10666 /* The end position of a row equals the start
10667 position of the next row. If CHARPOS is there, we
10668 would rather display it in the next line, except
10669 when this line ends in ZV. */
10670 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10671 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
10672 || !row->ends_at_zv_p)))
10673 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10674 ++row;
10675
10676 /* Give up if CHARPOS not found. */
10677 if ((end && row >= end)
10678 || charpos < MATRIX_ROW_START_CHARPOS (row)
10679 || charpos > MATRIX_ROW_END_CHARPOS (row))
10680 row = NULL;
10681
10682 return row;
10683 }
10684
10685
10686 /* Try to redisplay window W by reusing its existing display. W's
10687 current matrix must be up to date when this function is called,
10688 i.e. window_end_valid must not be nil.
10689
10690 Value is
10691
10692 1 if display has been updated
10693 0 if otherwise unsuccessful
10694 -1 if redisplay with same window start is known not to succeed
10695
10696 The following steps are performed:
10697
10698 1. Find the last row in the current matrix of W that is not
10699 affected by changes at the start of current_buffer. If no such row
10700 is found, give up.
10701
10702 2. Find the first row in W's current matrix that is not affected by
10703 changes at the end of current_buffer. Maybe there is no such row.
10704
10705 3. Display lines beginning with the row + 1 found in step 1 to the
10706 row found in step 2 or, if step 2 didn't find a row, to the end of
10707 the window.
10708
10709 4. If cursor is not known to appear on the window, give up.
10710
10711 5. If display stopped at the row found in step 2, scroll the
10712 display and current matrix as needed.
10713
10714 6. Maybe display some lines at the end of W, if we must. This can
10715 happen under various circumstances, like a partially visible line
10716 becoming fully visible, or because newly displayed lines are displayed
10717 in smaller font sizes.
10718
10719 7. Update W's window end information. */
10720
10721 /* Check that window end is what we expect it to be. */
10722
10723 static int
10724 try_window_id (w)
10725 struct window *w;
10726 {
10727 struct frame *f = XFRAME (w->frame);
10728 struct glyph_matrix *current_matrix = w->current_matrix;
10729 struct glyph_matrix *desired_matrix = w->desired_matrix;
10730 struct glyph_row *last_unchanged_at_beg_row;
10731 struct glyph_row *first_unchanged_at_end_row;
10732 struct glyph_row *row;
10733 struct glyph_row *bottom_row;
10734 int bottom_vpos;
10735 struct it it;
10736 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
10737 struct text_pos start_pos;
10738 struct run run;
10739 int first_unchanged_at_end_vpos = 0;
10740 struct glyph_row *last_text_row, *last_text_row_at_end;
10741 struct text_pos start;
10742
10743 SET_TEXT_POS_FROM_MARKER (start, w->start);
10744
10745 /* Check pre-conditions. Window end must be valid, otherwise
10746 the current matrix would not be up to date. */
10747 xassert (!NILP (w->window_end_valid));
10748 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
10749 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
10750
10751 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
10752 only if buffer has really changed. The reason is that the gap is
10753 initially at Z for freshly visited files. The code below would
10754 set end_unchanged to 0 in that case. */
10755 if (MODIFF > SAVE_MODIFF
10756 /* This seems to happen sometimes after saving a buffer. */
10757 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
10758 {
10759 if (GPT - BEG < BEG_UNCHANGED)
10760 BEG_UNCHANGED = GPT - BEG;
10761 if (Z - GPT < END_UNCHANGED)
10762 END_UNCHANGED = Z - GPT;
10763 }
10764
10765 /* If window starts after a line end, and the last change is in
10766 front of that newline, then changes don't affect the display.
10767 This case happens with stealth-fontification. Note that although
10768 the display is unchanged, glyph positions in the matrix have to
10769 be adjusted, of course. */
10770 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10771 if (CHARPOS (start) > BEGV
10772 && Z - END_UNCHANGED < CHARPOS (start) - 1
10773 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
10774 && PT < MATRIX_ROW_END_CHARPOS (row))
10775 {
10776 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
10777 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
10778
10779 if (delta)
10780 {
10781 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
10782 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
10783
10784 increment_matrix_positions (w->current_matrix,
10785 MATRIX_ROW_VPOS (r0, current_matrix),
10786 MATRIX_ROW_VPOS (r1, current_matrix),
10787 delta, delta_bytes);
10788 }
10789
10790 #if 0 /* If changes are all in front of the window start, the
10791 distance of the last displayed glyph from Z hasn't
10792 changed. */
10793 w->window_end_pos
10794 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10795 w->window_end_bytepos
10796 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10797 #endif
10798
10799 row = row_containing_pos (w, PT, r0, NULL);
10800 if (row == NULL)
10801 return 0;
10802
10803 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10804 return 1;
10805 }
10806
10807 /* Return quickly if changes are all below what is displayed in the
10808 window, and if PT is in the window. */
10809 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
10810 && PT < MATRIX_ROW_END_CHARPOS (row))
10811 {
10812 /* We have to update window end positions because the buffer's
10813 size has changed. */
10814 w->window_end_pos
10815 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
10816 w->window_end_bytepos
10817 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
10818
10819 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10820 row = row_containing_pos (w, PT, row, NULL);
10821 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10822 return 2;
10823 }
10824
10825 /* Check that window start agrees with the start of the first glyph
10826 row in its current matrix. Check this after we know the window
10827 start is not in changed text, otherwise positions would not be
10828 comparable. */
10829 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10830 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
10831 return 0;
10832
10833 /* Compute the position at which we have to start displaying new
10834 lines. Some of the lines at the top of the window might be
10835 reusable because they are not displaying changed text. Find the
10836 last row in W's current matrix not affected by changes at the
10837 start of current_buffer. Value is null if changes start in the
10838 first line of window. */
10839 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
10840 if (last_unchanged_at_beg_row)
10841 {
10842 /* Avoid starting to display in the moddle of a character, a TAB
10843 for instance. This is easier than to set up the iterator
10844 exactly, and it's not a frequent case, so the additional
10845 effort wouldn't really pay off. */
10846 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
10847 && last_unchanged_at_beg_row > w->current_matrix->rows)
10848 --last_unchanged_at_beg_row;
10849
10850 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
10851 return 0;
10852
10853 init_to_row_end (&it, w, last_unchanged_at_beg_row);
10854 start_pos = it.current.pos;
10855
10856 /* Start displaying new lines in the desired matrix at the same
10857 vpos we would use in the current matrix, i.e. below
10858 last_unchanged_at_beg_row. */
10859 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
10860 current_matrix);
10861 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
10862 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
10863
10864 xassert (it.hpos == 0 && it.current_x == 0);
10865 }
10866 else
10867 {
10868 /* There are no reusable lines at the start of the window.
10869 Start displaying in the first line. */
10870 start_display (&it, w, start);
10871 start_pos = it.current.pos;
10872 }
10873
10874 /* Find the first row that is not affected by changes at the end of
10875 the buffer. Value will be null if there is no unchanged row, in
10876 which case we must redisplay to the end of the window. delta
10877 will be set to the value by which buffer positions beginning with
10878 first_unchanged_at_end_row have to be adjusted due to text
10879 changes. */
10880 first_unchanged_at_end_row
10881 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
10882 IF_DEBUG (debug_delta = delta);
10883 IF_DEBUG (debug_delta_bytes = delta_bytes);
10884
10885 /* Set stop_pos to the buffer position up to which we will have to
10886 display new lines. If first_unchanged_at_end_row != NULL, this
10887 is the buffer position of the start of the line displayed in that
10888 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
10889 that we don't stop at a buffer position. */
10890 stop_pos = 0;
10891 if (first_unchanged_at_end_row)
10892 {
10893 xassert (last_unchanged_at_beg_row == NULL
10894 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
10895
10896 /* If this is a continuation line, move forward to the next one
10897 that isn't. Changes in lines above affect this line.
10898 Caution: this may move first_unchanged_at_end_row to a row
10899 not displaying text. */
10900 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
10901 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10902 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10903 < it.last_visible_y))
10904 ++first_unchanged_at_end_row;
10905
10906 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
10907 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
10908 >= it.last_visible_y))
10909 first_unchanged_at_end_row = NULL;
10910 else
10911 {
10912 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
10913 + delta);
10914 first_unchanged_at_end_vpos
10915 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
10916 xassert (stop_pos >= Z - END_UNCHANGED);
10917 }
10918 }
10919 else if (last_unchanged_at_beg_row == NULL)
10920 return 0;
10921
10922
10923 #if GLYPH_DEBUG
10924
10925 /* Either there is no unchanged row at the end, or the one we have
10926 now displays text. This is a necessary condition for the window
10927 end pos calculation at the end of this function. */
10928 xassert (first_unchanged_at_end_row == NULL
10929 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
10930
10931 debug_last_unchanged_at_beg_vpos
10932 = (last_unchanged_at_beg_row
10933 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
10934 : -1);
10935 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
10936
10937 #endif /* GLYPH_DEBUG != 0 */
10938
10939
10940 /* Display new lines. Set last_text_row to the last new line
10941 displayed which has text on it, i.e. might end up as being the
10942 line where the window_end_vpos is. */
10943 w->cursor.vpos = -1;
10944 last_text_row = NULL;
10945 overlay_arrow_seen = 0;
10946 while (it.current_y < it.last_visible_y
10947 && !fonts_changed_p
10948 && (first_unchanged_at_end_row == NULL
10949 || IT_CHARPOS (it) < stop_pos))
10950 {
10951 if (display_line (&it))
10952 last_text_row = it.glyph_row - 1;
10953 }
10954
10955 if (fonts_changed_p)
10956 return -1;
10957
10958
10959 /* Compute differences in buffer positions, y-positions etc. for
10960 lines reused at the bottom of the window. Compute what we can
10961 scroll. */
10962 if (first_unchanged_at_end_row
10963 /* No lines reused because we displayed everything up to the
10964 bottom of the window. */
10965 && it.current_y < it.last_visible_y)
10966 {
10967 dvpos = (it.vpos
10968 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
10969 current_matrix));
10970 dy = it.current_y - first_unchanged_at_end_row->y;
10971 run.current_y = first_unchanged_at_end_row->y;
10972 run.desired_y = run.current_y + dy;
10973 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
10974 }
10975 else
10976 {
10977 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
10978 first_unchanged_at_end_row = NULL;
10979 }
10980 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
10981
10982
10983 /* Find the cursor if not already found. We have to decide whether
10984 PT will appear on this window (it sometimes doesn't, but this is
10985 not a very frequent case.) This decision has to be made before
10986 the current matrix is altered. A value of cursor.vpos < 0 means
10987 that PT is either in one of the lines beginning at
10988 first_unchanged_at_end_row or below the window. Don't care for
10989 lines that might be displayed later at the window end; as
10990 mentioned, this is not a frequent case. */
10991 if (w->cursor.vpos < 0)
10992 {
10993 /* Cursor in unchanged rows at the top? */
10994 if (PT < CHARPOS (start_pos)
10995 && last_unchanged_at_beg_row)
10996 {
10997 row = row_containing_pos (w, PT,
10998 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
10999 last_unchanged_at_beg_row + 1);
11000 if (row)
11001 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11002 }
11003
11004 /* Start from first_unchanged_at_end_row looking for PT. */
11005 else if (first_unchanged_at_end_row)
11006 {
11007 row = row_containing_pos (w, PT - delta,
11008 first_unchanged_at_end_row, NULL);
11009 if (row)
11010 set_cursor_from_row (w, row, w->current_matrix, delta,
11011 delta_bytes, dy, dvpos);
11012 }
11013
11014 /* Give up if cursor was not found. */
11015 if (w->cursor.vpos < 0)
11016 {
11017 clear_glyph_matrix (w->desired_matrix);
11018 return -1;
11019 }
11020 }
11021
11022 /* Don't let the cursor end in the scroll margins. */
11023 {
11024 int this_scroll_margin, cursor_height;
11025
11026 this_scroll_margin = max (0, scroll_margin);
11027 this_scroll_margin = min (this_scroll_margin,
11028 XFASTINT (w->height) / 4);
11029 this_scroll_margin *= CANON_Y_UNIT (it.f);
11030 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11031
11032 if ((w->cursor.y < this_scroll_margin
11033 && CHARPOS (start) > BEGV)
11034 /* Don't take scroll margin into account at the bottom because
11035 old redisplay didn't do it either. */
11036 || w->cursor.y + cursor_height > it.last_visible_y)
11037 {
11038 w->cursor.vpos = -1;
11039 clear_glyph_matrix (w->desired_matrix);
11040 return -1;
11041 }
11042 }
11043
11044 /* Scroll the display. Do it before changing the current matrix so
11045 that xterm.c doesn't get confused about where the cursor glyph is
11046 found. */
11047 if (dy && run.height)
11048 {
11049 update_begin (f);
11050
11051 if (FRAME_WINDOW_P (f))
11052 {
11053 rif->update_window_begin_hook (w);
11054 rif->clear_mouse_face (w);
11055 rif->scroll_run_hook (w, &run);
11056 rif->update_window_end_hook (w, 0, 0);
11057 }
11058 else
11059 {
11060 /* Terminal frame. In this case, dvpos gives the number of
11061 lines to scroll by; dvpos < 0 means scroll up. */
11062 int first_unchanged_at_end_vpos
11063 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11064 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11065 int end = XFASTINT (w->top) + window_internal_height (w);
11066
11067 /* Perform the operation on the screen. */
11068 if (dvpos > 0)
11069 {
11070 /* Scroll last_unchanged_at_beg_row to the end of the
11071 window down dvpos lines. */
11072 set_terminal_window (end);
11073
11074 /* On dumb terminals delete dvpos lines at the end
11075 before inserting dvpos empty lines. */
11076 if (!scroll_region_ok)
11077 ins_del_lines (end - dvpos, -dvpos);
11078
11079 /* Insert dvpos empty lines in front of
11080 last_unchanged_at_beg_row. */
11081 ins_del_lines (from, dvpos);
11082 }
11083 else if (dvpos < 0)
11084 {
11085 /* Scroll up last_unchanged_at_beg_vpos to the end of
11086 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11087 set_terminal_window (end);
11088
11089 /* Delete dvpos lines in front of
11090 last_unchanged_at_beg_vpos. ins_del_lines will set
11091 the cursor to the given vpos and emit |dvpos| delete
11092 line sequences. */
11093 ins_del_lines (from + dvpos, dvpos);
11094
11095 /* On a dumb terminal insert dvpos empty lines at the
11096 end. */
11097 if (!scroll_region_ok)
11098 ins_del_lines (end + dvpos, -dvpos);
11099 }
11100
11101 set_terminal_window (0);
11102 }
11103
11104 update_end (f);
11105 }
11106
11107 /* Shift reused rows of the current matrix to the right position.
11108 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11109 text. */
11110 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11111 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
11112 if (dvpos < 0)
11113 {
11114 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11115 bottom_vpos, dvpos);
11116 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11117 bottom_vpos, 0);
11118 }
11119 else if (dvpos > 0)
11120 {
11121 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11122 bottom_vpos, dvpos);
11123 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11124 first_unchanged_at_end_vpos + dvpos, 0);
11125 }
11126
11127 /* For frame-based redisplay, make sure that current frame and window
11128 matrix are in sync with respect to glyph memory. */
11129 if (!FRAME_WINDOW_P (f))
11130 sync_frame_with_window_matrix_rows (w);
11131
11132 /* Adjust buffer positions in reused rows. */
11133 if (delta)
11134 increment_matrix_positions (current_matrix,
11135 first_unchanged_at_end_vpos + dvpos,
11136 bottom_vpos, delta, delta_bytes);
11137
11138 /* Adjust Y positions. */
11139 if (dy)
11140 shift_glyph_matrix (w, current_matrix,
11141 first_unchanged_at_end_vpos + dvpos,
11142 bottom_vpos, dy);
11143
11144 if (first_unchanged_at_end_row)
11145 first_unchanged_at_end_row += dvpos;
11146
11147 /* If scrolling up, there may be some lines to display at the end of
11148 the window. */
11149 last_text_row_at_end = NULL;
11150 if (dy < 0)
11151 {
11152 /* Set last_row to the glyph row in the current matrix where the
11153 window end line is found. It has been moved up or down in
11154 the matrix by dvpos. */
11155 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11156 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11157
11158 /* If last_row is the window end line, it should display text. */
11159 xassert (last_row->displays_text_p);
11160
11161 /* If window end line was partially visible before, begin
11162 displaying at that line. Otherwise begin displaying with the
11163 line following it. */
11164 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11165 {
11166 init_to_row_start (&it, w, last_row);
11167 it.vpos = last_vpos;
11168 it.current_y = last_row->y;
11169 }
11170 else
11171 {
11172 init_to_row_end (&it, w, last_row);
11173 it.vpos = 1 + last_vpos;
11174 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11175 ++last_row;
11176 }
11177
11178 /* We may start in a continuation line. If so, we have to get
11179 the right continuation_lines_width and current_x. */
11180 it.continuation_lines_width = last_row->continuation_lines_width;
11181 it.hpos = it.current_x = 0;
11182
11183 /* Display the rest of the lines at the window end. */
11184 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11185 while (it.current_y < it.last_visible_y
11186 && !fonts_changed_p)
11187 {
11188 /* Is it always sure that the display agrees with lines in
11189 the current matrix? I don't think so, so we mark rows
11190 displayed invalid in the current matrix by setting their
11191 enabled_p flag to zero. */
11192 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11193 if (display_line (&it))
11194 last_text_row_at_end = it.glyph_row - 1;
11195 }
11196 }
11197
11198 /* Update window_end_pos and window_end_vpos. */
11199 if (first_unchanged_at_end_row
11200 && first_unchanged_at_end_row->y < it.last_visible_y
11201 && !last_text_row_at_end)
11202 {
11203 /* Window end line if one of the preserved rows from the current
11204 matrix. Set row to the last row displaying text in current
11205 matrix starting at first_unchanged_at_end_row, after
11206 scrolling. */
11207 xassert (first_unchanged_at_end_row->displays_text_p);
11208 row = find_last_row_displaying_text (w->current_matrix, &it,
11209 first_unchanged_at_end_row);
11210 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11211
11212 XSETFASTINT (w->window_end_pos, Z - MATRIX_ROW_END_CHARPOS (row));
11213 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
11214 XSETFASTINT (w->window_end_vpos,
11215 MATRIX_ROW_VPOS (row, w->current_matrix));
11216 }
11217 else if (last_text_row_at_end)
11218 {
11219 XSETFASTINT (w->window_end_pos,
11220 Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
11221 w->window_end_bytepos
11222 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
11223 XSETFASTINT (w->window_end_vpos,
11224 MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
11225 }
11226 else if (last_text_row)
11227 {
11228 /* We have displayed either to the end of the window or at the
11229 end of the window, i.e. the last row with text is to be found
11230 in the desired matrix. */
11231 XSETFASTINT (w->window_end_pos,
11232 Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11233 w->window_end_bytepos
11234 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11235 XSETFASTINT (w->window_end_vpos,
11236 MATRIX_ROW_VPOS (last_text_row, desired_matrix));
11237 }
11238 else if (first_unchanged_at_end_row == NULL
11239 && last_text_row == NULL
11240 && last_text_row_at_end == NULL)
11241 {
11242 /* Displayed to end of window, but no line containing text was
11243 displayed. Lines were deleted at the end of the window. */
11244 int vpos;
11245 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
11246
11247 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
11248 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11249 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11250 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11251 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
11252 break;
11253
11254 w->window_end_vpos = make_number (vpos);
11255 }
11256 else
11257 abort ();
11258
11259 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11260 debug_end_vpos = XFASTINT (w->window_end_vpos));
11261
11262 /* Record that display has not been completed. */
11263 w->window_end_valid = Qnil;
11264 w->desired_matrix->no_scrolling_p = 1;
11265 return 3;
11266 }
11267
11268
11269 \f
11270 /***********************************************************************
11271 More debugging support
11272 ***********************************************************************/
11273
11274 #if GLYPH_DEBUG
11275
11276 void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11277 static void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
11278
11279
11280 /* Dump the contents of glyph matrix MATRIX on stderr. If
11281 WITH_GLYPHS_P is non-zero, dump glyph contents as well. */
11282
11283 static void
11284 dump_glyph_matrix (matrix, with_glyphs_p)
11285 struct glyph_matrix *matrix;
11286 int with_glyphs_p;
11287 {
11288 int i;
11289 for (i = 0; i < matrix->nrows; ++i)
11290 dump_glyph_row (matrix, i, with_glyphs_p);
11291 }
11292
11293
11294 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
11295 WITH_GLYPH_SP non-zero means dump glyph contents, too. */
11296
11297 void
11298 dump_glyph_row (matrix, vpos, with_glyphs_p)
11299 struct glyph_matrix *matrix;
11300 int vpos, with_glyphs_p;
11301 {
11302 struct glyph_row *row;
11303
11304 if (vpos < 0 || vpos >= matrix->nrows)
11305 return;
11306
11307 row = MATRIX_ROW (matrix, vpos);
11308
11309 fprintf (stderr, "Row Start End Used oEI><O\\CTZFes X Y W H V A P\n");
11310 fprintf (stderr, "=======================================================================\n");
11311
11312 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
11313 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
11314 row - matrix->rows,
11315 MATRIX_ROW_START_CHARPOS (row),
11316 MATRIX_ROW_END_CHARPOS (row),
11317 row->used[TEXT_AREA],
11318 row->contains_overlapping_glyphs_p,
11319 row->enabled_p,
11320 row->inverse_p,
11321 row->truncated_on_left_p,
11322 row->truncated_on_right_p,
11323 row->overlay_arrow_p,
11324 row->continued_p,
11325 MATRIX_ROW_CONTINUATION_LINE_P (row),
11326 row->displays_text_p,
11327 row->ends_at_zv_p,
11328 row->fill_line_p,
11329 row->ends_in_middle_of_char_p,
11330 row->starts_in_middle_of_char_p,
11331 row->x,
11332 row->y,
11333 row->pixel_width,
11334 row->height,
11335 row->visible_height,
11336 row->ascent,
11337 row->phys_ascent);
11338 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11339 row->end.overlay_string_index);
11340 fprintf (stderr, "%9d %5d\n",
11341 CHARPOS (row->start.string_pos),
11342 CHARPOS (row->end.string_pos));
11343 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11344 row->end.dpvec_index);
11345
11346 if (with_glyphs_p)
11347 {
11348 struct glyph *glyph, *glyph_end;
11349 int prev_had_glyphs_p;
11350
11351 glyph = row->glyphs[TEXT_AREA];
11352 glyph_end = glyph + row->used[TEXT_AREA];
11353
11354 /* Glyph for a line end in text. */
11355 if (glyph == glyph_end && glyph->charpos > 0)
11356 ++glyph_end;
11357
11358 if (glyph < glyph_end)
11359 {
11360 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
11361 prev_had_glyphs_p = 1;
11362 }
11363 else
11364 prev_had_glyphs_p = 0;
11365
11366 while (glyph < glyph_end)
11367 {
11368 if (glyph->type == CHAR_GLYPH)
11369 {
11370 fprintf (stderr,
11371 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11372 glyph - row->glyphs[TEXT_AREA],
11373 'C',
11374 glyph->charpos,
11375 (BUFFERP (glyph->object)
11376 ? 'B'
11377 : (STRINGP (glyph->object)
11378 ? 'S'
11379 : '-')),
11380 glyph->pixel_width,
11381 glyph->u.ch,
11382 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11383 ? glyph->u.ch
11384 : '.'),
11385 glyph->face_id,
11386 glyph->left_box_line_p,
11387 glyph->right_box_line_p);
11388 }
11389 else if (glyph->type == STRETCH_GLYPH)
11390 {
11391 fprintf (stderr,
11392 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11393 glyph - row->glyphs[TEXT_AREA],
11394 'S',
11395 glyph->charpos,
11396 (BUFFERP (glyph->object)
11397 ? 'B'
11398 : (STRINGP (glyph->object)
11399 ? 'S'
11400 : '-')),
11401 glyph->pixel_width,
11402 0,
11403 '.',
11404 glyph->face_id,
11405 glyph->left_box_line_p,
11406 glyph->right_box_line_p);
11407 }
11408 else if (glyph->type == IMAGE_GLYPH)
11409 {
11410 fprintf (stderr,
11411 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11412 glyph - row->glyphs[TEXT_AREA],
11413 'I',
11414 glyph->charpos,
11415 (BUFFERP (glyph->object)
11416 ? 'B'
11417 : (STRINGP (glyph->object)
11418 ? 'S'
11419 : '-')),
11420 glyph->pixel_width,
11421 glyph->u.img_id,
11422 '.',
11423 glyph->face_id,
11424 glyph->left_box_line_p,
11425 glyph->right_box_line_p);
11426 }
11427 ++glyph;
11428 }
11429 }
11430 }
11431
11432
11433 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11434 Sdump_glyph_matrix, 0, 1, "p",
11435 "Dump the current matrix of the selected window to stderr.\n\
11436 Shows contents of glyph row structures. With non-nil optional\n\
11437 parameter WITH-GLYPHS-P, dump glyphs as well.")
11438 (with_glyphs_p)
11439 Lisp_Object with_glyphs_p;
11440 {
11441 struct window *w = XWINDOW (selected_window);
11442 struct buffer *buffer = XBUFFER (w->buffer);
11443
11444 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11445 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11446 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11447 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11448 fprintf (stderr, "=============================================\n");
11449 dump_glyph_matrix (w->current_matrix, !NILP (with_glyphs_p));
11450 return Qnil;
11451 }
11452
11453
11454 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 1, "",
11455 "Dump glyph row ROW to stderr.")
11456 (row)
11457 Lisp_Object row;
11458 {
11459 CHECK_NUMBER (row, 0);
11460 dump_glyph_row (XWINDOW (selected_window)->current_matrix, XINT (row), 1);
11461 return Qnil;
11462 }
11463
11464
11465 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row,
11466 0, 0, "", "")
11467 ()
11468 {
11469 struct frame *sf = SELECTED_FRAME ();
11470 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)
11471 ->current_matrix);
11472 dump_glyph_row (m, 0, 1);
11473 return Qnil;
11474 }
11475
11476
11477 DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11478 Strace_redisplay_toggle, 0, 0, "",
11479 "Toggle tracing of redisplay.")
11480 ()
11481 {
11482 trace_redisplay_p = !trace_redisplay_p;
11483 return Qnil;
11484 }
11485
11486
11487 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11488 "Print STRING to stderr.")
11489 (string)
11490 Lisp_Object string;
11491 {
11492 CHECK_STRING (string, 0);
11493 fprintf (stderr, "%s", XSTRING (string)->data);
11494 return Qnil;
11495 }
11496
11497 #endif /* GLYPH_DEBUG */
11498
11499
11500 \f
11501 /***********************************************************************
11502 Building Desired Matrix Rows
11503 ***********************************************************************/
11504
11505 /* Return a temporary glyph row holding the glyphs of an overlay
11506 arrow. Only used for non-window-redisplay windows. */
11507
11508 static struct glyph_row *
11509 get_overlay_arrow_glyph_row (w)
11510 struct window *w;
11511 {
11512 struct frame *f = XFRAME (WINDOW_FRAME (w));
11513 struct buffer *buffer = XBUFFER (w->buffer);
11514 struct buffer *old = current_buffer;
11515 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11516 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11517 unsigned char *arrow_end = arrow_string + arrow_len;
11518 unsigned char *p;
11519 struct it it;
11520 int multibyte_p;
11521 int n_glyphs_before;
11522
11523 set_buffer_temp (buffer);
11524 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11525 it.glyph_row->used[TEXT_AREA] = 0;
11526 SET_TEXT_POS (it.position, 0, 0);
11527
11528 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11529 p = arrow_string;
11530 while (p < arrow_end)
11531 {
11532 Lisp_Object face, ilisp;
11533
11534 /* Get the next character. */
11535 if (multibyte_p)
11536 it.c = string_char_and_length (p, arrow_len, &it.len);
11537 else
11538 it.c = *p, it.len = 1;
11539 p += it.len;
11540
11541 /* Get its face. */
11542 XSETFASTINT (ilisp, p - arrow_string);
11543 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11544 it.face_id = compute_char_face (f, it.c, face);
11545
11546 /* Compute its width, get its glyphs. */
11547 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
11548 SET_TEXT_POS (it.position, -1, -1);
11549 PRODUCE_GLYPHS (&it);
11550
11551 /* If this character doesn't fit any more in the line, we have
11552 to remove some glyphs. */
11553 if (it.current_x > it.last_visible_x)
11554 {
11555 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11556 break;
11557 }
11558 }
11559
11560 set_buffer_temp (old);
11561 return it.glyph_row;
11562 }
11563
11564
11565 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11566 glyphs are only inserted for terminal frames since we can't really
11567 win with truncation glyphs when partially visible glyphs are
11568 involved. Which glyphs to insert is determined by
11569 produce_special_glyphs. */
11570
11571 static void
11572 insert_left_trunc_glyphs (it)
11573 struct it *it;
11574 {
11575 struct it truncate_it;
11576 struct glyph *from, *end, *to, *toend;
11577
11578 xassert (!FRAME_WINDOW_P (it->f));
11579
11580 /* Get the truncation glyphs. */
11581 truncate_it = *it;
11582 truncate_it.current_x = 0;
11583 truncate_it.face_id = DEFAULT_FACE_ID;
11584 truncate_it.glyph_row = &scratch_glyph_row;
11585 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11586 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
11587 truncate_it.object = make_number (0);
11588 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11589
11590 /* Overwrite glyphs from IT with truncation glyphs. */
11591 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11592 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11593 to = it->glyph_row->glyphs[TEXT_AREA];
11594 toend = to + it->glyph_row->used[TEXT_AREA];
11595
11596 while (from < end)
11597 *to++ = *from++;
11598
11599 /* There may be padding glyphs left over. Remove them. */
11600 from = to;
11601 while (from < toend && CHAR_GLYPH_PADDING_P (*from))
11602 ++from;
11603 while (from < toend)
11604 *to++ = *from++;
11605
11606 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
11607 }
11608
11609
11610 /* Compute the pixel height and width of IT->glyph_row.
11611
11612 Most of the time, ascent and height of a display line will be equal
11613 to the max_ascent and max_height values of the display iterator
11614 structure. This is not the case if
11615
11616 1. We hit ZV without displaying anything. In this case, max_ascent
11617 and max_height will be zero.
11618
11619 2. We have some glyphs that don't contribute to the line height.
11620 (The glyph row flag contributes_to_line_height_p is for future
11621 pixmap extensions).
11622
11623 The first case is easily covered by using default values because in
11624 these cases, the line height does not really matter, except that it
11625 must not be zero. */
11626
11627 static void
11628 compute_line_metrics (it)
11629 struct it *it;
11630 {
11631 struct glyph_row *row = it->glyph_row;
11632 int area, i;
11633
11634 if (FRAME_WINDOW_P (it->f))
11635 {
11636 int i, header_line_height;
11637
11638 /* The line may consist of one space only, that was added to
11639 place the cursor on it. If so, the row's height hasn't been
11640 computed yet. */
11641 if (row->height == 0)
11642 {
11643 if (it->max_ascent + it->max_descent == 0)
11644 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
11645 row->ascent = it->max_ascent;
11646 row->height = it->max_ascent + it->max_descent;
11647 row->phys_ascent = it->max_phys_ascent;
11648 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
11649 }
11650
11651 /* Compute the width of this line. */
11652 row->pixel_width = row->x;
11653 for (i = 0; i < row->used[TEXT_AREA]; ++i)
11654 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
11655
11656 xassert (row->pixel_width >= 0);
11657 xassert (row->ascent >= 0 && row->height > 0);
11658
11659 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
11660 || MATRIX_ROW_OVERLAPS_PRED_P (row));
11661
11662 /* If first line's physical ascent is larger than its logical
11663 ascent, use the physical ascent, and make the row taller.
11664 This makes accented characters fully visible. */
11665 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
11666 && row->phys_ascent > row->ascent)
11667 {
11668 row->height += row->phys_ascent - row->ascent;
11669 row->ascent = row->phys_ascent;
11670 }
11671
11672 /* Compute how much of the line is visible. */
11673 row->visible_height = row->height;
11674
11675 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
11676 if (row->y < header_line_height)
11677 row->visible_height -= header_line_height - row->y;
11678 else
11679 {
11680 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
11681 if (row->y + row->height > max_y)
11682 row->visible_height -= row->y + row->height - max_y;
11683 }
11684 }
11685 else
11686 {
11687 row->pixel_width = row->used[TEXT_AREA];
11688 row->ascent = row->phys_ascent = 0;
11689 row->height = row->phys_height = row->visible_height = 1;
11690 }
11691
11692 /* Compute a hash code for this row. */
11693 row->hash = 0;
11694 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11695 for (i = 0; i < row->used[area]; ++i)
11696 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
11697 + row->glyphs[area][i].u.val
11698 + row->glyphs[area][i].face_id
11699 + row->glyphs[area][i].padding_p
11700 + (row->glyphs[area][i].type << 2));
11701
11702 it->max_ascent = it->max_descent = 0;
11703 it->max_phys_ascent = it->max_phys_descent = 0;
11704 }
11705
11706
11707 /* Append one space to the glyph row of iterator IT if doing a
11708 window-based redisplay. DEFAULT_FACE_P non-zero means let the
11709 space have the default face, otherwise let it have the same face as
11710 IT->face_id. Value is non-zero if a space was added.
11711
11712 This function is called to make sure that there is always one glyph
11713 at the end of a glyph row that the cursor can be set on under
11714 window-systems. (If there weren't such a glyph we would not know
11715 how wide and tall a box cursor should be displayed).
11716
11717 At the same time this space let's a nicely handle clearing to the
11718 end of the line if the row ends in italic text. */
11719
11720 static int
11721 append_space (it, default_face_p)
11722 struct it *it;
11723 int default_face_p;
11724 {
11725 if (FRAME_WINDOW_P (it->f))
11726 {
11727 int n = it->glyph_row->used[TEXT_AREA];
11728
11729 if (it->glyph_row->glyphs[TEXT_AREA] + n
11730 < it->glyph_row->glyphs[1 + TEXT_AREA])
11731 {
11732 /* Save some values that must not be changed.
11733 Must save IT->c and IT->len because otherwise
11734 ITERATOR_AT_END_P wouldn't work anymore after
11735 append_space has been called. */
11736 int saved_what = it->what;
11737 int saved_c = it->c, saved_len = it->len;
11738 int saved_x = it->current_x;
11739 int saved_face_id = it->face_id;
11740 struct text_pos saved_pos;
11741 Lisp_Object saved_object;
11742 struct face *face;
11743
11744 saved_object = it->object;
11745 saved_pos = it->position;
11746
11747 it->what = IT_CHARACTER;
11748 bzero (&it->position, sizeof it->position);
11749 it->object = make_number (0);
11750 it->c = ' ';
11751 it->len = 1;
11752
11753 if (default_face_p)
11754 it->face_id = DEFAULT_FACE_ID;
11755 else if (it->face_before_selective_p)
11756 it->face_id = it->saved_face_id;
11757 face = FACE_FROM_ID (it->f, it->face_id);
11758 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
11759
11760 PRODUCE_GLYPHS (it);
11761
11762 it->current_x = saved_x;
11763 it->object = saved_object;
11764 it->position = saved_pos;
11765 it->what = saved_what;
11766 it->face_id = saved_face_id;
11767 it->len = saved_len;
11768 it->c = saved_c;
11769 return 1;
11770 }
11771 }
11772
11773 return 0;
11774 }
11775
11776
11777 /* Extend the face of the last glyph in the text area of IT->glyph_row
11778 to the end of the display line. Called from display_line.
11779 If the glyph row is empty, add a space glyph to it so that we
11780 know the face to draw. Set the glyph row flag fill_line_p. */
11781
11782 static void
11783 extend_face_to_end_of_line (it)
11784 struct it *it;
11785 {
11786 struct face *face;
11787 struct frame *f = it->f;
11788
11789 /* If line is already filled, do nothing. */
11790 if (it->current_x >= it->last_visible_x)
11791 return;
11792
11793 /* Face extension extends the background and box of IT->face_id
11794 to the end of the line. If the background equals the background
11795 of the frame, we don't have to do anything. */
11796 if (it->face_before_selective_p)
11797 face = FACE_FROM_ID (it->f, it->saved_face_id);
11798 else
11799 face = FACE_FROM_ID (f, it->face_id);
11800
11801 if (FRAME_WINDOW_P (f)
11802 && face->box == FACE_NO_BOX
11803 && face->background == FRAME_BACKGROUND_PIXEL (f)
11804 && !face->stipple)
11805 return;
11806
11807 /* Set the glyph row flag indicating that the face of the last glyph
11808 in the text area has to be drawn to the end of the text area. */
11809 it->glyph_row->fill_line_p = 1;
11810
11811 /* If current character of IT is not ASCII, make sure we have the
11812 ASCII face. This will be automatically undone the next time
11813 get_next_display_element returns a multibyte character. Note
11814 that the character will always be single byte in unibyte text. */
11815 if (!SINGLE_BYTE_CHAR_P (it->c))
11816 {
11817 it->face_id = FACE_FOR_CHAR (f, face, 0);
11818 }
11819
11820 if (FRAME_WINDOW_P (f))
11821 {
11822 /* If the row is empty, add a space with the current face of IT,
11823 so that we know which face to draw. */
11824 if (it->glyph_row->used[TEXT_AREA] == 0)
11825 {
11826 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
11827 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
11828 it->glyph_row->used[TEXT_AREA] = 1;
11829 }
11830 }
11831 else
11832 {
11833 /* Save some values that must not be changed. */
11834 int saved_x = it->current_x;
11835 struct text_pos saved_pos;
11836 Lisp_Object saved_object;
11837 int saved_what = it->what;
11838 int saved_face_id = it->face_id;
11839
11840 saved_object = it->object;
11841 saved_pos = it->position;
11842
11843 it->what = IT_CHARACTER;
11844 bzero (&it->position, sizeof it->position);
11845 it->object = make_number (0);
11846 it->c = ' ';
11847 it->len = 1;
11848 it->face_id = face->id;
11849
11850 PRODUCE_GLYPHS (it);
11851
11852 while (it->current_x <= it->last_visible_x)
11853 PRODUCE_GLYPHS (it);
11854
11855 /* Don't count these blanks really. It would let us insert a left
11856 truncation glyph below and make us set the cursor on them, maybe. */
11857 it->current_x = saved_x;
11858 it->object = saved_object;
11859 it->position = saved_pos;
11860 it->what = saved_what;
11861 it->face_id = saved_face_id;
11862 }
11863 }
11864
11865
11866 /* Value is non-zero if text starting at CHARPOS in current_buffer is
11867 trailing whitespace. */
11868
11869 static int
11870 trailing_whitespace_p (charpos)
11871 int charpos;
11872 {
11873 int bytepos = CHAR_TO_BYTE (charpos);
11874 int c = 0;
11875
11876 while (bytepos < ZV_BYTE
11877 && (c = FETCH_CHAR (bytepos),
11878 c == ' ' || c == '\t'))
11879 ++bytepos;
11880
11881 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
11882 {
11883 if (bytepos != PT_BYTE)
11884 return 1;
11885 }
11886 return 0;
11887 }
11888
11889
11890 /* Highlight trailing whitespace, if any, in ROW. */
11891
11892 void
11893 highlight_trailing_whitespace (f, row)
11894 struct frame *f;
11895 struct glyph_row *row;
11896 {
11897 int used = row->used[TEXT_AREA];
11898
11899 if (used)
11900 {
11901 struct glyph *start = row->glyphs[TEXT_AREA];
11902 struct glyph *glyph = start + used - 1;
11903
11904 /* Skip over the space glyph inserted to display the
11905 cursor at the end of a line. */
11906 if (glyph->type == CHAR_GLYPH
11907 && glyph->u.ch == ' '
11908 && INTEGERP (glyph->object))
11909 --glyph;
11910
11911 /* If last glyph is a space or stretch, and it's trailing
11912 whitespace, set the face of all trailing whitespace glyphs in
11913 IT->glyph_row to `trailing-whitespace'. */
11914 if (glyph >= start
11915 && BUFFERP (glyph->object)
11916 && (glyph->type == STRETCH_GLYPH
11917 || (glyph->type == CHAR_GLYPH
11918 && glyph->u.ch == ' '))
11919 && trailing_whitespace_p (glyph->charpos))
11920 {
11921 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
11922
11923 while (glyph >= start
11924 && BUFFERP (glyph->object)
11925 && (glyph->type == STRETCH_GLYPH
11926 || (glyph->type == CHAR_GLYPH
11927 && glyph->u.ch == ' ')))
11928 (glyph--)->face_id = face_id;
11929 }
11930 }
11931 }
11932
11933
11934 /* Value is non-zero if glyph row ROW in window W should be
11935 used to hold the cursor. */
11936
11937 static int
11938 cursor_row_p (w, row)
11939 struct window *w;
11940 struct glyph_row *row;
11941 {
11942 int cursor_row_p = 1;
11943
11944 if (PT == MATRIX_ROW_END_CHARPOS (row))
11945 {
11946 /* If the row ends with a newline from a string, we don't want
11947 the cursor there (if the row is continued it doesn't end in a
11948 newline). */
11949 if (CHARPOS (row->end.string_pos) >= 0
11950 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11951 cursor_row_p = row->continued_p;
11952
11953 /* If the row ends at ZV, display the cursor at the end of that
11954 row instead of at the start of the row below. */
11955 else if (row->ends_at_zv_p)
11956 cursor_row_p = 1;
11957 else
11958 cursor_row_p = 0;
11959 }
11960
11961 return cursor_row_p;
11962 }
11963
11964
11965 /* Construct the glyph row IT->glyph_row in the desired matrix of
11966 IT->w from text at the current position of IT. See dispextern.h
11967 for an overview of struct it. Value is non-zero if
11968 IT->glyph_row displays text, as opposed to a line displaying ZV
11969 only. */
11970
11971 static int
11972 display_line (it)
11973 struct it *it;
11974 {
11975 struct glyph_row *row = it->glyph_row;
11976
11977 /* We always start displaying at hpos zero even if hscrolled. */
11978 xassert (it->hpos == 0 && it->current_x == 0);
11979
11980 /* We must not display in a row that's not a text row. */
11981 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
11982 < it->w->desired_matrix->nrows);
11983
11984 /* Is IT->w showing the region? */
11985 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
11986
11987 /* Clear the result glyph row and enable it. */
11988 prepare_desired_row (row);
11989
11990 row->y = it->current_y;
11991 row->start = it->current;
11992 row->continuation_lines_width = it->continuation_lines_width;
11993 row->displays_text_p = 1;
11994 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
11995 it->starts_in_middle_of_char_p = 0;
11996
11997 /* Arrange the overlays nicely for our purposes. Usually, we call
11998 display_line on only one line at a time, in which case this
11999 can't really hurt too much, or we call it on lines which appear
12000 one after another in the buffer, in which case all calls to
12001 recenter_overlay_lists but the first will be pretty cheap. */
12002 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12003
12004 /* Move over display elements that are not visible because we are
12005 hscrolled. This may stop at an x-position < IT->first_visible_x
12006 if the first glyph is partially visible or if we hit a line end. */
12007 if (it->current_x < it->first_visible_x)
12008 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12009 MOVE_TO_POS | MOVE_TO_X);
12010
12011 /* Get the initial row height. This is either the height of the
12012 text hscrolled, if there is any, or zero. */
12013 row->ascent = it->max_ascent;
12014 row->height = it->max_ascent + it->max_descent;
12015 row->phys_ascent = it->max_phys_ascent;
12016 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
12017
12018 /* Loop generating characters. The loop is left with IT on the next
12019 character to display. */
12020 while (1)
12021 {
12022 int n_glyphs_before, hpos_before, x_before;
12023 int x, i, nglyphs;
12024 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
12025
12026 /* Retrieve the next thing to display. Value is zero if end of
12027 buffer reached. */
12028 if (!get_next_display_element (it))
12029 {
12030 /* Maybe add a space at the end of this line that is used to
12031 display the cursor there under X. Set the charpos of the
12032 first glyph of blank lines not corresponding to any text
12033 to -1. */
12034 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12035 || row->used[TEXT_AREA] == 0)
12036 {
12037 row->glyphs[TEXT_AREA]->charpos = -1;
12038 row->displays_text_p = 0;
12039
12040 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12041 row->indicate_empty_line_p = 1;
12042 }
12043
12044 it->continuation_lines_width = 0;
12045 row->ends_at_zv_p = 1;
12046 break;
12047 }
12048
12049 /* Now, get the metrics of what we want to display. This also
12050 generates glyphs in `row' (which is IT->glyph_row). */
12051 n_glyphs_before = row->used[TEXT_AREA];
12052 x = it->current_x;
12053
12054 /* Remember the line height so far in case the next element doesn't
12055 fit on the line. */
12056 if (!it->truncate_lines_p)
12057 {
12058 ascent = it->max_ascent;
12059 descent = it->max_descent;
12060 phys_ascent = it->max_phys_ascent;
12061 phys_descent = it->max_phys_descent;
12062 }
12063
12064 PRODUCE_GLYPHS (it);
12065
12066 /* If this display element was in marginal areas, continue with
12067 the next one. */
12068 if (it->area != TEXT_AREA)
12069 {
12070 row->ascent = max (row->ascent, it->max_ascent);
12071 row->height = max (row->height, it->max_ascent + it->max_descent);
12072 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12073 row->phys_height = max (row->phys_height,
12074 it->max_phys_ascent + it->max_phys_descent);
12075 set_iterator_to_next (it, 1);
12076 continue;
12077 }
12078
12079 /* Does the display element fit on the line? If we truncate
12080 lines, we should draw past the right edge of the window. If
12081 we don't truncate, we want to stop so that we can display the
12082 continuation glyph before the right margin. If lines are
12083 continued, there are two possible strategies for characters
12084 resulting in more than 1 glyph (e.g. tabs): Display as many
12085 glyphs as possible in this line and leave the rest for the
12086 continuation line, or display the whole element in the next
12087 line. Original redisplay did the former, so we do it also. */
12088 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12089 hpos_before = it->hpos;
12090 x_before = x;
12091
12092 if (nglyphs == 1
12093 && it->current_x < it->last_visible_x)
12094 {
12095 ++it->hpos;
12096 row->ascent = max (row->ascent, it->max_ascent);
12097 row->height = max (row->height, it->max_ascent + it->max_descent);
12098 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12099 row->phys_height = max (row->phys_height,
12100 it->max_phys_ascent + it->max_phys_descent);
12101 if (it->current_x - it->pixel_width < it->first_visible_x)
12102 row->x = x - it->first_visible_x;
12103 }
12104 else
12105 {
12106 int new_x;
12107 struct glyph *glyph;
12108
12109 for (i = 0; i < nglyphs; ++i, x = new_x)
12110 {
12111 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12112 new_x = x + glyph->pixel_width;
12113
12114 if (/* Lines are continued. */
12115 !it->truncate_lines_p
12116 && (/* Glyph doesn't fit on the line. */
12117 new_x > it->last_visible_x
12118 /* Or it fits exactly on a window system frame. */
12119 || (new_x == it->last_visible_x
12120 && FRAME_WINDOW_P (it->f))))
12121 {
12122 /* End of a continued line. */
12123
12124 if (it->hpos == 0
12125 || (new_x == it->last_visible_x
12126 && FRAME_WINDOW_P (it->f)))
12127 {
12128 /* Current glyph is the only one on the line or
12129 fits exactly on the line. We must continue
12130 the line because we can't draw the cursor
12131 after the glyph. */
12132 row->continued_p = 1;
12133 it->current_x = new_x;
12134 it->continuation_lines_width += new_x;
12135 ++it->hpos;
12136 if (i == nglyphs - 1)
12137 set_iterator_to_next (it, 1);
12138 }
12139 else if (CHAR_GLYPH_PADDING_P (*glyph)
12140 && !FRAME_WINDOW_P (it->f))
12141 {
12142 /* A padding glyph that doesn't fit on this line.
12143 This means the whole character doesn't fit
12144 on the line. */
12145 row->used[TEXT_AREA] = n_glyphs_before;
12146
12147 /* Fill the rest of the row with continuation
12148 glyphs like in 20.x. */
12149 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12150 < row->glyphs[1 + TEXT_AREA])
12151 produce_special_glyphs (it, IT_CONTINUATION);
12152
12153 row->continued_p = 1;
12154 it->current_x = x_before;
12155 it->continuation_lines_width += x_before;
12156
12157 /* Restore the height to what it was before the
12158 element not fitting on the line. */
12159 it->max_ascent = ascent;
12160 it->max_descent = descent;
12161 it->max_phys_ascent = phys_ascent;
12162 it->max_phys_descent = phys_descent;
12163 }
12164 else
12165 {
12166 /* Display element draws past the right edge of
12167 the window. Restore positions to values
12168 before the element. The next line starts
12169 with current_x before the glyph that could
12170 not be displayed, so that TAB works right. */
12171 row->used[TEXT_AREA] = n_glyphs_before + i;
12172
12173 /* Display continuation glyphs. */
12174 if (!FRAME_WINDOW_P (it->f))
12175 produce_special_glyphs (it, IT_CONTINUATION);
12176 row->continued_p = 1;
12177
12178 it->current_x = x;
12179 it->continuation_lines_width += x;
12180 if (nglyphs > 1 && i > 0)
12181 {
12182 row->ends_in_middle_of_char_p = 1;
12183 it->starts_in_middle_of_char_p = 1;
12184 }
12185
12186 /* Restore the height to what it was before the
12187 element not fitting on the line. */
12188 it->max_ascent = ascent;
12189 it->max_descent = descent;
12190 it->max_phys_ascent = phys_ascent;
12191 it->max_phys_descent = phys_descent;
12192 }
12193
12194 break;
12195 }
12196 else if (new_x > it->first_visible_x)
12197 {
12198 /* Increment number of glyphs actually displayed. */
12199 ++it->hpos;
12200
12201 if (x < it->first_visible_x)
12202 /* Glyph is partially visible, i.e. row starts at
12203 negative X position. */
12204 row->x = x - it->first_visible_x;
12205 }
12206 else
12207 {
12208 /* Glyph is completely off the left margin of the
12209 window. This should not happen because of the
12210 move_it_in_display_line at the start of
12211 this function. */
12212 abort ();
12213 }
12214 }
12215
12216 row->ascent = max (row->ascent, it->max_ascent);
12217 row->height = max (row->height, it->max_ascent + it->max_descent);
12218 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12219 row->phys_height = max (row->phys_height,
12220 it->max_phys_ascent + it->max_phys_descent);
12221
12222 /* End of this display line if row is continued. */
12223 if (row->continued_p)
12224 break;
12225 }
12226
12227 /* Is this a line end? If yes, we're also done, after making
12228 sure that a non-default face is extended up to the right
12229 margin of the window. */
12230 if (ITERATOR_AT_END_OF_LINE_P (it))
12231 {
12232 int used_before = row->used[TEXT_AREA];
12233
12234 /* Add a space at the end of the line that is used to
12235 display the cursor there. */
12236 append_space (it, 0);
12237
12238 /* Extend the face to the end of the line. */
12239 extend_face_to_end_of_line (it);
12240
12241 /* Make sure we have the position. */
12242 if (used_before == 0)
12243 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12244
12245 /* Consume the line end. This skips over invisible lines. */
12246 set_iterator_to_next (it, 1);
12247 it->continuation_lines_width = 0;
12248 break;
12249 }
12250
12251 /* Proceed with next display element. Note that this skips
12252 over lines invisible because of selective display. */
12253 set_iterator_to_next (it, 1);
12254
12255 /* If we truncate lines, we are done when the last displayed
12256 glyphs reach past the right margin of the window. */
12257 if (it->truncate_lines_p
12258 && (FRAME_WINDOW_P (it->f)
12259 ? (it->current_x >= it->last_visible_x)
12260 : (it->current_x > it->last_visible_x)))
12261 {
12262 /* Maybe add truncation glyphs. */
12263 if (!FRAME_WINDOW_P (it->f))
12264 {
12265 --it->glyph_row->used[TEXT_AREA];
12266 produce_special_glyphs (it, IT_TRUNCATION);
12267 }
12268
12269 row->truncated_on_right_p = 1;
12270 it->continuation_lines_width = 0;
12271 reseat_at_next_visible_line_start (it, 0);
12272 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12273 it->hpos = hpos_before;
12274 it->current_x = x_before;
12275 break;
12276 }
12277 }
12278
12279 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12280 at the left window margin. */
12281 if (it->first_visible_x
12282 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12283 {
12284 if (!FRAME_WINDOW_P (it->f))
12285 insert_left_trunc_glyphs (it);
12286 row->truncated_on_left_p = 1;
12287 }
12288
12289 /* If the start of this line is the overlay arrow-position, then
12290 mark this glyph row as the one containing the overlay arrow.
12291 This is clearly a mess with variable size fonts. It would be
12292 better to let it be displayed like cursors under X. */
12293 if (MARKERP (Voverlay_arrow_position)
12294 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
12295 && (MATRIX_ROW_START_CHARPOS (row)
12296 == marker_position (Voverlay_arrow_position))
12297 && STRINGP (Voverlay_arrow_string)
12298 && ! overlay_arrow_seen)
12299 {
12300 /* Overlay arrow in window redisplay is a bitmap. */
12301 if (!FRAME_WINDOW_P (it->f))
12302 {
12303 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12304 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12305 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12306 struct glyph *p = row->glyphs[TEXT_AREA];
12307 struct glyph *p2, *end;
12308
12309 /* Copy the arrow glyphs. */
12310 while (glyph < arrow_end)
12311 *p++ = *glyph++;
12312
12313 /* Throw away padding glyphs. */
12314 p2 = p;
12315 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12316 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12317 ++p2;
12318 if (p2 > p)
12319 {
12320 while (p2 < end)
12321 *p++ = *p2++;
12322 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
12323 }
12324 }
12325
12326 overlay_arrow_seen = 1;
12327 row->overlay_arrow_p = 1;
12328 }
12329
12330 /* Compute pixel dimensions of this line. */
12331 compute_line_metrics (it);
12332
12333 /* Remember the position at which this line ends. */
12334 row->end = it->current;
12335
12336 /* Maybe set the cursor. */
12337 if (it->w->cursor.vpos < 0
12338 && PT >= MATRIX_ROW_START_CHARPOS (row)
12339 && PT <= MATRIX_ROW_END_CHARPOS (row)
12340 && cursor_row_p (it->w, row))
12341 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
12342
12343 /* Highlight trailing whitespace. */
12344 if (!NILP (Vshow_trailing_whitespace))
12345 highlight_trailing_whitespace (it->f, it->glyph_row);
12346
12347 /* Prepare for the next line. This line starts horizontally at (X
12348 HPOS) = (0 0). Vertical positions are incremented. As a
12349 convenience for the caller, IT->glyph_row is set to the next
12350 row to be used. */
12351 it->current_x = it->hpos = 0;
12352 it->current_y += row->height;
12353 ++it->vpos;
12354 ++it->glyph_row;
12355 return row->displays_text_p;
12356 }
12357
12358
12359 \f
12360 /***********************************************************************
12361 Menu Bar
12362 ***********************************************************************/
12363
12364 /* Redisplay the menu bar in the frame for window W.
12365
12366 The menu bar of X frames that don't have X toolkit support is
12367 displayed in a special window W->frame->menu_bar_window.
12368
12369 The menu bar of terminal frames is treated specially as far as
12370 glyph matrices are concerned. Menu bar lines are not part of
12371 windows, so the update is done directly on the frame matrix rows
12372 for the menu bar. */
12373
12374 static void
12375 display_menu_bar (w)
12376 struct window *w;
12377 {
12378 struct frame *f = XFRAME (WINDOW_FRAME (w));
12379 struct it it;
12380 Lisp_Object items;
12381 int i;
12382
12383 /* Don't do all this for graphical frames. */
12384 #ifdef HAVE_NTGUI
12385 if (!NILP (Vwindow_system))
12386 return;
12387 #endif
12388 #ifdef USE_X_TOOLKIT
12389 if (FRAME_X_P (f))
12390 return;
12391 #endif
12392 #ifdef macintosh
12393 if (FRAME_MAC_P (f))
12394 return;
12395 #endif
12396
12397 #ifdef USE_X_TOOLKIT
12398 xassert (!FRAME_WINDOW_P (f));
12399 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
12400 it.first_visible_x = 0;
12401 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12402 #else /* not USE_X_TOOLKIT */
12403 if (FRAME_WINDOW_P (f))
12404 {
12405 /* Menu bar lines are displayed in the desired matrix of the
12406 dummy window menu_bar_window. */
12407 struct window *menu_w;
12408 xassert (WINDOWP (f->menu_bar_window));
12409 menu_w = XWINDOW (f->menu_bar_window);
12410 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
12411 MENU_FACE_ID);
12412 it.first_visible_x = 0;
12413 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12414 }
12415 else
12416 {
12417 /* This is a TTY frame, i.e. character hpos/vpos are used as
12418 pixel x/y. */
12419 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
12420 MENU_FACE_ID);
12421 it.first_visible_x = 0;
12422 it.last_visible_x = FRAME_WIDTH (f);
12423 }
12424 #endif /* not USE_X_TOOLKIT */
12425
12426 if (! mode_line_inverse_video)
12427 /* Force the menu-bar to be displayed in the default face. */
12428 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12429
12430 /* Clear all rows of the menu bar. */
12431 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12432 {
12433 struct glyph_row *row = it.glyph_row + i;
12434 clear_glyph_row (row);
12435 row->enabled_p = 1;
12436 row->full_width_p = 1;
12437 }
12438
12439 /* Display all items of the menu bar. */
12440 items = FRAME_MENU_BAR_ITEMS (it.f);
12441 for (i = 0; i < XVECTOR (items)->size; i += 4)
12442 {
12443 Lisp_Object string;
12444
12445 /* Stop at nil string. */
12446 string = XVECTOR (items)->contents[i + 1];
12447 if (NILP (string))
12448 break;
12449
12450 /* Remember where item was displayed. */
12451 XSETFASTINT (XVECTOR (items)->contents[i + 3], it.hpos);
12452
12453 /* Display the item, pad with one space. */
12454 if (it.current_x < it.last_visible_x)
12455 display_string (NULL, string, Qnil, 0, 0, &it,
12456 XSTRING (string)->size + 1, 0, 0, -1);
12457 }
12458
12459 /* Fill out the line with spaces. */
12460 if (it.current_x < it.last_visible_x)
12461 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
12462
12463 /* Compute the total height of the lines. */
12464 compute_line_metrics (&it);
12465 }
12466
12467
12468 \f
12469 /***********************************************************************
12470 Mode Line
12471 ***********************************************************************/
12472
12473 /* Redisplay mode lines in the window tree whose root is WINDOW. If
12474 FORCE is non-zero, redisplay mode lines unconditionally.
12475 Otherwise, redisplay only mode lines that are garbaged. Value is
12476 the number of windows whose mode lines were redisplayed. */
12477
12478 static int
12479 redisplay_mode_lines (window, force)
12480 Lisp_Object window;
12481 int force;
12482 {
12483 int nwindows = 0;
12484
12485 while (!NILP (window))
12486 {
12487 struct window *w = XWINDOW (window);
12488
12489 if (WINDOWP (w->hchild))
12490 nwindows += redisplay_mode_lines (w->hchild, force);
12491 else if (WINDOWP (w->vchild))
12492 nwindows += redisplay_mode_lines (w->vchild, force);
12493 else if (force
12494 || FRAME_GARBAGED_P (XFRAME (w->frame))
12495 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12496 {
12497 Lisp_Object old_selected_frame;
12498 struct text_pos lpoint;
12499 struct buffer *old = current_buffer;
12500
12501 /* Set the window's buffer for the mode line display. */
12502 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12503 set_buffer_internal_1 (XBUFFER (w->buffer));
12504
12505 /* Point refers normally to the selected window. For any
12506 other window, set up appropriate value. */
12507 if (!EQ (window, selected_window))
12508 {
12509 struct text_pos pt;
12510
12511 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12512 if (CHARPOS (pt) < BEGV)
12513 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12514 else if (CHARPOS (pt) > (ZV - 1))
12515 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12516 else
12517 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12518 }
12519
12520 /* Temporarily set up the selected frame. */
12521 old_selected_frame = selected_frame;
12522 selected_frame = w->frame;
12523
12524 /* Display mode lines. */
12525 clear_glyph_matrix (w->desired_matrix);
12526 if (display_mode_lines (w))
12527 {
12528 ++nwindows;
12529 w->must_be_updated_p = 1;
12530 }
12531
12532 /* Restore old settings. */
12533 selected_frame = old_selected_frame;
12534 set_buffer_internal_1 (old);
12535 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12536 }
12537
12538 window = w->next;
12539 }
12540
12541 return nwindows;
12542 }
12543
12544
12545 /* Display the mode and/or top line of window W. Value is the number
12546 of mode lines displayed. */
12547
12548 static int
12549 display_mode_lines (w)
12550 struct window *w;
12551 {
12552 int n = 0;
12553
12554 /* These will be set while the mode line specs are processed. */
12555 line_number_displayed = 0;
12556 w->column_number_displayed = Qnil;
12557
12558 if (WINDOW_WANTS_MODELINE_P (w))
12559 {
12560 display_mode_line (w, MODE_LINE_FACE_ID,
12561 current_buffer->mode_line_format);
12562 ++n;
12563 }
12564
12565 if (WINDOW_WANTS_HEADER_LINE_P (w))
12566 {
12567 display_mode_line (w, HEADER_LINE_FACE_ID,
12568 current_buffer->header_line_format);
12569 ++n;
12570 }
12571
12572 return n;
12573 }
12574
12575
12576 /* Display mode or top line of window W. FACE_ID specifies which line
12577 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
12578 FORMAT is the mode line format to display. Value is the pixel
12579 height of the mode line displayed. */
12580
12581 static int
12582 display_mode_line (w, face_id, format)
12583 struct window *w;
12584 enum face_id face_id;
12585 Lisp_Object format;
12586 {
12587 struct it it;
12588 struct face *face;
12589
12590 init_iterator (&it, w, -1, -1, NULL, face_id);
12591 prepare_desired_row (it.glyph_row);
12592
12593 if (! mode_line_inverse_video)
12594 /* Force the mode-line to be displayed in the default face. */
12595 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12596
12597 /* Temporarily make frame's keyboard the current kboard so that
12598 kboard-local variables in the mode_line_format will get the right
12599 values. */
12600 push_frame_kboard (it.f);
12601 display_mode_element (&it, 0, 0, 0, format);
12602 pop_frame_kboard ();
12603
12604 /* Fill up with spaces. */
12605 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
12606
12607 compute_line_metrics (&it);
12608 it.glyph_row->full_width_p = 1;
12609 it.glyph_row->mode_line_p = 1;
12610 it.glyph_row->inverse_p = 0;
12611 it.glyph_row->continued_p = 0;
12612 it.glyph_row->truncated_on_left_p = 0;
12613 it.glyph_row->truncated_on_right_p = 0;
12614
12615 /* Make a 3D mode-line have a shadow at its right end. */
12616 face = FACE_FROM_ID (it.f, face_id);
12617 extend_face_to_end_of_line (&it);
12618 if (face->box != FACE_NO_BOX)
12619 {
12620 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
12621 + it.glyph_row->used[TEXT_AREA] - 1);
12622 last->right_box_line_p = 1;
12623 }
12624
12625 return it.glyph_row->height;
12626 }
12627
12628
12629 /* Contribute ELT to the mode line for window IT->w. How it
12630 translates into text depends on its data type.
12631
12632 IT describes the display environment in which we display, as usual.
12633
12634 DEPTH is the depth in recursion. It is used to prevent
12635 infinite recursion here.
12636
12637 FIELD_WIDTH is the number of characters the display of ELT should
12638 occupy in the mode line, and PRECISION is the maximum number of
12639 characters to display from ELT's representation. See
12640 display_string for details. *
12641
12642 Returns the hpos of the end of the text generated by ELT. */
12643
12644 static int
12645 display_mode_element (it, depth, field_width, precision, elt)
12646 struct it *it;
12647 int depth;
12648 int field_width, precision;
12649 Lisp_Object elt;
12650 {
12651 int n = 0, field, prec;
12652
12653 tail_recurse:
12654 if (depth > 10)
12655 goto invalid;
12656
12657 depth++;
12658
12659 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
12660 {
12661 case Lisp_String:
12662 {
12663 /* A string: output it and check for %-constructs within it. */
12664 unsigned char c;
12665 unsigned char *this = XSTRING (elt)->data;
12666 unsigned char *lisp_string = this;
12667
12668 while ((precision <= 0 || n < precision)
12669 && *this
12670 && (frame_title_ptr
12671 || it->current_x < it->last_visible_x))
12672 {
12673 unsigned char *last = this;
12674
12675 /* Advance to end of string or next format specifier. */
12676 while ((c = *this++) != '\0' && c != '%')
12677 ;
12678
12679 if (this - 1 != last)
12680 {
12681 /* Output to end of string or up to '%'. Field width
12682 is length of string. Don't output more than
12683 PRECISION allows us. */
12684 prec = --this - last;
12685 if (precision > 0 && prec > precision - n)
12686 prec = precision - n;
12687
12688 if (frame_title_ptr)
12689 n += store_frame_title (last, prec, prec);
12690 else
12691 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
12692 it, 0, prec, 0, -1);
12693 }
12694 else /* c == '%' */
12695 {
12696 unsigned char *percent_position = this;
12697
12698 /* Get the specified minimum width. Zero means
12699 don't pad. */
12700 field = 0;
12701 while ((c = *this++) >= '0' && c <= '9')
12702 field = field * 10 + c - '0';
12703
12704 /* Don't pad beyond the total padding allowed. */
12705 if (field_width - n > 0 && field > field_width - n)
12706 field = field_width - n;
12707
12708 /* Note that either PRECISION <= 0 or N < PRECISION. */
12709 prec = precision - n;
12710
12711 if (c == 'M')
12712 n += display_mode_element (it, depth, field, prec,
12713 Vglobal_mode_string);
12714 else if (c != 0)
12715 {
12716 unsigned char *spec
12717 = decode_mode_spec (it->w, c, field, prec);
12718
12719 if (frame_title_ptr)
12720 n += store_frame_title (spec, field, prec);
12721 else
12722 {
12723 int nglyphs_before
12724 = it->glyph_row->used[TEXT_AREA];
12725 int charpos
12726 = percent_position - XSTRING (elt)->data;
12727 int nwritten
12728 = display_string (spec, Qnil, elt, charpos, 0, it,
12729 field, prec, 0, -1);
12730
12731 /* Assign to the glyphs written above the
12732 string where the `%x' came from, position
12733 of the `%'. */
12734 if (nwritten > 0)
12735 {
12736 struct glyph *glyph
12737 = (it->glyph_row->glyphs[TEXT_AREA]
12738 + nglyphs_before);
12739 int i;
12740
12741 for (i = 0; i < nwritten; ++i)
12742 {
12743 glyph[i].object = elt;
12744 glyph[i].charpos = charpos;
12745 }
12746
12747 n += nwritten;
12748 }
12749 }
12750 }
12751 }
12752 }
12753 }
12754 break;
12755
12756 case Lisp_Symbol:
12757 /* A symbol: process the value of the symbol recursively
12758 as if it appeared here directly. Avoid error if symbol void.
12759 Special case: if value of symbol is a string, output the string
12760 literally. */
12761 {
12762 register Lisp_Object tem;
12763 tem = Fboundp (elt);
12764 if (!NILP (tem))
12765 {
12766 tem = Fsymbol_value (elt);
12767 /* If value is a string, output that string literally:
12768 don't check for % within it. */
12769 if (STRINGP (tem))
12770 {
12771 prec = XSTRING (tem)->size;
12772 if (precision > 0 && prec > precision - n)
12773 prec = precision - n;
12774 if (frame_title_ptr)
12775 n += store_frame_title (XSTRING (tem)->data, -1, prec);
12776 else
12777 n += display_string (NULL, tem, Qnil, 0, 0, it,
12778 0, prec, 0, -1);
12779 }
12780 else if (!EQ (tem, elt))
12781 {
12782 /* Give up right away for nil or t. */
12783 elt = tem;
12784 goto tail_recurse;
12785 }
12786 }
12787 }
12788 break;
12789
12790 case Lisp_Cons:
12791 {
12792 register Lisp_Object car, tem;
12793
12794 /* A cons cell: three distinct cases.
12795 If first element is a string or a cons, process all the elements
12796 and effectively concatenate them.
12797 If first element is a negative number, truncate displaying cdr to
12798 at most that many characters. If positive, pad (with spaces)
12799 to at least that many characters.
12800 If first element is a symbol, process the cadr or caddr recursively
12801 according to whether the symbol's value is non-nil or nil. */
12802 car = XCAR (elt);
12803 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
12804 {
12805 /* An element of the form (:eval FORM) means evaluate FORM
12806 and use the result as mode line elements. */
12807 struct gcpro gcpro1;
12808 Lisp_Object spec;
12809
12810 spec = safe_eval (XCAR (XCDR (elt)));
12811 GCPRO1 (spec);
12812 n += display_mode_element (it, depth, field_width - n,
12813 precision - n, spec);
12814 UNGCPRO;
12815 }
12816 else if (SYMBOLP (car))
12817 {
12818 tem = Fboundp (car);
12819 elt = XCDR (elt);
12820 if (!CONSP (elt))
12821 goto invalid;
12822 /* elt is now the cdr, and we know it is a cons cell.
12823 Use its car if CAR has a non-nil value. */
12824 if (!NILP (tem))
12825 {
12826 tem = Fsymbol_value (car);
12827 if (!NILP (tem))
12828 {
12829 elt = XCAR (elt);
12830 goto tail_recurse;
12831 }
12832 }
12833 /* Symbol's value is nil (or symbol is unbound)
12834 Get the cddr of the original list
12835 and if possible find the caddr and use that. */
12836 elt = XCDR (elt);
12837 if (NILP (elt))
12838 break;
12839 else if (!CONSP (elt))
12840 goto invalid;
12841 elt = XCAR (elt);
12842 goto tail_recurse;
12843 }
12844 else if (INTEGERP (car))
12845 {
12846 register int lim = XINT (car);
12847 elt = XCDR (elt);
12848 if (lim < 0)
12849 {
12850 /* Negative int means reduce maximum width. */
12851 if (precision <= 0)
12852 precision = -lim;
12853 else
12854 precision = min (precision, -lim);
12855 }
12856 else if (lim > 0)
12857 {
12858 /* Padding specified. Don't let it be more than
12859 current maximum. */
12860 if (precision > 0)
12861 lim = min (precision, lim);
12862
12863 /* If that's more padding than already wanted, queue it.
12864 But don't reduce padding already specified even if
12865 that is beyond the current truncation point. */
12866 field_width = max (lim, field_width);
12867 }
12868 goto tail_recurse;
12869 }
12870 else if (STRINGP (car) || CONSP (car))
12871 {
12872 register int limit = 50;
12873 /* Limit is to protect against circular lists. */
12874 while (CONSP (elt)
12875 && --limit > 0
12876 && (precision <= 0 || n < precision))
12877 {
12878 n += display_mode_element (it, depth, field_width - n,
12879 precision - n, XCAR (elt));
12880 elt = XCDR (elt);
12881 }
12882 }
12883 }
12884 break;
12885
12886 default:
12887 invalid:
12888 if (frame_title_ptr)
12889 n += store_frame_title ("*invalid*", 0, precision - n);
12890 else
12891 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
12892 precision - n, 0, 0);
12893 return n;
12894 }
12895
12896 /* Pad to FIELD_WIDTH. */
12897 if (field_width > 0 && n < field_width)
12898 {
12899 if (frame_title_ptr)
12900 n += store_frame_title ("", field_width - n, 0);
12901 else
12902 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
12903 0, 0, 0);
12904 }
12905
12906 return n;
12907 }
12908
12909
12910 /* Write a null-terminated, right justified decimal representation of
12911 the positive integer D to BUF using a minimal field width WIDTH. */
12912
12913 static void
12914 pint2str (buf, width, d)
12915 register char *buf;
12916 register int width;
12917 register int d;
12918 {
12919 register char *p = buf;
12920
12921 if (d <= 0)
12922 *p++ = '0';
12923 else
12924 {
12925 while (d > 0)
12926 {
12927 *p++ = d % 10 + '0';
12928 d /= 10;
12929 }
12930 }
12931
12932 for (width -= (int) (p - buf); width > 0; --width)
12933 *p++ = ' ';
12934 *p-- = '\0';
12935 while (p > buf)
12936 {
12937 d = *buf;
12938 *buf++ = *p;
12939 *p-- = d;
12940 }
12941 }
12942
12943 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
12944 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
12945 type of CODING_SYSTEM. Return updated pointer into BUF. */
12946
12947 static unsigned char invalid_eol_type[] = "(*invalid*)";
12948
12949 static char *
12950 decode_mode_spec_coding (coding_system, buf, eol_flag)
12951 Lisp_Object coding_system;
12952 register char *buf;
12953 int eol_flag;
12954 {
12955 Lisp_Object val;
12956 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
12957 unsigned char *eol_str;
12958 int eol_str_len;
12959 /* The EOL conversion we are using. */
12960 Lisp_Object eoltype;
12961
12962 val = Fget (coding_system, Qcoding_system);
12963 eoltype = Qnil;
12964
12965 if (!VECTORP (val)) /* Not yet decided. */
12966 {
12967 if (multibyte)
12968 *buf++ = '-';
12969 if (eol_flag)
12970 eoltype = eol_mnemonic_undecided;
12971 /* Don't mention EOL conversion if it isn't decided. */
12972 }
12973 else
12974 {
12975 Lisp_Object eolvalue;
12976
12977 eolvalue = Fget (coding_system, Qeol_type);
12978
12979 if (multibyte)
12980 *buf++ = XFASTINT (XVECTOR (val)->contents[1]);
12981
12982 if (eol_flag)
12983 {
12984 /* The EOL conversion that is normal on this system. */
12985
12986 if (NILP (eolvalue)) /* Not yet decided. */
12987 eoltype = eol_mnemonic_undecided;
12988 else if (VECTORP (eolvalue)) /* Not yet decided. */
12989 eoltype = eol_mnemonic_undecided;
12990 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
12991 eoltype = (XFASTINT (eolvalue) == 0
12992 ? eol_mnemonic_unix
12993 : (XFASTINT (eolvalue) == 1
12994 ? eol_mnemonic_dos : eol_mnemonic_mac));
12995 }
12996 }
12997
12998 if (eol_flag)
12999 {
13000 /* Mention the EOL conversion if it is not the usual one. */
13001 if (STRINGP (eoltype))
13002 {
13003 eol_str = XSTRING (eoltype)->data;
13004 eol_str_len = XSTRING (eoltype)->size;
13005 }
13006 else if (INTEGERP (eoltype)
13007 && CHAR_VALID_P (XINT (eoltype), 0))
13008 {
13009 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
13010 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
13011 }
13012 else
13013 {
13014 eol_str = invalid_eol_type;
13015 eol_str_len = sizeof (invalid_eol_type) - 1;
13016 }
13017 bcopy (eol_str, buf, eol_str_len);
13018 buf += eol_str_len;
13019 }
13020
13021 return buf;
13022 }
13023
13024 /* Return a string for the output of a mode line %-spec for window W,
13025 generated by character C. PRECISION >= 0 means don't return a
13026 string longer than that value. FIELD_WIDTH > 0 means pad the
13027 string returned with spaces to that value. */
13028
13029 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13030
13031 static char *
13032 decode_mode_spec (w, c, field_width, precision)
13033 struct window *w;
13034 register int c;
13035 int field_width, precision;
13036 {
13037 Lisp_Object obj;
13038 struct frame *f = XFRAME (WINDOW_FRAME (w));
13039 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
13040 struct buffer *b = XBUFFER (w->buffer);
13041
13042 obj = Qnil;
13043
13044 switch (c)
13045 {
13046 case '*':
13047 if (!NILP (b->read_only))
13048 return "%";
13049 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13050 return "*";
13051 return "-";
13052
13053 case '+':
13054 /* This differs from %* only for a modified read-only buffer. */
13055 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13056 return "*";
13057 if (!NILP (b->read_only))
13058 return "%";
13059 return "-";
13060
13061 case '&':
13062 /* This differs from %* in ignoring read-only-ness. */
13063 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13064 return "*";
13065 return "-";
13066
13067 case '%':
13068 return "%";
13069
13070 case '[':
13071 {
13072 int i;
13073 char *p;
13074
13075 if (command_loop_level > 5)
13076 return "[[[... ";
13077 p = decode_mode_spec_buf;
13078 for (i = 0; i < command_loop_level; i++)
13079 *p++ = '[';
13080 *p = 0;
13081 return decode_mode_spec_buf;
13082 }
13083
13084 case ']':
13085 {
13086 int i;
13087 char *p;
13088
13089 if (command_loop_level > 5)
13090 return " ...]]]";
13091 p = decode_mode_spec_buf;
13092 for (i = 0; i < command_loop_level; i++)
13093 *p++ = ']';
13094 *p = 0;
13095 return decode_mode_spec_buf;
13096 }
13097
13098 case '-':
13099 {
13100 register int i;
13101
13102 /* Let lots_of_dashes be a string of infinite length. */
13103 if (field_width <= 0
13104 || field_width > sizeof (lots_of_dashes))
13105 {
13106 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13107 decode_mode_spec_buf[i] = '-';
13108 decode_mode_spec_buf[i] = '\0';
13109 return decode_mode_spec_buf;
13110 }
13111 else
13112 return lots_of_dashes;
13113 }
13114
13115 case 'b':
13116 obj = b->name;
13117 break;
13118
13119 case 'c':
13120 {
13121 int col = current_column ();
13122 XSETFASTINT (w->column_number_displayed, col);
13123 pint2str (decode_mode_spec_buf, field_width, col);
13124 return decode_mode_spec_buf;
13125 }
13126
13127 case 'F':
13128 /* %F displays the frame name. */
13129 if (!NILP (f->title))
13130 return (char *) XSTRING (f->title)->data;
13131 if (f->explicit_name || ! FRAME_WINDOW_P (f))
13132 return (char *) XSTRING (f->name)->data;
13133 return "Emacs";
13134
13135 case 'f':
13136 obj = b->filename;
13137 break;
13138
13139 case 'l':
13140 {
13141 int startpos = XMARKER (w->start)->charpos;
13142 int startpos_byte = marker_byte_position (w->start);
13143 int line, linepos, linepos_byte, topline;
13144 int nlines, junk;
13145 int height = XFASTINT (w->height);
13146
13147 /* If we decided that this buffer isn't suitable for line numbers,
13148 don't forget that too fast. */
13149 if (EQ (w->base_line_pos, w->buffer))
13150 goto no_value;
13151 /* But do forget it, if the window shows a different buffer now. */
13152 else if (BUFFERP (w->base_line_pos))
13153 w->base_line_pos = Qnil;
13154
13155 /* If the buffer is very big, don't waste time. */
13156 if (INTEGERP (Vline_number_display_limit)
13157 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
13158 {
13159 w->base_line_pos = Qnil;
13160 w->base_line_number = Qnil;
13161 goto no_value;
13162 }
13163
13164 if (!NILP (w->base_line_number)
13165 && !NILP (w->base_line_pos)
13166 && XFASTINT (w->base_line_pos) <= startpos)
13167 {
13168 line = XFASTINT (w->base_line_number);
13169 linepos = XFASTINT (w->base_line_pos);
13170 linepos_byte = buf_charpos_to_bytepos (b, linepos);
13171 }
13172 else
13173 {
13174 line = 1;
13175 linepos = BUF_BEGV (b);
13176 linepos_byte = BUF_BEGV_BYTE (b);
13177 }
13178
13179 /* Count lines from base line to window start position. */
13180 nlines = display_count_lines (linepos, linepos_byte,
13181 startpos_byte,
13182 startpos, &junk);
13183
13184 topline = nlines + line;
13185
13186 /* Determine a new base line, if the old one is too close
13187 or too far away, or if we did not have one.
13188 "Too close" means it's plausible a scroll-down would
13189 go back past it. */
13190 if (startpos == BUF_BEGV (b))
13191 {
13192 XSETFASTINT (w->base_line_number, topline);
13193 XSETFASTINT (w->base_line_pos, BUF_BEGV (b));
13194 }
13195 else if (nlines < height + 25 || nlines > height * 3 + 50
13196 || linepos == BUF_BEGV (b))
13197 {
13198 int limit = BUF_BEGV (b);
13199 int limit_byte = BUF_BEGV_BYTE (b);
13200 int position;
13201 int distance = (height * 2 + 30) * line_number_display_limit_width;
13202
13203 if (startpos - distance > limit)
13204 {
13205 limit = startpos - distance;
13206 limit_byte = CHAR_TO_BYTE (limit);
13207 }
13208
13209 nlines = display_count_lines (startpos, startpos_byte,
13210 limit_byte,
13211 - (height * 2 + 30),
13212 &position);
13213 /* If we couldn't find the lines we wanted within
13214 line_number_display_limit_width chars per line,
13215 give up on line numbers for this window. */
13216 if (position == limit_byte && limit == startpos - distance)
13217 {
13218 w->base_line_pos = w->buffer;
13219 w->base_line_number = Qnil;
13220 goto no_value;
13221 }
13222
13223 XSETFASTINT (w->base_line_number, topline - nlines);
13224 XSETFASTINT (w->base_line_pos, BYTE_TO_CHAR (position));
13225 }
13226
13227 /* Now count lines from the start pos to point. */
13228 nlines = display_count_lines (startpos, startpos_byte,
13229 PT_BYTE, PT, &junk);
13230
13231 /* Record that we did display the line number. */
13232 line_number_displayed = 1;
13233
13234 /* Make the string to show. */
13235 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
13236 return decode_mode_spec_buf;
13237 no_value:
13238 {
13239 char* p = decode_mode_spec_buf;
13240 int pad = field_width - 2;
13241 while (pad-- > 0)
13242 *p++ = ' ';
13243 *p++ = '?';
13244 *p++ = '?';
13245 *p = '\0';
13246 return decode_mode_spec_buf;
13247 }
13248 }
13249 break;
13250
13251 case 'm':
13252 obj = b->mode_name;
13253 break;
13254
13255 case 'n':
13256 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
13257 return " Narrow";
13258 break;
13259
13260 case 'p':
13261 {
13262 int pos = marker_position (w->start);
13263 int total = BUF_ZV (b) - BUF_BEGV (b);
13264
13265 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
13266 {
13267 if (pos <= BUF_BEGV (b))
13268 return "All";
13269 else
13270 return "Bottom";
13271 }
13272 else if (pos <= BUF_BEGV (b))
13273 return "Top";
13274 else
13275 {
13276 if (total > 1000000)
13277 /* Do it differently for a large value, to avoid overflow. */
13278 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13279 else
13280 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
13281 /* We can't normally display a 3-digit number,
13282 so get us a 2-digit number that is close. */
13283 if (total == 100)
13284 total = 99;
13285 sprintf (decode_mode_spec_buf, "%2d%%", total);
13286 return decode_mode_spec_buf;
13287 }
13288 }
13289
13290 /* Display percentage of size above the bottom of the screen. */
13291 case 'P':
13292 {
13293 int toppos = marker_position (w->start);
13294 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13295 int total = BUF_ZV (b) - BUF_BEGV (b);
13296
13297 if (botpos >= BUF_ZV (b))
13298 {
13299 if (toppos <= BUF_BEGV (b))
13300 return "All";
13301 else
13302 return "Bottom";
13303 }
13304 else
13305 {
13306 if (total > 1000000)
13307 /* Do it differently for a large value, to avoid overflow. */
13308 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13309 else
13310 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
13311 /* We can't normally display a 3-digit number,
13312 so get us a 2-digit number that is close. */
13313 if (total == 100)
13314 total = 99;
13315 if (toppos <= BUF_BEGV (b))
13316 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13317 else
13318 sprintf (decode_mode_spec_buf, "%2d%%", total);
13319 return decode_mode_spec_buf;
13320 }
13321 }
13322
13323 case 's':
13324 /* status of process */
13325 obj = Fget_buffer_process (w->buffer);
13326 if (NILP (obj))
13327 return "no process";
13328 #ifdef subprocesses
13329 obj = Fsymbol_name (Fprocess_status (obj));
13330 #endif
13331 break;
13332
13333 case 't': /* indicate TEXT or BINARY */
13334 #ifdef MODE_LINE_BINARY_TEXT
13335 return MODE_LINE_BINARY_TEXT (b);
13336 #else
13337 return "T";
13338 #endif
13339
13340 case 'z':
13341 /* coding-system (not including end-of-line format) */
13342 case 'Z':
13343 /* coding-system (including end-of-line type) */
13344 {
13345 int eol_flag = (c == 'Z');
13346 char *p = decode_mode_spec_buf;
13347
13348 if (! FRAME_WINDOW_P (f))
13349 {
13350 /* No need to mention EOL here--the terminal never needs
13351 to do EOL conversion. */
13352 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13353 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
13354 }
13355 p = decode_mode_spec_coding (b->buffer_file_coding_system,
13356 p, eol_flag);
13357
13358 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
13359 #ifdef subprocesses
13360 obj = Fget_buffer_process (Fcurrent_buffer ());
13361 if (PROCESSP (obj))
13362 {
13363 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13364 p, eol_flag);
13365 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13366 p, eol_flag);
13367 }
13368 #endif /* subprocesses */
13369 #endif /* 0 */
13370 *p = 0;
13371 return decode_mode_spec_buf;
13372 }
13373 }
13374
13375 if (STRINGP (obj))
13376 return (char *) XSTRING (obj)->data;
13377 else
13378 return "";
13379 }
13380
13381
13382 /* Count up to COUNT lines starting from START / START_BYTE.
13383 But don't go beyond LIMIT_BYTE.
13384 Return the number of lines thus found (always nonnegative).
13385
13386 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
13387
13388 static int
13389 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13390 int start, start_byte, limit_byte, count;
13391 int *byte_pos_ptr;
13392 {
13393 register unsigned char *cursor;
13394 unsigned char *base;
13395
13396 register int ceiling;
13397 register unsigned char *ceiling_addr;
13398 int orig_count = count;
13399
13400 /* If we are not in selective display mode,
13401 check only for newlines. */
13402 int selective_display = (!NILP (current_buffer->selective_display)
13403 && !INTEGERP (current_buffer->selective_display));
13404
13405 if (count > 0)
13406 {
13407 while (start_byte < limit_byte)
13408 {
13409 ceiling = BUFFER_CEILING_OF (start_byte);
13410 ceiling = min (limit_byte - 1, ceiling);
13411 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13412 base = (cursor = BYTE_POS_ADDR (start_byte));
13413 while (1)
13414 {
13415 if (selective_display)
13416 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13417 ;
13418 else
13419 while (*cursor != '\n' && ++cursor != ceiling_addr)
13420 ;
13421
13422 if (cursor != ceiling_addr)
13423 {
13424 if (--count == 0)
13425 {
13426 start_byte += cursor - base + 1;
13427 *byte_pos_ptr = start_byte;
13428 return orig_count;
13429 }
13430 else
13431 if (++cursor == ceiling_addr)
13432 break;
13433 }
13434 else
13435 break;
13436 }
13437 start_byte += cursor - base;
13438 }
13439 }
13440 else
13441 {
13442 while (start_byte > limit_byte)
13443 {
13444 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13445 ceiling = max (limit_byte, ceiling);
13446 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13447 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
13448 while (1)
13449 {
13450 if (selective_display)
13451 while (--cursor != ceiling_addr
13452 && *cursor != '\n' && *cursor != 015)
13453 ;
13454 else
13455 while (--cursor != ceiling_addr && *cursor != '\n')
13456 ;
13457
13458 if (cursor != ceiling_addr)
13459 {
13460 if (++count == 0)
13461 {
13462 start_byte += cursor - base + 1;
13463 *byte_pos_ptr = start_byte;
13464 /* When scanning backwards, we should
13465 not count the newline posterior to which we stop. */
13466 return - orig_count - 1;
13467 }
13468 }
13469 else
13470 break;
13471 }
13472 /* Here we add 1 to compensate for the last decrement
13473 of CURSOR, which took it past the valid range. */
13474 start_byte += cursor - base + 1;
13475 }
13476 }
13477
13478 *byte_pos_ptr = limit_byte;
13479
13480 if (count < 0)
13481 return - orig_count + count;
13482 return orig_count - count;
13483
13484 }
13485
13486
13487 \f
13488 /***********************************************************************
13489 Displaying strings
13490 ***********************************************************************/
13491
13492 /* Display a NUL-terminated string, starting with index START.
13493
13494 If STRING is non-null, display that C string. Otherwise, the Lisp
13495 string LISP_STRING is displayed.
13496
13497 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13498 FACE_STRING. Display STRING or LISP_STRING with the face at
13499 FACE_STRING_POS in FACE_STRING:
13500
13501 Display the string in the environment given by IT, but use the
13502 standard display table, temporarily.
13503
13504 FIELD_WIDTH is the minimum number of output glyphs to produce.
13505 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13506 with spaces. If STRING has more characters, more than FIELD_WIDTH
13507 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13508
13509 PRECISION is the maximum number of characters to output from
13510 STRING. PRECISION < 0 means don't truncate the string.
13511
13512 This is roughly equivalent to printf format specifiers:
13513
13514 FIELD_WIDTH PRECISION PRINTF
13515 ----------------------------------------
13516 -1 -1 %s
13517 -1 10 %.10s
13518 10 -1 %10s
13519 20 10 %20.10s
13520
13521 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13522 display them, and < 0 means obey the current buffer's value of
13523 enable_multibyte_characters.
13524
13525 Value is the number of glyphs produced. */
13526
13527 static int
13528 display_string (string, lisp_string, face_string, face_string_pos,
13529 start, it, field_width, precision, max_x, multibyte)
13530 unsigned char *string;
13531 Lisp_Object lisp_string;
13532 Lisp_Object face_string;
13533 int face_string_pos;
13534 int start;
13535 struct it *it;
13536 int field_width, precision, max_x;
13537 int multibyte;
13538 {
13539 int hpos_at_start = it->hpos;
13540 int saved_face_id = it->face_id;
13541 struct glyph_row *row = it->glyph_row;
13542
13543 /* Initialize the iterator IT for iteration over STRING beginning
13544 with index START. We assume that IT may be modified here (which
13545 means that display_line has to do something when displaying a
13546 mini-buffer prompt, which it does). */
13547 reseat_to_string (it, string, lisp_string, start,
13548 precision, field_width, multibyte);
13549
13550 /* If displaying STRING, set up the face of the iterator
13551 from LISP_STRING, if that's given. */
13552 if (STRINGP (face_string))
13553 {
13554 int endptr;
13555 struct face *face;
13556
13557 it->face_id
13558 = face_at_string_position (it->w, face_string, face_string_pos,
13559 0, it->region_beg_charpos,
13560 it->region_end_charpos,
13561 &endptr, it->base_face_id);
13562 face = FACE_FROM_ID (it->f, it->face_id);
13563 it->face_box_p = face->box != FACE_NO_BOX;
13564 }
13565
13566 /* Set max_x to the maximum allowed X position. Don't let it go
13567 beyond the right edge of the window. */
13568 if (max_x <= 0)
13569 max_x = it->last_visible_x;
13570 else
13571 max_x = min (max_x, it->last_visible_x);
13572
13573 /* Skip over display elements that are not visible. because IT->w is
13574 hscrolled. */
13575 if (it->current_x < it->first_visible_x)
13576 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13577 MOVE_TO_POS | MOVE_TO_X);
13578
13579 row->ascent = it->max_ascent;
13580 row->height = it->max_ascent + it->max_descent;
13581 row->phys_ascent = it->max_phys_ascent;
13582 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13583
13584 /* This condition is for the case that we are called with current_x
13585 past last_visible_x. */
13586 while (it->current_x < max_x)
13587 {
13588 int x_before, x, n_glyphs_before, i, nglyphs;
13589
13590 /* Get the next display element. */
13591 if (!get_next_display_element (it))
13592 break;
13593
13594 /* Produce glyphs. */
13595 x_before = it->current_x;
13596 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13597 PRODUCE_GLYPHS (it);
13598
13599 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
13600 i = 0;
13601 x = x_before;
13602 while (i < nglyphs)
13603 {
13604 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13605
13606 if (!it->truncate_lines_p
13607 && x + glyph->pixel_width > max_x)
13608 {
13609 /* End of continued line or max_x reached. */
13610 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
13611 it->current_x = x;
13612 break;
13613 }
13614 else if (x + glyph->pixel_width > it->first_visible_x)
13615 {
13616 /* Glyph is at least partially visible. */
13617 ++it->hpos;
13618 if (x < it->first_visible_x)
13619 it->glyph_row->x = x - it->first_visible_x;
13620 }
13621 else
13622 {
13623 /* Glyph is off the left margin of the display area.
13624 Should not happen. */
13625 abort ();
13626 }
13627
13628 row->ascent = max (row->ascent, it->max_ascent);
13629 row->height = max (row->height, it->max_ascent + it->max_descent);
13630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13631 row->phys_height = max (row->phys_height,
13632 it->max_phys_ascent + it->max_phys_descent);
13633 x += glyph->pixel_width;
13634 ++i;
13635 }
13636
13637 /* Stop if max_x reached. */
13638 if (i < nglyphs)
13639 break;
13640
13641 /* Stop at line ends. */
13642 if (ITERATOR_AT_END_OF_LINE_P (it))
13643 {
13644 it->continuation_lines_width = 0;
13645 break;
13646 }
13647
13648 set_iterator_to_next (it, 1);
13649
13650 /* Stop if truncating at the right edge. */
13651 if (it->truncate_lines_p
13652 && it->current_x >= it->last_visible_x)
13653 {
13654 /* Add truncation mark, but don't do it if the line is
13655 truncated at a padding space. */
13656 if (IT_CHARPOS (*it) < it->string_nchars)
13657 {
13658 if (!FRAME_WINDOW_P (it->f))
13659 produce_special_glyphs (it, IT_TRUNCATION);
13660 it->glyph_row->truncated_on_right_p = 1;
13661 }
13662 break;
13663 }
13664 }
13665
13666 /* Maybe insert a truncation at the left. */
13667 if (it->first_visible_x
13668 && IT_CHARPOS (*it) > 0)
13669 {
13670 if (!FRAME_WINDOW_P (it->f))
13671 insert_left_trunc_glyphs (it);
13672 it->glyph_row->truncated_on_left_p = 1;
13673 }
13674
13675 it->face_id = saved_face_id;
13676
13677 /* Value is number of columns displayed. */
13678 return it->hpos - hpos_at_start;
13679 }
13680
13681
13682 \f
13683 /* This is like a combination of memq and assq. Return 1 if PROPVAL
13684 appears as an element of LIST or as the car of an element of LIST.
13685 If PROPVAL is a list, compare each element against LIST in that
13686 way, and return 1 if any element of PROPVAL is found in LIST.
13687 Otherwise return 0. This function cannot quit. */
13688
13689 int
13690 invisible_p (propval, list)
13691 register Lisp_Object propval;
13692 Lisp_Object list;
13693 {
13694 register Lisp_Object tail, proptail;
13695
13696 for (tail = list; CONSP (tail); tail = XCDR (tail))
13697 {
13698 register Lisp_Object tem;
13699 tem = XCAR (tail);
13700 if (EQ (propval, tem))
13701 return 1;
13702 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13703 return 1;
13704 }
13705
13706 if (CONSP (propval))
13707 {
13708 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13709 {
13710 Lisp_Object propelt;
13711 propelt = XCAR (proptail);
13712 for (tail = list; CONSP (tail); tail = XCDR (tail))
13713 {
13714 register Lisp_Object tem;
13715 tem = XCAR (tail);
13716 if (EQ (propelt, tem))
13717 return 1;
13718 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13719 return 1;
13720 }
13721 }
13722 }
13723
13724 return 0;
13725 }
13726
13727
13728 /* Return 1 if PROPVAL appears as the car of an element of LIST and
13729 the cdr of that element is non-nil. If PROPVAL is a list, check
13730 each element of PROPVAL in that way, and the first time some
13731 element is found, return 1 if the cdr of that element is non-nil.
13732 Otherwise return 0. This function cannot quit. */
13733
13734 int
13735 invisible_ellipsis_p (propval, list)
13736 register Lisp_Object propval;
13737 Lisp_Object list;
13738 {
13739 register Lisp_Object tail, proptail;
13740
13741 for (tail = list; CONSP (tail); tail = XCDR (tail))
13742 {
13743 register Lisp_Object tem;
13744 tem = XCAR (tail);
13745 if (CONSP (tem) && EQ (propval, XCAR (tem)))
13746 return ! NILP (XCDR (tem));
13747 }
13748
13749 if (CONSP (propval))
13750 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
13751 {
13752 Lisp_Object propelt;
13753 propelt = XCAR (proptail);
13754 for (tail = list; CONSP (tail); tail = XCDR (tail))
13755 {
13756 register Lisp_Object tem;
13757 tem = XCAR (tail);
13758 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
13759 return ! NILP (XCDR (tem));
13760 }
13761 }
13762
13763 return 0;
13764 }
13765
13766
13767 \f
13768 /***********************************************************************
13769 Initialization
13770 ***********************************************************************/
13771
13772 void
13773 syms_of_xdisp ()
13774 {
13775 Vwith_echo_area_save_vector = Qnil;
13776 staticpro (&Vwith_echo_area_save_vector);
13777
13778 Vmessage_stack = Qnil;
13779 staticpro (&Vmessage_stack);
13780
13781 Qinhibit_redisplay = intern ("inhibit-redisplay");
13782 staticpro (&Qinhibit_redisplay);
13783
13784 #if GLYPH_DEBUG
13785 defsubr (&Sdump_glyph_matrix);
13786 defsubr (&Sdump_glyph_row);
13787 defsubr (&Sdump_tool_bar_row);
13788 defsubr (&Strace_redisplay_toggle);
13789 defsubr (&Strace_to_stderr);
13790 #endif
13791
13792 staticpro (&Qmenu_bar_update_hook);
13793 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
13794
13795 staticpro (&Qoverriding_terminal_local_map);
13796 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
13797
13798 staticpro (&Qoverriding_local_map);
13799 Qoverriding_local_map = intern ("overriding-local-map");
13800
13801 staticpro (&Qwindow_scroll_functions);
13802 Qwindow_scroll_functions = intern ("window-scroll-functions");
13803
13804 staticpro (&Qredisplay_end_trigger_functions);
13805 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
13806
13807 staticpro (&Qinhibit_point_motion_hooks);
13808 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
13809
13810 QCdata = intern (":data");
13811 staticpro (&QCdata);
13812 Qdisplay = intern ("display");
13813 staticpro (&Qdisplay);
13814 Qspace_width = intern ("space-width");
13815 staticpro (&Qspace_width);
13816 Qraise = intern ("raise");
13817 staticpro (&Qraise);
13818 Qspace = intern ("space");
13819 staticpro (&Qspace);
13820 Qmargin = intern ("margin");
13821 staticpro (&Qmargin);
13822 Qleft_margin = intern ("left-margin");
13823 staticpro (&Qleft_margin);
13824 Qright_margin = intern ("right-margin");
13825 staticpro (&Qright_margin);
13826 Qalign_to = intern ("align-to");
13827 staticpro (&Qalign_to);
13828 QCalign_to = intern (":align-to");
13829 staticpro (&QCalign_to);
13830 Qrelative_width = intern ("relative-width");
13831 staticpro (&Qrelative_width);
13832 QCrelative_width = intern (":relative-width");
13833 staticpro (&QCrelative_width);
13834 QCrelative_height = intern (":relative-height");
13835 staticpro (&QCrelative_height);
13836 QCeval = intern (":eval");
13837 staticpro (&QCeval);
13838 Qwhen = intern ("when");
13839 staticpro (&Qwhen);
13840 QCfile = intern (":file");
13841 staticpro (&QCfile);
13842 Qfontified = intern ("fontified");
13843 staticpro (&Qfontified);
13844 Qfontification_functions = intern ("fontification-functions");
13845 staticpro (&Qfontification_functions);
13846 Qtrailing_whitespace = intern ("trailing-whitespace");
13847 staticpro (&Qtrailing_whitespace);
13848 Qimage = intern ("image");
13849 staticpro (&Qimage);
13850 Qmessage_truncate_lines = intern ("message-truncate-lines");
13851 staticpro (&Qmessage_truncate_lines);
13852 Qgrow_only = intern ("grow-only");
13853 staticpro (&Qgrow_only);
13854
13855 last_arrow_position = Qnil;
13856 last_arrow_string = Qnil;
13857 staticpro (&last_arrow_position);
13858 staticpro (&last_arrow_string);
13859
13860 echo_buffer[0] = echo_buffer[1] = Qnil;
13861 staticpro (&echo_buffer[0]);
13862 staticpro (&echo_buffer[1]);
13863
13864 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
13865 staticpro (&echo_area_buffer[0]);
13866 staticpro (&echo_area_buffer[1]);
13867
13868 Vmessages_buffer_name = build_string ("*Messages*");
13869 staticpro (&Vmessages_buffer_name);
13870
13871 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
13872 "Non-nil means highlight trailing whitespace.\n\
13873 The face used for trailing whitespace is `trailing-whitespace'.");
13874 Vshow_trailing_whitespace = Qnil;
13875
13876 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
13877 "Non-nil means don't actually do any redisplay.\n\
13878 This is used for internal purposes.");
13879 Vinhibit_redisplay = Qnil;
13880
13881 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
13882 "String (or mode line construct) included (normally) in `mode-line-format'.");
13883 Vglobal_mode_string = Qnil;
13884
13885 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
13886 "Marker for where to display an arrow on top of the buffer text.\n\
13887 This must be the beginning of a line in order to work.\n\
13888 See also `overlay-arrow-string'.");
13889 Voverlay_arrow_position = Qnil;
13890
13891 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
13892 "String to display as an arrow. See also `overlay-arrow-position'.");
13893 Voverlay_arrow_string = Qnil;
13894
13895 DEFVAR_INT ("scroll-step", &scroll_step,
13896 "*The number of lines to try scrolling a window by when point moves out.\n\
13897 If that fails to bring point back on frame, point is centered instead.\n\
13898 If this is zero, point is always centered after it moves off frame.\n\
13899 If you want scrolling to always be a line at a time, you should set\n\
13900 `scroll-conservatively' to a large value rather than set this to 1.");
13901
13902 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
13903 "*Scroll up to this many lines, to bring point back on screen.\n\
13904 A value of zero means to scroll the text to center point vertically\n\
13905 in the window.");
13906 scroll_conservatively = 0;
13907
13908 DEFVAR_INT ("scroll-margin", &scroll_margin,
13909 "*Number of lines of margin at the top and bottom of a window.\n\
13910 Recenter the window whenever point gets within this many lines\n\
13911 of the top or bottom of the window.");
13912 scroll_margin = 0;
13913
13914 #if GLYPH_DEBUG
13915 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
13916 #endif
13917
13918 DEFVAR_BOOL ("truncate-partial-width-windows",
13919 &truncate_partial_width_windows,
13920 "*Non-nil means truncate lines in all windows less than full frame wide.");
13921 truncate_partial_width_windows = 1;
13922
13923 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
13924 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
13925 Any other value means to use the appropriate face, `mode-line',\n\
13926 `header-line', or `menu' respectively.\n\
13927 \n\
13928 This variable is deprecated; please change the above faces instead.");
13929 mode_line_inverse_video = 1;
13930
13931 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
13932 "*Maximum buffer size for which line number should be displayed.\n\
13933 If the buffer is bigger than this, the line number does not appear\n\
13934 in the mode line. A value of nil means no limit.");
13935 Vline_number_display_limit = Qnil;
13936
13937 DEFVAR_INT ("line-number-display-limit-width",
13938 &line_number_display_limit_width,
13939 "*Maximum line width (in characters) for line number display.\n\
13940 If the average length of the lines near point is bigger than this, then the\n\
13941 line number may be omitted from the mode line.");
13942 line_number_display_limit_width = 200;
13943
13944 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
13945 "*Non-nil means highlight region even in nonselected windows.");
13946 highlight_nonselected_windows = 0;
13947
13948 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
13949 "Non-nil if more than one frame is visible on this display.\n\
13950 Minibuffer-only frames don't count, but iconified frames do.\n\
13951 This variable is not guaranteed to be accurate except while processing\n\
13952 `frame-title-format' and `icon-title-format'.");
13953
13954 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
13955 "Template for displaying the title bar of visible frames.\n\
13956 \(Assuming the window manager supports this feature.)\n\
13957 This variable has the same structure as `mode-line-format' (which see),\n\
13958 and is used only on frames for which no explicit name has been set\n\
13959 \(see `modify-frame-parameters').");
13960 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
13961 "Template for displaying the title bar of an iconified frame.\n\
13962 \(Assuming the window manager supports this feature.)\n\
13963 This variable has the same structure as `mode-line-format' (which see),\n\
13964 and is used only on frames for which no explicit name has been set\n\
13965 \(see `modify-frame-parameters').");
13966 Vicon_title_format
13967 = Vframe_title_format
13968 = Fcons (intern ("multiple-frames"),
13969 Fcons (build_string ("%b"),
13970 Fcons (Fcons (build_string (""),
13971 Fcons (intern ("invocation-name"),
13972 Fcons (build_string ("@"),
13973 Fcons (intern ("system-name"),
13974 Qnil)))),
13975 Qnil)));
13976
13977 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
13978 "Maximum number of lines to keep in the message log buffer.\n\
13979 If nil, disable message logging. If t, log messages but don't truncate\n\
13980 the buffer when it becomes large.");
13981 XSETFASTINT (Vmessage_log_max, 50);
13982
13983 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
13984 "Functions called before redisplay, if window sizes have changed.\n\
13985 The value should be a list of functions that take one argument.\n\
13986 Just before redisplay, for each frame, if any of its windows have changed\n\
13987 size since the last redisplay, or have been split or deleted,\n\
13988 all the functions in the list are called, with the frame as argument.");
13989 Vwindow_size_change_functions = Qnil;
13990
13991 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
13992 "List of Functions to call before redisplaying a window with scrolling.\n\
13993 Each function is called with two arguments, the window\n\
13994 and its new display-start position. Note that the value of `window-end'\n\
13995 is not valid when these functions are called.");
13996 Vwindow_scroll_functions = Qnil;
13997
13998 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
13999 "*Non-nil means automatically resize tool-bars.\n\
14000 This increases a tool-bar's height if not all tool-bar items are visible.\n\
14001 It decreases a tool-bar's height when it would display blank lines\n\
14002 otherwise.");
14003 auto_resize_tool_bars_p = 1;
14004
14005 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14006 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14007 auto_raise_tool_bar_buttons_p = 1;
14008
14009 DEFVAR_INT ("tool-bar-button-margin", &tool_bar_button_margin,
14010 "*Margin around tool-bar buttons in pixels.");
14011 tool_bar_button_margin = 1;
14012
14013 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14014 "Relief thickness of tool-bar buttons.");
14015 tool_bar_button_relief = 3;
14016
14017 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14018 "List of functions to call to fontify regions of text.\n\
14019 Each function is called with one argument POS. Functions must\n\
14020 fontify a region starting at POS in the current buffer, and give\n\
14021 fontified regions the property `fontified'.\n\
14022 This variable automatically becomes buffer-local when set.");
14023 Vfontification_functions = Qnil;
14024 Fmake_variable_buffer_local (Qfontification_functions);
14025
14026 DEFVAR_BOOL ("unibyte-display-via-language-environment",
14027 &unibyte_display_via_language_environment,
14028 "*Non-nil means display unibyte text according to language environment.\n\
14029 Specifically this means that unibyte non-ASCII characters\n\
14030 are displayed by converting them to the equivalent multibyte characters\n\
14031 according to the current language environment. As a result, they are\n\
14032 displayed according to the current fontset.");
14033 unibyte_display_via_language_environment = 0;
14034
14035 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14036 "*Maximum height for resizing mini-windows.\n\
14037 If a float, it specifies a fraction of the mini-window frame's height.\n\
14038 If an integer, it specifies a number of lines.");
14039 Vmax_mini_window_height = make_float (0.25);
14040
14041 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
14042 "*How to resize the mini-window.\n\
14043 A value of nil means don't automatically resize mini-windows.\n\
14044 A value of t means resize it to fit the text displayed in it.\n\
14045 A value of `grow-only', the default, means let mini-windows grow\n\
14046 only, until the its display becomes empty, at which point the mini-window\n\
14047 goes back to its normal size.");
14048 Vresize_mini_windows = Qgrow_only;
14049
14050 DEFVAR_BOOL ("cursor-in-non-selected-windows",
14051 &cursor_in_non_selected_windows,
14052 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14053 Nil means don't display a cursor there.");
14054 cursor_in_non_selected_windows = 1;
14055
14056 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14057 "*Non-nil means scroll the display automatically to make point visible.");
14058 automatic_hscrolling_p = 1;
14059
14060 DEFVAR_LISP ("image-types", &Vimage_types,
14061 "List of supported image types.\n\
14062 Each element of the list is a symbol for a supported image type.");
14063 Vimage_types = Qnil;
14064
14065 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14066 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14067 Bind this around calls to `message' to let it take effect.");
14068 message_truncate_lines = 0;
14069
14070 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14071 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14072 Can be used to update submenus whose contents should vary.");
14073 Vmenu_bar_update_hook = Qnil;
14074 }
14075
14076
14077 /* Initialize this module when Emacs starts. */
14078
14079 void
14080 init_xdisp ()
14081 {
14082 Lisp_Object root_window;
14083 struct window *mini_w;
14084
14085 current_header_line_height = current_mode_line_height = -1;
14086
14087 CHARPOS (this_line_start_pos) = 0;
14088
14089 mini_w = XWINDOW (minibuf_window);
14090 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
14091
14092 if (!noninteractive)
14093 {
14094 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14095 int i;
14096
14097 XSETFASTINT (XWINDOW (root_window)->top, FRAME_TOP_MARGIN (f));
14098 set_window_height (root_window,
14099 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
14100 0);
14101 XSETFASTINT (mini_w->top, FRAME_HEIGHT (f) - 1);
14102 set_window_height (minibuf_window, 1, 0);
14103
14104 XSETFASTINT (XWINDOW (root_window)->width, FRAME_WIDTH (f));
14105 XSETFASTINT (mini_w->width, FRAME_WIDTH (f));
14106
14107 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14108 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14109 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14110
14111 /* The default ellipsis glyphs `...'. */
14112 for (i = 0; i < 3; ++i)
14113 XSETFASTINT (default_invis_vector[i], '.');
14114 }
14115
14116 #ifdef HAVE_WINDOW_SYSTEM
14117 {
14118 /* Allocate the buffer for frame titles. */
14119 int size = 100;
14120 frame_title_buf = (char *) xmalloc (size);
14121 frame_title_buf_end = frame_title_buf + size;
14122 frame_title_ptr = NULL;
14123 }
14124 #endif /* HAVE_WINDOW_SYSTEM */
14125
14126 help_echo_showing_p = 0;
14127 }
14128
14129