(x_produce_glyphs): Use FRAME_BASELINE_OFFSET.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99, 2000, 2001, 2002, 2003
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 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 do? 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 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 an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
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 MAC_OS
198 #include "macterm.h"
199 #endif
200
201 #define INFINITY 10000000
202
203 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
204 || defined (USE_GTK)
205 extern void set_frame_menubar P_ ((struct frame *f, int, int));
206 extern int pending_menu_activation;
207 #endif
208
209 extern int interrupt_input;
210 extern int command_loop_level;
211
212 extern int minibuffer_auto_raise;
213 extern Lisp_Object Vminibuffer_list;
214
215 extern Lisp_Object Qface;
216 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
217
218 extern Lisp_Object Voverriding_local_map;
219 extern Lisp_Object Voverriding_local_map_menu_flag;
220 extern Lisp_Object Qmenu_item;
221 extern Lisp_Object Qwhen;
222
223 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
224 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
225 Lisp_Object Qredisplay_end_trigger_functions;
226 Lisp_Object Qinhibit_point_motion_hooks;
227 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
228 Lisp_Object Qfontified;
229 Lisp_Object Qgrow_only;
230 Lisp_Object Qinhibit_eval_during_redisplay;
231 Lisp_Object Qbuffer_position, Qposition, Qobject;
232
233 /* Cursor shapes */
234 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
235
236 Lisp_Object Qrisky_local_variable;
237
238 /* Holds the list (error). */
239 Lisp_Object list_of_error;
240
241 /* Functions called to fontify regions of text. */
242
243 Lisp_Object Vfontification_functions;
244 Lisp_Object Qfontification_functions;
245
246 /* Non-zero means draw tool bar buttons raised when the mouse moves
247 over them. */
248
249 int auto_raise_tool_bar_buttons_p;
250
251 /* Margin around tool bar buttons in pixels. */
252
253 Lisp_Object Vtool_bar_button_margin;
254
255 /* Thickness of shadow to draw around tool bar buttons. */
256
257 EMACS_INT tool_bar_button_relief;
258
259 /* Non-zero means automatically resize tool-bars so that all tool-bar
260 items are visible, and no blank lines remain. */
261
262 int auto_resize_tool_bars_p;
263
264 /* Non-nil means don't actually do any redisplay. */
265
266 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
267
268 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
269
270 int inhibit_eval_during_redisplay;
271
272 /* Names of text properties relevant for redisplay. */
273
274 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
275 extern Lisp_Object Qface, Qinvisible, Qwidth;
276
277 /* Symbols used in text property values. */
278
279 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
280 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
281 Lisp_Object Qmargin;
282 extern Lisp_Object Qheight;
283 extern Lisp_Object QCwidth, QCheight, QCascent;
284
285 /* Non-nil means highlight trailing whitespace. */
286
287 Lisp_Object Vshow_trailing_whitespace;
288
289 /* Name of the face used to highlight trailing whitespace. */
290
291 Lisp_Object Qtrailing_whitespace;
292
293 /* The symbol `image' which is the car of the lists used to represent
294 images in Lisp. */
295
296 Lisp_Object Qimage;
297
298 /* Non-zero means print newline to stdout before next mini-buffer
299 message. */
300
301 int noninteractive_need_newline;
302
303 /* Non-zero means print newline to message log before next message. */
304
305 static int message_log_need_newline;
306
307 /* Three markers that message_dolog uses.
308 It could allocate them itself, but that causes trouble
309 in handling memory-full errors. */
310 static Lisp_Object message_dolog_marker1;
311 static Lisp_Object message_dolog_marker2;
312 static Lisp_Object message_dolog_marker3;
313 \f
314 /* The buffer position of the first character appearing entirely or
315 partially on the line of the selected window which contains the
316 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
317 redisplay optimization in redisplay_internal. */
318
319 static struct text_pos this_line_start_pos;
320
321 /* Number of characters past the end of the line above, including the
322 terminating newline. */
323
324 static struct text_pos this_line_end_pos;
325
326 /* The vertical positions and the height of this line. */
327
328 static int this_line_vpos;
329 static int this_line_y;
330 static int this_line_pixel_height;
331
332 /* X position at which this display line starts. Usually zero;
333 negative if first character is partially visible. */
334
335 static int this_line_start_x;
336
337 /* Buffer that this_line_.* variables are referring to. */
338
339 static struct buffer *this_line_buffer;
340
341 /* Nonzero means truncate lines in all windows less wide than the
342 frame. */
343
344 int truncate_partial_width_windows;
345
346 /* A flag to control how to display unibyte 8-bit character. */
347
348 int unibyte_display_via_language_environment;
349
350 /* Nonzero means we have more than one non-mini-buffer-only frame.
351 Not guaranteed to be accurate except while parsing
352 frame-title-format. */
353
354 int multiple_frames;
355
356 Lisp_Object Vglobal_mode_string;
357
358 /* Marker for where to display an arrow on top of the buffer text. */
359
360 Lisp_Object Voverlay_arrow_position;
361
362 /* String to display for the arrow. Only used on terminal frames. */
363
364 Lisp_Object Voverlay_arrow_string;
365
366 /* Values of those variables at last redisplay. However, if
367 Voverlay_arrow_position is a marker, last_arrow_position is its
368 numerical position. */
369
370 static Lisp_Object last_arrow_position, last_arrow_string;
371
372 /* Like mode-line-format, but for the title bar on a visible frame. */
373
374 Lisp_Object Vframe_title_format;
375
376 /* Like mode-line-format, but for the title bar on an iconified frame. */
377
378 Lisp_Object Vicon_title_format;
379
380 /* List of functions to call when a window's size changes. These
381 functions get one arg, a frame on which one or more windows' sizes
382 have changed. */
383
384 static Lisp_Object Vwindow_size_change_functions;
385
386 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
387
388 /* Nonzero if overlay arrow has been displayed once in this window. */
389
390 static int overlay_arrow_seen;
391
392 /* Nonzero means highlight the region even in nonselected windows. */
393
394 int highlight_nonselected_windows;
395
396 /* If cursor motion alone moves point off frame, try scrolling this
397 many lines up or down if that will bring it back. */
398
399 static EMACS_INT scroll_step;
400
401 /* Nonzero means scroll just far enough to bring point back on the
402 screen, when appropriate. */
403
404 static EMACS_INT scroll_conservatively;
405
406 /* Recenter the window whenever point gets within this many lines of
407 the top or bottom of the window. This value is translated into a
408 pixel value by multiplying it with CANON_Y_UNIT, which means that
409 there is really a fixed pixel height scroll margin. */
410
411 EMACS_INT scroll_margin;
412
413 /* Number of windows showing the buffer of the selected window (or
414 another buffer with the same base buffer). keyboard.c refers to
415 this. */
416
417 int buffer_shared;
418
419 /* Vector containing glyphs for an ellipsis `...'. */
420
421 static Lisp_Object default_invis_vector[3];
422
423 /* Zero means display the mode-line/header-line/menu-bar in the default face
424 (this slightly odd definition is for compatibility with previous versions
425 of emacs), non-zero means display them using their respective faces.
426
427 This variable is deprecated. */
428
429 int mode_line_inverse_video;
430
431 /* Prompt to display in front of the mini-buffer contents. */
432
433 Lisp_Object minibuf_prompt;
434
435 /* Width of current mini-buffer prompt. Only set after display_line
436 of the line that contains the prompt. */
437
438 int minibuf_prompt_width;
439
440 /* This is the window where the echo area message was displayed. It
441 is always a mini-buffer window, but it may not be the same window
442 currently active as a mini-buffer. */
443
444 Lisp_Object echo_area_window;
445
446 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
447 pushes the current message and the value of
448 message_enable_multibyte on the stack, the function restore_message
449 pops the stack and displays MESSAGE again. */
450
451 Lisp_Object Vmessage_stack;
452
453 /* Nonzero means multibyte characters were enabled when the echo area
454 message was specified. */
455
456 int message_enable_multibyte;
457
458 /* Nonzero if we should redraw the mode lines on the next redisplay. */
459
460 int update_mode_lines;
461
462 /* Nonzero if window sizes or contents have changed since last
463 redisplay that finished. */
464
465 int windows_or_buffers_changed;
466
467 /* Nonzero means a frame's cursor type has been changed. */
468
469 int cursor_type_changed;
470
471 /* Nonzero after display_mode_line if %l was used and it displayed a
472 line number. */
473
474 int line_number_displayed;
475
476 /* Maximum buffer size for which to display line numbers. */
477
478 Lisp_Object Vline_number_display_limit;
479
480 /* Line width to consider when repositioning for line number display. */
481
482 static EMACS_INT line_number_display_limit_width;
483
484 /* Number of lines to keep in the message log buffer. t means
485 infinite. nil means don't log at all. */
486
487 Lisp_Object Vmessage_log_max;
488
489 /* The name of the *Messages* buffer, a string. */
490
491 static Lisp_Object Vmessages_buffer_name;
492
493 /* Current, index 0, and last displayed echo area message. Either
494 buffers from echo_buffers, or nil to indicate no message. */
495
496 Lisp_Object echo_area_buffer[2];
497
498 /* The buffers referenced from echo_area_buffer. */
499
500 static Lisp_Object echo_buffer[2];
501
502 /* A vector saved used in with_area_buffer to reduce consing. */
503
504 static Lisp_Object Vwith_echo_area_save_vector;
505
506 /* Non-zero means display_echo_area should display the last echo area
507 message again. Set by redisplay_preserve_echo_area. */
508
509 static int display_last_displayed_message_p;
510
511 /* Nonzero if echo area is being used by print; zero if being used by
512 message. */
513
514 int message_buf_print;
515
516 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
517
518 Lisp_Object Qinhibit_menubar_update;
519 int inhibit_menubar_update;
520
521 /* Maximum height for resizing mini-windows. Either a float
522 specifying a fraction of the available height, or an integer
523 specifying a number of lines. */
524
525 Lisp_Object Vmax_mini_window_height;
526
527 /* Non-zero means messages should be displayed with truncated
528 lines instead of being continued. */
529
530 int message_truncate_lines;
531 Lisp_Object Qmessage_truncate_lines;
532
533 /* Set to 1 in clear_message to make redisplay_internal aware
534 of an emptied echo area. */
535
536 static int message_cleared_p;
537
538 /* Non-zero means we want a hollow cursor in windows that are not
539 selected. Zero means there's no cursor in such windows. */
540
541 Lisp_Object Vcursor_in_non_selected_windows;
542 Lisp_Object Qcursor_in_non_selected_windows;
543
544 /* How to blink the default frame cursor off. */
545 Lisp_Object Vblink_cursor_alist;
546
547 /* A scratch glyph row with contents used for generating truncation
548 glyphs. Also used in direct_output_for_insert. */
549
550 #define MAX_SCRATCH_GLYPHS 100
551 struct glyph_row scratch_glyph_row;
552 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
553
554 /* Ascent and height of the last line processed by move_it_to. */
555
556 static int last_max_ascent, last_height;
557
558 /* Non-zero if there's a help-echo in the echo area. */
559
560 int help_echo_showing_p;
561
562 /* If >= 0, computed, exact values of mode-line and header-line height
563 to use in the macros CURRENT_MODE_LINE_HEIGHT and
564 CURRENT_HEADER_LINE_HEIGHT. */
565
566 int current_mode_line_height, current_header_line_height;
567
568 /* The maximum distance to look ahead for text properties. Values
569 that are too small let us call compute_char_face and similar
570 functions too often which is expensive. Values that are too large
571 let us call compute_char_face and alike too often because we
572 might not be interested in text properties that far away. */
573
574 #define TEXT_PROP_DISTANCE_LIMIT 100
575
576 #if GLYPH_DEBUG
577
578 /* Variables to turn off display optimizations from Lisp. */
579
580 int inhibit_try_window_id, inhibit_try_window_reusing;
581 int inhibit_try_cursor_movement;
582
583 /* Non-zero means print traces of redisplay if compiled with
584 GLYPH_DEBUG != 0. */
585
586 int trace_redisplay_p;
587
588 #endif /* GLYPH_DEBUG */
589
590 #ifdef DEBUG_TRACE_MOVE
591 /* Non-zero means trace with TRACE_MOVE to stderr. */
592 int trace_move;
593
594 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
595 #else
596 #define TRACE_MOVE(x) (void) 0
597 #endif
598
599 /* Non-zero means automatically scroll windows horizontally to make
600 point visible. */
601
602 int automatic_hscrolling_p;
603
604 /* How close to the margin can point get before the window is scrolled
605 horizontally. */
606 EMACS_INT hscroll_margin;
607
608 /* How much to scroll horizontally when point is inside the above margin. */
609 Lisp_Object Vhscroll_step;
610
611 /* A list of symbols, one for each supported image type. */
612
613 Lisp_Object Vimage_types;
614
615 /* The variable `resize-mini-windows'. If nil, don't resize
616 mini-windows. If t, always resize them to fit the text they
617 display. If `grow-only', let mini-windows grow only until they
618 become empty. */
619
620 Lisp_Object Vresize_mini_windows;
621
622 /* Buffer being redisplayed -- for redisplay_window_error. */
623
624 struct buffer *displayed_buffer;
625
626 /* Value returned from text property handlers (see below). */
627
628 enum prop_handled
629 {
630 HANDLED_NORMALLY,
631 HANDLED_RECOMPUTE_PROPS,
632 HANDLED_OVERLAY_STRING_CONSUMED,
633 HANDLED_RETURN
634 };
635
636 /* A description of text properties that redisplay is interested
637 in. */
638
639 struct props
640 {
641 /* The name of the property. */
642 Lisp_Object *name;
643
644 /* A unique index for the property. */
645 enum prop_idx idx;
646
647 /* A handler function called to set up iterator IT from the property
648 at IT's current position. Value is used to steer handle_stop. */
649 enum prop_handled (*handler) P_ ((struct it *it));
650 };
651
652 static enum prop_handled handle_face_prop P_ ((struct it *));
653 static enum prop_handled handle_invisible_prop P_ ((struct it *));
654 static enum prop_handled handle_display_prop P_ ((struct it *));
655 static enum prop_handled handle_composition_prop P_ ((struct it *));
656 static enum prop_handled handle_overlay_change P_ ((struct it *));
657 static enum prop_handled handle_fontified_prop P_ ((struct it *));
658
659 /* Properties handled by iterators. */
660
661 static struct props it_props[] =
662 {
663 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
664 /* Handle `face' before `display' because some sub-properties of
665 `display' need to know the face. */
666 {&Qface, FACE_PROP_IDX, handle_face_prop},
667 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
668 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
669 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
670 {NULL, 0, NULL}
671 };
672
673 /* Value is the position described by X. If X is a marker, value is
674 the marker_position of X. Otherwise, value is X. */
675
676 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
677
678 /* Enumeration returned by some move_it_.* functions internally. */
679
680 enum move_it_result
681 {
682 /* Not used. Undefined value. */
683 MOVE_UNDEFINED,
684
685 /* Move ended at the requested buffer position or ZV. */
686 MOVE_POS_MATCH_OR_ZV,
687
688 /* Move ended at the requested X pixel position. */
689 MOVE_X_REACHED,
690
691 /* Move within a line ended at the end of a line that must be
692 continued. */
693 MOVE_LINE_CONTINUED,
694
695 /* Move within a line ended at the end of a line that would
696 be displayed truncated. */
697 MOVE_LINE_TRUNCATED,
698
699 /* Move within a line ended at a line end. */
700 MOVE_NEWLINE_OR_CR
701 };
702
703 /* This counter is used to clear the face cache every once in a while
704 in redisplay_internal. It is incremented for each redisplay.
705 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
706 cleared. */
707
708 #define CLEAR_FACE_CACHE_COUNT 500
709 static int clear_face_cache_count;
710
711 /* Record the previous terminal frame we displayed. */
712
713 static struct frame *previous_terminal_frame;
714
715 /* Non-zero while redisplay_internal is in progress. */
716
717 int redisplaying_p;
718
719 /* Non-zero means don't free realized faces. Bound while freeing
720 realized faces is dangerous because glyph matrices might still
721 reference them. */
722
723 int inhibit_free_realized_faces;
724 Lisp_Object Qinhibit_free_realized_faces;
725
726 \f
727 /* Function prototypes. */
728
729 static void setup_for_ellipsis P_ ((struct it *));
730 static void mark_window_display_accurate_1 P_ ((struct window *, int));
731 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
732 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
733 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
734 static int redisplay_mode_lines P_ ((Lisp_Object, int));
735 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
736
737 #if 0
738 static int invisible_text_between_p P_ ((struct it *, int, int));
739 #endif
740
741 static int next_element_from_ellipsis P_ ((struct it *));
742 static void pint2str P_ ((char *, int, int));
743 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
744 struct text_pos));
745 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
746 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
747 static void store_frame_title_char P_ ((char));
748 static int store_frame_title P_ ((const unsigned char *, int, int));
749 static void x_consider_frame_title P_ ((Lisp_Object));
750 static void handle_stop P_ ((struct it *));
751 static int tool_bar_lines_needed P_ ((struct frame *));
752 static int single_display_prop_intangible_p P_ ((Lisp_Object));
753 static void ensure_echo_area_buffers P_ ((void));
754 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
755 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
756 static int with_echo_area_buffer P_ ((struct window *, int,
757 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
758 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
759 static void clear_garbaged_frames P_ ((void));
760 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
761 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
762 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
763 static int display_echo_area P_ ((struct window *));
764 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
765 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
766 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
767 static int string_char_and_length P_ ((const unsigned char *, int, int *));
768 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
769 struct text_pos));
770 static int compute_window_start_on_continuation_line P_ ((struct window *));
771 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
772 static void insert_left_trunc_glyphs P_ ((struct it *));
773 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
774 static void extend_face_to_end_of_line P_ ((struct it *));
775 static int append_space P_ ((struct it *, int));
776 static int make_cursor_line_fully_visible P_ ((struct window *));
777 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
778 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
779 static int trailing_whitespace_p P_ ((int));
780 static int message_log_check_duplicate P_ ((int, int, int, int));
781 static void push_it P_ ((struct it *));
782 static void pop_it P_ ((struct it *));
783 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
784 static void redisplay_internal P_ ((int));
785 static int echo_area_display P_ ((int));
786 static void redisplay_windows P_ ((Lisp_Object));
787 static void redisplay_window P_ ((Lisp_Object, int));
788 static Lisp_Object redisplay_window_error ();
789 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
790 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
791 static void update_menu_bar P_ ((struct frame *, int));
792 static int try_window_reusing_current_matrix P_ ((struct window *));
793 static int try_window_id P_ ((struct window *));
794 static int display_line P_ ((struct it *));
795 static int display_mode_lines P_ ((struct window *));
796 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
797 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
798 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
799 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
800 static void display_menu_bar P_ ((struct window *));
801 static int display_count_lines P_ ((int, int, int, int, int *));
802 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
803 int, int, struct it *, int, int, int, int));
804 static void compute_line_metrics P_ ((struct it *));
805 static void run_redisplay_end_trigger_hook P_ ((struct it *));
806 static int get_overlay_strings P_ ((struct it *, int));
807 static void next_overlay_string P_ ((struct it *));
808 static void reseat P_ ((struct it *, struct text_pos, int));
809 static void reseat_1 P_ ((struct it *, struct text_pos, int));
810 static void back_to_previous_visible_line_start P_ ((struct it *));
811 static void reseat_at_previous_visible_line_start P_ ((struct it *));
812 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
813 static int next_element_from_display_vector P_ ((struct it *));
814 static int next_element_from_string P_ ((struct it *));
815 static int next_element_from_c_string P_ ((struct it *));
816 static int next_element_from_buffer P_ ((struct it *));
817 static int next_element_from_composition P_ ((struct it *));
818 static int next_element_from_image P_ ((struct it *));
819 static int next_element_from_stretch P_ ((struct it *));
820 static void load_overlay_strings P_ ((struct it *, int));
821 static int init_from_display_pos P_ ((struct it *, struct window *,
822 struct display_pos *));
823 static void reseat_to_string P_ ((struct it *, unsigned char *,
824 Lisp_Object, int, int, int, int));
825 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
826 int, int, int));
827 void move_it_vertically_backward P_ ((struct it *, int));
828 static void init_to_row_start P_ ((struct it *, struct window *,
829 struct glyph_row *));
830 static int init_to_row_end P_ ((struct it *, struct window *,
831 struct glyph_row *));
832 static void back_to_previous_line_start P_ ((struct it *));
833 static int forward_to_next_line_start P_ ((struct it *, int *));
834 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
835 Lisp_Object, int));
836 static struct text_pos string_pos P_ ((int, Lisp_Object));
837 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
838 static int number_of_chars P_ ((unsigned char *, int));
839 static void compute_stop_pos P_ ((struct it *));
840 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
841 Lisp_Object));
842 static int face_before_or_after_it_pos P_ ((struct it *, int));
843 static int next_overlay_change P_ ((int));
844 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
845 Lisp_Object, struct text_pos *,
846 int));
847 static int underlying_face_id P_ ((struct it *));
848 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
849 struct window *));
850
851 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
852 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
853
854 #ifdef HAVE_WINDOW_SYSTEM
855
856 static void update_tool_bar P_ ((struct frame *, int));
857 static void build_desired_tool_bar_string P_ ((struct frame *f));
858 static int redisplay_tool_bar P_ ((struct frame *));
859 static void display_tool_bar_line P_ ((struct it *));
860
861 #endif /* HAVE_WINDOW_SYSTEM */
862
863 \f
864 /***********************************************************************
865 Window display dimensions
866 ***********************************************************************/
867
868 /* Return the bottom boundary y-position for text lines in window W.
869 This is the first y position at which a line cannot start.
870 It is relative to the top of the window.
871
872 This is the height of W minus the height of a mode line, if any. */
873
874 INLINE int
875 window_text_bottom_y (w)
876 struct window *w;
877 {
878 struct frame *f = XFRAME (w->frame);
879 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
880
881 if (WINDOW_WANTS_MODELINE_P (w))
882 height -= CURRENT_MODE_LINE_HEIGHT (w);
883 return height;
884 }
885
886
887 /* Return the pixel width of display area AREA of window W. AREA < 0
888 means return the total width of W, not including fringes to
889 the left and right of the window. */
890
891 INLINE int
892 window_box_width (w, area)
893 struct window *w;
894 int area;
895 {
896 struct frame *f = XFRAME (w->frame);
897 int width = XFASTINT (w->width);
898
899 if (!w->pseudo_window_p)
900 {
901 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
902
903 if (area == TEXT_AREA)
904 {
905 if (INTEGERP (w->left_margin_width))
906 width -= XFASTINT (w->left_margin_width);
907 if (INTEGERP (w->right_margin_width))
908 width -= XFASTINT (w->right_margin_width);
909 }
910 else if (area == LEFT_MARGIN_AREA)
911 width = (INTEGERP (w->left_margin_width)
912 ? XFASTINT (w->left_margin_width) : 0);
913 else if (area == RIGHT_MARGIN_AREA)
914 width = (INTEGERP (w->right_margin_width)
915 ? XFASTINT (w->right_margin_width) : 0);
916 }
917
918 return width * CANON_X_UNIT (f);
919 }
920
921
922 /* Return the pixel height of the display area of window W, not
923 including mode lines of W, if any. */
924
925 INLINE int
926 window_box_height (w)
927 struct window *w;
928 {
929 struct frame *f = XFRAME (w->frame);
930 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
931
932 xassert (height >= 0);
933
934 /* Note: the code below that determines the mode-line/header-line
935 height is essentially the same as that contained in the macro
936 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
937 the appropriate glyph row has its `mode_line_p' flag set,
938 and if it doesn't, uses estimate_mode_line_height instead. */
939
940 if (WINDOW_WANTS_MODELINE_P (w))
941 {
942 struct glyph_row *ml_row
943 = (w->current_matrix && w->current_matrix->rows
944 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
945 : 0);
946 if (ml_row && ml_row->mode_line_p)
947 height -= ml_row->height;
948 else
949 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
950 }
951
952 if (WINDOW_WANTS_HEADER_LINE_P (w))
953 {
954 struct glyph_row *hl_row
955 = (w->current_matrix && w->current_matrix->rows
956 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
957 : 0);
958 if (hl_row && hl_row->mode_line_p)
959 height -= hl_row->height;
960 else
961 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
962 }
963
964 /* With a very small font and a mode-line that's taller than
965 default, we might end up with a negative height. */
966 return max (0, height);
967 }
968
969
970 /* Return the frame-relative coordinate of the left edge of display
971 area AREA of window W. AREA < 0 means return the left edge of the
972 whole window, to the right of the left fringe of W. */
973
974 INLINE int
975 window_box_left (w, area)
976 struct window *w;
977 int area;
978 {
979 struct frame *f = XFRAME (w->frame);
980 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
981
982 if (!w->pseudo_window_p)
983 {
984 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
985 + FRAME_LEFT_FRINGE_WIDTH (f));
986
987 if (area == TEXT_AREA)
988 x += window_box_width (w, LEFT_MARGIN_AREA);
989 else if (area == RIGHT_MARGIN_AREA)
990 x += (window_box_width (w, LEFT_MARGIN_AREA)
991 + window_box_width (w, TEXT_AREA));
992 }
993
994 return x;
995 }
996
997
998 /* Return the frame-relative coordinate of the right edge of display
999 area AREA of window W. AREA < 0 means return the left edge of the
1000 whole window, to the left of the right fringe of W. */
1001
1002 INLINE int
1003 window_box_right (w, area)
1004 struct window *w;
1005 int area;
1006 {
1007 return window_box_left (w, area) + window_box_width (w, area);
1008 }
1009
1010
1011 /* Get the bounding box of the display area AREA of window W, without
1012 mode lines, in frame-relative coordinates. AREA < 0 means the
1013 whole window, not including the left and right fringes of
1014 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1015 coordinates of the upper-left corner of the box. Return in
1016 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1017
1018 INLINE void
1019 window_box (w, area, box_x, box_y, box_width, box_height)
1020 struct window *w;
1021 int area;
1022 int *box_x, *box_y, *box_width, *box_height;
1023 {
1024 struct frame *f = XFRAME (w->frame);
1025
1026 *box_width = window_box_width (w, area);
1027 *box_height = window_box_height (w);
1028 *box_x = window_box_left (w, area);
1029 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1030 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1031 if (WINDOW_WANTS_HEADER_LINE_P (w))
1032 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1033 }
1034
1035
1036 /* Get the bounding box of the display area AREA of window W, without
1037 mode lines. AREA < 0 means the whole window, not including the
1038 left and right fringe of the window. Return in *TOP_LEFT_X
1039 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1040 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1041 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1042 box. */
1043
1044 INLINE void
1045 window_box_edges (w, area, top_left_x, top_left_y,
1046 bottom_right_x, bottom_right_y)
1047 struct window *w;
1048 int area;
1049 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1050 {
1051 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1052 bottom_right_y);
1053 *bottom_right_x += *top_left_x;
1054 *bottom_right_y += *top_left_y;
1055 }
1056
1057
1058 \f
1059 /***********************************************************************
1060 Utilities
1061 ***********************************************************************/
1062
1063 /* Return the bottom y-position of the line the iterator IT is in.
1064 This can modify IT's settings. */
1065
1066 int
1067 line_bottom_y (it)
1068 struct it *it;
1069 {
1070 int line_height = it->max_ascent + it->max_descent;
1071 int line_top_y = it->current_y;
1072
1073 if (line_height == 0)
1074 {
1075 if (last_height)
1076 line_height = last_height;
1077 else if (IT_CHARPOS (*it) < ZV)
1078 {
1079 move_it_by_lines (it, 1, 1);
1080 line_height = (it->max_ascent || it->max_descent
1081 ? it->max_ascent + it->max_descent
1082 : last_height);
1083 }
1084 else
1085 {
1086 struct glyph_row *row = it->glyph_row;
1087
1088 /* Use the default character height. */
1089 it->glyph_row = NULL;
1090 it->what = IT_CHARACTER;
1091 it->c = ' ';
1092 it->len = 1;
1093 PRODUCE_GLYPHS (it);
1094 line_height = it->ascent + it->descent;
1095 it->glyph_row = row;
1096 }
1097 }
1098
1099 return line_top_y + line_height;
1100 }
1101
1102
1103 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1104 1 if POS is visible and the line containing POS is fully visible.
1105 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1106 and header-lines heights. */
1107
1108 int
1109 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1110 struct window *w;
1111 int charpos, *fully, exact_mode_line_heights_p;
1112 {
1113 struct it it;
1114 struct text_pos top;
1115 int visible_p;
1116 struct buffer *old_buffer = NULL;
1117
1118 if (XBUFFER (w->buffer) != current_buffer)
1119 {
1120 old_buffer = current_buffer;
1121 set_buffer_internal_1 (XBUFFER (w->buffer));
1122 }
1123
1124 *fully = visible_p = 0;
1125 SET_TEXT_POS_FROM_MARKER (top, w->start);
1126
1127 /* Compute exact mode line heights, if requested. */
1128 if (exact_mode_line_heights_p)
1129 {
1130 if (WINDOW_WANTS_MODELINE_P (w))
1131 current_mode_line_height
1132 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1133 current_buffer->mode_line_format);
1134
1135 if (WINDOW_WANTS_HEADER_LINE_P (w))
1136 current_header_line_height
1137 = display_mode_line (w, HEADER_LINE_FACE_ID,
1138 current_buffer->header_line_format);
1139 }
1140
1141 start_display (&it, w, top);
1142 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1143 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1144
1145 /* Note that we may overshoot because of invisible text. */
1146 if (IT_CHARPOS (it) >= charpos)
1147 {
1148 int top_y = it.current_y;
1149 int bottom_y = line_bottom_y (&it);
1150 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1151
1152 if (top_y < window_top_y)
1153 visible_p = bottom_y > window_top_y;
1154 else if (top_y < it.last_visible_y)
1155 {
1156 visible_p = 1;
1157 *fully = bottom_y <= it.last_visible_y;
1158 }
1159 }
1160 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1161 {
1162 move_it_by_lines (&it, 1, 0);
1163 if (charpos < IT_CHARPOS (it))
1164 {
1165 visible_p = 1;
1166 *fully = 0;
1167 }
1168 }
1169
1170 if (old_buffer)
1171 set_buffer_internal_1 (old_buffer);
1172
1173 current_header_line_height = current_mode_line_height = -1;
1174 return visible_p;
1175 }
1176
1177
1178 /* Return the next character from STR which is MAXLEN bytes long.
1179 Return in *LEN the length of the character. This is like
1180 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1181 we find one, we return a `?', but with the length of the invalid
1182 character. */
1183
1184 static INLINE int
1185 string_char_and_length (str, maxlen, len)
1186 const unsigned char *str;
1187 int maxlen, *len;
1188 {
1189 int c;
1190
1191 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1192 if (!CHAR_VALID_P (c, 1))
1193 /* We may not change the length here because other places in Emacs
1194 don't use this function, i.e. they silently accept invalid
1195 characters. */
1196 c = '?';
1197
1198 return c;
1199 }
1200
1201
1202
1203 /* Given a position POS containing a valid character and byte position
1204 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1205
1206 static struct text_pos
1207 string_pos_nchars_ahead (pos, string, nchars)
1208 struct text_pos pos;
1209 Lisp_Object string;
1210 int nchars;
1211 {
1212 xassert (STRINGP (string) && nchars >= 0);
1213
1214 if (STRING_MULTIBYTE (string))
1215 {
1216 int rest = SBYTES (string) - BYTEPOS (pos);
1217 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1218 int len;
1219
1220 while (nchars--)
1221 {
1222 string_char_and_length (p, rest, &len);
1223 p += len, rest -= len;
1224 xassert (rest >= 0);
1225 CHARPOS (pos) += 1;
1226 BYTEPOS (pos) += len;
1227 }
1228 }
1229 else
1230 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1231
1232 return pos;
1233 }
1234
1235
1236 /* Value is the text position, i.e. character and byte position,
1237 for character position CHARPOS in STRING. */
1238
1239 static INLINE struct text_pos
1240 string_pos (charpos, string)
1241 int charpos;
1242 Lisp_Object string;
1243 {
1244 struct text_pos pos;
1245 xassert (STRINGP (string));
1246 xassert (charpos >= 0);
1247 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1248 return pos;
1249 }
1250
1251
1252 /* Value is a text position, i.e. character and byte position, for
1253 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1254 means recognize multibyte characters. */
1255
1256 static struct text_pos
1257 c_string_pos (charpos, s, multibyte_p)
1258 int charpos;
1259 unsigned char *s;
1260 int multibyte_p;
1261 {
1262 struct text_pos pos;
1263
1264 xassert (s != NULL);
1265 xassert (charpos >= 0);
1266
1267 if (multibyte_p)
1268 {
1269 int rest = strlen (s), len;
1270
1271 SET_TEXT_POS (pos, 0, 0);
1272 while (charpos--)
1273 {
1274 string_char_and_length (s, rest, &len);
1275 s += len, rest -= len;
1276 xassert (rest >= 0);
1277 CHARPOS (pos) += 1;
1278 BYTEPOS (pos) += len;
1279 }
1280 }
1281 else
1282 SET_TEXT_POS (pos, charpos, charpos);
1283
1284 return pos;
1285 }
1286
1287
1288 /* Value is the number of characters in C string S. MULTIBYTE_P
1289 non-zero means recognize multibyte characters. */
1290
1291 static int
1292 number_of_chars (s, multibyte_p)
1293 unsigned char *s;
1294 int multibyte_p;
1295 {
1296 int nchars;
1297
1298 if (multibyte_p)
1299 {
1300 int rest = strlen (s), len;
1301 unsigned char *p = (unsigned char *) s;
1302
1303 for (nchars = 0; rest > 0; ++nchars)
1304 {
1305 string_char_and_length (p, rest, &len);
1306 rest -= len, p += len;
1307 }
1308 }
1309 else
1310 nchars = strlen (s);
1311
1312 return nchars;
1313 }
1314
1315
1316 /* Compute byte position NEWPOS->bytepos corresponding to
1317 NEWPOS->charpos. POS is a known position in string STRING.
1318 NEWPOS->charpos must be >= POS.charpos. */
1319
1320 static void
1321 compute_string_pos (newpos, pos, string)
1322 struct text_pos *newpos, pos;
1323 Lisp_Object string;
1324 {
1325 xassert (STRINGP (string));
1326 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1327
1328 if (STRING_MULTIBYTE (string))
1329 *newpos = string_pos_nchars_ahead (pos, string,
1330 CHARPOS (*newpos) - CHARPOS (pos));
1331 else
1332 BYTEPOS (*newpos) = CHARPOS (*newpos);
1333 }
1334
1335
1336 \f
1337 /***********************************************************************
1338 Lisp form evaluation
1339 ***********************************************************************/
1340
1341 /* Error handler for safe_eval and safe_call. */
1342
1343 static Lisp_Object
1344 safe_eval_handler (arg)
1345 Lisp_Object arg;
1346 {
1347 add_to_log ("Error during redisplay: %s", arg, Qnil);
1348 return Qnil;
1349 }
1350
1351
1352 /* Evaluate SEXPR and return the result, or nil if something went
1353 wrong. Prevent redisplay during the evaluation. */
1354
1355 Lisp_Object
1356 safe_eval (sexpr)
1357 Lisp_Object sexpr;
1358 {
1359 Lisp_Object val;
1360
1361 if (inhibit_eval_during_redisplay)
1362 val = Qnil;
1363 else
1364 {
1365 int count = SPECPDL_INDEX ();
1366 struct gcpro gcpro1;
1367
1368 GCPRO1 (sexpr);
1369 specbind (Qinhibit_redisplay, Qt);
1370 /* Use Qt to ensure debugger does not run,
1371 so there is no possibility of wanting to redisplay. */
1372 val = internal_condition_case_1 (Feval, sexpr, Qt,
1373 safe_eval_handler);
1374 UNGCPRO;
1375 val = unbind_to (count, val);
1376 }
1377
1378 return val;
1379 }
1380
1381
1382 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1383 Return the result, or nil if something went wrong. Prevent
1384 redisplay during the evaluation. */
1385
1386 Lisp_Object
1387 safe_call (nargs, args)
1388 int nargs;
1389 Lisp_Object *args;
1390 {
1391 Lisp_Object val;
1392
1393 if (inhibit_eval_during_redisplay)
1394 val = Qnil;
1395 else
1396 {
1397 int count = SPECPDL_INDEX ();
1398 struct gcpro gcpro1;
1399
1400 GCPRO1 (args[0]);
1401 gcpro1.nvars = nargs;
1402 specbind (Qinhibit_redisplay, Qt);
1403 /* Use Qt to ensure debugger does not run,
1404 so there is no possibility of wanting to redisplay. */
1405 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1406 safe_eval_handler);
1407 UNGCPRO;
1408 val = unbind_to (count, val);
1409 }
1410
1411 return val;
1412 }
1413
1414
1415 /* Call function FN with one argument ARG.
1416 Return the result, or nil if something went wrong. */
1417
1418 Lisp_Object
1419 safe_call1 (fn, arg)
1420 Lisp_Object fn, arg;
1421 {
1422 Lisp_Object args[2];
1423 args[0] = fn;
1424 args[1] = arg;
1425 return safe_call (2, args);
1426 }
1427
1428
1429 \f
1430 /***********************************************************************
1431 Debugging
1432 ***********************************************************************/
1433
1434 #if 0
1435
1436 /* Define CHECK_IT to perform sanity checks on iterators.
1437 This is for debugging. It is too slow to do unconditionally. */
1438
1439 static void
1440 check_it (it)
1441 struct it *it;
1442 {
1443 if (it->method == next_element_from_string)
1444 {
1445 xassert (STRINGP (it->string));
1446 xassert (IT_STRING_CHARPOS (*it) >= 0);
1447 }
1448 else if (it->method == next_element_from_buffer)
1449 {
1450 /* Check that character and byte positions agree. */
1451 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1452 }
1453
1454 if (it->dpvec)
1455 xassert (it->current.dpvec_index >= 0);
1456 else
1457 xassert (it->current.dpvec_index < 0);
1458 }
1459
1460 #define CHECK_IT(IT) check_it ((IT))
1461
1462 #else /* not 0 */
1463
1464 #define CHECK_IT(IT) (void) 0
1465
1466 #endif /* not 0 */
1467
1468
1469 #if GLYPH_DEBUG
1470
1471 /* Check that the window end of window W is what we expect it
1472 to be---the last row in the current matrix displaying text. */
1473
1474 static void
1475 check_window_end (w)
1476 struct window *w;
1477 {
1478 if (!MINI_WINDOW_P (w)
1479 && !NILP (w->window_end_valid))
1480 {
1481 struct glyph_row *row;
1482 xassert ((row = MATRIX_ROW (w->current_matrix,
1483 XFASTINT (w->window_end_vpos)),
1484 !row->enabled_p
1485 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1486 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1487 }
1488 }
1489
1490 #define CHECK_WINDOW_END(W) check_window_end ((W))
1491
1492 #else /* not GLYPH_DEBUG */
1493
1494 #define CHECK_WINDOW_END(W) (void) 0
1495
1496 #endif /* not GLYPH_DEBUG */
1497
1498
1499 \f
1500 /***********************************************************************
1501 Iterator initialization
1502 ***********************************************************************/
1503
1504 /* Initialize IT for displaying current_buffer in window W, starting
1505 at character position CHARPOS. CHARPOS < 0 means that no buffer
1506 position is specified which is useful when the iterator is assigned
1507 a position later. BYTEPOS is the byte position corresponding to
1508 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1509
1510 If ROW is not null, calls to produce_glyphs with IT as parameter
1511 will produce glyphs in that row.
1512
1513 BASE_FACE_ID is the id of a base face to use. It must be one of
1514 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1515 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1516 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1517
1518 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1520 will be initialized to use the corresponding mode line glyph row of
1521 the desired matrix of W. */
1522
1523 void
1524 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1525 struct it *it;
1526 struct window *w;
1527 int charpos, bytepos;
1528 struct glyph_row *row;
1529 enum face_id base_face_id;
1530 {
1531 int highlight_region_p;
1532
1533 /* Some precondition checks. */
1534 xassert (w != NULL && it != NULL);
1535 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1536 && charpos <= ZV));
1537
1538 /* If face attributes have been changed since the last redisplay,
1539 free realized faces now because they depend on face definitions
1540 that might have changed. Don't free faces while there might be
1541 desired matrices pending which reference these faces. */
1542 if (face_change_count && !inhibit_free_realized_faces)
1543 {
1544 face_change_count = 0;
1545 free_all_realized_faces (Qnil);
1546 }
1547
1548 /* Use one of the mode line rows of W's desired matrix if
1549 appropriate. */
1550 if (row == NULL)
1551 {
1552 if (base_face_id == MODE_LINE_FACE_ID
1553 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1554 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1555 else if (base_face_id == HEADER_LINE_FACE_ID)
1556 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1557 }
1558
1559 /* Clear IT. */
1560 bzero (it, sizeof *it);
1561 it->current.overlay_string_index = -1;
1562 it->current.dpvec_index = -1;
1563 it->base_face_id = base_face_id;
1564
1565 /* The window in which we iterate over current_buffer: */
1566 XSETWINDOW (it->window, w);
1567 it->w = w;
1568 it->f = XFRAME (w->frame);
1569
1570 /* Extra space between lines (on window systems only). */
1571 if (base_face_id == DEFAULT_FACE_ID
1572 && FRAME_WINDOW_P (it->f))
1573 {
1574 if (NATNUMP (current_buffer->extra_line_spacing))
1575 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1576 else if (it->f->extra_line_spacing > 0)
1577 it->extra_line_spacing = it->f->extra_line_spacing;
1578 }
1579
1580 /* If realized faces have been removed, e.g. because of face
1581 attribute changes of named faces, recompute them. When running
1582 in batch mode, the face cache of Vterminal_frame is null. If
1583 we happen to get called, make a dummy face cache. */
1584 if (
1585 #ifndef WINDOWSNT
1586 noninteractive &&
1587 #endif
1588 FRAME_FACE_CACHE (it->f) == NULL)
1589 init_frame_faces (it->f);
1590 if (FRAME_FACE_CACHE (it->f)->used == 0)
1591 recompute_basic_faces (it->f);
1592
1593 /* Current value of the `space-width', and 'height' properties. */
1594 it->space_width = Qnil;
1595 it->font_height = Qnil;
1596
1597 /* Are control characters displayed as `^C'? */
1598 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1599
1600 /* -1 means everything between a CR and the following line end
1601 is invisible. >0 means lines indented more than this value are
1602 invisible. */
1603 it->selective = (INTEGERP (current_buffer->selective_display)
1604 ? XFASTINT (current_buffer->selective_display)
1605 : (!NILP (current_buffer->selective_display)
1606 ? -1 : 0));
1607 it->selective_display_ellipsis_p
1608 = !NILP (current_buffer->selective_display_ellipses);
1609
1610 /* Display table to use. */
1611 it->dp = window_display_table (w);
1612
1613 /* Are multibyte characters enabled in current_buffer? */
1614 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1615
1616 /* Non-zero if we should highlight the region. */
1617 highlight_region_p
1618 = (!NILP (Vtransient_mark_mode)
1619 && !NILP (current_buffer->mark_active)
1620 && XMARKER (current_buffer->mark)->buffer != 0);
1621
1622 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1623 start and end of a visible region in window IT->w. Set both to
1624 -1 to indicate no region. */
1625 if (highlight_region_p
1626 /* Maybe highlight only in selected window. */
1627 && (/* Either show region everywhere. */
1628 highlight_nonselected_windows
1629 /* Or show region in the selected window. */
1630 || w == XWINDOW (selected_window)
1631 /* Or show the region if we are in the mini-buffer and W is
1632 the window the mini-buffer refers to. */
1633 || (MINI_WINDOW_P (XWINDOW (selected_window))
1634 && WINDOWP (minibuf_selected_window)
1635 && w == XWINDOW (minibuf_selected_window))))
1636 {
1637 int charpos = marker_position (current_buffer->mark);
1638 it->region_beg_charpos = min (PT, charpos);
1639 it->region_end_charpos = max (PT, charpos);
1640 }
1641 else
1642 it->region_beg_charpos = it->region_end_charpos = -1;
1643
1644 /* Get the position at which the redisplay_end_trigger hook should
1645 be run, if it is to be run at all. */
1646 if (MARKERP (w->redisplay_end_trigger)
1647 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1648 it->redisplay_end_trigger_charpos
1649 = marker_position (w->redisplay_end_trigger);
1650 else if (INTEGERP (w->redisplay_end_trigger))
1651 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1652
1653 /* Correct bogus values of tab_width. */
1654 it->tab_width = XINT (current_buffer->tab_width);
1655 if (it->tab_width <= 0 || it->tab_width > 1000)
1656 it->tab_width = 8;
1657
1658 /* Are lines in the display truncated? */
1659 it->truncate_lines_p
1660 = (base_face_id != DEFAULT_FACE_ID
1661 || XINT (it->w->hscroll)
1662 || (truncate_partial_width_windows
1663 && !WINDOW_FULL_WIDTH_P (it->w))
1664 || !NILP (current_buffer->truncate_lines));
1665
1666 /* Get dimensions of truncation and continuation glyphs. These are
1667 displayed as fringe bitmaps under X, so we don't need them for such
1668 frames. */
1669 if (!FRAME_WINDOW_P (it->f))
1670 {
1671 if (it->truncate_lines_p)
1672 {
1673 /* We will need the truncation glyph. */
1674 xassert (it->glyph_row == NULL);
1675 produce_special_glyphs (it, IT_TRUNCATION);
1676 it->truncation_pixel_width = it->pixel_width;
1677 }
1678 else
1679 {
1680 /* We will need the continuation glyph. */
1681 xassert (it->glyph_row == NULL);
1682 produce_special_glyphs (it, IT_CONTINUATION);
1683 it->continuation_pixel_width = it->pixel_width;
1684 }
1685
1686 /* Reset these values to zero because the produce_special_glyphs
1687 above has changed them. */
1688 it->pixel_width = it->ascent = it->descent = 0;
1689 it->phys_ascent = it->phys_descent = 0;
1690 }
1691
1692 /* Set this after getting the dimensions of truncation and
1693 continuation glyphs, so that we don't produce glyphs when calling
1694 produce_special_glyphs, above. */
1695 it->glyph_row = row;
1696 it->area = TEXT_AREA;
1697
1698 /* Get the dimensions of the display area. The display area
1699 consists of the visible window area plus a horizontally scrolled
1700 part to the left of the window. All x-values are relative to the
1701 start of this total display area. */
1702 if (base_face_id != DEFAULT_FACE_ID)
1703 {
1704 /* Mode lines, menu bar in terminal frames. */
1705 it->first_visible_x = 0;
1706 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1707 }
1708 else
1709 {
1710 it->first_visible_x
1711 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1712 it->last_visible_x = (it->first_visible_x
1713 + window_box_width (w, TEXT_AREA));
1714
1715 /* If we truncate lines, leave room for the truncator glyph(s) at
1716 the right margin. Otherwise, leave room for the continuation
1717 glyph(s). Truncation and continuation glyphs are not inserted
1718 for window-based redisplay. */
1719 if (!FRAME_WINDOW_P (it->f))
1720 {
1721 if (it->truncate_lines_p)
1722 it->last_visible_x -= it->truncation_pixel_width;
1723 else
1724 it->last_visible_x -= it->continuation_pixel_width;
1725 }
1726
1727 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1728 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
1729 }
1730
1731 /* Leave room for a border glyph. */
1732 if (!FRAME_WINDOW_P (it->f)
1733 && !WINDOW_RIGHTMOST_P (it->w))
1734 it->last_visible_x -= 1;
1735
1736 it->last_visible_y = window_text_bottom_y (w);
1737
1738 /* For mode lines and alike, arrange for the first glyph having a
1739 left box line if the face specifies a box. */
1740 if (base_face_id != DEFAULT_FACE_ID)
1741 {
1742 struct face *face;
1743
1744 it->face_id = base_face_id;
1745
1746 /* If we have a boxed mode line, make the first character appear
1747 with a left box line. */
1748 face = FACE_FROM_ID (it->f, base_face_id);
1749 if (face->box != FACE_NO_BOX)
1750 it->start_of_box_run_p = 1;
1751 }
1752
1753 /* If a buffer position was specified, set the iterator there,
1754 getting overlays and face properties from that position. */
1755 if (charpos >= BUF_BEG (current_buffer))
1756 {
1757 it->end_charpos = ZV;
1758 it->face_id = -1;
1759 IT_CHARPOS (*it) = charpos;
1760
1761 /* Compute byte position if not specified. */
1762 if (bytepos < charpos)
1763 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1764 else
1765 IT_BYTEPOS (*it) = bytepos;
1766
1767 /* Compute faces etc. */
1768 reseat (it, it->current.pos, 1);
1769 }
1770
1771 CHECK_IT (it);
1772 }
1773
1774
1775 /* Initialize IT for the display of window W with window start POS. */
1776
1777 void
1778 start_display (it, w, pos)
1779 struct it *it;
1780 struct window *w;
1781 struct text_pos pos;
1782 {
1783 struct glyph_row *row;
1784 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
1785
1786 row = w->desired_matrix->rows + first_vpos;
1787 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1788
1789 if (!it->truncate_lines_p)
1790 {
1791 int start_at_line_beg_p;
1792 int first_y = it->current_y;
1793
1794 /* If window start is not at a line start, skip forward to POS to
1795 get the correct continuation lines width. */
1796 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1797 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1798 if (!start_at_line_beg_p)
1799 {
1800 int new_x;
1801
1802 reseat_at_previous_visible_line_start (it);
1803 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1804
1805 new_x = it->current_x + it->pixel_width;
1806
1807 /* If lines are continued, this line may end in the middle
1808 of a multi-glyph character (e.g. a control character
1809 displayed as \003, or in the middle of an overlay
1810 string). In this case move_it_to above will not have
1811 taken us to the start of the continuation line but to the
1812 end of the continued line. */
1813 if (it->current_x > 0
1814 && !it->truncate_lines_p /* Lines are continued. */
1815 && (/* And glyph doesn't fit on the line. */
1816 new_x > it->last_visible_x
1817 /* Or it fits exactly and we're on a window
1818 system frame. */
1819 || (new_x == it->last_visible_x
1820 && FRAME_WINDOW_P (it->f))))
1821 {
1822 if (it->current.dpvec_index >= 0
1823 || it->current.overlay_string_index >= 0)
1824 {
1825 set_iterator_to_next (it, 1);
1826 move_it_in_display_line_to (it, -1, -1, 0);
1827 }
1828
1829 it->continuation_lines_width += it->current_x;
1830 }
1831
1832 /* We're starting a new display line, not affected by the
1833 height of the continued line, so clear the appropriate
1834 fields in the iterator structure. */
1835 it->max_ascent = it->max_descent = 0;
1836 it->max_phys_ascent = it->max_phys_descent = 0;
1837
1838 it->current_y = first_y;
1839 it->vpos = 0;
1840 it->current_x = it->hpos = 0;
1841 }
1842 }
1843
1844 #if 0 /* Don't assert the following because start_display is sometimes
1845 called intentionally with a window start that is not at a
1846 line start. Please leave this code in as a comment. */
1847
1848 /* Window start should be on a line start, now. */
1849 xassert (it->continuation_lines_width
1850 || IT_CHARPOS (it) == BEGV
1851 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1852 #endif /* 0 */
1853 }
1854
1855
1856 /* Return 1 if POS is a position in ellipses displayed for invisible
1857 text. W is the window we display, for text property lookup. */
1858
1859 static int
1860 in_ellipses_for_invisible_text_p (pos, w)
1861 struct display_pos *pos;
1862 struct window *w;
1863 {
1864 Lisp_Object prop, window;
1865 int ellipses_p = 0;
1866 int charpos = CHARPOS (pos->pos);
1867
1868 /* If POS specifies a position in a display vector, this might
1869 be for an ellipsis displayed for invisible text. We won't
1870 get the iterator set up for delivering that ellipsis unless
1871 we make sure that it gets aware of the invisible text. */
1872 if (pos->dpvec_index >= 0
1873 && pos->overlay_string_index < 0
1874 && CHARPOS (pos->string_pos) < 0
1875 && charpos > BEGV
1876 && (XSETWINDOW (window, w),
1877 prop = Fget_char_property (make_number (charpos),
1878 Qinvisible, window),
1879 !TEXT_PROP_MEANS_INVISIBLE (prop)))
1880 {
1881 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1882 window);
1883 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
1884 }
1885
1886 return ellipses_p;
1887 }
1888
1889
1890 /* Initialize IT for stepping through current_buffer in window W,
1891 starting at position POS that includes overlay string and display
1892 vector/ control character translation position information. Value
1893 is zero if there are overlay strings with newlines at POS. */
1894
1895 static int
1896 init_from_display_pos (it, w, pos)
1897 struct it *it;
1898 struct window *w;
1899 struct display_pos *pos;
1900 {
1901 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1902 int i, overlay_strings_with_newlines = 0;
1903
1904 /* If POS specifies a position in a display vector, this might
1905 be for an ellipsis displayed for invisible text. We won't
1906 get the iterator set up for delivering that ellipsis unless
1907 we make sure that it gets aware of the invisible text. */
1908 if (in_ellipses_for_invisible_text_p (pos, w))
1909 {
1910 --charpos;
1911 bytepos = 0;
1912 }
1913
1914 /* Keep in mind: the call to reseat in init_iterator skips invisible
1915 text, so we might end up at a position different from POS. This
1916 is only a problem when POS is a row start after a newline and an
1917 overlay starts there with an after-string, and the overlay has an
1918 invisible property. Since we don't skip invisible text in
1919 display_line and elsewhere immediately after consuming the
1920 newline before the row start, such a POS will not be in a string,
1921 but the call to init_iterator below will move us to the
1922 after-string. */
1923 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
1924
1925 for (i = 0; i < it->n_overlay_strings; ++i)
1926 {
1927 const char *s = SDATA (it->overlay_strings[i]);
1928 const char *e = s + SBYTES (it->overlay_strings[i]);
1929
1930 while (s < e && *s != '\n')
1931 ++s;
1932
1933 if (s < e)
1934 {
1935 overlay_strings_with_newlines = 1;
1936 break;
1937 }
1938 }
1939
1940 /* If position is within an overlay string, set up IT to the right
1941 overlay string. */
1942 if (pos->overlay_string_index >= 0)
1943 {
1944 int relative_index;
1945
1946 /* If the first overlay string happens to have a `display'
1947 property for an image, the iterator will be set up for that
1948 image, and we have to undo that setup first before we can
1949 correct the overlay string index. */
1950 if (it->method == next_element_from_image)
1951 pop_it (it);
1952
1953 /* We already have the first chunk of overlay strings in
1954 IT->overlay_strings. Load more until the one for
1955 pos->overlay_string_index is in IT->overlay_strings. */
1956 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1957 {
1958 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1959 it->current.overlay_string_index = 0;
1960 while (n--)
1961 {
1962 load_overlay_strings (it, 0);
1963 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1964 }
1965 }
1966
1967 it->current.overlay_string_index = pos->overlay_string_index;
1968 relative_index = (it->current.overlay_string_index
1969 % OVERLAY_STRING_CHUNK_SIZE);
1970 it->string = it->overlay_strings[relative_index];
1971 xassert (STRINGP (it->string));
1972 it->current.string_pos = pos->string_pos;
1973 it->method = next_element_from_string;
1974 }
1975
1976 #if 0 /* This is bogus because POS not having an overlay string
1977 position does not mean it's after the string. Example: A
1978 line starting with a before-string and initialization of IT
1979 to the previous row's end position. */
1980 else if (it->current.overlay_string_index >= 0)
1981 {
1982 /* If POS says we're already after an overlay string ending at
1983 POS, make sure to pop the iterator because it will be in
1984 front of that overlay string. When POS is ZV, we've thereby
1985 also ``processed'' overlay strings at ZV. */
1986 while (it->sp)
1987 pop_it (it);
1988 it->current.overlay_string_index = -1;
1989 it->method = next_element_from_buffer;
1990 if (CHARPOS (pos->pos) == ZV)
1991 it->overlay_strings_at_end_processed_p = 1;
1992 }
1993 #endif /* 0 */
1994
1995 if (CHARPOS (pos->string_pos) >= 0)
1996 {
1997 /* Recorded position is not in an overlay string, but in another
1998 string. This can only be a string from a `display' property.
1999 IT should already be filled with that string. */
2000 it->current.string_pos = pos->string_pos;
2001 xassert (STRINGP (it->string));
2002 }
2003
2004 /* Restore position in display vector translations, control
2005 character translations or ellipses. */
2006 if (pos->dpvec_index >= 0)
2007 {
2008 if (it->dpvec == NULL)
2009 get_next_display_element (it);
2010 xassert (it->dpvec && it->current.dpvec_index == 0);
2011 it->current.dpvec_index = pos->dpvec_index;
2012 }
2013
2014 CHECK_IT (it);
2015 return !overlay_strings_with_newlines;
2016 }
2017
2018
2019 /* Initialize IT for stepping through current_buffer in window W
2020 starting at ROW->start. */
2021
2022 static void
2023 init_to_row_start (it, w, row)
2024 struct it *it;
2025 struct window *w;
2026 struct glyph_row *row;
2027 {
2028 init_from_display_pos (it, w, &row->start);
2029 it->continuation_lines_width = row->continuation_lines_width;
2030 CHECK_IT (it);
2031 }
2032
2033
2034 /* Initialize IT for stepping through current_buffer in window W
2035 starting in the line following ROW, i.e. starting at ROW->end.
2036 Value is zero if there are overlay strings with newlines at ROW's
2037 end position. */
2038
2039 static int
2040 init_to_row_end (it, w, row)
2041 struct it *it;
2042 struct window *w;
2043 struct glyph_row *row;
2044 {
2045 int success = 0;
2046
2047 if (init_from_display_pos (it, w, &row->end))
2048 {
2049 if (row->continued_p)
2050 it->continuation_lines_width
2051 = row->continuation_lines_width + row->pixel_width;
2052 CHECK_IT (it);
2053 success = 1;
2054 }
2055
2056 return success;
2057 }
2058
2059
2060
2061 \f
2062 /***********************************************************************
2063 Text properties
2064 ***********************************************************************/
2065
2066 /* Called when IT reaches IT->stop_charpos. Handle text property and
2067 overlay changes. Set IT->stop_charpos to the next position where
2068 to stop. */
2069
2070 static void
2071 handle_stop (it)
2072 struct it *it;
2073 {
2074 enum prop_handled handled;
2075 int handle_overlay_change_p = 1;
2076 struct props *p;
2077
2078 it->dpvec = NULL;
2079 it->current.dpvec_index = -1;
2080
2081 do
2082 {
2083 handled = HANDLED_NORMALLY;
2084
2085 /* Call text property handlers. */
2086 for (p = it_props; p->handler; ++p)
2087 {
2088 handled = p->handler (it);
2089
2090 if (handled == HANDLED_RECOMPUTE_PROPS)
2091 break;
2092 else if (handled == HANDLED_RETURN)
2093 return;
2094 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2095 handle_overlay_change_p = 0;
2096 }
2097
2098 if (handled != HANDLED_RECOMPUTE_PROPS)
2099 {
2100 /* Don't check for overlay strings below when set to deliver
2101 characters from a display vector. */
2102 if (it->method == next_element_from_display_vector)
2103 handle_overlay_change_p = 0;
2104
2105 /* Handle overlay changes. */
2106 if (handle_overlay_change_p)
2107 handled = handle_overlay_change (it);
2108
2109 /* Determine where to stop next. */
2110 if (handled == HANDLED_NORMALLY)
2111 compute_stop_pos (it);
2112 }
2113 }
2114 while (handled == HANDLED_RECOMPUTE_PROPS);
2115 }
2116
2117
2118 /* Compute IT->stop_charpos from text property and overlay change
2119 information for IT's current position. */
2120
2121 static void
2122 compute_stop_pos (it)
2123 struct it *it;
2124 {
2125 register INTERVAL iv, next_iv;
2126 Lisp_Object object, limit, position;
2127
2128 /* If nowhere else, stop at the end. */
2129 it->stop_charpos = it->end_charpos;
2130
2131 if (STRINGP (it->string))
2132 {
2133 /* Strings are usually short, so don't limit the search for
2134 properties. */
2135 object = it->string;
2136 limit = Qnil;
2137 position = make_number (IT_STRING_CHARPOS (*it));
2138 }
2139 else
2140 {
2141 int charpos;
2142
2143 /* If next overlay change is in front of the current stop pos
2144 (which is IT->end_charpos), stop there. Note: value of
2145 next_overlay_change is point-max if no overlay change
2146 follows. */
2147 charpos = next_overlay_change (IT_CHARPOS (*it));
2148 if (charpos < it->stop_charpos)
2149 it->stop_charpos = charpos;
2150
2151 /* If showing the region, we have to stop at the region
2152 start or end because the face might change there. */
2153 if (it->region_beg_charpos > 0)
2154 {
2155 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2156 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2157 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2158 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2159 }
2160
2161 /* Set up variables for computing the stop position from text
2162 property changes. */
2163 XSETBUFFER (object, current_buffer);
2164 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2165 position = make_number (IT_CHARPOS (*it));
2166
2167 }
2168
2169 /* Get the interval containing IT's position. Value is a null
2170 interval if there isn't such an interval. */
2171 iv = validate_interval_range (object, &position, &position, 0);
2172 if (!NULL_INTERVAL_P (iv))
2173 {
2174 Lisp_Object values_here[LAST_PROP_IDX];
2175 struct props *p;
2176
2177 /* Get properties here. */
2178 for (p = it_props; p->handler; ++p)
2179 values_here[p->idx] = textget (iv->plist, *p->name);
2180
2181 /* Look for an interval following iv that has different
2182 properties. */
2183 for (next_iv = next_interval (iv);
2184 (!NULL_INTERVAL_P (next_iv)
2185 && (NILP (limit)
2186 || XFASTINT (limit) > next_iv->position));
2187 next_iv = next_interval (next_iv))
2188 {
2189 for (p = it_props; p->handler; ++p)
2190 {
2191 Lisp_Object new_value;
2192
2193 new_value = textget (next_iv->plist, *p->name);
2194 if (!EQ (values_here[p->idx], new_value))
2195 break;
2196 }
2197
2198 if (p->handler)
2199 break;
2200 }
2201
2202 if (!NULL_INTERVAL_P (next_iv))
2203 {
2204 if (INTEGERP (limit)
2205 && next_iv->position >= XFASTINT (limit))
2206 /* No text property change up to limit. */
2207 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2208 else
2209 /* Text properties change in next_iv. */
2210 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2211 }
2212 }
2213
2214 xassert (STRINGP (it->string)
2215 || (it->stop_charpos >= BEGV
2216 && it->stop_charpos >= IT_CHARPOS (*it)));
2217 }
2218
2219
2220 /* Return the position of the next overlay change after POS in
2221 current_buffer. Value is point-max if no overlay change
2222 follows. This is like `next-overlay-change' but doesn't use
2223 xmalloc. */
2224
2225 static int
2226 next_overlay_change (pos)
2227 int pos;
2228 {
2229 int noverlays;
2230 int endpos;
2231 Lisp_Object *overlays;
2232 int len;
2233 int i;
2234
2235 /* Get all overlays at the given position. */
2236 len = 10;
2237 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2238 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2239 if (noverlays > len)
2240 {
2241 len = noverlays;
2242 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2243 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2244 }
2245
2246 /* If any of these overlays ends before endpos,
2247 use its ending point instead. */
2248 for (i = 0; i < noverlays; ++i)
2249 {
2250 Lisp_Object oend;
2251 int oendpos;
2252
2253 oend = OVERLAY_END (overlays[i]);
2254 oendpos = OVERLAY_POSITION (oend);
2255 endpos = min (endpos, oendpos);
2256 }
2257
2258 return endpos;
2259 }
2260
2261
2262 \f
2263 /***********************************************************************
2264 Fontification
2265 ***********************************************************************/
2266
2267 /* Handle changes in the `fontified' property of the current buffer by
2268 calling hook functions from Qfontification_functions to fontify
2269 regions of text. */
2270
2271 static enum prop_handled
2272 handle_fontified_prop (it)
2273 struct it *it;
2274 {
2275 Lisp_Object prop, pos;
2276 enum prop_handled handled = HANDLED_NORMALLY;
2277
2278 /* Get the value of the `fontified' property at IT's current buffer
2279 position. (The `fontified' property doesn't have a special
2280 meaning in strings.) If the value is nil, call functions from
2281 Qfontification_functions. */
2282 if (!STRINGP (it->string)
2283 && it->s == NULL
2284 && !NILP (Vfontification_functions)
2285 && !NILP (Vrun_hooks)
2286 && (pos = make_number (IT_CHARPOS (*it)),
2287 prop = Fget_char_property (pos, Qfontified, Qnil),
2288 NILP (prop)))
2289 {
2290 int count = SPECPDL_INDEX ();
2291 Lisp_Object val;
2292
2293 val = Vfontification_functions;
2294 specbind (Qfontification_functions, Qnil);
2295
2296 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2297 safe_call1 (val, pos);
2298 else
2299 {
2300 Lisp_Object globals, fn;
2301 struct gcpro gcpro1, gcpro2;
2302
2303 globals = Qnil;
2304 GCPRO2 (val, globals);
2305
2306 for (; CONSP (val); val = XCDR (val))
2307 {
2308 fn = XCAR (val);
2309
2310 if (EQ (fn, Qt))
2311 {
2312 /* A value of t indicates this hook has a local
2313 binding; it means to run the global binding too.
2314 In a global value, t should not occur. If it
2315 does, we must ignore it to avoid an endless
2316 loop. */
2317 for (globals = Fdefault_value (Qfontification_functions);
2318 CONSP (globals);
2319 globals = XCDR (globals))
2320 {
2321 fn = XCAR (globals);
2322 if (!EQ (fn, Qt))
2323 safe_call1 (fn, pos);
2324 }
2325 }
2326 else
2327 safe_call1 (fn, pos);
2328 }
2329
2330 UNGCPRO;
2331 }
2332
2333 unbind_to (count, Qnil);
2334
2335 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2336 something. This avoids an endless loop if they failed to
2337 fontify the text for which reason ever. */
2338 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2339 handled = HANDLED_RECOMPUTE_PROPS;
2340 }
2341
2342 return handled;
2343 }
2344
2345
2346 \f
2347 /***********************************************************************
2348 Faces
2349 ***********************************************************************/
2350
2351 /* Set up iterator IT from face properties at its current position.
2352 Called from handle_stop. */
2353
2354 static enum prop_handled
2355 handle_face_prop (it)
2356 struct it *it;
2357 {
2358 int new_face_id, next_stop;
2359
2360 if (!STRINGP (it->string))
2361 {
2362 new_face_id
2363 = face_at_buffer_position (it->w,
2364 IT_CHARPOS (*it),
2365 it->region_beg_charpos,
2366 it->region_end_charpos,
2367 &next_stop,
2368 (IT_CHARPOS (*it)
2369 + TEXT_PROP_DISTANCE_LIMIT),
2370 0);
2371
2372 /* Is this a start of a run of characters with box face?
2373 Caveat: this can be called for a freshly initialized
2374 iterator; face_id is -1 in this case. We know that the new
2375 face will not change until limit, i.e. if the new face has a
2376 box, all characters up to limit will have one. But, as
2377 usual, we don't know whether limit is really the end. */
2378 if (new_face_id != it->face_id)
2379 {
2380 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2381
2382 /* If new face has a box but old face has not, this is
2383 the start of a run of characters with box, i.e. it has
2384 a shadow on the left side. The value of face_id of the
2385 iterator will be -1 if this is the initial call that gets
2386 the face. In this case, we have to look in front of IT's
2387 position and see whether there is a face != new_face_id. */
2388 it->start_of_box_run_p
2389 = (new_face->box != FACE_NO_BOX
2390 && (it->face_id >= 0
2391 || IT_CHARPOS (*it) == BEG
2392 || new_face_id != face_before_it_pos (it)));
2393 it->face_box_p = new_face->box != FACE_NO_BOX;
2394 }
2395 }
2396 else
2397 {
2398 int base_face_id, bufpos;
2399
2400 if (it->current.overlay_string_index >= 0)
2401 bufpos = IT_CHARPOS (*it);
2402 else
2403 bufpos = 0;
2404
2405 /* For strings from a buffer, i.e. overlay strings or strings
2406 from a `display' property, use the face at IT's current
2407 buffer position as the base face to merge with, so that
2408 overlay strings appear in the same face as surrounding
2409 text, unless they specify their own faces. */
2410 base_face_id = underlying_face_id (it);
2411
2412 new_face_id = face_at_string_position (it->w,
2413 it->string,
2414 IT_STRING_CHARPOS (*it),
2415 bufpos,
2416 it->region_beg_charpos,
2417 it->region_end_charpos,
2418 &next_stop,
2419 base_face_id, 0);
2420
2421 #if 0 /* This shouldn't be neccessary. Let's check it. */
2422 /* If IT is used to display a mode line we would really like to
2423 use the mode line face instead of the frame's default face. */
2424 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2425 && new_face_id == DEFAULT_FACE_ID)
2426 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2427 #endif
2428
2429 /* Is this a start of a run of characters with box? Caveat:
2430 this can be called for a freshly allocated iterator; face_id
2431 is -1 is this case. We know that the new face will not
2432 change until the next check pos, i.e. if the new face has a
2433 box, all characters up to that position will have a
2434 box. But, as usual, we don't know whether that position
2435 is really the end. */
2436 if (new_face_id != it->face_id)
2437 {
2438 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2439 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2440
2441 /* If new face has a box but old face hasn't, this is the
2442 start of a run of characters with box, i.e. it has a
2443 shadow on the left side. */
2444 it->start_of_box_run_p
2445 = new_face->box && (old_face == NULL || !old_face->box);
2446 it->face_box_p = new_face->box != FACE_NO_BOX;
2447 }
2448 }
2449
2450 it->face_id = new_face_id;
2451 return HANDLED_NORMALLY;
2452 }
2453
2454
2455 /* Return the ID of the face ``underlying'' IT's current position,
2456 which is in a string. If the iterator is associated with a
2457 buffer, return the face at IT's current buffer position.
2458 Otherwise, use the iterator's base_face_id. */
2459
2460 static int
2461 underlying_face_id (it)
2462 struct it *it;
2463 {
2464 int face_id = it->base_face_id, i;
2465
2466 xassert (STRINGP (it->string));
2467
2468 for (i = it->sp - 1; i >= 0; --i)
2469 if (NILP (it->stack[i].string))
2470 face_id = it->stack[i].face_id;
2471
2472 return face_id;
2473 }
2474
2475
2476 /* Compute the face one character before or after the current position
2477 of IT. BEFORE_P non-zero means get the face in front of IT's
2478 position. Value is the id of the face. */
2479
2480 static int
2481 face_before_or_after_it_pos (it, before_p)
2482 struct it *it;
2483 int before_p;
2484 {
2485 int face_id, limit;
2486 int next_check_charpos;
2487 struct text_pos pos;
2488
2489 xassert (it->s == NULL);
2490
2491 if (STRINGP (it->string))
2492 {
2493 int bufpos, base_face_id;
2494
2495 /* No face change past the end of the string (for the case
2496 we are padding with spaces). No face change before the
2497 string start. */
2498 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2499 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2500 return it->face_id;
2501
2502 /* Set pos to the position before or after IT's current position. */
2503 if (before_p)
2504 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2505 else
2506 /* For composition, we must check the character after the
2507 composition. */
2508 pos = (it->what == IT_COMPOSITION
2509 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2510 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2511
2512 if (it->current.overlay_string_index >= 0)
2513 bufpos = IT_CHARPOS (*it);
2514 else
2515 bufpos = 0;
2516
2517 base_face_id = underlying_face_id (it);
2518
2519 /* Get the face for ASCII, or unibyte. */
2520 face_id = face_at_string_position (it->w,
2521 it->string,
2522 CHARPOS (pos),
2523 bufpos,
2524 it->region_beg_charpos,
2525 it->region_end_charpos,
2526 &next_check_charpos,
2527 base_face_id, 0);
2528
2529 /* Correct the face for charsets different from ASCII. Do it
2530 for the multibyte case only. The face returned above is
2531 suitable for unibyte text if IT->string is unibyte. */
2532 if (STRING_MULTIBYTE (it->string))
2533 {
2534 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2535 int rest = SBYTES (it->string) - BYTEPOS (pos);
2536 int c, len;
2537 struct face *face = FACE_FROM_ID (it->f, face_id);
2538
2539 c = string_char_and_length (p, rest, &len);
2540 face_id = FACE_FOR_CHAR (it->f, face, c);
2541 }
2542 }
2543 else
2544 {
2545 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2546 || (IT_CHARPOS (*it) <= BEGV && before_p))
2547 return it->face_id;
2548
2549 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2550 pos = it->current.pos;
2551
2552 if (before_p)
2553 DEC_TEXT_POS (pos, it->multibyte_p);
2554 else
2555 {
2556 if (it->what == IT_COMPOSITION)
2557 /* For composition, we must check the position after the
2558 composition. */
2559 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2560 else
2561 INC_TEXT_POS (pos, it->multibyte_p);
2562 }
2563
2564 /* Determine face for CHARSET_ASCII, or unibyte. */
2565 face_id = face_at_buffer_position (it->w,
2566 CHARPOS (pos),
2567 it->region_beg_charpos,
2568 it->region_end_charpos,
2569 &next_check_charpos,
2570 limit, 0);
2571
2572 /* Correct the face for charsets different from ASCII. Do it
2573 for the multibyte case only. The face returned above is
2574 suitable for unibyte text if current_buffer is unibyte. */
2575 if (it->multibyte_p)
2576 {
2577 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2578 struct face *face = FACE_FROM_ID (it->f, face_id);
2579 face_id = FACE_FOR_CHAR (it->f, face, c);
2580 }
2581 }
2582
2583 return face_id;
2584 }
2585
2586
2587 \f
2588 /***********************************************************************
2589 Invisible text
2590 ***********************************************************************/
2591
2592 /* Set up iterator IT from invisible properties at its current
2593 position. Called from handle_stop. */
2594
2595 static enum prop_handled
2596 handle_invisible_prop (it)
2597 struct it *it;
2598 {
2599 enum prop_handled handled = HANDLED_NORMALLY;
2600
2601 if (STRINGP (it->string))
2602 {
2603 extern Lisp_Object Qinvisible;
2604 Lisp_Object prop, end_charpos, limit, charpos;
2605
2606 /* Get the value of the invisible text property at the
2607 current position. Value will be nil if there is no such
2608 property. */
2609 charpos = make_number (IT_STRING_CHARPOS (*it));
2610 prop = Fget_text_property (charpos, Qinvisible, it->string);
2611
2612 if (!NILP (prop)
2613 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2614 {
2615 handled = HANDLED_RECOMPUTE_PROPS;
2616
2617 /* Get the position at which the next change of the
2618 invisible text property can be found in IT->string.
2619 Value will be nil if the property value is the same for
2620 all the rest of IT->string. */
2621 XSETINT (limit, SCHARS (it->string));
2622 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2623 it->string, limit);
2624
2625 /* Text at current position is invisible. The next
2626 change in the property is at position end_charpos.
2627 Move IT's current position to that position. */
2628 if (INTEGERP (end_charpos)
2629 && XFASTINT (end_charpos) < XFASTINT (limit))
2630 {
2631 struct text_pos old;
2632 old = it->current.string_pos;
2633 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2634 compute_string_pos (&it->current.string_pos, old, it->string);
2635 }
2636 else
2637 {
2638 /* The rest of the string is invisible. If this is an
2639 overlay string, proceed with the next overlay string
2640 or whatever comes and return a character from there. */
2641 if (it->current.overlay_string_index >= 0)
2642 {
2643 next_overlay_string (it);
2644 /* Don't check for overlay strings when we just
2645 finished processing them. */
2646 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2647 }
2648 else
2649 {
2650 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
2651 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
2652 }
2653 }
2654 }
2655 }
2656 else
2657 {
2658 int invis_p, newpos, next_stop, start_charpos;
2659 Lisp_Object pos, prop, overlay;
2660
2661 /* First of all, is there invisible text at this position? */
2662 start_charpos = IT_CHARPOS (*it);
2663 pos = make_number (IT_CHARPOS (*it));
2664 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
2665 &overlay);
2666 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2667
2668 /* If we are on invisible text, skip over it. */
2669 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
2670 {
2671 /* Record whether we have to display an ellipsis for the
2672 invisible text. */
2673 int display_ellipsis_p = invis_p == 2;
2674
2675 handled = HANDLED_RECOMPUTE_PROPS;
2676
2677 /* Loop skipping over invisible text. The loop is left at
2678 ZV or with IT on the first char being visible again. */
2679 do
2680 {
2681 /* Try to skip some invisible text. Return value is the
2682 position reached which can be equal to IT's position
2683 if there is nothing invisible here. This skips both
2684 over invisible text properties and overlays with
2685 invisible property. */
2686 newpos = skip_invisible (IT_CHARPOS (*it),
2687 &next_stop, ZV, it->window);
2688
2689 /* If we skipped nothing at all we weren't at invisible
2690 text in the first place. If everything to the end of
2691 the buffer was skipped, end the loop. */
2692 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2693 invis_p = 0;
2694 else
2695 {
2696 /* We skipped some characters but not necessarily
2697 all there are. Check if we ended up on visible
2698 text. Fget_char_property returns the property of
2699 the char before the given position, i.e. if we
2700 get invis_p = 0, this means that the char at
2701 newpos is visible. */
2702 pos = make_number (newpos);
2703 prop = Fget_char_property (pos, Qinvisible, it->window);
2704 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
2705 }
2706
2707 /* If we ended up on invisible text, proceed to
2708 skip starting with next_stop. */
2709 if (invis_p)
2710 IT_CHARPOS (*it) = next_stop;
2711 }
2712 while (invis_p);
2713
2714 /* The position newpos is now either ZV or on visible text. */
2715 IT_CHARPOS (*it) = newpos;
2716 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2717
2718 /* If there are before-strings at the start of invisible
2719 text, and the text is invisible because of a text
2720 property, arrange to show before-strings because 20.x did
2721 it that way. (If the text is invisible because of an
2722 overlay property instead of a text property, this is
2723 already handled in the overlay code.) */
2724 if (NILP (overlay)
2725 && get_overlay_strings (it, start_charpos))
2726 {
2727 handled = HANDLED_RECOMPUTE_PROPS;
2728 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
2729 }
2730 else if (display_ellipsis_p)
2731 setup_for_ellipsis (it);
2732 }
2733 }
2734
2735 return handled;
2736 }
2737
2738
2739 /* Make iterator IT return `...' next. */
2740
2741 static void
2742 setup_for_ellipsis (it)
2743 struct it *it;
2744 {
2745 if (it->dp
2746 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2747 {
2748 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2749 it->dpvec = v->contents;
2750 it->dpend = v->contents + v->size;
2751 }
2752 else
2753 {
2754 /* Default `...'. */
2755 it->dpvec = default_invis_vector;
2756 it->dpend = default_invis_vector + 3;
2757 }
2758
2759 /* The ellipsis display does not replace the display of the
2760 character at the new position. Indicate this by setting
2761 IT->dpvec_char_len to zero. */
2762 it->dpvec_char_len = 0;
2763
2764 it->current.dpvec_index = 0;
2765 it->method = next_element_from_display_vector;
2766 }
2767
2768
2769 \f
2770 /***********************************************************************
2771 'display' property
2772 ***********************************************************************/
2773
2774 /* Set up iterator IT from `display' property at its current position.
2775 Called from handle_stop. */
2776
2777 static enum prop_handled
2778 handle_display_prop (it)
2779 struct it *it;
2780 {
2781 Lisp_Object prop, object;
2782 struct text_pos *position;
2783 int display_replaced_p = 0;
2784
2785 if (STRINGP (it->string))
2786 {
2787 object = it->string;
2788 position = &it->current.string_pos;
2789 }
2790 else
2791 {
2792 object = it->w->buffer;
2793 position = &it->current.pos;
2794 }
2795
2796 /* Reset those iterator values set from display property values. */
2797 it->font_height = Qnil;
2798 it->space_width = Qnil;
2799 it->voffset = 0;
2800
2801 /* We don't support recursive `display' properties, i.e. string
2802 values that have a string `display' property, that have a string
2803 `display' property etc. */
2804 if (!it->string_from_display_prop_p)
2805 it->area = TEXT_AREA;
2806
2807 prop = Fget_char_property (make_number (position->charpos),
2808 Qdisplay, object);
2809 if (NILP (prop))
2810 return HANDLED_NORMALLY;
2811
2812 if (CONSP (prop)
2813 /* Simple properties. */
2814 && !EQ (XCAR (prop), Qimage)
2815 && !EQ (XCAR (prop), Qspace)
2816 && !EQ (XCAR (prop), Qwhen)
2817 && !EQ (XCAR (prop), Qspace_width)
2818 && !EQ (XCAR (prop), Qheight)
2819 && !EQ (XCAR (prop), Qraise)
2820 /* Marginal area specifications. */
2821 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
2822 && !NILP (XCAR (prop)))
2823 {
2824 for (; CONSP (prop); prop = XCDR (prop))
2825 {
2826 if (handle_single_display_prop (it, XCAR (prop), object,
2827 position, display_replaced_p))
2828 display_replaced_p = 1;
2829 }
2830 }
2831 else if (VECTORP (prop))
2832 {
2833 int i;
2834 for (i = 0; i < ASIZE (prop); ++i)
2835 if (handle_single_display_prop (it, AREF (prop, i), object,
2836 position, display_replaced_p))
2837 display_replaced_p = 1;
2838 }
2839 else
2840 {
2841 if (handle_single_display_prop (it, prop, object, position, 0))
2842 display_replaced_p = 1;
2843 }
2844
2845 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
2846 }
2847
2848
2849 /* Value is the position of the end of the `display' property starting
2850 at START_POS in OBJECT. */
2851
2852 static struct text_pos
2853 display_prop_end (it, object, start_pos)
2854 struct it *it;
2855 Lisp_Object object;
2856 struct text_pos start_pos;
2857 {
2858 Lisp_Object end;
2859 struct text_pos end_pos;
2860
2861 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2862 Qdisplay, object, Qnil);
2863 CHARPOS (end_pos) = XFASTINT (end);
2864 if (STRINGP (object))
2865 compute_string_pos (&end_pos, start_pos, it->string);
2866 else
2867 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
2868
2869 return end_pos;
2870 }
2871
2872
2873 /* Set up IT from a single `display' sub-property value PROP. OBJECT
2874 is the object in which the `display' property was found. *POSITION
2875 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2876 means that we previously saw a display sub-property which already
2877 replaced text display with something else, for example an image;
2878 ignore such properties after the first one has been processed.
2879
2880 If PROP is a `space' or `image' sub-property, set *POSITION to the
2881 end position of the `display' property.
2882
2883 Value is non-zero if something was found which replaces the display
2884 of buffer or string text. */
2885
2886 static int
2887 handle_single_display_prop (it, prop, object, position,
2888 display_replaced_before_p)
2889 struct it *it;
2890 Lisp_Object prop;
2891 Lisp_Object object;
2892 struct text_pos *position;
2893 int display_replaced_before_p;
2894 {
2895 Lisp_Object value;
2896 int replaces_text_display_p = 0;
2897 Lisp_Object form;
2898
2899 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
2900 evaluated. If the result is nil, VALUE is ignored. */
2901 form = Qt;
2902 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2903 {
2904 prop = XCDR (prop);
2905 if (!CONSP (prop))
2906 return 0;
2907 form = XCAR (prop);
2908 prop = XCDR (prop);
2909 }
2910
2911 if (!NILP (form) && !EQ (form, Qt))
2912 {
2913 int count = SPECPDL_INDEX ();
2914 struct gcpro gcpro1;
2915
2916 /* Bind `object' to the object having the `display' property, a
2917 buffer or string. Bind `position' to the position in the
2918 object where the property was found, and `buffer-position'
2919 to the current position in the buffer. */
2920 specbind (Qobject, object);
2921 specbind (Qposition, make_number (CHARPOS (*position)));
2922 specbind (Qbuffer_position,
2923 make_number (STRINGP (object)
2924 ? IT_CHARPOS (*it) : CHARPOS (*position)));
2925 GCPRO1 (form);
2926 form = safe_eval (form);
2927 UNGCPRO;
2928 unbind_to (count, Qnil);
2929 }
2930
2931 if (NILP (form))
2932 return 0;
2933
2934 if (CONSP (prop)
2935 && EQ (XCAR (prop), Qheight)
2936 && CONSP (XCDR (prop)))
2937 {
2938 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
2939 return 0;
2940
2941 /* `(height HEIGHT)'. */
2942 it->font_height = XCAR (XCDR (prop));
2943 if (!NILP (it->font_height))
2944 {
2945 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2946 int new_height = -1;
2947
2948 if (CONSP (it->font_height)
2949 && (EQ (XCAR (it->font_height), Qplus)
2950 || EQ (XCAR (it->font_height), Qminus))
2951 && CONSP (XCDR (it->font_height))
2952 && INTEGERP (XCAR (XCDR (it->font_height))))
2953 {
2954 /* `(+ N)' or `(- N)' where N is an integer. */
2955 int steps = XINT (XCAR (XCDR (it->font_height)));
2956 if (EQ (XCAR (it->font_height), Qplus))
2957 steps = - steps;
2958 it->face_id = smaller_face (it->f, it->face_id, steps);
2959 }
2960 else if (FUNCTIONP (it->font_height))
2961 {
2962 /* Call function with current height as argument.
2963 Value is the new height. */
2964 Lisp_Object height;
2965 height = safe_call1 (it->font_height,
2966 face->lface[LFACE_HEIGHT_INDEX]);
2967 if (NUMBERP (height))
2968 new_height = XFLOATINT (height);
2969 }
2970 else if (NUMBERP (it->font_height))
2971 {
2972 /* Value is a multiple of the canonical char height. */
2973 struct face *face;
2974
2975 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2976 new_height = (XFLOATINT (it->font_height)
2977 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2978 }
2979 else
2980 {
2981 /* Evaluate IT->font_height with `height' bound to the
2982 current specified height to get the new height. */
2983 Lisp_Object value;
2984 int count = SPECPDL_INDEX ();
2985
2986 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
2987 value = safe_eval (it->font_height);
2988 unbind_to (count, Qnil);
2989
2990 if (NUMBERP (value))
2991 new_height = XFLOATINT (value);
2992 }
2993
2994 if (new_height > 0)
2995 it->face_id = face_with_height (it->f, it->face_id, new_height);
2996 }
2997 }
2998 else if (CONSP (prop)
2999 && EQ (XCAR (prop), Qspace_width)
3000 && CONSP (XCDR (prop)))
3001 {
3002 /* `(space_width WIDTH)'. */
3003 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3004 return 0;
3005
3006 value = XCAR (XCDR (prop));
3007 if (NUMBERP (value) && XFLOATINT (value) > 0)
3008 it->space_width = value;
3009 }
3010 else if (CONSP (prop)
3011 && EQ (XCAR (prop), Qraise)
3012 && CONSP (XCDR (prop)))
3013 {
3014 /* `(raise FACTOR)'. */
3015 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3016 return 0;
3017
3018 #ifdef HAVE_WINDOW_SYSTEM
3019 value = XCAR (XCDR (prop));
3020 if (NUMBERP (value))
3021 {
3022 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3023 it->voffset = - (XFLOATINT (value)
3024 * (FONT_HEIGHT (face->font)));
3025 }
3026 #endif /* HAVE_WINDOW_SYSTEM */
3027 }
3028 else if (!it->string_from_display_prop_p)
3029 {
3030 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3031 VALUE) or `((margin nil) VALUE)' or VALUE. */
3032 Lisp_Object location, value;
3033 struct text_pos start_pos;
3034 int valid_p;
3035
3036 /* Characters having this form of property are not displayed, so
3037 we have to find the end of the property. */
3038 start_pos = *position;
3039 *position = display_prop_end (it, object, start_pos);
3040 value = Qnil;
3041
3042 /* Let's stop at the new position and assume that all
3043 text properties change there. */
3044 it->stop_charpos = position->charpos;
3045
3046 location = Qunbound;
3047 if (CONSP (prop) && CONSP (XCAR (prop)))
3048 {
3049 Lisp_Object tem;
3050
3051 value = XCDR (prop);
3052 if (CONSP (value))
3053 value = XCAR (value);
3054
3055 tem = XCAR (prop);
3056 if (EQ (XCAR (tem), Qmargin)
3057 && (tem = XCDR (tem),
3058 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3059 (NILP (tem)
3060 || EQ (tem, Qleft_margin)
3061 || EQ (tem, Qright_margin))))
3062 location = tem;
3063 }
3064
3065 if (EQ (location, Qunbound))
3066 {
3067 location = Qnil;
3068 value = prop;
3069 }
3070
3071 #ifdef HAVE_WINDOW_SYSTEM
3072 if (FRAME_TERMCAP_P (it->f))
3073 valid_p = STRINGP (value);
3074 else
3075 valid_p = (STRINGP (value)
3076 || (CONSP (value) && EQ (XCAR (value), Qspace))
3077 || valid_image_p (value));
3078 #else /* not HAVE_WINDOW_SYSTEM */
3079 valid_p = STRINGP (value);
3080 #endif /* not HAVE_WINDOW_SYSTEM */
3081
3082 if ((EQ (location, Qleft_margin)
3083 || EQ (location, Qright_margin)
3084 || NILP (location))
3085 && valid_p
3086 && !display_replaced_before_p)
3087 {
3088 replaces_text_display_p = 1;
3089
3090 /* Save current settings of IT so that we can restore them
3091 when we are finished with the glyph property value. */
3092 push_it (it);
3093
3094 if (NILP (location))
3095 it->area = TEXT_AREA;
3096 else if (EQ (location, Qleft_margin))
3097 it->area = LEFT_MARGIN_AREA;
3098 else
3099 it->area = RIGHT_MARGIN_AREA;
3100
3101 if (STRINGP (value))
3102 {
3103 it->string = value;
3104 it->multibyte_p = STRING_MULTIBYTE (it->string);
3105 it->current.overlay_string_index = -1;
3106 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3107 it->end_charpos = it->string_nchars = SCHARS (it->string);
3108 it->method = next_element_from_string;
3109 it->stop_charpos = 0;
3110 it->string_from_display_prop_p = 1;
3111 /* Say that we haven't consumed the characters with
3112 `display' property yet. The call to pop_it in
3113 set_iterator_to_next will clean this up. */
3114 *position = start_pos;
3115 }
3116 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3117 {
3118 it->method = next_element_from_stretch;
3119 it->object = value;
3120 it->current.pos = it->position = start_pos;
3121 }
3122 #ifdef HAVE_WINDOW_SYSTEM
3123 else
3124 {
3125 it->what = IT_IMAGE;
3126 it->image_id = lookup_image (it->f, value);
3127 it->position = start_pos;
3128 it->object = NILP (object) ? it->w->buffer : object;
3129 it->method = next_element_from_image;
3130
3131 /* Say that we haven't consumed the characters with
3132 `display' property yet. The call to pop_it in
3133 set_iterator_to_next will clean this up. */
3134 *position = start_pos;
3135 }
3136 #endif /* HAVE_WINDOW_SYSTEM */
3137 }
3138 else
3139 /* Invalid property or property not supported. Restore
3140 the position to what it was before. */
3141 *position = start_pos;
3142 }
3143
3144 return replaces_text_display_p;
3145 }
3146
3147
3148 /* Check if PROP is a display sub-property value whose text should be
3149 treated as intangible. */
3150
3151 static int
3152 single_display_prop_intangible_p (prop)
3153 Lisp_Object prop;
3154 {
3155 /* Skip over `when FORM'. */
3156 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3157 {
3158 prop = XCDR (prop);
3159 if (!CONSP (prop))
3160 return 0;
3161 prop = XCDR (prop);
3162 }
3163
3164 if (STRINGP (prop))
3165 return 1;
3166
3167 if (!CONSP (prop))
3168 return 0;
3169
3170 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3171 we don't need to treat text as intangible. */
3172 if (EQ (XCAR (prop), Qmargin))
3173 {
3174 prop = XCDR (prop);
3175 if (!CONSP (prop))
3176 return 0;
3177
3178 prop = XCDR (prop);
3179 if (!CONSP (prop)
3180 || EQ (XCAR (prop), Qleft_margin)
3181 || EQ (XCAR (prop), Qright_margin))
3182 return 0;
3183 }
3184
3185 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3186 }
3187
3188
3189 /* Check if PROP is a display property value whose text should be
3190 treated as intangible. */
3191
3192 int
3193 display_prop_intangible_p (prop)
3194 Lisp_Object prop;
3195 {
3196 if (CONSP (prop)
3197 && CONSP (XCAR (prop))
3198 && !EQ (Qmargin, XCAR (XCAR (prop))))
3199 {
3200 /* A list of sub-properties. */
3201 while (CONSP (prop))
3202 {
3203 if (single_display_prop_intangible_p (XCAR (prop)))
3204 return 1;
3205 prop = XCDR (prop);
3206 }
3207 }
3208 else if (VECTORP (prop))
3209 {
3210 /* A vector of sub-properties. */
3211 int i;
3212 for (i = 0; i < ASIZE (prop); ++i)
3213 if (single_display_prop_intangible_p (AREF (prop, i)))
3214 return 1;
3215 }
3216 else
3217 return single_display_prop_intangible_p (prop);
3218
3219 return 0;
3220 }
3221
3222
3223 /* Return 1 if PROP is a display sub-property value containing STRING. */
3224
3225 static int
3226 single_display_prop_string_p (prop, string)
3227 Lisp_Object prop, string;
3228 {
3229 if (EQ (string, prop))
3230 return 1;
3231
3232 /* Skip over `when FORM'. */
3233 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3234 {
3235 prop = XCDR (prop);
3236 if (!CONSP (prop))
3237 return 0;
3238 prop = XCDR (prop);
3239 }
3240
3241 if (CONSP (prop))
3242 /* Skip over `margin LOCATION'. */
3243 if (EQ (XCAR (prop), Qmargin))
3244 {
3245 prop = XCDR (prop);
3246 if (!CONSP (prop))
3247 return 0;
3248
3249 prop = XCDR (prop);
3250 if (!CONSP (prop))
3251 return 0;
3252 }
3253
3254 return CONSP (prop) && EQ (XCAR (prop), string);
3255 }
3256
3257
3258 /* Return 1 if STRING appears in the `display' property PROP. */
3259
3260 static int
3261 display_prop_string_p (prop, string)
3262 Lisp_Object prop, string;
3263 {
3264 if (CONSP (prop)
3265 && CONSP (XCAR (prop))
3266 && !EQ (Qmargin, XCAR (XCAR (prop))))
3267 {
3268 /* A list of sub-properties. */
3269 while (CONSP (prop))
3270 {
3271 if (single_display_prop_string_p (XCAR (prop), string))
3272 return 1;
3273 prop = XCDR (prop);
3274 }
3275 }
3276 else if (VECTORP (prop))
3277 {
3278 /* A vector of sub-properties. */
3279 int i;
3280 for (i = 0; i < ASIZE (prop); ++i)
3281 if (single_display_prop_string_p (AREF (prop, i), string))
3282 return 1;
3283 }
3284 else
3285 return single_display_prop_string_p (prop, string);
3286
3287 return 0;
3288 }
3289
3290
3291 /* Determine from which buffer position in W's buffer STRING comes
3292 from. AROUND_CHARPOS is an approximate position where it could
3293 be from. Value is the buffer position or 0 if it couldn't be
3294 determined.
3295
3296 W's buffer must be current.
3297
3298 This function is necessary because we don't record buffer positions
3299 in glyphs generated from strings (to keep struct glyph small).
3300 This function may only use code that doesn't eval because it is
3301 called asynchronously from note_mouse_highlight. */
3302
3303 int
3304 string_buffer_position (w, string, around_charpos)
3305 struct window *w;
3306 Lisp_Object string;
3307 int around_charpos;
3308 {
3309 Lisp_Object limit, prop, pos;
3310 const int MAX_DISTANCE = 1000;
3311 int found = 0;
3312
3313 pos = make_number (around_charpos);
3314 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3315 while (!found && !EQ (pos, limit))
3316 {
3317 prop = Fget_char_property (pos, Qdisplay, Qnil);
3318 if (!NILP (prop) && display_prop_string_p (prop, string))
3319 found = 1;
3320 else
3321 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3322 }
3323
3324 if (!found)
3325 {
3326 pos = make_number (around_charpos);
3327 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3328 while (!found && !EQ (pos, limit))
3329 {
3330 prop = Fget_char_property (pos, Qdisplay, Qnil);
3331 if (!NILP (prop) && display_prop_string_p (prop, string))
3332 found = 1;
3333 else
3334 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3335 limit);
3336 }
3337 }
3338
3339 return found ? XINT (pos) : 0;
3340 }
3341
3342
3343 \f
3344 /***********************************************************************
3345 `composition' property
3346 ***********************************************************************/
3347
3348 /* Set up iterator IT from `composition' property at its current
3349 position. Called from handle_stop. */
3350
3351 static enum prop_handled
3352 handle_composition_prop (it)
3353 struct it *it;
3354 {
3355 Lisp_Object prop, string;
3356 int pos, pos_byte, end;
3357 enum prop_handled handled = HANDLED_NORMALLY;
3358
3359 if (STRINGP (it->string))
3360 {
3361 pos = IT_STRING_CHARPOS (*it);
3362 pos_byte = IT_STRING_BYTEPOS (*it);
3363 string = it->string;
3364 }
3365 else
3366 {
3367 pos = IT_CHARPOS (*it);
3368 pos_byte = IT_BYTEPOS (*it);
3369 string = Qnil;
3370 }
3371
3372 /* If there's a valid composition and point is not inside of the
3373 composition (in the case that the composition is from the current
3374 buffer), draw a glyph composed from the composition components. */
3375 if (find_composition (pos, -1, &pos, &end, &prop, string)
3376 && COMPOSITION_VALID_P (pos, end, prop)
3377 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3378 {
3379 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3380
3381 if (id >= 0)
3382 {
3383 it->method = next_element_from_composition;
3384 it->cmp_id = id;
3385 it->cmp_len = COMPOSITION_LENGTH (prop);
3386 /* For a terminal, draw only the first character of the
3387 components. */
3388 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3389 it->len = (STRINGP (it->string)
3390 ? string_char_to_byte (it->string, end)
3391 : CHAR_TO_BYTE (end)) - pos_byte;
3392 it->stop_charpos = end;
3393 handled = HANDLED_RETURN;
3394 }
3395 }
3396
3397 return handled;
3398 }
3399
3400
3401 \f
3402 /***********************************************************************
3403 Overlay strings
3404 ***********************************************************************/
3405
3406 /* The following structure is used to record overlay strings for
3407 later sorting in load_overlay_strings. */
3408
3409 struct overlay_entry
3410 {
3411 Lisp_Object overlay;
3412 Lisp_Object string;
3413 int priority;
3414 int after_string_p;
3415 };
3416
3417
3418 /* Set up iterator IT from overlay strings at its current position.
3419 Called from handle_stop. */
3420
3421 static enum prop_handled
3422 handle_overlay_change (it)
3423 struct it *it;
3424 {
3425 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3426 return HANDLED_RECOMPUTE_PROPS;
3427 else
3428 return HANDLED_NORMALLY;
3429 }
3430
3431
3432 /* Set up the next overlay string for delivery by IT, if there is an
3433 overlay string to deliver. Called by set_iterator_to_next when the
3434 end of the current overlay string is reached. If there are more
3435 overlay strings to display, IT->string and
3436 IT->current.overlay_string_index are set appropriately here.
3437 Otherwise IT->string is set to nil. */
3438
3439 static void
3440 next_overlay_string (it)
3441 struct it *it;
3442 {
3443 ++it->current.overlay_string_index;
3444 if (it->current.overlay_string_index == it->n_overlay_strings)
3445 {
3446 /* No more overlay strings. Restore IT's settings to what
3447 they were before overlay strings were processed, and
3448 continue to deliver from current_buffer. */
3449 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3450
3451 pop_it (it);
3452 xassert (it->stop_charpos >= BEGV
3453 && it->stop_charpos <= it->end_charpos);
3454 it->string = Qnil;
3455 it->current.overlay_string_index = -1;
3456 SET_TEXT_POS (it->current.string_pos, -1, -1);
3457 it->n_overlay_strings = 0;
3458 it->method = next_element_from_buffer;
3459
3460 /* If we're at the end of the buffer, record that we have
3461 processed the overlay strings there already, so that
3462 next_element_from_buffer doesn't try it again. */
3463 if (IT_CHARPOS (*it) >= it->end_charpos)
3464 it->overlay_strings_at_end_processed_p = 1;
3465
3466 /* If we have to display `...' for invisible text, set
3467 the iterator up for that. */
3468 if (display_ellipsis_p)
3469 setup_for_ellipsis (it);
3470 }
3471 else
3472 {
3473 /* There are more overlay strings to process. If
3474 IT->current.overlay_string_index has advanced to a position
3475 where we must load IT->overlay_strings with more strings, do
3476 it. */
3477 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3478
3479 if (it->current.overlay_string_index && i == 0)
3480 load_overlay_strings (it, 0);
3481
3482 /* Initialize IT to deliver display elements from the overlay
3483 string. */
3484 it->string = it->overlay_strings[i];
3485 it->multibyte_p = STRING_MULTIBYTE (it->string);
3486 SET_TEXT_POS (it->current.string_pos, 0, 0);
3487 it->method = next_element_from_string;
3488 it->stop_charpos = 0;
3489 }
3490
3491 CHECK_IT (it);
3492 }
3493
3494
3495 /* Compare two overlay_entry structures E1 and E2. Used as a
3496 comparison function for qsort in load_overlay_strings. Overlay
3497 strings for the same position are sorted so that
3498
3499 1. All after-strings come in front of before-strings, except
3500 when they come from the same overlay.
3501
3502 2. Within after-strings, strings are sorted so that overlay strings
3503 from overlays with higher priorities come first.
3504
3505 2. Within before-strings, strings are sorted so that overlay
3506 strings from overlays with higher priorities come last.
3507
3508 Value is analogous to strcmp. */
3509
3510
3511 static int
3512 compare_overlay_entries (e1, e2)
3513 void *e1, *e2;
3514 {
3515 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3516 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3517 int result;
3518
3519 if (entry1->after_string_p != entry2->after_string_p)
3520 {
3521 /* Let after-strings appear in front of before-strings if
3522 they come from different overlays. */
3523 if (EQ (entry1->overlay, entry2->overlay))
3524 result = entry1->after_string_p ? 1 : -1;
3525 else
3526 result = entry1->after_string_p ? -1 : 1;
3527 }
3528 else if (entry1->after_string_p)
3529 /* After-strings sorted in order of decreasing priority. */
3530 result = entry2->priority - entry1->priority;
3531 else
3532 /* Before-strings sorted in order of increasing priority. */
3533 result = entry1->priority - entry2->priority;
3534
3535 return result;
3536 }
3537
3538
3539 /* Load the vector IT->overlay_strings with overlay strings from IT's
3540 current buffer position, or from CHARPOS if that is > 0. Set
3541 IT->n_overlays to the total number of overlay strings found.
3542
3543 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3544 a time. On entry into load_overlay_strings,
3545 IT->current.overlay_string_index gives the number of overlay
3546 strings that have already been loaded by previous calls to this
3547 function.
3548
3549 IT->add_overlay_start contains an additional overlay start
3550 position to consider for taking overlay strings from, if non-zero.
3551 This position comes into play when the overlay has an `invisible'
3552 property, and both before and after-strings. When we've skipped to
3553 the end of the overlay, because of its `invisible' property, we
3554 nevertheless want its before-string to appear.
3555 IT->add_overlay_start will contain the overlay start position
3556 in this case.
3557
3558 Overlay strings are sorted so that after-string strings come in
3559 front of before-string strings. Within before and after-strings,
3560 strings are sorted by overlay priority. See also function
3561 compare_overlay_entries. */
3562
3563 static void
3564 load_overlay_strings (it, charpos)
3565 struct it *it;
3566 int charpos;
3567 {
3568 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3569 Lisp_Object ov, overlay, window, str, invisible;
3570 int start, end;
3571 int size = 20;
3572 int n = 0, i, j, invis_p;
3573 struct overlay_entry *entries
3574 = (struct overlay_entry *) alloca (size * sizeof *entries);
3575
3576 if (charpos <= 0)
3577 charpos = IT_CHARPOS (*it);
3578
3579 /* Append the overlay string STRING of overlay OVERLAY to vector
3580 `entries' which has size `size' and currently contains `n'
3581 elements. AFTER_P non-zero means STRING is an after-string of
3582 OVERLAY. */
3583 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3584 do \
3585 { \
3586 Lisp_Object priority; \
3587 \
3588 if (n == size) \
3589 { \
3590 int new_size = 2 * size; \
3591 struct overlay_entry *old = entries; \
3592 entries = \
3593 (struct overlay_entry *) alloca (new_size \
3594 * sizeof *entries); \
3595 bcopy (old, entries, size * sizeof *entries); \
3596 size = new_size; \
3597 } \
3598 \
3599 entries[n].string = (STRING); \
3600 entries[n].overlay = (OVERLAY); \
3601 priority = Foverlay_get ((OVERLAY), Qpriority); \
3602 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3603 entries[n].after_string_p = (AFTER_P); \
3604 ++n; \
3605 } \
3606 while (0)
3607
3608 /* Process overlay before the overlay center. */
3609 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3610 {
3611 overlay = XCAR (ov);
3612 xassert (OVERLAYP (overlay));
3613 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3614 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3615
3616 if (end < charpos)
3617 break;
3618
3619 /* Skip this overlay if it doesn't start or end at IT's current
3620 position. */
3621 if (end != charpos && start != charpos)
3622 continue;
3623
3624 /* Skip this overlay if it doesn't apply to IT->w. */
3625 window = Foverlay_get (overlay, Qwindow);
3626 if (WINDOWP (window) && XWINDOW (window) != it->w)
3627 continue;
3628
3629 /* If the text ``under'' the overlay is invisible, both before-
3630 and after-strings from this overlay are visible; start and
3631 end position are indistinguishable. */
3632 invisible = Foverlay_get (overlay, Qinvisible);
3633 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3634
3635 /* If overlay has a non-empty before-string, record it. */
3636 if ((start == charpos || (end == charpos && invis_p))
3637 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3638 && SCHARS (str))
3639 RECORD_OVERLAY_STRING (overlay, str, 0);
3640
3641 /* If overlay has a non-empty after-string, record it. */
3642 if ((end == charpos || (start == charpos && invis_p))
3643 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3644 && SCHARS (str))
3645 RECORD_OVERLAY_STRING (overlay, str, 1);
3646 }
3647
3648 /* Process overlays after the overlay center. */
3649 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
3650 {
3651 overlay = XCAR (ov);
3652 xassert (OVERLAYP (overlay));
3653 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3654 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3655
3656 if (start > charpos)
3657 break;
3658
3659 /* Skip this overlay if it doesn't start or end at IT's current
3660 position. */
3661 if (end != charpos && start != charpos)
3662 continue;
3663
3664 /* Skip this overlay if it doesn't apply to IT->w. */
3665 window = Foverlay_get (overlay, Qwindow);
3666 if (WINDOWP (window) && XWINDOW (window) != it->w)
3667 continue;
3668
3669 /* If the text ``under'' the overlay is invisible, it has a zero
3670 dimension, and both before- and after-strings apply. */
3671 invisible = Foverlay_get (overlay, Qinvisible);
3672 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3673
3674 /* If overlay has a non-empty before-string, record it. */
3675 if ((start == charpos || (end == charpos && invis_p))
3676 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3677 && SCHARS (str))
3678 RECORD_OVERLAY_STRING (overlay, str, 0);
3679
3680 /* If overlay has a non-empty after-string, record it. */
3681 if ((end == charpos || (start == charpos && invis_p))
3682 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3683 && SCHARS (str))
3684 RECORD_OVERLAY_STRING (overlay, str, 1);
3685 }
3686
3687 #undef RECORD_OVERLAY_STRING
3688
3689 /* Sort entries. */
3690 if (n > 1)
3691 qsort (entries, n, sizeof *entries, compare_overlay_entries);
3692
3693 /* Record the total number of strings to process. */
3694 it->n_overlay_strings = n;
3695
3696 /* IT->current.overlay_string_index is the number of overlay strings
3697 that have already been consumed by IT. Copy some of the
3698 remaining overlay strings to IT->overlay_strings. */
3699 i = 0;
3700 j = it->current.overlay_string_index;
3701 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3702 it->overlay_strings[i++] = entries[j++].string;
3703
3704 CHECK_IT (it);
3705 }
3706
3707
3708 /* Get the first chunk of overlay strings at IT's current buffer
3709 position, or at CHARPOS if that is > 0. Value is non-zero if at
3710 least one overlay string was found. */
3711
3712 static int
3713 get_overlay_strings (it, charpos)
3714 struct it *it;
3715 int charpos;
3716 {
3717 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3718 process. This fills IT->overlay_strings with strings, and sets
3719 IT->n_overlay_strings to the total number of strings to process.
3720 IT->pos.overlay_string_index has to be set temporarily to zero
3721 because load_overlay_strings needs this; it must be set to -1
3722 when no overlay strings are found because a zero value would
3723 indicate a position in the first overlay string. */
3724 it->current.overlay_string_index = 0;
3725 load_overlay_strings (it, charpos);
3726
3727 /* If we found overlay strings, set up IT to deliver display
3728 elements from the first one. Otherwise set up IT to deliver
3729 from current_buffer. */
3730 if (it->n_overlay_strings)
3731 {
3732 /* Make sure we know settings in current_buffer, so that we can
3733 restore meaningful values when we're done with the overlay
3734 strings. */
3735 compute_stop_pos (it);
3736 xassert (it->face_id >= 0);
3737
3738 /* Save IT's settings. They are restored after all overlay
3739 strings have been processed. */
3740 xassert (it->sp == 0);
3741 push_it (it);
3742
3743 /* Set up IT to deliver display elements from the first overlay
3744 string. */
3745 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3746 it->string = it->overlay_strings[0];
3747 it->stop_charpos = 0;
3748 xassert (STRINGP (it->string));
3749 it->end_charpos = SCHARS (it->string);
3750 it->multibyte_p = STRING_MULTIBYTE (it->string);
3751 it->method = next_element_from_string;
3752 }
3753 else
3754 {
3755 it->string = Qnil;
3756 it->current.overlay_string_index = -1;
3757 it->method = next_element_from_buffer;
3758 }
3759
3760 CHECK_IT (it);
3761
3762 /* Value is non-zero if we found at least one overlay string. */
3763 return STRINGP (it->string);
3764 }
3765
3766
3767 \f
3768 /***********************************************************************
3769 Saving and restoring state
3770 ***********************************************************************/
3771
3772 /* Save current settings of IT on IT->stack. Called, for example,
3773 before setting up IT for an overlay string, to be able to restore
3774 IT's settings to what they were after the overlay string has been
3775 processed. */
3776
3777 static void
3778 push_it (it)
3779 struct it *it;
3780 {
3781 struct iterator_stack_entry *p;
3782
3783 xassert (it->sp < 2);
3784 p = it->stack + it->sp;
3785
3786 p->stop_charpos = it->stop_charpos;
3787 xassert (it->face_id >= 0);
3788 p->face_id = it->face_id;
3789 p->string = it->string;
3790 p->pos = it->current;
3791 p->end_charpos = it->end_charpos;
3792 p->string_nchars = it->string_nchars;
3793 p->area = it->area;
3794 p->multibyte_p = it->multibyte_p;
3795 p->space_width = it->space_width;
3796 p->font_height = it->font_height;
3797 p->voffset = it->voffset;
3798 p->string_from_display_prop_p = it->string_from_display_prop_p;
3799 p->display_ellipsis_p = 0;
3800 ++it->sp;
3801 }
3802
3803
3804 /* Restore IT's settings from IT->stack. Called, for example, when no
3805 more overlay strings must be processed, and we return to delivering
3806 display elements from a buffer, or when the end of a string from a
3807 `display' property is reached and we return to delivering display
3808 elements from an overlay string, or from a buffer. */
3809
3810 static void
3811 pop_it (it)
3812 struct it *it;
3813 {
3814 struct iterator_stack_entry *p;
3815
3816 xassert (it->sp > 0);
3817 --it->sp;
3818 p = it->stack + it->sp;
3819 it->stop_charpos = p->stop_charpos;
3820 it->face_id = p->face_id;
3821 it->string = p->string;
3822 it->current = p->pos;
3823 it->end_charpos = p->end_charpos;
3824 it->string_nchars = p->string_nchars;
3825 it->area = p->area;
3826 it->multibyte_p = p->multibyte_p;
3827 it->space_width = p->space_width;
3828 it->font_height = p->font_height;
3829 it->voffset = p->voffset;
3830 it->string_from_display_prop_p = p->string_from_display_prop_p;
3831 }
3832
3833
3834 \f
3835 /***********************************************************************
3836 Moving over lines
3837 ***********************************************************************/
3838
3839 /* Set IT's current position to the previous line start. */
3840
3841 static void
3842 back_to_previous_line_start (it)
3843 struct it *it;
3844 {
3845 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3846 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3847 }
3848
3849
3850 /* Move IT to the next line start.
3851
3852 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3853 we skipped over part of the text (as opposed to moving the iterator
3854 continuously over the text). Otherwise, don't change the value
3855 of *SKIPPED_P.
3856
3857 Newlines may come from buffer text, overlay strings, or strings
3858 displayed via the `display' property. That's the reason we can't
3859 simply use find_next_newline_no_quit.
3860
3861 Note that this function may not skip over invisible text that is so
3862 because of text properties and immediately follows a newline. If
3863 it would, function reseat_at_next_visible_line_start, when called
3864 from set_iterator_to_next, would effectively make invisible
3865 characters following a newline part of the wrong glyph row, which
3866 leads to wrong cursor motion. */
3867
3868 static int
3869 forward_to_next_line_start (it, skipped_p)
3870 struct it *it;
3871 int *skipped_p;
3872 {
3873 int old_selective, newline_found_p, n;
3874 const int MAX_NEWLINE_DISTANCE = 500;
3875
3876 /* If already on a newline, just consume it to avoid unintended
3877 skipping over invisible text below. */
3878 if (it->what == IT_CHARACTER
3879 && it->c == '\n'
3880 && CHARPOS (it->position) == IT_CHARPOS (*it))
3881 {
3882 set_iterator_to_next (it, 0);
3883 it->c = 0;
3884 return 1;
3885 }
3886
3887 /* Don't handle selective display in the following. It's (a)
3888 unnecessary because it's done by the caller, and (b) leads to an
3889 infinite recursion because next_element_from_ellipsis indirectly
3890 calls this function. */
3891 old_selective = it->selective;
3892 it->selective = 0;
3893
3894 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3895 from buffer text. */
3896 for (n = newline_found_p = 0;
3897 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3898 n += STRINGP (it->string) ? 0 : 1)
3899 {
3900 if (!get_next_display_element (it))
3901 return 0;
3902 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
3903 set_iterator_to_next (it, 0);
3904 }
3905
3906 /* If we didn't find a newline near enough, see if we can use a
3907 short-cut. */
3908 if (!newline_found_p)
3909 {
3910 int start = IT_CHARPOS (*it);
3911 int limit = find_next_newline_no_quit (start, 1);
3912 Lisp_Object pos;
3913
3914 xassert (!STRINGP (it->string));
3915
3916 /* If there isn't any `display' property in sight, and no
3917 overlays, we can just use the position of the newline in
3918 buffer text. */
3919 if (it->stop_charpos >= limit
3920 || ((pos = Fnext_single_property_change (make_number (start),
3921 Qdisplay,
3922 Qnil, make_number (limit)),
3923 NILP (pos))
3924 && next_overlay_change (start) == ZV))
3925 {
3926 IT_CHARPOS (*it) = limit;
3927 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3928 *skipped_p = newline_found_p = 1;
3929 }
3930 else
3931 {
3932 while (get_next_display_element (it)
3933 && !newline_found_p)
3934 {
3935 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3936 set_iterator_to_next (it, 0);
3937 }
3938 }
3939 }
3940
3941 it->selective = old_selective;
3942 return newline_found_p;
3943 }
3944
3945
3946 /* Set IT's current position to the previous visible line start. Skip
3947 invisible text that is so either due to text properties or due to
3948 selective display. Caution: this does not change IT->current_x and
3949 IT->hpos. */
3950
3951 static void
3952 back_to_previous_visible_line_start (it)
3953 struct it *it;
3954 {
3955 int visible_p = 0;
3956
3957 /* Go back one newline if not on BEGV already. */
3958 if (IT_CHARPOS (*it) > BEGV)
3959 back_to_previous_line_start (it);
3960
3961 /* Move over lines that are invisible because of selective display
3962 or text properties. */
3963 while (IT_CHARPOS (*it) > BEGV
3964 && !visible_p)
3965 {
3966 visible_p = 1;
3967
3968 /* If selective > 0, then lines indented more than that values
3969 are invisible. */
3970 if (it->selective > 0
3971 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3972 (double) it->selective)) /* iftc */
3973 visible_p = 0;
3974 else
3975 {
3976 Lisp_Object prop;
3977
3978 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3979 Qinvisible, it->window);
3980 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3981 visible_p = 0;
3982 }
3983
3984 /* Back one more newline if the current one is invisible. */
3985 if (!visible_p)
3986 back_to_previous_line_start (it);
3987 }
3988
3989 xassert (IT_CHARPOS (*it) >= BEGV);
3990 xassert (IT_CHARPOS (*it) == BEGV
3991 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3992 CHECK_IT (it);
3993 }
3994
3995
3996 /* Reseat iterator IT at the previous visible line start. Skip
3997 invisible text that is so either due to text properties or due to
3998 selective display. At the end, update IT's overlay information,
3999 face information etc. */
4000
4001 static void
4002 reseat_at_previous_visible_line_start (it)
4003 struct it *it;
4004 {
4005 back_to_previous_visible_line_start (it);
4006 reseat (it, it->current.pos, 1);
4007 CHECK_IT (it);
4008 }
4009
4010
4011 /* Reseat iterator IT on the next visible line start in the current
4012 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4013 preceding the line start. Skip over invisible text that is so
4014 because of selective display. Compute faces, overlays etc at the
4015 new position. Note that this function does not skip over text that
4016 is invisible because of text properties. */
4017
4018 static void
4019 reseat_at_next_visible_line_start (it, on_newline_p)
4020 struct it *it;
4021 int on_newline_p;
4022 {
4023 int newline_found_p, skipped_p = 0;
4024
4025 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4026
4027 /* Skip over lines that are invisible because they are indented
4028 more than the value of IT->selective. */
4029 if (it->selective > 0)
4030 while (IT_CHARPOS (*it) < ZV
4031 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4032 (double) it->selective)) /* iftc */
4033 {
4034 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4035 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4036 }
4037
4038 /* Position on the newline if that's what's requested. */
4039 if (on_newline_p && newline_found_p)
4040 {
4041 if (STRINGP (it->string))
4042 {
4043 if (IT_STRING_CHARPOS (*it) > 0)
4044 {
4045 --IT_STRING_CHARPOS (*it);
4046 --IT_STRING_BYTEPOS (*it);
4047 }
4048 }
4049 else if (IT_CHARPOS (*it) > BEGV)
4050 {
4051 --IT_CHARPOS (*it);
4052 --IT_BYTEPOS (*it);
4053 reseat (it, it->current.pos, 0);
4054 }
4055 }
4056 else if (skipped_p)
4057 reseat (it, it->current.pos, 0);
4058
4059 CHECK_IT (it);
4060 }
4061
4062
4063 \f
4064 /***********************************************************************
4065 Changing an iterator's position
4066 ***********************************************************************/
4067
4068 /* Change IT's current position to POS in current_buffer. If FORCE_P
4069 is non-zero, always check for text properties at the new position.
4070 Otherwise, text properties are only looked up if POS >=
4071 IT->check_charpos of a property. */
4072
4073 static void
4074 reseat (it, pos, force_p)
4075 struct it *it;
4076 struct text_pos pos;
4077 int force_p;
4078 {
4079 int original_pos = IT_CHARPOS (*it);
4080
4081 reseat_1 (it, pos, 0);
4082
4083 /* Determine where to check text properties. Avoid doing it
4084 where possible because text property lookup is very expensive. */
4085 if (force_p
4086 || CHARPOS (pos) > it->stop_charpos
4087 || CHARPOS (pos) < original_pos)
4088 handle_stop (it);
4089
4090 CHECK_IT (it);
4091 }
4092
4093
4094 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4095 IT->stop_pos to POS, also. */
4096
4097 static void
4098 reseat_1 (it, pos, set_stop_p)
4099 struct it *it;
4100 struct text_pos pos;
4101 int set_stop_p;
4102 {
4103 /* Don't call this function when scanning a C string. */
4104 xassert (it->s == NULL);
4105
4106 /* POS must be a reasonable value. */
4107 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4108
4109 it->current.pos = it->position = pos;
4110 XSETBUFFER (it->object, current_buffer);
4111 it->end_charpos = ZV;
4112 it->dpvec = NULL;
4113 it->current.dpvec_index = -1;
4114 it->current.overlay_string_index = -1;
4115 IT_STRING_CHARPOS (*it) = -1;
4116 IT_STRING_BYTEPOS (*it) = -1;
4117 it->string = Qnil;
4118 it->method = next_element_from_buffer;
4119 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4120 it->sp = 0;
4121 it->face_before_selective_p = 0;
4122
4123 if (set_stop_p)
4124 it->stop_charpos = CHARPOS (pos);
4125 }
4126
4127
4128 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4129 If S is non-null, it is a C string to iterate over. Otherwise,
4130 STRING gives a Lisp string to iterate over.
4131
4132 If PRECISION > 0, don't return more then PRECISION number of
4133 characters from the string.
4134
4135 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4136 characters have been returned. FIELD_WIDTH < 0 means an infinite
4137 field width.
4138
4139 MULTIBYTE = 0 means disable processing of multibyte characters,
4140 MULTIBYTE > 0 means enable it,
4141 MULTIBYTE < 0 means use IT->multibyte_p.
4142
4143 IT must be initialized via a prior call to init_iterator before
4144 calling this function. */
4145
4146 static void
4147 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4148 struct it *it;
4149 unsigned char *s;
4150 Lisp_Object string;
4151 int charpos;
4152 int precision, field_width, multibyte;
4153 {
4154 /* No region in strings. */
4155 it->region_beg_charpos = it->region_end_charpos = -1;
4156
4157 /* No text property checks performed by default, but see below. */
4158 it->stop_charpos = -1;
4159
4160 /* Set iterator position and end position. */
4161 bzero (&it->current, sizeof it->current);
4162 it->current.overlay_string_index = -1;
4163 it->current.dpvec_index = -1;
4164 xassert (charpos >= 0);
4165
4166 /* If STRING is specified, use its multibyteness, otherwise use the
4167 setting of MULTIBYTE, if specified. */
4168 if (multibyte >= 0)
4169 it->multibyte_p = multibyte > 0;
4170
4171 if (s == NULL)
4172 {
4173 xassert (STRINGP (string));
4174 it->string = string;
4175 it->s = NULL;
4176 it->end_charpos = it->string_nchars = SCHARS (string);
4177 it->method = next_element_from_string;
4178 it->current.string_pos = string_pos (charpos, string);
4179 }
4180 else
4181 {
4182 it->s = s;
4183 it->string = Qnil;
4184
4185 /* Note that we use IT->current.pos, not it->current.string_pos,
4186 for displaying C strings. */
4187 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4188 if (it->multibyte_p)
4189 {
4190 it->current.pos = c_string_pos (charpos, s, 1);
4191 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4192 }
4193 else
4194 {
4195 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4196 it->end_charpos = it->string_nchars = strlen (s);
4197 }
4198
4199 it->method = next_element_from_c_string;
4200 }
4201
4202 /* PRECISION > 0 means don't return more than PRECISION characters
4203 from the string. */
4204 if (precision > 0 && it->end_charpos - charpos > precision)
4205 it->end_charpos = it->string_nchars = charpos + precision;
4206
4207 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4208 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4209 FIELD_WIDTH < 0 means infinite field width. This is useful for
4210 padding with `-' at the end of a mode line. */
4211 if (field_width < 0)
4212 field_width = INFINITY;
4213 if (field_width > it->end_charpos - charpos)
4214 it->end_charpos = charpos + field_width;
4215
4216 /* Use the standard display table for displaying strings. */
4217 if (DISP_TABLE_P (Vstandard_display_table))
4218 it->dp = XCHAR_TABLE (Vstandard_display_table);
4219
4220 it->stop_charpos = charpos;
4221 CHECK_IT (it);
4222 }
4223
4224
4225 \f
4226 /***********************************************************************
4227 Iteration
4228 ***********************************************************************/
4229
4230 /* Load IT's display element fields with information about the next
4231 display element from the current position of IT. Value is zero if
4232 end of buffer (or C string) is reached. */
4233
4234 int
4235 get_next_display_element (it)
4236 struct it *it;
4237 {
4238 /* Non-zero means that we found a display element. Zero means that
4239 we hit the end of what we iterate over. Performance note: the
4240 function pointer `method' used here turns out to be faster than
4241 using a sequence of if-statements. */
4242 int success_p = (*it->method) (it);
4243
4244 if (it->what == IT_CHARACTER)
4245 {
4246 /* Map via display table or translate control characters.
4247 IT->c, IT->len etc. have been set to the next character by
4248 the function call above. If we have a display table, and it
4249 contains an entry for IT->c, translate it. Don't do this if
4250 IT->c itself comes from a display table, otherwise we could
4251 end up in an infinite recursion. (An alternative could be to
4252 count the recursion depth of this function and signal an
4253 error when a certain maximum depth is reached.) Is it worth
4254 it? */
4255 if (success_p && it->dpvec == NULL)
4256 {
4257 Lisp_Object dv;
4258
4259 if (it->dp
4260 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4261 VECTORP (dv)))
4262 {
4263 struct Lisp_Vector *v = XVECTOR (dv);
4264
4265 /* Return the first character from the display table
4266 entry, if not empty. If empty, don't display the
4267 current character. */
4268 if (v->size)
4269 {
4270 it->dpvec_char_len = it->len;
4271 it->dpvec = v->contents;
4272 it->dpend = v->contents + v->size;
4273 it->current.dpvec_index = 0;
4274 it->method = next_element_from_display_vector;
4275 success_p = get_next_display_element (it);
4276 }
4277 else
4278 {
4279 set_iterator_to_next (it, 0);
4280 success_p = get_next_display_element (it);
4281 }
4282 }
4283
4284 /* Translate control characters into `\003' or `^C' form.
4285 Control characters coming from a display table entry are
4286 currently not translated because we use IT->dpvec to hold
4287 the translation. This could easily be changed but I
4288 don't believe that it is worth doing.
4289
4290 If it->multibyte_p is nonzero, eight-bit characters and
4291 non-printable multibyte characters are also translated to
4292 octal form.
4293
4294 If it->multibyte_p is zero, eight-bit characters that
4295 don't have corresponding multibyte char code are also
4296 translated to octal form. */
4297 else if ((it->c < ' '
4298 && (it->area != TEXT_AREA
4299 || (it->c != '\n' && it->c != '\t')))
4300 || (it->multibyte_p
4301 ? ((it->c >= 127
4302 && it->len == 1)
4303 || !CHAR_PRINTABLE_P (it->c))
4304 : (it->c >= 127
4305 && it->c == unibyte_char_to_multibyte (it->c))))
4306 {
4307 /* IT->c is a control character which must be displayed
4308 either as '\003' or as `^C' where the '\\' and '^'
4309 can be defined in the display table. Fill
4310 IT->ctl_chars with glyphs for what we have to
4311 display. Then, set IT->dpvec to these glyphs. */
4312 GLYPH g;
4313
4314 if (it->c < 128 && it->ctl_arrow_p)
4315 {
4316 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4317 if (it->dp
4318 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4319 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4320 g = XINT (DISP_CTRL_GLYPH (it->dp));
4321 else
4322 g = FAST_MAKE_GLYPH ('^', 0);
4323 XSETINT (it->ctl_chars[0], g);
4324
4325 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4326 XSETINT (it->ctl_chars[1], g);
4327
4328 /* Set up IT->dpvec and return first character from it. */
4329 it->dpvec_char_len = it->len;
4330 it->dpvec = it->ctl_chars;
4331 it->dpend = it->dpvec + 2;
4332 it->current.dpvec_index = 0;
4333 it->method = next_element_from_display_vector;
4334 get_next_display_element (it);
4335 }
4336 else
4337 {
4338 unsigned char str[MAX_MULTIBYTE_LENGTH];
4339 int len;
4340 int i;
4341 GLYPH escape_glyph;
4342
4343 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4344 if (it->dp
4345 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4346 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4347 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4348 else
4349 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4350
4351 if (SINGLE_BYTE_CHAR_P (it->c))
4352 str[0] = it->c, len = 1;
4353 else
4354 {
4355 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4356 if (len < 0)
4357 {
4358 /* It's an invalid character, which
4359 shouldn't happen actually, but due to
4360 bugs it may happen. Let's print the char
4361 as is, there's not much meaningful we can
4362 do with it. */
4363 str[0] = it->c;
4364 str[1] = it->c >> 8;
4365 str[2] = it->c >> 16;
4366 str[3] = it->c >> 24;
4367 len = 4;
4368 }
4369 }
4370
4371 for (i = 0; i < len; i++)
4372 {
4373 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4374 /* Insert three more glyphs into IT->ctl_chars for
4375 the octal display of the character. */
4376 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4377 XSETINT (it->ctl_chars[i * 4 + 1], g);
4378 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4379 XSETINT (it->ctl_chars[i * 4 + 2], g);
4380 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4381 XSETINT (it->ctl_chars[i * 4 + 3], g);
4382 }
4383
4384 /* Set up IT->dpvec and return the first character
4385 from it. */
4386 it->dpvec_char_len = it->len;
4387 it->dpvec = it->ctl_chars;
4388 it->dpend = it->dpvec + len * 4;
4389 it->current.dpvec_index = 0;
4390 it->method = next_element_from_display_vector;
4391 get_next_display_element (it);
4392 }
4393 }
4394 }
4395
4396 /* Adjust face id for a multibyte character. There are no
4397 multibyte character in unibyte text. */
4398 if (it->multibyte_p
4399 && success_p
4400 && FRAME_WINDOW_P (it->f))
4401 {
4402 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4403 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4404 }
4405 }
4406
4407 /* Is this character the last one of a run of characters with
4408 box? If yes, set IT->end_of_box_run_p to 1. */
4409 if (it->face_box_p
4410 && it->s == NULL)
4411 {
4412 int face_id;
4413 struct face *face;
4414
4415 it->end_of_box_run_p
4416 = ((face_id = face_after_it_pos (it),
4417 face_id != it->face_id)
4418 && (face = FACE_FROM_ID (it->f, face_id),
4419 face->box == FACE_NO_BOX));
4420 }
4421
4422 /* Value is 0 if end of buffer or string reached. */
4423 return success_p;
4424 }
4425
4426
4427 /* Move IT to the next display element.
4428
4429 RESEAT_P non-zero means if called on a newline in buffer text,
4430 skip to the next visible line start.
4431
4432 Functions get_next_display_element and set_iterator_to_next are
4433 separate because I find this arrangement easier to handle than a
4434 get_next_display_element function that also increments IT's
4435 position. The way it is we can first look at an iterator's current
4436 display element, decide whether it fits on a line, and if it does,
4437 increment the iterator position. The other way around we probably
4438 would either need a flag indicating whether the iterator has to be
4439 incremented the next time, or we would have to implement a
4440 decrement position function which would not be easy to write. */
4441
4442 void
4443 set_iterator_to_next (it, reseat_p)
4444 struct it *it;
4445 int reseat_p;
4446 {
4447 /* Reset flags indicating start and end of a sequence of characters
4448 with box. Reset them at the start of this function because
4449 moving the iterator to a new position might set them. */
4450 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4451
4452 if (it->method == next_element_from_buffer)
4453 {
4454 /* The current display element of IT is a character from
4455 current_buffer. Advance in the buffer, and maybe skip over
4456 invisible lines that are so because of selective display. */
4457 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4458 reseat_at_next_visible_line_start (it, 0);
4459 else
4460 {
4461 xassert (it->len != 0);
4462 IT_BYTEPOS (*it) += it->len;
4463 IT_CHARPOS (*it) += 1;
4464 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4465 }
4466 }
4467 else if (it->method == next_element_from_composition)
4468 {
4469 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4470 if (STRINGP (it->string))
4471 {
4472 IT_STRING_BYTEPOS (*it) += it->len;
4473 IT_STRING_CHARPOS (*it) += it->cmp_len;
4474 it->method = next_element_from_string;
4475 goto consider_string_end;
4476 }
4477 else
4478 {
4479 IT_BYTEPOS (*it) += it->len;
4480 IT_CHARPOS (*it) += it->cmp_len;
4481 it->method = next_element_from_buffer;
4482 }
4483 }
4484 else if (it->method == next_element_from_c_string)
4485 {
4486 /* Current display element of IT is from a C string. */
4487 IT_BYTEPOS (*it) += it->len;
4488 IT_CHARPOS (*it) += 1;
4489 }
4490 else if (it->method == next_element_from_display_vector)
4491 {
4492 /* Current display element of IT is from a display table entry.
4493 Advance in the display table definition. Reset it to null if
4494 end reached, and continue with characters from buffers/
4495 strings. */
4496 ++it->current.dpvec_index;
4497
4498 /* Restore face of the iterator to what they were before the
4499 display vector entry (these entries may contain faces). */
4500 it->face_id = it->saved_face_id;
4501
4502 if (it->dpvec + it->current.dpvec_index == it->dpend)
4503 {
4504 if (it->s)
4505 it->method = next_element_from_c_string;
4506 else if (STRINGP (it->string))
4507 it->method = next_element_from_string;
4508 else
4509 it->method = next_element_from_buffer;
4510
4511 it->dpvec = NULL;
4512 it->current.dpvec_index = -1;
4513
4514 /* Skip over characters which were displayed via IT->dpvec. */
4515 if (it->dpvec_char_len < 0)
4516 reseat_at_next_visible_line_start (it, 1);
4517 else if (it->dpvec_char_len > 0)
4518 {
4519 it->len = it->dpvec_char_len;
4520 set_iterator_to_next (it, reseat_p);
4521 }
4522 }
4523 }
4524 else if (it->method == next_element_from_string)
4525 {
4526 /* Current display element is a character from a Lisp string. */
4527 xassert (it->s == NULL && STRINGP (it->string));
4528 IT_STRING_BYTEPOS (*it) += it->len;
4529 IT_STRING_CHARPOS (*it) += 1;
4530
4531 consider_string_end:
4532
4533 if (it->current.overlay_string_index >= 0)
4534 {
4535 /* IT->string is an overlay string. Advance to the
4536 next, if there is one. */
4537 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4538 next_overlay_string (it);
4539 }
4540 else
4541 {
4542 /* IT->string is not an overlay string. If we reached
4543 its end, and there is something on IT->stack, proceed
4544 with what is on the stack. This can be either another
4545 string, this time an overlay string, or a buffer. */
4546 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4547 && it->sp > 0)
4548 {
4549 pop_it (it);
4550 if (!STRINGP (it->string))
4551 it->method = next_element_from_buffer;
4552 else
4553 goto consider_string_end;
4554 }
4555 }
4556 }
4557 else if (it->method == next_element_from_image
4558 || it->method == next_element_from_stretch)
4559 {
4560 /* The position etc with which we have to proceed are on
4561 the stack. The position may be at the end of a string,
4562 if the `display' property takes up the whole string. */
4563 pop_it (it);
4564 it->image_id = 0;
4565 if (STRINGP (it->string))
4566 {
4567 it->method = next_element_from_string;
4568 goto consider_string_end;
4569 }
4570 else
4571 it->method = next_element_from_buffer;
4572 }
4573 else
4574 /* There are no other methods defined, so this should be a bug. */
4575 abort ();
4576
4577 xassert (it->method != next_element_from_string
4578 || (STRINGP (it->string)
4579 && IT_STRING_CHARPOS (*it) >= 0));
4580 }
4581
4582
4583 /* Load IT's display element fields with information about the next
4584 display element which comes from a display table entry or from the
4585 result of translating a control character to one of the forms `^C'
4586 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4587
4588 static int
4589 next_element_from_display_vector (it)
4590 struct it *it;
4591 {
4592 /* Precondition. */
4593 xassert (it->dpvec && it->current.dpvec_index >= 0);
4594
4595 /* Remember the current face id in case glyphs specify faces.
4596 IT's face is restored in set_iterator_to_next. */
4597 it->saved_face_id = it->face_id;
4598
4599 if (INTEGERP (*it->dpvec)
4600 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4601 {
4602 int lface_id;
4603 GLYPH g;
4604
4605 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4606 it->c = FAST_GLYPH_CHAR (g);
4607 it->len = CHAR_BYTES (it->c);
4608
4609 /* The entry may contain a face id to use. Such a face id is
4610 the id of a Lisp face, not a realized face. A face id of
4611 zero means no face is specified. */
4612 lface_id = FAST_GLYPH_FACE (g);
4613 if (lface_id)
4614 {
4615 /* The function returns -1 if lface_id is invalid. */
4616 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4617 if (face_id >= 0)
4618 it->face_id = face_id;
4619 }
4620 }
4621 else
4622 /* Display table entry is invalid. Return a space. */
4623 it->c = ' ', it->len = 1;
4624
4625 /* Don't change position and object of the iterator here. They are
4626 still the values of the character that had this display table
4627 entry or was translated, and that's what we want. */
4628 it->what = IT_CHARACTER;
4629 return 1;
4630 }
4631
4632
4633 /* Load IT with the next display element from Lisp string IT->string.
4634 IT->current.string_pos is the current position within the string.
4635 If IT->current.overlay_string_index >= 0, the Lisp string is an
4636 overlay string. */
4637
4638 static int
4639 next_element_from_string (it)
4640 struct it *it;
4641 {
4642 struct text_pos position;
4643
4644 xassert (STRINGP (it->string));
4645 xassert (IT_STRING_CHARPOS (*it) >= 0);
4646 position = it->current.string_pos;
4647
4648 /* Time to check for invisible text? */
4649 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4650 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4651 {
4652 handle_stop (it);
4653
4654 /* Since a handler may have changed IT->method, we must
4655 recurse here. */
4656 return get_next_display_element (it);
4657 }
4658
4659 if (it->current.overlay_string_index >= 0)
4660 {
4661 /* Get the next character from an overlay string. In overlay
4662 strings, There is no field width or padding with spaces to
4663 do. */
4664 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4665 {
4666 it->what = IT_EOB;
4667 return 0;
4668 }
4669 else if (STRING_MULTIBYTE (it->string))
4670 {
4671 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4672 const unsigned char *s = (SDATA (it->string)
4673 + IT_STRING_BYTEPOS (*it));
4674 it->c = string_char_and_length (s, remaining, &it->len);
4675 }
4676 else
4677 {
4678 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4679 it->len = 1;
4680 }
4681 }
4682 else
4683 {
4684 /* Get the next character from a Lisp string that is not an
4685 overlay string. Such strings come from the mode line, for
4686 example. We may have to pad with spaces, or truncate the
4687 string. See also next_element_from_c_string. */
4688 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4689 {
4690 it->what = IT_EOB;
4691 return 0;
4692 }
4693 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4694 {
4695 /* Pad with spaces. */
4696 it->c = ' ', it->len = 1;
4697 CHARPOS (position) = BYTEPOS (position) = -1;
4698 }
4699 else if (STRING_MULTIBYTE (it->string))
4700 {
4701 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
4702 const unsigned char *s = (SDATA (it->string)
4703 + IT_STRING_BYTEPOS (*it));
4704 it->c = string_char_and_length (s, maxlen, &it->len);
4705 }
4706 else
4707 {
4708 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
4709 it->len = 1;
4710 }
4711 }
4712
4713 /* Record what we have and where it came from. Note that we store a
4714 buffer position in IT->position although it could arguably be a
4715 string position. */
4716 it->what = IT_CHARACTER;
4717 it->object = it->string;
4718 it->position = position;
4719 return 1;
4720 }
4721
4722
4723 /* Load IT with next display element from C string IT->s.
4724 IT->string_nchars is the maximum number of characters to return
4725 from the string. IT->end_charpos may be greater than
4726 IT->string_nchars when this function is called, in which case we
4727 may have to return padding spaces. Value is zero if end of string
4728 reached, including padding spaces. */
4729
4730 static int
4731 next_element_from_c_string (it)
4732 struct it *it;
4733 {
4734 int success_p = 1;
4735
4736 xassert (it->s);
4737 it->what = IT_CHARACTER;
4738 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4739 it->object = Qnil;
4740
4741 /* IT's position can be greater IT->string_nchars in case a field
4742 width or precision has been specified when the iterator was
4743 initialized. */
4744 if (IT_CHARPOS (*it) >= it->end_charpos)
4745 {
4746 /* End of the game. */
4747 it->what = IT_EOB;
4748 success_p = 0;
4749 }
4750 else if (IT_CHARPOS (*it) >= it->string_nchars)
4751 {
4752 /* Pad with spaces. */
4753 it->c = ' ', it->len = 1;
4754 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4755 }
4756 else if (it->multibyte_p)
4757 {
4758 /* Implementation note: The calls to strlen apparently aren't a
4759 performance problem because there is no noticeable performance
4760 difference between Emacs running in unibyte or multibyte mode. */
4761 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4762 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4763 maxlen, &it->len);
4764 }
4765 else
4766 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4767
4768 return success_p;
4769 }
4770
4771
4772 /* Set up IT to return characters from an ellipsis, if appropriate.
4773 The definition of the ellipsis glyphs may come from a display table
4774 entry. This function Fills IT with the first glyph from the
4775 ellipsis if an ellipsis is to be displayed. */
4776
4777 static int
4778 next_element_from_ellipsis (it)
4779 struct it *it;
4780 {
4781 if (it->selective_display_ellipsis_p)
4782 {
4783 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4784 {
4785 /* Use the display table definition for `...'. Invalid glyphs
4786 will be handled by the method returning elements from dpvec. */
4787 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4788 it->dpvec_char_len = it->len;
4789 it->dpvec = v->contents;
4790 it->dpend = v->contents + v->size;
4791 it->current.dpvec_index = 0;
4792 it->method = next_element_from_display_vector;
4793 }
4794 else
4795 {
4796 /* Use default `...' which is stored in default_invis_vector. */
4797 it->dpvec_char_len = it->len;
4798 it->dpvec = default_invis_vector;
4799 it->dpend = default_invis_vector + 3;
4800 it->current.dpvec_index = 0;
4801 it->method = next_element_from_display_vector;
4802 }
4803 }
4804 else
4805 {
4806 /* The face at the current position may be different from the
4807 face we find after the invisible text. Remember what it
4808 was in IT->saved_face_id, and signal that it's there by
4809 setting face_before_selective_p. */
4810 it->saved_face_id = it->face_id;
4811 it->method = next_element_from_buffer;
4812 reseat_at_next_visible_line_start (it, 1);
4813 it->face_before_selective_p = 1;
4814 }
4815
4816 return get_next_display_element (it);
4817 }
4818
4819
4820 /* Deliver an image display element. The iterator IT is already
4821 filled with image information (done in handle_display_prop). Value
4822 is always 1. */
4823
4824
4825 static int
4826 next_element_from_image (it)
4827 struct it *it;
4828 {
4829 it->what = IT_IMAGE;
4830 return 1;
4831 }
4832
4833
4834 /* Fill iterator IT with next display element from a stretch glyph
4835 property. IT->object is the value of the text property. Value is
4836 always 1. */
4837
4838 static int
4839 next_element_from_stretch (it)
4840 struct it *it;
4841 {
4842 it->what = IT_STRETCH;
4843 return 1;
4844 }
4845
4846
4847 /* Load IT with the next display element from current_buffer. Value
4848 is zero if end of buffer reached. IT->stop_charpos is the next
4849 position at which to stop and check for text properties or buffer
4850 end. */
4851
4852 static int
4853 next_element_from_buffer (it)
4854 struct it *it;
4855 {
4856 int success_p = 1;
4857
4858 /* Check this assumption, otherwise, we would never enter the
4859 if-statement, below. */
4860 xassert (IT_CHARPOS (*it) >= BEGV
4861 && IT_CHARPOS (*it) <= it->stop_charpos);
4862
4863 if (IT_CHARPOS (*it) >= it->stop_charpos)
4864 {
4865 if (IT_CHARPOS (*it) >= it->end_charpos)
4866 {
4867 int overlay_strings_follow_p;
4868
4869 /* End of the game, except when overlay strings follow that
4870 haven't been returned yet. */
4871 if (it->overlay_strings_at_end_processed_p)
4872 overlay_strings_follow_p = 0;
4873 else
4874 {
4875 it->overlay_strings_at_end_processed_p = 1;
4876 overlay_strings_follow_p = get_overlay_strings (it, 0);
4877 }
4878
4879 if (overlay_strings_follow_p)
4880 success_p = get_next_display_element (it);
4881 else
4882 {
4883 it->what = IT_EOB;
4884 it->position = it->current.pos;
4885 success_p = 0;
4886 }
4887 }
4888 else
4889 {
4890 handle_stop (it);
4891 return get_next_display_element (it);
4892 }
4893 }
4894 else
4895 {
4896 /* No face changes, overlays etc. in sight, so just return a
4897 character from current_buffer. */
4898 unsigned char *p;
4899
4900 /* Maybe run the redisplay end trigger hook. Performance note:
4901 This doesn't seem to cost measurable time. */
4902 if (it->redisplay_end_trigger_charpos
4903 && it->glyph_row
4904 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4905 run_redisplay_end_trigger_hook (it);
4906
4907 /* Get the next character, maybe multibyte. */
4908 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
4909 if (it->multibyte_p && !ASCII_BYTE_P (*p))
4910 {
4911 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4912 - IT_BYTEPOS (*it));
4913 it->c = string_char_and_length (p, maxlen, &it->len);
4914 }
4915 else
4916 it->c = *p, it->len = 1;
4917
4918 /* Record what we have and where it came from. */
4919 it->what = IT_CHARACTER;;
4920 it->object = it->w->buffer;
4921 it->position = it->current.pos;
4922
4923 /* Normally we return the character found above, except when we
4924 really want to return an ellipsis for selective display. */
4925 if (it->selective)
4926 {
4927 if (it->c == '\n')
4928 {
4929 /* A value of selective > 0 means hide lines indented more
4930 than that number of columns. */
4931 if (it->selective > 0
4932 && IT_CHARPOS (*it) + 1 < ZV
4933 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4934 IT_BYTEPOS (*it) + 1,
4935 (double) it->selective)) /* iftc */
4936 {
4937 success_p = next_element_from_ellipsis (it);
4938 it->dpvec_char_len = -1;
4939 }
4940 }
4941 else if (it->c == '\r' && it->selective == -1)
4942 {
4943 /* A value of selective == -1 means that everything from the
4944 CR to the end of the line is invisible, with maybe an
4945 ellipsis displayed for it. */
4946 success_p = next_element_from_ellipsis (it);
4947 it->dpvec_char_len = -1;
4948 }
4949 }
4950 }
4951
4952 /* Value is zero if end of buffer reached. */
4953 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
4954 return success_p;
4955 }
4956
4957
4958 /* Run the redisplay end trigger hook for IT. */
4959
4960 static void
4961 run_redisplay_end_trigger_hook (it)
4962 struct it *it;
4963 {
4964 Lisp_Object args[3];
4965
4966 /* IT->glyph_row should be non-null, i.e. we should be actually
4967 displaying something, or otherwise we should not run the hook. */
4968 xassert (it->glyph_row);
4969
4970 /* Set up hook arguments. */
4971 args[0] = Qredisplay_end_trigger_functions;
4972 args[1] = it->window;
4973 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4974 it->redisplay_end_trigger_charpos = 0;
4975
4976 /* Since we are *trying* to run these functions, don't try to run
4977 them again, even if they get an error. */
4978 it->w->redisplay_end_trigger = Qnil;
4979 Frun_hook_with_args (3, args);
4980
4981 /* Notice if it changed the face of the character we are on. */
4982 handle_face_prop (it);
4983 }
4984
4985
4986 /* Deliver a composition display element. The iterator IT is already
4987 filled with composition information (done in
4988 handle_composition_prop). Value is always 1. */
4989
4990 static int
4991 next_element_from_composition (it)
4992 struct it *it;
4993 {
4994 it->what = IT_COMPOSITION;
4995 it->position = (STRINGP (it->string)
4996 ? it->current.string_pos
4997 : it->current.pos);
4998 return 1;
4999 }
5000
5001
5002 \f
5003 /***********************************************************************
5004 Moving an iterator without producing glyphs
5005 ***********************************************************************/
5006
5007 /* Move iterator IT to a specified buffer or X position within one
5008 line on the display without producing glyphs.
5009
5010 OP should be a bit mask including some or all of these bits:
5011 MOVE_TO_X: Stop on reaching x-position TO_X.
5012 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5013 Regardless of OP's value, stop in reaching the end of the display line.
5014
5015 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5016 This means, in particular, that TO_X includes window's horizontal
5017 scroll amount.
5018
5019 The return value has several possible values that
5020 say what condition caused the scan to stop:
5021
5022 MOVE_POS_MATCH_OR_ZV
5023 - when TO_POS or ZV was reached.
5024
5025 MOVE_X_REACHED
5026 -when TO_X was reached before TO_POS or ZV were reached.
5027
5028 MOVE_LINE_CONTINUED
5029 - when we reached the end of the display area and the line must
5030 be continued.
5031
5032 MOVE_LINE_TRUNCATED
5033 - when we reached the end of the display area and the line is
5034 truncated.
5035
5036 MOVE_NEWLINE_OR_CR
5037 - when we stopped at a line end, i.e. a newline or a CR and selective
5038 display is on. */
5039
5040 static enum move_it_result
5041 move_it_in_display_line_to (it, to_charpos, to_x, op)
5042 struct it *it;
5043 int to_charpos, to_x, op;
5044 {
5045 enum move_it_result result = MOVE_UNDEFINED;
5046 struct glyph_row *saved_glyph_row;
5047
5048 /* Don't produce glyphs in produce_glyphs. */
5049 saved_glyph_row = it->glyph_row;
5050 it->glyph_row = NULL;
5051
5052 while (1)
5053 {
5054 int x, i, ascent = 0, descent = 0;
5055
5056 /* Stop when ZV or TO_CHARPOS reached. */
5057 if (!get_next_display_element (it)
5058 || ((op & MOVE_TO_POS) != 0
5059 && BUFFERP (it->object)
5060 && IT_CHARPOS (*it) >= to_charpos))
5061 {
5062 result = MOVE_POS_MATCH_OR_ZV;
5063 break;
5064 }
5065
5066 /* The call to produce_glyphs will get the metrics of the
5067 display element IT is loaded with. We record in x the
5068 x-position before this display element in case it does not
5069 fit on the line. */
5070 x = it->current_x;
5071
5072 /* Remember the line height so far in case the next element doesn't
5073 fit on the line. */
5074 if (!it->truncate_lines_p)
5075 {
5076 ascent = it->max_ascent;
5077 descent = it->max_descent;
5078 }
5079
5080 PRODUCE_GLYPHS (it);
5081
5082 if (it->area != TEXT_AREA)
5083 {
5084 set_iterator_to_next (it, 1);
5085 continue;
5086 }
5087
5088 /* The number of glyphs we get back in IT->nglyphs will normally
5089 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5090 character on a terminal frame, or (iii) a line end. For the
5091 second case, IT->nglyphs - 1 padding glyphs will be present
5092 (on X frames, there is only one glyph produced for a
5093 composite character.
5094
5095 The behavior implemented below means, for continuation lines,
5096 that as many spaces of a TAB as fit on the current line are
5097 displayed there. For terminal frames, as many glyphs of a
5098 multi-glyph character are displayed in the current line, too.
5099 This is what the old redisplay code did, and we keep it that
5100 way. Under X, the whole shape of a complex character must
5101 fit on the line or it will be completely displayed in the
5102 next line.
5103
5104 Note that both for tabs and padding glyphs, all glyphs have
5105 the same width. */
5106 if (it->nglyphs)
5107 {
5108 /* More than one glyph or glyph doesn't fit on line. All
5109 glyphs have the same width. */
5110 int single_glyph_width = it->pixel_width / it->nglyphs;
5111 int new_x;
5112
5113 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5114 {
5115 new_x = x + single_glyph_width;
5116
5117 /* We want to leave anything reaching TO_X to the caller. */
5118 if ((op & MOVE_TO_X) && new_x > to_x)
5119 {
5120 it->current_x = x;
5121 result = MOVE_X_REACHED;
5122 break;
5123 }
5124 else if (/* Lines are continued. */
5125 !it->truncate_lines_p
5126 && (/* And glyph doesn't fit on the line. */
5127 new_x > it->last_visible_x
5128 /* Or it fits exactly and we're on a window
5129 system frame. */
5130 || (new_x == it->last_visible_x
5131 && FRAME_WINDOW_P (it->f))))
5132 {
5133 if (/* IT->hpos == 0 means the very first glyph
5134 doesn't fit on the line, e.g. a wide image. */
5135 it->hpos == 0
5136 || (new_x == it->last_visible_x
5137 && FRAME_WINDOW_P (it->f)))
5138 {
5139 ++it->hpos;
5140 it->current_x = new_x;
5141 if (i == it->nglyphs - 1)
5142 set_iterator_to_next (it, 1);
5143 }
5144 else
5145 {
5146 it->current_x = x;
5147 it->max_ascent = ascent;
5148 it->max_descent = descent;
5149 }
5150
5151 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5152 IT_CHARPOS (*it)));
5153 result = MOVE_LINE_CONTINUED;
5154 break;
5155 }
5156 else if (new_x > it->first_visible_x)
5157 {
5158 /* Glyph is visible. Increment number of glyphs that
5159 would be displayed. */
5160 ++it->hpos;
5161 }
5162 else
5163 {
5164 /* Glyph is completely off the left margin of the display
5165 area. Nothing to do. */
5166 }
5167 }
5168
5169 if (result != MOVE_UNDEFINED)
5170 break;
5171 }
5172 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5173 {
5174 /* Stop when TO_X specified and reached. This check is
5175 necessary here because of lines consisting of a line end,
5176 only. The line end will not produce any glyphs and we
5177 would never get MOVE_X_REACHED. */
5178 xassert (it->nglyphs == 0);
5179 result = MOVE_X_REACHED;
5180 break;
5181 }
5182
5183 /* Is this a line end? If yes, we're done. */
5184 if (ITERATOR_AT_END_OF_LINE_P (it))
5185 {
5186 result = MOVE_NEWLINE_OR_CR;
5187 break;
5188 }
5189
5190 /* The current display element has been consumed. Advance
5191 to the next. */
5192 set_iterator_to_next (it, 1);
5193
5194 /* Stop if lines are truncated and IT's current x-position is
5195 past the right edge of the window now. */
5196 if (it->truncate_lines_p
5197 && it->current_x >= it->last_visible_x)
5198 {
5199 result = MOVE_LINE_TRUNCATED;
5200 break;
5201 }
5202 }
5203
5204 /* Restore the iterator settings altered at the beginning of this
5205 function. */
5206 it->glyph_row = saved_glyph_row;
5207 return result;
5208 }
5209
5210
5211 /* Move IT forward until it satisfies one or more of the criteria in
5212 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5213
5214 OP is a bit-mask that specifies where to stop, and in particular,
5215 which of those four position arguments makes a difference. See the
5216 description of enum move_operation_enum.
5217
5218 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5219 screen line, this function will set IT to the next position >
5220 TO_CHARPOS. */
5221
5222 void
5223 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5224 struct it *it;
5225 int to_charpos, to_x, to_y, to_vpos;
5226 int op;
5227 {
5228 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5229 int line_height;
5230 int reached = 0;
5231
5232 for (;;)
5233 {
5234 if (op & MOVE_TO_VPOS)
5235 {
5236 /* If no TO_CHARPOS and no TO_X specified, stop at the
5237 start of the line TO_VPOS. */
5238 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5239 {
5240 if (it->vpos == to_vpos)
5241 {
5242 reached = 1;
5243 break;
5244 }
5245 else
5246 skip = move_it_in_display_line_to (it, -1, -1, 0);
5247 }
5248 else
5249 {
5250 /* TO_VPOS >= 0 means stop at TO_X in the line at
5251 TO_VPOS, or at TO_POS, whichever comes first. */
5252 if (it->vpos == to_vpos)
5253 {
5254 reached = 2;
5255 break;
5256 }
5257
5258 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5259
5260 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5261 {
5262 reached = 3;
5263 break;
5264 }
5265 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5266 {
5267 /* We have reached TO_X but not in the line we want. */
5268 skip = move_it_in_display_line_to (it, to_charpos,
5269 -1, MOVE_TO_POS);
5270 if (skip == MOVE_POS_MATCH_OR_ZV)
5271 {
5272 reached = 4;
5273 break;
5274 }
5275 }
5276 }
5277 }
5278 else if (op & MOVE_TO_Y)
5279 {
5280 struct it it_backup;
5281
5282 /* TO_Y specified means stop at TO_X in the line containing
5283 TO_Y---or at TO_CHARPOS if this is reached first. The
5284 problem is that we can't really tell whether the line
5285 contains TO_Y before we have completely scanned it, and
5286 this may skip past TO_X. What we do is to first scan to
5287 TO_X.
5288
5289 If TO_X is not specified, use a TO_X of zero. The reason
5290 is to make the outcome of this function more predictable.
5291 If we didn't use TO_X == 0, we would stop at the end of
5292 the line which is probably not what a caller would expect
5293 to happen. */
5294 skip = move_it_in_display_line_to (it, to_charpos,
5295 ((op & MOVE_TO_X)
5296 ? to_x : 0),
5297 (MOVE_TO_X
5298 | (op & MOVE_TO_POS)));
5299
5300 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5301 if (skip == MOVE_POS_MATCH_OR_ZV)
5302 {
5303 reached = 5;
5304 break;
5305 }
5306
5307 /* If TO_X was reached, we would like to know whether TO_Y
5308 is in the line. This can only be said if we know the
5309 total line height which requires us to scan the rest of
5310 the line. */
5311 if (skip == MOVE_X_REACHED)
5312 {
5313 it_backup = *it;
5314 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5315 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5316 op & MOVE_TO_POS);
5317 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5318 }
5319
5320 /* Now, decide whether TO_Y is in this line. */
5321 line_height = it->max_ascent + it->max_descent;
5322 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5323
5324 if (to_y >= it->current_y
5325 && to_y < it->current_y + line_height)
5326 {
5327 if (skip == MOVE_X_REACHED)
5328 /* If TO_Y is in this line and TO_X was reached above,
5329 we scanned too far. We have to restore IT's settings
5330 to the ones before skipping. */
5331 *it = it_backup;
5332 reached = 6;
5333 }
5334 else if (skip == MOVE_X_REACHED)
5335 {
5336 skip = skip2;
5337 if (skip == MOVE_POS_MATCH_OR_ZV)
5338 reached = 7;
5339 }
5340
5341 if (reached)
5342 break;
5343 }
5344 else
5345 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5346
5347 switch (skip)
5348 {
5349 case MOVE_POS_MATCH_OR_ZV:
5350 reached = 8;
5351 goto out;
5352
5353 case MOVE_NEWLINE_OR_CR:
5354 set_iterator_to_next (it, 1);
5355 it->continuation_lines_width = 0;
5356 break;
5357
5358 case MOVE_LINE_TRUNCATED:
5359 it->continuation_lines_width = 0;
5360 reseat_at_next_visible_line_start (it, 0);
5361 if ((op & MOVE_TO_POS) != 0
5362 && IT_CHARPOS (*it) > to_charpos)
5363 {
5364 reached = 9;
5365 goto out;
5366 }
5367 break;
5368
5369 case MOVE_LINE_CONTINUED:
5370 it->continuation_lines_width += it->current_x;
5371 break;
5372
5373 default:
5374 abort ();
5375 }
5376
5377 /* Reset/increment for the next run. */
5378 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5379 it->current_x = it->hpos = 0;
5380 it->current_y += it->max_ascent + it->max_descent;
5381 ++it->vpos;
5382 last_height = it->max_ascent + it->max_descent;
5383 last_max_ascent = it->max_ascent;
5384 it->max_ascent = it->max_descent = 0;
5385 }
5386
5387 out:
5388
5389 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5390 }
5391
5392
5393 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5394
5395 If DY > 0, move IT backward at least that many pixels. DY = 0
5396 means move IT backward to the preceding line start or BEGV. This
5397 function may move over more than DY pixels if IT->current_y - DY
5398 ends up in the middle of a line; in this case IT->current_y will be
5399 set to the top of the line moved to. */
5400
5401 void
5402 move_it_vertically_backward (it, dy)
5403 struct it *it;
5404 int dy;
5405 {
5406 int nlines, h;
5407 struct it it2, it3;
5408 int start_pos = IT_CHARPOS (*it);
5409
5410 xassert (dy >= 0);
5411
5412 /* Estimate how many newlines we must move back. */
5413 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5414
5415 /* Set the iterator's position that many lines back. */
5416 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5417 back_to_previous_visible_line_start (it);
5418
5419 /* Reseat the iterator here. When moving backward, we don't want
5420 reseat to skip forward over invisible text, set up the iterator
5421 to deliver from overlay strings at the new position etc. So,
5422 use reseat_1 here. */
5423 reseat_1 (it, it->current.pos, 1);
5424
5425 /* We are now surely at a line start. */
5426 it->current_x = it->hpos = 0;
5427 it->continuation_lines_width = 0;
5428
5429 /* Move forward and see what y-distance we moved. First move to the
5430 start of the next line so that we get its height. We need this
5431 height to be able to tell whether we reached the specified
5432 y-distance. */
5433 it2 = *it;
5434 it2.max_ascent = it2.max_descent = 0;
5435 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5436 MOVE_TO_POS | MOVE_TO_VPOS);
5437 xassert (IT_CHARPOS (*it) >= BEGV);
5438 it3 = it2;
5439
5440 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5441 xassert (IT_CHARPOS (*it) >= BEGV);
5442 h = it2.current_y - it->current_y;
5443 nlines = it2.vpos - it->vpos;
5444
5445 /* Correct IT's y and vpos position. */
5446 it->vpos -= nlines;
5447 it->current_y -= h;
5448
5449 if (dy == 0)
5450 {
5451 /* DY == 0 means move to the start of the screen line. The
5452 value of nlines is > 0 if continuation lines were involved. */
5453 if (nlines > 0)
5454 move_it_by_lines (it, nlines, 1);
5455 xassert (IT_CHARPOS (*it) <= start_pos);
5456 }
5457 else if (nlines)
5458 {
5459 /* The y-position we try to reach. Note that h has been
5460 subtracted in front of the if-statement. */
5461 int target_y = it->current_y + h - dy;
5462 int y0 = it3.current_y;
5463 int y1 = line_bottom_y (&it3);
5464 int line_height = y1 - y0;
5465
5466 /* If we did not reach target_y, try to move further backward if
5467 we can. If we moved too far backward, try to move forward. */
5468 if (target_y < it->current_y
5469 /* This is heuristic. In a window that's 3 lines high, with
5470 a line height of 13 pixels each, recentering with point
5471 on the bottom line will try to move -39/2 = 19 pixels
5472 backward. Try to avoid moving into the first line. */
5473 && it->current_y - target_y > line_height / 3 * 2
5474 && IT_CHARPOS (*it) > BEGV)
5475 {
5476 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5477 target_y - it->current_y));
5478 move_it_vertically (it, target_y - it->current_y);
5479 xassert (IT_CHARPOS (*it) >= BEGV);
5480 }
5481 else if (target_y >= it->current_y + line_height
5482 && IT_CHARPOS (*it) < ZV)
5483 {
5484 /* Should move forward by at least one line, maybe more.
5485
5486 Note: Calling move_it_by_lines can be expensive on
5487 terminal frames, where compute_motion is used (via
5488 vmotion) to do the job, when there are very long lines
5489 and truncate-lines is nil. That's the reason for
5490 treating terminal frames specially here. */
5491
5492 if (!FRAME_WINDOW_P (it->f))
5493 move_it_vertically (it, target_y - (it->current_y + line_height));
5494 else
5495 {
5496 do
5497 {
5498 move_it_by_lines (it, 1, 1);
5499 }
5500 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5501 }
5502
5503 xassert (IT_CHARPOS (*it) >= BEGV);
5504 }
5505 }
5506 }
5507
5508
5509 /* Move IT by a specified amount of pixel lines DY. DY negative means
5510 move backwards. DY = 0 means move to start of screen line. At the
5511 end, IT will be on the start of a screen line. */
5512
5513 void
5514 move_it_vertically (it, dy)
5515 struct it *it;
5516 int dy;
5517 {
5518 if (dy <= 0)
5519 move_it_vertically_backward (it, -dy);
5520 else if (dy > 0)
5521 {
5522 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5523 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5524 MOVE_TO_POS | MOVE_TO_Y);
5525 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5526
5527 /* If buffer ends in ZV without a newline, move to the start of
5528 the line to satisfy the post-condition. */
5529 if (IT_CHARPOS (*it) == ZV
5530 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5531 move_it_by_lines (it, 0, 0);
5532 }
5533 }
5534
5535
5536 /* Move iterator IT past the end of the text line it is in. */
5537
5538 void
5539 move_it_past_eol (it)
5540 struct it *it;
5541 {
5542 enum move_it_result rc;
5543
5544 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5545 if (rc == MOVE_NEWLINE_OR_CR)
5546 set_iterator_to_next (it, 0);
5547 }
5548
5549
5550 #if 0 /* Currently not used. */
5551
5552 /* Return non-zero if some text between buffer positions START_CHARPOS
5553 and END_CHARPOS is invisible. IT->window is the window for text
5554 property lookup. */
5555
5556 static int
5557 invisible_text_between_p (it, start_charpos, end_charpos)
5558 struct it *it;
5559 int start_charpos, end_charpos;
5560 {
5561 Lisp_Object prop, limit;
5562 int invisible_found_p;
5563
5564 xassert (it != NULL && start_charpos <= end_charpos);
5565
5566 /* Is text at START invisible? */
5567 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5568 it->window);
5569 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5570 invisible_found_p = 1;
5571 else
5572 {
5573 limit = Fnext_single_char_property_change (make_number (start_charpos),
5574 Qinvisible, Qnil,
5575 make_number (end_charpos));
5576 invisible_found_p = XFASTINT (limit) < end_charpos;
5577 }
5578
5579 return invisible_found_p;
5580 }
5581
5582 #endif /* 0 */
5583
5584
5585 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5586 negative means move up. DVPOS == 0 means move to the start of the
5587 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5588 NEED_Y_P is zero, IT->current_y will be left unchanged.
5589
5590 Further optimization ideas: If we would know that IT->f doesn't use
5591 a face with proportional font, we could be faster for
5592 truncate-lines nil. */
5593
5594 void
5595 move_it_by_lines (it, dvpos, need_y_p)
5596 struct it *it;
5597 int dvpos, need_y_p;
5598 {
5599 struct position pos;
5600
5601 if (!FRAME_WINDOW_P (it->f))
5602 {
5603 struct text_pos textpos;
5604
5605 /* We can use vmotion on frames without proportional fonts. */
5606 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5607 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5608 reseat (it, textpos, 1);
5609 it->vpos += pos.vpos;
5610 it->current_y += pos.vpos;
5611 }
5612 else if (dvpos == 0)
5613 {
5614 /* DVPOS == 0 means move to the start of the screen line. */
5615 move_it_vertically_backward (it, 0);
5616 xassert (it->current_x == 0 && it->hpos == 0);
5617 }
5618 else if (dvpos > 0)
5619 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5620 else
5621 {
5622 struct it it2;
5623 int start_charpos, i;
5624
5625 /* Start at the beginning of the screen line containing IT's
5626 position. */
5627 move_it_vertically_backward (it, 0);
5628
5629 /* Go back -DVPOS visible lines and reseat the iterator there. */
5630 start_charpos = IT_CHARPOS (*it);
5631 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5632 back_to_previous_visible_line_start (it);
5633 reseat (it, it->current.pos, 1);
5634 it->current_x = it->hpos = 0;
5635
5636 /* Above call may have moved too far if continuation lines
5637 are involved. Scan forward and see if it did. */
5638 it2 = *it;
5639 it2.vpos = it2.current_y = 0;
5640 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5641 it->vpos -= it2.vpos;
5642 it->current_y -= it2.current_y;
5643 it->current_x = it->hpos = 0;
5644
5645 /* If we moved too far, move IT some lines forward. */
5646 if (it2.vpos > -dvpos)
5647 {
5648 int delta = it2.vpos + dvpos;
5649 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5650 }
5651 }
5652 }
5653
5654 /* Return 1 if IT points into the middle of a display vector. */
5655
5656 int
5657 in_display_vector_p (it)
5658 struct it *it;
5659 {
5660 return (it->method == next_element_from_display_vector
5661 && it->current.dpvec_index > 0
5662 && it->dpvec + it->current.dpvec_index != it->dpend);
5663 }
5664
5665 \f
5666 /***********************************************************************
5667 Messages
5668 ***********************************************************************/
5669
5670
5671 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
5672 to *Messages*. */
5673
5674 void
5675 add_to_log (format, arg1, arg2)
5676 char *format;
5677 Lisp_Object arg1, arg2;
5678 {
5679 Lisp_Object args[3];
5680 Lisp_Object msg, fmt;
5681 char *buffer;
5682 int len;
5683 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5684
5685 /* Do nothing if called asynchronously. Inserting text into
5686 a buffer may call after-change-functions and alike and
5687 that would means running Lisp asynchronously. */
5688 if (handling_signal)
5689 return;
5690
5691 fmt = msg = Qnil;
5692 GCPRO4 (fmt, msg, arg1, arg2);
5693
5694 args[0] = fmt = build_string (format);
5695 args[1] = arg1;
5696 args[2] = arg2;
5697 msg = Fformat (3, args);
5698
5699 len = SBYTES (msg) + 1;
5700 buffer = (char *) alloca (len);
5701 bcopy (SDATA (msg), buffer, len);
5702
5703 message_dolog (buffer, len - 1, 1, 0);
5704 UNGCPRO;
5705 }
5706
5707
5708 /* Output a newline in the *Messages* buffer if "needs" one. */
5709
5710 void
5711 message_log_maybe_newline ()
5712 {
5713 if (message_log_need_newline)
5714 message_dolog ("", 0, 1, 0);
5715 }
5716
5717
5718 /* Add a string M of length NBYTES to the message log, optionally
5719 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5720 nonzero, means interpret the contents of M as multibyte. This
5721 function calls low-level routines in order to bypass text property
5722 hooks, etc. which might not be safe to run. */
5723
5724 void
5725 message_dolog (m, nbytes, nlflag, multibyte)
5726 const char *m;
5727 int nbytes, nlflag, multibyte;
5728 {
5729 if (!NILP (Vmemory_full))
5730 return;
5731
5732 if (!NILP (Vmessage_log_max))
5733 {
5734 struct buffer *oldbuf;
5735 Lisp_Object oldpoint, oldbegv, oldzv;
5736 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5737 int point_at_end = 0;
5738 int zv_at_end = 0;
5739 Lisp_Object old_deactivate_mark, tem;
5740 struct gcpro gcpro1;
5741
5742 old_deactivate_mark = Vdeactivate_mark;
5743 oldbuf = current_buffer;
5744 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5745 current_buffer->undo_list = Qt;
5746
5747 oldpoint = message_dolog_marker1;
5748 set_marker_restricted (oldpoint, make_number (PT), Qnil);
5749 oldbegv = message_dolog_marker2;
5750 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
5751 oldzv = message_dolog_marker3;
5752 set_marker_restricted (oldzv, make_number (ZV), Qnil);
5753 GCPRO1 (old_deactivate_mark);
5754
5755 if (PT == Z)
5756 point_at_end = 1;
5757 if (ZV == Z)
5758 zv_at_end = 1;
5759
5760 BEGV = BEG;
5761 BEGV_BYTE = BEG_BYTE;
5762 ZV = Z;
5763 ZV_BYTE = Z_BYTE;
5764 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5765
5766 /* Insert the string--maybe converting multibyte to single byte
5767 or vice versa, so that all the text fits the buffer. */
5768 if (multibyte
5769 && NILP (current_buffer->enable_multibyte_characters))
5770 {
5771 int i, c, char_bytes;
5772 unsigned char work[1];
5773
5774 /* Convert a multibyte string to single-byte
5775 for the *Message* buffer. */
5776 for (i = 0; i < nbytes; i += char_bytes)
5777 {
5778 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5779 work[0] = (SINGLE_BYTE_CHAR_P (c)
5780 ? c
5781 : multibyte_char_to_unibyte (c, Qnil));
5782 insert_1_both (work, 1, 1, 1, 0, 0);
5783 }
5784 }
5785 else if (! multibyte
5786 && ! NILP (current_buffer->enable_multibyte_characters))
5787 {
5788 int i, c, char_bytes;
5789 unsigned char *msg = (unsigned char *) m;
5790 unsigned char str[MAX_MULTIBYTE_LENGTH];
5791 /* Convert a single-byte string to multibyte
5792 for the *Message* buffer. */
5793 for (i = 0; i < nbytes; i++)
5794 {
5795 c = unibyte_char_to_multibyte (msg[i]);
5796 char_bytes = CHAR_STRING (c, str);
5797 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5798 }
5799 }
5800 else if (nbytes)
5801 insert_1 (m, nbytes, 1, 0, 0);
5802
5803 if (nlflag)
5804 {
5805 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5806 insert_1 ("\n", 1, 1, 0, 0);
5807
5808 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5809 this_bol = PT;
5810 this_bol_byte = PT_BYTE;
5811
5812 /* See if this line duplicates the previous one.
5813 If so, combine duplicates. */
5814 if (this_bol > BEG)
5815 {
5816 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5817 prev_bol = PT;
5818 prev_bol_byte = PT_BYTE;
5819
5820 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5821 this_bol, this_bol_byte);
5822 if (dup)
5823 {
5824 del_range_both (prev_bol, prev_bol_byte,
5825 this_bol, this_bol_byte, 0);
5826 if (dup > 1)
5827 {
5828 char dupstr[40];
5829 int duplen;
5830
5831 /* If you change this format, don't forget to also
5832 change message_log_check_duplicate. */
5833 sprintf (dupstr, " [%d times]", dup);
5834 duplen = strlen (dupstr);
5835 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5836 insert_1 (dupstr, duplen, 1, 0, 1);
5837 }
5838 }
5839 }
5840
5841 /* If we have more than the desired maximum number of lines
5842 in the *Messages* buffer now, delete the oldest ones.
5843 This is safe because we don't have undo in this buffer. */
5844
5845 if (NATNUMP (Vmessage_log_max))
5846 {
5847 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5848 -XFASTINT (Vmessage_log_max) - 1, 0);
5849 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5850 }
5851 }
5852 BEGV = XMARKER (oldbegv)->charpos;
5853 BEGV_BYTE = marker_byte_position (oldbegv);
5854
5855 if (zv_at_end)
5856 {
5857 ZV = Z;
5858 ZV_BYTE = Z_BYTE;
5859 }
5860 else
5861 {
5862 ZV = XMARKER (oldzv)->charpos;
5863 ZV_BYTE = marker_byte_position (oldzv);
5864 }
5865
5866 if (point_at_end)
5867 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5868 else
5869 /* We can't do Fgoto_char (oldpoint) because it will run some
5870 Lisp code. */
5871 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5872 XMARKER (oldpoint)->bytepos);
5873
5874 UNGCPRO;
5875 unchain_marker (oldpoint);
5876 unchain_marker (oldbegv);
5877 unchain_marker (oldzv);
5878
5879 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5880 set_buffer_internal (oldbuf);
5881 if (NILP (tem))
5882 windows_or_buffers_changed = old_windows_or_buffers_changed;
5883 message_log_need_newline = !nlflag;
5884 Vdeactivate_mark = old_deactivate_mark;
5885 }
5886 }
5887
5888
5889 /* We are at the end of the buffer after just having inserted a newline.
5890 (Note: We depend on the fact we won't be crossing the gap.)
5891 Check to see if the most recent message looks a lot like the previous one.
5892 Return 0 if different, 1 if the new one should just replace it, or a
5893 value N > 1 if we should also append " [N times]". */
5894
5895 static int
5896 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5897 int prev_bol, this_bol;
5898 int prev_bol_byte, this_bol_byte;
5899 {
5900 int i;
5901 int len = Z_BYTE - 1 - this_bol_byte;
5902 int seen_dots = 0;
5903 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5904 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5905
5906 for (i = 0; i < len; i++)
5907 {
5908 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5909 seen_dots = 1;
5910 if (p1[i] != p2[i])
5911 return seen_dots;
5912 }
5913 p1 += len;
5914 if (*p1 == '\n')
5915 return 2;
5916 if (*p1++ == ' ' && *p1++ == '[')
5917 {
5918 int n = 0;
5919 while (*p1 >= '0' && *p1 <= '9')
5920 n = n * 10 + *p1++ - '0';
5921 if (strncmp (p1, " times]\n", 8) == 0)
5922 return n+1;
5923 }
5924 return 0;
5925 }
5926
5927
5928 /* Display an echo area message M with a specified length of NBYTES
5929 bytes. The string may include null characters. If M is 0, clear
5930 out any existing message, and let the mini-buffer text show
5931 through.
5932
5933 The buffer M must continue to exist until after the echo area gets
5934 cleared or some other message gets displayed there. This means do
5935 not pass text that is stored in a Lisp string; do not pass text in
5936 a buffer that was alloca'd. */
5937
5938 void
5939 message2 (m, nbytes, multibyte)
5940 const char *m;
5941 int nbytes;
5942 int multibyte;
5943 {
5944 /* First flush out any partial line written with print. */
5945 message_log_maybe_newline ();
5946 if (m)
5947 message_dolog (m, nbytes, 1, multibyte);
5948 message2_nolog (m, nbytes, multibyte);
5949 }
5950
5951
5952 /* The non-logging counterpart of message2. */
5953
5954 void
5955 message2_nolog (m, nbytes, multibyte)
5956 const char *m;
5957 int nbytes, multibyte;
5958 {
5959 struct frame *sf = SELECTED_FRAME ();
5960 message_enable_multibyte = multibyte;
5961
5962 if (noninteractive)
5963 {
5964 if (noninteractive_need_newline)
5965 putc ('\n', stderr);
5966 noninteractive_need_newline = 0;
5967 if (m)
5968 fwrite (m, nbytes, 1, stderr);
5969 if (cursor_in_echo_area == 0)
5970 fprintf (stderr, "\n");
5971 fflush (stderr);
5972 }
5973 /* A null message buffer means that the frame hasn't really been
5974 initialized yet. Error messages get reported properly by
5975 cmd_error, so this must be just an informative message; toss it. */
5976 else if (INTERACTIVE
5977 && sf->glyphs_initialized_p
5978 && FRAME_MESSAGE_BUF (sf))
5979 {
5980 Lisp_Object mini_window;
5981 struct frame *f;
5982
5983 /* Get the frame containing the mini-buffer
5984 that the selected frame is using. */
5985 mini_window = FRAME_MINIBUF_WINDOW (sf);
5986 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5987
5988 FRAME_SAMPLE_VISIBILITY (f);
5989 if (FRAME_VISIBLE_P (sf)
5990 && ! FRAME_VISIBLE_P (f))
5991 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5992
5993 if (m)
5994 {
5995 set_message (m, Qnil, nbytes, multibyte);
5996 if (minibuffer_auto_raise)
5997 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5998 }
5999 else
6000 clear_message (1, 1);
6001
6002 do_pending_window_change (0);
6003 echo_area_display (1);
6004 do_pending_window_change (0);
6005 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6006 (*frame_up_to_date_hook) (f);
6007 }
6008 }
6009
6010
6011 /* Display an echo area message M with a specified length of NBYTES
6012 bytes. The string may include null characters. If M is not a
6013 string, clear out any existing message, and let the mini-buffer
6014 text show through. */
6015
6016 void
6017 message3 (m, nbytes, multibyte)
6018 Lisp_Object m;
6019 int nbytes;
6020 int multibyte;
6021 {
6022 struct gcpro gcpro1;
6023
6024 GCPRO1 (m);
6025
6026 /* First flush out any partial line written with print. */
6027 message_log_maybe_newline ();
6028 if (STRINGP (m))
6029 message_dolog (SDATA (m), nbytes, 1, multibyte);
6030 message3_nolog (m, nbytes, multibyte);
6031
6032 UNGCPRO;
6033 }
6034
6035
6036 /* The non-logging version of message3. */
6037
6038 void
6039 message3_nolog (m, nbytes, multibyte)
6040 Lisp_Object m;
6041 int nbytes, multibyte;
6042 {
6043 struct frame *sf = SELECTED_FRAME ();
6044 message_enable_multibyte = multibyte;
6045
6046 if (noninteractive)
6047 {
6048 if (noninteractive_need_newline)
6049 putc ('\n', stderr);
6050 noninteractive_need_newline = 0;
6051 if (STRINGP (m))
6052 fwrite (SDATA (m), nbytes, 1, stderr);
6053 if (cursor_in_echo_area == 0)
6054 fprintf (stderr, "\n");
6055 fflush (stderr);
6056 }
6057 /* A null message buffer means that the frame hasn't really been
6058 initialized yet. Error messages get reported properly by
6059 cmd_error, so this must be just an informative message; toss it. */
6060 else if (INTERACTIVE
6061 && sf->glyphs_initialized_p
6062 && FRAME_MESSAGE_BUF (sf))
6063 {
6064 Lisp_Object mini_window;
6065 Lisp_Object frame;
6066 struct frame *f;
6067
6068 /* Get the frame containing the mini-buffer
6069 that the selected frame is using. */
6070 mini_window = FRAME_MINIBUF_WINDOW (sf);
6071 frame = XWINDOW (mini_window)->frame;
6072 f = XFRAME (frame);
6073
6074 FRAME_SAMPLE_VISIBILITY (f);
6075 if (FRAME_VISIBLE_P (sf)
6076 && !FRAME_VISIBLE_P (f))
6077 Fmake_frame_visible (frame);
6078
6079 if (STRINGP (m) && SCHARS (m) > 0)
6080 {
6081 set_message (NULL, m, nbytes, multibyte);
6082 if (minibuffer_auto_raise)
6083 Fraise_frame (frame);
6084 }
6085 else
6086 clear_message (1, 1);
6087
6088 do_pending_window_change (0);
6089 echo_area_display (1);
6090 do_pending_window_change (0);
6091 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6092 (*frame_up_to_date_hook) (f);
6093 }
6094 }
6095
6096
6097 /* Display a null-terminated echo area message M. If M is 0, clear
6098 out any existing message, and let the mini-buffer text show through.
6099
6100 The buffer M must continue to exist until after the echo area gets
6101 cleared or some other message gets displayed there. Do not pass
6102 text that is stored in a Lisp string. Do not pass text in a buffer
6103 that was alloca'd. */
6104
6105 void
6106 message1 (m)
6107 char *m;
6108 {
6109 message2 (m, (m ? strlen (m) : 0), 0);
6110 }
6111
6112
6113 /* The non-logging counterpart of message1. */
6114
6115 void
6116 message1_nolog (m)
6117 char *m;
6118 {
6119 message2_nolog (m, (m ? strlen (m) : 0), 0);
6120 }
6121
6122 /* Display a message M which contains a single %s
6123 which gets replaced with STRING. */
6124
6125 void
6126 message_with_string (m, string, log)
6127 char *m;
6128 Lisp_Object string;
6129 int log;
6130 {
6131 CHECK_STRING (string);
6132
6133 if (noninteractive)
6134 {
6135 if (m)
6136 {
6137 if (noninteractive_need_newline)
6138 putc ('\n', stderr);
6139 noninteractive_need_newline = 0;
6140 fprintf (stderr, m, SDATA (string));
6141 if (cursor_in_echo_area == 0)
6142 fprintf (stderr, "\n");
6143 fflush (stderr);
6144 }
6145 }
6146 else if (INTERACTIVE)
6147 {
6148 /* The frame whose minibuffer we're going to display the message on.
6149 It may be larger than the selected frame, so we need
6150 to use its buffer, not the selected frame's buffer. */
6151 Lisp_Object mini_window;
6152 struct frame *f, *sf = SELECTED_FRAME ();
6153
6154 /* Get the frame containing the minibuffer
6155 that the selected frame is using. */
6156 mini_window = FRAME_MINIBUF_WINDOW (sf);
6157 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6158
6159 /* A null message buffer means that the frame hasn't really been
6160 initialized yet. Error messages get reported properly by
6161 cmd_error, so this must be just an informative message; toss it. */
6162 if (FRAME_MESSAGE_BUF (f))
6163 {
6164 Lisp_Object args[2], message;
6165 struct gcpro gcpro1, gcpro2;
6166
6167 args[0] = build_string (m);
6168 args[1] = message = string;
6169 GCPRO2 (args[0], message);
6170 gcpro1.nvars = 2;
6171
6172 message = Fformat (2, args);
6173
6174 if (log)
6175 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6176 else
6177 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6178
6179 UNGCPRO;
6180
6181 /* Print should start at the beginning of the message
6182 buffer next time. */
6183 message_buf_print = 0;
6184 }
6185 }
6186 }
6187
6188
6189 /* Dump an informative message to the minibuf. If M is 0, clear out
6190 any existing message, and let the mini-buffer text show through. */
6191
6192 /* VARARGS 1 */
6193 void
6194 message (m, a1, a2, a3)
6195 char *m;
6196 EMACS_INT a1, a2, a3;
6197 {
6198 if (noninteractive)
6199 {
6200 if (m)
6201 {
6202 if (noninteractive_need_newline)
6203 putc ('\n', stderr);
6204 noninteractive_need_newline = 0;
6205 fprintf (stderr, m, a1, a2, a3);
6206 if (cursor_in_echo_area == 0)
6207 fprintf (stderr, "\n");
6208 fflush (stderr);
6209 }
6210 }
6211 else if (INTERACTIVE)
6212 {
6213 /* The frame whose mini-buffer we're going to display the message
6214 on. It may be larger than the selected frame, so we need to
6215 use its buffer, not the selected frame's buffer. */
6216 Lisp_Object mini_window;
6217 struct frame *f, *sf = SELECTED_FRAME ();
6218
6219 /* Get the frame containing the mini-buffer
6220 that the selected frame is using. */
6221 mini_window = FRAME_MINIBUF_WINDOW (sf);
6222 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6223
6224 /* A null message buffer means that the frame hasn't really been
6225 initialized yet. Error messages get reported properly by
6226 cmd_error, so this must be just an informative message; toss
6227 it. */
6228 if (FRAME_MESSAGE_BUF (f))
6229 {
6230 if (m)
6231 {
6232 int len;
6233 #ifdef NO_ARG_ARRAY
6234 char *a[3];
6235 a[0] = (char *) a1;
6236 a[1] = (char *) a2;
6237 a[2] = (char *) a3;
6238
6239 len = doprnt (FRAME_MESSAGE_BUF (f),
6240 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6241 #else
6242 len = doprnt (FRAME_MESSAGE_BUF (f),
6243 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6244 (char **) &a1);
6245 #endif /* NO_ARG_ARRAY */
6246
6247 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6248 }
6249 else
6250 message1 (0);
6251
6252 /* Print should start at the beginning of the message
6253 buffer next time. */
6254 message_buf_print = 0;
6255 }
6256 }
6257 }
6258
6259
6260 /* The non-logging version of message. */
6261
6262 void
6263 message_nolog (m, a1, a2, a3)
6264 char *m;
6265 EMACS_INT a1, a2, a3;
6266 {
6267 Lisp_Object old_log_max;
6268 old_log_max = Vmessage_log_max;
6269 Vmessage_log_max = Qnil;
6270 message (m, a1, a2, a3);
6271 Vmessage_log_max = old_log_max;
6272 }
6273
6274
6275 /* Display the current message in the current mini-buffer. This is
6276 only called from error handlers in process.c, and is not time
6277 critical. */
6278
6279 void
6280 update_echo_area ()
6281 {
6282 if (!NILP (echo_area_buffer[0]))
6283 {
6284 Lisp_Object string;
6285 string = Fcurrent_message ();
6286 message3 (string, SBYTES (string),
6287 !NILP (current_buffer->enable_multibyte_characters));
6288 }
6289 }
6290
6291
6292 /* Make sure echo area buffers in `echo_buffers' are live.
6293 If they aren't, make new ones. */
6294
6295 static void
6296 ensure_echo_area_buffers ()
6297 {
6298 int i;
6299
6300 for (i = 0; i < 2; ++i)
6301 if (!BUFFERP (echo_buffer[i])
6302 || NILP (XBUFFER (echo_buffer[i])->name))
6303 {
6304 char name[30];
6305 Lisp_Object old_buffer;
6306 int j;
6307
6308 old_buffer = echo_buffer[i];
6309 sprintf (name, " *Echo Area %d*", i);
6310 echo_buffer[i] = Fget_buffer_create (build_string (name));
6311 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6312
6313 for (j = 0; j < 2; ++j)
6314 if (EQ (old_buffer, echo_area_buffer[j]))
6315 echo_area_buffer[j] = echo_buffer[i];
6316 }
6317 }
6318
6319
6320 /* Call FN with args A1..A4 with either the current or last displayed
6321 echo_area_buffer as current buffer.
6322
6323 WHICH zero means use the current message buffer
6324 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6325 from echo_buffer[] and clear it.
6326
6327 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6328 suitable buffer from echo_buffer[] and clear it.
6329
6330 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6331 that the current message becomes the last displayed one, make
6332 choose a suitable buffer for echo_area_buffer[0], and clear it.
6333
6334 Value is what FN returns. */
6335
6336 static int
6337 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6338 struct window *w;
6339 int which;
6340 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6341 EMACS_INT a1;
6342 Lisp_Object a2;
6343 EMACS_INT a3, a4;
6344 {
6345 Lisp_Object buffer;
6346 int this_one, the_other, clear_buffer_p, rc;
6347 int count = SPECPDL_INDEX ();
6348
6349 /* If buffers aren't live, make new ones. */
6350 ensure_echo_area_buffers ();
6351
6352 clear_buffer_p = 0;
6353
6354 if (which == 0)
6355 this_one = 0, the_other = 1;
6356 else if (which > 0)
6357 this_one = 1, the_other = 0;
6358 else
6359 {
6360 this_one = 0, the_other = 1;
6361 clear_buffer_p = 1;
6362
6363 /* We need a fresh one in case the current echo buffer equals
6364 the one containing the last displayed echo area message. */
6365 if (!NILP (echo_area_buffer[this_one])
6366 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6367 echo_area_buffer[this_one] = Qnil;
6368 }
6369
6370 /* Choose a suitable buffer from echo_buffer[] is we don't
6371 have one. */
6372 if (NILP (echo_area_buffer[this_one]))
6373 {
6374 echo_area_buffer[this_one]
6375 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6376 ? echo_buffer[the_other]
6377 : echo_buffer[this_one]);
6378 clear_buffer_p = 1;
6379 }
6380
6381 buffer = echo_area_buffer[this_one];
6382
6383 /* Don't get confused by reusing the buffer used for echoing
6384 for a different purpose. */
6385 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6386 cancel_echoing ();
6387
6388 record_unwind_protect (unwind_with_echo_area_buffer,
6389 with_echo_area_buffer_unwind_data (w));
6390
6391 /* Make the echo area buffer current. Note that for display
6392 purposes, it is not necessary that the displayed window's buffer
6393 == current_buffer, except for text property lookup. So, let's
6394 only set that buffer temporarily here without doing a full
6395 Fset_window_buffer. We must also change w->pointm, though,
6396 because otherwise an assertions in unshow_buffer fails, and Emacs
6397 aborts. */
6398 set_buffer_internal_1 (XBUFFER (buffer));
6399 if (w)
6400 {
6401 w->buffer = buffer;
6402 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6403 }
6404
6405 current_buffer->undo_list = Qt;
6406 current_buffer->read_only = Qnil;
6407 specbind (Qinhibit_read_only, Qt);
6408 specbind (Qinhibit_modification_hooks, Qt);
6409
6410 if (clear_buffer_p && Z > BEG)
6411 del_range (BEG, Z);
6412
6413 xassert (BEGV >= BEG);
6414 xassert (ZV <= Z && ZV >= BEGV);
6415
6416 rc = fn (a1, a2, a3, a4);
6417
6418 xassert (BEGV >= BEG);
6419 xassert (ZV <= Z && ZV >= BEGV);
6420
6421 unbind_to (count, Qnil);
6422 return rc;
6423 }
6424
6425
6426 /* Save state that should be preserved around the call to the function
6427 FN called in with_echo_area_buffer. */
6428
6429 static Lisp_Object
6430 with_echo_area_buffer_unwind_data (w)
6431 struct window *w;
6432 {
6433 int i = 0;
6434 Lisp_Object vector;
6435
6436 /* Reduce consing by keeping one vector in
6437 Vwith_echo_area_save_vector. */
6438 vector = Vwith_echo_area_save_vector;
6439 Vwith_echo_area_save_vector = Qnil;
6440
6441 if (NILP (vector))
6442 vector = Fmake_vector (make_number (7), Qnil);
6443
6444 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6445 AREF (vector, i) = Vdeactivate_mark, ++i;
6446 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6447
6448 if (w)
6449 {
6450 XSETWINDOW (AREF (vector, i), w); ++i;
6451 AREF (vector, i) = w->buffer; ++i;
6452 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6453 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6454 }
6455 else
6456 {
6457 int end = i + 4;
6458 for (; i < end; ++i)
6459 AREF (vector, i) = Qnil;
6460 }
6461
6462 xassert (i == ASIZE (vector));
6463 return vector;
6464 }
6465
6466
6467 /* Restore global state from VECTOR which was created by
6468 with_echo_area_buffer_unwind_data. */
6469
6470 static Lisp_Object
6471 unwind_with_echo_area_buffer (vector)
6472 Lisp_Object vector;
6473 {
6474 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6475 Vdeactivate_mark = AREF (vector, 1);
6476 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6477
6478 if (WINDOWP (AREF (vector, 3)))
6479 {
6480 struct window *w;
6481 Lisp_Object buffer, charpos, bytepos;
6482
6483 w = XWINDOW (AREF (vector, 3));
6484 buffer = AREF (vector, 4);
6485 charpos = AREF (vector, 5);
6486 bytepos = AREF (vector, 6);
6487
6488 w->buffer = buffer;
6489 set_marker_both (w->pointm, buffer,
6490 XFASTINT (charpos), XFASTINT (bytepos));
6491 }
6492
6493 Vwith_echo_area_save_vector = vector;
6494 return Qnil;
6495 }
6496
6497
6498 /* Set up the echo area for use by print functions. MULTIBYTE_P
6499 non-zero means we will print multibyte. */
6500
6501 void
6502 setup_echo_area_for_printing (multibyte_p)
6503 int multibyte_p;
6504 {
6505 /* If we can't find an echo area any more, exit. */
6506 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6507 Fkill_emacs (Qnil);
6508
6509 ensure_echo_area_buffers ();
6510
6511 if (!message_buf_print)
6512 {
6513 /* A message has been output since the last time we printed.
6514 Choose a fresh echo area buffer. */
6515 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6516 echo_area_buffer[0] = echo_buffer[1];
6517 else
6518 echo_area_buffer[0] = echo_buffer[0];
6519
6520 /* Switch to that buffer and clear it. */
6521 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6522 current_buffer->truncate_lines = Qnil;
6523
6524 if (Z > BEG)
6525 {
6526 int count = SPECPDL_INDEX ();
6527 specbind (Qinhibit_read_only, Qt);
6528 /* Note that undo recording is always disabled. */
6529 del_range (BEG, Z);
6530 unbind_to (count, Qnil);
6531 }
6532 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6533
6534 /* Set up the buffer for the multibyteness we need. */
6535 if (multibyte_p
6536 != !NILP (current_buffer->enable_multibyte_characters))
6537 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6538
6539 /* Raise the frame containing the echo area. */
6540 if (minibuffer_auto_raise)
6541 {
6542 struct frame *sf = SELECTED_FRAME ();
6543 Lisp_Object mini_window;
6544 mini_window = FRAME_MINIBUF_WINDOW (sf);
6545 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6546 }
6547
6548 message_log_maybe_newline ();
6549 message_buf_print = 1;
6550 }
6551 else
6552 {
6553 if (NILP (echo_area_buffer[0]))
6554 {
6555 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6556 echo_area_buffer[0] = echo_buffer[1];
6557 else
6558 echo_area_buffer[0] = echo_buffer[0];
6559 }
6560
6561 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6562 {
6563 /* Someone switched buffers between print requests. */
6564 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6565 current_buffer->truncate_lines = Qnil;
6566 }
6567 }
6568 }
6569
6570
6571 /* Display an echo area message in window W. Value is non-zero if W's
6572 height is changed. If display_last_displayed_message_p is
6573 non-zero, display the message that was last displayed, otherwise
6574 display the current message. */
6575
6576 static int
6577 display_echo_area (w)
6578 struct window *w;
6579 {
6580 int i, no_message_p, window_height_changed_p, count;
6581
6582 /* Temporarily disable garbage collections while displaying the echo
6583 area. This is done because a GC can print a message itself.
6584 That message would modify the echo area buffer's contents while a
6585 redisplay of the buffer is going on, and seriously confuse
6586 redisplay. */
6587 count = inhibit_garbage_collection ();
6588
6589 /* If there is no message, we must call display_echo_area_1
6590 nevertheless because it resizes the window. But we will have to
6591 reset the echo_area_buffer in question to nil at the end because
6592 with_echo_area_buffer will sets it to an empty buffer. */
6593 i = display_last_displayed_message_p ? 1 : 0;
6594 no_message_p = NILP (echo_area_buffer[i]);
6595
6596 window_height_changed_p
6597 = with_echo_area_buffer (w, display_last_displayed_message_p,
6598 display_echo_area_1,
6599 (EMACS_INT) w, Qnil, 0, 0);
6600
6601 if (no_message_p)
6602 echo_area_buffer[i] = Qnil;
6603
6604 unbind_to (count, Qnil);
6605 return window_height_changed_p;
6606 }
6607
6608
6609 /* Helper for display_echo_area. Display the current buffer which
6610 contains the current echo area message in window W, a mini-window,
6611 a pointer to which is passed in A1. A2..A4 are currently not used.
6612 Change the height of W so that all of the message is displayed.
6613 Value is non-zero if height of W was changed. */
6614
6615 static int
6616 display_echo_area_1 (a1, a2, a3, a4)
6617 EMACS_INT a1;
6618 Lisp_Object a2;
6619 EMACS_INT a3, a4;
6620 {
6621 struct window *w = (struct window *) a1;
6622 Lisp_Object window;
6623 struct text_pos start;
6624 int window_height_changed_p = 0;
6625
6626 /* Do this before displaying, so that we have a large enough glyph
6627 matrix for the display. */
6628 window_height_changed_p = resize_mini_window (w, 0);
6629
6630 /* Display. */
6631 clear_glyph_matrix (w->desired_matrix);
6632 XSETWINDOW (window, w);
6633 SET_TEXT_POS (start, BEG, BEG_BYTE);
6634 try_window (window, start);
6635
6636 return window_height_changed_p;
6637 }
6638
6639
6640 /* Resize the echo area window to exactly the size needed for the
6641 currently displayed message, if there is one. If a mini-buffer
6642 is active, don't shrink it. */
6643
6644 void
6645 resize_echo_area_exactly ()
6646 {
6647 if (BUFFERP (echo_area_buffer[0])
6648 && WINDOWP (echo_area_window))
6649 {
6650 struct window *w = XWINDOW (echo_area_window);
6651 int resized_p;
6652 Lisp_Object resize_exactly;
6653
6654 if (minibuf_level == 0)
6655 resize_exactly = Qt;
6656 else
6657 resize_exactly = Qnil;
6658
6659 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
6660 (EMACS_INT) w, resize_exactly, 0, 0);
6661 if (resized_p)
6662 {
6663 ++windows_or_buffers_changed;
6664 ++update_mode_lines;
6665 redisplay_internal (0);
6666 }
6667 }
6668 }
6669
6670
6671 /* Callback function for with_echo_area_buffer, when used from
6672 resize_echo_area_exactly. A1 contains a pointer to the window to
6673 resize, EXACTLY non-nil means resize the mini-window exactly to the
6674 size of the text displayed. A3 and A4 are not used. Value is what
6675 resize_mini_window returns. */
6676
6677 static int
6678 resize_mini_window_1 (a1, exactly, a3, a4)
6679 EMACS_INT a1;
6680 Lisp_Object exactly;
6681 EMACS_INT a3, a4;
6682 {
6683 return resize_mini_window ((struct window *) a1, !NILP (exactly));
6684 }
6685
6686
6687 /* Resize mini-window W to fit the size of its contents. EXACT:P
6688 means size the window exactly to the size needed. Otherwise, it's
6689 only enlarged until W's buffer is empty. Value is non-zero if
6690 the window height has been changed. */
6691
6692 int
6693 resize_mini_window (w, exact_p)
6694 struct window *w;
6695 int exact_p;
6696 {
6697 struct frame *f = XFRAME (w->frame);
6698 int window_height_changed_p = 0;
6699
6700 xassert (MINI_WINDOW_P (w));
6701
6702 /* Don't resize windows while redisplaying a window; it would
6703 confuse redisplay functions when the size of the window they are
6704 displaying changes from under them. Such a resizing can happen,
6705 for instance, when which-func prints a long message while
6706 we are running fontification-functions. We're running these
6707 functions with safe_call which binds inhibit-redisplay to t. */
6708 if (!NILP (Vinhibit_redisplay))
6709 return 0;
6710
6711 /* Nil means don't try to resize. */
6712 if (NILP (Vresize_mini_windows)
6713 || (FRAME_X_P (f) && f->output_data.x == NULL))
6714 return 0;
6715
6716 if (!FRAME_MINIBUF_ONLY_P (f))
6717 {
6718 struct it it;
6719 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6720 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6721 int height, max_height;
6722 int unit = CANON_Y_UNIT (f);
6723 struct text_pos start;
6724 struct buffer *old_current_buffer = NULL;
6725
6726 if (current_buffer != XBUFFER (w->buffer))
6727 {
6728 old_current_buffer = current_buffer;
6729 set_buffer_internal (XBUFFER (w->buffer));
6730 }
6731
6732 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
6733
6734 /* Compute the max. number of lines specified by the user. */
6735 if (FLOATP (Vmax_mini_window_height))
6736 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
6737 else if (INTEGERP (Vmax_mini_window_height))
6738 max_height = XINT (Vmax_mini_window_height);
6739 else
6740 max_height = total_height / 4;
6741
6742 /* Correct that max. height if it's bogus. */
6743 max_height = max (1, max_height);
6744 max_height = min (total_height, max_height);
6745
6746 /* Find out the height of the text in the window. */
6747 if (it.truncate_lines_p)
6748 height = 1;
6749 else
6750 {
6751 last_height = 0;
6752 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6753 if (it.max_ascent == 0 && it.max_descent == 0)
6754 height = it.current_y + last_height;
6755 else
6756 height = it.current_y + it.max_ascent + it.max_descent;
6757 height -= it.extra_line_spacing;
6758 height = (height + unit - 1) / unit;
6759 }
6760
6761 /* Compute a suitable window start. */
6762 if (height > max_height)
6763 {
6764 height = max_height;
6765 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6766 move_it_vertically_backward (&it, (height - 1) * unit);
6767 start = it.current.pos;
6768 }
6769 else
6770 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6771 SET_MARKER_FROM_TEXT_POS (w->start, start);
6772
6773 if (EQ (Vresize_mini_windows, Qgrow_only))
6774 {
6775 /* Let it grow only, until we display an empty message, in which
6776 case the window shrinks again. */
6777 if (height > XFASTINT (w->height))
6778 {
6779 int old_height = XFASTINT (w->height);
6780 freeze_window_starts (f, 1);
6781 grow_mini_window (w, height - XFASTINT (w->height));
6782 window_height_changed_p = XFASTINT (w->height) != old_height;
6783 }
6784 else if (height < XFASTINT (w->height)
6785 && (exact_p || BEGV == ZV))
6786 {
6787 int old_height = XFASTINT (w->height);
6788 freeze_window_starts (f, 0);
6789 shrink_mini_window (w);
6790 window_height_changed_p = XFASTINT (w->height) != old_height;
6791 }
6792 }
6793 else
6794 {
6795 /* Always resize to exact size needed. */
6796 if (height > XFASTINT (w->height))
6797 {
6798 int old_height = XFASTINT (w->height);
6799 freeze_window_starts (f, 1);
6800 grow_mini_window (w, height - XFASTINT (w->height));
6801 window_height_changed_p = XFASTINT (w->height) != old_height;
6802 }
6803 else if (height < XFASTINT (w->height))
6804 {
6805 int old_height = XFASTINT (w->height);
6806 freeze_window_starts (f, 0);
6807 shrink_mini_window (w);
6808
6809 if (height)
6810 {
6811 freeze_window_starts (f, 1);
6812 grow_mini_window (w, height - XFASTINT (w->height));
6813 }
6814
6815 window_height_changed_p = XFASTINT (w->height) != old_height;
6816 }
6817 }
6818
6819 if (old_current_buffer)
6820 set_buffer_internal (old_current_buffer);
6821 }
6822
6823 return window_height_changed_p;
6824 }
6825
6826
6827 /* Value is the current message, a string, or nil if there is no
6828 current message. */
6829
6830 Lisp_Object
6831 current_message ()
6832 {
6833 Lisp_Object msg;
6834
6835 if (NILP (echo_area_buffer[0]))
6836 msg = Qnil;
6837 else
6838 {
6839 with_echo_area_buffer (0, 0, current_message_1,
6840 (EMACS_INT) &msg, Qnil, 0, 0);
6841 if (NILP (msg))
6842 echo_area_buffer[0] = Qnil;
6843 }
6844
6845 return msg;
6846 }
6847
6848
6849 static int
6850 current_message_1 (a1, a2, a3, a4)
6851 EMACS_INT a1;
6852 Lisp_Object a2;
6853 EMACS_INT a3, a4;
6854 {
6855 Lisp_Object *msg = (Lisp_Object *) a1;
6856
6857 if (Z > BEG)
6858 *msg = make_buffer_string (BEG, Z, 1);
6859 else
6860 *msg = Qnil;
6861 return 0;
6862 }
6863
6864
6865 /* Push the current message on Vmessage_stack for later restauration
6866 by restore_message. Value is non-zero if the current message isn't
6867 empty. This is a relatively infrequent operation, so it's not
6868 worth optimizing. */
6869
6870 int
6871 push_message ()
6872 {
6873 Lisp_Object msg;
6874 msg = current_message ();
6875 Vmessage_stack = Fcons (msg, Vmessage_stack);
6876 return STRINGP (msg);
6877 }
6878
6879
6880 /* Restore message display from the top of Vmessage_stack. */
6881
6882 void
6883 restore_message ()
6884 {
6885 Lisp_Object msg;
6886
6887 xassert (CONSP (Vmessage_stack));
6888 msg = XCAR (Vmessage_stack);
6889 if (STRINGP (msg))
6890 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
6891 else
6892 message3_nolog (msg, 0, 0);
6893 }
6894
6895
6896 /* Handler for record_unwind_protect calling pop_message. */
6897
6898 Lisp_Object
6899 pop_message_unwind (dummy)
6900 Lisp_Object dummy;
6901 {
6902 pop_message ();
6903 return Qnil;
6904 }
6905
6906 /* Pop the top-most entry off Vmessage_stack. */
6907
6908 void
6909 pop_message ()
6910 {
6911 xassert (CONSP (Vmessage_stack));
6912 Vmessage_stack = XCDR (Vmessage_stack);
6913 }
6914
6915
6916 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6917 exits. If the stack is not empty, we have a missing pop_message
6918 somewhere. */
6919
6920 void
6921 check_message_stack ()
6922 {
6923 if (!NILP (Vmessage_stack))
6924 abort ();
6925 }
6926
6927
6928 /* Truncate to NCHARS what will be displayed in the echo area the next
6929 time we display it---but don't redisplay it now. */
6930
6931 void
6932 truncate_echo_area (nchars)
6933 int nchars;
6934 {
6935 if (nchars == 0)
6936 echo_area_buffer[0] = Qnil;
6937 /* A null message buffer means that the frame hasn't really been
6938 initialized yet. Error messages get reported properly by
6939 cmd_error, so this must be just an informative message; toss it. */
6940 else if (!noninteractive
6941 && INTERACTIVE
6942 && !NILP (echo_area_buffer[0]))
6943 {
6944 struct frame *sf = SELECTED_FRAME ();
6945 if (FRAME_MESSAGE_BUF (sf))
6946 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
6947 }
6948 }
6949
6950
6951 /* Helper function for truncate_echo_area. Truncate the current
6952 message to at most NCHARS characters. */
6953
6954 static int
6955 truncate_message_1 (nchars, a2, a3, a4)
6956 EMACS_INT nchars;
6957 Lisp_Object a2;
6958 EMACS_INT a3, a4;
6959 {
6960 if (BEG + nchars < Z)
6961 del_range (BEG + nchars, Z);
6962 if (Z == BEG)
6963 echo_area_buffer[0] = Qnil;
6964 return 0;
6965 }
6966
6967
6968 /* Set the current message to a substring of S or STRING.
6969
6970 If STRING is a Lisp string, set the message to the first NBYTES
6971 bytes from STRING. NBYTES zero means use the whole string. If
6972 STRING is multibyte, the message will be displayed multibyte.
6973
6974 If S is not null, set the message to the first LEN bytes of S. LEN
6975 zero means use the whole string. MULTIBYTE_P non-zero means S is
6976 multibyte. Display the message multibyte in that case. */
6977
6978 void
6979 set_message (s, string, nbytes, multibyte_p)
6980 const char *s;
6981 Lisp_Object string;
6982 int nbytes, multibyte_p;
6983 {
6984 message_enable_multibyte
6985 = ((s && multibyte_p)
6986 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6987
6988 with_echo_area_buffer (0, -1, set_message_1,
6989 (EMACS_INT) s, string, nbytes, multibyte_p);
6990 message_buf_print = 0;
6991 help_echo_showing_p = 0;
6992 }
6993
6994
6995 /* Helper function for set_message. Arguments have the same meaning
6996 as there, with A1 corresponding to S and A2 corresponding to STRING
6997 This function is called with the echo area buffer being
6998 current. */
6999
7000 static int
7001 set_message_1 (a1, a2, nbytes, multibyte_p)
7002 EMACS_INT a1;
7003 Lisp_Object a2;
7004 EMACS_INT nbytes, multibyte_p;
7005 {
7006 const char *s = (const char *) a1;
7007 Lisp_Object string = a2;
7008
7009 xassert (BEG == Z);
7010
7011 /* Change multibyteness of the echo buffer appropriately. */
7012 if (message_enable_multibyte
7013 != !NILP (current_buffer->enable_multibyte_characters))
7014 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7015
7016 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7017
7018 /* Insert new message at BEG. */
7019 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7020
7021 if (STRINGP (string))
7022 {
7023 int nchars;
7024
7025 if (nbytes == 0)
7026 nbytes = SBYTES (string);
7027 nchars = string_byte_to_char (string, nbytes);
7028
7029 /* This function takes care of single/multibyte conversion. We
7030 just have to ensure that the echo area buffer has the right
7031 setting of enable_multibyte_characters. */
7032 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7033 }
7034 else if (s)
7035 {
7036 if (nbytes == 0)
7037 nbytes = strlen (s);
7038
7039 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7040 {
7041 /* Convert from multi-byte to single-byte. */
7042 int i, c, n;
7043 unsigned char work[1];
7044
7045 /* Convert a multibyte string to single-byte. */
7046 for (i = 0; i < nbytes; i += n)
7047 {
7048 c = string_char_and_length (s + i, nbytes - i, &n);
7049 work[0] = (SINGLE_BYTE_CHAR_P (c)
7050 ? c
7051 : multibyte_char_to_unibyte (c, Qnil));
7052 insert_1_both (work, 1, 1, 1, 0, 0);
7053 }
7054 }
7055 else if (!multibyte_p
7056 && !NILP (current_buffer->enable_multibyte_characters))
7057 {
7058 /* Convert from single-byte to multi-byte. */
7059 int i, c, n;
7060 const unsigned char *msg = (const unsigned char *) s;
7061 unsigned char str[MAX_MULTIBYTE_LENGTH];
7062
7063 /* Convert a single-byte string to multibyte. */
7064 for (i = 0; i < nbytes; i++)
7065 {
7066 c = unibyte_char_to_multibyte (msg[i]);
7067 n = CHAR_STRING (c, str);
7068 insert_1_both (str, 1, n, 1, 0, 0);
7069 }
7070 }
7071 else
7072 insert_1 (s, nbytes, 1, 0, 0);
7073 }
7074
7075 return 0;
7076 }
7077
7078
7079 /* Clear messages. CURRENT_P non-zero means clear the current
7080 message. LAST_DISPLAYED_P non-zero means clear the message
7081 last displayed. */
7082
7083 void
7084 clear_message (current_p, last_displayed_p)
7085 int current_p, last_displayed_p;
7086 {
7087 if (current_p)
7088 {
7089 echo_area_buffer[0] = Qnil;
7090 message_cleared_p = 1;
7091 }
7092
7093 if (last_displayed_p)
7094 echo_area_buffer[1] = Qnil;
7095
7096 message_buf_print = 0;
7097 }
7098
7099 /* Clear garbaged frames.
7100
7101 This function is used where the old redisplay called
7102 redraw_garbaged_frames which in turn called redraw_frame which in
7103 turn called clear_frame. The call to clear_frame was a source of
7104 flickering. I believe a clear_frame is not necessary. It should
7105 suffice in the new redisplay to invalidate all current matrices,
7106 and ensure a complete redisplay of all windows. */
7107
7108 static void
7109 clear_garbaged_frames ()
7110 {
7111 if (frame_garbaged)
7112 {
7113 Lisp_Object tail, frame;
7114 int changed_count = 0;
7115
7116 FOR_EACH_FRAME (tail, frame)
7117 {
7118 struct frame *f = XFRAME (frame);
7119
7120 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7121 {
7122 if (f->resized_p)
7123 Fredraw_frame (frame);
7124 clear_current_matrices (f);
7125 changed_count++;
7126 f->garbaged = 0;
7127 f->resized_p = 0;
7128 }
7129 }
7130
7131 frame_garbaged = 0;
7132 if (changed_count)
7133 ++windows_or_buffers_changed;
7134 }
7135 }
7136
7137
7138 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7139 is non-zero update selected_frame. Value is non-zero if the
7140 mini-windows height has been changed. */
7141
7142 static int
7143 echo_area_display (update_frame_p)
7144 int update_frame_p;
7145 {
7146 Lisp_Object mini_window;
7147 struct window *w;
7148 struct frame *f;
7149 int window_height_changed_p = 0;
7150 struct frame *sf = SELECTED_FRAME ();
7151
7152 mini_window = FRAME_MINIBUF_WINDOW (sf);
7153 w = XWINDOW (mini_window);
7154 f = XFRAME (WINDOW_FRAME (w));
7155
7156 /* Don't display if frame is invisible or not yet initialized. */
7157 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7158 return 0;
7159
7160 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7161 #ifndef MAC_OS8
7162 #ifdef HAVE_WINDOW_SYSTEM
7163 /* When Emacs starts, selected_frame may be a visible terminal
7164 frame, even if we run under a window system. If we let this
7165 through, a message would be displayed on the terminal. */
7166 if (EQ (selected_frame, Vterminal_frame)
7167 && !NILP (Vwindow_system))
7168 return 0;
7169 #endif /* HAVE_WINDOW_SYSTEM */
7170 #endif
7171
7172 /* Redraw garbaged frames. */
7173 if (frame_garbaged)
7174 clear_garbaged_frames ();
7175
7176 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7177 {
7178 echo_area_window = mini_window;
7179 window_height_changed_p = display_echo_area (w);
7180 w->must_be_updated_p = 1;
7181
7182 /* Update the display, unless called from redisplay_internal.
7183 Also don't update the screen during redisplay itself. The
7184 update will happen at the end of redisplay, and an update
7185 here could cause confusion. */
7186 if (update_frame_p && !redisplaying_p)
7187 {
7188 int n = 0;
7189
7190 /* If the display update has been interrupted by pending
7191 input, update mode lines in the frame. Due to the
7192 pending input, it might have been that redisplay hasn't
7193 been called, so that mode lines above the echo area are
7194 garbaged. This looks odd, so we prevent it here. */
7195 if (!display_completed)
7196 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7197
7198 if (window_height_changed_p
7199 /* Don't do this if Emacs is shutting down. Redisplay
7200 needs to run hooks. */
7201 && !NILP (Vrun_hooks))
7202 {
7203 /* Must update other windows. Likewise as in other
7204 cases, don't let this update be interrupted by
7205 pending input. */
7206 int count = SPECPDL_INDEX ();
7207 specbind (Qredisplay_dont_pause, Qt);
7208 windows_or_buffers_changed = 1;
7209 redisplay_internal (0);
7210 unbind_to (count, Qnil);
7211 }
7212 else if (FRAME_WINDOW_P (f) && n == 0)
7213 {
7214 /* Window configuration is the same as before.
7215 Can do with a display update of the echo area,
7216 unless we displayed some mode lines. */
7217 update_single_window (w, 1);
7218 rif->flush_display (f);
7219 }
7220 else
7221 update_frame (f, 1, 1);
7222
7223 /* If cursor is in the echo area, make sure that the next
7224 redisplay displays the minibuffer, so that the cursor will
7225 be replaced with what the minibuffer wants. */
7226 if (cursor_in_echo_area)
7227 ++windows_or_buffers_changed;
7228 }
7229 }
7230 else if (!EQ (mini_window, selected_window))
7231 windows_or_buffers_changed++;
7232
7233 /* Last displayed message is now the current message. */
7234 echo_area_buffer[1] = echo_area_buffer[0];
7235
7236 /* Prevent redisplay optimization in redisplay_internal by resetting
7237 this_line_start_pos. This is done because the mini-buffer now
7238 displays the message instead of its buffer text. */
7239 if (EQ (mini_window, selected_window))
7240 CHARPOS (this_line_start_pos) = 0;
7241
7242 return window_height_changed_p;
7243 }
7244
7245
7246 \f
7247 /***********************************************************************
7248 Frame Titles
7249 ***********************************************************************/
7250
7251
7252 /* The frame title buffering code is also used by Fformat_mode_line.
7253 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7254
7255 /* A buffer for constructing frame titles in it; allocated from the
7256 heap in init_xdisp and resized as needed in store_frame_title_char. */
7257
7258 static char *frame_title_buf;
7259
7260 /* The buffer's end, and a current output position in it. */
7261
7262 static char *frame_title_buf_end;
7263 static char *frame_title_ptr;
7264
7265
7266 /* Store a single character C for the frame title in frame_title_buf.
7267 Re-allocate frame_title_buf if necessary. */
7268
7269 static void
7270 #ifdef PROTOTYPES
7271 store_frame_title_char (char c)
7272 #else
7273 store_frame_title_char (c)
7274 char c;
7275 #endif
7276 {
7277 /* If output position has reached the end of the allocated buffer,
7278 double the buffer's size. */
7279 if (frame_title_ptr == frame_title_buf_end)
7280 {
7281 int len = frame_title_ptr - frame_title_buf;
7282 int new_size = 2 * len * sizeof *frame_title_buf;
7283 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7284 frame_title_buf_end = frame_title_buf + new_size;
7285 frame_title_ptr = frame_title_buf + len;
7286 }
7287
7288 *frame_title_ptr++ = c;
7289 }
7290
7291
7292 /* Store part of a frame title in frame_title_buf, beginning at
7293 frame_title_ptr. STR is the string to store. Do not copy
7294 characters that yield more columns than PRECISION; PRECISION <= 0
7295 means copy the whole string. Pad with spaces until FIELD_WIDTH
7296 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7297 pad. Called from display_mode_element when it is used to build a
7298 frame title. */
7299
7300 static int
7301 store_frame_title (str, field_width, precision)
7302 const unsigned char *str;
7303 int field_width, precision;
7304 {
7305 int n = 0;
7306 int dummy, nbytes;
7307
7308 /* Copy at most PRECISION chars from STR. */
7309 nbytes = strlen (str);
7310 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7311 while (nbytes--)
7312 store_frame_title_char (*str++);
7313
7314 /* Fill up with spaces until FIELD_WIDTH reached. */
7315 while (field_width > 0
7316 && n < field_width)
7317 {
7318 store_frame_title_char (' ');
7319 ++n;
7320 }
7321
7322 return n;
7323 }
7324
7325 #ifdef HAVE_WINDOW_SYSTEM
7326
7327 /* Set the title of FRAME, if it has changed. The title format is
7328 Vicon_title_format if FRAME is iconified, otherwise it is
7329 frame_title_format. */
7330
7331 static void
7332 x_consider_frame_title (frame)
7333 Lisp_Object frame;
7334 {
7335 struct frame *f = XFRAME (frame);
7336
7337 if (FRAME_WINDOW_P (f)
7338 || FRAME_MINIBUF_ONLY_P (f)
7339 || f->explicit_name)
7340 {
7341 /* Do we have more than one visible frame on this X display? */
7342 Lisp_Object tail;
7343 Lisp_Object fmt;
7344 struct buffer *obuf;
7345 int len;
7346 struct it it;
7347
7348 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7349 {
7350 Lisp_Object other_frame = XCAR (tail);
7351 struct frame *tf = XFRAME (other_frame);
7352
7353 if (tf != f
7354 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7355 && !FRAME_MINIBUF_ONLY_P (tf)
7356 && !EQ (other_frame, tip_frame)
7357 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7358 break;
7359 }
7360
7361 /* Set global variable indicating that multiple frames exist. */
7362 multiple_frames = CONSP (tail);
7363
7364 /* Switch to the buffer of selected window of the frame. Set up
7365 frame_title_ptr so that display_mode_element will output into it;
7366 then display the title. */
7367 obuf = current_buffer;
7368 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7369 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7370 frame_title_ptr = frame_title_buf;
7371 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7372 NULL, DEFAULT_FACE_ID);
7373 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7374 len = frame_title_ptr - frame_title_buf;
7375 frame_title_ptr = NULL;
7376 set_buffer_internal_1 (obuf);
7377
7378 /* Set the title only if it's changed. This avoids consing in
7379 the common case where it hasn't. (If it turns out that we've
7380 already wasted too much time by walking through the list with
7381 display_mode_element, then we might need to optimize at a
7382 higher level than this.) */
7383 if (! STRINGP (f->name)
7384 || SBYTES (f->name) != len
7385 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7386 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7387 }
7388 }
7389
7390 #endif /* not HAVE_WINDOW_SYSTEM */
7391
7392
7393
7394 \f
7395 /***********************************************************************
7396 Menu Bars
7397 ***********************************************************************/
7398
7399
7400 /* Prepare for redisplay by updating menu-bar item lists when
7401 appropriate. This can call eval. */
7402
7403 void
7404 prepare_menu_bars ()
7405 {
7406 int all_windows;
7407 struct gcpro gcpro1, gcpro2;
7408 struct frame *f;
7409 Lisp_Object tooltip_frame;
7410
7411 #ifdef HAVE_WINDOW_SYSTEM
7412 tooltip_frame = tip_frame;
7413 #else
7414 tooltip_frame = Qnil;
7415 #endif
7416
7417 /* Update all frame titles based on their buffer names, etc. We do
7418 this before the menu bars so that the buffer-menu will show the
7419 up-to-date frame titles. */
7420 #ifdef HAVE_WINDOW_SYSTEM
7421 if (windows_or_buffers_changed || update_mode_lines)
7422 {
7423 Lisp_Object tail, frame;
7424
7425 FOR_EACH_FRAME (tail, frame)
7426 {
7427 f = XFRAME (frame);
7428 if (!EQ (frame, tooltip_frame)
7429 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7430 x_consider_frame_title (frame);
7431 }
7432 }
7433 #endif /* HAVE_WINDOW_SYSTEM */
7434
7435 /* Update the menu bar item lists, if appropriate. This has to be
7436 done before any actual redisplay or generation of display lines. */
7437 all_windows = (update_mode_lines
7438 || buffer_shared > 1
7439 || windows_or_buffers_changed);
7440 if (all_windows)
7441 {
7442 Lisp_Object tail, frame;
7443 int count = SPECPDL_INDEX ();
7444
7445 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7446
7447 FOR_EACH_FRAME (tail, frame)
7448 {
7449 f = XFRAME (frame);
7450
7451 /* Ignore tooltip frame. */
7452 if (EQ (frame, tooltip_frame))
7453 continue;
7454
7455 /* If a window on this frame changed size, report that to
7456 the user and clear the size-change flag. */
7457 if (FRAME_WINDOW_SIZES_CHANGED (f))
7458 {
7459 Lisp_Object functions;
7460
7461 /* Clear flag first in case we get an error below. */
7462 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7463 functions = Vwindow_size_change_functions;
7464 GCPRO2 (tail, functions);
7465
7466 while (CONSP (functions))
7467 {
7468 call1 (XCAR (functions), frame);
7469 functions = XCDR (functions);
7470 }
7471 UNGCPRO;
7472 }
7473
7474 GCPRO1 (tail);
7475 update_menu_bar (f, 0);
7476 #ifdef HAVE_WINDOW_SYSTEM
7477 update_tool_bar (f, 0);
7478 #endif
7479 UNGCPRO;
7480 }
7481
7482 unbind_to (count, Qnil);
7483 }
7484 else
7485 {
7486 struct frame *sf = SELECTED_FRAME ();
7487 update_menu_bar (sf, 1);
7488 #ifdef HAVE_WINDOW_SYSTEM
7489 update_tool_bar (sf, 1);
7490 #endif
7491 }
7492
7493 /* Motif needs this. See comment in xmenu.c. Turn it off when
7494 pending_menu_activation is not defined. */
7495 #ifdef USE_X_TOOLKIT
7496 pending_menu_activation = 0;
7497 #endif
7498 }
7499
7500
7501 /* Update the menu bar item list for frame F. This has to be done
7502 before we start to fill in any display lines, because it can call
7503 eval.
7504
7505 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7506
7507 static void
7508 update_menu_bar (f, save_match_data)
7509 struct frame *f;
7510 int save_match_data;
7511 {
7512 Lisp_Object window;
7513 register struct window *w;
7514
7515 /* If called recursively during a menu update, do nothing. This can
7516 happen when, for instance, an activate-menubar-hook causes a
7517 redisplay. */
7518 if (inhibit_menubar_update)
7519 return;
7520
7521 window = FRAME_SELECTED_WINDOW (f);
7522 w = XWINDOW (window);
7523
7524 #if 0 /* The if statement below this if statement used to include the
7525 condition !NILP (w->update_mode_line), rather than using
7526 update_mode_lines directly, and this if statement may have
7527 been added to make that condition work. Now the if
7528 statement below matches its comment, this isn't needed. */
7529 if (update_mode_lines)
7530 w->update_mode_line = Qt;
7531 #endif
7532
7533 if (FRAME_WINDOW_P (f)
7534 ?
7535 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7536 || defined (USE_GTK)
7537 FRAME_EXTERNAL_MENU_BAR (f)
7538 #else
7539 FRAME_MENU_BAR_LINES (f) > 0
7540 #endif
7541 : FRAME_MENU_BAR_LINES (f) > 0)
7542 {
7543 /* If the user has switched buffers or windows, we need to
7544 recompute to reflect the new bindings. But we'll
7545 recompute when update_mode_lines is set too; that means
7546 that people can use force-mode-line-update to request
7547 that the menu bar be recomputed. The adverse effect on
7548 the rest of the redisplay algorithm is about the same as
7549 windows_or_buffers_changed anyway. */
7550 if (windows_or_buffers_changed
7551 /* This used to test w->update_mode_line, but we believe
7552 there is no need to recompute the menu in that case. */
7553 || update_mode_lines
7554 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7555 < BUF_MODIFF (XBUFFER (w->buffer)))
7556 != !NILP (w->last_had_star))
7557 || ((!NILP (Vtransient_mark_mode)
7558 && !NILP (XBUFFER (w->buffer)->mark_active))
7559 != !NILP (w->region_showing)))
7560 {
7561 struct buffer *prev = current_buffer;
7562 int count = SPECPDL_INDEX ();
7563
7564 specbind (Qinhibit_menubar_update, Qt);
7565
7566 set_buffer_internal_1 (XBUFFER (w->buffer));
7567 if (save_match_data)
7568 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7569 if (NILP (Voverriding_local_map_menu_flag))
7570 {
7571 specbind (Qoverriding_terminal_local_map, Qnil);
7572 specbind (Qoverriding_local_map, Qnil);
7573 }
7574
7575 /* Run the Lucid hook. */
7576 safe_run_hooks (Qactivate_menubar_hook);
7577
7578 /* If it has changed current-menubar from previous value,
7579 really recompute the menu-bar from the value. */
7580 if (! NILP (Vlucid_menu_bar_dirty_flag))
7581 call0 (Qrecompute_lucid_menubar);
7582
7583 safe_run_hooks (Qmenu_bar_update_hook);
7584 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7585
7586 /* Redisplay the menu bar in case we changed it. */
7587 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7588 || defined (USE_GTK)
7589 if (FRAME_WINDOW_P (f)
7590 #if defined (MAC_OS)
7591 /* All frames on Mac OS share the same menubar. So only the
7592 selected frame should be allowed to set it. */
7593 && f == SELECTED_FRAME ()
7594 #endif
7595 )
7596 set_frame_menubar (f, 0, 0);
7597 else
7598 /* On a terminal screen, the menu bar is an ordinary screen
7599 line, and this makes it get updated. */
7600 w->update_mode_line = Qt;
7601 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7602 /* In the non-toolkit version, the menu bar is an ordinary screen
7603 line, and this makes it get updated. */
7604 w->update_mode_line = Qt;
7605 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7606
7607 unbind_to (count, Qnil);
7608 set_buffer_internal_1 (prev);
7609 }
7610 }
7611 }
7612
7613
7614 \f
7615 /***********************************************************************
7616 Tool-bars
7617 ***********************************************************************/
7618
7619 #ifdef HAVE_WINDOW_SYSTEM
7620
7621 /* Update the tool-bar item list for frame F. This has to be done
7622 before we start to fill in any display lines. Called from
7623 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7624 and restore it here. */
7625
7626 static void
7627 update_tool_bar (f, save_match_data)
7628 struct frame *f;
7629 int save_match_data;
7630 {
7631 #ifdef USE_GTK
7632 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
7633 #else
7634 int do_update = WINDOWP (f->tool_bar_window)
7635 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
7636 #endif
7637
7638 if (do_update)
7639 {
7640 Lisp_Object window;
7641 struct window *w;
7642
7643 window = FRAME_SELECTED_WINDOW (f);
7644 w = XWINDOW (window);
7645
7646 /* If the user has switched buffers or windows, we need to
7647 recompute to reflect the new bindings. But we'll
7648 recompute when update_mode_lines is set too; that means
7649 that people can use force-mode-line-update to request
7650 that the menu bar be recomputed. The adverse effect on
7651 the rest of the redisplay algorithm is about the same as
7652 windows_or_buffers_changed anyway. */
7653 if (windows_or_buffers_changed
7654 || !NILP (w->update_mode_line)
7655 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7656 < BUF_MODIFF (XBUFFER (w->buffer)))
7657 != !NILP (w->last_had_star))
7658 || ((!NILP (Vtransient_mark_mode)
7659 && !NILP (XBUFFER (w->buffer)->mark_active))
7660 != !NILP (w->region_showing)))
7661 {
7662 struct buffer *prev = current_buffer;
7663 int count = SPECPDL_INDEX ();
7664
7665 /* Set current_buffer to the buffer of the selected
7666 window of the frame, so that we get the right local
7667 keymaps. */
7668 set_buffer_internal_1 (XBUFFER (w->buffer));
7669
7670 /* Save match data, if we must. */
7671 if (save_match_data)
7672 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7673
7674 /* Make sure that we don't accidentally use bogus keymaps. */
7675 if (NILP (Voverriding_local_map_menu_flag))
7676 {
7677 specbind (Qoverriding_terminal_local_map, Qnil);
7678 specbind (Qoverriding_local_map, Qnil);
7679 }
7680
7681 /* Build desired tool-bar items from keymaps. */
7682 f->tool_bar_items
7683 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
7684
7685 /* Redisplay the tool-bar in case we changed it. */
7686 w->update_mode_line = Qt;
7687
7688 unbind_to (count, Qnil);
7689 set_buffer_internal_1 (prev);
7690 }
7691 }
7692 }
7693
7694
7695 /* Set F->desired_tool_bar_string to a Lisp string representing frame
7696 F's desired tool-bar contents. F->tool_bar_items must have
7697 been set up previously by calling prepare_menu_bars. */
7698
7699 static void
7700 build_desired_tool_bar_string (f)
7701 struct frame *f;
7702 {
7703 int i, size, size_needed;
7704 struct gcpro gcpro1, gcpro2, gcpro3;
7705 Lisp_Object image, plist, props;
7706
7707 image = plist = props = Qnil;
7708 GCPRO3 (image, plist, props);
7709
7710 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
7711 Otherwise, make a new string. */
7712
7713 /* The size of the string we might be able to reuse. */
7714 size = (STRINGP (f->desired_tool_bar_string)
7715 ? SCHARS (f->desired_tool_bar_string)
7716 : 0);
7717
7718 /* We need one space in the string for each image. */
7719 size_needed = f->n_tool_bar_items;
7720
7721 /* Reuse f->desired_tool_bar_string, if possible. */
7722 if (size < size_needed || NILP (f->desired_tool_bar_string))
7723 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7724 make_number (' '));
7725 else
7726 {
7727 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7728 Fremove_text_properties (make_number (0), make_number (size),
7729 props, f->desired_tool_bar_string);
7730 }
7731
7732 /* Put a `display' property on the string for the images to display,
7733 put a `menu_item' property on tool-bar items with a value that
7734 is the index of the item in F's tool-bar item vector. */
7735 for (i = 0; i < f->n_tool_bar_items; ++i)
7736 {
7737 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
7738
7739 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7740 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
7741 int hmargin, vmargin, relief, idx, end;
7742 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
7743
7744 /* If image is a vector, choose the image according to the
7745 button state. */
7746 image = PROP (TOOL_BAR_ITEM_IMAGES);
7747 if (VECTORP (image))
7748 {
7749 if (enabled_p)
7750 idx = (selected_p
7751 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7752 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
7753 else
7754 idx = (selected_p
7755 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7756 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
7757
7758 xassert (ASIZE (image) >= idx);
7759 image = AREF (image, idx);
7760 }
7761 else
7762 idx = -1;
7763
7764 /* Ignore invalid image specifications. */
7765 if (!valid_image_p (image))
7766 continue;
7767
7768 /* Display the tool-bar button pressed, or depressed. */
7769 plist = Fcopy_sequence (XCDR (image));
7770
7771 /* Compute margin and relief to draw. */
7772 relief = (tool_bar_button_relief >= 0
7773 ? tool_bar_button_relief
7774 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
7775 hmargin = vmargin = relief;
7776
7777 if (INTEGERP (Vtool_bar_button_margin)
7778 && XINT (Vtool_bar_button_margin) > 0)
7779 {
7780 hmargin += XFASTINT (Vtool_bar_button_margin);
7781 vmargin += XFASTINT (Vtool_bar_button_margin);
7782 }
7783 else if (CONSP (Vtool_bar_button_margin))
7784 {
7785 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7786 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7787 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7788
7789 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7790 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7791 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7792 }
7793
7794 if (auto_raise_tool_bar_buttons_p)
7795 {
7796 /* Add a `:relief' property to the image spec if the item is
7797 selected. */
7798 if (selected_p)
7799 {
7800 plist = Fplist_put (plist, QCrelief, make_number (-relief));
7801 hmargin -= relief;
7802 vmargin -= relief;
7803 }
7804 }
7805 else
7806 {
7807 /* If image is selected, display it pressed, i.e. with a
7808 negative relief. If it's not selected, display it with a
7809 raised relief. */
7810 plist = Fplist_put (plist, QCrelief,
7811 (selected_p
7812 ? make_number (-relief)
7813 : make_number (relief)));
7814 hmargin -= relief;
7815 vmargin -= relief;
7816 }
7817
7818 /* Put a margin around the image. */
7819 if (hmargin || vmargin)
7820 {
7821 if (hmargin == vmargin)
7822 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7823 else
7824 plist = Fplist_put (plist, QCmargin,
7825 Fcons (make_number (hmargin),
7826 make_number (vmargin)));
7827 }
7828
7829 /* If button is not enabled, and we don't have special images
7830 for the disabled state, make the image appear disabled by
7831 applying an appropriate algorithm to it. */
7832 if (!enabled_p && idx < 0)
7833 plist = Fplist_put (plist, QCconversion, Qdisabled);
7834
7835 /* Put a `display' text property on the string for the image to
7836 display. Put a `menu-item' property on the string that gives
7837 the start of this item's properties in the tool-bar items
7838 vector. */
7839 image = Fcons (Qimage, plist);
7840 props = list4 (Qdisplay, image,
7841 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7842
7843 /* Let the last image hide all remaining spaces in the tool bar
7844 string. The string can be longer than needed when we reuse a
7845 previous string. */
7846 if (i + 1 == f->n_tool_bar_items)
7847 end = SCHARS (f->desired_tool_bar_string);
7848 else
7849 end = i + 1;
7850 Fadd_text_properties (make_number (i), make_number (end),
7851 props, f->desired_tool_bar_string);
7852 #undef PROP
7853 }
7854
7855 UNGCPRO;
7856 }
7857
7858
7859 /* Display one line of the tool-bar of frame IT->f. */
7860
7861 static void
7862 display_tool_bar_line (it)
7863 struct it *it;
7864 {
7865 struct glyph_row *row = it->glyph_row;
7866 int max_x = it->last_visible_x;
7867 struct glyph *last;
7868
7869 prepare_desired_row (row);
7870 row->y = it->current_y;
7871
7872 /* Note that this isn't made use of if the face hasn't a box,
7873 so there's no need to check the face here. */
7874 it->start_of_box_run_p = 1;
7875
7876 while (it->current_x < max_x)
7877 {
7878 int x_before, x, n_glyphs_before, i, nglyphs;
7879
7880 /* Get the next display element. */
7881 if (!get_next_display_element (it))
7882 break;
7883
7884 /* Produce glyphs. */
7885 x_before = it->current_x;
7886 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7887 PRODUCE_GLYPHS (it);
7888
7889 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7890 i = 0;
7891 x = x_before;
7892 while (i < nglyphs)
7893 {
7894 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7895
7896 if (x + glyph->pixel_width > max_x)
7897 {
7898 /* Glyph doesn't fit on line. */
7899 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7900 it->current_x = x;
7901 goto out;
7902 }
7903
7904 ++it->hpos;
7905 x += glyph->pixel_width;
7906 ++i;
7907 }
7908
7909 /* Stop at line ends. */
7910 if (ITERATOR_AT_END_OF_LINE_P (it))
7911 break;
7912
7913 set_iterator_to_next (it, 1);
7914 }
7915
7916 out:;
7917
7918 row->displays_text_p = row->used[TEXT_AREA] != 0;
7919 extend_face_to_end_of_line (it);
7920 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7921 last->right_box_line_p = 1;
7922 if (last == row->glyphs[TEXT_AREA])
7923 last->left_box_line_p = 1;
7924 compute_line_metrics (it);
7925
7926 /* If line is empty, make it occupy the rest of the tool-bar. */
7927 if (!row->displays_text_p)
7928 {
7929 row->height = row->phys_height = it->last_visible_y - row->y;
7930 row->ascent = row->phys_ascent = 0;
7931 }
7932
7933 row->full_width_p = 1;
7934 row->continued_p = 0;
7935 row->truncated_on_left_p = 0;
7936 row->truncated_on_right_p = 0;
7937
7938 it->current_x = it->hpos = 0;
7939 it->current_y += row->height;
7940 ++it->vpos;
7941 ++it->glyph_row;
7942 }
7943
7944
7945 /* Value is the number of screen lines needed to make all tool-bar
7946 items of frame F visible. */
7947
7948 static int
7949 tool_bar_lines_needed (f)
7950 struct frame *f;
7951 {
7952 struct window *w = XWINDOW (f->tool_bar_window);
7953 struct it it;
7954
7955 /* Initialize an iterator for iteration over
7956 F->desired_tool_bar_string in the tool-bar window of frame F. */
7957 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
7958 it.first_visible_x = 0;
7959 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7960 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
7961
7962 while (!ITERATOR_AT_END_P (&it))
7963 {
7964 it.glyph_row = w->desired_matrix->rows;
7965 clear_glyph_row (it.glyph_row);
7966 display_tool_bar_line (&it);
7967 }
7968
7969 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
7970 }
7971
7972
7973 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7974 0, 1, 0,
7975 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
7976 (frame)
7977 Lisp_Object frame;
7978 {
7979 struct frame *f;
7980 struct window *w;
7981 int nlines = 0;
7982
7983 if (NILP (frame))
7984 frame = selected_frame;
7985 else
7986 CHECK_FRAME (frame);
7987 f = XFRAME (frame);
7988
7989 if (WINDOWP (f->tool_bar_window)
7990 || (w = XWINDOW (f->tool_bar_window),
7991 XFASTINT (w->height) > 0))
7992 {
7993 update_tool_bar (f, 1);
7994 if (f->n_tool_bar_items)
7995 {
7996 build_desired_tool_bar_string (f);
7997 nlines = tool_bar_lines_needed (f);
7998 }
7999 }
8000
8001 return make_number (nlines);
8002 }
8003
8004
8005 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8006 height should be changed. */
8007
8008 static int
8009 redisplay_tool_bar (f)
8010 struct frame *f;
8011 {
8012 struct window *w;
8013 struct it it;
8014 struct glyph_row *row;
8015 int change_height_p = 0;
8016
8017 #ifdef USE_GTK
8018 if (FRAME_EXTERNAL_TOOL_BAR(f))
8019 update_frame_tool_bar (f);
8020 return 0;
8021 #endif
8022
8023 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8024 do anything. This means you must start with tool-bar-lines
8025 non-zero to get the auto-sizing effect. Or in other words, you
8026 can turn off tool-bars by specifying tool-bar-lines zero. */
8027 if (!WINDOWP (f->tool_bar_window)
8028 || (w = XWINDOW (f->tool_bar_window),
8029 XFASTINT (w->height) == 0))
8030 return 0;
8031
8032 /* Set up an iterator for the tool-bar window. */
8033 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8034 it.first_visible_x = 0;
8035 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8036 row = it.glyph_row;
8037
8038 /* Build a string that represents the contents of the tool-bar. */
8039 build_desired_tool_bar_string (f);
8040 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8041
8042 /* Display as many lines as needed to display all tool-bar items. */
8043 while (it.current_y < it.last_visible_y)
8044 display_tool_bar_line (&it);
8045
8046 /* It doesn't make much sense to try scrolling in the tool-bar
8047 window, so don't do it. */
8048 w->desired_matrix->no_scrolling_p = 1;
8049 w->must_be_updated_p = 1;
8050
8051 if (auto_resize_tool_bars_p)
8052 {
8053 int nlines;
8054
8055 /* If we couldn't display everything, change the tool-bar's
8056 height. */
8057 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8058 change_height_p = 1;
8059
8060 /* If there are blank lines at the end, except for a partially
8061 visible blank line at the end that is smaller than
8062 CANON_Y_UNIT, change the tool-bar's height. */
8063 row = it.glyph_row - 1;
8064 if (!row->displays_text_p
8065 && row->height >= CANON_Y_UNIT (f))
8066 change_height_p = 1;
8067
8068 /* If row displays tool-bar items, but is partially visible,
8069 change the tool-bar's height. */
8070 if (row->displays_text_p
8071 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8072 change_height_p = 1;
8073
8074 /* Resize windows as needed by changing the `tool-bar-lines'
8075 frame parameter. */
8076 if (change_height_p
8077 && (nlines = tool_bar_lines_needed (f),
8078 nlines != XFASTINT (w->height)))
8079 {
8080 extern Lisp_Object Qtool_bar_lines;
8081 Lisp_Object frame;
8082 int old_height = XFASTINT (w->height);
8083
8084 XSETFRAME (frame, f);
8085 clear_glyph_matrix (w->desired_matrix);
8086 Fmodify_frame_parameters (frame,
8087 Fcons (Fcons (Qtool_bar_lines,
8088 make_number (nlines)),
8089 Qnil));
8090 if (XFASTINT (w->height) != old_height)
8091 fonts_changed_p = 1;
8092 }
8093 }
8094
8095 return change_height_p;
8096 }
8097
8098
8099 /* Get information about the tool-bar item which is displayed in GLYPH
8100 on frame F. Return in *PROP_IDX the index where tool-bar item
8101 properties start in F->tool_bar_items. Value is zero if
8102 GLYPH doesn't display a tool-bar item. */
8103
8104 int
8105 tool_bar_item_info (f, glyph, prop_idx)
8106 struct frame *f;
8107 struct glyph *glyph;
8108 int *prop_idx;
8109 {
8110 Lisp_Object prop;
8111 int success_p;
8112 int charpos;
8113
8114 /* This function can be called asynchronously, which means we must
8115 exclude any possibility that Fget_text_property signals an
8116 error. */
8117 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8118 charpos = max (0, charpos);
8119
8120 /* Get the text property `menu-item' at pos. The value of that
8121 property is the start index of this item's properties in
8122 F->tool_bar_items. */
8123 prop = Fget_text_property (make_number (charpos),
8124 Qmenu_item, f->current_tool_bar_string);
8125 if (INTEGERP (prop))
8126 {
8127 *prop_idx = XINT (prop);
8128 success_p = 1;
8129 }
8130 else
8131 success_p = 0;
8132
8133 return success_p;
8134 }
8135
8136 #endif /* HAVE_WINDOW_SYSTEM */
8137
8138
8139 \f
8140 /***********************************************************************
8141 Fringes
8142 ***********************************************************************/
8143
8144 #ifdef HAVE_WINDOW_SYSTEM
8145
8146 /* An arrow like this: `<-'. */
8147 static unsigned char left_bits[] = {
8148 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8149
8150 /* Right truncation arrow bitmap `->'. */
8151 static unsigned char right_bits[] = {
8152 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8153
8154 /* Marker for continued lines. */
8155 static unsigned char continued_bits[] = {
8156 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8157
8158 /* Marker for continuation lines. */
8159 static unsigned char continuation_bits[] = {
8160 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8161
8162 /* Overlay arrow bitmap. A triangular arrow. */
8163 static unsigned char ov_bits[] = {
8164 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8165
8166 /* Bitmap drawn to indicate lines not displaying text if
8167 `indicate-empty-lines' is non-nil. */
8168 static unsigned char zv_bits[] = {
8169 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8170 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8171 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8172 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8173 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8174 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8175 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8176 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8177
8178 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8179 {
8180 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8181 { 8, sizeof (left_bits), 0, left_bits },
8182 { 8, sizeof (right_bits), 0, right_bits },
8183 { 8, sizeof (continued_bits), 0, continued_bits },
8184 { 8, sizeof (continuation_bits), 0, continuation_bits },
8185 { 8, sizeof (ov_bits), 0, ov_bits },
8186 { 8, sizeof (zv_bits), 3, zv_bits }
8187 };
8188
8189
8190 /* Draw the bitmap WHICH in one of the left or right fringes of
8191 window W. ROW is the glyph row for which to display the bitmap; it
8192 determines the vertical position at which the bitmap has to be
8193 drawn. */
8194
8195 static void
8196 draw_fringe_bitmap (w, row, which, left_p)
8197 struct window *w;
8198 struct glyph_row *row;
8199 enum fringe_bitmap_type which;
8200 int left_p;
8201 {
8202 struct frame *f = XFRAME (WINDOW_FRAME (w));
8203 struct draw_fringe_bitmap_params p;
8204
8205 /* Convert row to frame coordinates. */
8206 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8207
8208 p.which = which;
8209 p.wd = fringe_bitmaps[which].width;
8210
8211 p.h = fringe_bitmaps[which].height;
8212 p.dh = (fringe_bitmaps[which].period
8213 ? (p.y % fringe_bitmaps[which].period)
8214 : 0);
8215 p.h -= p.dh;
8216 /* Clip bitmap if too high. */
8217 if (p.h > row->height)
8218 p.h = row->height;
8219
8220 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8221 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8222
8223 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8224 the fringe. */
8225 p.bx = -1;
8226 if (left_p)
8227 {
8228 if (p.wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
8229 p.wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
8230 p.x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
8231 - p.wd
8232 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - p.wd) / 2);
8233 if (p.wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > p.h)
8234 {
8235 /* If W has a vertical border to its left, don't draw over it. */
8236 int border = ((XFASTINT (w->left) > 0
8237 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8238 ? 1 : 0);
8239 p.bx = (window_box_left (w, -1)
8240 - FRAME_X_LEFT_FRINGE_WIDTH (f)
8241 + border);
8242 p.nx = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
8243 }
8244 }
8245 else
8246 {
8247 if (p.wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
8248 p.wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8249 p.x = (window_box_right (w, -1)
8250 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - p.wd) / 2);
8251 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8252 the fringe. */
8253 if (p.wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > p.h)
8254 {
8255 p.bx = window_box_right (w, -1);
8256 p.nx = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8257 }
8258 }
8259
8260 if (p.bx >= 0)
8261 {
8262 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8263
8264 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8265 p.ny = row->visible_height;
8266 }
8267
8268 /* Adjust y to the offset in the row to start drawing the bitmap. */
8269 p.y += (row->height - p.h) / 2;
8270
8271 rif->draw_fringe_bitmap (w, row, &p);
8272 }
8273
8274 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
8275 function with input blocked. */
8276
8277 void
8278 draw_row_fringe_bitmaps (w, row)
8279 struct window *w;
8280 struct glyph_row *row;
8281 {
8282 struct frame *f = XFRAME (w->frame);
8283 enum fringe_bitmap_type bitmap;
8284
8285 xassert (interrupt_input_blocked);
8286
8287 /* If row is completely invisible, because of vscrolling, we
8288 don't have to draw anything. */
8289 if (row->visible_height <= 0)
8290 return;
8291
8292 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
8293 {
8294 /* Decide which bitmap to draw in the left fringe. */
8295 if (row->overlay_arrow_p)
8296 bitmap = OVERLAY_ARROW_BITMAP;
8297 else if (row->truncated_on_left_p)
8298 bitmap = LEFT_TRUNCATION_BITMAP;
8299 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
8300 bitmap = CONTINUATION_LINE_BITMAP;
8301 else if (row->indicate_empty_line_p)
8302 bitmap = ZV_LINE_BITMAP;
8303 else
8304 bitmap = NO_FRINGE_BITMAP;
8305
8306 draw_fringe_bitmap (w, row, bitmap, 1);
8307 }
8308
8309 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
8310 {
8311 /* Decide which bitmap to draw in the right fringe. */
8312 if (row->truncated_on_right_p)
8313 bitmap = RIGHT_TRUNCATION_BITMAP;
8314 else if (row->continued_p)
8315 bitmap = CONTINUED_LINE_BITMAP;
8316 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
8317 bitmap = ZV_LINE_BITMAP;
8318 else
8319 bitmap = NO_FRINGE_BITMAP;
8320
8321 draw_fringe_bitmap (w, row, bitmap, 0);
8322 }
8323 }
8324
8325
8326 /* Compute actual fringe widths */
8327
8328 void
8329 compute_fringe_widths (f, redraw)
8330 struct frame *f;
8331 int redraw;
8332 {
8333 int o_left = FRAME_X_LEFT_FRINGE_WIDTH (f);
8334 int o_right = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8335 int o_cols = FRAME_X_FRINGE_COLS (f);
8336
8337 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
8338 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
8339 int left_fringe_width, right_fringe_width;
8340
8341 if (!NILP (left_fringe))
8342 left_fringe = Fcdr (left_fringe);
8343 if (!NILP (right_fringe))
8344 right_fringe = Fcdr (right_fringe);
8345
8346 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
8347 XINT (left_fringe));
8348 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
8349 XINT (right_fringe));
8350
8351 if (left_fringe_width || right_fringe_width)
8352 {
8353 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
8354 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
8355 int conf_wid = left_wid + right_wid;
8356 int font_wid = FONT_WIDTH (FRAME_FONT (f));
8357 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
8358 int real_wid = cols * font_wid;
8359 if (left_wid && right_wid)
8360 {
8361 if (left_fringe_width < 0)
8362 {
8363 /* Left fringe width is fixed, adjust right fringe if necessary */
8364 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid;
8365 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
8366 }
8367 else if (right_fringe_width < 0)
8368 {
8369 /* Right fringe width is fixed, adjust left fringe if necessary */
8370 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
8371 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid;
8372 }
8373 else
8374 {
8375 /* Adjust both fringes with an equal amount.
8376 Note that we are doing integer arithmetic here, so don't
8377 lose a pixel if the total width is an odd number. */
8378 int fill = real_wid - conf_wid;
8379 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
8380 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
8381 }
8382 }
8383 else if (left_fringe_width)
8384 {
8385 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid;
8386 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
8387 }
8388 else
8389 {
8390 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
8391 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid;
8392 }
8393 FRAME_X_FRINGE_COLS (f) = cols;
8394 FRAME_X_FRINGE_WIDTH (f) = real_wid;
8395 }
8396 else
8397 {
8398 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
8399 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
8400 FRAME_X_FRINGE_COLS (f) = 0;
8401 FRAME_X_FRINGE_WIDTH (f) = 0;
8402 }
8403
8404 if (redraw && FRAME_VISIBLE_P (f))
8405 if (o_left != FRAME_X_LEFT_FRINGE_WIDTH (f) ||
8406 o_right != FRAME_X_RIGHT_FRINGE_WIDTH (f) ||
8407 o_cols != FRAME_X_FRINGE_COLS (f))
8408 redraw_frame (f);
8409 }
8410
8411 #endif /* HAVE_WINDOW_SYSTEM */
8412
8413
8414 \f
8415 /************************************************************************
8416 Horizontal scrolling
8417 ************************************************************************/
8418
8419 static int hscroll_window_tree P_ ((Lisp_Object));
8420 static int hscroll_windows P_ ((Lisp_Object));
8421
8422 /* For all leaf windows in the window tree rooted at WINDOW, set their
8423 hscroll value so that PT is (i) visible in the window, and (ii) so
8424 that it is not within a certain margin at the window's left and
8425 right border. Value is non-zero if any window's hscroll has been
8426 changed. */
8427
8428 static int
8429 hscroll_window_tree (window)
8430 Lisp_Object window;
8431 {
8432 int hscrolled_p = 0;
8433 int hscroll_relative_p = FLOATP (Vhscroll_step);
8434 int hscroll_step_abs = 0;
8435 double hscroll_step_rel = 0;
8436
8437 if (hscroll_relative_p)
8438 {
8439 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
8440 if (hscroll_step_rel < 0)
8441 {
8442 hscroll_relative_p = 0;
8443 hscroll_step_abs = 0;
8444 }
8445 }
8446 else if (INTEGERP (Vhscroll_step))
8447 {
8448 hscroll_step_abs = XINT (Vhscroll_step);
8449 if (hscroll_step_abs < 0)
8450 hscroll_step_abs = 0;
8451 }
8452 else
8453 hscroll_step_abs = 0;
8454
8455 while (WINDOWP (window))
8456 {
8457 struct window *w = XWINDOW (window);
8458
8459 if (WINDOWP (w->hchild))
8460 hscrolled_p |= hscroll_window_tree (w->hchild);
8461 else if (WINDOWP (w->vchild))
8462 hscrolled_p |= hscroll_window_tree (w->vchild);
8463 else if (w->cursor.vpos >= 0)
8464 {
8465 int h_margin, text_area_x, text_area_y;
8466 int text_area_width, text_area_height;
8467 struct glyph_row *current_cursor_row
8468 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
8469 struct glyph_row *desired_cursor_row
8470 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
8471 struct glyph_row *cursor_row
8472 = (desired_cursor_row->enabled_p
8473 ? desired_cursor_row
8474 : current_cursor_row);
8475
8476 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
8477 &text_area_width, &text_area_height);
8478
8479 /* Scroll when cursor is inside this scroll margin. */
8480 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
8481
8482 if ((XFASTINT (w->hscroll)
8483 && w->cursor.x <= h_margin)
8484 || (cursor_row->enabled_p
8485 && cursor_row->truncated_on_right_p
8486 && (w->cursor.x >= text_area_width - h_margin)))
8487 {
8488 struct it it;
8489 int hscroll;
8490 struct buffer *saved_current_buffer;
8491 int pt;
8492 int wanted_x;
8493
8494 /* Find point in a display of infinite width. */
8495 saved_current_buffer = current_buffer;
8496 current_buffer = XBUFFER (w->buffer);
8497
8498 if (w == XWINDOW (selected_window))
8499 pt = BUF_PT (current_buffer);
8500 else
8501 {
8502 pt = marker_position (w->pointm);
8503 pt = max (BEGV, pt);
8504 pt = min (ZV, pt);
8505 }
8506
8507 /* Move iterator to pt starting at cursor_row->start in
8508 a line with infinite width. */
8509 init_to_row_start (&it, w, cursor_row);
8510 it.last_visible_x = INFINITY;
8511 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
8512 current_buffer = saved_current_buffer;
8513
8514 /* Position cursor in window. */
8515 if (!hscroll_relative_p && hscroll_step_abs == 0)
8516 hscroll = max (0, it.current_x - text_area_width / 2)
8517 / CANON_X_UNIT (it.f);
8518 else if (w->cursor.x >= text_area_width - h_margin)
8519 {
8520 if (hscroll_relative_p)
8521 wanted_x = text_area_width * (1 - hscroll_step_rel)
8522 - h_margin;
8523 else
8524 wanted_x = text_area_width
8525 - hscroll_step_abs * CANON_X_UNIT (it.f)
8526 - h_margin;
8527 hscroll
8528 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8529 }
8530 else
8531 {
8532 if (hscroll_relative_p)
8533 wanted_x = text_area_width * hscroll_step_rel
8534 + h_margin;
8535 else
8536 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
8537 + h_margin;
8538 hscroll
8539 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
8540 }
8541 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
8542
8543 /* Don't call Fset_window_hscroll if value hasn't
8544 changed because it will prevent redisplay
8545 optimizations. */
8546 if (XFASTINT (w->hscroll) != hscroll)
8547 {
8548 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
8549 w->hscroll = make_number (hscroll);
8550 hscrolled_p = 1;
8551 }
8552 }
8553 }
8554
8555 window = w->next;
8556 }
8557
8558 /* Value is non-zero if hscroll of any leaf window has been changed. */
8559 return hscrolled_p;
8560 }
8561
8562
8563 /* Set hscroll so that cursor is visible and not inside horizontal
8564 scroll margins for all windows in the tree rooted at WINDOW. See
8565 also hscroll_window_tree above. Value is non-zero if any window's
8566 hscroll has been changed. If it has, desired matrices on the frame
8567 of WINDOW are cleared. */
8568
8569 static int
8570 hscroll_windows (window)
8571 Lisp_Object window;
8572 {
8573 int hscrolled_p;
8574
8575 if (automatic_hscrolling_p)
8576 {
8577 hscrolled_p = hscroll_window_tree (window);
8578 if (hscrolled_p)
8579 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
8580 }
8581 else
8582 hscrolled_p = 0;
8583 return hscrolled_p;
8584 }
8585
8586
8587 \f
8588 /************************************************************************
8589 Redisplay
8590 ************************************************************************/
8591
8592 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
8593 to a non-zero value. This is sometimes handy to have in a debugger
8594 session. */
8595
8596 #if GLYPH_DEBUG
8597
8598 /* First and last unchanged row for try_window_id. */
8599
8600 int debug_first_unchanged_at_end_vpos;
8601 int debug_last_unchanged_at_beg_vpos;
8602
8603 /* Delta vpos and y. */
8604
8605 int debug_dvpos, debug_dy;
8606
8607 /* Delta in characters and bytes for try_window_id. */
8608
8609 int debug_delta, debug_delta_bytes;
8610
8611 /* Values of window_end_pos and window_end_vpos at the end of
8612 try_window_id. */
8613
8614 EMACS_INT debug_end_pos, debug_end_vpos;
8615
8616 /* Append a string to W->desired_matrix->method. FMT is a printf
8617 format string. A1...A9 are a supplement for a variable-length
8618 argument list. If trace_redisplay_p is non-zero also printf the
8619 resulting string to stderr. */
8620
8621 static void
8622 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
8623 struct window *w;
8624 char *fmt;
8625 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
8626 {
8627 char buffer[512];
8628 char *method = w->desired_matrix->method;
8629 int len = strlen (method);
8630 int size = sizeof w->desired_matrix->method;
8631 int remaining = size - len - 1;
8632
8633 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
8634 if (len && remaining)
8635 {
8636 method[len] = '|';
8637 --remaining, ++len;
8638 }
8639
8640 strncpy (method + len, buffer, remaining);
8641
8642 if (trace_redisplay_p)
8643 fprintf (stderr, "%p (%s): %s\n",
8644 w,
8645 ((BUFFERP (w->buffer)
8646 && STRINGP (XBUFFER (w->buffer)->name))
8647 ? (char *) SDATA (XBUFFER (w->buffer)->name)
8648 : "no buffer"),
8649 buffer);
8650 }
8651
8652 #endif /* GLYPH_DEBUG */
8653
8654
8655 /* Value is non-zero if all changes in window W, which displays
8656 current_buffer, are in the text between START and END. START is a
8657 buffer position, END is given as a distance from Z. Used in
8658 redisplay_internal for display optimization. */
8659
8660 static INLINE int
8661 text_outside_line_unchanged_p (w, start, end)
8662 struct window *w;
8663 int start, end;
8664 {
8665 int unchanged_p = 1;
8666
8667 /* If text or overlays have changed, see where. */
8668 if (XFASTINT (w->last_modified) < MODIFF
8669 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8670 {
8671 /* Gap in the line? */
8672 if (GPT < start || Z - GPT < end)
8673 unchanged_p = 0;
8674
8675 /* Changes start in front of the line, or end after it? */
8676 if (unchanged_p
8677 && (BEG_UNCHANGED < start - 1
8678 || END_UNCHANGED < end))
8679 unchanged_p = 0;
8680
8681 /* If selective display, can't optimize if changes start at the
8682 beginning of the line. */
8683 if (unchanged_p
8684 && INTEGERP (current_buffer->selective_display)
8685 && XINT (current_buffer->selective_display) > 0
8686 && (BEG_UNCHANGED < start || GPT <= start))
8687 unchanged_p = 0;
8688
8689 /* If there are overlays at the start or end of the line, these
8690 may have overlay strings with newlines in them. A change at
8691 START, for instance, may actually concern the display of such
8692 overlay strings as well, and they are displayed on different
8693 lines. So, quickly rule out this case. (For the future, it
8694 might be desirable to implement something more telling than
8695 just BEG/END_UNCHANGED.) */
8696 if (unchanged_p)
8697 {
8698 if (BEG + BEG_UNCHANGED == start
8699 && overlay_touches_p (start))
8700 unchanged_p = 0;
8701 if (END_UNCHANGED == end
8702 && overlay_touches_p (Z - end))
8703 unchanged_p = 0;
8704 }
8705 }
8706
8707 return unchanged_p;
8708 }
8709
8710
8711 /* Do a frame update, taking possible shortcuts into account. This is
8712 the main external entry point for redisplay.
8713
8714 If the last redisplay displayed an echo area message and that message
8715 is no longer requested, we clear the echo area or bring back the
8716 mini-buffer if that is in use. */
8717
8718 void
8719 redisplay ()
8720 {
8721 redisplay_internal (0);
8722 }
8723
8724
8725 /* Return 1 if point moved out of or into a composition. Otherwise
8726 return 0. PREV_BUF and PREV_PT are the last point buffer and
8727 position. BUF and PT are the current point buffer and position. */
8728
8729 int
8730 check_point_in_composition (prev_buf, prev_pt, buf, pt)
8731 struct buffer *prev_buf, *buf;
8732 int prev_pt, pt;
8733 {
8734 int start, end;
8735 Lisp_Object prop;
8736 Lisp_Object buffer;
8737
8738 XSETBUFFER (buffer, buf);
8739 /* Check a composition at the last point if point moved within the
8740 same buffer. */
8741 if (prev_buf == buf)
8742 {
8743 if (prev_pt == pt)
8744 /* Point didn't move. */
8745 return 0;
8746
8747 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8748 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8749 && COMPOSITION_VALID_P (start, end, prop)
8750 && start < prev_pt && end > prev_pt)
8751 /* The last point was within the composition. Return 1 iff
8752 point moved out of the composition. */
8753 return (pt <= start || pt >= end);
8754 }
8755
8756 /* Check a composition at the current point. */
8757 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8758 && find_composition (pt, -1, &start, &end, &prop, buffer)
8759 && COMPOSITION_VALID_P (start, end, prop)
8760 && start < pt && end > pt);
8761 }
8762
8763
8764 /* Reconsider the setting of B->clip_changed which is displayed
8765 in window W. */
8766
8767 static INLINE void
8768 reconsider_clip_changes (w, b)
8769 struct window *w;
8770 struct buffer *b;
8771 {
8772 if (b->clip_changed
8773 && !NILP (w->window_end_valid)
8774 && w->current_matrix->buffer == b
8775 && w->current_matrix->zv == BUF_ZV (b)
8776 && w->current_matrix->begv == BUF_BEGV (b))
8777 b->clip_changed = 0;
8778
8779 /* If display wasn't paused, and W is not a tool bar window, see if
8780 point has been moved into or out of a composition. In that case,
8781 we set b->clip_changed to 1 to force updating the screen. If
8782 b->clip_changed has already been set to 1, we can skip this
8783 check. */
8784 if (!b->clip_changed
8785 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8786 {
8787 int pt;
8788
8789 if (w == XWINDOW (selected_window))
8790 pt = BUF_PT (current_buffer);
8791 else
8792 pt = marker_position (w->pointm);
8793
8794 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
8795 || pt != XINT (w->last_point))
8796 && check_point_in_composition (w->current_matrix->buffer,
8797 XINT (w->last_point),
8798 XBUFFER (w->buffer), pt))
8799 b->clip_changed = 1;
8800 }
8801 }
8802 \f
8803 #define STOP_POLLING \
8804 do { if (! polling_stopped_here) stop_polling (); \
8805 polling_stopped_here = 1; } while (0)
8806
8807 #define RESUME_POLLING \
8808 do { if (polling_stopped_here) start_polling (); \
8809 polling_stopped_here = 0; } while (0)
8810
8811
8812 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8813 response to any user action; therefore, we should preserve the echo
8814 area. (Actually, our caller does that job.) Perhaps in the future
8815 avoid recentering windows if it is not necessary; currently that
8816 causes some problems. */
8817
8818 static void
8819 redisplay_internal (preserve_echo_area)
8820 int preserve_echo_area;
8821 {
8822 struct window *w = XWINDOW (selected_window);
8823 struct frame *f = XFRAME (w->frame);
8824 int pause;
8825 int must_finish = 0;
8826 struct text_pos tlbufpos, tlendpos;
8827 int number_of_visible_frames;
8828 int count;
8829 struct frame *sf = SELECTED_FRAME ();
8830 int polling_stopped_here = 0;
8831
8832 /* Non-zero means redisplay has to consider all windows on all
8833 frames. Zero means, only selected_window is considered. */
8834 int consider_all_windows_p;
8835
8836 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8837
8838 /* No redisplay if running in batch mode or frame is not yet fully
8839 initialized, or redisplay is explicitly turned off by setting
8840 Vinhibit_redisplay. */
8841 if (noninteractive
8842 || !NILP (Vinhibit_redisplay)
8843 || !f->glyphs_initialized_p)
8844 return;
8845
8846 /* The flag redisplay_performed_directly_p is set by
8847 direct_output_for_insert when it already did the whole screen
8848 update necessary. */
8849 if (redisplay_performed_directly_p)
8850 {
8851 redisplay_performed_directly_p = 0;
8852 if (!hscroll_windows (selected_window))
8853 return;
8854 }
8855
8856 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
8857 if (popup_activated ())
8858 return;
8859 #endif
8860
8861 /* I don't think this happens but let's be paranoid. */
8862 if (redisplaying_p)
8863 return;
8864
8865 /* Record a function that resets redisplaying_p to its old value
8866 when we leave this function. */
8867 count = SPECPDL_INDEX ();
8868 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8869 ++redisplaying_p;
8870 specbind (Qinhibit_free_realized_faces, Qnil);
8871
8872 retry:
8873 pause = 0;
8874 reconsider_clip_changes (w, current_buffer);
8875
8876 /* If new fonts have been loaded that make a glyph matrix adjustment
8877 necessary, do it. */
8878 if (fonts_changed_p)
8879 {
8880 adjust_glyphs (NULL);
8881 ++windows_or_buffers_changed;
8882 fonts_changed_p = 0;
8883 }
8884
8885 /* If face_change_count is non-zero, init_iterator will free all
8886 realized faces, which includes the faces referenced from current
8887 matrices. So, we can't reuse current matrices in this case. */
8888 if (face_change_count)
8889 ++windows_or_buffers_changed;
8890
8891 if (! FRAME_WINDOW_P (sf)
8892 && previous_terminal_frame != sf)
8893 {
8894 /* Since frames on an ASCII terminal share the same display
8895 area, displaying a different frame means redisplay the whole
8896 thing. */
8897 windows_or_buffers_changed++;
8898 SET_FRAME_GARBAGED (sf);
8899 XSETFRAME (Vterminal_frame, sf);
8900 }
8901 previous_terminal_frame = sf;
8902
8903 /* Set the visible flags for all frames. Do this before checking
8904 for resized or garbaged frames; they want to know if their frames
8905 are visible. See the comment in frame.h for
8906 FRAME_SAMPLE_VISIBILITY. */
8907 {
8908 Lisp_Object tail, frame;
8909
8910 number_of_visible_frames = 0;
8911
8912 FOR_EACH_FRAME (tail, frame)
8913 {
8914 struct frame *f = XFRAME (frame);
8915
8916 FRAME_SAMPLE_VISIBILITY (f);
8917 if (FRAME_VISIBLE_P (f))
8918 ++number_of_visible_frames;
8919 clear_desired_matrices (f);
8920 }
8921 }
8922
8923 /* Notice any pending interrupt request to change frame size. */
8924 do_pending_window_change (1);
8925
8926 /* Clear frames marked as garbaged. */
8927 if (frame_garbaged)
8928 clear_garbaged_frames ();
8929
8930 /* Build menubar and tool-bar items. */
8931 prepare_menu_bars ();
8932
8933 if (windows_or_buffers_changed)
8934 update_mode_lines++;
8935
8936 /* Detect case that we need to write or remove a star in the mode line. */
8937 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
8938 {
8939 w->update_mode_line = Qt;
8940 if (buffer_shared > 1)
8941 update_mode_lines++;
8942 }
8943
8944 /* If %c is in the mode line, update it if needed. */
8945 if (!NILP (w->column_number_displayed)
8946 /* This alternative quickly identifies a common case
8947 where no change is needed. */
8948 && !(PT == XFASTINT (w->last_point)
8949 && XFASTINT (w->last_modified) >= MODIFF
8950 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
8951 && (XFASTINT (w->column_number_displayed)
8952 != (int) current_column ())) /* iftc */
8953 w->update_mode_line = Qt;
8954
8955 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
8956
8957 /* The variable buffer_shared is set in redisplay_window and
8958 indicates that we redisplay a buffer in different windows. See
8959 there. */
8960 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
8961 || cursor_type_changed);
8962
8963 /* If specs for an arrow have changed, do thorough redisplay
8964 to ensure we remove any arrow that should no longer exist. */
8965 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
8966 || ! EQ (Voverlay_arrow_string, last_arrow_string))
8967 consider_all_windows_p = windows_or_buffers_changed = 1;
8968
8969 /* Normally the message* functions will have already displayed and
8970 updated the echo area, but the frame may have been trashed, or
8971 the update may have been preempted, so display the echo area
8972 again here. Checking message_cleared_p captures the case that
8973 the echo area should be cleared. */
8974 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
8975 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
8976 || (message_cleared_p
8977 && minibuf_level == 0
8978 /* If the mini-window is currently selected, this means the
8979 echo-area doesn't show through. */
8980 && !MINI_WINDOW_P (XWINDOW (selected_window))))
8981 {
8982 int window_height_changed_p = echo_area_display (0);
8983 must_finish = 1;
8984
8985 /* If we don't display the current message, don't clear the
8986 message_cleared_p flag, because, if we did, we wouldn't clear
8987 the echo area in the next redisplay which doesn't preserve
8988 the echo area. */
8989 if (!display_last_displayed_message_p)
8990 message_cleared_p = 0;
8991
8992 if (fonts_changed_p)
8993 goto retry;
8994 else if (window_height_changed_p)
8995 {
8996 consider_all_windows_p = 1;
8997 ++update_mode_lines;
8998 ++windows_or_buffers_changed;
8999
9000 /* If window configuration was changed, frames may have been
9001 marked garbaged. Clear them or we will experience
9002 surprises wrt scrolling. */
9003 if (frame_garbaged)
9004 clear_garbaged_frames ();
9005 }
9006 }
9007 else if (EQ (selected_window, minibuf_window)
9008 && (current_buffer->clip_changed
9009 || XFASTINT (w->last_modified) < MODIFF
9010 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9011 && resize_mini_window (w, 0))
9012 {
9013 /* Resized active mini-window to fit the size of what it is
9014 showing if its contents might have changed. */
9015 must_finish = 1;
9016 consider_all_windows_p = 1;
9017 ++windows_or_buffers_changed;
9018 ++update_mode_lines;
9019
9020 /* If window configuration was changed, frames may have been
9021 marked garbaged. Clear them or we will experience
9022 surprises wrt scrolling. */
9023 if (frame_garbaged)
9024 clear_garbaged_frames ();
9025 }
9026
9027
9028 /* If showing the region, and mark has changed, we must redisplay
9029 the whole window. The assignment to this_line_start_pos prevents
9030 the optimization directly below this if-statement. */
9031 if (((!NILP (Vtransient_mark_mode)
9032 && !NILP (XBUFFER (w->buffer)->mark_active))
9033 != !NILP (w->region_showing))
9034 || (!NILP (w->region_showing)
9035 && !EQ (w->region_showing,
9036 Fmarker_position (XBUFFER (w->buffer)->mark))))
9037 CHARPOS (this_line_start_pos) = 0;
9038
9039 /* Optimize the case that only the line containing the cursor in the
9040 selected window has changed. Variables starting with this_ are
9041 set in display_line and record information about the line
9042 containing the cursor. */
9043 tlbufpos = this_line_start_pos;
9044 tlendpos = this_line_end_pos;
9045 if (!consider_all_windows_p
9046 && CHARPOS (tlbufpos) > 0
9047 && NILP (w->update_mode_line)
9048 && !current_buffer->clip_changed
9049 && !current_buffer->prevent_redisplay_optimizations_p
9050 && FRAME_VISIBLE_P (XFRAME (w->frame))
9051 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9052 /* Make sure recorded data applies to current buffer, etc. */
9053 && this_line_buffer == current_buffer
9054 && current_buffer == XBUFFER (w->buffer)
9055 && NILP (w->force_start)
9056 && NILP (w->optional_new_start)
9057 /* Point must be on the line that we have info recorded about. */
9058 && PT >= CHARPOS (tlbufpos)
9059 && PT <= Z - CHARPOS (tlendpos)
9060 /* All text outside that line, including its final newline,
9061 must be unchanged */
9062 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9063 CHARPOS (tlendpos)))
9064 {
9065 if (CHARPOS (tlbufpos) > BEGV
9066 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9067 && (CHARPOS (tlbufpos) == ZV
9068 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9069 /* Former continuation line has disappeared by becoming empty */
9070 goto cancel;
9071 else if (XFASTINT (w->last_modified) < MODIFF
9072 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9073 || MINI_WINDOW_P (w))
9074 {
9075 /* We have to handle the case of continuation around a
9076 wide-column character (See the comment in indent.c around
9077 line 885).
9078
9079 For instance, in the following case:
9080
9081 -------- Insert --------
9082 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9083 J_I_ ==> J_I_ `^^' are cursors.
9084 ^^ ^^
9085 -------- --------
9086
9087 As we have to redraw the line above, we should goto cancel. */
9088
9089 struct it it;
9090 int line_height_before = this_line_pixel_height;
9091
9092 /* Note that start_display will handle the case that the
9093 line starting at tlbufpos is a continuation lines. */
9094 start_display (&it, w, tlbufpos);
9095
9096 /* Implementation note: It this still necessary? */
9097 if (it.current_x != this_line_start_x)
9098 goto cancel;
9099
9100 TRACE ((stderr, "trying display optimization 1\n"));
9101 w->cursor.vpos = -1;
9102 overlay_arrow_seen = 0;
9103 it.vpos = this_line_vpos;
9104 it.current_y = this_line_y;
9105 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9106 display_line (&it);
9107
9108 /* If line contains point, is not continued,
9109 and ends at same distance from eob as before, we win */
9110 if (w->cursor.vpos >= 0
9111 /* Line is not continued, otherwise this_line_start_pos
9112 would have been set to 0 in display_line. */
9113 && CHARPOS (this_line_start_pos)
9114 /* Line ends as before. */
9115 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9116 /* Line has same height as before. Otherwise other lines
9117 would have to be shifted up or down. */
9118 && this_line_pixel_height == line_height_before)
9119 {
9120 /* If this is not the window's last line, we must adjust
9121 the charstarts of the lines below. */
9122 if (it.current_y < it.last_visible_y)
9123 {
9124 struct glyph_row *row
9125 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9126 int delta, delta_bytes;
9127
9128 if (Z - CHARPOS (tlendpos) == ZV)
9129 {
9130 /* This line ends at end of (accessible part of)
9131 buffer. There is no newline to count. */
9132 delta = (Z
9133 - CHARPOS (tlendpos)
9134 - MATRIX_ROW_START_CHARPOS (row));
9135 delta_bytes = (Z_BYTE
9136 - BYTEPOS (tlendpos)
9137 - MATRIX_ROW_START_BYTEPOS (row));
9138 }
9139 else
9140 {
9141 /* This line ends in a newline. Must take
9142 account of the newline and the rest of the
9143 text that follows. */
9144 delta = (Z
9145 - CHARPOS (tlendpos)
9146 - MATRIX_ROW_START_CHARPOS (row));
9147 delta_bytes = (Z_BYTE
9148 - BYTEPOS (tlendpos)
9149 - MATRIX_ROW_START_BYTEPOS (row));
9150 }
9151
9152 increment_matrix_positions (w->current_matrix,
9153 this_line_vpos + 1,
9154 w->current_matrix->nrows,
9155 delta, delta_bytes);
9156 }
9157
9158 /* If this row displays text now but previously didn't,
9159 or vice versa, w->window_end_vpos may have to be
9160 adjusted. */
9161 if ((it.glyph_row - 1)->displays_text_p)
9162 {
9163 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9164 XSETINT (w->window_end_vpos, this_line_vpos);
9165 }
9166 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9167 && this_line_vpos > 0)
9168 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9169 w->window_end_valid = Qnil;
9170
9171 /* Update hint: No need to try to scroll in update_window. */
9172 w->desired_matrix->no_scrolling_p = 1;
9173
9174 #if GLYPH_DEBUG
9175 *w->desired_matrix->method = 0;
9176 debug_method_add (w, "optimization 1");
9177 #endif
9178 goto update;
9179 }
9180 else
9181 goto cancel;
9182 }
9183 else if (/* Cursor position hasn't changed. */
9184 PT == XFASTINT (w->last_point)
9185 /* Make sure the cursor was last displayed
9186 in this window. Otherwise we have to reposition it. */
9187 && 0 <= w->cursor.vpos
9188 && XINT (w->height) > w->cursor.vpos)
9189 {
9190 if (!must_finish)
9191 {
9192 do_pending_window_change (1);
9193
9194 /* We used to always goto end_of_redisplay here, but this
9195 isn't enough if we have a blinking cursor. */
9196 if (w->cursor_off_p == w->last_cursor_off_p)
9197 goto end_of_redisplay;
9198 }
9199 goto update;
9200 }
9201 /* If highlighting the region, or if the cursor is in the echo area,
9202 then we can't just move the cursor. */
9203 else if (! (!NILP (Vtransient_mark_mode)
9204 && !NILP (current_buffer->mark_active))
9205 && (EQ (selected_window, current_buffer->last_selected_window)
9206 || highlight_nonselected_windows)
9207 && NILP (w->region_showing)
9208 && NILP (Vshow_trailing_whitespace)
9209 && !cursor_in_echo_area)
9210 {
9211 struct it it;
9212 struct glyph_row *row;
9213
9214 /* Skip from tlbufpos to PT and see where it is. Note that
9215 PT may be in invisible text. If so, we will end at the
9216 next visible position. */
9217 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9218 NULL, DEFAULT_FACE_ID);
9219 it.current_x = this_line_start_x;
9220 it.current_y = this_line_y;
9221 it.vpos = this_line_vpos;
9222
9223 /* The call to move_it_to stops in front of PT, but
9224 moves over before-strings. */
9225 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9226
9227 if (it.vpos == this_line_vpos
9228 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9229 row->enabled_p))
9230 {
9231 xassert (this_line_vpos == it.vpos);
9232 xassert (this_line_y == it.current_y);
9233 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9234 #if GLYPH_DEBUG
9235 *w->desired_matrix->method = 0;
9236 debug_method_add (w, "optimization 3");
9237 #endif
9238 goto update;
9239 }
9240 else
9241 goto cancel;
9242 }
9243
9244 cancel:
9245 /* Text changed drastically or point moved off of line. */
9246 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9247 }
9248
9249 CHARPOS (this_line_start_pos) = 0;
9250 consider_all_windows_p |= buffer_shared > 1;
9251 ++clear_face_cache_count;
9252
9253
9254 /* Build desired matrices, and update the display. If
9255 consider_all_windows_p is non-zero, do it for all windows on all
9256 frames. Otherwise do it for selected_window, only. */
9257
9258 if (consider_all_windows_p)
9259 {
9260 Lisp_Object tail, frame;
9261 int i, n = 0, size = 50;
9262 struct frame **updated
9263 = (struct frame **) alloca (size * sizeof *updated);
9264
9265 /* Clear the face cache eventually. */
9266 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9267 {
9268 clear_face_cache (0);
9269 clear_face_cache_count = 0;
9270 }
9271
9272 /* Recompute # windows showing selected buffer. This will be
9273 incremented each time such a window is displayed. */
9274 buffer_shared = 0;
9275
9276 FOR_EACH_FRAME (tail, frame)
9277 {
9278 struct frame *f = XFRAME (frame);
9279
9280 if (FRAME_WINDOW_P (f) || f == sf)
9281 {
9282 #ifdef HAVE_WINDOW_SYSTEM
9283 if (clear_face_cache_count % 50 == 0
9284 && FRAME_WINDOW_P (f))
9285 clear_image_cache (f, 0);
9286 #endif /* HAVE_WINDOW_SYSTEM */
9287
9288 /* Mark all the scroll bars to be removed; we'll redeem
9289 the ones we want when we redisplay their windows. */
9290 if (condemn_scroll_bars_hook)
9291 condemn_scroll_bars_hook (f);
9292
9293 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9294 redisplay_windows (FRAME_ROOT_WINDOW (f));
9295
9296 /* Any scroll bars which redisplay_windows should have
9297 nuked should now go away. */
9298 if (judge_scroll_bars_hook)
9299 judge_scroll_bars_hook (f);
9300
9301 /* If fonts changed, display again. */
9302 /* ??? rms: I suspect it is a mistake to jump all the way
9303 back to retry here. It should just retry this frame. */
9304 if (fonts_changed_p)
9305 goto retry;
9306
9307 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9308 {
9309 /* See if we have to hscroll. */
9310 if (hscroll_windows (f->root_window))
9311 goto retry;
9312
9313 /* Prevent various kinds of signals during display
9314 update. stdio is not robust about handling
9315 signals, which can cause an apparent I/O
9316 error. */
9317 if (interrupt_input)
9318 unrequest_sigio ();
9319 STOP_POLLING;
9320
9321 /* Update the display. */
9322 set_window_update_flags (XWINDOW (f->root_window), 1);
9323 pause |= update_frame (f, 0, 0);
9324 if (pause)
9325 break;
9326
9327 if (n == size)
9328 {
9329 int nbytes = size * sizeof *updated;
9330 struct frame **p = (struct frame **) alloca (2 * nbytes);
9331 bcopy (updated, p, nbytes);
9332 size *= 2;
9333 }
9334
9335 updated[n++] = f;
9336 }
9337 }
9338 }
9339
9340 /* Do the mark_window_display_accurate after all windows have
9341 been redisplayed because this call resets flags in buffers
9342 which are needed for proper redisplay. */
9343 for (i = 0; i < n; ++i)
9344 {
9345 struct frame *f = updated[i];
9346 mark_window_display_accurate (f->root_window, 1);
9347 if (frame_up_to_date_hook)
9348 frame_up_to_date_hook (f);
9349 }
9350 }
9351 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9352 {
9353 Lisp_Object mini_window;
9354 struct frame *mini_frame;
9355
9356 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
9357 /* Use list_of_error, not Qerror, so that
9358 we catch only errors and don't run the debugger. */
9359 internal_condition_case_1 (redisplay_window_1, selected_window,
9360 list_of_error,
9361 redisplay_window_error);
9362
9363 /* Compare desired and current matrices, perform output. */
9364
9365 update:
9366 /* If fonts changed, display again. */
9367 if (fonts_changed_p)
9368 goto retry;
9369
9370 /* Prevent various kinds of signals during display update.
9371 stdio is not robust about handling signals,
9372 which can cause an apparent I/O error. */
9373 if (interrupt_input)
9374 unrequest_sigio ();
9375 STOP_POLLING;
9376
9377 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
9378 {
9379 if (hscroll_windows (selected_window))
9380 goto retry;
9381
9382 XWINDOW (selected_window)->must_be_updated_p = 1;
9383 pause = update_frame (sf, 0, 0);
9384 }
9385
9386 /* We may have called echo_area_display at the top of this
9387 function. If the echo area is on another frame, that may
9388 have put text on a frame other than the selected one, so the
9389 above call to update_frame would not have caught it. Catch
9390 it here. */
9391 mini_window = FRAME_MINIBUF_WINDOW (sf);
9392 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9393
9394 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
9395 {
9396 XWINDOW (mini_window)->must_be_updated_p = 1;
9397 pause |= update_frame (mini_frame, 0, 0);
9398 if (!pause && hscroll_windows (mini_window))
9399 goto retry;
9400 }
9401 }
9402
9403 /* If display was paused because of pending input, make sure we do a
9404 thorough update the next time. */
9405 if (pause)
9406 {
9407 /* Prevent the optimization at the beginning of
9408 redisplay_internal that tries a single-line update of the
9409 line containing the cursor in the selected window. */
9410 CHARPOS (this_line_start_pos) = 0;
9411
9412 /* Let the overlay arrow be updated the next time. */
9413 if (!NILP (last_arrow_position))
9414 {
9415 last_arrow_position = Qt;
9416 last_arrow_string = Qt;
9417 }
9418
9419 /* If we pause after scrolling, some rows in the current
9420 matrices of some windows are not valid. */
9421 if (!WINDOW_FULL_WIDTH_P (w)
9422 && !FRAME_WINDOW_P (XFRAME (w->frame)))
9423 update_mode_lines = 1;
9424 }
9425 else
9426 {
9427 if (!consider_all_windows_p)
9428 {
9429 /* This has already been done above if
9430 consider_all_windows_p is set. */
9431 mark_window_display_accurate_1 (w, 1);
9432
9433 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9434 last_arrow_string = Voverlay_arrow_string;
9435
9436 if (frame_up_to_date_hook != 0)
9437 frame_up_to_date_hook (sf);
9438 }
9439
9440 update_mode_lines = 0;
9441 windows_or_buffers_changed = 0;
9442 cursor_type_changed = 0;
9443 }
9444
9445 /* Start SIGIO interrupts coming again. Having them off during the
9446 code above makes it less likely one will discard output, but not
9447 impossible, since there might be stuff in the system buffer here.
9448 But it is much hairier to try to do anything about that. */
9449 if (interrupt_input)
9450 request_sigio ();
9451 RESUME_POLLING;
9452
9453 /* If a frame has become visible which was not before, redisplay
9454 again, so that we display it. Expose events for such a frame
9455 (which it gets when becoming visible) don't call the parts of
9456 redisplay constructing glyphs, so simply exposing a frame won't
9457 display anything in this case. So, we have to display these
9458 frames here explicitly. */
9459 if (!pause)
9460 {
9461 Lisp_Object tail, frame;
9462 int new_count = 0;
9463
9464 FOR_EACH_FRAME (tail, frame)
9465 {
9466 int this_is_visible = 0;
9467
9468 if (XFRAME (frame)->visible)
9469 this_is_visible = 1;
9470 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
9471 if (XFRAME (frame)->visible)
9472 this_is_visible = 1;
9473
9474 if (this_is_visible)
9475 new_count++;
9476 }
9477
9478 if (new_count != number_of_visible_frames)
9479 windows_or_buffers_changed++;
9480 }
9481
9482 /* Change frame size now if a change is pending. */
9483 do_pending_window_change (1);
9484
9485 /* If we just did a pending size change, or have additional
9486 visible frames, redisplay again. */
9487 if (windows_or_buffers_changed && !pause)
9488 goto retry;
9489
9490 end_of_redisplay:
9491 unbind_to (count, Qnil);
9492 RESUME_POLLING;
9493 }
9494
9495
9496 /* Redisplay, but leave alone any recent echo area message unless
9497 another message has been requested in its place.
9498
9499 This is useful in situations where you need to redisplay but no
9500 user action has occurred, making it inappropriate for the message
9501 area to be cleared. See tracking_off and
9502 wait_reading_process_input for examples of these situations.
9503
9504 FROM_WHERE is an integer saying from where this function was
9505 called. This is useful for debugging. */
9506
9507 void
9508 redisplay_preserve_echo_area (from_where)
9509 int from_where;
9510 {
9511 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
9512
9513 if (!NILP (echo_area_buffer[1]))
9514 {
9515 /* We have a previously displayed message, but no current
9516 message. Redisplay the previous message. */
9517 display_last_displayed_message_p = 1;
9518 redisplay_internal (1);
9519 display_last_displayed_message_p = 0;
9520 }
9521 else
9522 redisplay_internal (1);
9523 }
9524
9525
9526 /* Function registered with record_unwind_protect in
9527 redisplay_internal. Reset redisplaying_p to the value it had
9528 before redisplay_internal was called, and clear
9529 prevent_freeing_realized_faces_p. */
9530
9531 static Lisp_Object
9532 unwind_redisplay (old_redisplaying_p)
9533 Lisp_Object old_redisplaying_p;
9534 {
9535 redisplaying_p = XFASTINT (old_redisplaying_p);
9536 return Qnil;
9537 }
9538
9539
9540 /* Mark the display of window W as accurate or inaccurate. If
9541 ACCURATE_P is non-zero mark display of W as accurate. If
9542 ACCURATE_P is zero, arrange for W to be redisplayed the next time
9543 redisplay_internal is called. */
9544
9545 static void
9546 mark_window_display_accurate_1 (w, accurate_p)
9547 struct window *w;
9548 int accurate_p;
9549 {
9550 if (BUFFERP (w->buffer))
9551 {
9552 struct buffer *b = XBUFFER (w->buffer);
9553
9554 w->last_modified
9555 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
9556 w->last_overlay_modified
9557 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
9558 w->last_had_star
9559 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
9560
9561 if (accurate_p)
9562 {
9563 b->clip_changed = 0;
9564 b->prevent_redisplay_optimizations_p = 0;
9565
9566 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
9567 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
9568 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
9569 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
9570
9571 w->current_matrix->buffer = b;
9572 w->current_matrix->begv = BUF_BEGV (b);
9573 w->current_matrix->zv = BUF_ZV (b);
9574
9575 w->last_cursor = w->cursor;
9576 w->last_cursor_off_p = w->cursor_off_p;
9577
9578 if (w == XWINDOW (selected_window))
9579 w->last_point = make_number (BUF_PT (b));
9580 else
9581 w->last_point = make_number (XMARKER (w->pointm)->charpos);
9582 }
9583 }
9584
9585 if (accurate_p)
9586 {
9587 w->window_end_valid = w->buffer;
9588 #if 0 /* This is incorrect with variable-height lines. */
9589 xassert (XINT (w->window_end_vpos)
9590 < (XINT (w->height)
9591 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
9592 #endif
9593 w->update_mode_line = Qnil;
9594 }
9595 }
9596
9597
9598 /* Mark the display of windows in the window tree rooted at WINDOW as
9599 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
9600 windows as accurate. If ACCURATE_P is zero, arrange for windows to
9601 be redisplayed the next time redisplay_internal is called. */
9602
9603 void
9604 mark_window_display_accurate (window, accurate_p)
9605 Lisp_Object window;
9606 int accurate_p;
9607 {
9608 struct window *w;
9609
9610 for (; !NILP (window); window = w->next)
9611 {
9612 w = XWINDOW (window);
9613 mark_window_display_accurate_1 (w, accurate_p);
9614
9615 if (!NILP (w->vchild))
9616 mark_window_display_accurate (w->vchild, accurate_p);
9617 if (!NILP (w->hchild))
9618 mark_window_display_accurate (w->hchild, accurate_p);
9619 }
9620
9621 if (accurate_p)
9622 {
9623 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
9624 last_arrow_string = Voverlay_arrow_string;
9625 }
9626 else
9627 {
9628 /* Force a thorough redisplay the next time by setting
9629 last_arrow_position and last_arrow_string to t, which is
9630 unequal to any useful value of Voverlay_arrow_... */
9631 last_arrow_position = Qt;
9632 last_arrow_string = Qt;
9633 }
9634 }
9635
9636
9637 /* Return value in display table DP (Lisp_Char_Table *) for character
9638 C. Since a display table doesn't have any parent, we don't have to
9639 follow parent. Do not call this function directly but use the
9640 macro DISP_CHAR_VECTOR. */
9641
9642 Lisp_Object
9643 disp_char_vector (dp, c)
9644 struct Lisp_Char_Table *dp;
9645 int c;
9646 {
9647 int code[4], i;
9648 Lisp_Object val;
9649
9650 if (SINGLE_BYTE_CHAR_P (c))
9651 return (dp->contents[c]);
9652
9653 SPLIT_CHAR (c, code[0], code[1], code[2]);
9654 if (code[1] < 32)
9655 code[1] = -1;
9656 else if (code[2] < 32)
9657 code[2] = -1;
9658
9659 /* Here, the possible range of code[0] (== charset ID) is
9660 128..max_charset. Since the top level char table contains data
9661 for multibyte characters after 256th element, we must increment
9662 code[0] by 128 to get a correct index. */
9663 code[0] += 128;
9664 code[3] = -1; /* anchor */
9665
9666 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
9667 {
9668 val = dp->contents[code[i]];
9669 if (!SUB_CHAR_TABLE_P (val))
9670 return (NILP (val) ? dp->defalt : val);
9671 }
9672
9673 /* Here, val is a sub char table. We return the default value of
9674 it. */
9675 return (dp->defalt);
9676 }
9677
9678
9679 \f
9680 /***********************************************************************
9681 Window Redisplay
9682 ***********************************************************************/
9683
9684 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
9685
9686 static void
9687 redisplay_windows (window)
9688 Lisp_Object window;
9689 {
9690 while (!NILP (window))
9691 {
9692 struct window *w = XWINDOW (window);
9693
9694 if (!NILP (w->hchild))
9695 redisplay_windows (w->hchild);
9696 else if (!NILP (w->vchild))
9697 redisplay_windows (w->vchild);
9698 else
9699 {
9700 displayed_buffer = XBUFFER (w->buffer);
9701 /* Use list_of_error, not Qerror, so that
9702 we catch only errors and don't run the debugger. */
9703 internal_condition_case_1 (redisplay_window_0, window,
9704 list_of_error,
9705 redisplay_window_error);
9706 }
9707
9708 window = w->next;
9709 }
9710 }
9711
9712 static Lisp_Object
9713 redisplay_window_error ()
9714 {
9715 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
9716 return Qnil;
9717 }
9718
9719 static Lisp_Object
9720 redisplay_window_0 (window)
9721 Lisp_Object window;
9722 {
9723 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9724 redisplay_window (window, 0);
9725 return Qnil;
9726 }
9727
9728 static Lisp_Object
9729 redisplay_window_1 (window)
9730 Lisp_Object window;
9731 {
9732 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
9733 redisplay_window (window, 1);
9734 return Qnil;
9735 }
9736 \f
9737
9738 /* Increment GLYPH until it reaches END or CONDITION fails while
9739 adding (GLYPH)->pixel_width to X. */
9740
9741 #define SKIP_GLYPHS(glyph, end, x, condition) \
9742 do \
9743 { \
9744 (x) += (glyph)->pixel_width; \
9745 ++(glyph); \
9746 } \
9747 while ((glyph) < (end) && (condition))
9748
9749
9750 /* Set cursor position of W. PT is assumed to be displayed in ROW.
9751 DELTA is the number of bytes by which positions recorded in ROW
9752 differ from current buffer positions. */
9753
9754 void
9755 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9756 struct window *w;
9757 struct glyph_row *row;
9758 struct glyph_matrix *matrix;
9759 int delta, delta_bytes, dy, dvpos;
9760 {
9761 struct glyph *glyph = row->glyphs[TEXT_AREA];
9762 struct glyph *end = glyph + row->used[TEXT_AREA];
9763 /* The first glyph that starts a sequence of glyphs from string. */
9764 struct glyph *string_start;
9765 /* The X coordinate of string_start. */
9766 int string_start_x;
9767 /* The last known character position. */
9768 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
9769 /* The last known character position before string_start. */
9770 int string_before_pos;
9771 int x = row->x;
9772 int pt_old = PT - delta;
9773
9774 /* Skip over glyphs not having an object at the start of the row.
9775 These are special glyphs like truncation marks on terminal
9776 frames. */
9777 if (row->displays_text_p)
9778 while (glyph < end
9779 && INTEGERP (glyph->object)
9780 && glyph->charpos < 0)
9781 {
9782 x += glyph->pixel_width;
9783 ++glyph;
9784 }
9785
9786 string_start = NULL;
9787 while (glyph < end
9788 && !INTEGERP (glyph->object)
9789 && (!BUFFERP (glyph->object)
9790 || (last_pos = glyph->charpos) < pt_old))
9791 {
9792 if (! STRINGP (glyph->object))
9793 {
9794 string_start = NULL;
9795 x += glyph->pixel_width;
9796 ++glyph;
9797 }
9798 else
9799 {
9800 string_before_pos = last_pos;
9801 string_start = glyph;
9802 string_start_x = x;
9803 /* Skip all glyphs from string. */
9804 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
9805 }
9806 }
9807
9808 if (string_start
9809 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
9810 {
9811 /* We may have skipped over point because the previous glyphs
9812 are from string. As there's no easy way to know the
9813 character position of the current glyph, find the correct
9814 glyph on point by scanning from string_start again. */
9815 Lisp_Object limit;
9816 Lisp_Object string;
9817 int pos;
9818
9819 limit = make_number (pt_old + 1);
9820 end = glyph;
9821 glyph = string_start;
9822 x = string_start_x;
9823 string = glyph->object;
9824 pos = string_buffer_position (w, string, string_before_pos);
9825 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
9826 because we always put cursor after overlay strings. */
9827 while (pos == 0 && glyph < end)
9828 {
9829 string = glyph->object;
9830 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9831 if (glyph < end)
9832 pos = string_buffer_position (w, glyph->object, string_before_pos);
9833 }
9834
9835 while (glyph < end)
9836 {
9837 pos = XINT (Fnext_single_char_property_change
9838 (make_number (pos), Qdisplay, Qnil, limit));
9839 if (pos > pt_old)
9840 break;
9841 /* Skip glyphs from the same string. */
9842 string = glyph->object;
9843 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9844 /* Skip glyphs from an overlay. */
9845 while (glyph < end
9846 && ! string_buffer_position (w, glyph->object, pos))
9847 {
9848 string = glyph->object;
9849 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
9850 }
9851 }
9852 }
9853
9854 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9855 w->cursor.x = x;
9856 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9857 w->cursor.y = row->y + dy;
9858
9859 if (w == XWINDOW (selected_window))
9860 {
9861 if (!row->continued_p
9862 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9863 && row->x == 0)
9864 {
9865 this_line_buffer = XBUFFER (w->buffer);
9866
9867 CHARPOS (this_line_start_pos)
9868 = MATRIX_ROW_START_CHARPOS (row) + delta;
9869 BYTEPOS (this_line_start_pos)
9870 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9871
9872 CHARPOS (this_line_end_pos)
9873 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9874 BYTEPOS (this_line_end_pos)
9875 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9876
9877 this_line_y = w->cursor.y;
9878 this_line_pixel_height = row->height;
9879 this_line_vpos = w->cursor.vpos;
9880 this_line_start_x = row->x;
9881 }
9882 else
9883 CHARPOS (this_line_start_pos) = 0;
9884 }
9885 }
9886
9887
9888 /* Run window scroll functions, if any, for WINDOW with new window
9889 start STARTP. Sets the window start of WINDOW to that position.
9890
9891 We assume that the window's buffer is really current. */
9892
9893 static INLINE struct text_pos
9894 run_window_scroll_functions (window, startp)
9895 Lisp_Object window;
9896 struct text_pos startp;
9897 {
9898 struct window *w = XWINDOW (window);
9899 SET_MARKER_FROM_TEXT_POS (w->start, startp);
9900
9901 if (current_buffer != XBUFFER (w->buffer))
9902 abort ();
9903
9904 if (!NILP (Vwindow_scroll_functions))
9905 {
9906 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9907 make_number (CHARPOS (startp)));
9908 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9909 /* In case the hook functions switch buffers. */
9910 if (current_buffer != XBUFFER (w->buffer))
9911 set_buffer_internal_1 (XBUFFER (w->buffer));
9912 }
9913
9914 return startp;
9915 }
9916
9917
9918 /* Make sure the line containing the cursor is fully visible.
9919 A value of 1 means there is nothing to be done.
9920 (Either the line is fully visible, or it cannot be made so,
9921 or we cannot tell.)
9922 A value of 0 means the caller should do scrolling
9923 as if point had gone off the screen. */
9924
9925 static int
9926 make_cursor_line_fully_visible (w)
9927 struct window *w;
9928 {
9929 struct glyph_matrix *matrix;
9930 struct glyph_row *row;
9931 int window_height;
9932
9933 /* It's not always possible to find the cursor, e.g, when a window
9934 is full of overlay strings. Don't do anything in that case. */
9935 if (w->cursor.vpos < 0)
9936 return 1;
9937
9938 matrix = w->desired_matrix;
9939 row = MATRIX_ROW (matrix, w->cursor.vpos);
9940
9941 /* If the cursor row is not partially visible, there's nothing to do. */
9942 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9943 return 1;
9944
9945 /* If the row the cursor is in is taller than the window's height,
9946 it's not clear what to do, so do nothing. */
9947 window_height = window_box_height (w);
9948 if (row->height >= window_height)
9949 return 1;
9950
9951 return 0;
9952
9953 #if 0
9954 /* This code used to try to scroll the window just enough to make
9955 the line visible. It returned 0 to say that the caller should
9956 allocate larger glyph matrices. */
9957
9958 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
9959 {
9960 int dy = row->height - row->visible_height;
9961 w->vscroll = 0;
9962 w->cursor.y += dy;
9963 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9964 }
9965 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
9966 {
9967 int dy = - (row->height - row->visible_height);
9968 w->vscroll = dy;
9969 w->cursor.y += dy;
9970 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9971 }
9972
9973 /* When we change the cursor y-position of the selected window,
9974 change this_line_y as well so that the display optimization for
9975 the cursor line of the selected window in redisplay_internal uses
9976 the correct y-position. */
9977 if (w == XWINDOW (selected_window))
9978 this_line_y = w->cursor.y;
9979
9980 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
9981 redisplay with larger matrices. */
9982 if (matrix->nrows < required_matrix_height (w))
9983 {
9984 fonts_changed_p = 1;
9985 return 0;
9986 }
9987
9988 return 1;
9989 #endif /* 0 */
9990 }
9991
9992
9993 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9994 non-zero means only WINDOW is redisplayed in redisplay_internal.
9995 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9996 in redisplay_window to bring a partially visible line into view in
9997 the case that only the cursor has moved.
9998
9999 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10000 last screen line's vertical height extends past the end of the screen.
10001
10002 Value is
10003
10004 1 if scrolling succeeded
10005
10006 0 if scrolling didn't find point.
10007
10008 -1 if new fonts have been loaded so that we must interrupt
10009 redisplay, adjust glyph matrices, and try again. */
10010
10011 enum
10012 {
10013 SCROLLING_SUCCESS,
10014 SCROLLING_FAILED,
10015 SCROLLING_NEED_LARGER_MATRICES
10016 };
10017
10018 static int
10019 try_scrolling (window, just_this_one_p, scroll_conservatively,
10020 scroll_step, temp_scroll_step, last_line_misfit)
10021 Lisp_Object window;
10022 int just_this_one_p;
10023 EMACS_INT scroll_conservatively, scroll_step;
10024 int temp_scroll_step;
10025 int last_line_misfit;
10026 {
10027 struct window *w = XWINDOW (window);
10028 struct frame *f = XFRAME (w->frame);
10029 struct text_pos scroll_margin_pos;
10030 struct text_pos pos;
10031 struct text_pos startp;
10032 struct it it;
10033 Lisp_Object window_end;
10034 int this_scroll_margin;
10035 int dy = 0;
10036 int scroll_max;
10037 int rc;
10038 int amount_to_scroll = 0;
10039 Lisp_Object aggressive;
10040 int height;
10041 int end_scroll_margin;
10042
10043 #if GLYPH_DEBUG
10044 debug_method_add (w, "try_scrolling");
10045 #endif
10046
10047 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10048
10049 /* Compute scroll margin height in pixels. We scroll when point is
10050 within this distance from the top or bottom of the window. */
10051 if (scroll_margin > 0)
10052 {
10053 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
10054 this_scroll_margin *= CANON_Y_UNIT (f);
10055 }
10056 else
10057 this_scroll_margin = 0;
10058
10059 /* Compute how much we should try to scroll maximally to bring point
10060 into view. */
10061 if (scroll_step || scroll_conservatively || temp_scroll_step)
10062 scroll_max = max (scroll_step,
10063 max (scroll_conservatively, temp_scroll_step));
10064 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10065 || NUMBERP (current_buffer->scroll_up_aggressively))
10066 /* We're trying to scroll because of aggressive scrolling
10067 but no scroll_step is set. Choose an arbitrary one. Maybe
10068 there should be a variable for this. */
10069 scroll_max = 10;
10070 else
10071 scroll_max = 0;
10072 scroll_max *= CANON_Y_UNIT (f);
10073
10074 /* Decide whether we have to scroll down. Start at the window end
10075 and move this_scroll_margin up to find the position of the scroll
10076 margin. */
10077 window_end = Fwindow_end (window, Qt);
10078
10079 too_near_end:
10080
10081 CHARPOS (scroll_margin_pos) = XINT (window_end);
10082 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10083
10084 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10085 if (end_scroll_margin)
10086 {
10087 start_display (&it, w, scroll_margin_pos);
10088 move_it_vertically (&it, - end_scroll_margin);
10089 scroll_margin_pos = it.current.pos;
10090 }
10091
10092 if (PT >= CHARPOS (scroll_margin_pos))
10093 {
10094 int y0;
10095
10096 /* Point is in the scroll margin at the bottom of the window, or
10097 below. Compute a new window start that makes point visible. */
10098
10099 /* Compute the distance from the scroll margin to PT.
10100 Give up if the distance is greater than scroll_max. */
10101 start_display (&it, w, scroll_margin_pos);
10102 y0 = it.current_y;
10103 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10104 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10105
10106 /* To make point visible, we have to move the window start
10107 down so that the line the cursor is in is visible, which
10108 means we have to add in the height of the cursor line. */
10109 dy = line_bottom_y (&it) - y0;
10110
10111 if (dy > scroll_max)
10112 return SCROLLING_FAILED;
10113
10114 /* Move the window start down. If scrolling conservatively,
10115 move it just enough down to make point visible. If
10116 scroll_step is set, move it down by scroll_step. */
10117 start_display (&it, w, startp);
10118
10119 if (scroll_conservatively)
10120 /* Set AMOUNT_TO_SCROLL to at least one line,
10121 and at most scroll_conservatively lines. */
10122 amount_to_scroll
10123 = min (max (dy, CANON_Y_UNIT (f)),
10124 CANON_Y_UNIT (f) * scroll_conservatively);
10125 else if (scroll_step || temp_scroll_step)
10126 amount_to_scroll = scroll_max;
10127 else
10128 {
10129 aggressive = current_buffer->scroll_up_aggressively;
10130 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10131 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10132 if (NUMBERP (aggressive))
10133 amount_to_scroll = XFLOATINT (aggressive) * height;
10134 }
10135
10136 if (amount_to_scroll <= 0)
10137 return SCROLLING_FAILED;
10138
10139 /* If moving by amount_to_scroll leaves STARTP unchanged,
10140 move it down one screen line. */
10141
10142 move_it_vertically (&it, amount_to_scroll);
10143 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10144 move_it_by_lines (&it, 1, 1);
10145 startp = it.current.pos;
10146 }
10147 else
10148 {
10149 /* See if point is inside the scroll margin at the top of the
10150 window. */
10151 scroll_margin_pos = startp;
10152 if (this_scroll_margin)
10153 {
10154 start_display (&it, w, startp);
10155 move_it_vertically (&it, this_scroll_margin);
10156 scroll_margin_pos = it.current.pos;
10157 }
10158
10159 if (PT < CHARPOS (scroll_margin_pos))
10160 {
10161 /* Point is in the scroll margin at the top of the window or
10162 above what is displayed in the window. */
10163 int y0;
10164
10165 /* Compute the vertical distance from PT to the scroll
10166 margin position. Give up if distance is greater than
10167 scroll_max. */
10168 SET_TEXT_POS (pos, PT, PT_BYTE);
10169 start_display (&it, w, pos);
10170 y0 = it.current_y;
10171 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10172 it.last_visible_y, -1,
10173 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10174 dy = it.current_y - y0;
10175 if (dy > scroll_max)
10176 return SCROLLING_FAILED;
10177
10178 /* Compute new window start. */
10179 start_display (&it, w, startp);
10180
10181 if (scroll_conservatively)
10182 amount_to_scroll =
10183 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
10184 else if (scroll_step || temp_scroll_step)
10185 amount_to_scroll = scroll_max;
10186 else
10187 {
10188 aggressive = current_buffer->scroll_down_aggressively;
10189 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10190 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10191 if (NUMBERP (aggressive))
10192 amount_to_scroll = XFLOATINT (aggressive) * height;
10193 }
10194
10195 if (amount_to_scroll <= 0)
10196 return SCROLLING_FAILED;
10197
10198 move_it_vertically (&it, - amount_to_scroll);
10199 startp = it.current.pos;
10200 }
10201 }
10202
10203 /* Run window scroll functions. */
10204 startp = run_window_scroll_functions (window, startp);
10205
10206 /* Display the window. Give up if new fonts are loaded, or if point
10207 doesn't appear. */
10208 if (!try_window (window, startp))
10209 rc = SCROLLING_NEED_LARGER_MATRICES;
10210 else if (w->cursor.vpos < 0)
10211 {
10212 clear_glyph_matrix (w->desired_matrix);
10213 rc = SCROLLING_FAILED;
10214 }
10215 else
10216 {
10217 /* Maybe forget recorded base line for line number display. */
10218 if (!just_this_one_p
10219 || current_buffer->clip_changed
10220 || BEG_UNCHANGED < CHARPOS (startp))
10221 w->base_line_number = Qnil;
10222
10223 /* If cursor ends up on a partially visible line,
10224 treat that as being off the bottom of the screen. */
10225 if (! make_cursor_line_fully_visible (w))
10226 {
10227 clear_glyph_matrix (w->desired_matrix);
10228 last_line_misfit = 1;
10229 goto too_near_end;
10230 }
10231 rc = SCROLLING_SUCCESS;
10232 }
10233
10234 return rc;
10235 }
10236
10237
10238 /* Compute a suitable window start for window W if display of W starts
10239 on a continuation line. Value is non-zero if a new window start
10240 was computed.
10241
10242 The new window start will be computed, based on W's width, starting
10243 from the start of the continued line. It is the start of the
10244 screen line with the minimum distance from the old start W->start. */
10245
10246 static int
10247 compute_window_start_on_continuation_line (w)
10248 struct window *w;
10249 {
10250 struct text_pos pos, start_pos;
10251 int window_start_changed_p = 0;
10252
10253 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10254
10255 /* If window start is on a continuation line... Window start may be
10256 < BEGV in case there's invisible text at the start of the
10257 buffer (M-x rmail, for example). */
10258 if (CHARPOS (start_pos) > BEGV
10259 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10260 {
10261 struct it it;
10262 struct glyph_row *row;
10263
10264 /* Handle the case that the window start is out of range. */
10265 if (CHARPOS (start_pos) < BEGV)
10266 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10267 else if (CHARPOS (start_pos) > ZV)
10268 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10269
10270 /* Find the start of the continued line. This should be fast
10271 because scan_buffer is fast (newline cache). */
10272 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
10273 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10274 row, DEFAULT_FACE_ID);
10275 reseat_at_previous_visible_line_start (&it);
10276
10277 /* If the line start is "too far" away from the window start,
10278 say it takes too much time to compute a new window start. */
10279 if (CHARPOS (start_pos) - IT_CHARPOS (it)
10280 < XFASTINT (w->height) * XFASTINT (w->width))
10281 {
10282 int min_distance, distance;
10283
10284 /* Move forward by display lines to find the new window
10285 start. If window width was enlarged, the new start can
10286 be expected to be > the old start. If window width was
10287 decreased, the new window start will be < the old start.
10288 So, we're looking for the display line start with the
10289 minimum distance from the old window start. */
10290 pos = it.current.pos;
10291 min_distance = INFINITY;
10292 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10293 distance < min_distance)
10294 {
10295 min_distance = distance;
10296 pos = it.current.pos;
10297 move_it_by_lines (&it, 1, 0);
10298 }
10299
10300 /* Set the window start there. */
10301 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10302 window_start_changed_p = 1;
10303 }
10304 }
10305
10306 return window_start_changed_p;
10307 }
10308
10309
10310 /* Try cursor movement in case text has not changed in window WINDOW,
10311 with window start STARTP. Value is
10312
10313 CURSOR_MOVEMENT_SUCCESS if successful
10314
10315 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
10316
10317 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
10318 display. *SCROLL_STEP is set to 1, under certain circumstances, if
10319 we want to scroll as if scroll-step were set to 1. See the code.
10320
10321 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
10322 which case we have to abort this redisplay, and adjust matrices
10323 first. */
10324
10325 enum
10326 {
10327 CURSOR_MOVEMENT_SUCCESS,
10328 CURSOR_MOVEMENT_CANNOT_BE_USED,
10329 CURSOR_MOVEMENT_MUST_SCROLL,
10330 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
10331 };
10332
10333 static int
10334 try_cursor_movement (window, startp, scroll_step)
10335 Lisp_Object window;
10336 struct text_pos startp;
10337 int *scroll_step;
10338 {
10339 struct window *w = XWINDOW (window);
10340 struct frame *f = XFRAME (w->frame);
10341 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
10342
10343 #if GLYPH_DEBUG
10344 if (inhibit_try_cursor_movement)
10345 return rc;
10346 #endif
10347
10348 /* Handle case where text has not changed, only point, and it has
10349 not moved off the frame. */
10350 if (/* Point may be in this window. */
10351 PT >= CHARPOS (startp)
10352 /* Selective display hasn't changed. */
10353 && !current_buffer->clip_changed
10354 /* Function force-mode-line-update is used to force a thorough
10355 redisplay. It sets either windows_or_buffers_changed or
10356 update_mode_lines. So don't take a shortcut here for these
10357 cases. */
10358 && !update_mode_lines
10359 && !windows_or_buffers_changed
10360 && !cursor_type_changed
10361 /* Can't use this case if highlighting a region. When a
10362 region exists, cursor movement has to do more than just
10363 set the cursor. */
10364 && !(!NILP (Vtransient_mark_mode)
10365 && !NILP (current_buffer->mark_active))
10366 && NILP (w->region_showing)
10367 && NILP (Vshow_trailing_whitespace)
10368 /* Right after splitting windows, last_point may be nil. */
10369 && INTEGERP (w->last_point)
10370 /* This code is not used for mini-buffer for the sake of the case
10371 of redisplaying to replace an echo area message; since in
10372 that case the mini-buffer contents per se are usually
10373 unchanged. This code is of no real use in the mini-buffer
10374 since the handling of this_line_start_pos, etc., in redisplay
10375 handles the same cases. */
10376 && !EQ (window, minibuf_window)
10377 /* When splitting windows or for new windows, it happens that
10378 redisplay is called with a nil window_end_vpos or one being
10379 larger than the window. This should really be fixed in
10380 window.c. I don't have this on my list, now, so we do
10381 approximately the same as the old redisplay code. --gerd. */
10382 && INTEGERP (w->window_end_vpos)
10383 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
10384 && (FRAME_WINDOW_P (f)
10385 || !MARKERP (Voverlay_arrow_position)
10386 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
10387 {
10388 int this_scroll_margin;
10389 struct glyph_row *row = NULL;
10390
10391 #if GLYPH_DEBUG
10392 debug_method_add (w, "cursor movement");
10393 #endif
10394
10395 /* Scroll if point within this distance from the top or bottom
10396 of the window. This is a pixel value. */
10397 this_scroll_margin = max (0, scroll_margin);
10398 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
10399 this_scroll_margin *= CANON_Y_UNIT (f);
10400
10401 /* Start with the row the cursor was displayed during the last
10402 not paused redisplay. Give up if that row is not valid. */
10403 if (w->last_cursor.vpos < 0
10404 || w->last_cursor.vpos >= w->current_matrix->nrows)
10405 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10406 else
10407 {
10408 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
10409 if (row->mode_line_p)
10410 ++row;
10411 if (!row->enabled_p)
10412 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10413 }
10414
10415 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
10416 {
10417 int scroll_p = 0;
10418 int last_y = window_text_bottom_y (w) - this_scroll_margin;
10419
10420 if (PT > XFASTINT (w->last_point))
10421 {
10422 /* Point has moved forward. */
10423 while (MATRIX_ROW_END_CHARPOS (row) < PT
10424 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
10425 {
10426 xassert (row->enabled_p);
10427 ++row;
10428 }
10429
10430 /* The end position of a row equals the start position
10431 of the next row. If PT is there, we would rather
10432 display it in the next line. */
10433 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10434 && MATRIX_ROW_END_CHARPOS (row) == PT
10435 && !cursor_row_p (w, row))
10436 ++row;
10437
10438 /* If within the scroll margin, scroll. Note that
10439 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
10440 the next line would be drawn, and that
10441 this_scroll_margin can be zero. */
10442 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
10443 || PT > MATRIX_ROW_END_CHARPOS (row)
10444 /* Line is completely visible last line in window
10445 and PT is to be set in the next line. */
10446 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
10447 && PT == MATRIX_ROW_END_CHARPOS (row)
10448 && !row->ends_at_zv_p
10449 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
10450 scroll_p = 1;
10451 }
10452 else if (PT < XFASTINT (w->last_point))
10453 {
10454 /* Cursor has to be moved backward. Note that PT >=
10455 CHARPOS (startp) because of the outer
10456 if-statement. */
10457 while (!row->mode_line_p
10458 && (MATRIX_ROW_START_CHARPOS (row) > PT
10459 || (MATRIX_ROW_START_CHARPOS (row) == PT
10460 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
10461 && (row->y > this_scroll_margin
10462 || CHARPOS (startp) == BEGV))
10463 {
10464 xassert (row->enabled_p);
10465 --row;
10466 }
10467
10468 /* Consider the following case: Window starts at BEGV,
10469 there is invisible, intangible text at BEGV, so that
10470 display starts at some point START > BEGV. It can
10471 happen that we are called with PT somewhere between
10472 BEGV and START. Try to handle that case. */
10473 if (row < w->current_matrix->rows
10474 || row->mode_line_p)
10475 {
10476 row = w->current_matrix->rows;
10477 if (row->mode_line_p)
10478 ++row;
10479 }
10480
10481 /* Due to newlines in overlay strings, we may have to
10482 skip forward over overlay strings. */
10483 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
10484 && MATRIX_ROW_END_CHARPOS (row) == PT
10485 && !cursor_row_p (w, row))
10486 ++row;
10487
10488 /* If within the scroll margin, scroll. */
10489 if (row->y < this_scroll_margin
10490 && CHARPOS (startp) != BEGV)
10491 scroll_p = 1;
10492 }
10493
10494 if (PT < MATRIX_ROW_START_CHARPOS (row)
10495 || PT > MATRIX_ROW_END_CHARPOS (row))
10496 {
10497 /* if PT is not in the glyph row, give up. */
10498 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10499 }
10500 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10501 {
10502 if (PT == MATRIX_ROW_END_CHARPOS (row)
10503 && !row->ends_at_zv_p
10504 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
10505 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10506 else if (row->height > window_box_height (w))
10507 {
10508 /* If we end up in a partially visible line, let's
10509 make it fully visible, except when it's taller
10510 than the window, in which case we can't do much
10511 about it. */
10512 *scroll_step = 1;
10513 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10514 }
10515 else
10516 {
10517 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10518 if (!make_cursor_line_fully_visible (w))
10519 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10520 else
10521 rc = CURSOR_MOVEMENT_SUCCESS;
10522 }
10523 }
10524 else if (scroll_p)
10525 rc = CURSOR_MOVEMENT_MUST_SCROLL;
10526 else
10527 {
10528 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10529 rc = CURSOR_MOVEMENT_SUCCESS;
10530 }
10531 }
10532 }
10533
10534 return rc;
10535 }
10536
10537
10538 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
10539 selected_window is redisplayed.
10540
10541 We can return without actually redisplaying the window if
10542 fonts_changed_p is nonzero. In that case, redisplay_internal will
10543 retry. */
10544
10545 static void
10546 redisplay_window (window, just_this_one_p)
10547 Lisp_Object window;
10548 int just_this_one_p;
10549 {
10550 struct window *w = XWINDOW (window);
10551 struct frame *f = XFRAME (w->frame);
10552 struct buffer *buffer = XBUFFER (w->buffer);
10553 struct buffer *old = current_buffer;
10554 struct text_pos lpoint, opoint, startp;
10555 int update_mode_line;
10556 int tem;
10557 struct it it;
10558 /* Record it now because it's overwritten. */
10559 int current_matrix_up_to_date_p = 0;
10560 /* This is less strict than current_matrix_up_to_date_p.
10561 It indictes that the buffer contents and narrowing are unchanged. */
10562 int buffer_unchanged_p = 0;
10563 int temp_scroll_step = 0;
10564 int count = SPECPDL_INDEX ();
10565 int rc;
10566 int centering_position;
10567 int last_line_misfit = 0;
10568
10569 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10570 opoint = lpoint;
10571
10572 /* W must be a leaf window here. */
10573 xassert (!NILP (w->buffer));
10574 #if GLYPH_DEBUG
10575 *w->desired_matrix->method = 0;
10576 #endif
10577
10578 specbind (Qinhibit_point_motion_hooks, Qt);
10579
10580 reconsider_clip_changes (w, buffer);
10581
10582 /* Has the mode line to be updated? */
10583 update_mode_line = (!NILP (w->update_mode_line)
10584 || update_mode_lines
10585 || buffer->clip_changed
10586 || buffer->prevent_redisplay_optimizations_p);
10587
10588 if (MINI_WINDOW_P (w))
10589 {
10590 if (w == XWINDOW (echo_area_window)
10591 && !NILP (echo_area_buffer[0]))
10592 {
10593 if (update_mode_line)
10594 /* We may have to update a tty frame's menu bar or a
10595 tool-bar. Example `M-x C-h C-h C-g'. */
10596 goto finish_menu_bars;
10597 else
10598 /* We've already displayed the echo area glyphs in this window. */
10599 goto finish_scroll_bars;
10600 }
10601 else if ((w != XWINDOW (minibuf_window)
10602 || minibuf_level == 0)
10603 /* Quail displays non-mini buffers in minibuffer window.
10604 In that case, redisplay the window normally. */
10605 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
10606 {
10607 /* W is a mini-buffer window, but it's not active, so clear
10608 it. */
10609 int yb = window_text_bottom_y (w);
10610 struct glyph_row *row;
10611 int y;
10612
10613 for (y = 0, row = w->desired_matrix->rows;
10614 y < yb;
10615 y += row->height, ++row)
10616 blank_row (w, row, y);
10617 goto finish_scroll_bars;
10618 }
10619
10620 clear_glyph_matrix (w->desired_matrix);
10621 }
10622
10623 /* Otherwise set up data on this window; select its buffer and point
10624 value. */
10625 /* Really select the buffer, for the sake of buffer-local
10626 variables. */
10627 set_buffer_internal_1 (XBUFFER (w->buffer));
10628 SET_TEXT_POS (opoint, PT, PT_BYTE);
10629
10630 current_matrix_up_to_date_p
10631 = (!NILP (w->window_end_valid)
10632 && !current_buffer->clip_changed
10633 && !current_buffer->prevent_redisplay_optimizations_p
10634 && XFASTINT (w->last_modified) >= MODIFF
10635 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10636
10637 buffer_unchanged_p
10638 = (!NILP (w->window_end_valid)
10639 && !current_buffer->clip_changed
10640 && XFASTINT (w->last_modified) >= MODIFF
10641 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
10642
10643 /* When windows_or_buffers_changed is non-zero, we can't rely on
10644 the window end being valid, so set it to nil there. */
10645 if (windows_or_buffers_changed)
10646 {
10647 /* If window starts on a continuation line, maybe adjust the
10648 window start in case the window's width changed. */
10649 if (XMARKER (w->start)->buffer == current_buffer)
10650 compute_window_start_on_continuation_line (w);
10651
10652 w->window_end_valid = Qnil;
10653 }
10654
10655 /* Some sanity checks. */
10656 CHECK_WINDOW_END (w);
10657 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
10658 abort ();
10659 if (BYTEPOS (opoint) < CHARPOS (opoint))
10660 abort ();
10661
10662 /* If %c is in mode line, update it if needed. */
10663 if (!NILP (w->column_number_displayed)
10664 /* This alternative quickly identifies a common case
10665 where no change is needed. */
10666 && !(PT == XFASTINT (w->last_point)
10667 && XFASTINT (w->last_modified) >= MODIFF
10668 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10669 && (XFASTINT (w->column_number_displayed)
10670 != (int) current_column ())) /* iftc */
10671 update_mode_line = 1;
10672
10673 /* Count number of windows showing the selected buffer. An indirect
10674 buffer counts as its base buffer. */
10675 if (!just_this_one_p)
10676 {
10677 struct buffer *current_base, *window_base;
10678 current_base = current_buffer;
10679 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
10680 if (current_base->base_buffer)
10681 current_base = current_base->base_buffer;
10682 if (window_base->base_buffer)
10683 window_base = window_base->base_buffer;
10684 if (current_base == window_base)
10685 buffer_shared++;
10686 }
10687
10688 /* Point refers normally to the selected window. For any other
10689 window, set up appropriate value. */
10690 if (!EQ (window, selected_window))
10691 {
10692 int new_pt = XMARKER (w->pointm)->charpos;
10693 int new_pt_byte = marker_byte_position (w->pointm);
10694 if (new_pt < BEGV)
10695 {
10696 new_pt = BEGV;
10697 new_pt_byte = BEGV_BYTE;
10698 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
10699 }
10700 else if (new_pt > (ZV - 1))
10701 {
10702 new_pt = ZV;
10703 new_pt_byte = ZV_BYTE;
10704 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
10705 }
10706
10707 /* We don't use SET_PT so that the point-motion hooks don't run. */
10708 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
10709 }
10710
10711 /* If any of the character widths specified in the display table
10712 have changed, invalidate the width run cache. It's true that
10713 this may be a bit late to catch such changes, but the rest of
10714 redisplay goes (non-fatally) haywire when the display table is
10715 changed, so why should we worry about doing any better? */
10716 if (current_buffer->width_run_cache)
10717 {
10718 struct Lisp_Char_Table *disptab = buffer_display_table ();
10719
10720 if (! disptab_matches_widthtab (disptab,
10721 XVECTOR (current_buffer->width_table)))
10722 {
10723 invalidate_region_cache (current_buffer,
10724 current_buffer->width_run_cache,
10725 BEG, Z);
10726 recompute_width_table (current_buffer, disptab);
10727 }
10728 }
10729
10730 /* If window-start is screwed up, choose a new one. */
10731 if (XMARKER (w->start)->buffer != current_buffer)
10732 goto recenter;
10733
10734 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10735
10736 /* If someone specified a new starting point but did not insist,
10737 check whether it can be used. */
10738 if (!NILP (w->optional_new_start)
10739 && CHARPOS (startp) >= BEGV
10740 && CHARPOS (startp) <= ZV)
10741 {
10742 w->optional_new_start = Qnil;
10743 start_display (&it, w, startp);
10744 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10745 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10746 if (IT_CHARPOS (it) == PT)
10747 w->force_start = Qt;
10748 }
10749
10750 /* Handle case where place to start displaying has been specified,
10751 unless the specified location is outside the accessible range. */
10752 if (!NILP (w->force_start)
10753 || w->frozen_window_start_p)
10754 {
10755 /* We set this later on if we have to adjust point. */
10756 int new_vpos = -1;
10757
10758 w->force_start = Qnil;
10759 w->vscroll = 0;
10760 w->window_end_valid = Qnil;
10761
10762 /* Forget any recorded base line for line number display. */
10763 if (!buffer_unchanged_p)
10764 w->base_line_number = Qnil;
10765
10766 /* Redisplay the mode line. Select the buffer properly for that.
10767 Also, run the hook window-scroll-functions
10768 because we have scrolled. */
10769 /* Note, we do this after clearing force_start because
10770 if there's an error, it is better to forget about force_start
10771 than to get into an infinite loop calling the hook functions
10772 and having them get more errors. */
10773 if (!update_mode_line
10774 || ! NILP (Vwindow_scroll_functions))
10775 {
10776 update_mode_line = 1;
10777 w->update_mode_line = Qt;
10778 startp = run_window_scroll_functions (window, startp);
10779 }
10780
10781 w->last_modified = make_number (0);
10782 w->last_overlay_modified = make_number (0);
10783 if (CHARPOS (startp) < BEGV)
10784 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
10785 else if (CHARPOS (startp) > ZV)
10786 SET_TEXT_POS (startp, ZV, ZV_BYTE);
10787
10788 /* Redisplay, then check if cursor has been set during the
10789 redisplay. Give up if new fonts were loaded. */
10790 if (!try_window (window, startp))
10791 {
10792 w->force_start = Qt;
10793 clear_glyph_matrix (w->desired_matrix);
10794 goto need_larger_matrices;
10795 }
10796
10797 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
10798 {
10799 /* If point does not appear, try to move point so it does
10800 appear. The desired matrix has been built above, so we
10801 can use it here. */
10802 new_vpos = window_box_height (w) / 2;
10803 }
10804
10805 if (!make_cursor_line_fully_visible (w))
10806 {
10807 /* Point does appear, but on a line partly visible at end of window.
10808 Move it back to a fully-visible line. */
10809 new_vpos = window_box_height (w);
10810 }
10811
10812 /* If we need to move point for either of the above reasons,
10813 now actually do it. */
10814 if (new_vpos >= 0)
10815 {
10816 struct glyph_row *row;
10817
10818 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
10819 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
10820 ++row;
10821
10822 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
10823 MATRIX_ROW_START_BYTEPOS (row));
10824
10825 if (w != XWINDOW (selected_window))
10826 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
10827 else if (current_buffer == old)
10828 SET_TEXT_POS (lpoint, PT, PT_BYTE);
10829
10830 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
10831
10832 /* If we are highlighting the region, then we just changed
10833 the region, so redisplay to show it. */
10834 if (!NILP (Vtransient_mark_mode)
10835 && !NILP (current_buffer->mark_active))
10836 {
10837 clear_glyph_matrix (w->desired_matrix);
10838 if (!try_window (window, startp))
10839 goto need_larger_matrices;
10840 }
10841 }
10842
10843 #if GLYPH_DEBUG
10844 debug_method_add (w, "forced window start");
10845 #endif
10846 goto done;
10847 }
10848
10849 /* Handle case where text has not changed, only point, and it has
10850 not moved off the frame, and we are not retrying after hscroll.
10851 (current_matrix_up_to_date_p is nonzero when retrying.) */
10852 if (current_matrix_up_to_date_p
10853 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
10854 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
10855 {
10856 switch (rc)
10857 {
10858 case CURSOR_MOVEMENT_SUCCESS:
10859 goto done;
10860
10861 #if 0 /* try_cursor_movement never returns this value. */
10862 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
10863 goto need_larger_matrices;
10864 #endif
10865
10866 case CURSOR_MOVEMENT_MUST_SCROLL:
10867 goto try_to_scroll;
10868
10869 default:
10870 abort ();
10871 }
10872 }
10873 /* If current starting point was originally the beginning of a line
10874 but no longer is, find a new starting point. */
10875 else if (!NILP (w->start_at_line_beg)
10876 && !(CHARPOS (startp) <= BEGV
10877 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
10878 {
10879 #if GLYPH_DEBUG
10880 debug_method_add (w, "recenter 1");
10881 #endif
10882 goto recenter;
10883 }
10884
10885 /* Try scrolling with try_window_id. Value is > 0 if update has
10886 been done, it is -1 if we know that the same window start will
10887 not work. It is 0 if unsuccessful for some other reason. */
10888 else if ((tem = try_window_id (w)) != 0)
10889 {
10890 #if GLYPH_DEBUG
10891 debug_method_add (w, "try_window_id %d", tem);
10892 #endif
10893
10894 if (fonts_changed_p)
10895 goto need_larger_matrices;
10896 if (tem > 0)
10897 goto done;
10898
10899 /* Otherwise try_window_id has returned -1 which means that we
10900 don't want the alternative below this comment to execute. */
10901 }
10902 else if (CHARPOS (startp) >= BEGV
10903 && CHARPOS (startp) <= ZV
10904 && PT >= CHARPOS (startp)
10905 && (CHARPOS (startp) < ZV
10906 /* Avoid starting at end of buffer. */
10907 || CHARPOS (startp) == BEGV
10908 || (XFASTINT (w->last_modified) >= MODIFF
10909 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
10910 {
10911 #if GLYPH_DEBUG
10912 debug_method_add (w, "same window start");
10913 #endif
10914
10915 /* Try to redisplay starting at same place as before.
10916 If point has not moved off frame, accept the results. */
10917 if (!current_matrix_up_to_date_p
10918 /* Don't use try_window_reusing_current_matrix in this case
10919 because a window scroll function can have changed the
10920 buffer. */
10921 || !NILP (Vwindow_scroll_functions)
10922 || MINI_WINDOW_P (w)
10923 || !try_window_reusing_current_matrix (w))
10924 {
10925 IF_DEBUG (debug_method_add (w, "1"));
10926 try_window (window, startp);
10927 }
10928
10929 if (fonts_changed_p)
10930 goto need_larger_matrices;
10931
10932 if (w->cursor.vpos >= 0)
10933 {
10934 if (!just_this_one_p
10935 || current_buffer->clip_changed
10936 || BEG_UNCHANGED < CHARPOS (startp))
10937 /* Forget any recorded base line for line number display. */
10938 w->base_line_number = Qnil;
10939
10940 if (!make_cursor_line_fully_visible (w))
10941 {
10942 clear_glyph_matrix (w->desired_matrix);
10943 last_line_misfit = 1;
10944 }
10945 /* Drop through and scroll. */
10946 else
10947 goto done;
10948 }
10949 else
10950 clear_glyph_matrix (w->desired_matrix);
10951 }
10952
10953 try_to_scroll:
10954
10955 w->last_modified = make_number (0);
10956 w->last_overlay_modified = make_number (0);
10957
10958 /* Redisplay the mode line. Select the buffer properly for that. */
10959 if (!update_mode_line)
10960 {
10961 update_mode_line = 1;
10962 w->update_mode_line = Qt;
10963 }
10964
10965 /* Try to scroll by specified few lines. */
10966 if ((scroll_conservatively
10967 || scroll_step
10968 || temp_scroll_step
10969 || NUMBERP (current_buffer->scroll_up_aggressively)
10970 || NUMBERP (current_buffer->scroll_down_aggressively))
10971 && !current_buffer->clip_changed
10972 && CHARPOS (startp) >= BEGV
10973 && CHARPOS (startp) <= ZV)
10974 {
10975 /* The function returns -1 if new fonts were loaded, 1 if
10976 successful, 0 if not successful. */
10977 int rc = try_scrolling (window, just_this_one_p,
10978 scroll_conservatively,
10979 scroll_step,
10980 temp_scroll_step, last_line_misfit);
10981 switch (rc)
10982 {
10983 case SCROLLING_SUCCESS:
10984 goto done;
10985
10986 case SCROLLING_NEED_LARGER_MATRICES:
10987 goto need_larger_matrices;
10988
10989 case SCROLLING_FAILED:
10990 break;
10991
10992 default:
10993 abort ();
10994 }
10995 }
10996
10997 /* Finally, just choose place to start which centers point */
10998
10999 recenter:
11000 centering_position = window_box_height (w) / 2;
11001
11002 point_at_top:
11003 /* Jump here with centering_position already set to 0. */
11004
11005 #if GLYPH_DEBUG
11006 debug_method_add (w, "recenter");
11007 #endif
11008
11009 /* w->vscroll = 0; */
11010
11011 /* Forget any previously recorded base line for line number display. */
11012 if (!buffer_unchanged_p)
11013 w->base_line_number = Qnil;
11014
11015 /* Move backward half the height of the window. */
11016 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11017 it.current_y = it.last_visible_y;
11018 move_it_vertically_backward (&it, centering_position);
11019 xassert (IT_CHARPOS (it) >= BEGV);
11020
11021 /* The function move_it_vertically_backward may move over more
11022 than the specified y-distance. If it->w is small, e.g. a
11023 mini-buffer window, we may end up in front of the window's
11024 display area. Start displaying at the start of the line
11025 containing PT in this case. */
11026 if (it.current_y <= 0)
11027 {
11028 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11029 move_it_vertically (&it, 0);
11030 xassert (IT_CHARPOS (it) <= PT);
11031 it.current_y = 0;
11032 }
11033
11034 it.current_x = it.hpos = 0;
11035
11036 /* Set startp here explicitly in case that helps avoid an infinite loop
11037 in case the window-scroll-functions functions get errors. */
11038 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11039
11040 /* Run scroll hooks. */
11041 startp = run_window_scroll_functions (window, it.current.pos);
11042
11043 /* Redisplay the window. */
11044 if (!current_matrix_up_to_date_p
11045 || windows_or_buffers_changed
11046 || cursor_type_changed
11047 /* Don't use try_window_reusing_current_matrix in this case
11048 because it can have changed the buffer. */
11049 || !NILP (Vwindow_scroll_functions)
11050 || !just_this_one_p
11051 || MINI_WINDOW_P (w)
11052 || !try_window_reusing_current_matrix (w))
11053 try_window (window, startp);
11054
11055 /* If new fonts have been loaded (due to fontsets), give up. We
11056 have to start a new redisplay since we need to re-adjust glyph
11057 matrices. */
11058 if (fonts_changed_p)
11059 goto need_larger_matrices;
11060
11061 /* If cursor did not appear assume that the middle of the window is
11062 in the first line of the window. Do it again with the next line.
11063 (Imagine a window of height 100, displaying two lines of height
11064 60. Moving back 50 from it->last_visible_y will end in the first
11065 line.) */
11066 if (w->cursor.vpos < 0)
11067 {
11068 if (!NILP (w->window_end_valid)
11069 && PT >= Z - XFASTINT (w->window_end_pos))
11070 {
11071 clear_glyph_matrix (w->desired_matrix);
11072 move_it_by_lines (&it, 1, 0);
11073 try_window (window, it.current.pos);
11074 }
11075 else if (PT < IT_CHARPOS (it))
11076 {
11077 clear_glyph_matrix (w->desired_matrix);
11078 move_it_by_lines (&it, -1, 0);
11079 try_window (window, it.current.pos);
11080 }
11081 else
11082 {
11083 /* Not much we can do about it. */
11084 }
11085 }
11086
11087 /* Consider the following case: Window starts at BEGV, there is
11088 invisible, intangible text at BEGV, so that display starts at
11089 some point START > BEGV. It can happen that we are called with
11090 PT somewhere between BEGV and START. Try to handle that case. */
11091 if (w->cursor.vpos < 0)
11092 {
11093 struct glyph_row *row = w->current_matrix->rows;
11094 if (row->mode_line_p)
11095 ++row;
11096 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11097 }
11098
11099 if (!make_cursor_line_fully_visible (w))
11100 {
11101 /* If vscroll is enabled, disable it and try again. */
11102 if (w->vscroll)
11103 {
11104 w->vscroll = 0;
11105 clear_glyph_matrix (w->desired_matrix);
11106 goto recenter;
11107 }
11108
11109 /* If centering point failed to make the whole line visible,
11110 put point at the top instead. That has to make the whole line
11111 visible, if it can be done. */
11112 centering_position = 0;
11113 goto point_at_top;
11114 }
11115
11116 done:
11117
11118 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11119 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11120 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11121 ? Qt : Qnil);
11122
11123 /* Display the mode line, if we must. */
11124 if ((update_mode_line
11125 /* If window not full width, must redo its mode line
11126 if (a) the window to its side is being redone and
11127 (b) we do a frame-based redisplay. This is a consequence
11128 of how inverted lines are drawn in frame-based redisplay. */
11129 || (!just_this_one_p
11130 && !FRAME_WINDOW_P (f)
11131 && !WINDOW_FULL_WIDTH_P (w))
11132 /* Line number to display. */
11133 || INTEGERP (w->base_line_pos)
11134 /* Column number is displayed and different from the one displayed. */
11135 || (!NILP (w->column_number_displayed)
11136 && (XFASTINT (w->column_number_displayed)
11137 != (int) current_column ()))) /* iftc */
11138 /* This means that the window has a mode line. */
11139 && (WINDOW_WANTS_MODELINE_P (w)
11140 || WINDOW_WANTS_HEADER_LINE_P (w)))
11141 {
11142 display_mode_lines (w);
11143
11144 /* If mode line height has changed, arrange for a thorough
11145 immediate redisplay using the correct mode line height. */
11146 if (WINDOW_WANTS_MODELINE_P (w)
11147 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11148 {
11149 fonts_changed_p = 1;
11150 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11151 = DESIRED_MODE_LINE_HEIGHT (w);
11152 }
11153
11154 /* If top line height has changed, arrange for a thorough
11155 immediate redisplay using the correct mode line height. */
11156 if (WINDOW_WANTS_HEADER_LINE_P (w)
11157 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11158 {
11159 fonts_changed_p = 1;
11160 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11161 = DESIRED_HEADER_LINE_HEIGHT (w);
11162 }
11163
11164 if (fonts_changed_p)
11165 goto need_larger_matrices;
11166 }
11167
11168 if (!line_number_displayed
11169 && !BUFFERP (w->base_line_pos))
11170 {
11171 w->base_line_pos = Qnil;
11172 w->base_line_number = Qnil;
11173 }
11174
11175 finish_menu_bars:
11176
11177 /* When we reach a frame's selected window, redo the frame's menu bar. */
11178 if (update_mode_line
11179 && EQ (FRAME_SELECTED_WINDOW (f), window))
11180 {
11181 int redisplay_menu_p = 0;
11182 int redisplay_tool_bar_p = 0;
11183
11184 if (FRAME_WINDOW_P (f))
11185 {
11186 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11187 || defined (USE_GTK)
11188 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11189 #else
11190 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11191 #endif
11192 }
11193 else
11194 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11195
11196 if (redisplay_menu_p)
11197 display_menu_bar (w);
11198
11199 #ifdef HAVE_WINDOW_SYSTEM
11200 #ifdef USE_GTK
11201 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11202 #else
11203 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11204 && (FRAME_TOOL_BAR_LINES (f) > 0
11205 || auto_resize_tool_bars_p);
11206
11207 #endif
11208
11209 if (redisplay_tool_bar_p)
11210 redisplay_tool_bar (f);
11211 #endif
11212 }
11213
11214 /* We go to this label, with fonts_changed_p nonzero,
11215 if it is necessary to try again using larger glyph matrices.
11216 We have to redeem the scroll bar even in this case,
11217 because the loop in redisplay_internal expects that. */
11218 need_larger_matrices:
11219 ;
11220 finish_scroll_bars:
11221
11222 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11223 {
11224 int start, end, whole;
11225
11226 /* Calculate the start and end positions for the current window.
11227 At some point, it would be nice to choose between scrollbars
11228 which reflect the whole buffer size, with special markers
11229 indicating narrowing, and scrollbars which reflect only the
11230 visible region.
11231
11232 Note that mini-buffers sometimes aren't displaying any text. */
11233 if (!MINI_WINDOW_P (w)
11234 || (w == XWINDOW (minibuf_window)
11235 && NILP (echo_area_buffer[0])))
11236 {
11237 whole = ZV - BEGV;
11238 start = marker_position (w->start) - BEGV;
11239 /* I don't think this is guaranteed to be right. For the
11240 moment, we'll pretend it is. */
11241 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
11242
11243 if (end < start)
11244 end = start;
11245 if (whole < (end - start))
11246 whole = end - start;
11247 }
11248 else
11249 start = end = whole = 0;
11250
11251 /* Indicate what this scroll bar ought to be displaying now. */
11252 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11253
11254 /* Note that we actually used the scroll bar attached to this
11255 window, so it shouldn't be deleted at the end of redisplay. */
11256 redeem_scroll_bar_hook (w);
11257 }
11258
11259 /* Restore current_buffer and value of point in it. */
11260 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
11261 set_buffer_internal_1 (old);
11262 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
11263
11264 unbind_to (count, Qnil);
11265 }
11266
11267
11268 /* Build the complete desired matrix of WINDOW with a window start
11269 buffer position POS. Value is non-zero if successful. It is zero
11270 if fonts were loaded during redisplay which makes re-adjusting
11271 glyph matrices necessary. */
11272
11273 int
11274 try_window (window, pos)
11275 Lisp_Object window;
11276 struct text_pos pos;
11277 {
11278 struct window *w = XWINDOW (window);
11279 struct it it;
11280 struct glyph_row *last_text_row = NULL;
11281
11282 /* Make POS the new window start. */
11283 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
11284
11285 /* Mark cursor position as unknown. No overlay arrow seen. */
11286 w->cursor.vpos = -1;
11287 overlay_arrow_seen = 0;
11288
11289 /* Initialize iterator and info to start at POS. */
11290 start_display (&it, w, pos);
11291
11292 /* Display all lines of W. */
11293 while (it.current_y < it.last_visible_y)
11294 {
11295 if (display_line (&it))
11296 last_text_row = it.glyph_row - 1;
11297 if (fonts_changed_p)
11298 return 0;
11299 }
11300
11301 /* If bottom moved off end of frame, change mode line percentage. */
11302 if (XFASTINT (w->window_end_pos) <= 0
11303 && Z != IT_CHARPOS (it))
11304 w->update_mode_line = Qt;
11305
11306 /* Set window_end_pos to the offset of the last character displayed
11307 on the window from the end of current_buffer. Set
11308 window_end_vpos to its row number. */
11309 if (last_text_row)
11310 {
11311 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
11312 w->window_end_bytepos
11313 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11314 w->window_end_pos
11315 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11316 w->window_end_vpos
11317 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11318 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
11319 ->displays_text_p);
11320 }
11321 else
11322 {
11323 w->window_end_bytepos = 0;
11324 w->window_end_pos = w->window_end_vpos = make_number (0);
11325 }
11326
11327 /* But that is not valid info until redisplay finishes. */
11328 w->window_end_valid = Qnil;
11329 return 1;
11330 }
11331
11332
11333 \f
11334 /************************************************************************
11335 Window redisplay reusing current matrix when buffer has not changed
11336 ************************************************************************/
11337
11338 /* Try redisplay of window W showing an unchanged buffer with a
11339 different window start than the last time it was displayed by
11340 reusing its current matrix. Value is non-zero if successful.
11341 W->start is the new window start. */
11342
11343 static int
11344 try_window_reusing_current_matrix (w)
11345 struct window *w;
11346 {
11347 struct frame *f = XFRAME (w->frame);
11348 struct glyph_row *row, *bottom_row;
11349 struct it it;
11350 struct run run;
11351 struct text_pos start, new_start;
11352 int nrows_scrolled, i;
11353 struct glyph_row *last_text_row;
11354 struct glyph_row *last_reused_text_row;
11355 struct glyph_row *start_row;
11356 int start_vpos, min_y, max_y;
11357
11358 #if GLYPH_DEBUG
11359 if (inhibit_try_window_reusing)
11360 return 0;
11361 #endif
11362
11363 if (/* This function doesn't handle terminal frames. */
11364 !FRAME_WINDOW_P (f)
11365 /* Don't try to reuse the display if windows have been split
11366 or such. */
11367 || windows_or_buffers_changed
11368 || cursor_type_changed)
11369 return 0;
11370
11371 /* Can't do this if region may have changed. */
11372 if ((!NILP (Vtransient_mark_mode)
11373 && !NILP (current_buffer->mark_active))
11374 || !NILP (w->region_showing)
11375 || !NILP (Vshow_trailing_whitespace))
11376 return 0;
11377
11378 /* If top-line visibility has changed, give up. */
11379 if (WINDOW_WANTS_HEADER_LINE_P (w)
11380 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
11381 return 0;
11382
11383 /* Give up if old or new display is scrolled vertically. We could
11384 make this function handle this, but right now it doesn't. */
11385 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11386 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
11387 return 0;
11388
11389 /* The variable new_start now holds the new window start. The old
11390 start `start' can be determined from the current matrix. */
11391 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
11392 start = start_row->start.pos;
11393 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
11394
11395 /* Clear the desired matrix for the display below. */
11396 clear_glyph_matrix (w->desired_matrix);
11397
11398 if (CHARPOS (new_start) <= CHARPOS (start))
11399 {
11400 int first_row_y;
11401
11402 /* Don't use this method if the display starts with an ellipsis
11403 displayed for invisible text. It's not easy to handle that case
11404 below, and it's certainly not worth the effort since this is
11405 not a frequent case. */
11406 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
11407 return 0;
11408
11409 IF_DEBUG (debug_method_add (w, "twu1"));
11410
11411 /* Display up to a row that can be reused. The variable
11412 last_text_row is set to the last row displayed that displays
11413 text. Note that it.vpos == 0 if or if not there is a
11414 header-line; it's not the same as the MATRIX_ROW_VPOS! */
11415 start_display (&it, w, new_start);
11416 first_row_y = it.current_y;
11417 w->cursor.vpos = -1;
11418 last_text_row = last_reused_text_row = NULL;
11419
11420 while (it.current_y < it.last_visible_y
11421 && IT_CHARPOS (it) < CHARPOS (start)
11422 && !fonts_changed_p)
11423 if (display_line (&it))
11424 last_text_row = it.glyph_row - 1;
11425
11426 /* A value of current_y < last_visible_y means that we stopped
11427 at the previous window start, which in turn means that we
11428 have at least one reusable row. */
11429 if (it.current_y < it.last_visible_y)
11430 {
11431 /* IT.vpos always starts from 0; it counts text lines. */
11432 nrows_scrolled = it.vpos;
11433
11434 /* Find PT if not already found in the lines displayed. */
11435 if (w->cursor.vpos < 0)
11436 {
11437 int dy = it.current_y - first_row_y;
11438
11439 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11440 row = row_containing_pos (w, PT, row, NULL, dy);
11441 if (row)
11442 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
11443 dy, nrows_scrolled);
11444 else
11445 {
11446 clear_glyph_matrix (w->desired_matrix);
11447 return 0;
11448 }
11449 }
11450
11451 /* Scroll the display. Do it before the current matrix is
11452 changed. The problem here is that update has not yet
11453 run, i.e. part of the current matrix is not up to date.
11454 scroll_run_hook will clear the cursor, and use the
11455 current matrix to get the height of the row the cursor is
11456 in. */
11457 run.current_y = first_row_y;
11458 run.desired_y = it.current_y;
11459 run.height = it.last_visible_y - it.current_y;
11460
11461 if (run.height > 0 && run.current_y != run.desired_y)
11462 {
11463 update_begin (f);
11464 rif->update_window_begin_hook (w);
11465 rif->clear_mouse_face (w);
11466 rif->scroll_run_hook (w, &run);
11467 rif->update_window_end_hook (w, 0, 0);
11468 update_end (f);
11469 }
11470
11471 /* Shift current matrix down by nrows_scrolled lines. */
11472 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11473 rotate_matrix (w->current_matrix,
11474 start_vpos,
11475 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11476 nrows_scrolled);
11477
11478 /* Disable lines that must be updated. */
11479 for (i = 0; i < it.vpos; ++i)
11480 (start_row + i)->enabled_p = 0;
11481
11482 /* Re-compute Y positions. */
11483 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11484 max_y = it.last_visible_y;
11485 for (row = start_row + nrows_scrolled;
11486 row < bottom_row;
11487 ++row)
11488 {
11489 row->y = it.current_y;
11490 row->visible_height = row->height;
11491
11492 if (row->y < min_y)
11493 row->visible_height -= min_y - row->y;
11494 if (row->y + row->height > max_y)
11495 row->visible_height -= row->y + row->height - max_y;
11496
11497 it.current_y += row->height;
11498
11499 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11500 last_reused_text_row = row;
11501 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
11502 break;
11503 }
11504
11505 /* Disable lines in the current matrix which are now
11506 below the window. */
11507 for (++row; row < bottom_row; ++row)
11508 row->enabled_p = 0;
11509 }
11510
11511 /* Update window_end_pos etc.; last_reused_text_row is the last
11512 reused row from the current matrix containing text, if any.
11513 The value of last_text_row is the last displayed line
11514 containing text. */
11515 if (last_reused_text_row)
11516 {
11517 w->window_end_bytepos
11518 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
11519 w->window_end_pos
11520 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
11521 w->window_end_vpos
11522 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
11523 w->current_matrix));
11524 }
11525 else if (last_text_row)
11526 {
11527 w->window_end_bytepos
11528 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11529 w->window_end_pos
11530 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11531 w->window_end_vpos
11532 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11533 }
11534 else
11535 {
11536 /* This window must be completely empty. */
11537 w->window_end_bytepos = 0;
11538 w->window_end_pos = w->window_end_vpos = make_number (0);
11539 }
11540 w->window_end_valid = Qnil;
11541
11542 /* Update hint: don't try scrolling again in update_window. */
11543 w->desired_matrix->no_scrolling_p = 1;
11544
11545 #if GLYPH_DEBUG
11546 debug_method_add (w, "try_window_reusing_current_matrix 1");
11547 #endif
11548 return 1;
11549 }
11550 else if (CHARPOS (new_start) > CHARPOS (start))
11551 {
11552 struct glyph_row *pt_row, *row;
11553 struct glyph_row *first_reusable_row;
11554 struct glyph_row *first_row_to_display;
11555 int dy;
11556 int yb = window_text_bottom_y (w);
11557
11558 /* Find the row starting at new_start, if there is one. Don't
11559 reuse a partially visible line at the end. */
11560 first_reusable_row = start_row;
11561 while (first_reusable_row->enabled_p
11562 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
11563 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11564 < CHARPOS (new_start)))
11565 ++first_reusable_row;
11566
11567 /* Give up if there is no row to reuse. */
11568 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
11569 || !first_reusable_row->enabled_p
11570 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
11571 != CHARPOS (new_start)))
11572 return 0;
11573
11574 /* We can reuse fully visible rows beginning with
11575 first_reusable_row to the end of the window. Set
11576 first_row_to_display to the first row that cannot be reused.
11577 Set pt_row to the row containing point, if there is any. */
11578 pt_row = NULL;
11579 for (first_row_to_display = first_reusable_row;
11580 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
11581 ++first_row_to_display)
11582 {
11583 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
11584 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
11585 pt_row = first_row_to_display;
11586 }
11587
11588 /* Start displaying at the start of first_row_to_display. */
11589 xassert (first_row_to_display->y < yb);
11590 init_to_row_start (&it, w, first_row_to_display);
11591
11592 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
11593 - start_vpos);
11594 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
11595 - nrows_scrolled);
11596 it.current_y = (first_row_to_display->y - first_reusable_row->y
11597 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
11598
11599 /* Display lines beginning with first_row_to_display in the
11600 desired matrix. Set last_text_row to the last row displayed
11601 that displays text. */
11602 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
11603 if (pt_row == NULL)
11604 w->cursor.vpos = -1;
11605 last_text_row = NULL;
11606 while (it.current_y < it.last_visible_y && !fonts_changed_p)
11607 if (display_line (&it))
11608 last_text_row = it.glyph_row - 1;
11609
11610 /* Give up If point isn't in a row displayed or reused. */
11611 if (w->cursor.vpos < 0)
11612 {
11613 clear_glyph_matrix (w->desired_matrix);
11614 return 0;
11615 }
11616
11617 /* If point is in a reused row, adjust y and vpos of the cursor
11618 position. */
11619 if (pt_row)
11620 {
11621 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
11622 w->current_matrix);
11623 w->cursor.y -= first_reusable_row->y;
11624 }
11625
11626 /* Scroll the display. */
11627 run.current_y = first_reusable_row->y;
11628 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11629 run.height = it.last_visible_y - run.current_y;
11630 dy = run.current_y - run.desired_y;
11631
11632 if (run.height)
11633 {
11634 struct frame *f = XFRAME (WINDOW_FRAME (w));
11635 update_begin (f);
11636 rif->update_window_begin_hook (w);
11637 rif->clear_mouse_face (w);
11638 rif->scroll_run_hook (w, &run);
11639 rif->update_window_end_hook (w, 0, 0);
11640 update_end (f);
11641 }
11642
11643 /* Adjust Y positions of reused rows. */
11644 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
11645 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11646 max_y = it.last_visible_y;
11647 for (row = first_reusable_row; row < first_row_to_display; ++row)
11648 {
11649 row->y -= dy;
11650 row->visible_height = row->height;
11651 if (row->y < min_y)
11652 row->visible_height -= min_y - row->y;
11653 if (row->y + row->height > max_y)
11654 row->visible_height -= row->y + row->height - max_y;
11655 }
11656
11657 /* Scroll the current matrix. */
11658 xassert (nrows_scrolled > 0);
11659 rotate_matrix (w->current_matrix,
11660 start_vpos,
11661 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
11662 -nrows_scrolled);
11663
11664 /* Disable rows not reused. */
11665 for (row -= nrows_scrolled; row < bottom_row; ++row)
11666 row->enabled_p = 0;
11667
11668 /* Adjust window end. A null value of last_text_row means that
11669 the window end is in reused rows which in turn means that
11670 only its vpos can have changed. */
11671 if (last_text_row)
11672 {
11673 w->window_end_bytepos
11674 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
11675 w->window_end_pos
11676 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
11677 w->window_end_vpos
11678 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
11679 }
11680 else
11681 {
11682 w->window_end_vpos
11683 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
11684 }
11685
11686 w->window_end_valid = Qnil;
11687 w->desired_matrix->no_scrolling_p = 1;
11688
11689 #if GLYPH_DEBUG
11690 debug_method_add (w, "try_window_reusing_current_matrix 2");
11691 #endif
11692 return 1;
11693 }
11694
11695 return 0;
11696 }
11697
11698
11699 \f
11700 /************************************************************************
11701 Window redisplay reusing current matrix when buffer has changed
11702 ************************************************************************/
11703
11704 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
11705 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
11706 int *, int *));
11707 static struct glyph_row *
11708 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
11709 struct glyph_row *));
11710
11711
11712 /* Return the last row in MATRIX displaying text. If row START is
11713 non-null, start searching with that row. IT gives the dimensions
11714 of the display. Value is null if matrix is empty; otherwise it is
11715 a pointer to the row found. */
11716
11717 static struct glyph_row *
11718 find_last_row_displaying_text (matrix, it, start)
11719 struct glyph_matrix *matrix;
11720 struct it *it;
11721 struct glyph_row *start;
11722 {
11723 struct glyph_row *row, *row_found;
11724
11725 /* Set row_found to the last row in IT->w's current matrix
11726 displaying text. The loop looks funny but think of partially
11727 visible lines. */
11728 row_found = NULL;
11729 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
11730 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11731 {
11732 xassert (row->enabled_p);
11733 row_found = row;
11734 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
11735 break;
11736 ++row;
11737 }
11738
11739 return row_found;
11740 }
11741
11742
11743 /* Return the last row in the current matrix of W that is not affected
11744 by changes at the start of current_buffer that occurred since W's
11745 current matrix was built. Value is null if no such row exists.
11746
11747 BEG_UNCHANGED us the number of characters unchanged at the start of
11748 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
11749 first changed character in current_buffer. Characters at positions <
11750 BEG + BEG_UNCHANGED are at the same buffer positions as they were
11751 when the current matrix was built. */
11752
11753 static struct glyph_row *
11754 find_last_unchanged_at_beg_row (w)
11755 struct window *w;
11756 {
11757 int first_changed_pos = BEG + BEG_UNCHANGED;
11758 struct glyph_row *row;
11759 struct glyph_row *row_found = NULL;
11760 int yb = window_text_bottom_y (w);
11761
11762 /* Find the last row displaying unchanged text. */
11763 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11764 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
11765 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
11766 {
11767 if (/* If row ends before first_changed_pos, it is unchanged,
11768 except in some case. */
11769 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
11770 /* When row ends in ZV and we write at ZV it is not
11771 unchanged. */
11772 && !row->ends_at_zv_p
11773 /* When first_changed_pos is the end of a continued line,
11774 row is not unchanged because it may be no longer
11775 continued. */
11776 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
11777 && row->continued_p))
11778 row_found = row;
11779
11780 /* Stop if last visible row. */
11781 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
11782 break;
11783
11784 ++row;
11785 }
11786
11787 return row_found;
11788 }
11789
11790
11791 /* Find the first glyph row in the current matrix of W that is not
11792 affected by changes at the end of current_buffer since the
11793 time W's current matrix was built.
11794
11795 Return in *DELTA the number of chars by which buffer positions in
11796 unchanged text at the end of current_buffer must be adjusted.
11797
11798 Return in *DELTA_BYTES the corresponding number of bytes.
11799
11800 Value is null if no such row exists, i.e. all rows are affected by
11801 changes. */
11802
11803 static struct glyph_row *
11804 find_first_unchanged_at_end_row (w, delta, delta_bytes)
11805 struct window *w;
11806 int *delta, *delta_bytes;
11807 {
11808 struct glyph_row *row;
11809 struct glyph_row *row_found = NULL;
11810
11811 *delta = *delta_bytes = 0;
11812
11813 /* Display must not have been paused, otherwise the current matrix
11814 is not up to date. */
11815 if (NILP (w->window_end_valid))
11816 abort ();
11817
11818 /* A value of window_end_pos >= END_UNCHANGED means that the window
11819 end is in the range of changed text. If so, there is no
11820 unchanged row at the end of W's current matrix. */
11821 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
11822 return NULL;
11823
11824 /* Set row to the last row in W's current matrix displaying text. */
11825 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11826
11827 /* If matrix is entirely empty, no unchanged row exists. */
11828 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
11829 {
11830 /* The value of row is the last glyph row in the matrix having a
11831 meaningful buffer position in it. The end position of row
11832 corresponds to window_end_pos. This allows us to translate
11833 buffer positions in the current matrix to current buffer
11834 positions for characters not in changed text. */
11835 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
11836 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
11837 int last_unchanged_pos, last_unchanged_pos_old;
11838 struct glyph_row *first_text_row
11839 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11840
11841 *delta = Z - Z_old;
11842 *delta_bytes = Z_BYTE - Z_BYTE_old;
11843
11844 /* Set last_unchanged_pos to the buffer position of the last
11845 character in the buffer that has not been changed. Z is the
11846 index + 1 of the last character in current_buffer, i.e. by
11847 subtracting END_UNCHANGED we get the index of the last
11848 unchanged character, and we have to add BEG to get its buffer
11849 position. */
11850 last_unchanged_pos = Z - END_UNCHANGED + BEG;
11851 last_unchanged_pos_old = last_unchanged_pos - *delta;
11852
11853 /* Search backward from ROW for a row displaying a line that
11854 starts at a minimum position >= last_unchanged_pos_old. */
11855 for (; row > first_text_row; --row)
11856 {
11857 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
11858 abort ();
11859
11860 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
11861 row_found = row;
11862 }
11863 }
11864
11865 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
11866 abort ();
11867
11868 return row_found;
11869 }
11870
11871
11872 /* Make sure that glyph rows in the current matrix of window W
11873 reference the same glyph memory as corresponding rows in the
11874 frame's frame matrix. This function is called after scrolling W's
11875 current matrix on a terminal frame in try_window_id and
11876 try_window_reusing_current_matrix. */
11877
11878 static void
11879 sync_frame_with_window_matrix_rows (w)
11880 struct window *w;
11881 {
11882 struct frame *f = XFRAME (w->frame);
11883 struct glyph_row *window_row, *window_row_end, *frame_row;
11884
11885 /* Preconditions: W must be a leaf window and full-width. Its frame
11886 must have a frame matrix. */
11887 xassert (NILP (w->hchild) && NILP (w->vchild));
11888 xassert (WINDOW_FULL_WIDTH_P (w));
11889 xassert (!FRAME_WINDOW_P (f));
11890
11891 /* If W is a full-width window, glyph pointers in W's current matrix
11892 have, by definition, to be the same as glyph pointers in the
11893 corresponding frame matrix. Note that frame matrices have no
11894 marginal areas (see build_frame_matrix). */
11895 window_row = w->current_matrix->rows;
11896 window_row_end = window_row + w->current_matrix->nrows;
11897 frame_row = f->current_matrix->rows + XFASTINT (w->top);
11898 while (window_row < window_row_end)
11899 {
11900 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
11901 struct glyph *end = window_row->glyphs[LAST_AREA];
11902
11903 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
11904 frame_row->glyphs[TEXT_AREA] = start;
11905 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
11906 frame_row->glyphs[LAST_AREA] = end;
11907
11908 /* Disable frame rows whose corresponding window rows have
11909 been disabled in try_window_id. */
11910 if (!window_row->enabled_p)
11911 frame_row->enabled_p = 0;
11912
11913 ++window_row, ++frame_row;
11914 }
11915 }
11916
11917
11918 /* Find the glyph row in window W containing CHARPOS. Consider all
11919 rows between START and END (not inclusive). END null means search
11920 all rows to the end of the display area of W. Value is the row
11921 containing CHARPOS or null. */
11922
11923 struct glyph_row *
11924 row_containing_pos (w, charpos, start, end, dy)
11925 struct window *w;
11926 int charpos;
11927 struct glyph_row *start, *end;
11928 int dy;
11929 {
11930 struct glyph_row *row = start;
11931 int last_y;
11932
11933 /* If we happen to start on a header-line, skip that. */
11934 if (row->mode_line_p)
11935 ++row;
11936
11937 if ((end && row >= end) || !row->enabled_p)
11938 return NULL;
11939
11940 last_y = window_text_bottom_y (w) - dy;
11941
11942 while (1)
11943 {
11944 /* Give up if we have gone too far. */
11945 if (end && row >= end)
11946 return NULL;
11947 /* This formerly returned if they were equal.
11948 I think that both quantities are of a "last plus one" type;
11949 if so, when they are equal, the row is within the screen. -- rms. */
11950 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
11951 return NULL;
11952
11953 /* If it is in this row, return this row. */
11954 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
11955 || (MATRIX_ROW_END_CHARPOS (row) == charpos
11956 /* The end position of a row equals the start
11957 position of the next row. If CHARPOS is there, we
11958 would rather display it in the next line, except
11959 when this line ends in ZV. */
11960 && !row->ends_at_zv_p
11961 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11962 && charpos >= MATRIX_ROW_START_CHARPOS (row))
11963 return row;
11964 ++row;
11965 }
11966 }
11967
11968
11969 /* Try to redisplay window W by reusing its existing display. W's
11970 current matrix must be up to date when this function is called,
11971 i.e. window_end_valid must not be nil.
11972
11973 Value is
11974
11975 1 if display has been updated
11976 0 if otherwise unsuccessful
11977 -1 if redisplay with same window start is known not to succeed
11978
11979 The following steps are performed:
11980
11981 1. Find the last row in the current matrix of W that is not
11982 affected by changes at the start of current_buffer. If no such row
11983 is found, give up.
11984
11985 2. Find the first row in W's current matrix that is not affected by
11986 changes at the end of current_buffer. Maybe there is no such row.
11987
11988 3. Display lines beginning with the row + 1 found in step 1 to the
11989 row found in step 2 or, if step 2 didn't find a row, to the end of
11990 the window.
11991
11992 4. If cursor is not known to appear on the window, give up.
11993
11994 5. If display stopped at the row found in step 2, scroll the
11995 display and current matrix as needed.
11996
11997 6. Maybe display some lines at the end of W, if we must. This can
11998 happen under various circumstances, like a partially visible line
11999 becoming fully visible, or because newly displayed lines are displayed
12000 in smaller font sizes.
12001
12002 7. Update W's window end information. */
12003
12004 static int
12005 try_window_id (w)
12006 struct window *w;
12007 {
12008 struct frame *f = XFRAME (w->frame);
12009 struct glyph_matrix *current_matrix = w->current_matrix;
12010 struct glyph_matrix *desired_matrix = w->desired_matrix;
12011 struct glyph_row *last_unchanged_at_beg_row;
12012 struct glyph_row *first_unchanged_at_end_row;
12013 struct glyph_row *row;
12014 struct glyph_row *bottom_row;
12015 int bottom_vpos;
12016 struct it it;
12017 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12018 struct text_pos start_pos;
12019 struct run run;
12020 int first_unchanged_at_end_vpos = 0;
12021 struct glyph_row *last_text_row, *last_text_row_at_end;
12022 struct text_pos start;
12023 int first_changed_charpos, last_changed_charpos;
12024
12025 #if GLYPH_DEBUG
12026 if (inhibit_try_window_id)
12027 return 0;
12028 #endif
12029
12030 /* This is handy for debugging. */
12031 #if 0
12032 #define GIVE_UP(X) \
12033 do { \
12034 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12035 return 0; \
12036 } while (0)
12037 #else
12038 #define GIVE_UP(X) return 0
12039 #endif
12040
12041 SET_TEXT_POS_FROM_MARKER (start, w->start);
12042
12043 /* Don't use this for mini-windows because these can show
12044 messages and mini-buffers, and we don't handle that here. */
12045 if (MINI_WINDOW_P (w))
12046 GIVE_UP (1);
12047
12048 /* This flag is used to prevent redisplay optimizations. */
12049 if (windows_or_buffers_changed || cursor_type_changed)
12050 GIVE_UP (2);
12051
12052 /* Verify that narrowing has not changed.
12053 Also verify that we were not told to prevent redisplay optimizations.
12054 It would be nice to further
12055 reduce the number of cases where this prevents try_window_id. */
12056 if (current_buffer->clip_changed
12057 || current_buffer->prevent_redisplay_optimizations_p)
12058 GIVE_UP (3);
12059
12060 /* Window must either use window-based redisplay or be full width. */
12061 if (!FRAME_WINDOW_P (f)
12062 && (!line_ins_del_ok
12063 || !WINDOW_FULL_WIDTH_P (w)))
12064 GIVE_UP (4);
12065
12066 /* Give up if point is not known NOT to appear in W. */
12067 if (PT < CHARPOS (start))
12068 GIVE_UP (5);
12069
12070 /* Another way to prevent redisplay optimizations. */
12071 if (XFASTINT (w->last_modified) == 0)
12072 GIVE_UP (6);
12073
12074 /* Verify that window is not hscrolled. */
12075 if (XFASTINT (w->hscroll) != 0)
12076 GIVE_UP (7);
12077
12078 /* Verify that display wasn't paused. */
12079 if (NILP (w->window_end_valid))
12080 GIVE_UP (8);
12081
12082 /* Can't use this if highlighting a region because a cursor movement
12083 will do more than just set the cursor. */
12084 if (!NILP (Vtransient_mark_mode)
12085 && !NILP (current_buffer->mark_active))
12086 GIVE_UP (9);
12087
12088 /* Likewise if highlighting trailing whitespace. */
12089 if (!NILP (Vshow_trailing_whitespace))
12090 GIVE_UP (11);
12091
12092 /* Likewise if showing a region. */
12093 if (!NILP (w->region_showing))
12094 GIVE_UP (10);
12095
12096 /* Can use this if overlay arrow position and or string have changed. */
12097 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12098 || !EQ (last_arrow_string, Voverlay_arrow_string))
12099 GIVE_UP (12);
12100
12101
12102 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12103 only if buffer has really changed. The reason is that the gap is
12104 initially at Z for freshly visited files. The code below would
12105 set end_unchanged to 0 in that case. */
12106 if (MODIFF > SAVE_MODIFF
12107 /* This seems to happen sometimes after saving a buffer. */
12108 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12109 {
12110 if (GPT - BEG < BEG_UNCHANGED)
12111 BEG_UNCHANGED = GPT - BEG;
12112 if (Z - GPT < END_UNCHANGED)
12113 END_UNCHANGED = Z - GPT;
12114 }
12115
12116 /* The position of the first and last character that has been changed. */
12117 first_changed_charpos = BEG + BEG_UNCHANGED;
12118 last_changed_charpos = Z - END_UNCHANGED;
12119
12120 /* If window starts after a line end, and the last change is in
12121 front of that newline, then changes don't affect the display.
12122 This case happens with stealth-fontification. Note that although
12123 the display is unchanged, glyph positions in the matrix have to
12124 be adjusted, of course. */
12125 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12126 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12127 && ((last_changed_charpos < CHARPOS (start)
12128 && CHARPOS (start) == BEGV)
12129 || (last_changed_charpos < CHARPOS (start) - 1
12130 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12131 {
12132 int Z_old, delta, Z_BYTE_old, delta_bytes;
12133 struct glyph_row *r0;
12134
12135 /* Compute how many chars/bytes have been added to or removed
12136 from the buffer. */
12137 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12138 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12139 delta = Z - Z_old;
12140 delta_bytes = Z_BYTE - Z_BYTE_old;
12141
12142 /* Give up if PT is not in the window. Note that it already has
12143 been checked at the start of try_window_id that PT is not in
12144 front of the window start. */
12145 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12146 GIVE_UP (13);
12147
12148 /* If window start is unchanged, we can reuse the whole matrix
12149 as is, after adjusting glyph positions. No need to compute
12150 the window end again, since its offset from Z hasn't changed. */
12151 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12152 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12153 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
12154 {
12155 /* Adjust positions in the glyph matrix. */
12156 if (delta || delta_bytes)
12157 {
12158 struct glyph_row *r1
12159 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12160 increment_matrix_positions (w->current_matrix,
12161 MATRIX_ROW_VPOS (r0, current_matrix),
12162 MATRIX_ROW_VPOS (r1, current_matrix),
12163 delta, delta_bytes);
12164 }
12165
12166 /* Set the cursor. */
12167 row = row_containing_pos (w, PT, r0, NULL, 0);
12168 if (row)
12169 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12170 else
12171 abort ();
12172 return 1;
12173 }
12174 }
12175
12176 /* Handle the case that changes are all below what is displayed in
12177 the window, and that PT is in the window. This shortcut cannot
12178 be taken if ZV is visible in the window, and text has been added
12179 there that is visible in the window. */
12180 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12181 /* ZV is not visible in the window, or there are no
12182 changes at ZV, actually. */
12183 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12184 || first_changed_charpos == last_changed_charpos))
12185 {
12186 struct glyph_row *r0;
12187
12188 /* Give up if PT is not in the window. Note that it already has
12189 been checked at the start of try_window_id that PT is not in
12190 front of the window start. */
12191 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12192 GIVE_UP (14);
12193
12194 /* If window start is unchanged, we can reuse the whole matrix
12195 as is, without changing glyph positions since no text has
12196 been added/removed in front of the window end. */
12197 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12198 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
12199 {
12200 /* We have to compute the window end anew since text
12201 can have been added/removed after it. */
12202 w->window_end_pos
12203 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12204 w->window_end_bytepos
12205 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12206
12207 /* Set the cursor. */
12208 row = row_containing_pos (w, PT, r0, NULL, 0);
12209 if (row)
12210 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12211 else
12212 abort ();
12213 return 2;
12214 }
12215 }
12216
12217 /* Give up if window start is in the changed area.
12218
12219 The condition used to read
12220
12221 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12222
12223 but why that was tested escapes me at the moment. */
12224 if (CHARPOS (start) >= first_changed_charpos
12225 && CHARPOS (start) <= last_changed_charpos)
12226 GIVE_UP (15);
12227
12228 /* Check that window start agrees with the start of the first glyph
12229 row in its current matrix. Check this after we know the window
12230 start is not in changed text, otherwise positions would not be
12231 comparable. */
12232 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12233 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12234 GIVE_UP (16);
12235
12236 /* Give up if the window ends in strings. Overlay strings
12237 at the end are difficult to handle, so don't try. */
12238 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12239 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12240 GIVE_UP (20);
12241
12242 /* Compute the position at which we have to start displaying new
12243 lines. Some of the lines at the top of the window might be
12244 reusable because they are not displaying changed text. Find the
12245 last row in W's current matrix not affected by changes at the
12246 start of current_buffer. Value is null if changes start in the
12247 first line of window. */
12248 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12249 if (last_unchanged_at_beg_row)
12250 {
12251 /* Avoid starting to display in the moddle of a character, a TAB
12252 for instance. This is easier than to set up the iterator
12253 exactly, and it's not a frequent case, so the additional
12254 effort wouldn't really pay off. */
12255 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12256 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
12257 && last_unchanged_at_beg_row > w->current_matrix->rows)
12258 --last_unchanged_at_beg_row;
12259
12260 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
12261 GIVE_UP (17);
12262
12263 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
12264 GIVE_UP (18);
12265 start_pos = it.current.pos;
12266
12267 /* Start displaying new lines in the desired matrix at the same
12268 vpos we would use in the current matrix, i.e. below
12269 last_unchanged_at_beg_row. */
12270 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
12271 current_matrix);
12272 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12273 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
12274
12275 xassert (it.hpos == 0 && it.current_x == 0);
12276 }
12277 else
12278 {
12279 /* There are no reusable lines at the start of the window.
12280 Start displaying in the first line. */
12281 start_display (&it, w, start);
12282 start_pos = it.current.pos;
12283 }
12284
12285 /* Find the first row that is not affected by changes at the end of
12286 the buffer. Value will be null if there is no unchanged row, in
12287 which case we must redisplay to the end of the window. delta
12288 will be set to the value by which buffer positions beginning with
12289 first_unchanged_at_end_row have to be adjusted due to text
12290 changes. */
12291 first_unchanged_at_end_row
12292 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
12293 IF_DEBUG (debug_delta = delta);
12294 IF_DEBUG (debug_delta_bytes = delta_bytes);
12295
12296 /* Set stop_pos to the buffer position up to which we will have to
12297 display new lines. If first_unchanged_at_end_row != NULL, this
12298 is the buffer position of the start of the line displayed in that
12299 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12300 that we don't stop at a buffer position. */
12301 stop_pos = 0;
12302 if (first_unchanged_at_end_row)
12303 {
12304 xassert (last_unchanged_at_beg_row == NULL
12305 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
12306
12307 /* If this is a continuation line, move forward to the next one
12308 that isn't. Changes in lines above affect this line.
12309 Caution: this may move first_unchanged_at_end_row to a row
12310 not displaying text. */
12311 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
12312 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12313 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12314 < it.last_visible_y))
12315 ++first_unchanged_at_end_row;
12316
12317 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
12318 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
12319 >= it.last_visible_y))
12320 first_unchanged_at_end_row = NULL;
12321 else
12322 {
12323 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
12324 + delta);
12325 first_unchanged_at_end_vpos
12326 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
12327 xassert (stop_pos >= Z - END_UNCHANGED);
12328 }
12329 }
12330 else if (last_unchanged_at_beg_row == NULL)
12331 GIVE_UP (19);
12332
12333
12334 #if GLYPH_DEBUG
12335
12336 /* Either there is no unchanged row at the end, or the one we have
12337 now displays text. This is a necessary condition for the window
12338 end pos calculation at the end of this function. */
12339 xassert (first_unchanged_at_end_row == NULL
12340 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
12341
12342 debug_last_unchanged_at_beg_vpos
12343 = (last_unchanged_at_beg_row
12344 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
12345 : -1);
12346 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
12347
12348 #endif /* GLYPH_DEBUG != 0 */
12349
12350
12351 /* Display new lines. Set last_text_row to the last new line
12352 displayed which has text on it, i.e. might end up as being the
12353 line where the window_end_vpos is. */
12354 w->cursor.vpos = -1;
12355 last_text_row = NULL;
12356 overlay_arrow_seen = 0;
12357 while (it.current_y < it.last_visible_y
12358 && !fonts_changed_p
12359 && (first_unchanged_at_end_row == NULL
12360 || IT_CHARPOS (it) < stop_pos))
12361 {
12362 if (display_line (&it))
12363 last_text_row = it.glyph_row - 1;
12364 }
12365
12366 if (fonts_changed_p)
12367 return -1;
12368
12369
12370 /* Compute differences in buffer positions, y-positions etc. for
12371 lines reused at the bottom of the window. Compute what we can
12372 scroll. */
12373 if (first_unchanged_at_end_row
12374 /* No lines reused because we displayed everything up to the
12375 bottom of the window. */
12376 && it.current_y < it.last_visible_y)
12377 {
12378 dvpos = (it.vpos
12379 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
12380 current_matrix));
12381 dy = it.current_y - first_unchanged_at_end_row->y;
12382 run.current_y = first_unchanged_at_end_row->y;
12383 run.desired_y = run.current_y + dy;
12384 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
12385 }
12386 else
12387 {
12388 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
12389 first_unchanged_at_end_row = NULL;
12390 }
12391 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
12392
12393
12394 /* Find the cursor if not already found. We have to decide whether
12395 PT will appear on this window (it sometimes doesn't, but this is
12396 not a very frequent case.) This decision has to be made before
12397 the current matrix is altered. A value of cursor.vpos < 0 means
12398 that PT is either in one of the lines beginning at
12399 first_unchanged_at_end_row or below the window. Don't care for
12400 lines that might be displayed later at the window end; as
12401 mentioned, this is not a frequent case. */
12402 if (w->cursor.vpos < 0)
12403 {
12404 /* Cursor in unchanged rows at the top? */
12405 if (PT < CHARPOS (start_pos)
12406 && last_unchanged_at_beg_row)
12407 {
12408 row = row_containing_pos (w, PT,
12409 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
12410 last_unchanged_at_beg_row + 1, 0);
12411 if (row)
12412 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12413 }
12414
12415 /* Start from first_unchanged_at_end_row looking for PT. */
12416 else if (first_unchanged_at_end_row)
12417 {
12418 row = row_containing_pos (w, PT - delta,
12419 first_unchanged_at_end_row, NULL, 0);
12420 if (row)
12421 set_cursor_from_row (w, row, w->current_matrix, delta,
12422 delta_bytes, dy, dvpos);
12423 }
12424
12425 /* Give up if cursor was not found. */
12426 if (w->cursor.vpos < 0)
12427 {
12428 clear_glyph_matrix (w->desired_matrix);
12429 return -1;
12430 }
12431 }
12432
12433 /* Don't let the cursor end in the scroll margins. */
12434 {
12435 int this_scroll_margin, cursor_height;
12436
12437 this_scroll_margin = max (0, scroll_margin);
12438 this_scroll_margin = min (this_scroll_margin,
12439 XFASTINT (w->height) / 4);
12440 this_scroll_margin *= CANON_Y_UNIT (it.f);
12441 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12442
12443 if ((w->cursor.y < this_scroll_margin
12444 && CHARPOS (start) > BEGV)
12445 /* Don't take scroll margin into account at the bottom because
12446 old redisplay didn't do it either. */
12447 || w->cursor.y + cursor_height > it.last_visible_y)
12448 {
12449 w->cursor.vpos = -1;
12450 clear_glyph_matrix (w->desired_matrix);
12451 return -1;
12452 }
12453 }
12454
12455 /* Scroll the display. Do it before changing the current matrix so
12456 that xterm.c doesn't get confused about where the cursor glyph is
12457 found. */
12458 if (dy && run.height)
12459 {
12460 update_begin (f);
12461
12462 if (FRAME_WINDOW_P (f))
12463 {
12464 rif->update_window_begin_hook (w);
12465 rif->clear_mouse_face (w);
12466 rif->scroll_run_hook (w, &run);
12467 rif->update_window_end_hook (w, 0, 0);
12468 }
12469 else
12470 {
12471 /* Terminal frame. In this case, dvpos gives the number of
12472 lines to scroll by; dvpos < 0 means scroll up. */
12473 int first_unchanged_at_end_vpos
12474 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
12475 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
12476 int end = (XFASTINT (w->top)
12477 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
12478 + window_internal_height (w));
12479
12480 /* Perform the operation on the screen. */
12481 if (dvpos > 0)
12482 {
12483 /* Scroll last_unchanged_at_beg_row to the end of the
12484 window down dvpos lines. */
12485 set_terminal_window (end);
12486
12487 /* On dumb terminals delete dvpos lines at the end
12488 before inserting dvpos empty lines. */
12489 if (!scroll_region_ok)
12490 ins_del_lines (end - dvpos, -dvpos);
12491
12492 /* Insert dvpos empty lines in front of
12493 last_unchanged_at_beg_row. */
12494 ins_del_lines (from, dvpos);
12495 }
12496 else if (dvpos < 0)
12497 {
12498 /* Scroll up last_unchanged_at_beg_vpos to the end of
12499 the window to last_unchanged_at_beg_vpos - |dvpos|. */
12500 set_terminal_window (end);
12501
12502 /* Delete dvpos lines in front of
12503 last_unchanged_at_beg_vpos. ins_del_lines will set
12504 the cursor to the given vpos and emit |dvpos| delete
12505 line sequences. */
12506 ins_del_lines (from + dvpos, dvpos);
12507
12508 /* On a dumb terminal insert dvpos empty lines at the
12509 end. */
12510 if (!scroll_region_ok)
12511 ins_del_lines (end + dvpos, -dvpos);
12512 }
12513
12514 set_terminal_window (0);
12515 }
12516
12517 update_end (f);
12518 }
12519
12520 /* Shift reused rows of the current matrix to the right position.
12521 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
12522 text. */
12523 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12524 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
12525 if (dvpos < 0)
12526 {
12527 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
12528 bottom_vpos, dvpos);
12529 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
12530 bottom_vpos, 0);
12531 }
12532 else if (dvpos > 0)
12533 {
12534 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
12535 bottom_vpos, dvpos);
12536 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
12537 first_unchanged_at_end_vpos + dvpos, 0);
12538 }
12539
12540 /* For frame-based redisplay, make sure that current frame and window
12541 matrix are in sync with respect to glyph memory. */
12542 if (!FRAME_WINDOW_P (f))
12543 sync_frame_with_window_matrix_rows (w);
12544
12545 /* Adjust buffer positions in reused rows. */
12546 if (delta)
12547 increment_matrix_positions (current_matrix,
12548 first_unchanged_at_end_vpos + dvpos,
12549 bottom_vpos, delta, delta_bytes);
12550
12551 /* Adjust Y positions. */
12552 if (dy)
12553 shift_glyph_matrix (w, current_matrix,
12554 first_unchanged_at_end_vpos + dvpos,
12555 bottom_vpos, dy);
12556
12557 if (first_unchanged_at_end_row)
12558 first_unchanged_at_end_row += dvpos;
12559
12560 /* If scrolling up, there may be some lines to display at the end of
12561 the window. */
12562 last_text_row_at_end = NULL;
12563 if (dy < 0)
12564 {
12565 /* Scrolling up can leave for example a partially visible line
12566 at the end of the window to be redisplayed. */
12567 /* Set last_row to the glyph row in the current matrix where the
12568 window end line is found. It has been moved up or down in
12569 the matrix by dvpos. */
12570 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
12571 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
12572
12573 /* If last_row is the window end line, it should display text. */
12574 xassert (last_row->displays_text_p);
12575
12576 /* If window end line was partially visible before, begin
12577 displaying at that line. Otherwise begin displaying with the
12578 line following it. */
12579 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
12580 {
12581 init_to_row_start (&it, w, last_row);
12582 it.vpos = last_vpos;
12583 it.current_y = last_row->y;
12584 }
12585 else
12586 {
12587 init_to_row_end (&it, w, last_row);
12588 it.vpos = 1 + last_vpos;
12589 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
12590 ++last_row;
12591 }
12592
12593 /* We may start in a continuation line. If so, we have to
12594 get the right continuation_lines_width and current_x. */
12595 it.continuation_lines_width = last_row->continuation_lines_width;
12596 it.hpos = it.current_x = 0;
12597
12598 /* Display the rest of the lines at the window end. */
12599 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12600 while (it.current_y < it.last_visible_y
12601 && !fonts_changed_p)
12602 {
12603 /* Is it always sure that the display agrees with lines in
12604 the current matrix? I don't think so, so we mark rows
12605 displayed invalid in the current matrix by setting their
12606 enabled_p flag to zero. */
12607 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
12608 if (display_line (&it))
12609 last_text_row_at_end = it.glyph_row - 1;
12610 }
12611 }
12612
12613 /* Update window_end_pos and window_end_vpos. */
12614 if (first_unchanged_at_end_row
12615 && first_unchanged_at_end_row->y < it.last_visible_y
12616 && !last_text_row_at_end)
12617 {
12618 /* Window end line if one of the preserved rows from the current
12619 matrix. Set row to the last row displaying text in current
12620 matrix starting at first_unchanged_at_end_row, after
12621 scrolling. */
12622 xassert (first_unchanged_at_end_row->displays_text_p);
12623 row = find_last_row_displaying_text (w->current_matrix, &it,
12624 first_unchanged_at_end_row);
12625 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
12626
12627 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12628 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12629 w->window_end_vpos
12630 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
12631 xassert (w->window_end_bytepos >= 0);
12632 IF_DEBUG (debug_method_add (w, "A"));
12633 }
12634 else if (last_text_row_at_end)
12635 {
12636 w->window_end_pos
12637 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
12638 w->window_end_bytepos
12639 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
12640 w->window_end_vpos
12641 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
12642 xassert (w->window_end_bytepos >= 0);
12643 IF_DEBUG (debug_method_add (w, "B"));
12644 }
12645 else if (last_text_row)
12646 {
12647 /* We have displayed either to the end of the window or at the
12648 end of the window, i.e. the last row with text is to be found
12649 in the desired matrix. */
12650 w->window_end_pos
12651 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12652 w->window_end_bytepos
12653 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12654 w->window_end_vpos
12655 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
12656 xassert (w->window_end_bytepos >= 0);
12657 }
12658 else if (first_unchanged_at_end_row == NULL
12659 && last_text_row == NULL
12660 && last_text_row_at_end == NULL)
12661 {
12662 /* Displayed to end of window, but no line containing text was
12663 displayed. Lines were deleted at the end of the window. */
12664 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
12665 int vpos = XFASTINT (w->window_end_vpos);
12666 struct glyph_row *current_row = current_matrix->rows + vpos;
12667 struct glyph_row *desired_row = desired_matrix->rows + vpos;
12668
12669 for (row = NULL;
12670 row == NULL && vpos >= first_vpos;
12671 --vpos, --current_row, --desired_row)
12672 {
12673 if (desired_row->enabled_p)
12674 {
12675 if (desired_row->displays_text_p)
12676 row = desired_row;
12677 }
12678 else if (current_row->displays_text_p)
12679 row = current_row;
12680 }
12681
12682 xassert (row != NULL);
12683 w->window_end_vpos = make_number (vpos + 1);
12684 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12685 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12686 xassert (w->window_end_bytepos >= 0);
12687 IF_DEBUG (debug_method_add (w, "C"));
12688 }
12689 else
12690 abort ();
12691
12692 #if 0 /* This leads to problems, for instance when the cursor is
12693 at ZV, and the cursor line displays no text. */
12694 /* Disable rows below what's displayed in the window. This makes
12695 debugging easier. */
12696 enable_glyph_matrix_rows (current_matrix,
12697 XFASTINT (w->window_end_vpos) + 1,
12698 bottom_vpos, 0);
12699 #endif
12700
12701 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
12702 debug_end_vpos = XFASTINT (w->window_end_vpos));
12703
12704 /* Record that display has not been completed. */
12705 w->window_end_valid = Qnil;
12706 w->desired_matrix->no_scrolling_p = 1;
12707 return 3;
12708
12709 #undef GIVE_UP
12710 }
12711
12712
12713 \f
12714 /***********************************************************************
12715 More debugging support
12716 ***********************************************************************/
12717
12718 #if GLYPH_DEBUG
12719
12720 void dump_glyph_row P_ ((struct glyph_row *, int, int));
12721 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
12722 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
12723
12724
12725 /* Dump the contents of glyph matrix MATRIX on stderr.
12726
12727 GLYPHS 0 means don't show glyph contents.
12728 GLYPHS 1 means show glyphs in short form
12729 GLYPHS > 1 means show glyphs in long form. */
12730
12731 void
12732 dump_glyph_matrix (matrix, glyphs)
12733 struct glyph_matrix *matrix;
12734 int glyphs;
12735 {
12736 int i;
12737 for (i = 0; i < matrix->nrows; ++i)
12738 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
12739 }
12740
12741
12742 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
12743 the glyph row and area where the glyph comes from. */
12744
12745 void
12746 dump_glyph (row, glyph, area)
12747 struct glyph_row *row;
12748 struct glyph *glyph;
12749 int area;
12750 {
12751 if (glyph->type == CHAR_GLYPH)
12752 {
12753 fprintf (stderr,
12754 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12755 glyph - row->glyphs[TEXT_AREA],
12756 'C',
12757 glyph->charpos,
12758 (BUFFERP (glyph->object)
12759 ? 'B'
12760 : (STRINGP (glyph->object)
12761 ? 'S'
12762 : '-')),
12763 glyph->pixel_width,
12764 glyph->u.ch,
12765 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
12766 ? glyph->u.ch
12767 : '.'),
12768 glyph->face_id,
12769 glyph->left_box_line_p,
12770 glyph->right_box_line_p);
12771 }
12772 else if (glyph->type == STRETCH_GLYPH)
12773 {
12774 fprintf (stderr,
12775 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12776 glyph - row->glyphs[TEXT_AREA],
12777 'S',
12778 glyph->charpos,
12779 (BUFFERP (glyph->object)
12780 ? 'B'
12781 : (STRINGP (glyph->object)
12782 ? 'S'
12783 : '-')),
12784 glyph->pixel_width,
12785 0,
12786 '.',
12787 glyph->face_id,
12788 glyph->left_box_line_p,
12789 glyph->right_box_line_p);
12790 }
12791 else if (glyph->type == IMAGE_GLYPH)
12792 {
12793 fprintf (stderr,
12794 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
12795 glyph - row->glyphs[TEXT_AREA],
12796 'I',
12797 glyph->charpos,
12798 (BUFFERP (glyph->object)
12799 ? 'B'
12800 : (STRINGP (glyph->object)
12801 ? 'S'
12802 : '-')),
12803 glyph->pixel_width,
12804 glyph->u.img_id,
12805 '.',
12806 glyph->face_id,
12807 glyph->left_box_line_p,
12808 glyph->right_box_line_p);
12809 }
12810 }
12811
12812
12813 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
12814 GLYPHS 0 means don't show glyph contents.
12815 GLYPHS 1 means show glyphs in short form
12816 GLYPHS > 1 means show glyphs in long form. */
12817
12818 void
12819 dump_glyph_row (row, vpos, glyphs)
12820 struct glyph_row *row;
12821 int vpos, glyphs;
12822 {
12823 if (glyphs != 1)
12824 {
12825 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
12826 fprintf (stderr, "=======================================================================\n");
12827
12828 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
12829 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
12830 vpos,
12831 MATRIX_ROW_START_CHARPOS (row),
12832 MATRIX_ROW_END_CHARPOS (row),
12833 row->used[TEXT_AREA],
12834 row->contains_overlapping_glyphs_p,
12835 row->enabled_p,
12836 row->truncated_on_left_p,
12837 row->truncated_on_right_p,
12838 row->overlay_arrow_p,
12839 row->continued_p,
12840 MATRIX_ROW_CONTINUATION_LINE_P (row),
12841 row->displays_text_p,
12842 row->ends_at_zv_p,
12843 row->fill_line_p,
12844 row->ends_in_middle_of_char_p,
12845 row->starts_in_middle_of_char_p,
12846 row->mouse_face_p,
12847 row->x,
12848 row->y,
12849 row->pixel_width,
12850 row->height,
12851 row->visible_height,
12852 row->ascent,
12853 row->phys_ascent);
12854 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
12855 row->end.overlay_string_index,
12856 row->continuation_lines_width);
12857 fprintf (stderr, "%9d %5d\n",
12858 CHARPOS (row->start.string_pos),
12859 CHARPOS (row->end.string_pos));
12860 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
12861 row->end.dpvec_index);
12862 }
12863
12864 if (glyphs > 1)
12865 {
12866 int area;
12867
12868 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12869 {
12870 struct glyph *glyph = row->glyphs[area];
12871 struct glyph *glyph_end = glyph + row->used[area];
12872
12873 /* Glyph for a line end in text. */
12874 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
12875 ++glyph_end;
12876
12877 if (glyph < glyph_end)
12878 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
12879
12880 for (; glyph < glyph_end; ++glyph)
12881 dump_glyph (row, glyph, area);
12882 }
12883 }
12884 else if (glyphs == 1)
12885 {
12886 int area;
12887
12888 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12889 {
12890 char *s = (char *) alloca (row->used[area] + 1);
12891 int i;
12892
12893 for (i = 0; i < row->used[area]; ++i)
12894 {
12895 struct glyph *glyph = row->glyphs[area] + i;
12896 if (glyph->type == CHAR_GLYPH
12897 && glyph->u.ch < 0x80
12898 && glyph->u.ch >= ' ')
12899 s[i] = glyph->u.ch;
12900 else
12901 s[i] = '.';
12902 }
12903
12904 s[i] = '\0';
12905 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
12906 }
12907 }
12908 }
12909
12910
12911 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
12912 Sdump_glyph_matrix, 0, 1, "p",
12913 doc: /* Dump the current matrix of the selected window to stderr.
12914 Shows contents of glyph row structures. With non-nil
12915 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
12916 glyphs in short form, otherwise show glyphs in long form. */)
12917 (glyphs)
12918 Lisp_Object glyphs;
12919 {
12920 struct window *w = XWINDOW (selected_window);
12921 struct buffer *buffer = XBUFFER (w->buffer);
12922
12923 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
12924 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
12925 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
12926 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
12927 fprintf (stderr, "=============================================\n");
12928 dump_glyph_matrix (w->current_matrix,
12929 NILP (glyphs) ? 0 : XINT (glyphs));
12930 return Qnil;
12931 }
12932
12933
12934 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
12935 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
12936 ()
12937 {
12938 struct frame *f = XFRAME (selected_frame);
12939 dump_glyph_matrix (f->current_matrix, 1);
12940 return Qnil;
12941 }
12942
12943
12944 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
12945 doc: /* Dump glyph row ROW to stderr.
12946 GLYPH 0 means don't dump glyphs.
12947 GLYPH 1 means dump glyphs in short form.
12948 GLYPH > 1 or omitted means dump glyphs in long form. */)
12949 (row, glyphs)
12950 Lisp_Object row, glyphs;
12951 {
12952 struct glyph_matrix *matrix;
12953 int vpos;
12954
12955 CHECK_NUMBER (row);
12956 matrix = XWINDOW (selected_window)->current_matrix;
12957 vpos = XINT (row);
12958 if (vpos >= 0 && vpos < matrix->nrows)
12959 dump_glyph_row (MATRIX_ROW (matrix, vpos),
12960 vpos,
12961 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12962 return Qnil;
12963 }
12964
12965
12966 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
12967 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
12968 GLYPH 0 means don't dump glyphs.
12969 GLYPH 1 means dump glyphs in short form.
12970 GLYPH > 1 or omitted means dump glyphs in long form. */)
12971 (row, glyphs)
12972 Lisp_Object row, glyphs;
12973 {
12974 struct frame *sf = SELECTED_FRAME ();
12975 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
12976 int vpos;
12977
12978 CHECK_NUMBER (row);
12979 vpos = XINT (row);
12980 if (vpos >= 0 && vpos < m->nrows)
12981 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
12982 INTEGERP (glyphs) ? XINT (glyphs) : 2);
12983 return Qnil;
12984 }
12985
12986
12987 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
12988 doc: /* Toggle tracing of redisplay.
12989 With ARG, turn tracing on if and only if ARG is positive. */)
12990 (arg)
12991 Lisp_Object arg;
12992 {
12993 if (NILP (arg))
12994 trace_redisplay_p = !trace_redisplay_p;
12995 else
12996 {
12997 arg = Fprefix_numeric_value (arg);
12998 trace_redisplay_p = XINT (arg) > 0;
12999 }
13000
13001 return Qnil;
13002 }
13003
13004
13005 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13006 doc: /* Like `format', but print result to stderr.
13007 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13008 (nargs, args)
13009 int nargs;
13010 Lisp_Object *args;
13011 {
13012 Lisp_Object s = Fformat (nargs, args);
13013 fprintf (stderr, "%s", SDATA (s));
13014 return Qnil;
13015 }
13016
13017 #endif /* GLYPH_DEBUG */
13018
13019
13020 \f
13021 /***********************************************************************
13022 Building Desired Matrix Rows
13023 ***********************************************************************/
13024
13025 /* Return a temporary glyph row holding the glyphs of an overlay
13026 arrow. Only used for non-window-redisplay windows. */
13027
13028 static struct glyph_row *
13029 get_overlay_arrow_glyph_row (w)
13030 struct window *w;
13031 {
13032 struct frame *f = XFRAME (WINDOW_FRAME (w));
13033 struct buffer *buffer = XBUFFER (w->buffer);
13034 struct buffer *old = current_buffer;
13035 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13036 int arrow_len = SCHARS (Voverlay_arrow_string);
13037 const unsigned char *arrow_end = arrow_string + arrow_len;
13038 const unsigned char *p;
13039 struct it it;
13040 int multibyte_p;
13041 int n_glyphs_before;
13042
13043 set_buffer_temp (buffer);
13044 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13045 it.glyph_row->used[TEXT_AREA] = 0;
13046 SET_TEXT_POS (it.position, 0, 0);
13047
13048 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13049 p = arrow_string;
13050 while (p < arrow_end)
13051 {
13052 Lisp_Object face, ilisp;
13053
13054 /* Get the next character. */
13055 if (multibyte_p)
13056 it.c = string_char_and_length (p, arrow_len, &it.len);
13057 else
13058 it.c = *p, it.len = 1;
13059 p += it.len;
13060
13061 /* Get its face. */
13062 ilisp = make_number (p - arrow_string);
13063 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13064 it.face_id = compute_char_face (f, it.c, face);
13065
13066 /* Compute its width, get its glyphs. */
13067 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13068 SET_TEXT_POS (it.position, -1, -1);
13069 PRODUCE_GLYPHS (&it);
13070
13071 /* If this character doesn't fit any more in the line, we have
13072 to remove some glyphs. */
13073 if (it.current_x > it.last_visible_x)
13074 {
13075 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13076 break;
13077 }
13078 }
13079
13080 set_buffer_temp (old);
13081 return it.glyph_row;
13082 }
13083
13084
13085 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13086 glyphs are only inserted for terminal frames since we can't really
13087 win with truncation glyphs when partially visible glyphs are
13088 involved. Which glyphs to insert is determined by
13089 produce_special_glyphs. */
13090
13091 static void
13092 insert_left_trunc_glyphs (it)
13093 struct it *it;
13094 {
13095 struct it truncate_it;
13096 struct glyph *from, *end, *to, *toend;
13097
13098 xassert (!FRAME_WINDOW_P (it->f));
13099
13100 /* Get the truncation glyphs. */
13101 truncate_it = *it;
13102 truncate_it.current_x = 0;
13103 truncate_it.face_id = DEFAULT_FACE_ID;
13104 truncate_it.glyph_row = &scratch_glyph_row;
13105 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13106 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13107 truncate_it.object = make_number (0);
13108 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13109
13110 /* Overwrite glyphs from IT with truncation glyphs. */
13111 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13112 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13113 to = it->glyph_row->glyphs[TEXT_AREA];
13114 toend = to + it->glyph_row->used[TEXT_AREA];
13115
13116 while (from < end)
13117 *to++ = *from++;
13118
13119 /* There may be padding glyphs left over. Overwrite them too. */
13120 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13121 {
13122 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13123 while (from < end)
13124 *to++ = *from++;
13125 }
13126
13127 if (to > toend)
13128 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13129 }
13130
13131
13132 /* Compute the pixel height and width of IT->glyph_row.
13133
13134 Most of the time, ascent and height of a display line will be equal
13135 to the max_ascent and max_height values of the display iterator
13136 structure. This is not the case if
13137
13138 1. We hit ZV without displaying anything. In this case, max_ascent
13139 and max_height will be zero.
13140
13141 2. We have some glyphs that don't contribute to the line height.
13142 (The glyph row flag contributes_to_line_height_p is for future
13143 pixmap extensions).
13144
13145 The first case is easily covered by using default values because in
13146 these cases, the line height does not really matter, except that it
13147 must not be zero. */
13148
13149 static void
13150 compute_line_metrics (it)
13151 struct it *it;
13152 {
13153 struct glyph_row *row = it->glyph_row;
13154 int area, i;
13155
13156 if (FRAME_WINDOW_P (it->f))
13157 {
13158 int i, min_y, max_y;
13159
13160 /* The line may consist of one space only, that was added to
13161 place the cursor on it. If so, the row's height hasn't been
13162 computed yet. */
13163 if (row->height == 0)
13164 {
13165 if (it->max_ascent + it->max_descent == 0)
13166 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
13167 row->ascent = it->max_ascent;
13168 row->height = it->max_ascent + it->max_descent;
13169 row->phys_ascent = it->max_phys_ascent;
13170 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13171 }
13172
13173 /* Compute the width of this line. */
13174 row->pixel_width = row->x;
13175 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13176 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13177
13178 xassert (row->pixel_width >= 0);
13179 xassert (row->ascent >= 0 && row->height > 0);
13180
13181 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13182 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13183
13184 /* If first line's physical ascent is larger than its logical
13185 ascent, use the physical ascent, and make the row taller.
13186 This makes accented characters fully visible. */
13187 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13188 && row->phys_ascent > row->ascent)
13189 {
13190 row->height += row->phys_ascent - row->ascent;
13191 row->ascent = row->phys_ascent;
13192 }
13193
13194 /* Compute how much of the line is visible. */
13195 row->visible_height = row->height;
13196
13197 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
13198 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
13199
13200 if (row->y < min_y)
13201 row->visible_height -= min_y - row->y;
13202 if (row->y + row->height > max_y)
13203 row->visible_height -= row->y + row->height - max_y;
13204 }
13205 else
13206 {
13207 row->pixel_width = row->used[TEXT_AREA];
13208 if (row->continued_p)
13209 row->pixel_width -= it->continuation_pixel_width;
13210 else if (row->truncated_on_right_p)
13211 row->pixel_width -= it->truncation_pixel_width;
13212 row->ascent = row->phys_ascent = 0;
13213 row->height = row->phys_height = row->visible_height = 1;
13214 }
13215
13216 /* Compute a hash code for this row. */
13217 row->hash = 0;
13218 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13219 for (i = 0; i < row->used[area]; ++i)
13220 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13221 + row->glyphs[area][i].u.val
13222 + row->glyphs[area][i].face_id
13223 + row->glyphs[area][i].padding_p
13224 + (row->glyphs[area][i].type << 2));
13225
13226 it->max_ascent = it->max_descent = 0;
13227 it->max_phys_ascent = it->max_phys_descent = 0;
13228 }
13229
13230
13231 /* Append one space to the glyph row of iterator IT if doing a
13232 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13233 space have the default face, otherwise let it have the same face as
13234 IT->face_id. Value is non-zero if a space was added.
13235
13236 This function is called to make sure that there is always one glyph
13237 at the end of a glyph row that the cursor can be set on under
13238 window-systems. (If there weren't such a glyph we would not know
13239 how wide and tall a box cursor should be displayed).
13240
13241 At the same time this space let's a nicely handle clearing to the
13242 end of the line if the row ends in italic text. */
13243
13244 static int
13245 append_space (it, default_face_p)
13246 struct it *it;
13247 int default_face_p;
13248 {
13249 if (FRAME_WINDOW_P (it->f))
13250 {
13251 int n = it->glyph_row->used[TEXT_AREA];
13252
13253 if (it->glyph_row->glyphs[TEXT_AREA] + n
13254 < it->glyph_row->glyphs[1 + TEXT_AREA])
13255 {
13256 /* Save some values that must not be changed.
13257 Must save IT->c and IT->len because otherwise
13258 ITERATOR_AT_END_P wouldn't work anymore after
13259 append_space has been called. */
13260 enum display_element_type saved_what = it->what;
13261 int saved_c = it->c, saved_len = it->len;
13262 int saved_x = it->current_x;
13263 int saved_face_id = it->face_id;
13264 struct text_pos saved_pos;
13265 Lisp_Object saved_object;
13266 struct face *face;
13267
13268 saved_object = it->object;
13269 saved_pos = it->position;
13270
13271 it->what = IT_CHARACTER;
13272 bzero (&it->position, sizeof it->position);
13273 it->object = make_number (0);
13274 it->c = ' ';
13275 it->len = 1;
13276
13277 if (default_face_p)
13278 it->face_id = DEFAULT_FACE_ID;
13279 else if (it->face_before_selective_p)
13280 it->face_id = it->saved_face_id;
13281 face = FACE_FROM_ID (it->f, it->face_id);
13282 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
13283
13284 PRODUCE_GLYPHS (it);
13285
13286 it->current_x = saved_x;
13287 it->object = saved_object;
13288 it->position = saved_pos;
13289 it->what = saved_what;
13290 it->face_id = saved_face_id;
13291 it->len = saved_len;
13292 it->c = saved_c;
13293 return 1;
13294 }
13295 }
13296
13297 return 0;
13298 }
13299
13300
13301 /* Extend the face of the last glyph in the text area of IT->glyph_row
13302 to the end of the display line. Called from display_line.
13303 If the glyph row is empty, add a space glyph to it so that we
13304 know the face to draw. Set the glyph row flag fill_line_p. */
13305
13306 static void
13307 extend_face_to_end_of_line (it)
13308 struct it *it;
13309 {
13310 struct face *face;
13311 struct frame *f = it->f;
13312
13313 /* If line is already filled, do nothing. */
13314 if (it->current_x >= it->last_visible_x)
13315 return;
13316
13317 /* Face extension extends the background and box of IT->face_id
13318 to the end of the line. If the background equals the background
13319 of the frame, we don't have to do anything. */
13320 if (it->face_before_selective_p)
13321 face = FACE_FROM_ID (it->f, it->saved_face_id);
13322 else
13323 face = FACE_FROM_ID (f, it->face_id);
13324
13325 if (FRAME_WINDOW_P (f)
13326 && face->box == FACE_NO_BOX
13327 && face->background == FRAME_BACKGROUND_PIXEL (f)
13328 && !face->stipple)
13329 return;
13330
13331 /* Set the glyph row flag indicating that the face of the last glyph
13332 in the text area has to be drawn to the end of the text area. */
13333 it->glyph_row->fill_line_p = 1;
13334
13335 /* If current character of IT is not ASCII, make sure we have the
13336 ASCII face. This will be automatically undone the next time
13337 get_next_display_element returns a multibyte character. Note
13338 that the character will always be single byte in unibyte text. */
13339 if (!SINGLE_BYTE_CHAR_P (it->c))
13340 {
13341 it->face_id = FACE_FOR_CHAR (f, face, 0);
13342 }
13343
13344 if (FRAME_WINDOW_P (f))
13345 {
13346 /* If the row is empty, add a space with the current face of IT,
13347 so that we know which face to draw. */
13348 if (it->glyph_row->used[TEXT_AREA] == 0)
13349 {
13350 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
13351 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
13352 it->glyph_row->used[TEXT_AREA] = 1;
13353 }
13354 }
13355 else
13356 {
13357 /* Save some values that must not be changed. */
13358 int saved_x = it->current_x;
13359 struct text_pos saved_pos;
13360 Lisp_Object saved_object;
13361 enum display_element_type saved_what = it->what;
13362 int saved_face_id = it->face_id;
13363
13364 saved_object = it->object;
13365 saved_pos = it->position;
13366
13367 it->what = IT_CHARACTER;
13368 bzero (&it->position, sizeof it->position);
13369 it->object = make_number (0);
13370 it->c = ' ';
13371 it->len = 1;
13372 it->face_id = face->id;
13373
13374 PRODUCE_GLYPHS (it);
13375
13376 while (it->current_x <= it->last_visible_x)
13377 PRODUCE_GLYPHS (it);
13378
13379 /* Don't count these blanks really. It would let us insert a left
13380 truncation glyph below and make us set the cursor on them, maybe. */
13381 it->current_x = saved_x;
13382 it->object = saved_object;
13383 it->position = saved_pos;
13384 it->what = saved_what;
13385 it->face_id = saved_face_id;
13386 }
13387 }
13388
13389
13390 /* Value is non-zero if text starting at CHARPOS in current_buffer is
13391 trailing whitespace. */
13392
13393 static int
13394 trailing_whitespace_p (charpos)
13395 int charpos;
13396 {
13397 int bytepos = CHAR_TO_BYTE (charpos);
13398 int c = 0;
13399
13400 while (bytepos < ZV_BYTE
13401 && (c = FETCH_CHAR (bytepos),
13402 c == ' ' || c == '\t'))
13403 ++bytepos;
13404
13405 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
13406 {
13407 if (bytepos != PT_BYTE)
13408 return 1;
13409 }
13410 return 0;
13411 }
13412
13413
13414 /* Highlight trailing whitespace, if any, in ROW. */
13415
13416 void
13417 highlight_trailing_whitespace (f, row)
13418 struct frame *f;
13419 struct glyph_row *row;
13420 {
13421 int used = row->used[TEXT_AREA];
13422
13423 if (used)
13424 {
13425 struct glyph *start = row->glyphs[TEXT_AREA];
13426 struct glyph *glyph = start + used - 1;
13427
13428 /* Skip over glyphs inserted to display the cursor at the
13429 end of a line, for extending the face of the last glyph
13430 to the end of the line on terminals, and for truncation
13431 and continuation glyphs. */
13432 while (glyph >= start
13433 && glyph->type == CHAR_GLYPH
13434 && INTEGERP (glyph->object))
13435 --glyph;
13436
13437 /* If last glyph is a space or stretch, and it's trailing
13438 whitespace, set the face of all trailing whitespace glyphs in
13439 IT->glyph_row to `trailing-whitespace'. */
13440 if (glyph >= start
13441 && BUFFERP (glyph->object)
13442 && (glyph->type == STRETCH_GLYPH
13443 || (glyph->type == CHAR_GLYPH
13444 && glyph->u.ch == ' '))
13445 && trailing_whitespace_p (glyph->charpos))
13446 {
13447 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
13448
13449 while (glyph >= start
13450 && BUFFERP (glyph->object)
13451 && (glyph->type == STRETCH_GLYPH
13452 || (glyph->type == CHAR_GLYPH
13453 && glyph->u.ch == ' ')))
13454 (glyph--)->face_id = face_id;
13455 }
13456 }
13457 }
13458
13459
13460 /* Value is non-zero if glyph row ROW in window W should be
13461 used to hold the cursor. */
13462
13463 static int
13464 cursor_row_p (w, row)
13465 struct window *w;
13466 struct glyph_row *row;
13467 {
13468 int cursor_row_p = 1;
13469
13470 if (PT == MATRIX_ROW_END_CHARPOS (row))
13471 {
13472 /* If the row ends with a newline from a string, we don't want
13473 the cursor there (if the row is continued it doesn't end in a
13474 newline). */
13475 if (CHARPOS (row->end.string_pos) >= 0
13476 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13477 cursor_row_p = row->continued_p;
13478
13479 /* If the row ends at ZV, display the cursor at the end of that
13480 row instead of at the start of the row below. */
13481 else if (row->ends_at_zv_p)
13482 cursor_row_p = 1;
13483 else
13484 cursor_row_p = 0;
13485 }
13486
13487 return cursor_row_p;
13488 }
13489
13490
13491 /* Construct the glyph row IT->glyph_row in the desired matrix of
13492 IT->w from text at the current position of IT. See dispextern.h
13493 for an overview of struct it. Value is non-zero if
13494 IT->glyph_row displays text, as opposed to a line displaying ZV
13495 only. */
13496
13497 static int
13498 display_line (it)
13499 struct it *it;
13500 {
13501 struct glyph_row *row = it->glyph_row;
13502
13503 /* We always start displaying at hpos zero even if hscrolled. */
13504 xassert (it->hpos == 0 && it->current_x == 0);
13505
13506 /* We must not display in a row that's not a text row. */
13507 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
13508 < it->w->desired_matrix->nrows);
13509
13510 /* Is IT->w showing the region? */
13511 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
13512
13513 /* Clear the result glyph row and enable it. */
13514 prepare_desired_row (row);
13515
13516 row->y = it->current_y;
13517 row->start = it->current;
13518 row->continuation_lines_width = it->continuation_lines_width;
13519 row->displays_text_p = 1;
13520 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
13521 it->starts_in_middle_of_char_p = 0;
13522
13523 /* Arrange the overlays nicely for our purposes. Usually, we call
13524 display_line on only one line at a time, in which case this
13525 can't really hurt too much, or we call it on lines which appear
13526 one after another in the buffer, in which case all calls to
13527 recenter_overlay_lists but the first will be pretty cheap. */
13528 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
13529
13530 /* Move over display elements that are not visible because we are
13531 hscrolled. This may stop at an x-position < IT->first_visible_x
13532 if the first glyph is partially visible or if we hit a line end. */
13533 if (it->current_x < it->first_visible_x)
13534 move_it_in_display_line_to (it, ZV, it->first_visible_x,
13535 MOVE_TO_POS | MOVE_TO_X);
13536
13537 /* Get the initial row height. This is either the height of the
13538 text hscrolled, if there is any, or zero. */
13539 row->ascent = it->max_ascent;
13540 row->height = it->max_ascent + it->max_descent;
13541 row->phys_ascent = it->max_phys_ascent;
13542 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13543
13544 /* Loop generating characters. The loop is left with IT on the next
13545 character to display. */
13546 while (1)
13547 {
13548 int n_glyphs_before, hpos_before, x_before;
13549 int x, i, nglyphs;
13550 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
13551
13552 /* Retrieve the next thing to display. Value is zero if end of
13553 buffer reached. */
13554 if (!get_next_display_element (it))
13555 {
13556 /* Maybe add a space at the end of this line that is used to
13557 display the cursor there under X. Set the charpos of the
13558 first glyph of blank lines not corresponding to any text
13559 to -1. */
13560 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
13561 || row->used[TEXT_AREA] == 0)
13562 {
13563 row->glyphs[TEXT_AREA]->charpos = -1;
13564 row->displays_text_p = 0;
13565
13566 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
13567 && (!MINI_WINDOW_P (it->w)
13568 || (minibuf_level && EQ (it->window, minibuf_window))))
13569 row->indicate_empty_line_p = 1;
13570 }
13571
13572 it->continuation_lines_width = 0;
13573 row->ends_at_zv_p = 1;
13574 break;
13575 }
13576
13577 /* Now, get the metrics of what we want to display. This also
13578 generates glyphs in `row' (which is IT->glyph_row). */
13579 n_glyphs_before = row->used[TEXT_AREA];
13580 x = it->current_x;
13581
13582 /* Remember the line height so far in case the next element doesn't
13583 fit on the line. */
13584 if (!it->truncate_lines_p)
13585 {
13586 ascent = it->max_ascent;
13587 descent = it->max_descent;
13588 phys_ascent = it->max_phys_ascent;
13589 phys_descent = it->max_phys_descent;
13590 }
13591
13592 PRODUCE_GLYPHS (it);
13593
13594 /* If this display element was in marginal areas, continue with
13595 the next one. */
13596 if (it->area != TEXT_AREA)
13597 {
13598 row->ascent = max (row->ascent, it->max_ascent);
13599 row->height = max (row->height, it->max_ascent + it->max_descent);
13600 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13601 row->phys_height = max (row->phys_height,
13602 it->max_phys_ascent + it->max_phys_descent);
13603 set_iterator_to_next (it, 1);
13604 continue;
13605 }
13606
13607 /* Does the display element fit on the line? If we truncate
13608 lines, we should draw past the right edge of the window. If
13609 we don't truncate, we want to stop so that we can display the
13610 continuation glyph before the right margin. If lines are
13611 continued, there are two possible strategies for characters
13612 resulting in more than 1 glyph (e.g. tabs): Display as many
13613 glyphs as possible in this line and leave the rest for the
13614 continuation line, or display the whole element in the next
13615 line. Original redisplay did the former, so we do it also. */
13616 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
13617 hpos_before = it->hpos;
13618 x_before = x;
13619
13620 if (/* Not a newline. */
13621 nglyphs > 0
13622 /* Glyphs produced fit entirely in the line. */
13623 && it->current_x < it->last_visible_x)
13624 {
13625 it->hpos += nglyphs;
13626 row->ascent = max (row->ascent, it->max_ascent);
13627 row->height = max (row->height, it->max_ascent + it->max_descent);
13628 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13629 row->phys_height = max (row->phys_height,
13630 it->max_phys_ascent + it->max_phys_descent);
13631 if (it->current_x - it->pixel_width < it->first_visible_x)
13632 row->x = x - it->first_visible_x;
13633 }
13634 else
13635 {
13636 int new_x;
13637 struct glyph *glyph;
13638
13639 for (i = 0; i < nglyphs; ++i, x = new_x)
13640 {
13641 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
13642 new_x = x + glyph->pixel_width;
13643
13644 if (/* Lines are continued. */
13645 !it->truncate_lines_p
13646 && (/* Glyph doesn't fit on the line. */
13647 new_x > it->last_visible_x
13648 /* Or it fits exactly on a window system frame. */
13649 || (new_x == it->last_visible_x
13650 && FRAME_WINDOW_P (it->f))))
13651 {
13652 /* End of a continued line. */
13653
13654 if (it->hpos == 0
13655 || (new_x == it->last_visible_x
13656 && FRAME_WINDOW_P (it->f)))
13657 {
13658 /* Current glyph is the only one on the line or
13659 fits exactly on the line. We must continue
13660 the line because we can't draw the cursor
13661 after the glyph. */
13662 row->continued_p = 1;
13663 it->current_x = new_x;
13664 it->continuation_lines_width += new_x;
13665 ++it->hpos;
13666 if (i == nglyphs - 1)
13667 set_iterator_to_next (it, 1);
13668 }
13669 else if (CHAR_GLYPH_PADDING_P (*glyph)
13670 && !FRAME_WINDOW_P (it->f))
13671 {
13672 /* A padding glyph that doesn't fit on this line.
13673 This means the whole character doesn't fit
13674 on the line. */
13675 row->used[TEXT_AREA] = n_glyphs_before;
13676
13677 /* Fill the rest of the row with continuation
13678 glyphs like in 20.x. */
13679 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
13680 < row->glyphs[1 + TEXT_AREA])
13681 produce_special_glyphs (it, IT_CONTINUATION);
13682
13683 row->continued_p = 1;
13684 it->current_x = x_before;
13685 it->continuation_lines_width += x_before;
13686
13687 /* Restore the height to what it was before the
13688 element not fitting on the line. */
13689 it->max_ascent = ascent;
13690 it->max_descent = descent;
13691 it->max_phys_ascent = phys_ascent;
13692 it->max_phys_descent = phys_descent;
13693 }
13694 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
13695 {
13696 /* A TAB that extends past the right edge of the
13697 window. This produces a single glyph on
13698 window system frames. We leave the glyph in
13699 this row and let it fill the row, but don't
13700 consume the TAB. */
13701 it->continuation_lines_width += it->last_visible_x;
13702 row->ends_in_middle_of_char_p = 1;
13703 row->continued_p = 1;
13704 glyph->pixel_width = it->last_visible_x - x;
13705 it->starts_in_middle_of_char_p = 1;
13706 }
13707 else
13708 {
13709 /* Something other than a TAB that draws past
13710 the right edge of the window. Restore
13711 positions to values before the element. */
13712 row->used[TEXT_AREA] = n_glyphs_before + i;
13713
13714 /* Display continuation glyphs. */
13715 if (!FRAME_WINDOW_P (it->f))
13716 produce_special_glyphs (it, IT_CONTINUATION);
13717 row->continued_p = 1;
13718
13719 it->continuation_lines_width += x;
13720
13721 if (nglyphs > 1 && i > 0)
13722 {
13723 row->ends_in_middle_of_char_p = 1;
13724 it->starts_in_middle_of_char_p = 1;
13725 }
13726
13727 /* Restore the height to what it was before the
13728 element not fitting on the line. */
13729 it->max_ascent = ascent;
13730 it->max_descent = descent;
13731 it->max_phys_ascent = phys_ascent;
13732 it->max_phys_descent = phys_descent;
13733 }
13734
13735 break;
13736 }
13737 else if (new_x > it->first_visible_x)
13738 {
13739 /* Increment number of glyphs actually displayed. */
13740 ++it->hpos;
13741
13742 if (x < it->first_visible_x)
13743 /* Glyph is partially visible, i.e. row starts at
13744 negative X position. */
13745 row->x = x - it->first_visible_x;
13746 }
13747 else
13748 {
13749 /* Glyph is completely off the left margin of the
13750 window. This should not happen because of the
13751 move_it_in_display_line at the start of this
13752 function, unless the text display area of the
13753 window is empty. */
13754 xassert (it->first_visible_x <= it->last_visible_x);
13755 }
13756 }
13757
13758 row->ascent = max (row->ascent, it->max_ascent);
13759 row->height = max (row->height, it->max_ascent + it->max_descent);
13760 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
13761 row->phys_height = max (row->phys_height,
13762 it->max_phys_ascent + it->max_phys_descent);
13763
13764 /* End of this display line if row is continued. */
13765 if (row->continued_p)
13766 break;
13767 }
13768
13769 /* Is this a line end? If yes, we're also done, after making
13770 sure that a non-default face is extended up to the right
13771 margin of the window. */
13772 if (ITERATOR_AT_END_OF_LINE_P (it))
13773 {
13774 int used_before = row->used[TEXT_AREA];
13775
13776 row->ends_in_newline_from_string_p = STRINGP (it->object);
13777
13778 /* Add a space at the end of the line that is used to
13779 display the cursor there. */
13780 append_space (it, 0);
13781
13782 /* Extend the face to the end of the line. */
13783 extend_face_to_end_of_line (it);
13784
13785 /* Make sure we have the position. */
13786 if (used_before == 0)
13787 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
13788
13789 /* Consume the line end. This skips over invisible lines. */
13790 set_iterator_to_next (it, 1);
13791 it->continuation_lines_width = 0;
13792 break;
13793 }
13794
13795 /* Proceed with next display element. Note that this skips
13796 over lines invisible because of selective display. */
13797 set_iterator_to_next (it, 1);
13798
13799 /* If we truncate lines, we are done when the last displayed
13800 glyphs reach past the right margin of the window. */
13801 if (it->truncate_lines_p
13802 && (FRAME_WINDOW_P (it->f)
13803 ? (it->current_x >= it->last_visible_x)
13804 : (it->current_x > it->last_visible_x)))
13805 {
13806 /* Maybe add truncation glyphs. */
13807 if (!FRAME_WINDOW_P (it->f))
13808 {
13809 int i, n;
13810
13811 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
13812 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
13813 break;
13814
13815 for (n = row->used[TEXT_AREA]; i < n; ++i)
13816 {
13817 row->used[TEXT_AREA] = i;
13818 produce_special_glyphs (it, IT_TRUNCATION);
13819 }
13820 }
13821
13822 row->truncated_on_right_p = 1;
13823 it->continuation_lines_width = 0;
13824 reseat_at_next_visible_line_start (it, 0);
13825 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
13826 it->hpos = hpos_before;
13827 it->current_x = x_before;
13828 break;
13829 }
13830 }
13831
13832 /* If line is not empty and hscrolled, maybe insert truncation glyphs
13833 at the left window margin. */
13834 if (it->first_visible_x
13835 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
13836 {
13837 if (!FRAME_WINDOW_P (it->f))
13838 insert_left_trunc_glyphs (it);
13839 row->truncated_on_left_p = 1;
13840 }
13841
13842 /* If the start of this line is the overlay arrow-position, then
13843 mark this glyph row as the one containing the overlay arrow.
13844 This is clearly a mess with variable size fonts. It would be
13845 better to let it be displayed like cursors under X. */
13846 if (MARKERP (Voverlay_arrow_position)
13847 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
13848 && (MATRIX_ROW_START_CHARPOS (row)
13849 == marker_position (Voverlay_arrow_position))
13850 && STRINGP (Voverlay_arrow_string)
13851 && ! overlay_arrow_seen)
13852 {
13853 /* Overlay arrow in window redisplay is a fringe bitmap. */
13854 if (!FRAME_WINDOW_P (it->f))
13855 {
13856 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
13857 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
13858 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
13859 struct glyph *p = row->glyphs[TEXT_AREA];
13860 struct glyph *p2, *end;
13861
13862 /* Copy the arrow glyphs. */
13863 while (glyph < arrow_end)
13864 *p++ = *glyph++;
13865
13866 /* Throw away padding glyphs. */
13867 p2 = p;
13868 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
13869 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
13870 ++p2;
13871 if (p2 > p)
13872 {
13873 while (p2 < end)
13874 *p++ = *p2++;
13875 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
13876 }
13877 }
13878
13879 overlay_arrow_seen = 1;
13880 row->overlay_arrow_p = 1;
13881 }
13882
13883 /* Compute pixel dimensions of this line. */
13884 compute_line_metrics (it);
13885
13886 /* Remember the position at which this line ends. */
13887 row->end = it->current;
13888
13889 /* Maybe set the cursor. */
13890 if (it->w->cursor.vpos < 0
13891 && PT >= MATRIX_ROW_START_CHARPOS (row)
13892 && PT <= MATRIX_ROW_END_CHARPOS (row)
13893 && cursor_row_p (it->w, row))
13894 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
13895
13896 /* Highlight trailing whitespace. */
13897 if (!NILP (Vshow_trailing_whitespace))
13898 highlight_trailing_whitespace (it->f, it->glyph_row);
13899
13900 /* Prepare for the next line. This line starts horizontally at (X
13901 HPOS) = (0 0). Vertical positions are incremented. As a
13902 convenience for the caller, IT->glyph_row is set to the next
13903 row to be used. */
13904 it->current_x = it->hpos = 0;
13905 it->current_y += row->height;
13906 ++it->vpos;
13907 ++it->glyph_row;
13908 return row->displays_text_p;
13909 }
13910
13911
13912 \f
13913 /***********************************************************************
13914 Menu Bar
13915 ***********************************************************************/
13916
13917 /* Redisplay the menu bar in the frame for window W.
13918
13919 The menu bar of X frames that don't have X toolkit support is
13920 displayed in a special window W->frame->menu_bar_window.
13921
13922 The menu bar of terminal frames is treated specially as far as
13923 glyph matrices are concerned. Menu bar lines are not part of
13924 windows, so the update is done directly on the frame matrix rows
13925 for the menu bar. */
13926
13927 static void
13928 display_menu_bar (w)
13929 struct window *w;
13930 {
13931 struct frame *f = XFRAME (WINDOW_FRAME (w));
13932 struct it it;
13933 Lisp_Object items;
13934 int i;
13935
13936 /* Don't do all this for graphical frames. */
13937 #ifdef HAVE_NTGUI
13938 if (!NILP (Vwindow_system))
13939 return;
13940 #endif
13941 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
13942 if (FRAME_X_P (f))
13943 return;
13944 #endif
13945 #ifdef MAC_OS
13946 if (FRAME_MAC_P (f))
13947 return;
13948 #endif
13949
13950 #ifdef USE_X_TOOLKIT
13951 xassert (!FRAME_WINDOW_P (f));
13952 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
13953 it.first_visible_x = 0;
13954 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13955 #else /* not USE_X_TOOLKIT */
13956 if (FRAME_WINDOW_P (f))
13957 {
13958 /* Menu bar lines are displayed in the desired matrix of the
13959 dummy window menu_bar_window. */
13960 struct window *menu_w;
13961 xassert (WINDOWP (f->menu_bar_window));
13962 menu_w = XWINDOW (f->menu_bar_window);
13963 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
13964 MENU_FACE_ID);
13965 it.first_visible_x = 0;
13966 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
13967 }
13968 else
13969 {
13970 /* This is a TTY frame, i.e. character hpos/vpos are used as
13971 pixel x/y. */
13972 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
13973 MENU_FACE_ID);
13974 it.first_visible_x = 0;
13975 it.last_visible_x = FRAME_WIDTH (f);
13976 }
13977 #endif /* not USE_X_TOOLKIT */
13978
13979 if (! mode_line_inverse_video)
13980 /* Force the menu-bar to be displayed in the default face. */
13981 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
13982
13983 /* Clear all rows of the menu bar. */
13984 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
13985 {
13986 struct glyph_row *row = it.glyph_row + i;
13987 clear_glyph_row (row);
13988 row->enabled_p = 1;
13989 row->full_width_p = 1;
13990 }
13991
13992 /* Display all items of the menu bar. */
13993 items = FRAME_MENU_BAR_ITEMS (it.f);
13994 for (i = 0; i < XVECTOR (items)->size; i += 4)
13995 {
13996 Lisp_Object string;
13997
13998 /* Stop at nil string. */
13999 string = AREF (items, i + 1);
14000 if (NILP (string))
14001 break;
14002
14003 /* Remember where item was displayed. */
14004 AREF (items, i + 3) = make_number (it.hpos);
14005
14006 /* Display the item, pad with one space. */
14007 if (it.current_x < it.last_visible_x)
14008 display_string (NULL, string, Qnil, 0, 0, &it,
14009 SCHARS (string) + 1, 0, 0, -1);
14010 }
14011
14012 /* Fill out the line with spaces. */
14013 if (it.current_x < it.last_visible_x)
14014 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14015
14016 /* Compute the total height of the lines. */
14017 compute_line_metrics (&it);
14018 }
14019
14020
14021 \f
14022 /***********************************************************************
14023 Mode Line
14024 ***********************************************************************/
14025
14026 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14027 FORCE is non-zero, redisplay mode lines unconditionally.
14028 Otherwise, redisplay only mode lines that are garbaged. Value is
14029 the number of windows whose mode lines were redisplayed. */
14030
14031 static int
14032 redisplay_mode_lines (window, force)
14033 Lisp_Object window;
14034 int force;
14035 {
14036 int nwindows = 0;
14037
14038 while (!NILP (window))
14039 {
14040 struct window *w = XWINDOW (window);
14041
14042 if (WINDOWP (w->hchild))
14043 nwindows += redisplay_mode_lines (w->hchild, force);
14044 else if (WINDOWP (w->vchild))
14045 nwindows += redisplay_mode_lines (w->vchild, force);
14046 else if (force
14047 || FRAME_GARBAGED_P (XFRAME (w->frame))
14048 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14049 {
14050 struct text_pos lpoint;
14051 struct buffer *old = current_buffer;
14052
14053 /* Set the window's buffer for the mode line display. */
14054 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14055 set_buffer_internal_1 (XBUFFER (w->buffer));
14056
14057 /* Point refers normally to the selected window. For any
14058 other window, set up appropriate value. */
14059 if (!EQ (window, selected_window))
14060 {
14061 struct text_pos pt;
14062
14063 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14064 if (CHARPOS (pt) < BEGV)
14065 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14066 else if (CHARPOS (pt) > (ZV - 1))
14067 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14068 else
14069 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14070 }
14071
14072 /* Display mode lines. */
14073 clear_glyph_matrix (w->desired_matrix);
14074 if (display_mode_lines (w))
14075 {
14076 ++nwindows;
14077 w->must_be_updated_p = 1;
14078 }
14079
14080 /* Restore old settings. */
14081 set_buffer_internal_1 (old);
14082 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14083 }
14084
14085 window = w->next;
14086 }
14087
14088 return nwindows;
14089 }
14090
14091
14092 /* Display the mode and/or top line of window W. Value is the number
14093 of mode lines displayed. */
14094
14095 static int
14096 display_mode_lines (w)
14097 struct window *w;
14098 {
14099 Lisp_Object old_selected_window, old_selected_frame;
14100 int n = 0;
14101
14102 old_selected_frame = selected_frame;
14103 selected_frame = w->frame;
14104 old_selected_window = selected_window;
14105 XSETWINDOW (selected_window, w);
14106
14107 /* These will be set while the mode line specs are processed. */
14108 line_number_displayed = 0;
14109 w->column_number_displayed = Qnil;
14110
14111 if (WINDOW_WANTS_MODELINE_P (w))
14112 {
14113 struct window *sel_w = XWINDOW (old_selected_window);
14114
14115 /* Select mode line face based on the real selected window. */
14116 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14117 current_buffer->mode_line_format);
14118 ++n;
14119 }
14120
14121 if (WINDOW_WANTS_HEADER_LINE_P (w))
14122 {
14123 display_mode_line (w, HEADER_LINE_FACE_ID,
14124 current_buffer->header_line_format);
14125 ++n;
14126 }
14127
14128 selected_frame = old_selected_frame;
14129 selected_window = old_selected_window;
14130 return n;
14131 }
14132
14133
14134 /* Display mode or top line of window W. FACE_ID specifies which line
14135 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14136 FORMAT is the mode line format to display. Value is the pixel
14137 height of the mode line displayed. */
14138
14139 static int
14140 display_mode_line (w, face_id, format)
14141 struct window *w;
14142 enum face_id face_id;
14143 Lisp_Object format;
14144 {
14145 struct it it;
14146 struct face *face;
14147
14148 init_iterator (&it, w, -1, -1, NULL, face_id);
14149 prepare_desired_row (it.glyph_row);
14150
14151 if (! mode_line_inverse_video)
14152 /* Force the mode-line to be displayed in the default face. */
14153 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14154
14155 /* Temporarily make frame's keyboard the current kboard so that
14156 kboard-local variables in the mode_line_format will get the right
14157 values. */
14158 push_frame_kboard (it.f);
14159 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14160 pop_frame_kboard ();
14161
14162 /* Fill up with spaces. */
14163 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14164
14165 compute_line_metrics (&it);
14166 it.glyph_row->full_width_p = 1;
14167 it.glyph_row->mode_line_p = 1;
14168 it.glyph_row->continued_p = 0;
14169 it.glyph_row->truncated_on_left_p = 0;
14170 it.glyph_row->truncated_on_right_p = 0;
14171
14172 /* Make a 3D mode-line have a shadow at its right end. */
14173 face = FACE_FROM_ID (it.f, face_id);
14174 extend_face_to_end_of_line (&it);
14175 if (face->box != FACE_NO_BOX)
14176 {
14177 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14178 + it.glyph_row->used[TEXT_AREA] - 1);
14179 last->right_box_line_p = 1;
14180 }
14181
14182 return it.glyph_row->height;
14183 }
14184
14185 /* Alist that caches the results of :propertize.
14186 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14187 Lisp_Object mode_line_proptrans_alist;
14188
14189 /* List of strings making up the mode-line. */
14190 Lisp_Object mode_line_string_list;
14191
14192 /* Base face property when building propertized mode line string. */
14193 static Lisp_Object mode_line_string_face;
14194 static Lisp_Object mode_line_string_face_prop;
14195
14196
14197 /* Contribute ELT to the mode line for window IT->w. How it
14198 translates into text depends on its data type.
14199
14200 IT describes the display environment in which we display, as usual.
14201
14202 DEPTH is the depth in recursion. It is used to prevent
14203 infinite recursion here.
14204
14205 FIELD_WIDTH is the number of characters the display of ELT should
14206 occupy in the mode line, and PRECISION is the maximum number of
14207 characters to display from ELT's representation. See
14208 display_string for details.
14209
14210 Returns the hpos of the end of the text generated by ELT.
14211
14212 PROPS is a property list to add to any string we encounter.
14213
14214 If RISKY is nonzero, remove (disregard) any properties in any string
14215 we encounter, and ignore :eval and :propertize.
14216
14217 If the global variable `frame_title_ptr' is non-NULL, then the output
14218 is passed to `store_frame_title' instead of `display_string'. */
14219
14220 static int
14221 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14222 struct it *it;
14223 int depth;
14224 int field_width, precision;
14225 Lisp_Object elt, props;
14226 int risky;
14227 {
14228 int n = 0, field, prec;
14229 int literal = 0;
14230
14231 tail_recurse:
14232 if (depth > 10)
14233 goto invalid;
14234
14235 depth++;
14236
14237 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14238 {
14239 case Lisp_String:
14240 {
14241 /* A string: output it and check for %-constructs within it. */
14242 unsigned char c;
14243 const unsigned char *this, *lisp_string;
14244
14245 if (!NILP (props) || risky)
14246 {
14247 Lisp_Object oprops, aelt;
14248 oprops = Ftext_properties_at (make_number (0), elt);
14249
14250 if (NILP (Fequal (props, oprops)) || risky)
14251 {
14252 /* If the starting string has properties,
14253 merge the specified ones onto the existing ones. */
14254 if (! NILP (oprops) && !risky)
14255 {
14256 Lisp_Object tem;
14257
14258 oprops = Fcopy_sequence (oprops);
14259 tem = props;
14260 while (CONSP (tem))
14261 {
14262 oprops = Fplist_put (oprops, XCAR (tem),
14263 XCAR (XCDR (tem)));
14264 tem = XCDR (XCDR (tem));
14265 }
14266 props = oprops;
14267 }
14268
14269 aelt = Fassoc (elt, mode_line_proptrans_alist);
14270 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
14271 {
14272 mode_line_proptrans_alist
14273 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
14274 elt = XCAR (aelt);
14275 }
14276 else
14277 {
14278 Lisp_Object tem;
14279
14280 elt = Fcopy_sequence (elt);
14281 Fset_text_properties (make_number (0), Flength (elt),
14282 props, elt);
14283 /* Add this item to mode_line_proptrans_alist. */
14284 mode_line_proptrans_alist
14285 = Fcons (Fcons (elt, props),
14286 mode_line_proptrans_alist);
14287 /* Truncate mode_line_proptrans_alist
14288 to at most 50 elements. */
14289 tem = Fnthcdr (make_number (50),
14290 mode_line_proptrans_alist);
14291 if (! NILP (tem))
14292 XSETCDR (tem, Qnil);
14293 }
14294 }
14295 }
14296
14297 this = SDATA (elt);
14298 lisp_string = this;
14299
14300 if (literal)
14301 {
14302 prec = precision - n;
14303 if (frame_title_ptr)
14304 n += store_frame_title (SDATA (elt), -1, prec);
14305 else if (!NILP (mode_line_string_list))
14306 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
14307 else
14308 n += display_string (NULL, elt, Qnil, 0, 0, it,
14309 0, prec, 0, STRING_MULTIBYTE (elt));
14310
14311 break;
14312 }
14313
14314 while ((precision <= 0 || n < precision)
14315 && *this
14316 && (frame_title_ptr
14317 || !NILP (mode_line_string_list)
14318 || it->current_x < it->last_visible_x))
14319 {
14320 const unsigned char *last = this;
14321
14322 /* Advance to end of string or next format specifier. */
14323 while ((c = *this++) != '\0' && c != '%')
14324 ;
14325
14326 if (this - 1 != last)
14327 {
14328 /* Output to end of string or up to '%'. Field width
14329 is length of string. Don't output more than
14330 PRECISION allows us. */
14331 --this;
14332
14333 prec = chars_in_text (last, this - last);
14334 if (precision > 0 && prec > precision - n)
14335 prec = precision - n;
14336
14337 if (frame_title_ptr)
14338 n += store_frame_title (last, 0, prec);
14339 else if (!NILP (mode_line_string_list))
14340 {
14341 int bytepos = last - lisp_string;
14342 int charpos = string_byte_to_char (elt, bytepos);
14343 n += store_mode_line_string (NULL,
14344 Fsubstring (elt, make_number (charpos),
14345 make_number (charpos + prec)),
14346 0, 0, 0, Qnil);
14347 }
14348 else
14349 {
14350 int bytepos = last - lisp_string;
14351 int charpos = string_byte_to_char (elt, bytepos);
14352 n += display_string (NULL, elt, Qnil, 0, charpos,
14353 it, 0, prec, 0,
14354 STRING_MULTIBYTE (elt));
14355 }
14356 }
14357 else /* c == '%' */
14358 {
14359 const unsigned char *percent_position = this;
14360
14361 /* Get the specified minimum width. Zero means
14362 don't pad. */
14363 field = 0;
14364 while ((c = *this++) >= '0' && c <= '9')
14365 field = field * 10 + c - '0';
14366
14367 /* Don't pad beyond the total padding allowed. */
14368 if (field_width - n > 0 && field > field_width - n)
14369 field = field_width - n;
14370
14371 /* Note that either PRECISION <= 0 or N < PRECISION. */
14372 prec = precision - n;
14373
14374 if (c == 'M')
14375 n += display_mode_element (it, depth, field, prec,
14376 Vglobal_mode_string, props,
14377 risky);
14378 else if (c != 0)
14379 {
14380 int multibyte;
14381 int bytepos, charpos;
14382 unsigned char *spec;
14383
14384 bytepos = percent_position - lisp_string;
14385 charpos = (STRING_MULTIBYTE (elt)
14386 ? string_byte_to_char (elt, bytepos)
14387 : bytepos);
14388
14389 spec
14390 = decode_mode_spec (it->w, c, field, prec, &multibyte);
14391
14392 if (frame_title_ptr)
14393 n += store_frame_title (spec, field, prec);
14394 else if (!NILP (mode_line_string_list))
14395 {
14396 int len = strlen (spec);
14397 Lisp_Object tem = make_string (spec, len);
14398 props = Ftext_properties_at (make_number (charpos), elt);
14399 /* Should only keep face property in props */
14400 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
14401 }
14402 else
14403 {
14404 int nglyphs_before, nwritten;
14405
14406 nglyphs_before = it->glyph_row->used[TEXT_AREA];
14407 nwritten = display_string (spec, Qnil, elt,
14408 charpos, 0, it,
14409 field, prec, 0,
14410 multibyte);
14411
14412 /* Assign to the glyphs written above the
14413 string where the `%x' came from, position
14414 of the `%'. */
14415 if (nwritten > 0)
14416 {
14417 struct glyph *glyph
14418 = (it->glyph_row->glyphs[TEXT_AREA]
14419 + nglyphs_before);
14420 int i;
14421
14422 for (i = 0; i < nwritten; ++i)
14423 {
14424 glyph[i].object = elt;
14425 glyph[i].charpos = charpos;
14426 }
14427
14428 n += nwritten;
14429 }
14430 }
14431 }
14432 else /* c == 0 */
14433 break;
14434 }
14435 }
14436 }
14437 break;
14438
14439 case Lisp_Symbol:
14440 /* A symbol: process the value of the symbol recursively
14441 as if it appeared here directly. Avoid error if symbol void.
14442 Special case: if value of symbol is a string, output the string
14443 literally. */
14444 {
14445 register Lisp_Object tem;
14446
14447 /* If the variable is not marked as risky to set
14448 then its contents are risky to use. */
14449 if (NILP (Fget (elt, Qrisky_local_variable)))
14450 risky = 1;
14451
14452 tem = Fboundp (elt);
14453 if (!NILP (tem))
14454 {
14455 tem = Fsymbol_value (elt);
14456 /* If value is a string, output that string literally:
14457 don't check for % within it. */
14458 if (STRINGP (tem))
14459 literal = 1;
14460
14461 if (!EQ (tem, elt))
14462 {
14463 /* Give up right away for nil or t. */
14464 elt = tem;
14465 goto tail_recurse;
14466 }
14467 }
14468 }
14469 break;
14470
14471 case Lisp_Cons:
14472 {
14473 register Lisp_Object car, tem;
14474
14475 /* A cons cell: five distinct cases.
14476 If first element is :eval or :propertize, do something special.
14477 If first element is a string or a cons, process all the elements
14478 and effectively concatenate them.
14479 If first element is a negative number, truncate displaying cdr to
14480 at most that many characters. If positive, pad (with spaces)
14481 to at least that many characters.
14482 If first element is a symbol, process the cadr or caddr recursively
14483 according to whether the symbol's value is non-nil or nil. */
14484 car = XCAR (elt);
14485 if (EQ (car, QCeval))
14486 {
14487 /* An element of the form (:eval FORM) means evaluate FORM
14488 and use the result as mode line elements. */
14489
14490 if (risky)
14491 break;
14492
14493 if (CONSP (XCDR (elt)))
14494 {
14495 Lisp_Object spec;
14496 spec = safe_eval (XCAR (XCDR (elt)));
14497 n += display_mode_element (it, depth, field_width - n,
14498 precision - n, spec, props,
14499 risky);
14500 }
14501 }
14502 else if (EQ (car, QCpropertize))
14503 {
14504 /* An element of the form (:propertize ELT PROPS...)
14505 means display ELT but applying properties PROPS. */
14506
14507 if (risky)
14508 break;
14509
14510 if (CONSP (XCDR (elt)))
14511 n += display_mode_element (it, depth, field_width - n,
14512 precision - n, XCAR (XCDR (elt)),
14513 XCDR (XCDR (elt)), risky);
14514 }
14515 else if (SYMBOLP (car))
14516 {
14517 tem = Fboundp (car);
14518 elt = XCDR (elt);
14519 if (!CONSP (elt))
14520 goto invalid;
14521 /* elt is now the cdr, and we know it is a cons cell.
14522 Use its car if CAR has a non-nil value. */
14523 if (!NILP (tem))
14524 {
14525 tem = Fsymbol_value (car);
14526 if (!NILP (tem))
14527 {
14528 elt = XCAR (elt);
14529 goto tail_recurse;
14530 }
14531 }
14532 /* Symbol's value is nil (or symbol is unbound)
14533 Get the cddr of the original list
14534 and if possible find the caddr and use that. */
14535 elt = XCDR (elt);
14536 if (NILP (elt))
14537 break;
14538 else if (!CONSP (elt))
14539 goto invalid;
14540 elt = XCAR (elt);
14541 goto tail_recurse;
14542 }
14543 else if (INTEGERP (car))
14544 {
14545 register int lim = XINT (car);
14546 elt = XCDR (elt);
14547 if (lim < 0)
14548 {
14549 /* Negative int means reduce maximum width. */
14550 if (precision <= 0)
14551 precision = -lim;
14552 else
14553 precision = min (precision, -lim);
14554 }
14555 else if (lim > 0)
14556 {
14557 /* Padding specified. Don't let it be more than
14558 current maximum. */
14559 if (precision > 0)
14560 lim = min (precision, lim);
14561
14562 /* If that's more padding than already wanted, queue it.
14563 But don't reduce padding already specified even if
14564 that is beyond the current truncation point. */
14565 field_width = max (lim, field_width);
14566 }
14567 goto tail_recurse;
14568 }
14569 else if (STRINGP (car) || CONSP (car))
14570 {
14571 register int limit = 50;
14572 /* Limit is to protect against circular lists. */
14573 while (CONSP (elt)
14574 && --limit > 0
14575 && (precision <= 0 || n < precision))
14576 {
14577 n += display_mode_element (it, depth, field_width - n,
14578 precision - n, XCAR (elt),
14579 props, risky);
14580 elt = XCDR (elt);
14581 }
14582 }
14583 }
14584 break;
14585
14586 default:
14587 invalid:
14588 if (frame_title_ptr)
14589 n += store_frame_title ("*invalid*", 0, precision - n);
14590 else if (!NILP (mode_line_string_list))
14591 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
14592 else
14593 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
14594 precision - n, 0, 0);
14595 return n;
14596 }
14597
14598 /* Pad to FIELD_WIDTH. */
14599 if (field_width > 0 && n < field_width)
14600 {
14601 if (frame_title_ptr)
14602 n += store_frame_title ("", field_width - n, 0);
14603 else if (!NILP (mode_line_string_list))
14604 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
14605 else
14606 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
14607 0, 0, 0);
14608 }
14609
14610 return n;
14611 }
14612
14613 /* Store a mode-line string element in mode_line_string_list.
14614
14615 If STRING is non-null, display that C string. Otherwise, the Lisp
14616 string LISP_STRING is displayed.
14617
14618 FIELD_WIDTH is the minimum number of output glyphs to produce.
14619 If STRING has fewer characters than FIELD_WIDTH, pad to the right
14620 with spaces. FIELD_WIDTH <= 0 means don't pad.
14621
14622 PRECISION is the maximum number of characters to output from
14623 STRING. PRECISION <= 0 means don't truncate the string.
14624
14625 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
14626 properties to the string.
14627
14628 PROPS are the properties to add to the string.
14629 The mode_line_string_face face property is always added to the string.
14630 */
14631
14632 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
14633 char *string;
14634 Lisp_Object lisp_string;
14635 int copy_string;
14636 int field_width;
14637 int precision;
14638 Lisp_Object props;
14639 {
14640 int len;
14641 int n = 0;
14642
14643 if (string != NULL)
14644 {
14645 len = strlen (string);
14646 if (precision > 0 && len > precision)
14647 len = precision;
14648 lisp_string = make_string (string, len);
14649 if (NILP (props))
14650 props = mode_line_string_face_prop;
14651 else if (!NILP (mode_line_string_face))
14652 {
14653 Lisp_Object face = Fplist_get (props, Qface);
14654 props = Fcopy_sequence (props);
14655 if (NILP (face))
14656 face = mode_line_string_face;
14657 else
14658 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14659 props = Fplist_put (props, Qface, face);
14660 }
14661 Fadd_text_properties (make_number (0), make_number (len),
14662 props, lisp_string);
14663 }
14664 else
14665 {
14666 len = XFASTINT (Flength (lisp_string));
14667 if (precision > 0 && len > precision)
14668 {
14669 len = precision;
14670 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
14671 precision = -1;
14672 }
14673 if (!NILP (mode_line_string_face))
14674 {
14675 Lisp_Object face;
14676 if (NILP (props))
14677 props = Ftext_properties_at (make_number (0), lisp_string);
14678 face = Fplist_get (props, Qface);
14679 if (NILP (face))
14680 face = mode_line_string_face;
14681 else
14682 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
14683 props = Fcons (Qface, Fcons (face, Qnil));
14684 if (copy_string)
14685 lisp_string = Fcopy_sequence (lisp_string);
14686 }
14687 if (!NILP (props))
14688 Fadd_text_properties (make_number (0), make_number (len),
14689 props, lisp_string);
14690 }
14691
14692 if (len > 0)
14693 {
14694 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14695 n += len;
14696 }
14697
14698 if (field_width > len)
14699 {
14700 field_width -= len;
14701 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
14702 if (!NILP (props))
14703 Fadd_text_properties (make_number (0), make_number (field_width),
14704 props, lisp_string);
14705 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
14706 n += field_width;
14707 }
14708
14709 return n;
14710 }
14711
14712
14713 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
14714 0, 3, 0,
14715 doc: /* Return the mode-line of selected window as a string.
14716 First optional arg FORMAT specifies a different format string (see
14717 `mode-line-format' for details) to use. If FORMAT is t, return
14718 the buffer's header-line. Second optional arg WINDOW specifies a
14719 different window to use as the context for the formatting.
14720 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
14721 (format, window, no_props)
14722 Lisp_Object format, window, no_props;
14723 {
14724 struct it it;
14725 int len;
14726 struct window *w;
14727 struct buffer *old_buffer = NULL;
14728 enum face_id face_id = DEFAULT_FACE_ID;
14729
14730 if (NILP (window))
14731 window = selected_window;
14732 CHECK_WINDOW (window);
14733 w = XWINDOW (window);
14734 CHECK_BUFFER (w->buffer);
14735
14736 if (XBUFFER (w->buffer) != current_buffer)
14737 {
14738 old_buffer = current_buffer;
14739 set_buffer_internal_1 (XBUFFER (w->buffer));
14740 }
14741
14742 if (NILP (format) || EQ (format, Qt))
14743 {
14744 face_id = NILP (format)
14745 ? CURRENT_MODE_LINE_FACE_ID (w) :
14746 HEADER_LINE_FACE_ID;
14747 format = NILP (format)
14748 ? current_buffer->mode_line_format
14749 : current_buffer->header_line_format;
14750 }
14751
14752 init_iterator (&it, w, -1, -1, NULL, face_id);
14753
14754 if (NILP (no_props))
14755 {
14756 mode_line_string_face =
14757 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
14758 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
14759 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
14760
14761 mode_line_string_face_prop =
14762 NILP (mode_line_string_face) ? Qnil :
14763 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
14764
14765 /* We need a dummy last element in mode_line_string_list to
14766 indicate we are building the propertized mode-line string.
14767 Using mode_line_string_face_prop here GC protects it. */
14768 mode_line_string_list =
14769 Fcons (mode_line_string_face_prop, Qnil);
14770 frame_title_ptr = NULL;
14771 }
14772 else
14773 {
14774 mode_line_string_face_prop = Qnil;
14775 mode_line_string_list = Qnil;
14776 frame_title_ptr = frame_title_buf;
14777 }
14778
14779 push_frame_kboard (it.f);
14780 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14781 pop_frame_kboard ();
14782
14783 if (old_buffer)
14784 set_buffer_internal_1 (old_buffer);
14785
14786 if (NILP (no_props))
14787 {
14788 Lisp_Object str;
14789 mode_line_string_list = Fnreverse (mode_line_string_list);
14790 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
14791 make_string ("", 0));
14792 mode_line_string_face_prop = Qnil;
14793 mode_line_string_list = Qnil;
14794 return str;
14795 }
14796
14797 len = frame_title_ptr - frame_title_buf;
14798 if (len > 0 && frame_title_ptr[-1] == '-')
14799 {
14800 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
14801 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
14802 ;
14803 frame_title_ptr += 3; /* restore last non-dash + two dashes */
14804 if (len > frame_title_ptr - frame_title_buf)
14805 len = frame_title_ptr - frame_title_buf;
14806 }
14807
14808 frame_title_ptr = NULL;
14809 return make_string (frame_title_buf, len);
14810 }
14811
14812 /* Write a null-terminated, right justified decimal representation of
14813 the positive integer D to BUF using a minimal field width WIDTH. */
14814
14815 static void
14816 pint2str (buf, width, d)
14817 register char *buf;
14818 register int width;
14819 register int d;
14820 {
14821 register char *p = buf;
14822
14823 if (d <= 0)
14824 *p++ = '0';
14825 else
14826 {
14827 while (d > 0)
14828 {
14829 *p++ = d % 10 + '0';
14830 d /= 10;
14831 }
14832 }
14833
14834 for (width -= (int) (p - buf); width > 0; --width)
14835 *p++ = ' ';
14836 *p-- = '\0';
14837 while (p > buf)
14838 {
14839 d = *buf;
14840 *buf++ = *p;
14841 *p-- = d;
14842 }
14843 }
14844
14845 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
14846 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
14847 type of CODING_SYSTEM. Return updated pointer into BUF. */
14848
14849 static unsigned char invalid_eol_type[] = "(*invalid*)";
14850
14851 static char *
14852 decode_mode_spec_coding (coding_system, buf, eol_flag)
14853 Lisp_Object coding_system;
14854 register char *buf;
14855 int eol_flag;
14856 {
14857 Lisp_Object val;
14858 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
14859 const unsigned char *eol_str;
14860 int eol_str_len;
14861 /* The EOL conversion we are using. */
14862 Lisp_Object eoltype;
14863
14864 val = Fget (coding_system, Qcoding_system);
14865 eoltype = Qnil;
14866
14867 if (!VECTORP (val)) /* Not yet decided. */
14868 {
14869 if (multibyte)
14870 *buf++ = '-';
14871 if (eol_flag)
14872 eoltype = eol_mnemonic_undecided;
14873 /* Don't mention EOL conversion if it isn't decided. */
14874 }
14875 else
14876 {
14877 Lisp_Object eolvalue;
14878
14879 eolvalue = Fget (coding_system, Qeol_type);
14880
14881 if (multibyte)
14882 *buf++ = XFASTINT (AREF (val, 1));
14883
14884 if (eol_flag)
14885 {
14886 /* The EOL conversion that is normal on this system. */
14887
14888 if (NILP (eolvalue)) /* Not yet decided. */
14889 eoltype = eol_mnemonic_undecided;
14890 else if (VECTORP (eolvalue)) /* Not yet decided. */
14891 eoltype = eol_mnemonic_undecided;
14892 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
14893 eoltype = (XFASTINT (eolvalue) == 0
14894 ? eol_mnemonic_unix
14895 : (XFASTINT (eolvalue) == 1
14896 ? eol_mnemonic_dos : eol_mnemonic_mac));
14897 }
14898 }
14899
14900 if (eol_flag)
14901 {
14902 /* Mention the EOL conversion if it is not the usual one. */
14903 if (STRINGP (eoltype))
14904 {
14905 eol_str = SDATA (eoltype);
14906 eol_str_len = SBYTES (eoltype);
14907 }
14908 else if (INTEGERP (eoltype)
14909 && CHAR_VALID_P (XINT (eoltype), 0))
14910 {
14911 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
14912 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
14913 eol_str = tmp;
14914 }
14915 else
14916 {
14917 eol_str = invalid_eol_type;
14918 eol_str_len = sizeof (invalid_eol_type) - 1;
14919 }
14920 bcopy (eol_str, buf, eol_str_len);
14921 buf += eol_str_len;
14922 }
14923
14924 return buf;
14925 }
14926
14927 /* Return a string for the output of a mode line %-spec for window W,
14928 generated by character C. PRECISION >= 0 means don't return a
14929 string longer than that value. FIELD_WIDTH > 0 means pad the
14930 string returned with spaces to that value. Return 1 in *MULTIBYTE
14931 if the result is multibyte text. */
14932
14933 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
14934
14935 static char *
14936 decode_mode_spec (w, c, field_width, precision, multibyte)
14937 struct window *w;
14938 register int c;
14939 int field_width, precision;
14940 int *multibyte;
14941 {
14942 Lisp_Object obj;
14943 struct frame *f = XFRAME (WINDOW_FRAME (w));
14944 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
14945 struct buffer *b = XBUFFER (w->buffer);
14946
14947 obj = Qnil;
14948 *multibyte = 0;
14949
14950 switch (c)
14951 {
14952 case '*':
14953 if (!NILP (b->read_only))
14954 return "%";
14955 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14956 return "*";
14957 return "-";
14958
14959 case '+':
14960 /* This differs from %* only for a modified read-only buffer. */
14961 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14962 return "*";
14963 if (!NILP (b->read_only))
14964 return "%";
14965 return "-";
14966
14967 case '&':
14968 /* This differs from %* in ignoring read-only-ness. */
14969 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
14970 return "*";
14971 return "-";
14972
14973 case '%':
14974 return "%";
14975
14976 case '[':
14977 {
14978 int i;
14979 char *p;
14980
14981 if (command_loop_level > 5)
14982 return "[[[... ";
14983 p = decode_mode_spec_buf;
14984 for (i = 0; i < command_loop_level; i++)
14985 *p++ = '[';
14986 *p = 0;
14987 return decode_mode_spec_buf;
14988 }
14989
14990 case ']':
14991 {
14992 int i;
14993 char *p;
14994
14995 if (command_loop_level > 5)
14996 return " ...]]]";
14997 p = decode_mode_spec_buf;
14998 for (i = 0; i < command_loop_level; i++)
14999 *p++ = ']';
15000 *p = 0;
15001 return decode_mode_spec_buf;
15002 }
15003
15004 case '-':
15005 {
15006 register int i;
15007
15008 /* Let lots_of_dashes be a string of infinite length. */
15009 if (!NILP (mode_line_string_list))
15010 return "--";
15011 if (field_width <= 0
15012 || field_width > sizeof (lots_of_dashes))
15013 {
15014 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15015 decode_mode_spec_buf[i] = '-';
15016 decode_mode_spec_buf[i] = '\0';
15017 return decode_mode_spec_buf;
15018 }
15019 else
15020 return lots_of_dashes;
15021 }
15022
15023 case 'b':
15024 obj = b->name;
15025 break;
15026
15027 case 'c':
15028 {
15029 int col = (int) current_column (); /* iftc */
15030 w->column_number_displayed = make_number (col);
15031 pint2str (decode_mode_spec_buf, field_width, col);
15032 return decode_mode_spec_buf;
15033 }
15034
15035 case 'F':
15036 /* %F displays the frame name. */
15037 if (!NILP (f->title))
15038 return (char *) SDATA (f->title);
15039 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15040 return (char *) SDATA (f->name);
15041 return "Emacs";
15042
15043 case 'f':
15044 obj = b->filename;
15045 break;
15046
15047 case 'l':
15048 {
15049 int startpos = XMARKER (w->start)->charpos;
15050 int startpos_byte = marker_byte_position (w->start);
15051 int line, linepos, linepos_byte, topline;
15052 int nlines, junk;
15053 int height = XFASTINT (w->height);
15054
15055 /* If we decided that this buffer isn't suitable for line numbers,
15056 don't forget that too fast. */
15057 if (EQ (w->base_line_pos, w->buffer))
15058 goto no_value;
15059 /* But do forget it, if the window shows a different buffer now. */
15060 else if (BUFFERP (w->base_line_pos))
15061 w->base_line_pos = Qnil;
15062
15063 /* If the buffer is very big, don't waste time. */
15064 if (INTEGERP (Vline_number_display_limit)
15065 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15066 {
15067 w->base_line_pos = Qnil;
15068 w->base_line_number = Qnil;
15069 goto no_value;
15070 }
15071
15072 if (!NILP (w->base_line_number)
15073 && !NILP (w->base_line_pos)
15074 && XFASTINT (w->base_line_pos) <= startpos)
15075 {
15076 line = XFASTINT (w->base_line_number);
15077 linepos = XFASTINT (w->base_line_pos);
15078 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15079 }
15080 else
15081 {
15082 line = 1;
15083 linepos = BUF_BEGV (b);
15084 linepos_byte = BUF_BEGV_BYTE (b);
15085 }
15086
15087 /* Count lines from base line to window start position. */
15088 nlines = display_count_lines (linepos, linepos_byte,
15089 startpos_byte,
15090 startpos, &junk);
15091
15092 topline = nlines + line;
15093
15094 /* Determine a new base line, if the old one is too close
15095 or too far away, or if we did not have one.
15096 "Too close" means it's plausible a scroll-down would
15097 go back past it. */
15098 if (startpos == BUF_BEGV (b))
15099 {
15100 w->base_line_number = make_number (topline);
15101 w->base_line_pos = make_number (BUF_BEGV (b));
15102 }
15103 else if (nlines < height + 25 || nlines > height * 3 + 50
15104 || linepos == BUF_BEGV (b))
15105 {
15106 int limit = BUF_BEGV (b);
15107 int limit_byte = BUF_BEGV_BYTE (b);
15108 int position;
15109 int distance = (height * 2 + 30) * line_number_display_limit_width;
15110
15111 if (startpos - distance > limit)
15112 {
15113 limit = startpos - distance;
15114 limit_byte = CHAR_TO_BYTE (limit);
15115 }
15116
15117 nlines = display_count_lines (startpos, startpos_byte,
15118 limit_byte,
15119 - (height * 2 + 30),
15120 &position);
15121 /* If we couldn't find the lines we wanted within
15122 line_number_display_limit_width chars per line,
15123 give up on line numbers for this window. */
15124 if (position == limit_byte && limit == startpos - distance)
15125 {
15126 w->base_line_pos = w->buffer;
15127 w->base_line_number = Qnil;
15128 goto no_value;
15129 }
15130
15131 w->base_line_number = make_number (topline - nlines);
15132 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15133 }
15134
15135 /* Now count lines from the start pos to point. */
15136 nlines = display_count_lines (startpos, startpos_byte,
15137 PT_BYTE, PT, &junk);
15138
15139 /* Record that we did display the line number. */
15140 line_number_displayed = 1;
15141
15142 /* Make the string to show. */
15143 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15144 return decode_mode_spec_buf;
15145 no_value:
15146 {
15147 char* p = decode_mode_spec_buf;
15148 int pad = field_width - 2;
15149 while (pad-- > 0)
15150 *p++ = ' ';
15151 *p++ = '?';
15152 *p++ = '?';
15153 *p = '\0';
15154 return decode_mode_spec_buf;
15155 }
15156 }
15157 break;
15158
15159 case 'm':
15160 obj = b->mode_name;
15161 break;
15162
15163 case 'n':
15164 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15165 return " Narrow";
15166 break;
15167
15168 case 'p':
15169 {
15170 int pos = marker_position (w->start);
15171 int total = BUF_ZV (b) - BUF_BEGV (b);
15172
15173 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15174 {
15175 if (pos <= BUF_BEGV (b))
15176 return "All";
15177 else
15178 return "Bottom";
15179 }
15180 else if (pos <= BUF_BEGV (b))
15181 return "Top";
15182 else
15183 {
15184 if (total > 1000000)
15185 /* Do it differently for a large value, to avoid overflow. */
15186 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15187 else
15188 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15189 /* We can't normally display a 3-digit number,
15190 so get us a 2-digit number that is close. */
15191 if (total == 100)
15192 total = 99;
15193 sprintf (decode_mode_spec_buf, "%2d%%", total);
15194 return decode_mode_spec_buf;
15195 }
15196 }
15197
15198 /* Display percentage of size above the bottom of the screen. */
15199 case 'P':
15200 {
15201 int toppos = marker_position (w->start);
15202 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15203 int total = BUF_ZV (b) - BUF_BEGV (b);
15204
15205 if (botpos >= BUF_ZV (b))
15206 {
15207 if (toppos <= BUF_BEGV (b))
15208 return "All";
15209 else
15210 return "Bottom";
15211 }
15212 else
15213 {
15214 if (total > 1000000)
15215 /* Do it differently for a large value, to avoid overflow. */
15216 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15217 else
15218 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15219 /* We can't normally display a 3-digit number,
15220 so get us a 2-digit number that is close. */
15221 if (total == 100)
15222 total = 99;
15223 if (toppos <= BUF_BEGV (b))
15224 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15225 else
15226 sprintf (decode_mode_spec_buf, "%2d%%", total);
15227 return decode_mode_spec_buf;
15228 }
15229 }
15230
15231 case 's':
15232 /* status of process */
15233 obj = Fget_buffer_process (w->buffer);
15234 if (NILP (obj))
15235 return "no process";
15236 #ifdef subprocesses
15237 obj = Fsymbol_name (Fprocess_status (obj));
15238 #endif
15239 break;
15240
15241 case 't': /* indicate TEXT or BINARY */
15242 #ifdef MODE_LINE_BINARY_TEXT
15243 return MODE_LINE_BINARY_TEXT (b);
15244 #else
15245 return "T";
15246 #endif
15247
15248 case 'z':
15249 /* coding-system (not including end-of-line format) */
15250 case 'Z':
15251 /* coding-system (including end-of-line type) */
15252 {
15253 int eol_flag = (c == 'Z');
15254 char *p = decode_mode_spec_buf;
15255
15256 if (! FRAME_WINDOW_P (f))
15257 {
15258 /* No need to mention EOL here--the terminal never needs
15259 to do EOL conversion. */
15260 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15261 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
15262 }
15263 p = decode_mode_spec_coding (b->buffer_file_coding_system,
15264 p, eol_flag);
15265
15266 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
15267 #ifdef subprocesses
15268 obj = Fget_buffer_process (Fcurrent_buffer ());
15269 if (PROCESSP (obj))
15270 {
15271 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
15272 p, eol_flag);
15273 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
15274 p, eol_flag);
15275 }
15276 #endif /* subprocesses */
15277 #endif /* 0 */
15278 *p = 0;
15279 return decode_mode_spec_buf;
15280 }
15281 }
15282
15283 if (STRINGP (obj))
15284 {
15285 *multibyte = STRING_MULTIBYTE (obj);
15286 return (char *) SDATA (obj);
15287 }
15288 else
15289 return "";
15290 }
15291
15292
15293 /* Count up to COUNT lines starting from START / START_BYTE.
15294 But don't go beyond LIMIT_BYTE.
15295 Return the number of lines thus found (always nonnegative).
15296
15297 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
15298
15299 static int
15300 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15301 int start, start_byte, limit_byte, count;
15302 int *byte_pos_ptr;
15303 {
15304 register unsigned char *cursor;
15305 unsigned char *base;
15306
15307 register int ceiling;
15308 register unsigned char *ceiling_addr;
15309 int orig_count = count;
15310
15311 /* If we are not in selective display mode,
15312 check only for newlines. */
15313 int selective_display = (!NILP (current_buffer->selective_display)
15314 && !INTEGERP (current_buffer->selective_display));
15315
15316 if (count > 0)
15317 {
15318 while (start_byte < limit_byte)
15319 {
15320 ceiling = BUFFER_CEILING_OF (start_byte);
15321 ceiling = min (limit_byte - 1, ceiling);
15322 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
15323 base = (cursor = BYTE_POS_ADDR (start_byte));
15324 while (1)
15325 {
15326 if (selective_display)
15327 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
15328 ;
15329 else
15330 while (*cursor != '\n' && ++cursor != ceiling_addr)
15331 ;
15332
15333 if (cursor != ceiling_addr)
15334 {
15335 if (--count == 0)
15336 {
15337 start_byte += cursor - base + 1;
15338 *byte_pos_ptr = start_byte;
15339 return orig_count;
15340 }
15341 else
15342 if (++cursor == ceiling_addr)
15343 break;
15344 }
15345 else
15346 break;
15347 }
15348 start_byte += cursor - base;
15349 }
15350 }
15351 else
15352 {
15353 while (start_byte > limit_byte)
15354 {
15355 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
15356 ceiling = max (limit_byte, ceiling);
15357 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
15358 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
15359 while (1)
15360 {
15361 if (selective_display)
15362 while (--cursor != ceiling_addr
15363 && *cursor != '\n' && *cursor != 015)
15364 ;
15365 else
15366 while (--cursor != ceiling_addr && *cursor != '\n')
15367 ;
15368
15369 if (cursor != ceiling_addr)
15370 {
15371 if (++count == 0)
15372 {
15373 start_byte += cursor - base + 1;
15374 *byte_pos_ptr = start_byte;
15375 /* When scanning backwards, we should
15376 not count the newline posterior to which we stop. */
15377 return - orig_count - 1;
15378 }
15379 }
15380 else
15381 break;
15382 }
15383 /* Here we add 1 to compensate for the last decrement
15384 of CURSOR, which took it past the valid range. */
15385 start_byte += cursor - base + 1;
15386 }
15387 }
15388
15389 *byte_pos_ptr = limit_byte;
15390
15391 if (count < 0)
15392 return - orig_count + count;
15393 return orig_count - count;
15394
15395 }
15396
15397
15398 \f
15399 /***********************************************************************
15400 Displaying strings
15401 ***********************************************************************/
15402
15403 /* Display a NUL-terminated string, starting with index START.
15404
15405 If STRING is non-null, display that C string. Otherwise, the Lisp
15406 string LISP_STRING is displayed.
15407
15408 If FACE_STRING is not nil, FACE_STRING_POS is a position in
15409 FACE_STRING. Display STRING or LISP_STRING with the face at
15410 FACE_STRING_POS in FACE_STRING:
15411
15412 Display the string in the environment given by IT, but use the
15413 standard display table, temporarily.
15414
15415 FIELD_WIDTH is the minimum number of output glyphs to produce.
15416 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15417 with spaces. If STRING has more characters, more than FIELD_WIDTH
15418 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
15419
15420 PRECISION is the maximum number of characters to output from
15421 STRING. PRECISION < 0 means don't truncate the string.
15422
15423 This is roughly equivalent to printf format specifiers:
15424
15425 FIELD_WIDTH PRECISION PRINTF
15426 ----------------------------------------
15427 -1 -1 %s
15428 -1 10 %.10s
15429 10 -1 %10s
15430 20 10 %20.10s
15431
15432 MULTIBYTE zero means do not display multibyte chars, > 0 means do
15433 display them, and < 0 means obey the current buffer's value of
15434 enable_multibyte_characters.
15435
15436 Value is the number of glyphs produced. */
15437
15438 static int
15439 display_string (string, lisp_string, face_string, face_string_pos,
15440 start, it, field_width, precision, max_x, multibyte)
15441 unsigned char *string;
15442 Lisp_Object lisp_string;
15443 Lisp_Object face_string;
15444 int face_string_pos;
15445 int start;
15446 struct it *it;
15447 int field_width, precision, max_x;
15448 int multibyte;
15449 {
15450 int hpos_at_start = it->hpos;
15451 int saved_face_id = it->face_id;
15452 struct glyph_row *row = it->glyph_row;
15453
15454 /* Initialize the iterator IT for iteration over STRING beginning
15455 with index START. */
15456 reseat_to_string (it, string, lisp_string, start,
15457 precision, field_width, multibyte);
15458
15459 /* If displaying STRING, set up the face of the iterator
15460 from LISP_STRING, if that's given. */
15461 if (STRINGP (face_string))
15462 {
15463 int endptr;
15464 struct face *face;
15465
15466 it->face_id
15467 = face_at_string_position (it->w, face_string, face_string_pos,
15468 0, it->region_beg_charpos,
15469 it->region_end_charpos,
15470 &endptr, it->base_face_id, 0);
15471 face = FACE_FROM_ID (it->f, it->face_id);
15472 it->face_box_p = face->box != FACE_NO_BOX;
15473 }
15474
15475 /* Set max_x to the maximum allowed X position. Don't let it go
15476 beyond the right edge of the window. */
15477 if (max_x <= 0)
15478 max_x = it->last_visible_x;
15479 else
15480 max_x = min (max_x, it->last_visible_x);
15481
15482 /* Skip over display elements that are not visible. because IT->w is
15483 hscrolled. */
15484 if (it->current_x < it->first_visible_x)
15485 move_it_in_display_line_to (it, 100000, it->first_visible_x,
15486 MOVE_TO_POS | MOVE_TO_X);
15487
15488 row->ascent = it->max_ascent;
15489 row->height = it->max_ascent + it->max_descent;
15490 row->phys_ascent = it->max_phys_ascent;
15491 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15492
15493 /* This condition is for the case that we are called with current_x
15494 past last_visible_x. */
15495 while (it->current_x < max_x)
15496 {
15497 int x_before, x, n_glyphs_before, i, nglyphs;
15498
15499 /* Get the next display element. */
15500 if (!get_next_display_element (it))
15501 break;
15502
15503 /* Produce glyphs. */
15504 x_before = it->current_x;
15505 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
15506 PRODUCE_GLYPHS (it);
15507
15508 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
15509 i = 0;
15510 x = x_before;
15511 while (i < nglyphs)
15512 {
15513 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15514
15515 if (!it->truncate_lines_p
15516 && x + glyph->pixel_width > max_x)
15517 {
15518 /* End of continued line or max_x reached. */
15519 if (CHAR_GLYPH_PADDING_P (*glyph))
15520 {
15521 /* A wide character is unbreakable. */
15522 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
15523 it->current_x = x_before;
15524 }
15525 else
15526 {
15527 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
15528 it->current_x = x;
15529 }
15530 break;
15531 }
15532 else if (x + glyph->pixel_width > it->first_visible_x)
15533 {
15534 /* Glyph is at least partially visible. */
15535 ++it->hpos;
15536 if (x < it->first_visible_x)
15537 it->glyph_row->x = x - it->first_visible_x;
15538 }
15539 else
15540 {
15541 /* Glyph is off the left margin of the display area.
15542 Should not happen. */
15543 abort ();
15544 }
15545
15546 row->ascent = max (row->ascent, it->max_ascent);
15547 row->height = max (row->height, it->max_ascent + it->max_descent);
15548 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15549 row->phys_height = max (row->phys_height,
15550 it->max_phys_ascent + it->max_phys_descent);
15551 x += glyph->pixel_width;
15552 ++i;
15553 }
15554
15555 /* Stop if max_x reached. */
15556 if (i < nglyphs)
15557 break;
15558
15559 /* Stop at line ends. */
15560 if (ITERATOR_AT_END_OF_LINE_P (it))
15561 {
15562 it->continuation_lines_width = 0;
15563 break;
15564 }
15565
15566 set_iterator_to_next (it, 1);
15567
15568 /* Stop if truncating at the right edge. */
15569 if (it->truncate_lines_p
15570 && it->current_x >= it->last_visible_x)
15571 {
15572 /* Add truncation mark, but don't do it if the line is
15573 truncated at a padding space. */
15574 if (IT_CHARPOS (*it) < it->string_nchars)
15575 {
15576 if (!FRAME_WINDOW_P (it->f))
15577 {
15578 int i, n;
15579
15580 if (it->current_x > it->last_visible_x)
15581 {
15582 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15583 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15584 break;
15585 for (n = row->used[TEXT_AREA]; i < n; ++i)
15586 {
15587 row->used[TEXT_AREA] = i;
15588 produce_special_glyphs (it, IT_TRUNCATION);
15589 }
15590 }
15591 produce_special_glyphs (it, IT_TRUNCATION);
15592 }
15593 it->glyph_row->truncated_on_right_p = 1;
15594 }
15595 break;
15596 }
15597 }
15598
15599 /* Maybe insert a truncation at the left. */
15600 if (it->first_visible_x
15601 && IT_CHARPOS (*it) > 0)
15602 {
15603 if (!FRAME_WINDOW_P (it->f))
15604 insert_left_trunc_glyphs (it);
15605 it->glyph_row->truncated_on_left_p = 1;
15606 }
15607
15608 it->face_id = saved_face_id;
15609
15610 /* Value is number of columns displayed. */
15611 return it->hpos - hpos_at_start;
15612 }
15613
15614
15615 \f
15616 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
15617 appears as an element of LIST or as the car of an element of LIST.
15618 If PROPVAL is a list, compare each element against LIST in that
15619 way, and return 1/2 if any element of PROPVAL is found in LIST.
15620 Otherwise return 0. This function cannot quit.
15621 The return value is 2 if the text is invisible but with an ellipsis
15622 and 1 if it's invisible and without an ellipsis. */
15623
15624 int
15625 invisible_p (propval, list)
15626 register Lisp_Object propval;
15627 Lisp_Object list;
15628 {
15629 register Lisp_Object tail, proptail;
15630
15631 for (tail = list; CONSP (tail); tail = XCDR (tail))
15632 {
15633 register Lisp_Object tem;
15634 tem = XCAR (tail);
15635 if (EQ (propval, tem))
15636 return 1;
15637 if (CONSP (tem) && EQ (propval, XCAR (tem)))
15638 return NILP (XCDR (tem)) ? 1 : 2;
15639 }
15640
15641 if (CONSP (propval))
15642 {
15643 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
15644 {
15645 Lisp_Object propelt;
15646 propelt = XCAR (proptail);
15647 for (tail = list; CONSP (tail); tail = XCDR (tail))
15648 {
15649 register Lisp_Object tem;
15650 tem = XCAR (tail);
15651 if (EQ (propelt, tem))
15652 return 1;
15653 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
15654 return NILP (XCDR (tem)) ? 1 : 2;
15655 }
15656 }
15657 }
15658
15659 return 0;
15660 }
15661
15662 \f
15663 /***********************************************************************
15664 Glyph Display
15665 ***********************************************************************/
15666
15667 #if GLYPH_DEBUG
15668
15669 void
15670 dump_glyph_string (s)
15671 struct glyph_string *s;
15672 {
15673 fprintf (stderr, "glyph string\n");
15674 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
15675 s->x, s->y, s->width, s->height);
15676 fprintf (stderr, " ybase = %d\n", s->ybase);
15677 fprintf (stderr, " hl = %d\n", s->hl);
15678 fprintf (stderr, " left overhang = %d, right = %d\n",
15679 s->left_overhang, s->right_overhang);
15680 fprintf (stderr, " nchars = %d\n", s->nchars);
15681 fprintf (stderr, " extends to end of line = %d\n",
15682 s->extends_to_end_of_line_p);
15683 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
15684 fprintf (stderr, " bg width = %d\n", s->background_width);
15685 }
15686
15687 #endif /* GLYPH_DEBUG */
15688
15689 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
15690 of XChar2b structures for S; it can't be allocated in
15691 init_glyph_string because it must be allocated via `alloca'. W
15692 is the window on which S is drawn. ROW and AREA are the glyph row
15693 and area within the row from which S is constructed. START is the
15694 index of the first glyph structure covered by S. HL is a
15695 face-override for drawing S. */
15696
15697 #ifdef HAVE_NTGUI
15698 #define OPTIONAL_HDC(hdc) hdc,
15699 #define DECLARE_HDC(hdc) HDC hdc;
15700 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
15701 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
15702 #endif
15703
15704 #ifndef OPTIONAL_HDC
15705 #define OPTIONAL_HDC(hdc)
15706 #define DECLARE_HDC(hdc)
15707 #define ALLOCATE_HDC(hdc, f)
15708 #define RELEASE_HDC(hdc, f)
15709 #endif
15710
15711 static void
15712 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
15713 struct glyph_string *s;
15714 DECLARE_HDC (hdc)
15715 XChar2b *char2b;
15716 struct window *w;
15717 struct glyph_row *row;
15718 enum glyph_row_area area;
15719 int start;
15720 enum draw_glyphs_face hl;
15721 {
15722 bzero (s, sizeof *s);
15723 s->w = w;
15724 s->f = XFRAME (w->frame);
15725 #ifdef HAVE_NTGUI
15726 s->hdc = hdc;
15727 #endif
15728 s->display = FRAME_X_DISPLAY (s->f);
15729 s->window = FRAME_X_WINDOW (s->f);
15730 s->char2b = char2b;
15731 s->hl = hl;
15732 s->row = row;
15733 s->area = area;
15734 s->first_glyph = row->glyphs[area] + start;
15735 s->height = row->height;
15736 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
15737
15738 /* Display the internal border below the tool-bar window. */
15739 if (s->w == XWINDOW (s->f->tool_bar_window))
15740 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
15741
15742 s->ybase = s->y + row->ascent;
15743 }
15744
15745
15746 /* Append the list of glyph strings with head H and tail T to the list
15747 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
15748
15749 static INLINE void
15750 append_glyph_string_lists (head, tail, h, t)
15751 struct glyph_string **head, **tail;
15752 struct glyph_string *h, *t;
15753 {
15754 if (h)
15755 {
15756 if (*head)
15757 (*tail)->next = h;
15758 else
15759 *head = h;
15760 h->prev = *tail;
15761 *tail = t;
15762 }
15763 }
15764
15765
15766 /* Prepend the list of glyph strings with head H and tail T to the
15767 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
15768 result. */
15769
15770 static INLINE void
15771 prepend_glyph_string_lists (head, tail, h, t)
15772 struct glyph_string **head, **tail;
15773 struct glyph_string *h, *t;
15774 {
15775 if (h)
15776 {
15777 if (*head)
15778 (*head)->prev = t;
15779 else
15780 *tail = t;
15781 t->next = *head;
15782 *head = h;
15783 }
15784 }
15785
15786
15787 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
15788 Set *HEAD and *TAIL to the resulting list. */
15789
15790 static INLINE void
15791 append_glyph_string (head, tail, s)
15792 struct glyph_string **head, **tail;
15793 struct glyph_string *s;
15794 {
15795 s->next = s->prev = NULL;
15796 append_glyph_string_lists (head, tail, s, s);
15797 }
15798
15799
15800 /* Get face and two-byte form of character glyph GLYPH on frame F.
15801 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
15802 a pointer to a realized face that is ready for display. */
15803
15804 static INLINE struct face *
15805 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
15806 struct frame *f;
15807 struct glyph *glyph;
15808 XChar2b *char2b;
15809 int *two_byte_p;
15810 {
15811 struct face *face;
15812
15813 xassert (glyph->type == CHAR_GLYPH);
15814 face = FACE_FROM_ID (f, glyph->face_id);
15815
15816 if (two_byte_p)
15817 *two_byte_p = 0;
15818
15819 if (!glyph->multibyte_p)
15820 {
15821 /* Unibyte case. We don't have to encode, but we have to make
15822 sure to use a face suitable for unibyte. */
15823 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
15824 }
15825 else if (glyph->u.ch < 128
15826 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
15827 {
15828 /* Case of ASCII in a face known to fit ASCII. */
15829 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
15830 }
15831 else
15832 {
15833 int c1, c2, charset;
15834
15835 /* Split characters into bytes. If c2 is -1 afterwards, C is
15836 really a one-byte character so that byte1 is zero. */
15837 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
15838 if (c2 > 0)
15839 STORE_XCHAR2B (char2b, c1, c2);
15840 else
15841 STORE_XCHAR2B (char2b, 0, c1);
15842
15843 /* Maybe encode the character in *CHAR2B. */
15844 if (charset != CHARSET_ASCII)
15845 {
15846 struct font_info *font_info
15847 = FONT_INFO_FROM_ID (f, face->font_info_id);
15848 if (font_info)
15849 glyph->font_type
15850 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
15851 }
15852 }
15853
15854 /* Make sure X resources of the face are allocated. */
15855 xassert (face != NULL);
15856 PREPARE_FACE_FOR_DISPLAY (f, face);
15857 return face;
15858 }
15859
15860
15861 /* Fill glyph string S with composition components specified by S->cmp.
15862
15863 FACES is an array of faces for all components of this composition.
15864 S->gidx is the index of the first component for S.
15865 OVERLAPS_P non-zero means S should draw the foreground only, and
15866 use its physical height for clipping.
15867
15868 Value is the index of a component not in S. */
15869
15870 static int
15871 fill_composite_glyph_string (s, faces, overlaps_p)
15872 struct glyph_string *s;
15873 struct face **faces;
15874 int overlaps_p;
15875 {
15876 int i;
15877
15878 xassert (s);
15879
15880 s->for_overlaps_p = overlaps_p;
15881
15882 s->face = faces[s->gidx];
15883 s->font = s->face->font;
15884 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
15885
15886 /* For all glyphs of this composition, starting at the offset
15887 S->gidx, until we reach the end of the definition or encounter a
15888 glyph that requires the different face, add it to S. */
15889 ++s->nchars;
15890 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
15891 ++s->nchars;
15892
15893 /* All glyph strings for the same composition has the same width,
15894 i.e. the width set for the first component of the composition. */
15895
15896 s->width = s->first_glyph->pixel_width;
15897
15898 /* If the specified font could not be loaded, use the frame's
15899 default font, but record the fact that we couldn't load it in
15900 the glyph string so that we can draw rectangles for the
15901 characters of the glyph string. */
15902 if (s->font == NULL)
15903 {
15904 s->font_not_found_p = 1;
15905 s->font = FRAME_FONT (s->f);
15906 }
15907
15908 /* Adjust base line for subscript/superscript text. */
15909 s->ybase += s->first_glyph->voffset;
15910
15911 xassert (s->face && s->face->gc);
15912
15913 /* This glyph string must always be drawn with 16-bit functions. */
15914 s->two_byte_p = 1;
15915
15916 return s->gidx + s->nchars;
15917 }
15918
15919
15920 /* Fill glyph string S from a sequence of character glyphs.
15921
15922 FACE_ID is the face id of the string. START is the index of the
15923 first glyph to consider, END is the index of the last + 1.
15924 OVERLAPS_P non-zero means S should draw the foreground only, and
15925 use its physical height for clipping.
15926
15927 Value is the index of the first glyph not in S. */
15928
15929 static int
15930 fill_glyph_string (s, face_id, start, end, overlaps_p)
15931 struct glyph_string *s;
15932 int face_id;
15933 int start, end, overlaps_p;
15934 {
15935 struct glyph *glyph, *last;
15936 int voffset;
15937 int glyph_not_available_p;
15938
15939 xassert (s->f == XFRAME (s->w->frame));
15940 xassert (s->nchars == 0);
15941 xassert (start >= 0 && end > start);
15942
15943 s->for_overlaps_p = overlaps_p,
15944 glyph = s->row->glyphs[s->area] + start;
15945 last = s->row->glyphs[s->area] + end;
15946 voffset = glyph->voffset;
15947
15948 glyph_not_available_p = glyph->glyph_not_available_p;
15949
15950 while (glyph < last
15951 && glyph->type == CHAR_GLYPH
15952 && glyph->voffset == voffset
15953 /* Same face id implies same font, nowadays. */
15954 && glyph->face_id == face_id
15955 && glyph->glyph_not_available_p == glyph_not_available_p)
15956 {
15957 int two_byte_p;
15958
15959 s->face = get_glyph_face_and_encoding (s->f, glyph,
15960 s->char2b + s->nchars,
15961 &two_byte_p);
15962 s->two_byte_p = two_byte_p;
15963 ++s->nchars;
15964 xassert (s->nchars <= end - start);
15965 s->width += glyph->pixel_width;
15966 ++glyph;
15967 }
15968
15969 s->font = s->face->font;
15970 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
15971
15972 /* If the specified font could not be loaded, use the frame's font,
15973 but record the fact that we couldn't load it in
15974 S->font_not_found_p so that we can draw rectangles for the
15975 characters of the glyph string. */
15976 if (s->font == NULL || glyph_not_available_p)
15977 {
15978 s->font_not_found_p = 1;
15979 s->font = FRAME_FONT (s->f);
15980 }
15981
15982 /* Adjust base line for subscript/superscript text. */
15983 s->ybase += voffset;
15984
15985 xassert (s->face && s->face->gc);
15986 return glyph - s->row->glyphs[s->area];
15987 }
15988
15989
15990 /* Fill glyph string S from image glyph S->first_glyph. */
15991
15992 static void
15993 fill_image_glyph_string (s)
15994 struct glyph_string *s;
15995 {
15996 xassert (s->first_glyph->type == IMAGE_GLYPH);
15997 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
15998 xassert (s->img);
15999 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16000 s->font = s->face->font;
16001 s->width = s->first_glyph->pixel_width;
16002
16003 /* Adjust base line for subscript/superscript text. */
16004 s->ybase += s->first_glyph->voffset;
16005 }
16006
16007
16008 /* Fill glyph string S from a sequence of stretch glyphs.
16009
16010 ROW is the glyph row in which the glyphs are found, AREA is the
16011 area within the row. START is the index of the first glyph to
16012 consider, END is the index of the last + 1.
16013
16014 Value is the index of the first glyph not in S. */
16015
16016 static int
16017 fill_stretch_glyph_string (s, row, area, start, end)
16018 struct glyph_string *s;
16019 struct glyph_row *row;
16020 enum glyph_row_area area;
16021 int start, end;
16022 {
16023 struct glyph *glyph, *last;
16024 int voffset, face_id;
16025
16026 xassert (s->first_glyph->type == STRETCH_GLYPH);
16027
16028 glyph = s->row->glyphs[s->area] + start;
16029 last = s->row->glyphs[s->area] + end;
16030 face_id = glyph->face_id;
16031 s->face = FACE_FROM_ID (s->f, face_id);
16032 s->font = s->face->font;
16033 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16034 s->width = glyph->pixel_width;
16035 voffset = glyph->voffset;
16036
16037 for (++glyph;
16038 (glyph < last
16039 && glyph->type == STRETCH_GLYPH
16040 && glyph->voffset == voffset
16041 && glyph->face_id == face_id);
16042 ++glyph)
16043 s->width += glyph->pixel_width;
16044
16045 /* Adjust base line for subscript/superscript text. */
16046 s->ybase += voffset;
16047
16048 /* The case that face->gc == 0 is handled when drawing the glyph
16049 string by calling PREPARE_FACE_FOR_DISPLAY. */
16050 xassert (s->face);
16051 return glyph - s->row->glyphs[s->area];
16052 }
16053
16054
16055 /* EXPORT for RIF:
16056 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16057 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16058 assumed to be zero. */
16059
16060 void
16061 x_get_glyph_overhangs (glyph, f, left, right)
16062 struct glyph *glyph;
16063 struct frame *f;
16064 int *left, *right;
16065 {
16066 *left = *right = 0;
16067
16068 if (glyph->type == CHAR_GLYPH)
16069 {
16070 XFontStruct *font;
16071 struct face *face;
16072 struct font_info *font_info;
16073 XChar2b char2b;
16074 XCharStruct *pcm;
16075
16076 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16077 font = face->font;
16078 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16079 if (font /* ++KFS: Should this be font_info ? */
16080 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16081 {
16082 if (pcm->rbearing > pcm->width)
16083 *right = pcm->rbearing - pcm->width;
16084 if (pcm->lbearing < 0)
16085 *left = -pcm->lbearing;
16086 }
16087 }
16088 }
16089
16090
16091 /* Return the index of the first glyph preceding glyph string S that
16092 is overwritten by S because of S's left overhang. Value is -1
16093 if no glyphs are overwritten. */
16094
16095 static int
16096 left_overwritten (s)
16097 struct glyph_string *s;
16098 {
16099 int k;
16100
16101 if (s->left_overhang)
16102 {
16103 int x = 0, i;
16104 struct glyph *glyphs = s->row->glyphs[s->area];
16105 int first = s->first_glyph - glyphs;
16106
16107 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16108 x -= glyphs[i].pixel_width;
16109
16110 k = i + 1;
16111 }
16112 else
16113 k = -1;
16114
16115 return k;
16116 }
16117
16118
16119 /* Return the index of the first glyph preceding glyph string S that
16120 is overwriting S because of its right overhang. Value is -1 if no
16121 glyph in front of S overwrites S. */
16122
16123 static int
16124 left_overwriting (s)
16125 struct glyph_string *s;
16126 {
16127 int i, k, x;
16128 struct glyph *glyphs = s->row->glyphs[s->area];
16129 int first = s->first_glyph - glyphs;
16130
16131 k = -1;
16132 x = 0;
16133 for (i = first - 1; i >= 0; --i)
16134 {
16135 int left, right;
16136 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16137 if (x + right > 0)
16138 k = i;
16139 x -= glyphs[i].pixel_width;
16140 }
16141
16142 return k;
16143 }
16144
16145
16146 /* Return the index of the last glyph following glyph string S that is
16147 not overwritten by S because of S's right overhang. Value is -1 if
16148 no such glyph is found. */
16149
16150 static int
16151 right_overwritten (s)
16152 struct glyph_string *s;
16153 {
16154 int k = -1;
16155
16156 if (s->right_overhang)
16157 {
16158 int x = 0, i;
16159 struct glyph *glyphs = s->row->glyphs[s->area];
16160 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16161 int end = s->row->used[s->area];
16162
16163 for (i = first; i < end && s->right_overhang > x; ++i)
16164 x += glyphs[i].pixel_width;
16165
16166 k = i;
16167 }
16168
16169 return k;
16170 }
16171
16172
16173 /* Return the index of the last glyph following glyph string S that
16174 overwrites S because of its left overhang. Value is negative
16175 if no such glyph is found. */
16176
16177 static int
16178 right_overwriting (s)
16179 struct glyph_string *s;
16180 {
16181 int i, k, x;
16182 int end = s->row->used[s->area];
16183 struct glyph *glyphs = s->row->glyphs[s->area];
16184 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16185
16186 k = -1;
16187 x = 0;
16188 for (i = first; i < end; ++i)
16189 {
16190 int left, right;
16191 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16192 if (x - left < 0)
16193 k = i;
16194 x += glyphs[i].pixel_width;
16195 }
16196
16197 return k;
16198 }
16199
16200
16201 /* Get face and two-byte form of character C in face FACE_ID on frame
16202 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16203 means we want to display multibyte text. DISPLAY_P non-zero means
16204 make sure that X resources for the face returned are allocated.
16205 Value is a pointer to a realized face that is ready for display if
16206 DISPLAY_P is non-zero. */
16207
16208 static INLINE struct face *
16209 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16210 struct frame *f;
16211 int c, face_id;
16212 XChar2b *char2b;
16213 int multibyte_p, display_p;
16214 {
16215 struct face *face = FACE_FROM_ID (f, face_id);
16216
16217 if (!multibyte_p)
16218 {
16219 /* Unibyte case. We don't have to encode, but we have to make
16220 sure to use a face suitable for unibyte. */
16221 STORE_XCHAR2B (char2b, 0, c);
16222 face_id = FACE_FOR_CHAR (f, face, c);
16223 face = FACE_FROM_ID (f, face_id);
16224 }
16225 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16226 {
16227 /* Case of ASCII in a face known to fit ASCII. */
16228 STORE_XCHAR2B (char2b, 0, c);
16229 }
16230 else
16231 {
16232 int c1, c2, charset;
16233
16234 /* Split characters into bytes. If c2 is -1 afterwards, C is
16235 really a one-byte character so that byte1 is zero. */
16236 SPLIT_CHAR (c, charset, c1, c2);
16237 if (c2 > 0)
16238 STORE_XCHAR2B (char2b, c1, c2);
16239 else
16240 STORE_XCHAR2B (char2b, 0, c1);
16241
16242 /* Maybe encode the character in *CHAR2B. */
16243 if (face->font != NULL)
16244 {
16245 struct font_info *font_info
16246 = FONT_INFO_FROM_ID (f, face->font_info_id);
16247 if (font_info)
16248 rif->encode_char (c, char2b, font_info, 0);
16249 }
16250 }
16251
16252 /* Make sure X resources of the face are allocated. */
16253 #ifdef HAVE_X_WINDOWS
16254 if (display_p)
16255 #endif
16256 {
16257 xassert (face != NULL);
16258 PREPARE_FACE_FOR_DISPLAY (f, face);
16259 }
16260
16261 return face;
16262 }
16263
16264
16265 /* Set background width of glyph string S. START is the index of the
16266 first glyph following S. LAST_X is the right-most x-position + 1
16267 in the drawing area. */
16268
16269 static INLINE void
16270 set_glyph_string_background_width (s, start, last_x)
16271 struct glyph_string *s;
16272 int start;
16273 int last_x;
16274 {
16275 /* If the face of this glyph string has to be drawn to the end of
16276 the drawing area, set S->extends_to_end_of_line_p. */
16277 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
16278
16279 if (start == s->row->used[s->area]
16280 && s->area == TEXT_AREA
16281 && ((s->hl == DRAW_NORMAL_TEXT
16282 && (s->row->fill_line_p
16283 || s->face->background != default_face->background
16284 || s->face->stipple != default_face->stipple
16285 || s->row->mouse_face_p))
16286 || s->hl == DRAW_MOUSE_FACE
16287 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
16288 && s->row->fill_line_p)))
16289 s->extends_to_end_of_line_p = 1;
16290
16291 /* If S extends its face to the end of the line, set its
16292 background_width to the distance to the right edge of the drawing
16293 area. */
16294 if (s->extends_to_end_of_line_p)
16295 s->background_width = last_x - s->x + 1;
16296 else
16297 s->background_width = s->width;
16298 }
16299
16300
16301 /* Compute overhangs and x-positions for glyph string S and its
16302 predecessors, or successors. X is the starting x-position for S.
16303 BACKWARD_P non-zero means process predecessors. */
16304
16305 static void
16306 compute_overhangs_and_x (s, x, backward_p)
16307 struct glyph_string *s;
16308 int x;
16309 int backward_p;
16310 {
16311 if (backward_p)
16312 {
16313 while (s)
16314 {
16315 if (rif->compute_glyph_string_overhangs)
16316 rif->compute_glyph_string_overhangs (s);
16317 x -= s->width;
16318 s->x = x;
16319 s = s->prev;
16320 }
16321 }
16322 else
16323 {
16324 while (s)
16325 {
16326 if (rif->compute_glyph_string_overhangs)
16327 rif->compute_glyph_string_overhangs (s);
16328 s->x = x;
16329 x += s->width;
16330 s = s->next;
16331 }
16332 }
16333 }
16334
16335
16336
16337 /* The following macros are only called from x_draw_glyphs below.
16338 They reference the following parameters of that function directly:
16339 `w', `row', `area', and `overlap_p'
16340 as well as the following local variables:
16341 `s', `f', and `hdc' (in W32) */
16342
16343 #ifdef HAVE_NTGUI
16344 /* On W32, silently add local `hdc' variable to argument list of
16345 init_glyph_string. */
16346 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
16347 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
16348 #else
16349 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
16350 init_glyph_string (s, char2b, w, row, area, start, hl)
16351 #endif
16352
16353 /* Add a glyph string for a stretch glyph to the list of strings
16354 between HEAD and TAIL. START is the index of the stretch glyph in
16355 row area AREA of glyph row ROW. END is the index of the last glyph
16356 in that glyph row area. X is the current output position assigned
16357 to the new glyph string constructed. HL overrides that face of the
16358 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
16359 is the right-most x-position of the drawing area. */
16360
16361 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
16362 and below -- keep them on one line. */
16363 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
16364 do \
16365 { \
16366 s = (struct glyph_string *) alloca (sizeof *s); \
16367 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
16368 START = fill_stretch_glyph_string (s, row, area, START, END); \
16369 append_glyph_string (&HEAD, &TAIL, s); \
16370 s->x = (X); \
16371 } \
16372 while (0)
16373
16374
16375 /* Add a glyph string for an image glyph to the list of strings
16376 between HEAD and TAIL. START is the index of the image glyph in
16377 row area AREA of glyph row ROW. END is the index of the last glyph
16378 in that glyph row area. X is the current output position assigned
16379 to the new glyph string constructed. HL overrides that face of the
16380 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
16381 is the right-most x-position of the drawing area. */
16382
16383 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
16384 do \
16385 { \
16386 s = (struct glyph_string *) alloca (sizeof *s); \
16387 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
16388 fill_image_glyph_string (s); \
16389 append_glyph_string (&HEAD, &TAIL, s); \
16390 ++START; \
16391 s->x = (X); \
16392 } \
16393 while (0)
16394
16395
16396 /* Add a glyph string for a sequence of character glyphs to the list
16397 of strings between HEAD and TAIL. START is the index of the first
16398 glyph in row area AREA of glyph row ROW that is part of the new
16399 glyph string. END is the index of the last glyph in that glyph row
16400 area. X is the current output position assigned to the new glyph
16401 string constructed. HL overrides that face of the glyph; e.g. it
16402 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
16403 right-most x-position of the drawing area. */
16404
16405 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
16406 do \
16407 { \
16408 int c, face_id; \
16409 XChar2b *char2b; \
16410 \
16411 c = (row)->glyphs[area][START].u.ch; \
16412 face_id = (row)->glyphs[area][START].face_id; \
16413 \
16414 s = (struct glyph_string *) alloca (sizeof *s); \
16415 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
16416 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
16417 append_glyph_string (&HEAD, &TAIL, s); \
16418 s->x = (X); \
16419 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
16420 } \
16421 while (0)
16422
16423
16424 /* Add a glyph string for a composite sequence to the list of strings
16425 between HEAD and TAIL. START is the index of the first glyph in
16426 row area AREA of glyph row ROW that is part of the new glyph
16427 string. END is the index of the last glyph in that glyph row area.
16428 X is the current output position assigned to the new glyph string
16429 constructed. HL overrides that face of the glyph; e.g. it is
16430 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
16431 x-position of the drawing area. */
16432
16433 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
16434 do { \
16435 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
16436 int face_id = (row)->glyphs[area][START].face_id; \
16437 struct face *base_face = FACE_FROM_ID (f, face_id); \
16438 struct composition *cmp = composition_table[cmp_id]; \
16439 int glyph_len = cmp->glyph_len; \
16440 XChar2b *char2b; \
16441 struct face **faces; \
16442 struct glyph_string *first_s = NULL; \
16443 int n; \
16444 \
16445 base_face = base_face->ascii_face; \
16446 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
16447 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
16448 /* At first, fill in `char2b' and `faces'. */ \
16449 for (n = 0; n < glyph_len; n++) \
16450 { \
16451 int c = COMPOSITION_GLYPH (cmp, n); \
16452 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
16453 faces[n] = FACE_FROM_ID (f, this_face_id); \
16454 get_char_face_and_encoding (f, c, this_face_id, \
16455 char2b + n, 1, 1); \
16456 } \
16457 \
16458 /* Make glyph_strings for each glyph sequence that is drawable by \
16459 the same face, and append them to HEAD/TAIL. */ \
16460 for (n = 0; n < cmp->glyph_len;) \
16461 { \
16462 s = (struct glyph_string *) alloca (sizeof *s); \
16463 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
16464 append_glyph_string (&(HEAD), &(TAIL), s); \
16465 s->cmp = cmp; \
16466 s->gidx = n; \
16467 s->x = (X); \
16468 \
16469 if (n == 0) \
16470 first_s = s; \
16471 \
16472 n = fill_composite_glyph_string (s, faces, overlaps_p); \
16473 } \
16474 \
16475 ++START; \
16476 s = first_s; \
16477 } while (0)
16478
16479
16480 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
16481 of AREA of glyph row ROW on window W between indices START and END.
16482 HL overrides the face for drawing glyph strings, e.g. it is
16483 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
16484 x-positions of the drawing area.
16485
16486 This is an ugly monster macro construct because we must use alloca
16487 to allocate glyph strings (because x_draw_glyphs can be called
16488 asynchronously). */
16489
16490 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
16491 do \
16492 { \
16493 HEAD = TAIL = NULL; \
16494 while (START < END) \
16495 { \
16496 struct glyph *first_glyph = (row)->glyphs[area] + START; \
16497 switch (first_glyph->type) \
16498 { \
16499 case CHAR_GLYPH: \
16500 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
16501 HL, X, LAST_X); \
16502 break; \
16503 \
16504 case COMPOSITE_GLYPH: \
16505 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
16506 HL, X, LAST_X); \
16507 break; \
16508 \
16509 case STRETCH_GLYPH: \
16510 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
16511 HL, X, LAST_X); \
16512 break; \
16513 \
16514 case IMAGE_GLYPH: \
16515 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
16516 HL, X, LAST_X); \
16517 break; \
16518 \
16519 default: \
16520 abort (); \
16521 } \
16522 \
16523 set_glyph_string_background_width (s, START, LAST_X); \
16524 (X) += s->width; \
16525 } \
16526 } \
16527 while (0)
16528
16529
16530 /* Draw glyphs between START and END in AREA of ROW on window W,
16531 starting at x-position X. X is relative to AREA in W. HL is a
16532 face-override with the following meaning:
16533
16534 DRAW_NORMAL_TEXT draw normally
16535 DRAW_CURSOR draw in cursor face
16536 DRAW_MOUSE_FACE draw in mouse face.
16537 DRAW_INVERSE_VIDEO draw in mode line face
16538 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
16539 DRAW_IMAGE_RAISED draw an image with a raised relief around it
16540
16541 If OVERLAPS_P is non-zero, draw only the foreground of characters
16542 and clip to the physical height of ROW.
16543
16544 Value is the x-position reached, relative to AREA of W. */
16545
16546 int
16547 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
16548 struct window *w;
16549 int x;
16550 struct glyph_row *row;
16551 enum glyph_row_area area;
16552 int start, end;
16553 enum draw_glyphs_face hl;
16554 int overlaps_p;
16555 {
16556 struct glyph_string *head, *tail;
16557 struct glyph_string *s;
16558 int last_x, area_width;
16559 int x_reached;
16560 int i, j;
16561 struct frame *f = XFRAME (WINDOW_FRAME (w));
16562 DECLARE_HDC (hdc);
16563
16564 ALLOCATE_HDC (hdc, f);
16565
16566 /* Let's rather be paranoid than getting a SEGV. */
16567 end = min (end, row->used[area]);
16568 start = max (0, start);
16569 start = min (end, start);
16570
16571 /* Translate X to frame coordinates. Set last_x to the right
16572 end of the drawing area. */
16573 if (row->full_width_p)
16574 {
16575 /* X is relative to the left edge of W, without scroll bars
16576 or fringes. */
16577 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
16578
16579 x += window_left_x;
16580 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
16581 last_x = window_left_x + area_width;
16582
16583 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
16584 {
16585 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
16586 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
16587 last_x += width;
16588 else
16589 x -= width;
16590 }
16591
16592 x += FRAME_INTERNAL_BORDER_WIDTH (f);
16593 /* ++KFS: W32 and MAC versions had -= in next line (bug??) */
16594 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
16595 }
16596 else
16597 {
16598 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
16599 area_width = window_box_width (w, area);
16600 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
16601 }
16602
16603 /* Build a doubly-linked list of glyph_string structures between
16604 head and tail from what we have to draw. Note that the macro
16605 BUILD_GLYPH_STRINGS will modify its start parameter. That's
16606 the reason we use a separate variable `i'. */
16607 i = start;
16608 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
16609 if (tail)
16610 x_reached = tail->x + tail->background_width;
16611 else
16612 x_reached = x;
16613
16614 /* If there are any glyphs with lbearing < 0 or rbearing > width in
16615 the row, redraw some glyphs in front or following the glyph
16616 strings built above. */
16617 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
16618 {
16619 int dummy_x = 0;
16620 struct glyph_string *h, *t;
16621
16622 /* Compute overhangs for all glyph strings. */
16623 if (rif->compute_glyph_string_overhangs)
16624 for (s = head; s; s = s->next)
16625 rif->compute_glyph_string_overhangs (s);
16626
16627 /* Prepend glyph strings for glyphs in front of the first glyph
16628 string that are overwritten because of the first glyph
16629 string's left overhang. The background of all strings
16630 prepended must be drawn because the first glyph string
16631 draws over it. */
16632 i = left_overwritten (head);
16633 if (i >= 0)
16634 {
16635 j = i;
16636 BUILD_GLYPH_STRINGS (j, start, h, t,
16637 DRAW_NORMAL_TEXT, dummy_x, last_x);
16638 start = i;
16639 compute_overhangs_and_x (t, head->x, 1);
16640 prepend_glyph_string_lists (&head, &tail, h, t);
16641 }
16642
16643 /* Prepend glyph strings for glyphs in front of the first glyph
16644 string that overwrite that glyph string because of their
16645 right overhang. For these strings, only the foreground must
16646 be drawn, because it draws over the glyph string at `head'.
16647 The background must not be drawn because this would overwrite
16648 right overhangs of preceding glyphs for which no glyph
16649 strings exist. */
16650 i = left_overwriting (head);
16651 if (i >= 0)
16652 {
16653 BUILD_GLYPH_STRINGS (i, start, h, t,
16654 DRAW_NORMAL_TEXT, dummy_x, last_x);
16655 for (s = h; s; s = s->next)
16656 s->background_filled_p = 1;
16657 compute_overhangs_and_x (t, head->x, 1);
16658 prepend_glyph_string_lists (&head, &tail, h, t);
16659 }
16660
16661 /* Append glyphs strings for glyphs following the last glyph
16662 string tail that are overwritten by tail. The background of
16663 these strings has to be drawn because tail's foreground draws
16664 over it. */
16665 i = right_overwritten (tail);
16666 if (i >= 0)
16667 {
16668 BUILD_GLYPH_STRINGS (end, i, h, t,
16669 DRAW_NORMAL_TEXT, x, last_x);
16670 compute_overhangs_and_x (h, tail->x + tail->width, 0);
16671 append_glyph_string_lists (&head, &tail, h, t);
16672 }
16673
16674 /* Append glyph strings for glyphs following the last glyph
16675 string tail that overwrite tail. The foreground of such
16676 glyphs has to be drawn because it writes into the background
16677 of tail. The background must not be drawn because it could
16678 paint over the foreground of following glyphs. */
16679 i = right_overwriting (tail);
16680 if (i >= 0)
16681 {
16682 BUILD_GLYPH_STRINGS (end, i, h, t,
16683 DRAW_NORMAL_TEXT, x, last_x);
16684 for (s = h; s; s = s->next)
16685 s->background_filled_p = 1;
16686 compute_overhangs_and_x (h, tail->x + tail->width, 0);
16687 append_glyph_string_lists (&head, &tail, h, t);
16688 }
16689 }
16690
16691 /* Draw all strings. */
16692 for (s = head; s; s = s->next)
16693 rif->draw_glyph_string (s);
16694
16695 if (area == TEXT_AREA
16696 && !row->full_width_p
16697 /* When drawing overlapping rows, only the glyph strings'
16698 foreground is drawn, which doesn't erase a cursor
16699 completely. */
16700 && !overlaps_p)
16701 {
16702 int x0 = head ? head->x : x;
16703 int x1 = tail ? tail->x + tail->background_width : x;
16704
16705 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
16706 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
16707
16708 /* ++KFS: W32 and MAC versions had following test here:
16709 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
16710 */
16711
16712 if (XFASTINT (w->left_margin_width) != 0)
16713 {
16714 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
16715 x0 -= left_area_width;
16716 x1 -= left_area_width;
16717 }
16718
16719 notice_overwritten_cursor (w, area, x0, x1,
16720 row->y, MATRIX_ROW_BOTTOM_Y (row));
16721 }
16722
16723 /* Value is the x-position up to which drawn, relative to AREA of W.
16724 This doesn't include parts drawn because of overhangs. */
16725 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
16726 if (!row->full_width_p)
16727 {
16728 /* ++KFS: W32 and MAC versions only had this test here:
16729 if (area > LEFT_MARGIN_AREA)
16730 */
16731
16732 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
16733 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
16734 if (area > TEXT_AREA)
16735 x_reached -= window_box_width (w, TEXT_AREA);
16736 }
16737
16738 RELEASE_HDC (hdc, f);
16739
16740 return x_reached;
16741 }
16742
16743
16744 /* Store one glyph for IT->char_to_display in IT->glyph_row.
16745 Called from x_produce_glyphs when IT->glyph_row is non-null. */
16746
16747 static INLINE void
16748 append_glyph (it)
16749 struct it *it;
16750 {
16751 struct glyph *glyph;
16752 enum glyph_row_area area = it->area;
16753
16754 xassert (it->glyph_row);
16755 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
16756
16757 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16758 if (glyph < it->glyph_row->glyphs[area + 1])
16759 {
16760 glyph->charpos = CHARPOS (it->position);
16761 glyph->object = it->object;
16762 glyph->pixel_width = it->pixel_width;
16763 glyph->voffset = it->voffset;
16764 glyph->type = CHAR_GLYPH;
16765 glyph->multibyte_p = it->multibyte_p;
16766 glyph->left_box_line_p = it->start_of_box_run_p;
16767 glyph->right_box_line_p = it->end_of_box_run_p;
16768 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
16769 || it->phys_descent > it->descent);
16770 glyph->padding_p = 0;
16771 glyph->glyph_not_available_p = it->glyph_not_available_p;
16772 glyph->face_id = it->face_id;
16773 glyph->u.ch = it->char_to_display;
16774 glyph->font_type = FONT_TYPE_UNKNOWN;
16775 ++it->glyph_row->used[area];
16776 }
16777 }
16778
16779 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
16780 Called from x_produce_glyphs when IT->glyph_row is non-null. */
16781
16782 static INLINE void
16783 append_composite_glyph (it)
16784 struct it *it;
16785 {
16786 struct glyph *glyph;
16787 enum glyph_row_area area = it->area;
16788
16789 xassert (it->glyph_row);
16790
16791 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16792 if (glyph < it->glyph_row->glyphs[area + 1])
16793 {
16794 glyph->charpos = CHARPOS (it->position);
16795 glyph->object = it->object;
16796 glyph->pixel_width = it->pixel_width;
16797 glyph->voffset = it->voffset;
16798 glyph->type = COMPOSITE_GLYPH;
16799 glyph->multibyte_p = it->multibyte_p;
16800 glyph->left_box_line_p = it->start_of_box_run_p;
16801 glyph->right_box_line_p = it->end_of_box_run_p;
16802 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
16803 || it->phys_descent > it->descent);
16804 glyph->padding_p = 0;
16805 glyph->glyph_not_available_p = 0;
16806 glyph->face_id = it->face_id;
16807 glyph->u.cmp_id = it->cmp_id;
16808 glyph->font_type = FONT_TYPE_UNKNOWN;
16809 ++it->glyph_row->used[area];
16810 }
16811 }
16812
16813
16814 /* Change IT->ascent and IT->height according to the setting of
16815 IT->voffset. */
16816
16817 static INLINE void
16818 take_vertical_position_into_account (it)
16819 struct it *it;
16820 {
16821 if (it->voffset)
16822 {
16823 if (it->voffset < 0)
16824 /* Increase the ascent so that we can display the text higher
16825 in the line. */
16826 it->ascent += abs (it->voffset);
16827 else
16828 /* Increase the descent so that we can display the text lower
16829 in the line. */
16830 it->descent += it->voffset;
16831 }
16832 }
16833
16834
16835 /* Produce glyphs/get display metrics for the image IT is loaded with.
16836 See the description of struct display_iterator in dispextern.h for
16837 an overview of struct display_iterator. */
16838
16839 static void
16840 produce_image_glyph (it)
16841 struct it *it;
16842 {
16843 struct image *img;
16844 struct face *face;
16845
16846 xassert (it->what == IT_IMAGE);
16847
16848 face = FACE_FROM_ID (it->f, it->face_id);
16849 img = IMAGE_FROM_ID (it->f, it->image_id);
16850 xassert (img);
16851
16852 /* Make sure X resources of the face and image are loaded. */
16853 PREPARE_FACE_FOR_DISPLAY (it->f, face);
16854 prepare_image_for_display (it->f, img);
16855
16856 it->ascent = it->phys_ascent = image_ascent (img, face);
16857 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
16858 it->pixel_width = img->width + 2 * img->hmargin;
16859
16860 it->nglyphs = 1;
16861
16862 if (face->box != FACE_NO_BOX)
16863 {
16864 if (face->box_line_width > 0)
16865 {
16866 it->ascent += face->box_line_width;
16867 it->descent += face->box_line_width;
16868 }
16869
16870 if (it->start_of_box_run_p)
16871 it->pixel_width += abs (face->box_line_width);
16872 if (it->end_of_box_run_p)
16873 it->pixel_width += abs (face->box_line_width);
16874 }
16875
16876 take_vertical_position_into_account (it);
16877
16878 if (it->glyph_row)
16879 {
16880 struct glyph *glyph;
16881 enum glyph_row_area area = it->area;
16882
16883 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16884 if (glyph < it->glyph_row->glyphs[area + 1])
16885 {
16886 glyph->charpos = CHARPOS (it->position);
16887 glyph->object = it->object;
16888 glyph->pixel_width = it->pixel_width;
16889 glyph->voffset = it->voffset;
16890 glyph->type = IMAGE_GLYPH;
16891 glyph->multibyte_p = it->multibyte_p;
16892 glyph->left_box_line_p = it->start_of_box_run_p;
16893 glyph->right_box_line_p = it->end_of_box_run_p;
16894 glyph->overlaps_vertically_p = 0;
16895 glyph->padding_p = 0;
16896 glyph->glyph_not_available_p = 0;
16897 glyph->face_id = it->face_id;
16898 glyph->u.img_id = img->id;
16899 glyph->font_type = FONT_TYPE_UNKNOWN;
16900 ++it->glyph_row->used[area];
16901 }
16902 }
16903 }
16904
16905
16906 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
16907 of the glyph, WIDTH and HEIGHT are the width and height of the
16908 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
16909 ascent of the glyph (0 <= ASCENT <= 1). */
16910
16911 static void
16912 append_stretch_glyph (it, object, width, height, ascent)
16913 struct it *it;
16914 Lisp_Object object;
16915 int width, height;
16916 double ascent;
16917 {
16918 struct glyph *glyph;
16919 enum glyph_row_area area = it->area;
16920
16921 xassert (ascent >= 0 && ascent <= 1);
16922
16923 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
16924 if (glyph < it->glyph_row->glyphs[area + 1])
16925 {
16926 glyph->charpos = CHARPOS (it->position);
16927 glyph->object = object;
16928 glyph->pixel_width = width;
16929 glyph->voffset = it->voffset;
16930 glyph->type = STRETCH_GLYPH;
16931 glyph->multibyte_p = it->multibyte_p;
16932 glyph->left_box_line_p = it->start_of_box_run_p;
16933 glyph->right_box_line_p = it->end_of_box_run_p;
16934 glyph->overlaps_vertically_p = 0;
16935 glyph->padding_p = 0;
16936 glyph->glyph_not_available_p = 0;
16937 glyph->face_id = it->face_id;
16938 glyph->u.stretch.ascent = height * ascent;
16939 glyph->u.stretch.height = height;
16940 glyph->font_type = FONT_TYPE_UNKNOWN;
16941 ++it->glyph_row->used[area];
16942 }
16943 }
16944
16945
16946 /* Produce a stretch glyph for iterator IT. IT->object is the value
16947 of the glyph property displayed. The value must be a list
16948 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
16949 being recognized:
16950
16951 1. `:width WIDTH' specifies that the space should be WIDTH *
16952 canonical char width wide. WIDTH may be an integer or floating
16953 point number.
16954
16955 2. `:relative-width FACTOR' specifies that the width of the stretch
16956 should be computed from the width of the first character having the
16957 `glyph' property, and should be FACTOR times that width.
16958
16959 3. `:align-to HPOS' specifies that the space should be wide enough
16960 to reach HPOS, a value in canonical character units.
16961
16962 Exactly one of the above pairs must be present.
16963
16964 4. `:height HEIGHT' specifies that the height of the stretch produced
16965 should be HEIGHT, measured in canonical character units.
16966
16967 5. `:relative-height FACTOR' specifies that the height of the
16968 stretch should be FACTOR times the height of the characters having
16969 the glyph property.
16970
16971 Either none or exactly one of 4 or 5 must be present.
16972
16973 6. `:ascent ASCENT' specifies that ASCENT percent of the height
16974 of the stretch should be used for the ascent of the stretch.
16975 ASCENT must be in the range 0 <= ASCENT <= 100. */
16976
16977 #define NUMVAL(X) \
16978 ((INTEGERP (X) || FLOATP (X)) \
16979 ? XFLOATINT (X) \
16980 : - 1)
16981
16982
16983 static void
16984 produce_stretch_glyph (it)
16985 struct it *it;
16986 {
16987 /* (space :width WIDTH :height HEIGHT. */
16988 Lisp_Object prop, plist;
16989 int width = 0, height = 0;
16990 double ascent = 0;
16991 struct face *face = FACE_FROM_ID (it->f, it->face_id);
16992 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
16993
16994 PREPARE_FACE_FOR_DISPLAY (it->f, face);
16995
16996 /* List should start with `space'. */
16997 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
16998 plist = XCDR (it->object);
16999
17000 /* Compute the width of the stretch. */
17001 if (prop = Fplist_get (plist, QCwidth),
17002 NUMVAL (prop) > 0)
17003 /* Absolute width `:width WIDTH' specified and valid. */
17004 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
17005 else if (prop = Fplist_get (plist, QCrelative_width),
17006 NUMVAL (prop) > 0)
17007 {
17008 /* Relative width `:relative-width FACTOR' specified and valid.
17009 Compute the width of the characters having the `glyph'
17010 property. */
17011 struct it it2;
17012 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17013
17014 it2 = *it;
17015 if (it->multibyte_p)
17016 {
17017 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17018 - IT_BYTEPOS (*it));
17019 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17020 }
17021 else
17022 it2.c = *p, it2.len = 1;
17023
17024 it2.glyph_row = NULL;
17025 it2.what = IT_CHARACTER;
17026 x_produce_glyphs (&it2);
17027 width = NUMVAL (prop) * it2.pixel_width;
17028 }
17029 else if (prop = Fplist_get (plist, QCalign_to),
17030 NUMVAL (prop) > 0)
17031 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
17032 else
17033 /* Nothing specified -> width defaults to canonical char width. */
17034 width = CANON_X_UNIT (it->f);
17035
17036 /* Compute height. */
17037 if (prop = Fplist_get (plist, QCheight),
17038 NUMVAL (prop) > 0)
17039 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
17040 else if (prop = Fplist_get (plist, QCrelative_height),
17041 NUMVAL (prop) > 0)
17042 height = FONT_HEIGHT (font) * NUMVAL (prop);
17043 else
17044 height = FONT_HEIGHT (font);
17045
17046 /* Compute percentage of height used for ascent. If
17047 `:ascent ASCENT' is present and valid, use that. Otherwise,
17048 derive the ascent from the font in use. */
17049 if (prop = Fplist_get (plist, QCascent),
17050 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17051 ascent = NUMVAL (prop) / 100.0;
17052 else
17053 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17054
17055 if (width <= 0)
17056 width = 1;
17057 if (height <= 0)
17058 height = 1;
17059
17060 if (it->glyph_row)
17061 {
17062 Lisp_Object object = it->stack[it->sp - 1].string;
17063 if (!STRINGP (object))
17064 object = it->w->buffer;
17065 append_stretch_glyph (it, object, width, height, ascent);
17066 }
17067
17068 it->pixel_width = width;
17069 it->ascent = it->phys_ascent = height * ascent;
17070 it->descent = it->phys_descent = height - it->ascent;
17071 it->nglyphs = 1;
17072
17073 if (face->box != FACE_NO_BOX)
17074 {
17075 if (face->box_line_width > 0)
17076 {
17077 it->ascent += face->box_line_width;
17078 it->descent += face->box_line_width;
17079 }
17080
17081 if (it->start_of_box_run_p)
17082 it->pixel_width += abs (face->box_line_width);
17083 if (it->end_of_box_run_p)
17084 it->pixel_width += abs (face->box_line_width);
17085 }
17086
17087 take_vertical_position_into_account (it);
17088 }
17089
17090 /* RIF:
17091 Produce glyphs/get display metrics for the display element IT is
17092 loaded with. See the description of struct display_iterator in
17093 dispextern.h for an overview of struct display_iterator. */
17094
17095 void
17096 x_produce_glyphs (it)
17097 struct it *it;
17098 {
17099 it->glyph_not_available_p = 0;
17100
17101 if (it->what == IT_CHARACTER)
17102 {
17103 XChar2b char2b;
17104 XFontStruct *font;
17105 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17106 XCharStruct *pcm;
17107 int font_not_found_p;
17108 struct font_info *font_info;
17109 int boff; /* baseline offset */
17110 /* We may change it->multibyte_p upon unibyte<->multibyte
17111 conversion. So, save the current value now and restore it
17112 later.
17113
17114 Note: It seems that we don't have to record multibyte_p in
17115 struct glyph because the character code itself tells if or
17116 not the character is multibyte. Thus, in the future, we must
17117 consider eliminating the field `multibyte_p' in the struct
17118 glyph. */
17119 int saved_multibyte_p = it->multibyte_p;
17120
17121 /* Maybe translate single-byte characters to multibyte, or the
17122 other way. */
17123 it->char_to_display = it->c;
17124 if (!ASCII_BYTE_P (it->c))
17125 {
17126 if (unibyte_display_via_language_environment
17127 && SINGLE_BYTE_CHAR_P (it->c)
17128 && (it->c >= 0240
17129 || !NILP (Vnonascii_translation_table)))
17130 {
17131 it->char_to_display = unibyte_char_to_multibyte (it->c);
17132 it->multibyte_p = 1;
17133 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17134 face = FACE_FROM_ID (it->f, it->face_id);
17135 }
17136 else if (!SINGLE_BYTE_CHAR_P (it->c)
17137 && !it->multibyte_p)
17138 {
17139 it->multibyte_p = 1;
17140 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17141 face = FACE_FROM_ID (it->f, it->face_id);
17142 }
17143 }
17144
17145 /* Get font to use. Encode IT->char_to_display. */
17146 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17147 &char2b, it->multibyte_p, 0);
17148 font = face->font;
17149
17150 /* When no suitable font found, use the default font. */
17151 font_not_found_p = font == NULL;
17152 if (font_not_found_p)
17153 {
17154 font = FRAME_FONT (it->f);
17155 boff = FRAME_BASELINE_OFFSET (it->f);
17156 font_info = NULL;
17157 }
17158 else
17159 {
17160 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17161 boff = font_info->baseline_offset;
17162 if (font_info->vertical_centering)
17163 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17164 }
17165
17166 if (it->char_to_display >= ' '
17167 && (!it->multibyte_p || it->char_to_display < 128))
17168 {
17169 /* Either unibyte or ASCII. */
17170 int stretched_p;
17171
17172 it->nglyphs = 1;
17173
17174 pcm = rif->per_char_metric (font, &char2b,
17175 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17176 it->ascent = FONT_BASE (font) + boff;
17177 it->descent = FONT_DESCENT (font) - boff;
17178
17179 if (pcm)
17180 {
17181 it->phys_ascent = pcm->ascent + boff;
17182 it->phys_descent = pcm->descent - boff;
17183 it->pixel_width = pcm->width;
17184 }
17185 else
17186 {
17187 it->glyph_not_available_p = 1;
17188 it->phys_ascent = FONT_BASE (font) + boff;
17189 it->phys_descent = FONT_DESCENT (font) - boff;
17190 it->pixel_width = FONT_WIDTH (font);
17191 }
17192
17193 /* If this is a space inside a region of text with
17194 `space-width' property, change its width. */
17195 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17196 if (stretched_p)
17197 it->pixel_width *= XFLOATINT (it->space_width);
17198
17199 /* If face has a box, add the box thickness to the character
17200 height. If character has a box line to the left and/or
17201 right, add the box line width to the character's width. */
17202 if (face->box != FACE_NO_BOX)
17203 {
17204 int thick = face->box_line_width;
17205
17206 if (thick > 0)
17207 {
17208 it->ascent += thick;
17209 it->descent += thick;
17210 }
17211 else
17212 thick = -thick;
17213
17214 if (it->start_of_box_run_p)
17215 it->pixel_width += thick;
17216 if (it->end_of_box_run_p)
17217 it->pixel_width += thick;
17218 }
17219
17220 /* If face has an overline, add the height of the overline
17221 (1 pixel) and a 1 pixel margin to the character height. */
17222 if (face->overline_p)
17223 it->ascent += 2;
17224
17225 take_vertical_position_into_account (it);
17226
17227 /* If we have to actually produce glyphs, do it. */
17228 if (it->glyph_row)
17229 {
17230 if (stretched_p)
17231 {
17232 /* Translate a space with a `space-width' property
17233 into a stretch glyph. */
17234 double ascent = (double) FONT_BASE (font)
17235 / FONT_HEIGHT (font);
17236 append_stretch_glyph (it, it->object, it->pixel_width,
17237 it->ascent + it->descent, ascent);
17238 }
17239 else
17240 append_glyph (it);
17241
17242 /* If characters with lbearing or rbearing are displayed
17243 in this line, record that fact in a flag of the
17244 glyph row. This is used to optimize X output code. */
17245 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17246 it->glyph_row->contains_overlapping_glyphs_p = 1;
17247 }
17248 }
17249 else if (it->char_to_display == '\n')
17250 {
17251 /* A newline has no width but we need the height of the line. */
17252 it->pixel_width = 0;
17253 it->nglyphs = 0;
17254 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17255 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17256
17257 if (face->box != FACE_NO_BOX
17258 && face->box_line_width > 0)
17259 {
17260 it->ascent += face->box_line_width;
17261 it->descent += face->box_line_width;
17262 }
17263 }
17264 else if (it->char_to_display == '\t')
17265 {
17266 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
17267 int x = it->current_x + it->continuation_lines_width;
17268 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17269
17270 /* If the distance from the current position to the next tab
17271 stop is less than a canonical character width, use the
17272 tab stop after that. */
17273 if (next_tab_x - x < CANON_X_UNIT (it->f))
17274 next_tab_x += tab_width;
17275
17276 it->pixel_width = next_tab_x - x;
17277 it->nglyphs = 1;
17278 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17279 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17280
17281 if (it->glyph_row)
17282 {
17283 double ascent = (double) it->ascent / (it->ascent + it->descent);
17284 append_stretch_glyph (it, it->object, it->pixel_width,
17285 it->ascent + it->descent, ascent);
17286 }
17287 }
17288 else
17289 {
17290 /* A multi-byte character. Assume that the display width of the
17291 character is the width of the character multiplied by the
17292 width of the font. */
17293
17294 /* If we found a font, this font should give us the right
17295 metrics. If we didn't find a font, use the frame's
17296 default font and calculate the width of the character
17297 from the charset width; this is what old redisplay code
17298 did. */
17299
17300 pcm = rif->per_char_metric (font, &char2b,
17301 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
17302
17303 if (font_not_found_p || !pcm)
17304 {
17305 int charset = CHAR_CHARSET (it->char_to_display);
17306
17307 it->glyph_not_available_p = 1;
17308 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
17309 * CHARSET_WIDTH (charset));
17310 it->phys_ascent = FONT_BASE (font) + boff;
17311 it->phys_descent = FONT_DESCENT (font) - boff;
17312 }
17313 else
17314 {
17315 it->pixel_width = pcm->width;
17316 it->phys_ascent = pcm->ascent + boff;
17317 it->phys_descent = pcm->descent - boff;
17318 if (it->glyph_row
17319 && (pcm->lbearing < 0
17320 || pcm->rbearing > pcm->width))
17321 it->glyph_row->contains_overlapping_glyphs_p = 1;
17322 }
17323 it->nglyphs = 1;
17324 it->ascent = FONT_BASE (font) + boff;
17325 it->descent = FONT_DESCENT (font) - boff;
17326 if (face->box != FACE_NO_BOX)
17327 {
17328 int thick = face->box_line_width;
17329
17330 if (thick > 0)
17331 {
17332 it->ascent += thick;
17333 it->descent += thick;
17334 }
17335 else
17336 thick = - thick;
17337
17338 if (it->start_of_box_run_p)
17339 it->pixel_width += thick;
17340 if (it->end_of_box_run_p)
17341 it->pixel_width += thick;
17342 }
17343
17344 /* If face has an overline, add the height of the overline
17345 (1 pixel) and a 1 pixel margin to the character height. */
17346 if (face->overline_p)
17347 it->ascent += 2;
17348
17349 take_vertical_position_into_account (it);
17350
17351 if (it->glyph_row)
17352 append_glyph (it);
17353 }
17354 it->multibyte_p = saved_multibyte_p;
17355 }
17356 else if (it->what == IT_COMPOSITION)
17357 {
17358 /* Note: A composition is represented as one glyph in the
17359 glyph matrix. There are no padding glyphs. */
17360 XChar2b char2b;
17361 XFontStruct *font;
17362 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17363 XCharStruct *pcm;
17364 int font_not_found_p;
17365 struct font_info *font_info;
17366 int boff; /* baseline offset */
17367 struct composition *cmp = composition_table[it->cmp_id];
17368
17369 /* Maybe translate single-byte characters to multibyte. */
17370 it->char_to_display = it->c;
17371 if (unibyte_display_via_language_environment
17372 && SINGLE_BYTE_CHAR_P (it->c)
17373 && (it->c >= 0240
17374 || (it->c >= 0200
17375 && !NILP (Vnonascii_translation_table))))
17376 {
17377 it->char_to_display = unibyte_char_to_multibyte (it->c);
17378 }
17379
17380 /* Get face and font to use. Encode IT->char_to_display. */
17381 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17382 face = FACE_FROM_ID (it->f, it->face_id);
17383 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17384 &char2b, it->multibyte_p, 0);
17385 font = face->font;
17386
17387 /* When no suitable font found, use the default font. */
17388 font_not_found_p = font == NULL;
17389 if (font_not_found_p)
17390 {
17391 font = FRAME_FONT (it->f);
17392 boff = FRAME_BASELINE_OFFSET (it->f);
17393 font_info = NULL;
17394 }
17395 else
17396 {
17397 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17398 boff = font_info->baseline_offset;
17399 if (font_info->vertical_centering)
17400 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17401 }
17402
17403 /* There are no padding glyphs, so there is only one glyph to
17404 produce for the composition. Important is that pixel_width,
17405 ascent and descent are the values of what is drawn by
17406 draw_glyphs (i.e. the values of the overall glyphs composed). */
17407 it->nglyphs = 1;
17408
17409 /* If we have not yet calculated pixel size data of glyphs of
17410 the composition for the current face font, calculate them
17411 now. Theoretically, we have to check all fonts for the
17412 glyphs, but that requires much time and memory space. So,
17413 here we check only the font of the first glyph. This leads
17414 to incorrect display very rarely, and C-l (recenter) can
17415 correct the display anyway. */
17416 if (cmp->font != (void *) font)
17417 {
17418 /* Ascent and descent of the font of the first character of
17419 this composition (adjusted by baseline offset). Ascent
17420 and descent of overall glyphs should not be less than
17421 them respectively. */
17422 int font_ascent = FONT_BASE (font) + boff;
17423 int font_descent = FONT_DESCENT (font) - boff;
17424 /* Bounding box of the overall glyphs. */
17425 int leftmost, rightmost, lowest, highest;
17426 int i, width, ascent, descent;
17427
17428 cmp->font = (void *) font;
17429
17430 /* Initialize the bounding box. */
17431 if (font_info
17432 && (pcm = rif->per_char_metric (font, &char2b,
17433 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
17434 {
17435 width = pcm->width;
17436 ascent = pcm->ascent;
17437 descent = pcm->descent;
17438 }
17439 else
17440 {
17441 width = FONT_WIDTH (font);
17442 ascent = FONT_BASE (font);
17443 descent = FONT_DESCENT (font);
17444 }
17445
17446 rightmost = width;
17447 lowest = - descent + boff;
17448 highest = ascent + boff;
17449 leftmost = 0;
17450
17451 if (font_info
17452 && font_info->default_ascent
17453 && CHAR_TABLE_P (Vuse_default_ascent)
17454 && !NILP (Faref (Vuse_default_ascent,
17455 make_number (it->char_to_display))))
17456 highest = font_info->default_ascent + boff;
17457
17458 /* Draw the first glyph at the normal position. It may be
17459 shifted to right later if some other glyphs are drawn at
17460 the left. */
17461 cmp->offsets[0] = 0;
17462 cmp->offsets[1] = boff;
17463
17464 /* Set cmp->offsets for the remaining glyphs. */
17465 for (i = 1; i < cmp->glyph_len; i++)
17466 {
17467 int left, right, btm, top;
17468 int ch = COMPOSITION_GLYPH (cmp, i);
17469 int face_id = FACE_FOR_CHAR (it->f, face, ch);
17470
17471 face = FACE_FROM_ID (it->f, face_id);
17472 get_char_face_and_encoding (it->f, ch, face->id,
17473 &char2b, it->multibyte_p, 0);
17474 font = face->font;
17475 if (font == NULL)
17476 {
17477 font = FRAME_FONT (it->f);
17478 boff = FRAME_BASELINE_OFFSET (it->f);
17479 font_info = NULL;
17480 }
17481 else
17482 {
17483 font_info
17484 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17485 boff = font_info->baseline_offset;
17486 if (font_info->vertical_centering)
17487 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17488 }
17489
17490 if (font_info
17491 && (pcm = rif->per_char_metric (font, &char2b,
17492 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
17493 {
17494 width = pcm->width;
17495 ascent = pcm->ascent;
17496 descent = pcm->descent;
17497 }
17498 else
17499 {
17500 width = FONT_WIDTH (font);
17501 ascent = 1;
17502 descent = 0;
17503 }
17504
17505 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
17506 {
17507 /* Relative composition with or without
17508 alternate chars. */
17509 left = (leftmost + rightmost - width) / 2;
17510 btm = - descent + boff;
17511 if (font_info && font_info->relative_compose
17512 && (! CHAR_TABLE_P (Vignore_relative_composition)
17513 || NILP (Faref (Vignore_relative_composition,
17514 make_number (ch)))))
17515 {
17516
17517 if (- descent >= font_info->relative_compose)
17518 /* One extra pixel between two glyphs. */
17519 btm = highest + 1;
17520 else if (ascent <= 0)
17521 /* One extra pixel between two glyphs. */
17522 btm = lowest - 1 - ascent - descent;
17523 }
17524 }
17525 else
17526 {
17527 /* A composition rule is specified by an integer
17528 value that encodes global and new reference
17529 points (GREF and NREF). GREF and NREF are
17530 specified by numbers as below:
17531
17532 0---1---2 -- ascent
17533 | |
17534 | |
17535 | |
17536 9--10--11 -- center
17537 | |
17538 ---3---4---5--- baseline
17539 | |
17540 6---7---8 -- descent
17541 */
17542 int rule = COMPOSITION_RULE (cmp, i);
17543 int gref, nref, grefx, grefy, nrefx, nrefy;
17544
17545 COMPOSITION_DECODE_RULE (rule, gref, nref);
17546 grefx = gref % 3, nrefx = nref % 3;
17547 grefy = gref / 3, nrefy = nref / 3;
17548
17549 left = (leftmost
17550 + grefx * (rightmost - leftmost) / 2
17551 - nrefx * width / 2);
17552 btm = ((grefy == 0 ? highest
17553 : grefy == 1 ? 0
17554 : grefy == 2 ? lowest
17555 : (highest + lowest) / 2)
17556 - (nrefy == 0 ? ascent + descent
17557 : nrefy == 1 ? descent - boff
17558 : nrefy == 2 ? 0
17559 : (ascent + descent) / 2));
17560 }
17561
17562 cmp->offsets[i * 2] = left;
17563 cmp->offsets[i * 2 + 1] = btm + descent;
17564
17565 /* Update the bounding box of the overall glyphs. */
17566 right = left + width;
17567 top = btm + descent + ascent;
17568 if (left < leftmost)
17569 leftmost = left;
17570 if (right > rightmost)
17571 rightmost = right;
17572 if (top > highest)
17573 highest = top;
17574 if (btm < lowest)
17575 lowest = btm;
17576 }
17577
17578 /* If there are glyphs whose x-offsets are negative,
17579 shift all glyphs to the right and make all x-offsets
17580 non-negative. */
17581 if (leftmost < 0)
17582 {
17583 for (i = 0; i < cmp->glyph_len; i++)
17584 cmp->offsets[i * 2] -= leftmost;
17585 rightmost -= leftmost;
17586 }
17587
17588 cmp->pixel_width = rightmost;
17589 cmp->ascent = highest;
17590 cmp->descent = - lowest;
17591 if (cmp->ascent < font_ascent)
17592 cmp->ascent = font_ascent;
17593 if (cmp->descent < font_descent)
17594 cmp->descent = font_descent;
17595 }
17596
17597 it->pixel_width = cmp->pixel_width;
17598 it->ascent = it->phys_ascent = cmp->ascent;
17599 it->descent = it->phys_descent = cmp->descent;
17600
17601 if (face->box != FACE_NO_BOX)
17602 {
17603 int thick = face->box_line_width;
17604
17605 if (thick > 0)
17606 {
17607 it->ascent += thick;
17608 it->descent += thick;
17609 }
17610 else
17611 thick = - thick;
17612
17613 if (it->start_of_box_run_p)
17614 it->pixel_width += thick;
17615 if (it->end_of_box_run_p)
17616 it->pixel_width += thick;
17617 }
17618
17619 /* If face has an overline, add the height of the overline
17620 (1 pixel) and a 1 pixel margin to the character height. */
17621 if (face->overline_p)
17622 it->ascent += 2;
17623
17624 take_vertical_position_into_account (it);
17625
17626 if (it->glyph_row)
17627 append_composite_glyph (it);
17628 }
17629 else if (it->what == IT_IMAGE)
17630 produce_image_glyph (it);
17631 else if (it->what == IT_STRETCH)
17632 produce_stretch_glyph (it);
17633
17634 /* Accumulate dimensions. Note: can't assume that it->descent > 0
17635 because this isn't true for images with `:ascent 100'. */
17636 xassert (it->ascent >= 0 && it->descent >= 0);
17637 if (it->area == TEXT_AREA)
17638 it->current_x += it->pixel_width;
17639
17640 it->descent += it->extra_line_spacing;
17641
17642 it->max_ascent = max (it->max_ascent, it->ascent);
17643 it->max_descent = max (it->max_descent, it->descent);
17644 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
17645 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
17646 }
17647
17648
17649
17650
17651
17652 \f
17653 /***********************************************************************
17654 Cursor types
17655 ***********************************************************************/
17656
17657 /* Value is the internal representation of the specified cursor type
17658 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
17659 of the bar cursor. */
17660
17661 enum text_cursor_kinds
17662 get_specified_cursor_type (arg, width)
17663 Lisp_Object arg;
17664 int *width;
17665 {
17666 enum text_cursor_kinds type;
17667
17668 if (NILP (arg))
17669 return NO_CURSOR;
17670
17671 if (EQ (arg, Qbox))
17672 return FILLED_BOX_CURSOR;
17673
17674 if (EQ (arg, Qhollow))
17675 return HOLLOW_BOX_CURSOR;
17676
17677 if (EQ (arg, Qbar))
17678 {
17679 *width = 2;
17680 return BAR_CURSOR;
17681 }
17682
17683 if (CONSP (arg)
17684 && EQ (XCAR (arg), Qbar)
17685 && INTEGERP (XCDR (arg))
17686 && XINT (XCDR (arg)) >= 0)
17687 {
17688 *width = XINT (XCDR (arg));
17689 return BAR_CURSOR;
17690 }
17691
17692 if (EQ (arg, Qhbar))
17693 {
17694 *width = 2;
17695 return HBAR_CURSOR;
17696 }
17697
17698 if (CONSP (arg)
17699 && EQ (XCAR (arg), Qhbar)
17700 && INTEGERP (XCDR (arg))
17701 && XINT (XCDR (arg)) >= 0)
17702 {
17703 *width = XINT (XCDR (arg));
17704 return HBAR_CURSOR;
17705 }
17706
17707 /* Treat anything unknown as "hollow box cursor".
17708 It was bad to signal an error; people have trouble fixing
17709 .Xdefaults with Emacs, when it has something bad in it. */
17710 type = HOLLOW_BOX_CURSOR;
17711
17712 return type;
17713 }
17714
17715 /* Set the default cursor types for specified frame. */
17716 void
17717 set_frame_cursor_types (f, arg)
17718 struct frame *f;
17719 Lisp_Object arg;
17720 {
17721 int width;
17722 Lisp_Object tem;
17723
17724 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
17725 FRAME_CURSOR_WIDTH (f) = width;
17726
17727 /* By default, set up the blink-off state depending on the on-state. */
17728
17729 tem = Fassoc (arg, Vblink_cursor_alist);
17730 if (!NILP (tem))
17731 {
17732 FRAME_BLINK_OFF_CURSOR (f)
17733 = get_specified_cursor_type (XCDR (tem), &width);
17734 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
17735 }
17736 else
17737 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
17738 }
17739
17740
17741 /* Return the cursor we want to be displayed in window W. Return
17742 width of bar/hbar cursor through WIDTH arg. Return with
17743 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
17744 (i.e. if the `system caret' should track this cursor).
17745
17746 In a mini-buffer window, we want the cursor only to appear if we
17747 are reading input from this window. For the selected window, we
17748 want the cursor type given by the frame parameter or buffer local
17749 setting of cursor-type. If explicitly marked off, draw no cursor.
17750 In all other cases, we want a hollow box cursor. */
17751
17752 enum text_cursor_kinds
17753 get_window_cursor_type (w, width, active_cursor)
17754 struct window *w;
17755 int *width;
17756 int *active_cursor;
17757 {
17758 struct frame *f = XFRAME (w->frame);
17759 struct buffer *b = XBUFFER (w->buffer);
17760 int cursor_type = DEFAULT_CURSOR;
17761 Lisp_Object alt_cursor;
17762 int non_selected = 0;
17763
17764 *active_cursor = 1;
17765
17766 /* Echo area */
17767 if (cursor_in_echo_area
17768 && FRAME_HAS_MINIBUF_P (f)
17769 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
17770 {
17771 if (w == XWINDOW (echo_area_window))
17772 {
17773 *width = FRAME_CURSOR_WIDTH (f);
17774 return FRAME_DESIRED_CURSOR (f);
17775 }
17776
17777 *active_cursor = 0;
17778 non_selected = 1;
17779 }
17780
17781 /* Nonselected window or nonselected frame. */
17782 else if (w != XWINDOW (f->selected_window)
17783 #ifdef HAVE_WINDOW_SYSTEM
17784 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
17785 #endif
17786 )
17787 {
17788 *active_cursor = 0;
17789
17790 if (MINI_WINDOW_P (w) && minibuf_level == 0)
17791 return NO_CURSOR;
17792
17793 non_selected = 1;
17794 }
17795
17796 /* Never display a cursor in a window in which cursor-type is nil. */
17797 if (NILP (b->cursor_type))
17798 return NO_CURSOR;
17799
17800 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
17801 if (non_selected)
17802 {
17803 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
17804 return get_specified_cursor_type (alt_cursor, width);
17805 }
17806
17807 /* Get the normal cursor type for this window. */
17808 if (EQ (b->cursor_type, Qt))
17809 {
17810 cursor_type = FRAME_DESIRED_CURSOR (f);
17811 *width = FRAME_CURSOR_WIDTH (f);
17812 }
17813 else
17814 cursor_type = get_specified_cursor_type (b->cursor_type, width);
17815
17816 /* Use normal cursor if not blinked off. */
17817 if (!w->cursor_off_p)
17818 return cursor_type;
17819
17820 /* Cursor is blinked off, so determine how to "toggle" it. */
17821
17822 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
17823 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
17824 return get_specified_cursor_type (XCDR (alt_cursor), width);
17825
17826 /* Then see if frame has specified a specific blink off cursor type. */
17827 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
17828 {
17829 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
17830 return FRAME_BLINK_OFF_CURSOR (f);
17831 }
17832
17833 /* Finally perform built-in cursor blinking:
17834 filled box <-> hollow box
17835 wide [h]bar <-> narrow [h]bar
17836 narrow [h]bar <-> no cursor
17837 other type <-> no cursor */
17838
17839 if (cursor_type == FILLED_BOX_CURSOR)
17840 return HOLLOW_BOX_CURSOR;
17841
17842 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
17843 {
17844 *width = 1;
17845 return cursor_type;
17846 }
17847
17848 return NO_CURSOR;
17849 }
17850
17851
17852 /* Notice when the text cursor of window W has been completely
17853 overwritten by a drawing operation that outputs glyphs in AREA
17854 starting at X0 and ending at X1 in the line starting at Y0 and
17855 ending at Y1. X coordinates are area-relative. X1 < 0 means all
17856 the rest of the line after X0 has been written. Y coordinates
17857 are window-relative. */
17858
17859 void
17860 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
17861 struct window *w;
17862 enum glyph_row_area area;
17863 int x0, y0, x1, y1;
17864 {
17865 #ifdef HAVE_CARBON
17866 /* ++KFS: Why is there a special version of this for the mac ? */
17867 if (area == TEXT_AREA
17868 && w->phys_cursor_on_p
17869 && y0 <= w->phys_cursor.y
17870 && y1 >= w->phys_cursor.y + w->phys_cursor_height
17871 && x0 <= w->phys_cursor.x
17872 && (x1 < 0 || x1 > w->phys_cursor.x))
17873 w->phys_cursor_on_p = 0;
17874 #else
17875 if (area == TEXT_AREA && w->phys_cursor_on_p)
17876 {
17877 int cx0 = w->phys_cursor.x;
17878 int cx1 = cx0 + w->phys_cursor_width;
17879 int cy0 = w->phys_cursor.y;
17880 int cy1 = cy0 + w->phys_cursor_height;
17881
17882 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
17883 {
17884 /* The cursor image will be completely removed from the
17885 screen if the output area intersects the cursor area in
17886 y-direction. When we draw in [y0 y1[, and some part of
17887 the cursor is at y < y0, that part must have been drawn
17888 before. When scrolling, the cursor is erased before
17889 actually scrolling, so we don't come here. When not
17890 scrolling, the rows above the old cursor row must have
17891 changed, and in this case these rows must have written
17892 over the cursor image.
17893
17894 Likewise if part of the cursor is below y1, with the
17895 exception of the cursor being in the first blank row at
17896 the buffer and window end because update_text_area
17897 doesn't draw that row. (Except when it does, but
17898 that's handled in update_text_area.) */
17899
17900 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
17901 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
17902 w->phys_cursor_on_p = 0;
17903 }
17904 }
17905 #endif
17906 }
17907
17908 \f
17909 /***********************************************************************
17910 Initialization
17911 ***********************************************************************/
17912
17913 void
17914 syms_of_xdisp ()
17915 {
17916 Vwith_echo_area_save_vector = Qnil;
17917 staticpro (&Vwith_echo_area_save_vector);
17918
17919 Vmessage_stack = Qnil;
17920 staticpro (&Vmessage_stack);
17921
17922 Qinhibit_redisplay = intern ("inhibit-redisplay");
17923 staticpro (&Qinhibit_redisplay);
17924
17925 message_dolog_marker1 = Fmake_marker ();
17926 staticpro (&message_dolog_marker1);
17927 message_dolog_marker2 = Fmake_marker ();
17928 staticpro (&message_dolog_marker2);
17929 message_dolog_marker3 = Fmake_marker ();
17930 staticpro (&message_dolog_marker3);
17931
17932 #if GLYPH_DEBUG
17933 defsubr (&Sdump_frame_glyph_matrix);
17934 defsubr (&Sdump_glyph_matrix);
17935 defsubr (&Sdump_glyph_row);
17936 defsubr (&Sdump_tool_bar_row);
17937 defsubr (&Strace_redisplay);
17938 defsubr (&Strace_to_stderr);
17939 #endif
17940 #ifdef HAVE_WINDOW_SYSTEM
17941 defsubr (&Stool_bar_lines_needed);
17942 #endif
17943 defsubr (&Sformat_mode_line);
17944
17945 staticpro (&Qmenu_bar_update_hook);
17946 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
17947
17948 staticpro (&Qoverriding_terminal_local_map);
17949 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
17950
17951 staticpro (&Qoverriding_local_map);
17952 Qoverriding_local_map = intern ("overriding-local-map");
17953
17954 staticpro (&Qwindow_scroll_functions);
17955 Qwindow_scroll_functions = intern ("window-scroll-functions");
17956
17957 staticpro (&Qredisplay_end_trigger_functions);
17958 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
17959
17960 staticpro (&Qinhibit_point_motion_hooks);
17961 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
17962
17963 QCdata = intern (":data");
17964 staticpro (&QCdata);
17965 Qdisplay = intern ("display");
17966 staticpro (&Qdisplay);
17967 Qspace_width = intern ("space-width");
17968 staticpro (&Qspace_width);
17969 Qraise = intern ("raise");
17970 staticpro (&Qraise);
17971 Qspace = intern ("space");
17972 staticpro (&Qspace);
17973 Qmargin = intern ("margin");
17974 staticpro (&Qmargin);
17975 Qleft_margin = intern ("left-margin");
17976 staticpro (&Qleft_margin);
17977 Qright_margin = intern ("right-margin");
17978 staticpro (&Qright_margin);
17979 Qalign_to = intern ("align-to");
17980 staticpro (&Qalign_to);
17981 QCalign_to = intern (":align-to");
17982 staticpro (&QCalign_to);
17983 Qrelative_width = intern ("relative-width");
17984 staticpro (&Qrelative_width);
17985 QCrelative_width = intern (":relative-width");
17986 staticpro (&QCrelative_width);
17987 QCrelative_height = intern (":relative-height");
17988 staticpro (&QCrelative_height);
17989 QCeval = intern (":eval");
17990 staticpro (&QCeval);
17991 QCpropertize = intern (":propertize");
17992 staticpro (&QCpropertize);
17993 QCfile = intern (":file");
17994 staticpro (&QCfile);
17995 Qfontified = intern ("fontified");
17996 staticpro (&Qfontified);
17997 Qfontification_functions = intern ("fontification-functions");
17998 staticpro (&Qfontification_functions);
17999 Qtrailing_whitespace = intern ("trailing-whitespace");
18000 staticpro (&Qtrailing_whitespace);
18001 Qimage = intern ("image");
18002 staticpro (&Qimage);
18003 Qmessage_truncate_lines = intern ("message-truncate-lines");
18004 staticpro (&Qmessage_truncate_lines);
18005 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
18006 staticpro (&Qcursor_in_non_selected_windows);
18007 Qgrow_only = intern ("grow-only");
18008 staticpro (&Qgrow_only);
18009 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
18010 staticpro (&Qinhibit_menubar_update);
18011 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
18012 staticpro (&Qinhibit_eval_during_redisplay);
18013 Qposition = intern ("position");
18014 staticpro (&Qposition);
18015 Qbuffer_position = intern ("buffer-position");
18016 staticpro (&Qbuffer_position);
18017 Qobject = intern ("object");
18018 staticpro (&Qobject);
18019 Qbar = intern ("bar");
18020 staticpro (&Qbar);
18021 Qhbar = intern ("hbar");
18022 staticpro (&Qhbar);
18023 Qbox = intern ("box");
18024 staticpro (&Qbox);
18025 Qhollow = intern ("hollow");
18026 staticpro (&Qhollow);
18027 Qrisky_local_variable = intern ("risky-local-variable");
18028 staticpro (&Qrisky_local_variable);
18029 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
18030 staticpro (&Qinhibit_free_realized_faces);
18031
18032 list_of_error = Fcons (intern ("error"), Qnil);
18033 staticpro (&list_of_error);
18034
18035 last_arrow_position = Qnil;
18036 last_arrow_string = Qnil;
18037 staticpro (&last_arrow_position);
18038 staticpro (&last_arrow_string);
18039
18040 echo_buffer[0] = echo_buffer[1] = Qnil;
18041 staticpro (&echo_buffer[0]);
18042 staticpro (&echo_buffer[1]);
18043
18044 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
18045 staticpro (&echo_area_buffer[0]);
18046 staticpro (&echo_area_buffer[1]);
18047
18048 Vmessages_buffer_name = build_string ("*Messages*");
18049 staticpro (&Vmessages_buffer_name);
18050
18051 mode_line_proptrans_alist = Qnil;
18052 staticpro (&mode_line_proptrans_alist);
18053
18054 mode_line_string_list = Qnil;
18055 staticpro (&mode_line_string_list);
18056
18057 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
18058 doc: /* Non-nil means highlight trailing whitespace.
18059 The face used for trailing whitespace is `trailing-whitespace'. */);
18060 Vshow_trailing_whitespace = Qnil;
18061
18062 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
18063 doc: /* Non-nil means don't actually do any redisplay.
18064 This is used for internal purposes. */);
18065 Vinhibit_redisplay = Qnil;
18066
18067 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
18068 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
18069 Vglobal_mode_string = Qnil;
18070
18071 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
18072 doc: /* Marker for where to display an arrow on top of the buffer text.
18073 This must be the beginning of a line in order to work.
18074 See also `overlay-arrow-string'. */);
18075 Voverlay_arrow_position = Qnil;
18076
18077 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
18078 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
18079 Voverlay_arrow_string = Qnil;
18080
18081 DEFVAR_INT ("scroll-step", &scroll_step,
18082 doc: /* *The number of lines to try scrolling a window by when point moves out.
18083 If that fails to bring point back on frame, point is centered instead.
18084 If this is zero, point is always centered after it moves off frame.
18085 If you want scrolling to always be a line at a time, you should set
18086 `scroll-conservatively' to a large value rather than set this to 1. */);
18087
18088 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
18089 doc: /* *Scroll up to this many lines, to bring point back on screen.
18090 A value of zero means to scroll the text to center point vertically
18091 in the window. */);
18092 scroll_conservatively = 0;
18093
18094 DEFVAR_INT ("scroll-margin", &scroll_margin,
18095 doc: /* *Number of lines of margin at the top and bottom of a window.
18096 Recenter the window whenever point gets within this many lines
18097 of the top or bottom of the window. */);
18098 scroll_margin = 0;
18099
18100 #if GLYPH_DEBUG
18101 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
18102 #endif
18103
18104 DEFVAR_BOOL ("truncate-partial-width-windows",
18105 &truncate_partial_width_windows,
18106 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
18107 truncate_partial_width_windows = 1;
18108
18109 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
18110 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
18111 Any other value means to use the appropriate face, `mode-line',
18112 `header-line', or `menu' respectively. */);
18113 mode_line_inverse_video = 1;
18114
18115 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
18116 doc: /* *Maximum buffer size for which line number should be displayed.
18117 If the buffer is bigger than this, the line number does not appear
18118 in the mode line. A value of nil means no limit. */);
18119 Vline_number_display_limit = Qnil;
18120
18121 DEFVAR_INT ("line-number-display-limit-width",
18122 &line_number_display_limit_width,
18123 doc: /* *Maximum line width (in characters) for line number display.
18124 If the average length of the lines near point is bigger than this, then the
18125 line number may be omitted from the mode line. */);
18126 line_number_display_limit_width = 200;
18127
18128 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
18129 doc: /* *Non-nil means highlight region even in nonselected windows. */);
18130 highlight_nonselected_windows = 0;
18131
18132 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
18133 doc: /* Non-nil if more than one frame is visible on this display.
18134 Minibuffer-only frames don't count, but iconified frames do.
18135 This variable is not guaranteed to be accurate except while processing
18136 `frame-title-format' and `icon-title-format'. */);
18137
18138 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
18139 doc: /* Template for displaying the title bar of visible frames.
18140 \(Assuming the window manager supports this feature.)
18141 This variable has the same structure as `mode-line-format' (which see),
18142 and is used only on frames for which no explicit name has been set
18143 \(see `modify-frame-parameters'). */);
18144 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
18145 doc: /* Template for displaying the title bar of an iconified frame.
18146 \(Assuming the window manager supports this feature.)
18147 This variable has the same structure as `mode-line-format' (which see),
18148 and is used only on frames for which no explicit name has been set
18149 \(see `modify-frame-parameters'). */);
18150 Vicon_title_format
18151 = Vframe_title_format
18152 = Fcons (intern ("multiple-frames"),
18153 Fcons (build_string ("%b"),
18154 Fcons (Fcons (empty_string,
18155 Fcons (intern ("invocation-name"),
18156 Fcons (build_string ("@"),
18157 Fcons (intern ("system-name"),
18158 Qnil)))),
18159 Qnil)));
18160
18161 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
18162 doc: /* Maximum number of lines to keep in the message log buffer.
18163 If nil, disable message logging. If t, log messages but don't truncate
18164 the buffer when it becomes large. */);
18165 Vmessage_log_max = make_number (50);
18166
18167 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
18168 doc: /* Functions called before redisplay, if window sizes have changed.
18169 The value should be a list of functions that take one argument.
18170 Just before redisplay, for each frame, if any of its windows have changed
18171 size since the last redisplay, or have been split or deleted,
18172 all the functions in the list are called, with the frame as argument. */);
18173 Vwindow_size_change_functions = Qnil;
18174
18175 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
18176 doc: /* List of Functions to call before redisplaying a window with scrolling.
18177 Each function is called with two arguments, the window
18178 and its new display-start position. Note that the value of `window-end'
18179 is not valid when these functions are called. */);
18180 Vwindow_scroll_functions = Qnil;
18181
18182 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
18183 doc: /* *Non-nil means automatically resize tool-bars.
18184 This increases a tool-bar's height if not all tool-bar items are visible.
18185 It decreases a tool-bar's height when it would display blank lines
18186 otherwise. */);
18187 auto_resize_tool_bars_p = 1;
18188
18189 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
18190 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
18191 auto_raise_tool_bar_buttons_p = 1;
18192
18193 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
18194 doc: /* *Margin around tool-bar buttons in pixels.
18195 If an integer, use that for both horizontal and vertical margins.
18196 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
18197 HORZ specifying the horizontal margin, and VERT specifying the
18198 vertical margin. */);
18199 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
18200
18201 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
18202 doc: /* *Relief thickness of tool-bar buttons. */);
18203 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
18204
18205 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
18206 doc: /* List of functions to call to fontify regions of text.
18207 Each function is called with one argument POS. Functions must
18208 fontify a region starting at POS in the current buffer, and give
18209 fontified regions the property `fontified'. */);
18210 Vfontification_functions = Qnil;
18211 Fmake_variable_buffer_local (Qfontification_functions);
18212
18213 DEFVAR_BOOL ("unibyte-display-via-language-environment",
18214 &unibyte_display_via_language_environment,
18215 doc: /* *Non-nil means display unibyte text according to language environment.
18216 Specifically this means that unibyte non-ASCII characters
18217 are displayed by converting them to the equivalent multibyte characters
18218 according to the current language environment. As a result, they are
18219 displayed according to the current fontset. */);
18220 unibyte_display_via_language_environment = 0;
18221
18222 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
18223 doc: /* *Maximum height for resizing mini-windows.
18224 If a float, it specifies a fraction of the mini-window frame's height.
18225 If an integer, it specifies a number of lines. */);
18226 Vmax_mini_window_height = make_float (0.25);
18227
18228 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
18229 doc: /* *How to resize mini-windows.
18230 A value of nil means don't automatically resize mini-windows.
18231 A value of t means resize them to fit the text displayed in them.
18232 A value of `grow-only', the default, means let mini-windows grow
18233 only, until their display becomes empty, at which point the windows
18234 go back to their normal size. */);
18235 Vresize_mini_windows = Qgrow_only;
18236
18237 DEFVAR_LISP ("cursor-in-non-selected-windows",
18238 &Vcursor_in_non_selected_windows,
18239 doc: /* *Cursor type to display in non-selected windows.
18240 t means to use hollow box cursor. See `cursor-type' for other values. */);
18241 Vcursor_in_non_selected_windows = Qt;
18242
18243 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
18244 doc: /* Alist specifying how to blink the cursor off.
18245 Each element has the form (ON-STATE . OFF-STATE). Whenever the
18246 `cursor-type' frame-parameter or variable equals ON-STATE,
18247 comparing using `equal', Emacs uses OFF-STATE to specify
18248 how to blink it off. */);
18249 Vblink_cursor_alist = Qnil;
18250
18251 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
18252 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
18253 automatic_hscrolling_p = 1;
18254
18255 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
18256 doc: /* *How many columns away from the window edge point is allowed to get
18257 before automatic hscrolling will horizontally scroll the window. */);
18258 hscroll_margin = 5;
18259
18260 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
18261 doc: /* *How many columns to scroll the window when point gets too close to the edge.
18262 When point is less than `automatic-hscroll-margin' columns from the window
18263 edge, automatic hscrolling will scroll the window by the amount of columns
18264 determined by this variable. If its value is a positive integer, scroll that
18265 many columns. If it's a positive floating-point number, it specifies the
18266 fraction of the window's width to scroll. If it's nil or zero, point will be
18267 centered horizontally after the scroll. Any other value, including negative
18268 numbers, are treated as if the value were zero.
18269
18270 Automatic hscrolling always moves point outside the scroll margin, so if
18271 point was more than scroll step columns inside the margin, the window will
18272 scroll more than the value given by the scroll step.
18273
18274 Note that the lower bound for automatic hscrolling specified by `scroll-left'
18275 and `scroll-right' overrides this variable's effect. */);
18276 Vhscroll_step = make_number (0);
18277
18278 DEFVAR_LISP ("image-types", &Vimage_types,
18279 doc: /* List of supported image types.
18280 Each element of the list is a symbol for a supported image type. */);
18281 Vimage_types = Qnil;
18282
18283 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
18284 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
18285 Bind this around calls to `message' to let it take effect. */);
18286 message_truncate_lines = 0;
18287
18288 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
18289 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
18290 Can be used to update submenus whose contents should vary. */);
18291 Vmenu_bar_update_hook = Qnil;
18292
18293 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
18294 doc: /* Non-nil means don't update menu bars. Internal use only. */);
18295 inhibit_menubar_update = 0;
18296
18297 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
18298 doc: /* Non-nil means don't eval Lisp during redisplay. */);
18299 inhibit_eval_during_redisplay = 0;
18300
18301 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
18302 doc: /* Non-nil means don't free realized faces. Internal use only. */);
18303 inhibit_free_realized_faces = 0;
18304
18305 #if GLYPH_DEBUG
18306 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
18307 doc: /* Inhibit try_window_id display optimization. */);
18308 inhibit_try_window_id = 0;
18309
18310 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
18311 doc: /* Inhibit try_window_reusing display optimization. */);
18312 inhibit_try_window_reusing = 0;
18313
18314 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
18315 doc: /* Inhibit try_cursor_movement display optimization. */);
18316 inhibit_try_cursor_movement = 0;
18317 #endif /* GLYPH_DEBUG */
18318 }
18319
18320
18321 /* Initialize this module when Emacs starts. */
18322
18323 void
18324 init_xdisp ()
18325 {
18326 Lisp_Object root_window;
18327 struct window *mini_w;
18328
18329 current_header_line_height = current_mode_line_height = -1;
18330
18331 CHARPOS (this_line_start_pos) = 0;
18332
18333 mini_w = XWINDOW (minibuf_window);
18334 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
18335
18336 if (!noninteractive)
18337 {
18338 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
18339 int i;
18340
18341 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
18342 set_window_height (root_window,
18343 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
18344 0);
18345 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
18346 set_window_height (minibuf_window, 1, 0);
18347
18348 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
18349 mini_w->width = make_number (FRAME_WIDTH (f));
18350
18351 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
18352 scratch_glyph_row.glyphs[TEXT_AREA + 1]
18353 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
18354
18355 /* The default ellipsis glyphs `...'. */
18356 for (i = 0; i < 3; ++i)
18357 default_invis_vector[i] = make_number ('.');
18358 }
18359
18360 {
18361 /* Allocate the buffer for frame titles.
18362 Also used for `format-mode-line'. */
18363 int size = 100;
18364 frame_title_buf = (char *) xmalloc (size);
18365 frame_title_buf_end = frame_title_buf + size;
18366 frame_title_ptr = NULL;
18367 }
18368
18369 help_echo_showing_p = 0;
18370 }
18371
18372