.
[emacs.git] / src / xdisp.c
blob629d210daa8a758e1519ca4762f3a1a10a1e0017
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99,2000,01,02,03
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
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.
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.)
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.
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! |
63 expose_window (asynchronous) |
65 X expose events -----+
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.
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.
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.
89 Direct operations.
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.
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.
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.
110 Desired matrices.
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.
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.
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.
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.
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.
148 Frame matrices.
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.
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. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
202 Cursor No_Cursor;
203 #endif
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
209 #define INFINITY 10000000
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
217 extern int interrupt_input;
218 extern int command_loop_level;
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245 Lisp_Object Qrisky_local_variable;
247 /* Holds the list (error). */
248 Lisp_Object list_of_error;
250 /* Functions called to fontify regions of text. */
252 Lisp_Object Vfontification_functions;
253 Lisp_Object Qfontification_functions;
255 /* Non-zero means automatically select any window when the mouse
256 cursor moves into it. */
257 int mouse_autoselect_window;
259 /* Non-zero means draw tool bar buttons raised when the mouse moves
260 over them. */
262 int auto_raise_tool_bar_buttons_p;
264 /* Margin around tool bar buttons in pixels. */
266 Lisp_Object Vtool_bar_button_margin;
268 /* Thickness of shadow to draw around tool bar buttons. */
270 EMACS_INT tool_bar_button_relief;
272 /* Non-zero means automatically resize tool-bars so that all tool-bar
273 items are visible, and no blank lines remain. */
275 int auto_resize_tool_bars_p;
277 /* Non-zero means draw block and hollow cursor as wide as the glyph
278 under it. For example, if a block cursor is over a tab, it will be
279 drawn as wide as that tab on the display. */
281 int x_stretch_cursor_p;
283 /* Non-nil means don't actually do any redisplay. */
285 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
287 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
289 int inhibit_eval_during_redisplay;
291 /* Names of text properties relevant for redisplay. */
293 Lisp_Object Qdisplay, Qrelative_width, Qalign_to;
294 extern Lisp_Object Qface, Qinvisible, Qwidth;
296 /* Symbols used in text property values. */
298 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
299 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
300 Lisp_Object Qmargin;
301 extern Lisp_Object Qheight;
302 extern Lisp_Object QCwidth, QCheight, QCascent;
304 /* Non-nil means highlight trailing whitespace. */
306 Lisp_Object Vshow_trailing_whitespace;
308 /* Name of the face used to highlight trailing whitespace. */
310 Lisp_Object Qtrailing_whitespace;
312 /* The symbol `image' which is the car of the lists used to represent
313 images in Lisp. */
315 Lisp_Object Qimage;
317 /* Non-zero means print newline to stdout before next mini-buffer
318 message. */
320 int noninteractive_need_newline;
322 /* Non-zero means print newline to message log before next message. */
324 static int message_log_need_newline;
326 /* Three markers that message_dolog uses.
327 It could allocate them itself, but that causes trouble
328 in handling memory-full errors. */
329 static Lisp_Object message_dolog_marker1;
330 static Lisp_Object message_dolog_marker2;
331 static Lisp_Object message_dolog_marker3;
333 /* The buffer position of the first character appearing entirely or
334 partially on the line of the selected window which contains the
335 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
336 redisplay optimization in redisplay_internal. */
338 static struct text_pos this_line_start_pos;
340 /* Number of characters past the end of the line above, including the
341 terminating newline. */
343 static struct text_pos this_line_end_pos;
345 /* The vertical positions and the height of this line. */
347 static int this_line_vpos;
348 static int this_line_y;
349 static int this_line_pixel_height;
351 /* X position at which this display line starts. Usually zero;
352 negative if first character is partially visible. */
354 static int this_line_start_x;
356 /* Buffer that this_line_.* variables are referring to. */
358 static struct buffer *this_line_buffer;
360 /* Nonzero means truncate lines in all windows less wide than the
361 frame. */
363 int truncate_partial_width_windows;
365 /* A flag to control how to display unibyte 8-bit character. */
367 int unibyte_display_via_language_environment;
369 /* Nonzero means we have more than one non-mini-buffer-only frame.
370 Not guaranteed to be accurate except while parsing
371 frame-title-format. */
373 int multiple_frames;
375 Lisp_Object Vglobal_mode_string;
377 /* Marker for where to display an arrow on top of the buffer text. */
379 Lisp_Object Voverlay_arrow_position;
381 /* String to display for the arrow. Only used on terminal frames. */
383 Lisp_Object Voverlay_arrow_string;
385 /* Values of those variables at last redisplay. However, if
386 Voverlay_arrow_position is a marker, last_arrow_position is its
387 numerical position. */
389 static Lisp_Object last_arrow_position, last_arrow_string;
391 /* Like mode-line-format, but for the title bar on a visible frame. */
393 Lisp_Object Vframe_title_format;
395 /* Like mode-line-format, but for the title bar on an iconified frame. */
397 Lisp_Object Vicon_title_format;
399 /* List of functions to call when a window's size changes. These
400 functions get one arg, a frame on which one or more windows' sizes
401 have changed. */
403 static Lisp_Object Vwindow_size_change_functions;
405 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
407 /* Nonzero if overlay arrow has been displayed once in this window. */
409 static int overlay_arrow_seen;
411 /* Nonzero means highlight the region even in nonselected windows. */
413 int highlight_nonselected_windows;
415 /* If cursor motion alone moves point off frame, try scrolling this
416 many lines up or down if that will bring it back. */
418 static EMACS_INT scroll_step;
420 /* Nonzero means scroll just far enough to bring point back on the
421 screen, when appropriate. */
423 static EMACS_INT scroll_conservatively;
425 /* Recenter the window whenever point gets within this many lines of
426 the top or bottom of the window. This value is translated into a
427 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
428 that there is really a fixed pixel height scroll margin. */
430 EMACS_INT scroll_margin;
432 /* Number of windows showing the buffer of the selected window (or
433 another buffer with the same base buffer). keyboard.c refers to
434 this. */
436 int buffer_shared;
438 /* Vector containing glyphs for an ellipsis `...'. */
440 static Lisp_Object default_invis_vector[3];
442 /* Zero means display the mode-line/header-line/menu-bar in the default face
443 (this slightly odd definition is for compatibility with previous versions
444 of emacs), non-zero means display them using their respective faces.
446 This variable is deprecated. */
448 int mode_line_inverse_video;
450 /* Prompt to display in front of the mini-buffer contents. */
452 Lisp_Object minibuf_prompt;
454 /* Width of current mini-buffer prompt. Only set after display_line
455 of the line that contains the prompt. */
457 int minibuf_prompt_width;
459 /* This is the window where the echo area message was displayed. It
460 is always a mini-buffer window, but it may not be the same window
461 currently active as a mini-buffer. */
463 Lisp_Object echo_area_window;
465 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
466 pushes the current message and the value of
467 message_enable_multibyte on the stack, the function restore_message
468 pops the stack and displays MESSAGE again. */
470 Lisp_Object Vmessage_stack;
472 /* Nonzero means multibyte characters were enabled when the echo area
473 message was specified. */
475 int message_enable_multibyte;
477 /* Nonzero if we should redraw the mode lines on the next redisplay. */
479 int update_mode_lines;
481 /* Nonzero if window sizes or contents have changed since last
482 redisplay that finished. */
484 int windows_or_buffers_changed;
486 /* Nonzero means a frame's cursor type has been changed. */
488 int cursor_type_changed;
490 /* Nonzero after display_mode_line if %l was used and it displayed a
491 line number. */
493 int line_number_displayed;
495 /* Maximum buffer size for which to display line numbers. */
497 Lisp_Object Vline_number_display_limit;
499 /* Line width to consider when repositioning for line number display. */
501 static EMACS_INT line_number_display_limit_width;
503 /* Number of lines to keep in the message log buffer. t means
504 infinite. nil means don't log at all. */
506 Lisp_Object Vmessage_log_max;
508 /* The name of the *Messages* buffer, a string. */
510 static Lisp_Object Vmessages_buffer_name;
512 /* Current, index 0, and last displayed echo area message. Either
513 buffers from echo_buffers, or nil to indicate no message. */
515 Lisp_Object echo_area_buffer[2];
517 /* The buffers referenced from echo_area_buffer. */
519 static Lisp_Object echo_buffer[2];
521 /* A vector saved used in with_area_buffer to reduce consing. */
523 static Lisp_Object Vwith_echo_area_save_vector;
525 /* Non-zero means display_echo_area should display the last echo area
526 message again. Set by redisplay_preserve_echo_area. */
528 static int display_last_displayed_message_p;
530 /* Nonzero if echo area is being used by print; zero if being used by
531 message. */
533 int message_buf_print;
535 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
537 Lisp_Object Qinhibit_menubar_update;
538 int inhibit_menubar_update;
540 /* Maximum height for resizing mini-windows. Either a float
541 specifying a fraction of the available height, or an integer
542 specifying a number of lines. */
544 Lisp_Object Vmax_mini_window_height;
546 /* Non-zero means messages should be displayed with truncated
547 lines instead of being continued. */
549 int message_truncate_lines;
550 Lisp_Object Qmessage_truncate_lines;
552 /* Set to 1 in clear_message to make redisplay_internal aware
553 of an emptied echo area. */
555 static int message_cleared_p;
557 /* Non-zero means we want a hollow cursor in windows that are not
558 selected. Zero means there's no cursor in such windows. */
560 Lisp_Object Vcursor_in_non_selected_windows;
561 Lisp_Object Qcursor_in_non_selected_windows;
563 /* How to blink the default frame cursor off. */
564 Lisp_Object Vblink_cursor_alist;
566 /* A scratch glyph row with contents used for generating truncation
567 glyphs. Also used in direct_output_for_insert. */
569 #define MAX_SCRATCH_GLYPHS 100
570 struct glyph_row scratch_glyph_row;
571 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
573 /* Ascent and height of the last line processed by move_it_to. */
575 static int last_max_ascent, last_height;
577 /* Non-zero if there's a help-echo in the echo area. */
579 int help_echo_showing_p;
581 /* If >= 0, computed, exact values of mode-line and header-line height
582 to use in the macros CURRENT_MODE_LINE_HEIGHT and
583 CURRENT_HEADER_LINE_HEIGHT. */
585 int current_mode_line_height, current_header_line_height;
587 /* The maximum distance to look ahead for text properties. Values
588 that are too small let us call compute_char_face and similar
589 functions too often which is expensive. Values that are too large
590 let us call compute_char_face and alike too often because we
591 might not be interested in text properties that far away. */
593 #define TEXT_PROP_DISTANCE_LIMIT 100
595 #if GLYPH_DEBUG
597 /* Variables to turn off display optimizations from Lisp. */
599 int inhibit_try_window_id, inhibit_try_window_reusing;
600 int inhibit_try_cursor_movement;
602 /* Non-zero means print traces of redisplay if compiled with
603 GLYPH_DEBUG != 0. */
605 int trace_redisplay_p;
607 #endif /* GLYPH_DEBUG */
609 #ifdef DEBUG_TRACE_MOVE
610 /* Non-zero means trace with TRACE_MOVE to stderr. */
611 int trace_move;
613 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
614 #else
615 #define TRACE_MOVE(x) (void) 0
616 #endif
618 /* Non-zero means automatically scroll windows horizontally to make
619 point visible. */
621 int automatic_hscrolling_p;
623 /* How close to the margin can point get before the window is scrolled
624 horizontally. */
625 EMACS_INT hscroll_margin;
627 /* How much to scroll horizontally when point is inside the above margin. */
628 Lisp_Object Vhscroll_step;
630 /* A list of symbols, one for each supported image type. */
632 Lisp_Object Vimage_types;
634 /* The variable `resize-mini-windows'. If nil, don't resize
635 mini-windows. If t, always resize them to fit the text they
636 display. If `grow-only', let mini-windows grow only until they
637 become empty. */
639 Lisp_Object Vresize_mini_windows;
641 /* Buffer being redisplayed -- for redisplay_window_error. */
643 struct buffer *displayed_buffer;
645 /* Value returned from text property handlers (see below). */
647 enum prop_handled
649 HANDLED_NORMALLY,
650 HANDLED_RECOMPUTE_PROPS,
651 HANDLED_OVERLAY_STRING_CONSUMED,
652 HANDLED_RETURN
655 /* A description of text properties that redisplay is interested
656 in. */
658 struct props
660 /* The name of the property. */
661 Lisp_Object *name;
663 /* A unique index for the property. */
664 enum prop_idx idx;
666 /* A handler function called to set up iterator IT from the property
667 at IT's current position. Value is used to steer handle_stop. */
668 enum prop_handled (*handler) P_ ((struct it *it));
671 static enum prop_handled handle_face_prop P_ ((struct it *));
672 static enum prop_handled handle_invisible_prop P_ ((struct it *));
673 static enum prop_handled handle_display_prop P_ ((struct it *));
674 static enum prop_handled handle_composition_prop P_ ((struct it *));
675 static enum prop_handled handle_overlay_change P_ ((struct it *));
676 static enum prop_handled handle_fontified_prop P_ ((struct it *));
678 /* Properties handled by iterators. */
680 static struct props it_props[] =
682 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
683 /* Handle `face' before `display' because some sub-properties of
684 `display' need to know the face. */
685 {&Qface, FACE_PROP_IDX, handle_face_prop},
686 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
687 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
688 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
689 {NULL, 0, NULL}
692 /* Value is the position described by X. If X is a marker, value is
693 the marker_position of X. Otherwise, value is X. */
695 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
697 /* Enumeration returned by some move_it_.* functions internally. */
699 enum move_it_result
701 /* Not used. Undefined value. */
702 MOVE_UNDEFINED,
704 /* Move ended at the requested buffer position or ZV. */
705 MOVE_POS_MATCH_OR_ZV,
707 /* Move ended at the requested X pixel position. */
708 MOVE_X_REACHED,
710 /* Move within a line ended at the end of a line that must be
711 continued. */
712 MOVE_LINE_CONTINUED,
714 /* Move within a line ended at the end of a line that would
715 be displayed truncated. */
716 MOVE_LINE_TRUNCATED,
718 /* Move within a line ended at a line end. */
719 MOVE_NEWLINE_OR_CR
722 /* This counter is used to clear the face cache every once in a while
723 in redisplay_internal. It is incremented for each redisplay.
724 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
725 cleared. */
727 #define CLEAR_FACE_CACHE_COUNT 500
728 static int clear_face_cache_count;
730 /* Record the previous terminal frame we displayed. */
732 static struct frame *previous_terminal_frame;
734 /* Non-zero while redisplay_internal is in progress. */
736 int redisplaying_p;
738 /* Non-zero means don't free realized faces. Bound while freeing
739 realized faces is dangerous because glyph matrices might still
740 reference them. */
742 int inhibit_free_realized_faces;
743 Lisp_Object Qinhibit_free_realized_faces;
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 int help_echo_pos;
753 /* Temporary variable for XTread_socket. */
755 Lisp_Object previous_help_echo_string;
759 /* Function prototypes. */
761 static void setup_for_ellipsis P_ ((struct it *));
762 static void mark_window_display_accurate_1 P_ ((struct window *, int));
763 static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
764 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
765 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
766 static int redisplay_mode_lines P_ ((Lisp_Object, int));
767 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
769 #if 0
770 static int invisible_text_between_p P_ ((struct it *, int, int));
771 #endif
773 static int next_element_from_ellipsis P_ ((struct it *));
774 static void pint2str P_ ((char *, int, int));
775 static void pint2hrstr P_ ((char *, int, int));
776 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
777 struct text_pos));
778 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
779 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
780 static void store_frame_title_char P_ ((char));
781 static int store_frame_title P_ ((const unsigned char *, int, int));
782 static void x_consider_frame_title P_ ((Lisp_Object));
783 static void handle_stop P_ ((struct it *));
784 static int tool_bar_lines_needed P_ ((struct frame *));
785 static int single_display_prop_intangible_p P_ ((Lisp_Object));
786 static void ensure_echo_area_buffers P_ ((void));
787 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
788 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
789 static int with_echo_area_buffer P_ ((struct window *, int,
790 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
791 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
792 static void clear_garbaged_frames P_ ((void));
793 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
794 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
795 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
796 static int display_echo_area P_ ((struct window *));
797 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
798 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
799 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
800 static int string_char_and_length P_ ((const unsigned char *, int, int *));
801 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
802 struct text_pos));
803 static int compute_window_start_on_continuation_line P_ ((struct window *));
804 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
805 static void insert_left_trunc_glyphs P_ ((struct it *));
806 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
807 static void extend_face_to_end_of_line P_ ((struct it *));
808 static int append_space P_ ((struct it *, int));
809 static int make_cursor_line_fully_visible P_ ((struct window *));
810 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
811 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
812 static int trailing_whitespace_p P_ ((int));
813 static int message_log_check_duplicate P_ ((int, int, int, int));
814 static void push_it P_ ((struct it *));
815 static void pop_it P_ ((struct it *));
816 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
817 static void redisplay_internal P_ ((int));
818 static int echo_area_display P_ ((int));
819 static void redisplay_windows P_ ((Lisp_Object));
820 static void redisplay_window P_ ((Lisp_Object, int));
821 static Lisp_Object redisplay_window_error ();
822 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
823 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
824 static void update_menu_bar P_ ((struct frame *, int));
825 static int try_window_reusing_current_matrix P_ ((struct window *));
826 static int try_window_id P_ ((struct window *));
827 static int display_line P_ ((struct it *));
828 static int display_mode_lines P_ ((struct window *));
829 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
830 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
831 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
832 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
833 static void display_menu_bar P_ ((struct window *));
834 static int display_count_lines P_ ((int, int, int, int, int *));
835 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
836 int, int, struct it *, int, int, int, int));
837 static void compute_line_metrics P_ ((struct it *));
838 static void run_redisplay_end_trigger_hook P_ ((struct it *));
839 static int get_overlay_strings P_ ((struct it *, int));
840 static void next_overlay_string P_ ((struct it *));
841 static void reseat P_ ((struct it *, struct text_pos, int));
842 static void reseat_1 P_ ((struct it *, struct text_pos, int));
843 static void back_to_previous_visible_line_start P_ ((struct it *));
844 static void reseat_at_previous_visible_line_start P_ ((struct it *));
845 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
846 static int next_element_from_display_vector P_ ((struct it *));
847 static int next_element_from_string P_ ((struct it *));
848 static int next_element_from_c_string P_ ((struct it *));
849 static int next_element_from_buffer P_ ((struct it *));
850 static int next_element_from_composition P_ ((struct it *));
851 static int next_element_from_image P_ ((struct it *));
852 static int next_element_from_stretch P_ ((struct it *));
853 static void load_overlay_strings P_ ((struct it *, int));
854 static int init_from_display_pos P_ ((struct it *, struct window *,
855 struct display_pos *));
856 static void reseat_to_string P_ ((struct it *, unsigned char *,
857 Lisp_Object, int, int, int, int));
858 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
859 int, int, int));
860 void move_it_vertically_backward P_ ((struct it *, int));
861 static void init_to_row_start P_ ((struct it *, struct window *,
862 struct glyph_row *));
863 static int init_to_row_end P_ ((struct it *, struct window *,
864 struct glyph_row *));
865 static void back_to_previous_line_start P_ ((struct it *));
866 static int forward_to_next_line_start P_ ((struct it *, int *));
867 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
868 Lisp_Object, int));
869 static struct text_pos string_pos P_ ((int, Lisp_Object));
870 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
871 static int number_of_chars P_ ((unsigned char *, int));
872 static void compute_stop_pos P_ ((struct it *));
873 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
874 Lisp_Object));
875 static int face_before_or_after_it_pos P_ ((struct it *, int));
876 static int next_overlay_change P_ ((int));
877 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
878 Lisp_Object, struct text_pos *,
879 int));
880 static int underlying_face_id P_ ((struct it *));
881 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
882 struct window *));
884 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
885 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
887 #ifdef HAVE_WINDOW_SYSTEM
889 static void update_tool_bar P_ ((struct frame *, int));
890 static void build_desired_tool_bar_string P_ ((struct frame *f));
891 static int redisplay_tool_bar P_ ((struct frame *));
892 static void display_tool_bar_line P_ ((struct it *));
893 static void notice_overwritten_cursor P_ ((struct window *,
894 enum glyph_row_area,
895 int, int, int, int));
899 #endif /* HAVE_WINDOW_SYSTEM */
902 /***********************************************************************
903 Window display dimensions
904 ***********************************************************************/
906 /* Return the bottom boundary y-position for text lines in window W.
907 This is the first y position at which a line cannot start.
908 It is relative to the top of the window.
910 This is the height of W minus the height of a mode line, if any. */
912 INLINE int
913 window_text_bottom_y (w)
914 struct window *w;
916 int height = WINDOW_TOTAL_HEIGHT (w);
918 if (WINDOW_WANTS_MODELINE_P (w))
919 height -= CURRENT_MODE_LINE_HEIGHT (w);
920 return height;
923 /* Return the pixel width of display area AREA of window W. AREA < 0
924 means return the total width of W, not including fringes to
925 the left and right of the window. */
927 INLINE int
928 window_box_width (w, area)
929 struct window *w;
930 int area;
932 int cols = XFASTINT (w->total_cols);
933 int pixels = 0;
935 if (!w->pseudo_window_p)
937 cols -= WINDOW_SCROLL_BAR_COLS (w);
939 if (area == TEXT_AREA)
941 if (INTEGERP (w->left_margin_cols))
942 cols -= XFASTINT (w->left_margin_cols);
943 if (INTEGERP (w->right_margin_cols))
944 cols -= XFASTINT (w->right_margin_cols);
945 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
947 else if (area == LEFT_MARGIN_AREA)
949 cols = (INTEGERP (w->left_margin_cols)
950 ? XFASTINT (w->left_margin_cols) : 0);
951 pixels = 0;
953 else if (area == RIGHT_MARGIN_AREA)
955 cols = (INTEGERP (w->right_margin_cols)
956 ? XFASTINT (w->right_margin_cols) : 0);
957 pixels = 0;
961 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
965 /* Return the pixel height of the display area of window W, not
966 including mode lines of W, if any. */
968 INLINE int
969 window_box_height (w)
970 struct window *w;
972 struct frame *f = XFRAME (w->frame);
973 int height = WINDOW_TOTAL_HEIGHT (w);
975 xassert (height >= 0);
977 /* Note: the code below that determines the mode-line/header-line
978 height is essentially the same as that contained in the macro
979 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
980 the appropriate glyph row has its `mode_line_p' flag set,
981 and if it doesn't, uses estimate_mode_line_height instead. */
983 if (WINDOW_WANTS_MODELINE_P (w))
985 struct glyph_row *ml_row
986 = (w->current_matrix && w->current_matrix->rows
987 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
988 : 0);
989 if (ml_row && ml_row->mode_line_p)
990 height -= ml_row->height;
991 else
992 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
995 if (WINDOW_WANTS_HEADER_LINE_P (w))
997 struct glyph_row *hl_row
998 = (w->current_matrix && w->current_matrix->rows
999 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1000 : 0);
1001 if (hl_row && hl_row->mode_line_p)
1002 height -= hl_row->height;
1003 else
1004 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1007 /* With a very small font and a mode-line that's taller than
1008 default, we might end up with a negative height. */
1009 return max (0, height);
1012 /* Return the window-relative coordinate of the left edge of display
1013 area AREA of window W. AREA < 0 means return the left edge of the
1014 whole window, to the right of the left fringe of W. */
1016 INLINE int
1017 window_box_left_offset (w, area)
1018 struct window *w;
1019 int area;
1021 int x;
1023 if (w->pseudo_window_p)
1024 return 0;
1026 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1028 if (area == TEXT_AREA)
1029 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1030 + window_box_width (w, LEFT_MARGIN_AREA));
1031 else if (area == RIGHT_MARGIN_AREA)
1032 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1033 + window_box_width (w, LEFT_MARGIN_AREA)
1034 + window_box_width (w, TEXT_AREA)
1035 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1037 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1038 else if (area == LEFT_MARGIN_AREA
1039 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1040 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1042 return x;
1046 /* Return the window-relative coordinate of the right edge of display
1047 area AREA of window W. AREA < 0 means return the left edge of the
1048 whole window, to the left of the right fringe of W. */
1050 INLINE int
1051 window_box_right_offset (w, area)
1052 struct window *w;
1053 int area;
1055 return window_box_left_offset (w, area) + window_box_width (w, area);
1058 /* Return the frame-relative coordinate of the left edge of display
1059 area AREA of window W. AREA < 0 means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1062 INLINE int
1063 window_box_left (w, area)
1064 struct window *w;
1065 int area;
1067 struct frame *f = XFRAME (w->frame);
1068 int x;
1070 if (w->pseudo_window_p)
1071 return FRAME_INTERNAL_BORDER_WIDTH (f);
1073 x = (WINDOW_LEFT_EDGE_X (w)
1074 + window_box_left_offset (w, area));
1076 return x;
1080 /* Return the frame-relative coordinate of the right edge of display
1081 area AREA of window W. AREA < 0 means return the left edge of the
1082 whole window, to the left of the right fringe of W. */
1084 INLINE int
1085 window_box_right (w, area)
1086 struct window *w;
1087 int area;
1089 return window_box_left (w, area) + window_box_width (w, area);
1092 /* Get the bounding box of the display area AREA of window W, without
1093 mode lines, in frame-relative coordinates. AREA < 0 means the
1094 whole window, not including the left and right fringes of
1095 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1096 coordinates of the upper-left corner of the box. Return in
1097 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1099 INLINE void
1100 window_box (w, area, box_x, box_y, box_width, box_height)
1101 struct window *w;
1102 int area;
1103 int *box_x, *box_y, *box_width, *box_height;
1105 if (box_width)
1106 *box_width = window_box_width (w, area);
1107 if (box_height)
1108 *box_height = window_box_height (w);
1109 if (box_x)
1110 *box_x = window_box_left (w, area);
1111 if (box_y)
1113 *box_y = WINDOW_TOP_EDGE_Y (w);
1114 if (WINDOW_WANTS_HEADER_LINE_P (w))
1115 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1120 /* Get the bounding box of the display area AREA of window W, without
1121 mode lines. AREA < 0 means the whole window, not including the
1122 left and right fringe of the window. Return in *TOP_LEFT_X
1123 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1124 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1125 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1126 box. */
1128 INLINE void
1129 window_box_edges (w, area, top_left_x, top_left_y,
1130 bottom_right_x, bottom_right_y)
1131 struct window *w;
1132 int area;
1133 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1135 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1136 bottom_right_y);
1137 *bottom_right_x += *top_left_x;
1138 *bottom_right_y += *top_left_y;
1143 /***********************************************************************
1144 Utilities
1145 ***********************************************************************/
1147 /* Return the bottom y-position of the line the iterator IT is in.
1148 This can modify IT's settings. */
1151 line_bottom_y (it)
1152 struct it *it;
1154 int line_height = it->max_ascent + it->max_descent;
1155 int line_top_y = it->current_y;
1157 if (line_height == 0)
1159 if (last_height)
1160 line_height = last_height;
1161 else if (IT_CHARPOS (*it) < ZV)
1163 move_it_by_lines (it, 1, 1);
1164 line_height = (it->max_ascent || it->max_descent
1165 ? it->max_ascent + it->max_descent
1166 : last_height);
1168 else
1170 struct glyph_row *row = it->glyph_row;
1172 /* Use the default character height. */
1173 it->glyph_row = NULL;
1174 it->what = IT_CHARACTER;
1175 it->c = ' ';
1176 it->len = 1;
1177 PRODUCE_GLYPHS (it);
1178 line_height = it->ascent + it->descent;
1179 it->glyph_row = row;
1183 return line_top_y + line_height;
1187 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1188 1 if POS is visible and the line containing POS is fully visible.
1189 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1190 and header-lines heights. */
1193 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1194 struct window *w;
1195 int charpos, *fully, exact_mode_line_heights_p;
1197 struct it it;
1198 struct text_pos top;
1199 int visible_p;
1200 struct buffer *old_buffer = NULL;
1202 if (XBUFFER (w->buffer) != current_buffer)
1204 old_buffer = current_buffer;
1205 set_buffer_internal_1 (XBUFFER (w->buffer));
1208 *fully = visible_p = 0;
1209 SET_TEXT_POS_FROM_MARKER (top, w->start);
1211 /* Compute exact mode line heights, if requested. */
1212 if (exact_mode_line_heights_p)
1214 if (WINDOW_WANTS_MODELINE_P (w))
1215 current_mode_line_height
1216 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1217 current_buffer->mode_line_format);
1219 if (WINDOW_WANTS_HEADER_LINE_P (w))
1220 current_header_line_height
1221 = display_mode_line (w, HEADER_LINE_FACE_ID,
1222 current_buffer->header_line_format);
1225 start_display (&it, w, top);
1226 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1227 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1229 /* Note that we may overshoot because of invisible text. */
1230 if (IT_CHARPOS (it) >= charpos)
1232 int top_y = it.current_y;
1233 int bottom_y = line_bottom_y (&it);
1234 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1236 if (top_y < window_top_y)
1237 visible_p = bottom_y > window_top_y;
1238 else if (top_y < it.last_visible_y)
1240 visible_p = 1;
1241 *fully = bottom_y <= it.last_visible_y;
1244 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1246 move_it_by_lines (&it, 1, 0);
1247 if (charpos < IT_CHARPOS (it))
1249 visible_p = 1;
1250 *fully = 0;
1254 if (old_buffer)
1255 set_buffer_internal_1 (old_buffer);
1257 current_header_line_height = current_mode_line_height = -1;
1258 return visible_p;
1262 /* Return the next character from STR which is MAXLEN bytes long.
1263 Return in *LEN the length of the character. This is like
1264 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1265 we find one, we return a `?', but with the length of the invalid
1266 character. */
1268 static INLINE int
1269 string_char_and_length (str, maxlen, len)
1270 const unsigned char *str;
1271 int maxlen, *len;
1273 int c;
1275 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1276 if (!CHAR_VALID_P (c, 1))
1277 /* We may not change the length here because other places in Emacs
1278 don't use this function, i.e. they silently accept invalid
1279 characters. */
1280 c = '?';
1282 return c;
1287 /* Given a position POS containing a valid character and byte position
1288 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1290 static struct text_pos
1291 string_pos_nchars_ahead (pos, string, nchars)
1292 struct text_pos pos;
1293 Lisp_Object string;
1294 int nchars;
1296 xassert (STRINGP (string) && nchars >= 0);
1298 if (STRING_MULTIBYTE (string))
1300 int rest = SBYTES (string) - BYTEPOS (pos);
1301 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1302 int len;
1304 while (nchars--)
1306 string_char_and_length (p, rest, &len);
1307 p += len, rest -= len;
1308 xassert (rest >= 0);
1309 CHARPOS (pos) += 1;
1310 BYTEPOS (pos) += len;
1313 else
1314 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1316 return pos;
1320 /* Value is the text position, i.e. character and byte position,
1321 for character position CHARPOS in STRING. */
1323 static INLINE struct text_pos
1324 string_pos (charpos, string)
1325 int charpos;
1326 Lisp_Object string;
1328 struct text_pos pos;
1329 xassert (STRINGP (string));
1330 xassert (charpos >= 0);
1331 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1332 return pos;
1336 /* Value is a text position, i.e. character and byte position, for
1337 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1338 means recognize multibyte characters. */
1340 static struct text_pos
1341 c_string_pos (charpos, s, multibyte_p)
1342 int charpos;
1343 unsigned char *s;
1344 int multibyte_p;
1346 struct text_pos pos;
1348 xassert (s != NULL);
1349 xassert (charpos >= 0);
1351 if (multibyte_p)
1353 int rest = strlen (s), len;
1355 SET_TEXT_POS (pos, 0, 0);
1356 while (charpos--)
1358 string_char_and_length (s, rest, &len);
1359 s += len, rest -= len;
1360 xassert (rest >= 0);
1361 CHARPOS (pos) += 1;
1362 BYTEPOS (pos) += len;
1365 else
1366 SET_TEXT_POS (pos, charpos, charpos);
1368 return pos;
1372 /* Value is the number of characters in C string S. MULTIBYTE_P
1373 non-zero means recognize multibyte characters. */
1375 static int
1376 number_of_chars (s, multibyte_p)
1377 unsigned char *s;
1378 int multibyte_p;
1380 int nchars;
1382 if (multibyte_p)
1384 int rest = strlen (s), len;
1385 unsigned char *p = (unsigned char *) s;
1387 for (nchars = 0; rest > 0; ++nchars)
1389 string_char_and_length (p, rest, &len);
1390 rest -= len, p += len;
1393 else
1394 nchars = strlen (s);
1396 return nchars;
1400 /* Compute byte position NEWPOS->bytepos corresponding to
1401 NEWPOS->charpos. POS is a known position in string STRING.
1402 NEWPOS->charpos must be >= POS.charpos. */
1404 static void
1405 compute_string_pos (newpos, pos, string)
1406 struct text_pos *newpos, pos;
1407 Lisp_Object string;
1409 xassert (STRINGP (string));
1410 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1412 if (STRING_MULTIBYTE (string))
1413 *newpos = string_pos_nchars_ahead (pos, string,
1414 CHARPOS (*newpos) - CHARPOS (pos));
1415 else
1416 BYTEPOS (*newpos) = CHARPOS (*newpos);
1419 /* EXPORT:
1420 Return an estimation of the pixel height of mode or top lines on
1421 frame F. FACE_ID specifies what line's height to estimate. */
1424 estimate_mode_line_height (f, face_id)
1425 struct frame *f;
1426 enum face_id face_id;
1428 #ifdef HAVE_WINDOW_SYSTEM
1429 if (FRAME_WINDOW_P (f))
1431 int height = FONT_HEIGHT (FRAME_FONT (f));
1433 /* This function is called so early when Emacs starts that the face
1434 cache and mode line face are not yet initialized. */
1435 if (FRAME_FACE_CACHE (f))
1437 struct face *face = FACE_FROM_ID (f, face_id);
1438 if (face)
1440 if (face->font)
1441 height = FONT_HEIGHT (face->font);
1442 if (face->box_line_width > 0)
1443 height += 2 * face->box_line_width;
1447 return height;
1449 #endif
1451 return 1;
1454 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1455 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1456 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1457 not force the value into range. */
1459 void
1460 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1461 FRAME_PTR f;
1462 register int pix_x, pix_y;
1463 int *x, *y;
1464 NativeRectangle *bounds;
1465 int noclip;
1468 #ifdef HAVE_WINDOW_SYSTEM
1469 if (FRAME_WINDOW_P (f))
1471 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1472 even for negative values. */
1473 if (pix_x < 0)
1474 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1475 if (pix_y < 0)
1476 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1478 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1479 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1481 if (bounds)
1482 STORE_NATIVE_RECT (*bounds,
1483 FRAME_COL_TO_PIXEL_X (f, pix_x),
1484 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1485 FRAME_COLUMN_WIDTH (f) - 1,
1486 FRAME_LINE_HEIGHT (f) - 1);
1488 if (!noclip)
1490 if (pix_x < 0)
1491 pix_x = 0;
1492 else if (pix_x > FRAME_TOTAL_COLS (f))
1493 pix_x = FRAME_TOTAL_COLS (f);
1495 if (pix_y < 0)
1496 pix_y = 0;
1497 else if (pix_y > FRAME_LINES (f))
1498 pix_y = FRAME_LINES (f);
1501 #endif
1503 *x = pix_x;
1504 *y = pix_y;
1508 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1509 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1510 can't tell the positions because W's display is not up to date,
1511 return 0. */
1514 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1515 struct window *w;
1516 int hpos, vpos;
1517 int *frame_x, *frame_y;
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1522 int success_p;
1524 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1525 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1527 if (display_completed)
1529 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1530 struct glyph *glyph = row->glyphs[TEXT_AREA];
1531 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1533 hpos = row->x;
1534 vpos = row->y;
1535 while (glyph < end)
1537 hpos += glyph->pixel_width;
1538 ++glyph;
1541 success_p = 1;
1543 else
1545 hpos = vpos = 0;
1546 success_p = 0;
1549 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1550 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1551 return success_p;
1553 #endif
1555 *frame_x = hpos;
1556 *frame_y = vpos;
1557 return 1;
1561 #ifdef HAVE_WINDOW_SYSTEM
1563 /* Find the glyph under window-relative coordinates X/Y in window W.
1564 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1565 strings. Return in *HPOS and *VPOS the row and column number of
1566 the glyph found. Return in *AREA the glyph area containing X.
1567 Value is a pointer to the glyph found or null if X/Y is not on
1568 text, or we can't tell because W's current matrix is not up to
1569 date. */
1571 static struct glyph *
1572 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1573 struct window *w;
1574 int x, y;
1575 int *hpos, *vpos, *area;
1576 int buffer_only_p;
1578 struct glyph *glyph, *end;
1579 struct glyph_row *row = NULL;
1580 int x0, i;
1582 /* Find row containing Y. Give up if some row is not enabled. */
1583 for (i = 0; i < w->current_matrix->nrows; ++i)
1585 row = MATRIX_ROW (w->current_matrix, i);
1586 if (!row->enabled_p)
1587 return NULL;
1588 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1589 break;
1592 *vpos = i;
1593 *hpos = 0;
1595 /* Give up if Y is not in the window. */
1596 if (i == w->current_matrix->nrows)
1597 return NULL;
1599 /* Get the glyph area containing X. */
1600 if (w->pseudo_window_p)
1602 *area = TEXT_AREA;
1603 x0 = 0;
1605 else
1607 if (x < window_box_left_offset (w, TEXT_AREA))
1609 *area = LEFT_MARGIN_AREA;
1610 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1612 else if (x < window_box_right_offset (w, TEXT_AREA))
1614 *area = TEXT_AREA;
1615 x0 = window_box_left_offset (w, TEXT_AREA);
1617 else
1619 *area = RIGHT_MARGIN_AREA;
1620 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1624 /* Find glyph containing X. */
1625 glyph = row->glyphs[*area];
1626 end = glyph + row->used[*area];
1627 while (glyph < end)
1629 if (x < x0 + glyph->pixel_width)
1631 if (w->pseudo_window_p)
1632 break;
1633 else if (!buffer_only_p || BUFFERP (glyph->object))
1634 break;
1637 x0 += glyph->pixel_width;
1638 ++glyph;
1641 if (glyph == end)
1642 return NULL;
1644 *hpos = glyph - row->glyphs[*area];
1645 return glyph;
1649 /* EXPORT:
1650 Convert frame-relative x/y to coordinates relative to window W.
1651 Takes pseudo-windows into account. */
1653 void
1654 frame_to_window_pixel_xy (w, x, y)
1655 struct window *w;
1656 int *x, *y;
1658 if (w->pseudo_window_p)
1660 /* A pseudo-window is always full-width, and starts at the
1661 left edge of the frame, plus a frame border. */
1662 struct frame *f = XFRAME (w->frame);
1663 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1664 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1666 else
1668 *x -= WINDOW_LEFT_EDGE_X (w);
1669 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1673 /* EXPORT:
1674 Return in *R the clipping rectangle for glyph string S. */
1676 void
1677 get_glyph_string_clip_rect (s, nr)
1678 struct glyph_string *s;
1679 NativeRectangle *nr;
1681 XRectangle r;
1683 if (s->row->full_width_p)
1685 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1686 r.x = WINDOW_LEFT_EDGE_X (s->w);
1687 r.width = WINDOW_TOTAL_WIDTH (s->w);
1689 /* Unless displaying a mode or menu bar line, which are always
1690 fully visible, clip to the visible part of the row. */
1691 if (s->w->pseudo_window_p)
1692 r.height = s->row->visible_height;
1693 else
1694 r.height = s->height;
1696 else
1698 /* This is a text line that may be partially visible. */
1699 r.x = window_box_left (s->w, s->area);
1700 r.width = window_box_width (s->w, s->area);
1701 r.height = s->row->visible_height;
1704 /* If S draws overlapping rows, it's sufficient to use the top and
1705 bottom of the window for clipping because this glyph string
1706 intentionally draws over other lines. */
1707 if (s->for_overlaps_p)
1709 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1710 r.height = window_text_bottom_y (s->w) - r.y;
1712 else
1714 /* Don't use S->y for clipping because it doesn't take partially
1715 visible lines into account. For example, it can be negative for
1716 partially visible lines at the top of a window. */
1717 if (!s->row->full_width_p
1718 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1719 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1720 else
1721 r.y = max (0, s->row->y);
1723 /* If drawing a tool-bar window, draw it over the internal border
1724 at the top of the window. */
1725 if (s->w == XWINDOW (s->f->tool_bar_window))
1726 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1729 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1731 #ifdef HAVE_NTGUI
1732 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1733 /* If drawing the cursor, don't let glyph draw outside its
1734 advertised boundaries. Cleartype does this under some circumstances. */
1735 if (s->hl == DRAW_CURSOR)
1737 if (s->x > r.x)
1739 r.width -= s->x - r.x;
1740 r.x = s->x;
1742 r.width = min (r.width, s->first_glyph->pixel_width);
1744 #endif
1746 #ifdef CONVERT_FROM_XRECT
1747 CONVERT_FROM_XRECT (r, *nr);
1748 #else
1749 *nr = r;
1750 #endif
1753 #endif /* HAVE_WINDOW_SYSTEM */
1756 /***********************************************************************
1757 Lisp form evaluation
1758 ***********************************************************************/
1760 /* Error handler for safe_eval and safe_call. */
1762 static Lisp_Object
1763 safe_eval_handler (arg)
1764 Lisp_Object arg;
1766 add_to_log ("Error during redisplay: %s", arg, Qnil);
1767 return Qnil;
1771 /* Evaluate SEXPR and return the result, or nil if something went
1772 wrong. Prevent redisplay during the evaluation. */
1774 Lisp_Object
1775 safe_eval (sexpr)
1776 Lisp_Object sexpr;
1778 Lisp_Object val;
1780 if (inhibit_eval_during_redisplay)
1781 val = Qnil;
1782 else
1784 int count = SPECPDL_INDEX ();
1785 struct gcpro gcpro1;
1787 GCPRO1 (sexpr);
1788 specbind (Qinhibit_redisplay, Qt);
1789 /* Use Qt to ensure debugger does not run,
1790 so there is no possibility of wanting to redisplay. */
1791 val = internal_condition_case_1 (Feval, sexpr, Qt,
1792 safe_eval_handler);
1793 UNGCPRO;
1794 val = unbind_to (count, val);
1797 return val;
1801 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1802 Return the result, or nil if something went wrong. Prevent
1803 redisplay during the evaluation. */
1805 Lisp_Object
1806 safe_call (nargs, args)
1807 int nargs;
1808 Lisp_Object *args;
1810 Lisp_Object val;
1812 if (inhibit_eval_during_redisplay)
1813 val = Qnil;
1814 else
1816 int count = SPECPDL_INDEX ();
1817 struct gcpro gcpro1;
1819 GCPRO1 (args[0]);
1820 gcpro1.nvars = nargs;
1821 specbind (Qinhibit_redisplay, Qt);
1822 /* Use Qt to ensure debugger does not run,
1823 so there is no possibility of wanting to redisplay. */
1824 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1825 safe_eval_handler);
1826 UNGCPRO;
1827 val = unbind_to (count, val);
1830 return val;
1834 /* Call function FN with one argument ARG.
1835 Return the result, or nil if something went wrong. */
1837 Lisp_Object
1838 safe_call1 (fn, arg)
1839 Lisp_Object fn, arg;
1841 Lisp_Object args[2];
1842 args[0] = fn;
1843 args[1] = arg;
1844 return safe_call (2, args);
1849 /***********************************************************************
1850 Debugging
1851 ***********************************************************************/
1853 #if 0
1855 /* Define CHECK_IT to perform sanity checks on iterators.
1856 This is for debugging. It is too slow to do unconditionally. */
1858 static void
1859 check_it (it)
1860 struct it *it;
1862 if (it->method == next_element_from_string)
1864 xassert (STRINGP (it->string));
1865 xassert (IT_STRING_CHARPOS (*it) >= 0);
1867 else if (it->method == next_element_from_buffer)
1869 /* Check that character and byte positions agree. */
1870 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1873 if (it->dpvec)
1874 xassert (it->current.dpvec_index >= 0);
1875 else
1876 xassert (it->current.dpvec_index < 0);
1879 #define CHECK_IT(IT) check_it ((IT))
1881 #else /* not 0 */
1883 #define CHECK_IT(IT) (void) 0
1885 #endif /* not 0 */
1888 #if GLYPH_DEBUG
1890 /* Check that the window end of window W is what we expect it
1891 to be---the last row in the current matrix displaying text. */
1893 static void
1894 check_window_end (w)
1895 struct window *w;
1897 if (!MINI_WINDOW_P (w)
1898 && !NILP (w->window_end_valid))
1900 struct glyph_row *row;
1901 xassert ((row = MATRIX_ROW (w->current_matrix,
1902 XFASTINT (w->window_end_vpos)),
1903 !row->enabled_p
1904 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1905 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1909 #define CHECK_WINDOW_END(W) check_window_end ((W))
1911 #else /* not GLYPH_DEBUG */
1913 #define CHECK_WINDOW_END(W) (void) 0
1915 #endif /* not GLYPH_DEBUG */
1919 /***********************************************************************
1920 Iterator initialization
1921 ***********************************************************************/
1923 /* Initialize IT for displaying current_buffer in window W, starting
1924 at character position CHARPOS. CHARPOS < 0 means that no buffer
1925 position is specified which is useful when the iterator is assigned
1926 a position later. BYTEPOS is the byte position corresponding to
1927 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1929 If ROW is not null, calls to produce_glyphs with IT as parameter
1930 will produce glyphs in that row.
1932 BASE_FACE_ID is the id of a base face to use. It must be one of
1933 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1934 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1935 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1937 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1938 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1939 will be initialized to use the corresponding mode line glyph row of
1940 the desired matrix of W. */
1942 void
1943 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1944 struct it *it;
1945 struct window *w;
1946 int charpos, bytepos;
1947 struct glyph_row *row;
1948 enum face_id base_face_id;
1950 int highlight_region_p;
1952 /* Some precondition checks. */
1953 xassert (w != NULL && it != NULL);
1954 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1955 && charpos <= ZV));
1957 /* If face attributes have been changed since the last redisplay,
1958 free realized faces now because they depend on face definitions
1959 that might have changed. Don't free faces while there might be
1960 desired matrices pending which reference these faces. */
1961 if (face_change_count && !inhibit_free_realized_faces)
1963 face_change_count = 0;
1964 free_all_realized_faces (Qnil);
1967 /* Use one of the mode line rows of W's desired matrix if
1968 appropriate. */
1969 if (row == NULL)
1971 if (base_face_id == MODE_LINE_FACE_ID
1972 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1973 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1974 else if (base_face_id == HEADER_LINE_FACE_ID)
1975 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1978 /* Clear IT. */
1979 bzero (it, sizeof *it);
1980 it->current.overlay_string_index = -1;
1981 it->current.dpvec_index = -1;
1982 it->base_face_id = base_face_id;
1984 /* The window in which we iterate over current_buffer: */
1985 XSETWINDOW (it->window, w);
1986 it->w = w;
1987 it->f = XFRAME (w->frame);
1989 /* Extra space between lines (on window systems only). */
1990 if (base_face_id == DEFAULT_FACE_ID
1991 && FRAME_WINDOW_P (it->f))
1993 if (NATNUMP (current_buffer->extra_line_spacing))
1994 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1995 else if (it->f->extra_line_spacing > 0)
1996 it->extra_line_spacing = it->f->extra_line_spacing;
1999 /* If realized faces have been removed, e.g. because of face
2000 attribute changes of named faces, recompute them. When running
2001 in batch mode, the face cache of Vterminal_frame is null. If
2002 we happen to get called, make a dummy face cache. */
2003 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2004 init_frame_faces (it->f);
2005 if (FRAME_FACE_CACHE (it->f)->used == 0)
2006 recompute_basic_faces (it->f);
2008 /* Current value of the `space-width', and 'height' properties. */
2009 it->space_width = Qnil;
2010 it->font_height = Qnil;
2012 /* Are control characters displayed as `^C'? */
2013 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2015 /* -1 means everything between a CR and the following line end
2016 is invisible. >0 means lines indented more than this value are
2017 invisible. */
2018 it->selective = (INTEGERP (current_buffer->selective_display)
2019 ? XFASTINT (current_buffer->selective_display)
2020 : (!NILP (current_buffer->selective_display)
2021 ? -1 : 0));
2022 it->selective_display_ellipsis_p
2023 = !NILP (current_buffer->selective_display_ellipses);
2025 /* Display table to use. */
2026 it->dp = window_display_table (w);
2028 /* Are multibyte characters enabled in current_buffer? */
2029 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2031 /* Non-zero if we should highlight the region. */
2032 highlight_region_p
2033 = (!NILP (Vtransient_mark_mode)
2034 && !NILP (current_buffer->mark_active)
2035 && XMARKER (current_buffer->mark)->buffer != 0);
2037 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2038 start and end of a visible region in window IT->w. Set both to
2039 -1 to indicate no region. */
2040 if (highlight_region_p
2041 /* Maybe highlight only in selected window. */
2042 && (/* Either show region everywhere. */
2043 highlight_nonselected_windows
2044 /* Or show region in the selected window. */
2045 || w == XWINDOW (selected_window)
2046 /* Or show the region if we are in the mini-buffer and W is
2047 the window the mini-buffer refers to. */
2048 || (MINI_WINDOW_P (XWINDOW (selected_window))
2049 && WINDOWP (minibuf_selected_window)
2050 && w == XWINDOW (minibuf_selected_window))))
2052 int charpos = marker_position (current_buffer->mark);
2053 it->region_beg_charpos = min (PT, charpos);
2054 it->region_end_charpos = max (PT, charpos);
2056 else
2057 it->region_beg_charpos = it->region_end_charpos = -1;
2059 /* Get the position at which the redisplay_end_trigger hook should
2060 be run, if it is to be run at all. */
2061 if (MARKERP (w->redisplay_end_trigger)
2062 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2063 it->redisplay_end_trigger_charpos
2064 = marker_position (w->redisplay_end_trigger);
2065 else if (INTEGERP (w->redisplay_end_trigger))
2066 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2068 /* Correct bogus values of tab_width. */
2069 it->tab_width = XINT (current_buffer->tab_width);
2070 if (it->tab_width <= 0 || it->tab_width > 1000)
2071 it->tab_width = 8;
2073 /* Are lines in the display truncated? */
2074 it->truncate_lines_p
2075 = (base_face_id != DEFAULT_FACE_ID
2076 || XINT (it->w->hscroll)
2077 || (truncate_partial_width_windows
2078 && !WINDOW_FULL_WIDTH_P (it->w))
2079 || !NILP (current_buffer->truncate_lines));
2081 /* Get dimensions of truncation and continuation glyphs. These are
2082 displayed as fringe bitmaps under X, so we don't need them for such
2083 frames. */
2084 if (!FRAME_WINDOW_P (it->f))
2086 if (it->truncate_lines_p)
2088 /* We will need the truncation glyph. */
2089 xassert (it->glyph_row == NULL);
2090 produce_special_glyphs (it, IT_TRUNCATION);
2091 it->truncation_pixel_width = it->pixel_width;
2093 else
2095 /* We will need the continuation glyph. */
2096 xassert (it->glyph_row == NULL);
2097 produce_special_glyphs (it, IT_CONTINUATION);
2098 it->continuation_pixel_width = it->pixel_width;
2101 /* Reset these values to zero because the produce_special_glyphs
2102 above has changed them. */
2103 it->pixel_width = it->ascent = it->descent = 0;
2104 it->phys_ascent = it->phys_descent = 0;
2107 /* Set this after getting the dimensions of truncation and
2108 continuation glyphs, so that we don't produce glyphs when calling
2109 produce_special_glyphs, above. */
2110 it->glyph_row = row;
2111 it->area = TEXT_AREA;
2113 /* Get the dimensions of the display area. The display area
2114 consists of the visible window area plus a horizontally scrolled
2115 part to the left of the window. All x-values are relative to the
2116 start of this total display area. */
2117 if (base_face_id != DEFAULT_FACE_ID)
2119 /* Mode lines, menu bar in terminal frames. */
2120 it->first_visible_x = 0;
2121 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2123 else
2125 it->first_visible_x
2126 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2127 it->last_visible_x = (it->first_visible_x
2128 + window_box_width (w, TEXT_AREA));
2130 /* If we truncate lines, leave room for the truncator glyph(s) at
2131 the right margin. Otherwise, leave room for the continuation
2132 glyph(s). Truncation and continuation glyphs are not inserted
2133 for window-based redisplay. */
2134 if (!FRAME_WINDOW_P (it->f))
2136 if (it->truncate_lines_p)
2137 it->last_visible_x -= it->truncation_pixel_width;
2138 else
2139 it->last_visible_x -= it->continuation_pixel_width;
2142 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2143 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2146 /* Leave room for a border glyph. */
2147 if (!FRAME_WINDOW_P (it->f)
2148 && !WINDOW_RIGHTMOST_P (it->w))
2149 it->last_visible_x -= 1;
2151 it->last_visible_y = window_text_bottom_y (w);
2153 /* For mode lines and alike, arrange for the first glyph having a
2154 left box line if the face specifies a box. */
2155 if (base_face_id != DEFAULT_FACE_ID)
2157 struct face *face;
2159 it->face_id = base_face_id;
2161 /* If we have a boxed mode line, make the first character appear
2162 with a left box line. */
2163 face = FACE_FROM_ID (it->f, base_face_id);
2164 if (face->box != FACE_NO_BOX)
2165 it->start_of_box_run_p = 1;
2168 /* If a buffer position was specified, set the iterator there,
2169 getting overlays and face properties from that position. */
2170 if (charpos >= BUF_BEG (current_buffer))
2172 it->end_charpos = ZV;
2173 it->face_id = -1;
2174 IT_CHARPOS (*it) = charpos;
2176 /* Compute byte position if not specified. */
2177 if (bytepos < charpos)
2178 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2179 else
2180 IT_BYTEPOS (*it) = bytepos;
2182 /* Compute faces etc. */
2183 reseat (it, it->current.pos, 1);
2186 CHECK_IT (it);
2190 /* Initialize IT for the display of window W with window start POS. */
2192 void
2193 start_display (it, w, pos)
2194 struct it *it;
2195 struct window *w;
2196 struct text_pos pos;
2198 struct glyph_row *row;
2199 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2201 row = w->desired_matrix->rows + first_vpos;
2202 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2204 if (!it->truncate_lines_p)
2206 int start_at_line_beg_p;
2207 int first_y = it->current_y;
2209 /* If window start is not at a line start, skip forward to POS to
2210 get the correct continuation lines width. */
2211 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2212 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2213 if (!start_at_line_beg_p)
2215 int new_x;
2217 reseat_at_previous_visible_line_start (it);
2218 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2220 new_x = it->current_x + it->pixel_width;
2222 /* If lines are continued, this line may end in the middle
2223 of a multi-glyph character (e.g. a control character
2224 displayed as \003, or in the middle of an overlay
2225 string). In this case move_it_to above will not have
2226 taken us to the start of the continuation line but to the
2227 end of the continued line. */
2228 if (it->current_x > 0
2229 && !it->truncate_lines_p /* Lines are continued. */
2230 && (/* And glyph doesn't fit on the line. */
2231 new_x > it->last_visible_x
2232 /* Or it fits exactly and we're on a window
2233 system frame. */
2234 || (new_x == it->last_visible_x
2235 && FRAME_WINDOW_P (it->f))))
2237 if (it->current.dpvec_index >= 0
2238 || it->current.overlay_string_index >= 0)
2240 set_iterator_to_next (it, 1);
2241 move_it_in_display_line_to (it, -1, -1, 0);
2244 it->continuation_lines_width += it->current_x;
2247 /* We're starting a new display line, not affected by the
2248 height of the continued line, so clear the appropriate
2249 fields in the iterator structure. */
2250 it->max_ascent = it->max_descent = 0;
2251 it->max_phys_ascent = it->max_phys_descent = 0;
2253 it->current_y = first_y;
2254 it->vpos = 0;
2255 it->current_x = it->hpos = 0;
2259 #if 0 /* Don't assert the following because start_display is sometimes
2260 called intentionally with a window start that is not at a
2261 line start. Please leave this code in as a comment. */
2263 /* Window start should be on a line start, now. */
2264 xassert (it->continuation_lines_width
2265 || IT_CHARPOS (it) == BEGV
2266 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2267 #endif /* 0 */
2271 /* Return 1 if POS is a position in ellipses displayed for invisible
2272 text. W is the window we display, for text property lookup. */
2274 static int
2275 in_ellipses_for_invisible_text_p (pos, w)
2276 struct display_pos *pos;
2277 struct window *w;
2279 Lisp_Object prop, window;
2280 int ellipses_p = 0;
2281 int charpos = CHARPOS (pos->pos);
2283 /* If POS specifies a position in a display vector, this might
2284 be for an ellipsis displayed for invisible text. We won't
2285 get the iterator set up for delivering that ellipsis unless
2286 we make sure that it gets aware of the invisible text. */
2287 if (pos->dpvec_index >= 0
2288 && pos->overlay_string_index < 0
2289 && CHARPOS (pos->string_pos) < 0
2290 && charpos > BEGV
2291 && (XSETWINDOW (window, w),
2292 prop = Fget_char_property (make_number (charpos),
2293 Qinvisible, window),
2294 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2296 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2297 window);
2298 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2301 return ellipses_p;
2305 /* Initialize IT for stepping through current_buffer in window W,
2306 starting at position POS that includes overlay string and display
2307 vector/ control character translation position information. Value
2308 is zero if there are overlay strings with newlines at POS. */
2310 static int
2311 init_from_display_pos (it, w, pos)
2312 struct it *it;
2313 struct window *w;
2314 struct display_pos *pos;
2316 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2317 int i, overlay_strings_with_newlines = 0;
2319 /* If POS specifies a position in a display vector, this might
2320 be for an ellipsis displayed for invisible text. We won't
2321 get the iterator set up for delivering that ellipsis unless
2322 we make sure that it gets aware of the invisible text. */
2323 if (in_ellipses_for_invisible_text_p (pos, w))
2325 --charpos;
2326 bytepos = 0;
2329 /* Keep in mind: the call to reseat in init_iterator skips invisible
2330 text, so we might end up at a position different from POS. This
2331 is only a problem when POS is a row start after a newline and an
2332 overlay starts there with an after-string, and the overlay has an
2333 invisible property. Since we don't skip invisible text in
2334 display_line and elsewhere immediately after consuming the
2335 newline before the row start, such a POS will not be in a string,
2336 but the call to init_iterator below will move us to the
2337 after-string. */
2338 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2340 for (i = 0; i < it->n_overlay_strings; ++i)
2342 const char *s = SDATA (it->overlay_strings[i]);
2343 const char *e = s + SBYTES (it->overlay_strings[i]);
2345 while (s < e && *s != '\n')
2346 ++s;
2348 if (s < e)
2350 overlay_strings_with_newlines = 1;
2351 break;
2355 /* If position is within an overlay string, set up IT to the right
2356 overlay string. */
2357 if (pos->overlay_string_index >= 0)
2359 int relative_index;
2361 /* If the first overlay string happens to have a `display'
2362 property for an image, the iterator will be set up for that
2363 image, and we have to undo that setup first before we can
2364 correct the overlay string index. */
2365 if (it->method == next_element_from_image)
2366 pop_it (it);
2368 /* We already have the first chunk of overlay strings in
2369 IT->overlay_strings. Load more until the one for
2370 pos->overlay_string_index is in IT->overlay_strings. */
2371 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2373 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2374 it->current.overlay_string_index = 0;
2375 while (n--)
2377 load_overlay_strings (it, 0);
2378 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2382 it->current.overlay_string_index = pos->overlay_string_index;
2383 relative_index = (it->current.overlay_string_index
2384 % OVERLAY_STRING_CHUNK_SIZE);
2385 it->string = it->overlay_strings[relative_index];
2386 xassert (STRINGP (it->string));
2387 it->current.string_pos = pos->string_pos;
2388 it->method = next_element_from_string;
2391 #if 0 /* This is bogus because POS not having an overlay string
2392 position does not mean it's after the string. Example: A
2393 line starting with a before-string and initialization of IT
2394 to the previous row's end position. */
2395 else if (it->current.overlay_string_index >= 0)
2397 /* If POS says we're already after an overlay string ending at
2398 POS, make sure to pop the iterator because it will be in
2399 front of that overlay string. When POS is ZV, we've thereby
2400 also ``processed'' overlay strings at ZV. */
2401 while (it->sp)
2402 pop_it (it);
2403 it->current.overlay_string_index = -1;
2404 it->method = next_element_from_buffer;
2405 if (CHARPOS (pos->pos) == ZV)
2406 it->overlay_strings_at_end_processed_p = 1;
2408 #endif /* 0 */
2410 if (CHARPOS (pos->string_pos) >= 0)
2412 /* Recorded position is not in an overlay string, but in another
2413 string. This can only be a string from a `display' property.
2414 IT should already be filled with that string. */
2415 it->current.string_pos = pos->string_pos;
2416 xassert (STRINGP (it->string));
2419 /* Restore position in display vector translations, control
2420 character translations or ellipses. */
2421 if (pos->dpvec_index >= 0)
2423 if (it->dpvec == NULL)
2424 get_next_display_element (it);
2425 xassert (it->dpvec && it->current.dpvec_index == 0);
2426 it->current.dpvec_index = pos->dpvec_index;
2429 CHECK_IT (it);
2430 return !overlay_strings_with_newlines;
2434 /* Initialize IT for stepping through current_buffer in window W
2435 starting at ROW->start. */
2437 static void
2438 init_to_row_start (it, w, row)
2439 struct it *it;
2440 struct window *w;
2441 struct glyph_row *row;
2443 init_from_display_pos (it, w, &row->start);
2444 it->continuation_lines_width = row->continuation_lines_width;
2445 CHECK_IT (it);
2449 /* Initialize IT for stepping through current_buffer in window W
2450 starting in the line following ROW, i.e. starting at ROW->end.
2451 Value is zero if there are overlay strings with newlines at ROW's
2452 end position. */
2454 static int
2455 init_to_row_end (it, w, row)
2456 struct it *it;
2457 struct window *w;
2458 struct glyph_row *row;
2460 int success = 0;
2462 if (init_from_display_pos (it, w, &row->end))
2464 if (row->continued_p)
2465 it->continuation_lines_width
2466 = row->continuation_lines_width + row->pixel_width;
2467 CHECK_IT (it);
2468 success = 1;
2471 return success;
2477 /***********************************************************************
2478 Text properties
2479 ***********************************************************************/
2481 /* Called when IT reaches IT->stop_charpos. Handle text property and
2482 overlay changes. Set IT->stop_charpos to the next position where
2483 to stop. */
2485 static void
2486 handle_stop (it)
2487 struct it *it;
2489 enum prop_handled handled;
2490 int handle_overlay_change_p = 1;
2491 struct props *p;
2493 it->dpvec = NULL;
2494 it->current.dpvec_index = -1;
2498 handled = HANDLED_NORMALLY;
2500 /* Call text property handlers. */
2501 for (p = it_props; p->handler; ++p)
2503 handled = p->handler (it);
2505 if (handled == HANDLED_RECOMPUTE_PROPS)
2506 break;
2507 else if (handled == HANDLED_RETURN)
2508 return;
2509 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2510 handle_overlay_change_p = 0;
2513 if (handled != HANDLED_RECOMPUTE_PROPS)
2515 /* Don't check for overlay strings below when set to deliver
2516 characters from a display vector. */
2517 if (it->method == next_element_from_display_vector)
2518 handle_overlay_change_p = 0;
2520 /* Handle overlay changes. */
2521 if (handle_overlay_change_p)
2522 handled = handle_overlay_change (it);
2524 /* Determine where to stop next. */
2525 if (handled == HANDLED_NORMALLY)
2526 compute_stop_pos (it);
2529 while (handled == HANDLED_RECOMPUTE_PROPS);
2533 /* Compute IT->stop_charpos from text property and overlay change
2534 information for IT's current position. */
2536 static void
2537 compute_stop_pos (it)
2538 struct it *it;
2540 register INTERVAL iv, next_iv;
2541 Lisp_Object object, limit, position;
2543 /* If nowhere else, stop at the end. */
2544 it->stop_charpos = it->end_charpos;
2546 if (STRINGP (it->string))
2548 /* Strings are usually short, so don't limit the search for
2549 properties. */
2550 object = it->string;
2551 limit = Qnil;
2552 position = make_number (IT_STRING_CHARPOS (*it));
2554 else
2556 int charpos;
2558 /* If next overlay change is in front of the current stop pos
2559 (which is IT->end_charpos), stop there. Note: value of
2560 next_overlay_change is point-max if no overlay change
2561 follows. */
2562 charpos = next_overlay_change (IT_CHARPOS (*it));
2563 if (charpos < it->stop_charpos)
2564 it->stop_charpos = charpos;
2566 /* If showing the region, we have to stop at the region
2567 start or end because the face might change there. */
2568 if (it->region_beg_charpos > 0)
2570 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2571 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2572 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2573 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2576 /* Set up variables for computing the stop position from text
2577 property changes. */
2578 XSETBUFFER (object, current_buffer);
2579 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2580 position = make_number (IT_CHARPOS (*it));
2584 /* Get the interval containing IT's position. Value is a null
2585 interval if there isn't such an interval. */
2586 iv = validate_interval_range (object, &position, &position, 0);
2587 if (!NULL_INTERVAL_P (iv))
2589 Lisp_Object values_here[LAST_PROP_IDX];
2590 struct props *p;
2592 /* Get properties here. */
2593 for (p = it_props; p->handler; ++p)
2594 values_here[p->idx] = textget (iv->plist, *p->name);
2596 /* Look for an interval following iv that has different
2597 properties. */
2598 for (next_iv = next_interval (iv);
2599 (!NULL_INTERVAL_P (next_iv)
2600 && (NILP (limit)
2601 || XFASTINT (limit) > next_iv->position));
2602 next_iv = next_interval (next_iv))
2604 for (p = it_props; p->handler; ++p)
2606 Lisp_Object new_value;
2608 new_value = textget (next_iv->plist, *p->name);
2609 if (!EQ (values_here[p->idx], new_value))
2610 break;
2613 if (p->handler)
2614 break;
2617 if (!NULL_INTERVAL_P (next_iv))
2619 if (INTEGERP (limit)
2620 && next_iv->position >= XFASTINT (limit))
2621 /* No text property change up to limit. */
2622 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2623 else
2624 /* Text properties change in next_iv. */
2625 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2629 xassert (STRINGP (it->string)
2630 || (it->stop_charpos >= BEGV
2631 && it->stop_charpos >= IT_CHARPOS (*it)));
2635 /* Return the position of the next overlay change after POS in
2636 current_buffer. Value is point-max if no overlay change
2637 follows. This is like `next-overlay-change' but doesn't use
2638 xmalloc. */
2640 static int
2641 next_overlay_change (pos)
2642 int pos;
2644 int noverlays;
2645 int endpos;
2646 Lisp_Object *overlays;
2647 int len;
2648 int i;
2650 /* Get all overlays at the given position. */
2651 len = 10;
2652 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2653 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2654 if (noverlays > len)
2656 len = noverlays;
2657 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2658 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2661 /* If any of these overlays ends before endpos,
2662 use its ending point instead. */
2663 for (i = 0; i < noverlays; ++i)
2665 Lisp_Object oend;
2666 int oendpos;
2668 oend = OVERLAY_END (overlays[i]);
2669 oendpos = OVERLAY_POSITION (oend);
2670 endpos = min (endpos, oendpos);
2673 return endpos;
2678 /***********************************************************************
2679 Fontification
2680 ***********************************************************************/
2682 /* Handle changes in the `fontified' property of the current buffer by
2683 calling hook functions from Qfontification_functions to fontify
2684 regions of text. */
2686 static enum prop_handled
2687 handle_fontified_prop (it)
2688 struct it *it;
2690 Lisp_Object prop, pos;
2691 enum prop_handled handled = HANDLED_NORMALLY;
2693 /* Get the value of the `fontified' property at IT's current buffer
2694 position. (The `fontified' property doesn't have a special
2695 meaning in strings.) If the value is nil, call functions from
2696 Qfontification_functions. */
2697 if (!STRINGP (it->string)
2698 && it->s == NULL
2699 && !NILP (Vfontification_functions)
2700 && !NILP (Vrun_hooks)
2701 && (pos = make_number (IT_CHARPOS (*it)),
2702 prop = Fget_char_property (pos, Qfontified, Qnil),
2703 NILP (prop)))
2705 int count = SPECPDL_INDEX ();
2706 Lisp_Object val;
2708 val = Vfontification_functions;
2709 specbind (Qfontification_functions, Qnil);
2711 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2712 safe_call1 (val, pos);
2713 else
2715 Lisp_Object globals, fn;
2716 struct gcpro gcpro1, gcpro2;
2718 globals = Qnil;
2719 GCPRO2 (val, globals);
2721 for (; CONSP (val); val = XCDR (val))
2723 fn = XCAR (val);
2725 if (EQ (fn, Qt))
2727 /* A value of t indicates this hook has a local
2728 binding; it means to run the global binding too.
2729 In a global value, t should not occur. If it
2730 does, we must ignore it to avoid an endless
2731 loop. */
2732 for (globals = Fdefault_value (Qfontification_functions);
2733 CONSP (globals);
2734 globals = XCDR (globals))
2736 fn = XCAR (globals);
2737 if (!EQ (fn, Qt))
2738 safe_call1 (fn, pos);
2741 else
2742 safe_call1 (fn, pos);
2745 UNGCPRO;
2748 unbind_to (count, Qnil);
2750 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2751 something. This avoids an endless loop if they failed to
2752 fontify the text for which reason ever. */
2753 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2754 handled = HANDLED_RECOMPUTE_PROPS;
2757 return handled;
2762 /***********************************************************************
2763 Faces
2764 ***********************************************************************/
2766 /* Set up iterator IT from face properties at its current position.
2767 Called from handle_stop. */
2769 static enum prop_handled
2770 handle_face_prop (it)
2771 struct it *it;
2773 int new_face_id, next_stop;
2775 if (!STRINGP (it->string))
2777 new_face_id
2778 = face_at_buffer_position (it->w,
2779 IT_CHARPOS (*it),
2780 it->region_beg_charpos,
2781 it->region_end_charpos,
2782 &next_stop,
2783 (IT_CHARPOS (*it)
2784 + TEXT_PROP_DISTANCE_LIMIT),
2787 /* Is this a start of a run of characters with box face?
2788 Caveat: this can be called for a freshly initialized
2789 iterator; face_id is -1 in this case. We know that the new
2790 face will not change until limit, i.e. if the new face has a
2791 box, all characters up to limit will have one. But, as
2792 usual, we don't know whether limit is really the end. */
2793 if (new_face_id != it->face_id)
2795 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2797 /* If new face has a box but old face has not, this is
2798 the start of a run of characters with box, i.e. it has
2799 a shadow on the left side. The value of face_id of the
2800 iterator will be -1 if this is the initial call that gets
2801 the face. In this case, we have to look in front of IT's
2802 position and see whether there is a face != new_face_id. */
2803 it->start_of_box_run_p
2804 = (new_face->box != FACE_NO_BOX
2805 && (it->face_id >= 0
2806 || IT_CHARPOS (*it) == BEG
2807 || new_face_id != face_before_it_pos (it)));
2808 it->face_box_p = new_face->box != FACE_NO_BOX;
2811 else
2813 int base_face_id, bufpos;
2815 if (it->current.overlay_string_index >= 0)
2816 bufpos = IT_CHARPOS (*it);
2817 else
2818 bufpos = 0;
2820 /* For strings from a buffer, i.e. overlay strings or strings
2821 from a `display' property, use the face at IT's current
2822 buffer position as the base face to merge with, so that
2823 overlay strings appear in the same face as surrounding
2824 text, unless they specify their own faces. */
2825 base_face_id = underlying_face_id (it);
2827 new_face_id = face_at_string_position (it->w,
2828 it->string,
2829 IT_STRING_CHARPOS (*it),
2830 bufpos,
2831 it->region_beg_charpos,
2832 it->region_end_charpos,
2833 &next_stop,
2834 base_face_id, 0);
2836 #if 0 /* This shouldn't be neccessary. Let's check it. */
2837 /* If IT is used to display a mode line we would really like to
2838 use the mode line face instead of the frame's default face. */
2839 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2840 && new_face_id == DEFAULT_FACE_ID)
2841 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2842 #endif
2844 /* Is this a start of a run of characters with box? Caveat:
2845 this can be called for a freshly allocated iterator; face_id
2846 is -1 is this case. We know that the new face will not
2847 change until the next check pos, i.e. if the new face has a
2848 box, all characters up to that position will have a
2849 box. But, as usual, we don't know whether that position
2850 is really the end. */
2851 if (new_face_id != it->face_id)
2853 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2854 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2856 /* If new face has a box but old face hasn't, this is the
2857 start of a run of characters with box, i.e. it has a
2858 shadow on the left side. */
2859 it->start_of_box_run_p
2860 = new_face->box && (old_face == NULL || !old_face->box);
2861 it->face_box_p = new_face->box != FACE_NO_BOX;
2865 it->face_id = new_face_id;
2866 return HANDLED_NORMALLY;
2870 /* Return the ID of the face ``underlying'' IT's current position,
2871 which is in a string. If the iterator is associated with a
2872 buffer, return the face at IT's current buffer position.
2873 Otherwise, use the iterator's base_face_id. */
2875 static int
2876 underlying_face_id (it)
2877 struct it *it;
2879 int face_id = it->base_face_id, i;
2881 xassert (STRINGP (it->string));
2883 for (i = it->sp - 1; i >= 0; --i)
2884 if (NILP (it->stack[i].string))
2885 face_id = it->stack[i].face_id;
2887 return face_id;
2891 /* Compute the face one character before or after the current position
2892 of IT. BEFORE_P non-zero means get the face in front of IT's
2893 position. Value is the id of the face. */
2895 static int
2896 face_before_or_after_it_pos (it, before_p)
2897 struct it *it;
2898 int before_p;
2900 int face_id, limit;
2901 int next_check_charpos;
2902 struct text_pos pos;
2904 xassert (it->s == NULL);
2906 if (STRINGP (it->string))
2908 int bufpos, base_face_id;
2910 /* No face change past the end of the string (for the case
2911 we are padding with spaces). No face change before the
2912 string start. */
2913 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2914 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2915 return it->face_id;
2917 /* Set pos to the position before or after IT's current position. */
2918 if (before_p)
2919 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2920 else
2921 /* For composition, we must check the character after the
2922 composition. */
2923 pos = (it->what == IT_COMPOSITION
2924 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2925 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2927 if (it->current.overlay_string_index >= 0)
2928 bufpos = IT_CHARPOS (*it);
2929 else
2930 bufpos = 0;
2932 base_face_id = underlying_face_id (it);
2934 /* Get the face for ASCII, or unibyte. */
2935 face_id = face_at_string_position (it->w,
2936 it->string,
2937 CHARPOS (pos),
2938 bufpos,
2939 it->region_beg_charpos,
2940 it->region_end_charpos,
2941 &next_check_charpos,
2942 base_face_id, 0);
2944 /* Correct the face for charsets different from ASCII. Do it
2945 for the multibyte case only. The face returned above is
2946 suitable for unibyte text if IT->string is unibyte. */
2947 if (STRING_MULTIBYTE (it->string))
2949 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2950 int rest = SBYTES (it->string) - BYTEPOS (pos);
2951 int c, len;
2952 struct face *face = FACE_FROM_ID (it->f, face_id);
2954 c = string_char_and_length (p, rest, &len);
2955 face_id = FACE_FOR_CHAR (it->f, face, c);
2958 else
2960 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2961 || (IT_CHARPOS (*it) <= BEGV && before_p))
2962 return it->face_id;
2964 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2965 pos = it->current.pos;
2967 if (before_p)
2968 DEC_TEXT_POS (pos, it->multibyte_p);
2969 else
2971 if (it->what == IT_COMPOSITION)
2972 /* For composition, we must check the position after the
2973 composition. */
2974 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2975 else
2976 INC_TEXT_POS (pos, it->multibyte_p);
2979 /* Determine face for CHARSET_ASCII, or unibyte. */
2980 face_id = face_at_buffer_position (it->w,
2981 CHARPOS (pos),
2982 it->region_beg_charpos,
2983 it->region_end_charpos,
2984 &next_check_charpos,
2985 limit, 0);
2987 /* Correct the face for charsets different from ASCII. Do it
2988 for the multibyte case only. The face returned above is
2989 suitable for unibyte text if current_buffer is unibyte. */
2990 if (it->multibyte_p)
2992 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2993 struct face *face = FACE_FROM_ID (it->f, face_id);
2994 face_id = FACE_FOR_CHAR (it->f, face, c);
2998 return face_id;
3003 /***********************************************************************
3004 Invisible text
3005 ***********************************************************************/
3007 /* Set up iterator IT from invisible properties at its current
3008 position. Called from handle_stop. */
3010 static enum prop_handled
3011 handle_invisible_prop (it)
3012 struct it *it;
3014 enum prop_handled handled = HANDLED_NORMALLY;
3016 if (STRINGP (it->string))
3018 extern Lisp_Object Qinvisible;
3019 Lisp_Object prop, end_charpos, limit, charpos;
3021 /* Get the value of the invisible text property at the
3022 current position. Value will be nil if there is no such
3023 property. */
3024 charpos = make_number (IT_STRING_CHARPOS (*it));
3025 prop = Fget_text_property (charpos, Qinvisible, it->string);
3027 if (!NILP (prop)
3028 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3030 handled = HANDLED_RECOMPUTE_PROPS;
3032 /* Get the position at which the next change of the
3033 invisible text property can be found in IT->string.
3034 Value will be nil if the property value is the same for
3035 all the rest of IT->string. */
3036 XSETINT (limit, SCHARS (it->string));
3037 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3038 it->string, limit);
3040 /* Text at current position is invisible. The next
3041 change in the property is at position end_charpos.
3042 Move IT's current position to that position. */
3043 if (INTEGERP (end_charpos)
3044 && XFASTINT (end_charpos) < XFASTINT (limit))
3046 struct text_pos old;
3047 old = it->current.string_pos;
3048 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3049 compute_string_pos (&it->current.string_pos, old, it->string);
3051 else
3053 /* The rest of the string is invisible. If this is an
3054 overlay string, proceed with the next overlay string
3055 or whatever comes and return a character from there. */
3056 if (it->current.overlay_string_index >= 0)
3058 next_overlay_string (it);
3059 /* Don't check for overlay strings when we just
3060 finished processing them. */
3061 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3063 else
3065 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3066 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3071 else
3073 int invis_p, newpos, next_stop, start_charpos;
3074 Lisp_Object pos, prop, overlay;
3076 /* First of all, is there invisible text at this position? */
3077 start_charpos = IT_CHARPOS (*it);
3078 pos = make_number (IT_CHARPOS (*it));
3079 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3080 &overlay);
3081 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3083 /* If we are on invisible text, skip over it. */
3084 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3086 /* Record whether we have to display an ellipsis for the
3087 invisible text. */
3088 int display_ellipsis_p = invis_p == 2;
3090 handled = HANDLED_RECOMPUTE_PROPS;
3092 /* Loop skipping over invisible text. The loop is left at
3093 ZV or with IT on the first char being visible again. */
3096 /* Try to skip some invisible text. Return value is the
3097 position reached which can be equal to IT's position
3098 if there is nothing invisible here. This skips both
3099 over invisible text properties and overlays with
3100 invisible property. */
3101 newpos = skip_invisible (IT_CHARPOS (*it),
3102 &next_stop, ZV, it->window);
3104 /* If we skipped nothing at all we weren't at invisible
3105 text in the first place. If everything to the end of
3106 the buffer was skipped, end the loop. */
3107 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3108 invis_p = 0;
3109 else
3111 /* We skipped some characters but not necessarily
3112 all there are. Check if we ended up on visible
3113 text. Fget_char_property returns the property of
3114 the char before the given position, i.e. if we
3115 get invis_p = 0, this means that the char at
3116 newpos is visible. */
3117 pos = make_number (newpos);
3118 prop = Fget_char_property (pos, Qinvisible, it->window);
3119 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3122 /* If we ended up on invisible text, proceed to
3123 skip starting with next_stop. */
3124 if (invis_p)
3125 IT_CHARPOS (*it) = next_stop;
3127 while (invis_p);
3129 /* The position newpos is now either ZV or on visible text. */
3130 IT_CHARPOS (*it) = newpos;
3131 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3133 /* If there are before-strings at the start of invisible
3134 text, and the text is invisible because of a text
3135 property, arrange to show before-strings because 20.x did
3136 it that way. (If the text is invisible because of an
3137 overlay property instead of a text property, this is
3138 already handled in the overlay code.) */
3139 if (NILP (overlay)
3140 && get_overlay_strings (it, start_charpos))
3142 handled = HANDLED_RECOMPUTE_PROPS;
3143 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3145 else if (display_ellipsis_p)
3146 setup_for_ellipsis (it);
3150 return handled;
3154 /* Make iterator IT return `...' next. */
3156 static void
3157 setup_for_ellipsis (it)
3158 struct it *it;
3160 if (it->dp
3161 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3163 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3164 it->dpvec = v->contents;
3165 it->dpend = v->contents + v->size;
3167 else
3169 /* Default `...'. */
3170 it->dpvec = default_invis_vector;
3171 it->dpend = default_invis_vector + 3;
3174 /* The ellipsis display does not replace the display of the
3175 character at the new position. Indicate this by setting
3176 IT->dpvec_char_len to zero. */
3177 it->dpvec_char_len = 0;
3179 it->current.dpvec_index = 0;
3180 it->method = next_element_from_display_vector;
3185 /***********************************************************************
3186 'display' property
3187 ***********************************************************************/
3189 /* Set up iterator IT from `display' property at its current position.
3190 Called from handle_stop. */
3192 static enum prop_handled
3193 handle_display_prop (it)
3194 struct it *it;
3196 Lisp_Object prop, object;
3197 struct text_pos *position;
3198 int display_replaced_p = 0;
3200 if (STRINGP (it->string))
3202 object = it->string;
3203 position = &it->current.string_pos;
3205 else
3207 object = it->w->buffer;
3208 position = &it->current.pos;
3211 /* Reset those iterator values set from display property values. */
3212 it->font_height = Qnil;
3213 it->space_width = Qnil;
3214 it->voffset = 0;
3216 /* We don't support recursive `display' properties, i.e. string
3217 values that have a string `display' property, that have a string
3218 `display' property etc. */
3219 if (!it->string_from_display_prop_p)
3220 it->area = TEXT_AREA;
3222 prop = Fget_char_property (make_number (position->charpos),
3223 Qdisplay, object);
3224 if (NILP (prop))
3225 return HANDLED_NORMALLY;
3227 if (CONSP (prop)
3228 /* Simple properties. */
3229 && !EQ (XCAR (prop), Qimage)
3230 && !EQ (XCAR (prop), Qspace)
3231 && !EQ (XCAR (prop), Qwhen)
3232 && !EQ (XCAR (prop), Qspace_width)
3233 && !EQ (XCAR (prop), Qheight)
3234 && !EQ (XCAR (prop), Qraise)
3235 /* Marginal area specifications. */
3236 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3237 && !NILP (XCAR (prop)))
3239 for (; CONSP (prop); prop = XCDR (prop))
3241 if (handle_single_display_prop (it, XCAR (prop), object,
3242 position, display_replaced_p))
3243 display_replaced_p = 1;
3246 else if (VECTORP (prop))
3248 int i;
3249 for (i = 0; i < ASIZE (prop); ++i)
3250 if (handle_single_display_prop (it, AREF (prop, i), object,
3251 position, display_replaced_p))
3252 display_replaced_p = 1;
3254 else
3256 if (handle_single_display_prop (it, prop, object, position, 0))
3257 display_replaced_p = 1;
3260 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3264 /* Value is the position of the end of the `display' property starting
3265 at START_POS in OBJECT. */
3267 static struct text_pos
3268 display_prop_end (it, object, start_pos)
3269 struct it *it;
3270 Lisp_Object object;
3271 struct text_pos start_pos;
3273 Lisp_Object end;
3274 struct text_pos end_pos;
3276 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3277 Qdisplay, object, Qnil);
3278 CHARPOS (end_pos) = XFASTINT (end);
3279 if (STRINGP (object))
3280 compute_string_pos (&end_pos, start_pos, it->string);
3281 else
3282 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3284 return end_pos;
3288 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3289 is the object in which the `display' property was found. *POSITION
3290 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3291 means that we previously saw a display sub-property which already
3292 replaced text display with something else, for example an image;
3293 ignore such properties after the first one has been processed.
3295 If PROP is a `space' or `image' sub-property, set *POSITION to the
3296 end position of the `display' property.
3298 Value is non-zero if something was found which replaces the display
3299 of buffer or string text. */
3301 static int
3302 handle_single_display_prop (it, prop, object, position,
3303 display_replaced_before_p)
3304 struct it *it;
3305 Lisp_Object prop;
3306 Lisp_Object object;
3307 struct text_pos *position;
3308 int display_replaced_before_p;
3310 Lisp_Object value;
3311 int replaces_text_display_p = 0;
3312 Lisp_Object form;
3314 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3315 evaluated. If the result is nil, VALUE is ignored. */
3316 form = Qt;
3317 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3319 prop = XCDR (prop);
3320 if (!CONSP (prop))
3321 return 0;
3322 form = XCAR (prop);
3323 prop = XCDR (prop);
3326 if (!NILP (form) && !EQ (form, Qt))
3328 int count = SPECPDL_INDEX ();
3329 struct gcpro gcpro1;
3331 /* Bind `object' to the object having the `display' property, a
3332 buffer or string. Bind `position' to the position in the
3333 object where the property was found, and `buffer-position'
3334 to the current position in the buffer. */
3335 specbind (Qobject, object);
3336 specbind (Qposition, make_number (CHARPOS (*position)));
3337 specbind (Qbuffer_position,
3338 make_number (STRINGP (object)
3339 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3340 GCPRO1 (form);
3341 form = safe_eval (form);
3342 UNGCPRO;
3343 unbind_to (count, Qnil);
3346 if (NILP (form))
3347 return 0;
3349 if (CONSP (prop)
3350 && EQ (XCAR (prop), Qheight)
3351 && CONSP (XCDR (prop)))
3353 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3354 return 0;
3356 /* `(height HEIGHT)'. */
3357 it->font_height = XCAR (XCDR (prop));
3358 if (!NILP (it->font_height))
3360 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3361 int new_height = -1;
3363 if (CONSP (it->font_height)
3364 && (EQ (XCAR (it->font_height), Qplus)
3365 || EQ (XCAR (it->font_height), Qminus))
3366 && CONSP (XCDR (it->font_height))
3367 && INTEGERP (XCAR (XCDR (it->font_height))))
3369 /* `(+ N)' or `(- N)' where N is an integer. */
3370 int steps = XINT (XCAR (XCDR (it->font_height)));
3371 if (EQ (XCAR (it->font_height), Qplus))
3372 steps = - steps;
3373 it->face_id = smaller_face (it->f, it->face_id, steps);
3375 else if (FUNCTIONP (it->font_height))
3377 /* Call function with current height as argument.
3378 Value is the new height. */
3379 Lisp_Object height;
3380 height = safe_call1 (it->font_height,
3381 face->lface[LFACE_HEIGHT_INDEX]);
3382 if (NUMBERP (height))
3383 new_height = XFLOATINT (height);
3385 else if (NUMBERP (it->font_height))
3387 /* Value is a multiple of the canonical char height. */
3388 struct face *face;
3390 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3391 new_height = (XFLOATINT (it->font_height)
3392 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3394 else
3396 /* Evaluate IT->font_height with `height' bound to the
3397 current specified height to get the new height. */
3398 Lisp_Object value;
3399 int count = SPECPDL_INDEX ();
3401 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3402 value = safe_eval (it->font_height);
3403 unbind_to (count, Qnil);
3405 if (NUMBERP (value))
3406 new_height = XFLOATINT (value);
3409 if (new_height > 0)
3410 it->face_id = face_with_height (it->f, it->face_id, new_height);
3413 else if (CONSP (prop)
3414 && EQ (XCAR (prop), Qspace_width)
3415 && CONSP (XCDR (prop)))
3417 /* `(space_width WIDTH)'. */
3418 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3419 return 0;
3421 value = XCAR (XCDR (prop));
3422 if (NUMBERP (value) && XFLOATINT (value) > 0)
3423 it->space_width = value;
3425 else if (CONSP (prop)
3426 && EQ (XCAR (prop), Qraise)
3427 && CONSP (XCDR (prop)))
3429 /* `(raise FACTOR)'. */
3430 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3431 return 0;
3433 #ifdef HAVE_WINDOW_SYSTEM
3434 value = XCAR (XCDR (prop));
3435 if (NUMBERP (value))
3437 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3438 it->voffset = - (XFLOATINT (value)
3439 * (FONT_HEIGHT (face->font)));
3441 #endif /* HAVE_WINDOW_SYSTEM */
3443 else if (!it->string_from_display_prop_p)
3445 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3446 VALUE) or `((margin nil) VALUE)' or VALUE. */
3447 Lisp_Object location, value;
3448 struct text_pos start_pos;
3449 int valid_p;
3451 /* Characters having this form of property are not displayed, so
3452 we have to find the end of the property. */
3453 start_pos = *position;
3454 *position = display_prop_end (it, object, start_pos);
3455 value = Qnil;
3457 /* Let's stop at the new position and assume that all
3458 text properties change there. */
3459 it->stop_charpos = position->charpos;
3461 location = Qunbound;
3462 if (CONSP (prop) && CONSP (XCAR (prop)))
3464 Lisp_Object tem;
3466 value = XCDR (prop);
3467 if (CONSP (value))
3468 value = XCAR (value);
3470 tem = XCAR (prop);
3471 if (EQ (XCAR (tem), Qmargin)
3472 && (tem = XCDR (tem),
3473 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3474 (NILP (tem)
3475 || EQ (tem, Qleft_margin)
3476 || EQ (tem, Qright_margin))))
3477 location = tem;
3480 if (EQ (location, Qunbound))
3482 location = Qnil;
3483 value = prop;
3486 #ifdef HAVE_WINDOW_SYSTEM
3487 if (FRAME_TERMCAP_P (it->f))
3488 valid_p = STRINGP (value);
3489 else
3490 valid_p = (STRINGP (value)
3491 || (CONSP (value) && EQ (XCAR (value), Qspace))
3492 || valid_image_p (value));
3493 #else /* not HAVE_WINDOW_SYSTEM */
3494 valid_p = STRINGP (value);
3495 #endif /* not HAVE_WINDOW_SYSTEM */
3497 if ((EQ (location, Qleft_margin)
3498 || EQ (location, Qright_margin)
3499 || NILP (location))
3500 && valid_p
3501 && !display_replaced_before_p)
3503 replaces_text_display_p = 1;
3505 /* Save current settings of IT so that we can restore them
3506 when we are finished with the glyph property value. */
3507 push_it (it);
3509 if (NILP (location))
3510 it->area = TEXT_AREA;
3511 else if (EQ (location, Qleft_margin))
3512 it->area = LEFT_MARGIN_AREA;
3513 else
3514 it->area = RIGHT_MARGIN_AREA;
3516 if (STRINGP (value))
3518 it->string = value;
3519 it->multibyte_p = STRING_MULTIBYTE (it->string);
3520 it->current.overlay_string_index = -1;
3521 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3522 it->end_charpos = it->string_nchars = SCHARS (it->string);
3523 it->method = next_element_from_string;
3524 it->stop_charpos = 0;
3525 it->string_from_display_prop_p = 1;
3526 /* Say that we haven't consumed the characters with
3527 `display' property yet. The call to pop_it in
3528 set_iterator_to_next will clean this up. */
3529 *position = start_pos;
3531 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3533 it->method = next_element_from_stretch;
3534 it->object = value;
3535 it->current.pos = it->position = start_pos;
3537 #ifdef HAVE_WINDOW_SYSTEM
3538 else
3540 it->what = IT_IMAGE;
3541 it->image_id = lookup_image (it->f, value);
3542 it->position = start_pos;
3543 it->object = NILP (object) ? it->w->buffer : object;
3544 it->method = next_element_from_image;
3546 /* Say that we haven't consumed the characters with
3547 `display' property yet. The call to pop_it in
3548 set_iterator_to_next will clean this up. */
3549 *position = start_pos;
3551 #endif /* HAVE_WINDOW_SYSTEM */
3553 else
3554 /* Invalid property or property not supported. Restore
3555 the position to what it was before. */
3556 *position = start_pos;
3559 return replaces_text_display_p;
3563 /* Check if PROP is a display sub-property value whose text should be
3564 treated as intangible. */
3566 static int
3567 single_display_prop_intangible_p (prop)
3568 Lisp_Object prop;
3570 /* Skip over `when FORM'. */
3571 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3573 prop = XCDR (prop);
3574 if (!CONSP (prop))
3575 return 0;
3576 prop = XCDR (prop);
3579 if (STRINGP (prop))
3580 return 1;
3582 if (!CONSP (prop))
3583 return 0;
3585 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3586 we don't need to treat text as intangible. */
3587 if (EQ (XCAR (prop), Qmargin))
3589 prop = XCDR (prop);
3590 if (!CONSP (prop))
3591 return 0;
3593 prop = XCDR (prop);
3594 if (!CONSP (prop)
3595 || EQ (XCAR (prop), Qleft_margin)
3596 || EQ (XCAR (prop), Qright_margin))
3597 return 0;
3600 return (CONSP (prop)
3601 && (EQ (XCAR (prop), Qimage)
3602 || EQ (XCAR (prop), Qspace)));
3606 /* Check if PROP is a display property value whose text should be
3607 treated as intangible. */
3610 display_prop_intangible_p (prop)
3611 Lisp_Object prop;
3613 if (CONSP (prop)
3614 && CONSP (XCAR (prop))
3615 && !EQ (Qmargin, XCAR (XCAR (prop))))
3617 /* A list of sub-properties. */
3618 while (CONSP (prop))
3620 if (single_display_prop_intangible_p (XCAR (prop)))
3621 return 1;
3622 prop = XCDR (prop);
3625 else if (VECTORP (prop))
3627 /* A vector of sub-properties. */
3628 int i;
3629 for (i = 0; i < ASIZE (prop); ++i)
3630 if (single_display_prop_intangible_p (AREF (prop, i)))
3631 return 1;
3633 else
3634 return single_display_prop_intangible_p (prop);
3636 return 0;
3640 /* Return 1 if PROP is a display sub-property value containing STRING. */
3642 static int
3643 single_display_prop_string_p (prop, string)
3644 Lisp_Object prop, string;
3646 if (EQ (string, prop))
3647 return 1;
3649 /* Skip over `when FORM'. */
3650 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3652 prop = XCDR (prop);
3653 if (!CONSP (prop))
3654 return 0;
3655 prop = XCDR (prop);
3658 if (CONSP (prop))
3659 /* Skip over `margin LOCATION'. */
3660 if (EQ (XCAR (prop), Qmargin))
3662 prop = XCDR (prop);
3663 if (!CONSP (prop))
3664 return 0;
3666 prop = XCDR (prop);
3667 if (!CONSP (prop))
3668 return 0;
3671 return CONSP (prop) && EQ (XCAR (prop), string);
3675 /* Return 1 if STRING appears in the `display' property PROP. */
3677 static int
3678 display_prop_string_p (prop, string)
3679 Lisp_Object prop, string;
3681 if (CONSP (prop)
3682 && CONSP (XCAR (prop))
3683 && !EQ (Qmargin, XCAR (XCAR (prop))))
3685 /* A list of sub-properties. */
3686 while (CONSP (prop))
3688 if (single_display_prop_string_p (XCAR (prop), string))
3689 return 1;
3690 prop = XCDR (prop);
3693 else if (VECTORP (prop))
3695 /* A vector of sub-properties. */
3696 int i;
3697 for (i = 0; i < ASIZE (prop); ++i)
3698 if (single_display_prop_string_p (AREF (prop, i), string))
3699 return 1;
3701 else
3702 return single_display_prop_string_p (prop, string);
3704 return 0;
3708 /* Determine from which buffer position in W's buffer STRING comes
3709 from. AROUND_CHARPOS is an approximate position where it could
3710 be from. Value is the buffer position or 0 if it couldn't be
3711 determined.
3713 W's buffer must be current.
3715 This function is necessary because we don't record buffer positions
3716 in glyphs generated from strings (to keep struct glyph small).
3717 This function may only use code that doesn't eval because it is
3718 called asynchronously from note_mouse_highlight. */
3721 string_buffer_position (w, string, around_charpos)
3722 struct window *w;
3723 Lisp_Object string;
3724 int around_charpos;
3726 Lisp_Object limit, prop, pos;
3727 const int MAX_DISTANCE = 1000;
3728 int found = 0;
3730 pos = make_number (around_charpos);
3731 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3732 while (!found && !EQ (pos, limit))
3734 prop = Fget_char_property (pos, Qdisplay, Qnil);
3735 if (!NILP (prop) && display_prop_string_p (prop, string))
3736 found = 1;
3737 else
3738 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3741 if (!found)
3743 pos = make_number (around_charpos);
3744 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3745 while (!found && !EQ (pos, limit))
3747 prop = Fget_char_property (pos, Qdisplay, Qnil);
3748 if (!NILP (prop) && display_prop_string_p (prop, string))
3749 found = 1;
3750 else
3751 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3752 limit);
3756 return found ? XINT (pos) : 0;
3761 /***********************************************************************
3762 `composition' property
3763 ***********************************************************************/
3765 /* Set up iterator IT from `composition' property at its current
3766 position. Called from handle_stop. */
3768 static enum prop_handled
3769 handle_composition_prop (it)
3770 struct it *it;
3772 Lisp_Object prop, string;
3773 int pos, pos_byte, end;
3774 enum prop_handled handled = HANDLED_NORMALLY;
3776 if (STRINGP (it->string))
3778 pos = IT_STRING_CHARPOS (*it);
3779 pos_byte = IT_STRING_BYTEPOS (*it);
3780 string = it->string;
3782 else
3784 pos = IT_CHARPOS (*it);
3785 pos_byte = IT_BYTEPOS (*it);
3786 string = Qnil;
3789 /* If there's a valid composition and point is not inside of the
3790 composition (in the case that the composition is from the current
3791 buffer), draw a glyph composed from the composition components. */
3792 if (find_composition (pos, -1, &pos, &end, &prop, string)
3793 && COMPOSITION_VALID_P (pos, end, prop)
3794 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3796 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3798 if (id >= 0)
3800 it->method = next_element_from_composition;
3801 it->cmp_id = id;
3802 it->cmp_len = COMPOSITION_LENGTH (prop);
3803 /* For a terminal, draw only the first character of the
3804 components. */
3805 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3806 it->len = (STRINGP (it->string)
3807 ? string_char_to_byte (it->string, end)
3808 : CHAR_TO_BYTE (end)) - pos_byte;
3809 it->stop_charpos = end;
3810 handled = HANDLED_RETURN;
3814 return handled;
3819 /***********************************************************************
3820 Overlay strings
3821 ***********************************************************************/
3823 /* The following structure is used to record overlay strings for
3824 later sorting in load_overlay_strings. */
3826 struct overlay_entry
3828 Lisp_Object overlay;
3829 Lisp_Object string;
3830 int priority;
3831 int after_string_p;
3835 /* Set up iterator IT from overlay strings at its current position.
3836 Called from handle_stop. */
3838 static enum prop_handled
3839 handle_overlay_change (it)
3840 struct it *it;
3842 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3843 return HANDLED_RECOMPUTE_PROPS;
3844 else
3845 return HANDLED_NORMALLY;
3849 /* Set up the next overlay string for delivery by IT, if there is an
3850 overlay string to deliver. Called by set_iterator_to_next when the
3851 end of the current overlay string is reached. If there are more
3852 overlay strings to display, IT->string and
3853 IT->current.overlay_string_index are set appropriately here.
3854 Otherwise IT->string is set to nil. */
3856 static void
3857 next_overlay_string (it)
3858 struct it *it;
3860 ++it->current.overlay_string_index;
3861 if (it->current.overlay_string_index == it->n_overlay_strings)
3863 /* No more overlay strings. Restore IT's settings to what
3864 they were before overlay strings were processed, and
3865 continue to deliver from current_buffer. */
3866 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3868 pop_it (it);
3869 xassert (it->stop_charpos >= BEGV
3870 && it->stop_charpos <= it->end_charpos);
3871 it->string = Qnil;
3872 it->current.overlay_string_index = -1;
3873 SET_TEXT_POS (it->current.string_pos, -1, -1);
3874 it->n_overlay_strings = 0;
3875 it->method = next_element_from_buffer;
3877 /* If we're at the end of the buffer, record that we have
3878 processed the overlay strings there already, so that
3879 next_element_from_buffer doesn't try it again. */
3880 if (IT_CHARPOS (*it) >= it->end_charpos)
3881 it->overlay_strings_at_end_processed_p = 1;
3883 /* If we have to display `...' for invisible text, set
3884 the iterator up for that. */
3885 if (display_ellipsis_p)
3886 setup_for_ellipsis (it);
3888 else
3890 /* There are more overlay strings to process. If
3891 IT->current.overlay_string_index has advanced to a position
3892 where we must load IT->overlay_strings with more strings, do
3893 it. */
3894 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3896 if (it->current.overlay_string_index && i == 0)
3897 load_overlay_strings (it, 0);
3899 /* Initialize IT to deliver display elements from the overlay
3900 string. */
3901 it->string = it->overlay_strings[i];
3902 it->multibyte_p = STRING_MULTIBYTE (it->string);
3903 SET_TEXT_POS (it->current.string_pos, 0, 0);
3904 it->method = next_element_from_string;
3905 it->stop_charpos = 0;
3908 CHECK_IT (it);
3912 /* Compare two overlay_entry structures E1 and E2. Used as a
3913 comparison function for qsort in load_overlay_strings. Overlay
3914 strings for the same position are sorted so that
3916 1. All after-strings come in front of before-strings, except
3917 when they come from the same overlay.
3919 2. Within after-strings, strings are sorted so that overlay strings
3920 from overlays with higher priorities come first.
3922 2. Within before-strings, strings are sorted so that overlay
3923 strings from overlays with higher priorities come last.
3925 Value is analogous to strcmp. */
3928 static int
3929 compare_overlay_entries (e1, e2)
3930 void *e1, *e2;
3932 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3933 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3934 int result;
3936 if (entry1->after_string_p != entry2->after_string_p)
3938 /* Let after-strings appear in front of before-strings if
3939 they come from different overlays. */
3940 if (EQ (entry1->overlay, entry2->overlay))
3941 result = entry1->after_string_p ? 1 : -1;
3942 else
3943 result = entry1->after_string_p ? -1 : 1;
3945 else if (entry1->after_string_p)
3946 /* After-strings sorted in order of decreasing priority. */
3947 result = entry2->priority - entry1->priority;
3948 else
3949 /* Before-strings sorted in order of increasing priority. */
3950 result = entry1->priority - entry2->priority;
3952 return result;
3956 /* Load the vector IT->overlay_strings with overlay strings from IT's
3957 current buffer position, or from CHARPOS if that is > 0. Set
3958 IT->n_overlays to the total number of overlay strings found.
3960 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3961 a time. On entry into load_overlay_strings,
3962 IT->current.overlay_string_index gives the number of overlay
3963 strings that have already been loaded by previous calls to this
3964 function.
3966 IT->add_overlay_start contains an additional overlay start
3967 position to consider for taking overlay strings from, if non-zero.
3968 This position comes into play when the overlay has an `invisible'
3969 property, and both before and after-strings. When we've skipped to
3970 the end of the overlay, because of its `invisible' property, we
3971 nevertheless want its before-string to appear.
3972 IT->add_overlay_start will contain the overlay start position
3973 in this case.
3975 Overlay strings are sorted so that after-string strings come in
3976 front of before-string strings. Within before and after-strings,
3977 strings are sorted by overlay priority. See also function
3978 compare_overlay_entries. */
3980 static void
3981 load_overlay_strings (it, charpos)
3982 struct it *it;
3983 int charpos;
3985 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3986 Lisp_Object overlay, window, str, invisible;
3987 struct Lisp_Overlay *ov;
3988 int start, end;
3989 int size = 20;
3990 int n = 0, i, j, invis_p;
3991 struct overlay_entry *entries
3992 = (struct overlay_entry *) alloca (size * sizeof *entries);
3994 if (charpos <= 0)
3995 charpos = IT_CHARPOS (*it);
3997 /* Append the overlay string STRING of overlay OVERLAY to vector
3998 `entries' which has size `size' and currently contains `n'
3999 elements. AFTER_P non-zero means STRING is an after-string of
4000 OVERLAY. */
4001 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4002 do \
4004 Lisp_Object priority; \
4006 if (n == size) \
4008 int new_size = 2 * size; \
4009 struct overlay_entry *old = entries; \
4010 entries = \
4011 (struct overlay_entry *) alloca (new_size \
4012 * sizeof *entries); \
4013 bcopy (old, entries, size * sizeof *entries); \
4014 size = new_size; \
4017 entries[n].string = (STRING); \
4018 entries[n].overlay = (OVERLAY); \
4019 priority = Foverlay_get ((OVERLAY), Qpriority); \
4020 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4021 entries[n].after_string_p = (AFTER_P); \
4022 ++n; \
4024 while (0)
4026 /* Process overlay before the overlay center. */
4027 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4029 XSETMISC (overlay, ov);
4030 xassert (OVERLAYP (overlay));
4031 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4032 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4034 if (end < charpos)
4035 break;
4037 /* Skip this overlay if it doesn't start or end at IT's current
4038 position. */
4039 if (end != charpos && start != charpos)
4040 continue;
4042 /* Skip this overlay if it doesn't apply to IT->w. */
4043 window = Foverlay_get (overlay, Qwindow);
4044 if (WINDOWP (window) && XWINDOW (window) != it->w)
4045 continue;
4047 /* If the text ``under'' the overlay is invisible, both before-
4048 and after-strings from this overlay are visible; start and
4049 end position are indistinguishable. */
4050 invisible = Foverlay_get (overlay, Qinvisible);
4051 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4053 /* If overlay has a non-empty before-string, record it. */
4054 if ((start == charpos || (end == charpos && invis_p))
4055 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4056 && SCHARS (str))
4057 RECORD_OVERLAY_STRING (overlay, str, 0);
4059 /* If overlay has a non-empty after-string, record it. */
4060 if ((end == charpos || (start == charpos && invis_p))
4061 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4062 && SCHARS (str))
4063 RECORD_OVERLAY_STRING (overlay, str, 1);
4066 /* Process overlays after the overlay center. */
4067 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4069 XSETMISC (overlay, ov);
4070 xassert (OVERLAYP (overlay));
4071 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4072 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4074 if (start > charpos)
4075 break;
4077 /* Skip this overlay if it doesn't start or end at IT's current
4078 position. */
4079 if (end != charpos && start != charpos)
4080 continue;
4082 /* Skip this overlay if it doesn't apply to IT->w. */
4083 window = Foverlay_get (overlay, Qwindow);
4084 if (WINDOWP (window) && XWINDOW (window) != it->w)
4085 continue;
4087 /* If the text ``under'' the overlay is invisible, it has a zero
4088 dimension, and both before- and after-strings apply. */
4089 invisible = Foverlay_get (overlay, Qinvisible);
4090 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4092 /* If overlay has a non-empty before-string, record it. */
4093 if ((start == charpos || (end == charpos && invis_p))
4094 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4095 && SCHARS (str))
4096 RECORD_OVERLAY_STRING (overlay, str, 0);
4098 /* If overlay has a non-empty after-string, record it. */
4099 if ((end == charpos || (start == charpos && invis_p))
4100 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4101 && SCHARS (str))
4102 RECORD_OVERLAY_STRING (overlay, str, 1);
4105 #undef RECORD_OVERLAY_STRING
4107 /* Sort entries. */
4108 if (n > 1)
4109 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4111 /* Record the total number of strings to process. */
4112 it->n_overlay_strings = n;
4114 /* IT->current.overlay_string_index is the number of overlay strings
4115 that have already been consumed by IT. Copy some of the
4116 remaining overlay strings to IT->overlay_strings. */
4117 i = 0;
4118 j = it->current.overlay_string_index;
4119 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4120 it->overlay_strings[i++] = entries[j++].string;
4122 CHECK_IT (it);
4126 /* Get the first chunk of overlay strings at IT's current buffer
4127 position, or at CHARPOS if that is > 0. Value is non-zero if at
4128 least one overlay string was found. */
4130 static int
4131 get_overlay_strings (it, charpos)
4132 struct it *it;
4133 int charpos;
4135 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4136 process. This fills IT->overlay_strings with strings, and sets
4137 IT->n_overlay_strings to the total number of strings to process.
4138 IT->pos.overlay_string_index has to be set temporarily to zero
4139 because load_overlay_strings needs this; it must be set to -1
4140 when no overlay strings are found because a zero value would
4141 indicate a position in the first overlay string. */
4142 it->current.overlay_string_index = 0;
4143 load_overlay_strings (it, charpos);
4145 /* If we found overlay strings, set up IT to deliver display
4146 elements from the first one. Otherwise set up IT to deliver
4147 from current_buffer. */
4148 if (it->n_overlay_strings)
4150 /* Make sure we know settings in current_buffer, so that we can
4151 restore meaningful values when we're done with the overlay
4152 strings. */
4153 compute_stop_pos (it);
4154 xassert (it->face_id >= 0);
4156 /* Save IT's settings. They are restored after all overlay
4157 strings have been processed. */
4158 xassert (it->sp == 0);
4159 push_it (it);
4161 /* Set up IT to deliver display elements from the first overlay
4162 string. */
4163 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4164 it->string = it->overlay_strings[0];
4165 it->stop_charpos = 0;
4166 xassert (STRINGP (it->string));
4167 it->end_charpos = SCHARS (it->string);
4168 it->multibyte_p = STRING_MULTIBYTE (it->string);
4169 it->method = next_element_from_string;
4171 else
4173 it->string = Qnil;
4174 it->current.overlay_string_index = -1;
4175 it->method = next_element_from_buffer;
4178 CHECK_IT (it);
4180 /* Value is non-zero if we found at least one overlay string. */
4181 return STRINGP (it->string);
4186 /***********************************************************************
4187 Saving and restoring state
4188 ***********************************************************************/
4190 /* Save current settings of IT on IT->stack. Called, for example,
4191 before setting up IT for an overlay string, to be able to restore
4192 IT's settings to what they were after the overlay string has been
4193 processed. */
4195 static void
4196 push_it (it)
4197 struct it *it;
4199 struct iterator_stack_entry *p;
4201 xassert (it->sp < 2);
4202 p = it->stack + it->sp;
4204 p->stop_charpos = it->stop_charpos;
4205 xassert (it->face_id >= 0);
4206 p->face_id = it->face_id;
4207 p->string = it->string;
4208 p->pos = it->current;
4209 p->end_charpos = it->end_charpos;
4210 p->string_nchars = it->string_nchars;
4211 p->area = it->area;
4212 p->multibyte_p = it->multibyte_p;
4213 p->space_width = it->space_width;
4214 p->font_height = it->font_height;
4215 p->voffset = it->voffset;
4216 p->string_from_display_prop_p = it->string_from_display_prop_p;
4217 p->display_ellipsis_p = 0;
4218 ++it->sp;
4222 /* Restore IT's settings from IT->stack. Called, for example, when no
4223 more overlay strings must be processed, and we return to delivering
4224 display elements from a buffer, or when the end of a string from a
4225 `display' property is reached and we return to delivering display
4226 elements from an overlay string, or from a buffer. */
4228 static void
4229 pop_it (it)
4230 struct it *it;
4232 struct iterator_stack_entry *p;
4234 xassert (it->sp > 0);
4235 --it->sp;
4236 p = it->stack + it->sp;
4237 it->stop_charpos = p->stop_charpos;
4238 it->face_id = p->face_id;
4239 it->string = p->string;
4240 it->current = p->pos;
4241 it->end_charpos = p->end_charpos;
4242 it->string_nchars = p->string_nchars;
4243 it->area = p->area;
4244 it->multibyte_p = p->multibyte_p;
4245 it->space_width = p->space_width;
4246 it->font_height = p->font_height;
4247 it->voffset = p->voffset;
4248 it->string_from_display_prop_p = p->string_from_display_prop_p;
4253 /***********************************************************************
4254 Moving over lines
4255 ***********************************************************************/
4257 /* Set IT's current position to the previous line start. */
4259 static void
4260 back_to_previous_line_start (it)
4261 struct it *it;
4263 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4264 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4268 /* Move IT to the next line start.
4270 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4271 we skipped over part of the text (as opposed to moving the iterator
4272 continuously over the text). Otherwise, don't change the value
4273 of *SKIPPED_P.
4275 Newlines may come from buffer text, overlay strings, or strings
4276 displayed via the `display' property. That's the reason we can't
4277 simply use find_next_newline_no_quit.
4279 Note that this function may not skip over invisible text that is so
4280 because of text properties and immediately follows a newline. If
4281 it would, function reseat_at_next_visible_line_start, when called
4282 from set_iterator_to_next, would effectively make invisible
4283 characters following a newline part of the wrong glyph row, which
4284 leads to wrong cursor motion. */
4286 static int
4287 forward_to_next_line_start (it, skipped_p)
4288 struct it *it;
4289 int *skipped_p;
4291 int old_selective, newline_found_p, n;
4292 const int MAX_NEWLINE_DISTANCE = 500;
4294 /* If already on a newline, just consume it to avoid unintended
4295 skipping over invisible text below. */
4296 if (it->what == IT_CHARACTER
4297 && it->c == '\n'
4298 && CHARPOS (it->position) == IT_CHARPOS (*it))
4300 set_iterator_to_next (it, 0);
4301 it->c = 0;
4302 return 1;
4305 /* Don't handle selective display in the following. It's (a)
4306 unnecessary because it's done by the caller, and (b) leads to an
4307 infinite recursion because next_element_from_ellipsis indirectly
4308 calls this function. */
4309 old_selective = it->selective;
4310 it->selective = 0;
4312 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4313 from buffer text. */
4314 for (n = newline_found_p = 0;
4315 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4316 n += STRINGP (it->string) ? 0 : 1)
4318 if (!get_next_display_element (it))
4319 return 0;
4320 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4321 set_iterator_to_next (it, 0);
4324 /* If we didn't find a newline near enough, see if we can use a
4325 short-cut. */
4326 if (!newline_found_p)
4328 int start = IT_CHARPOS (*it);
4329 int limit = find_next_newline_no_quit (start, 1);
4330 Lisp_Object pos;
4332 xassert (!STRINGP (it->string));
4334 /* If there isn't any `display' property in sight, and no
4335 overlays, we can just use the position of the newline in
4336 buffer text. */
4337 if (it->stop_charpos >= limit
4338 || ((pos = Fnext_single_property_change (make_number (start),
4339 Qdisplay,
4340 Qnil, make_number (limit)),
4341 NILP (pos))
4342 && next_overlay_change (start) == ZV))
4344 IT_CHARPOS (*it) = limit;
4345 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4346 *skipped_p = newline_found_p = 1;
4348 else
4350 while (get_next_display_element (it)
4351 && !newline_found_p)
4353 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4354 set_iterator_to_next (it, 0);
4359 it->selective = old_selective;
4360 return newline_found_p;
4364 /* Set IT's current position to the previous visible line start. Skip
4365 invisible text that is so either due to text properties or due to
4366 selective display. Caution: this does not change IT->current_x and
4367 IT->hpos. */
4369 static void
4370 back_to_previous_visible_line_start (it)
4371 struct it *it;
4373 int visible_p = 0;
4375 /* Go back one newline if not on BEGV already. */
4376 if (IT_CHARPOS (*it) > BEGV)
4377 back_to_previous_line_start (it);
4379 /* Move over lines that are invisible because of selective display
4380 or text properties. */
4381 while (IT_CHARPOS (*it) > BEGV
4382 && !visible_p)
4384 visible_p = 1;
4386 /* If selective > 0, then lines indented more than that values
4387 are invisible. */
4388 if (it->selective > 0
4389 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4390 (double) it->selective)) /* iftc */
4391 visible_p = 0;
4392 else
4394 Lisp_Object prop;
4396 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4397 Qinvisible, it->window);
4398 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4399 visible_p = 0;
4402 /* Back one more newline if the current one is invisible. */
4403 if (!visible_p)
4404 back_to_previous_line_start (it);
4407 xassert (IT_CHARPOS (*it) >= BEGV);
4408 xassert (IT_CHARPOS (*it) == BEGV
4409 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4410 CHECK_IT (it);
4414 /* Reseat iterator IT at the previous visible line start. Skip
4415 invisible text that is so either due to text properties or due to
4416 selective display. At the end, update IT's overlay information,
4417 face information etc. */
4419 static void
4420 reseat_at_previous_visible_line_start (it)
4421 struct it *it;
4423 back_to_previous_visible_line_start (it);
4424 reseat (it, it->current.pos, 1);
4425 CHECK_IT (it);
4429 /* Reseat iterator IT on the next visible line start in the current
4430 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4431 preceding the line start. Skip over invisible text that is so
4432 because of selective display. Compute faces, overlays etc at the
4433 new position. Note that this function does not skip over text that
4434 is invisible because of text properties. */
4436 static void
4437 reseat_at_next_visible_line_start (it, on_newline_p)
4438 struct it *it;
4439 int on_newline_p;
4441 int newline_found_p, skipped_p = 0;
4443 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4445 /* Skip over lines that are invisible because they are indented
4446 more than the value of IT->selective. */
4447 if (it->selective > 0)
4448 while (IT_CHARPOS (*it) < ZV
4449 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4450 (double) it->selective)) /* iftc */
4452 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4453 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4456 /* Position on the newline if that's what's requested. */
4457 if (on_newline_p && newline_found_p)
4459 if (STRINGP (it->string))
4461 if (IT_STRING_CHARPOS (*it) > 0)
4463 --IT_STRING_CHARPOS (*it);
4464 --IT_STRING_BYTEPOS (*it);
4467 else if (IT_CHARPOS (*it) > BEGV)
4469 --IT_CHARPOS (*it);
4470 --IT_BYTEPOS (*it);
4471 reseat (it, it->current.pos, 0);
4474 else if (skipped_p)
4475 reseat (it, it->current.pos, 0);
4477 CHECK_IT (it);
4482 /***********************************************************************
4483 Changing an iterator's position
4484 ***********************************************************************/
4486 /* Change IT's current position to POS in current_buffer. If FORCE_P
4487 is non-zero, always check for text properties at the new position.
4488 Otherwise, text properties are only looked up if POS >=
4489 IT->check_charpos of a property. */
4491 static void
4492 reseat (it, pos, force_p)
4493 struct it *it;
4494 struct text_pos pos;
4495 int force_p;
4497 int original_pos = IT_CHARPOS (*it);
4499 reseat_1 (it, pos, 0);
4501 /* Determine where to check text properties. Avoid doing it
4502 where possible because text property lookup is very expensive. */
4503 if (force_p
4504 || CHARPOS (pos) > it->stop_charpos
4505 || CHARPOS (pos) < original_pos)
4506 handle_stop (it);
4508 CHECK_IT (it);
4512 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4513 IT->stop_pos to POS, also. */
4515 static void
4516 reseat_1 (it, pos, set_stop_p)
4517 struct it *it;
4518 struct text_pos pos;
4519 int set_stop_p;
4521 /* Don't call this function when scanning a C string. */
4522 xassert (it->s == NULL);
4524 /* POS must be a reasonable value. */
4525 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4527 it->current.pos = it->position = pos;
4528 XSETBUFFER (it->object, current_buffer);
4529 it->end_charpos = ZV;
4530 it->dpvec = NULL;
4531 it->current.dpvec_index = -1;
4532 it->current.overlay_string_index = -1;
4533 IT_STRING_CHARPOS (*it) = -1;
4534 IT_STRING_BYTEPOS (*it) = -1;
4535 it->string = Qnil;
4536 it->method = next_element_from_buffer;
4537 /* RMS: I added this to fix a bug in move_it_vertically_backward
4538 where it->area continued to relate to the starting point
4539 for the backward motion. Bug report from
4540 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4541 However, I am not sure whether reseat still does the right thing
4542 in general after this change. */
4543 it->area = TEXT_AREA;
4544 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4545 it->sp = 0;
4546 it->face_before_selective_p = 0;
4548 if (set_stop_p)
4549 it->stop_charpos = CHARPOS (pos);
4553 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4554 If S is non-null, it is a C string to iterate over. Otherwise,
4555 STRING gives a Lisp string to iterate over.
4557 If PRECISION > 0, don't return more then PRECISION number of
4558 characters from the string.
4560 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4561 characters have been returned. FIELD_WIDTH < 0 means an infinite
4562 field width.
4564 MULTIBYTE = 0 means disable processing of multibyte characters,
4565 MULTIBYTE > 0 means enable it,
4566 MULTIBYTE < 0 means use IT->multibyte_p.
4568 IT must be initialized via a prior call to init_iterator before
4569 calling this function. */
4571 static void
4572 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4573 struct it *it;
4574 unsigned char *s;
4575 Lisp_Object string;
4576 int charpos;
4577 int precision, field_width, multibyte;
4579 /* No region in strings. */
4580 it->region_beg_charpos = it->region_end_charpos = -1;
4582 /* No text property checks performed by default, but see below. */
4583 it->stop_charpos = -1;
4585 /* Set iterator position and end position. */
4586 bzero (&it->current, sizeof it->current);
4587 it->current.overlay_string_index = -1;
4588 it->current.dpvec_index = -1;
4589 xassert (charpos >= 0);
4591 /* If STRING is specified, use its multibyteness, otherwise use the
4592 setting of MULTIBYTE, if specified. */
4593 if (multibyte >= 0)
4594 it->multibyte_p = multibyte > 0;
4596 if (s == NULL)
4598 xassert (STRINGP (string));
4599 it->string = string;
4600 it->s = NULL;
4601 it->end_charpos = it->string_nchars = SCHARS (string);
4602 it->method = next_element_from_string;
4603 it->current.string_pos = string_pos (charpos, string);
4605 else
4607 it->s = s;
4608 it->string = Qnil;
4610 /* Note that we use IT->current.pos, not it->current.string_pos,
4611 for displaying C strings. */
4612 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4613 if (it->multibyte_p)
4615 it->current.pos = c_string_pos (charpos, s, 1);
4616 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4618 else
4620 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4621 it->end_charpos = it->string_nchars = strlen (s);
4624 it->method = next_element_from_c_string;
4627 /* PRECISION > 0 means don't return more than PRECISION characters
4628 from the string. */
4629 if (precision > 0 && it->end_charpos - charpos > precision)
4630 it->end_charpos = it->string_nchars = charpos + precision;
4632 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4633 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4634 FIELD_WIDTH < 0 means infinite field width. This is useful for
4635 padding with `-' at the end of a mode line. */
4636 if (field_width < 0)
4637 field_width = INFINITY;
4638 if (field_width > it->end_charpos - charpos)
4639 it->end_charpos = charpos + field_width;
4641 /* Use the standard display table for displaying strings. */
4642 if (DISP_TABLE_P (Vstandard_display_table))
4643 it->dp = XCHAR_TABLE (Vstandard_display_table);
4645 it->stop_charpos = charpos;
4646 CHECK_IT (it);
4651 /***********************************************************************
4652 Iteration
4653 ***********************************************************************/
4655 /* Load IT's display element fields with information about the next
4656 display element from the current position of IT. Value is zero if
4657 end of buffer (or C string) is reached. */
4660 get_next_display_element (it)
4661 struct it *it;
4663 /* Non-zero means that we found a display element. Zero means that
4664 we hit the end of what we iterate over. Performance note: the
4665 function pointer `method' used here turns out to be faster than
4666 using a sequence of if-statements. */
4667 int success_p = (*it->method) (it);
4669 if (it->what == IT_CHARACTER)
4671 /* Map via display table or translate control characters.
4672 IT->c, IT->len etc. have been set to the next character by
4673 the function call above. If we have a display table, and it
4674 contains an entry for IT->c, translate it. Don't do this if
4675 IT->c itself comes from a display table, otherwise we could
4676 end up in an infinite recursion. (An alternative could be to
4677 count the recursion depth of this function and signal an
4678 error when a certain maximum depth is reached.) Is it worth
4679 it? */
4680 if (success_p && it->dpvec == NULL)
4682 Lisp_Object dv;
4684 if (it->dp
4685 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4686 VECTORP (dv)))
4688 struct Lisp_Vector *v = XVECTOR (dv);
4690 /* Return the first character from the display table
4691 entry, if not empty. If empty, don't display the
4692 current character. */
4693 if (v->size)
4695 it->dpvec_char_len = it->len;
4696 it->dpvec = v->contents;
4697 it->dpend = v->contents + v->size;
4698 it->current.dpvec_index = 0;
4699 it->method = next_element_from_display_vector;
4700 success_p = get_next_display_element (it);
4702 else
4704 set_iterator_to_next (it, 0);
4705 success_p = get_next_display_element (it);
4709 /* Translate control characters into `\003' or `^C' form.
4710 Control characters coming from a display table entry are
4711 currently not translated because we use IT->dpvec to hold
4712 the translation. This could easily be changed but I
4713 don't believe that it is worth doing.
4715 If it->multibyte_p is nonzero, eight-bit characters and
4716 non-printable multibyte characters are also translated to
4717 octal form.
4719 If it->multibyte_p is zero, eight-bit characters that
4720 don't have corresponding multibyte char code are also
4721 translated to octal form. */
4722 else if ((it->c < ' '
4723 && (it->area != TEXT_AREA
4724 || (it->c != '\n' && it->c != '\t')))
4725 || (it->multibyte_p
4726 ? ((it->c >= 127
4727 && it->len == 1)
4728 || !CHAR_PRINTABLE_P (it->c))
4729 : (it->c >= 127
4730 && it->c == unibyte_char_to_multibyte (it->c))))
4732 /* IT->c is a control character which must be displayed
4733 either as '\003' or as `^C' where the '\\' and '^'
4734 can be defined in the display table. Fill
4735 IT->ctl_chars with glyphs for what we have to
4736 display. Then, set IT->dpvec to these glyphs. */
4737 GLYPH g;
4739 if (it->c < 128 && it->ctl_arrow_p)
4741 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4742 if (it->dp
4743 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4744 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4745 g = XINT (DISP_CTRL_GLYPH (it->dp));
4746 else
4747 g = FAST_MAKE_GLYPH ('^', 0);
4748 XSETINT (it->ctl_chars[0], g);
4750 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4751 XSETINT (it->ctl_chars[1], g);
4753 /* Set up IT->dpvec and return first character from it. */
4754 it->dpvec_char_len = it->len;
4755 it->dpvec = it->ctl_chars;
4756 it->dpend = it->dpvec + 2;
4757 it->current.dpvec_index = 0;
4758 it->method = next_element_from_display_vector;
4759 get_next_display_element (it);
4761 else
4763 unsigned char str[MAX_MULTIBYTE_LENGTH];
4764 int len;
4765 int i;
4766 GLYPH escape_glyph;
4768 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4769 if (it->dp
4770 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4771 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4772 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4773 else
4774 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4776 if (SINGLE_BYTE_CHAR_P (it->c))
4777 str[0] = it->c, len = 1;
4778 else
4780 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4781 if (len < 0)
4783 /* It's an invalid character, which
4784 shouldn't happen actually, but due to
4785 bugs it may happen. Let's print the char
4786 as is, there's not much meaningful we can
4787 do with it. */
4788 str[0] = it->c;
4789 str[1] = it->c >> 8;
4790 str[2] = it->c >> 16;
4791 str[3] = it->c >> 24;
4792 len = 4;
4796 for (i = 0; i < len; i++)
4798 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4799 /* Insert three more glyphs into IT->ctl_chars for
4800 the octal display of the character. */
4801 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4802 XSETINT (it->ctl_chars[i * 4 + 1], g);
4803 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4804 XSETINT (it->ctl_chars[i * 4 + 2], g);
4805 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4806 XSETINT (it->ctl_chars[i * 4 + 3], g);
4809 /* Set up IT->dpvec and return the first character
4810 from it. */
4811 it->dpvec_char_len = it->len;
4812 it->dpvec = it->ctl_chars;
4813 it->dpend = it->dpvec + len * 4;
4814 it->current.dpvec_index = 0;
4815 it->method = next_element_from_display_vector;
4816 get_next_display_element (it);
4821 /* Adjust face id for a multibyte character. There are no
4822 multibyte character in unibyte text. */
4823 if (it->multibyte_p
4824 && success_p
4825 && FRAME_WINDOW_P (it->f))
4827 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4828 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4832 /* Is this character the last one of a run of characters with
4833 box? If yes, set IT->end_of_box_run_p to 1. */
4834 if (it->face_box_p
4835 && it->s == NULL)
4837 int face_id;
4838 struct face *face;
4840 it->end_of_box_run_p
4841 = ((face_id = face_after_it_pos (it),
4842 face_id != it->face_id)
4843 && (face = FACE_FROM_ID (it->f, face_id),
4844 face->box == FACE_NO_BOX));
4847 /* Value is 0 if end of buffer or string reached. */
4848 return success_p;
4852 /* Move IT to the next display element.
4854 RESEAT_P non-zero means if called on a newline in buffer text,
4855 skip to the next visible line start.
4857 Functions get_next_display_element and set_iterator_to_next are
4858 separate because I find this arrangement easier to handle than a
4859 get_next_display_element function that also increments IT's
4860 position. The way it is we can first look at an iterator's current
4861 display element, decide whether it fits on a line, and if it does,
4862 increment the iterator position. The other way around we probably
4863 would either need a flag indicating whether the iterator has to be
4864 incremented the next time, or we would have to implement a
4865 decrement position function which would not be easy to write. */
4867 void
4868 set_iterator_to_next (it, reseat_p)
4869 struct it *it;
4870 int reseat_p;
4872 /* Reset flags indicating start and end of a sequence of characters
4873 with box. Reset them at the start of this function because
4874 moving the iterator to a new position might set them. */
4875 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4877 if (it->method == next_element_from_buffer)
4879 /* The current display element of IT is a character from
4880 current_buffer. Advance in the buffer, and maybe skip over
4881 invisible lines that are so because of selective display. */
4882 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4883 reseat_at_next_visible_line_start (it, 0);
4884 else
4886 xassert (it->len != 0);
4887 IT_BYTEPOS (*it) += it->len;
4888 IT_CHARPOS (*it) += 1;
4889 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4892 else if (it->method == next_element_from_composition)
4894 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4895 if (STRINGP (it->string))
4897 IT_STRING_BYTEPOS (*it) += it->len;
4898 IT_STRING_CHARPOS (*it) += it->cmp_len;
4899 it->method = next_element_from_string;
4900 goto consider_string_end;
4902 else
4904 IT_BYTEPOS (*it) += it->len;
4905 IT_CHARPOS (*it) += it->cmp_len;
4906 it->method = next_element_from_buffer;
4909 else if (it->method == next_element_from_c_string)
4911 /* Current display element of IT is from a C string. */
4912 IT_BYTEPOS (*it) += it->len;
4913 IT_CHARPOS (*it) += 1;
4915 else if (it->method == next_element_from_display_vector)
4917 /* Current display element of IT is from a display table entry.
4918 Advance in the display table definition. Reset it to null if
4919 end reached, and continue with characters from buffers/
4920 strings. */
4921 ++it->current.dpvec_index;
4923 /* Restore face of the iterator to what they were before the
4924 display vector entry (these entries may contain faces). */
4925 it->face_id = it->saved_face_id;
4927 if (it->dpvec + it->current.dpvec_index == it->dpend)
4929 if (it->s)
4930 it->method = next_element_from_c_string;
4931 else if (STRINGP (it->string))
4932 it->method = next_element_from_string;
4933 else
4934 it->method = next_element_from_buffer;
4936 it->dpvec = NULL;
4937 it->current.dpvec_index = -1;
4939 /* Skip over characters which were displayed via IT->dpvec. */
4940 if (it->dpvec_char_len < 0)
4941 reseat_at_next_visible_line_start (it, 1);
4942 else if (it->dpvec_char_len > 0)
4944 it->len = it->dpvec_char_len;
4945 set_iterator_to_next (it, reseat_p);
4949 else if (it->method == next_element_from_string)
4951 /* Current display element is a character from a Lisp string. */
4952 xassert (it->s == NULL && STRINGP (it->string));
4953 IT_STRING_BYTEPOS (*it) += it->len;
4954 IT_STRING_CHARPOS (*it) += 1;
4956 consider_string_end:
4958 if (it->current.overlay_string_index >= 0)
4960 /* IT->string is an overlay string. Advance to the
4961 next, if there is one. */
4962 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4963 next_overlay_string (it);
4965 else
4967 /* IT->string is not an overlay string. If we reached
4968 its end, and there is something on IT->stack, proceed
4969 with what is on the stack. This can be either another
4970 string, this time an overlay string, or a buffer. */
4971 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4972 && it->sp > 0)
4974 pop_it (it);
4975 if (!STRINGP (it->string))
4976 it->method = next_element_from_buffer;
4977 else
4978 goto consider_string_end;
4982 else if (it->method == next_element_from_image
4983 || it->method == next_element_from_stretch)
4985 /* The position etc with which we have to proceed are on
4986 the stack. The position may be at the end of a string,
4987 if the `display' property takes up the whole string. */
4988 pop_it (it);
4989 it->image_id = 0;
4990 if (STRINGP (it->string))
4992 it->method = next_element_from_string;
4993 goto consider_string_end;
4995 else
4996 it->method = next_element_from_buffer;
4998 else
4999 /* There are no other methods defined, so this should be a bug. */
5000 abort ();
5002 xassert (it->method != next_element_from_string
5003 || (STRINGP (it->string)
5004 && IT_STRING_CHARPOS (*it) >= 0));
5008 /* Load IT's display element fields with information about the next
5009 display element which comes from a display table entry or from the
5010 result of translating a control character to one of the forms `^C'
5011 or `\003'. IT->dpvec holds the glyphs to return as characters. */
5013 static int
5014 next_element_from_display_vector (it)
5015 struct it *it;
5017 /* Precondition. */
5018 xassert (it->dpvec && it->current.dpvec_index >= 0);
5020 /* Remember the current face id in case glyphs specify faces.
5021 IT's face is restored in set_iterator_to_next. */
5022 it->saved_face_id = it->face_id;
5024 if (INTEGERP (*it->dpvec)
5025 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5027 int lface_id;
5028 GLYPH g;
5030 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5031 it->c = FAST_GLYPH_CHAR (g);
5032 it->len = CHAR_BYTES (it->c);
5034 /* The entry may contain a face id to use. Such a face id is
5035 the id of a Lisp face, not a realized face. A face id of
5036 zero means no face is specified. */
5037 lface_id = FAST_GLYPH_FACE (g);
5038 if (lface_id)
5040 /* The function returns -1 if lface_id is invalid. */
5041 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5042 if (face_id >= 0)
5043 it->face_id = face_id;
5046 else
5047 /* Display table entry is invalid. Return a space. */
5048 it->c = ' ', it->len = 1;
5050 /* Don't change position and object of the iterator here. They are
5051 still the values of the character that had this display table
5052 entry or was translated, and that's what we want. */
5053 it->what = IT_CHARACTER;
5054 return 1;
5058 /* Load IT with the next display element from Lisp string IT->string.
5059 IT->current.string_pos is the current position within the string.
5060 If IT->current.overlay_string_index >= 0, the Lisp string is an
5061 overlay string. */
5063 static int
5064 next_element_from_string (it)
5065 struct it *it;
5067 struct text_pos position;
5069 xassert (STRINGP (it->string));
5070 xassert (IT_STRING_CHARPOS (*it) >= 0);
5071 position = it->current.string_pos;
5073 /* Time to check for invisible text? */
5074 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5075 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5077 handle_stop (it);
5079 /* Since a handler may have changed IT->method, we must
5080 recurse here. */
5081 return get_next_display_element (it);
5084 if (it->current.overlay_string_index >= 0)
5086 /* Get the next character from an overlay string. In overlay
5087 strings, There is no field width or padding with spaces to
5088 do. */
5089 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5091 it->what = IT_EOB;
5092 return 0;
5094 else if (STRING_MULTIBYTE (it->string))
5096 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5097 const unsigned char *s = (SDATA (it->string)
5098 + IT_STRING_BYTEPOS (*it));
5099 it->c = string_char_and_length (s, remaining, &it->len);
5101 else
5103 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5104 it->len = 1;
5107 else
5109 /* Get the next character from a Lisp string that is not an
5110 overlay string. Such strings come from the mode line, for
5111 example. We may have to pad with spaces, or truncate the
5112 string. See also next_element_from_c_string. */
5113 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5115 it->what = IT_EOB;
5116 return 0;
5118 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5120 /* Pad with spaces. */
5121 it->c = ' ', it->len = 1;
5122 CHARPOS (position) = BYTEPOS (position) = -1;
5124 else if (STRING_MULTIBYTE (it->string))
5126 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5127 const unsigned char *s = (SDATA (it->string)
5128 + IT_STRING_BYTEPOS (*it));
5129 it->c = string_char_and_length (s, maxlen, &it->len);
5131 else
5133 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5134 it->len = 1;
5138 /* Record what we have and where it came from. Note that we store a
5139 buffer position in IT->position although it could arguably be a
5140 string position. */
5141 it->what = IT_CHARACTER;
5142 it->object = it->string;
5143 it->position = position;
5144 return 1;
5148 /* Load IT with next display element from C string IT->s.
5149 IT->string_nchars is the maximum number of characters to return
5150 from the string. IT->end_charpos may be greater than
5151 IT->string_nchars when this function is called, in which case we
5152 may have to return padding spaces. Value is zero if end of string
5153 reached, including padding spaces. */
5155 static int
5156 next_element_from_c_string (it)
5157 struct it *it;
5159 int success_p = 1;
5161 xassert (it->s);
5162 it->what = IT_CHARACTER;
5163 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5164 it->object = Qnil;
5166 /* IT's position can be greater IT->string_nchars in case a field
5167 width or precision has been specified when the iterator was
5168 initialized. */
5169 if (IT_CHARPOS (*it) >= it->end_charpos)
5171 /* End of the game. */
5172 it->what = IT_EOB;
5173 success_p = 0;
5175 else if (IT_CHARPOS (*it) >= it->string_nchars)
5177 /* Pad with spaces. */
5178 it->c = ' ', it->len = 1;
5179 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5181 else if (it->multibyte_p)
5183 /* Implementation note: The calls to strlen apparently aren't a
5184 performance problem because there is no noticeable performance
5185 difference between Emacs running in unibyte or multibyte mode. */
5186 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5187 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5188 maxlen, &it->len);
5190 else
5191 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5193 return success_p;
5197 /* Set up IT to return characters from an ellipsis, if appropriate.
5198 The definition of the ellipsis glyphs may come from a display table
5199 entry. This function Fills IT with the first glyph from the
5200 ellipsis if an ellipsis is to be displayed. */
5202 static int
5203 next_element_from_ellipsis (it)
5204 struct it *it;
5206 if (it->selective_display_ellipsis_p)
5208 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5210 /* Use the display table definition for `...'. Invalid glyphs
5211 will be handled by the method returning elements from dpvec. */
5212 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5213 it->dpvec_char_len = it->len;
5214 it->dpvec = v->contents;
5215 it->dpend = v->contents + v->size;
5216 it->current.dpvec_index = 0;
5217 it->method = next_element_from_display_vector;
5219 else
5221 /* Use default `...' which is stored in default_invis_vector. */
5222 it->dpvec_char_len = it->len;
5223 it->dpvec = default_invis_vector;
5224 it->dpend = default_invis_vector + 3;
5225 it->current.dpvec_index = 0;
5226 it->method = next_element_from_display_vector;
5229 else
5231 /* The face at the current position may be different from the
5232 face we find after the invisible text. Remember what it
5233 was in IT->saved_face_id, and signal that it's there by
5234 setting face_before_selective_p. */
5235 it->saved_face_id = it->face_id;
5236 it->method = next_element_from_buffer;
5237 reseat_at_next_visible_line_start (it, 1);
5238 it->face_before_selective_p = 1;
5241 return get_next_display_element (it);
5245 /* Deliver an image display element. The iterator IT is already
5246 filled with image information (done in handle_display_prop). Value
5247 is always 1. */
5250 static int
5251 next_element_from_image (it)
5252 struct it *it;
5254 it->what = IT_IMAGE;
5255 return 1;
5259 /* Fill iterator IT with next display element from a stretch glyph
5260 property. IT->object is the value of the text property. Value is
5261 always 1. */
5263 static int
5264 next_element_from_stretch (it)
5265 struct it *it;
5267 it->what = IT_STRETCH;
5268 return 1;
5272 /* Load IT with the next display element from current_buffer. Value
5273 is zero if end of buffer reached. IT->stop_charpos is the next
5274 position at which to stop and check for text properties or buffer
5275 end. */
5277 static int
5278 next_element_from_buffer (it)
5279 struct it *it;
5281 int success_p = 1;
5283 /* Check this assumption, otherwise, we would never enter the
5284 if-statement, below. */
5285 xassert (IT_CHARPOS (*it) >= BEGV
5286 && IT_CHARPOS (*it) <= it->stop_charpos);
5288 if (IT_CHARPOS (*it) >= it->stop_charpos)
5290 if (IT_CHARPOS (*it) >= it->end_charpos)
5292 int overlay_strings_follow_p;
5294 /* End of the game, except when overlay strings follow that
5295 haven't been returned yet. */
5296 if (it->overlay_strings_at_end_processed_p)
5297 overlay_strings_follow_p = 0;
5298 else
5300 it->overlay_strings_at_end_processed_p = 1;
5301 overlay_strings_follow_p = get_overlay_strings (it, 0);
5304 if (overlay_strings_follow_p)
5305 success_p = get_next_display_element (it);
5306 else
5308 it->what = IT_EOB;
5309 it->position = it->current.pos;
5310 success_p = 0;
5313 else
5315 handle_stop (it);
5316 return get_next_display_element (it);
5319 else
5321 /* No face changes, overlays etc. in sight, so just return a
5322 character from current_buffer. */
5323 unsigned char *p;
5325 /* Maybe run the redisplay end trigger hook. Performance note:
5326 This doesn't seem to cost measurable time. */
5327 if (it->redisplay_end_trigger_charpos
5328 && it->glyph_row
5329 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5330 run_redisplay_end_trigger_hook (it);
5332 /* Get the next character, maybe multibyte. */
5333 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5334 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5336 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5337 - IT_BYTEPOS (*it));
5338 it->c = string_char_and_length (p, maxlen, &it->len);
5340 else
5341 it->c = *p, it->len = 1;
5343 /* Record what we have and where it came from. */
5344 it->what = IT_CHARACTER;;
5345 it->object = it->w->buffer;
5346 it->position = it->current.pos;
5348 /* Normally we return the character found above, except when we
5349 really want to return an ellipsis for selective display. */
5350 if (it->selective)
5352 if (it->c == '\n')
5354 /* A value of selective > 0 means hide lines indented more
5355 than that number of columns. */
5356 if (it->selective > 0
5357 && IT_CHARPOS (*it) + 1 < ZV
5358 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5359 IT_BYTEPOS (*it) + 1,
5360 (double) it->selective)) /* iftc */
5362 success_p = next_element_from_ellipsis (it);
5363 it->dpvec_char_len = -1;
5366 else if (it->c == '\r' && it->selective == -1)
5368 /* A value of selective == -1 means that everything from the
5369 CR to the end of the line is invisible, with maybe an
5370 ellipsis displayed for it. */
5371 success_p = next_element_from_ellipsis (it);
5372 it->dpvec_char_len = -1;
5377 /* Value is zero if end of buffer reached. */
5378 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5379 return success_p;
5383 /* Run the redisplay end trigger hook for IT. */
5385 static void
5386 run_redisplay_end_trigger_hook (it)
5387 struct it *it;
5389 Lisp_Object args[3];
5391 /* IT->glyph_row should be non-null, i.e. we should be actually
5392 displaying something, or otherwise we should not run the hook. */
5393 xassert (it->glyph_row);
5395 /* Set up hook arguments. */
5396 args[0] = Qredisplay_end_trigger_functions;
5397 args[1] = it->window;
5398 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5399 it->redisplay_end_trigger_charpos = 0;
5401 /* Since we are *trying* to run these functions, don't try to run
5402 them again, even if they get an error. */
5403 it->w->redisplay_end_trigger = Qnil;
5404 Frun_hook_with_args (3, args);
5406 /* Notice if it changed the face of the character we are on. */
5407 handle_face_prop (it);
5411 /* Deliver a composition display element. The iterator IT is already
5412 filled with composition information (done in
5413 handle_composition_prop). Value is always 1. */
5415 static int
5416 next_element_from_composition (it)
5417 struct it *it;
5419 it->what = IT_COMPOSITION;
5420 it->position = (STRINGP (it->string)
5421 ? it->current.string_pos
5422 : it->current.pos);
5423 return 1;
5428 /***********************************************************************
5429 Moving an iterator without producing glyphs
5430 ***********************************************************************/
5432 /* Move iterator IT to a specified buffer or X position within one
5433 line on the display without producing glyphs.
5435 OP should be a bit mask including some or all of these bits:
5436 MOVE_TO_X: Stop on reaching x-position TO_X.
5437 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5438 Regardless of OP's value, stop in reaching the end of the display line.
5440 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5441 This means, in particular, that TO_X includes window's horizontal
5442 scroll amount.
5444 The return value has several possible values that
5445 say what condition caused the scan to stop:
5447 MOVE_POS_MATCH_OR_ZV
5448 - when TO_POS or ZV was reached.
5450 MOVE_X_REACHED
5451 -when TO_X was reached before TO_POS or ZV were reached.
5453 MOVE_LINE_CONTINUED
5454 - when we reached the end of the display area and the line must
5455 be continued.
5457 MOVE_LINE_TRUNCATED
5458 - when we reached the end of the display area and the line is
5459 truncated.
5461 MOVE_NEWLINE_OR_CR
5462 - when we stopped at a line end, i.e. a newline or a CR and selective
5463 display is on. */
5465 static enum move_it_result
5466 move_it_in_display_line_to (it, to_charpos, to_x, op)
5467 struct it *it;
5468 int to_charpos, to_x, op;
5470 enum move_it_result result = MOVE_UNDEFINED;
5471 struct glyph_row *saved_glyph_row;
5473 /* Don't produce glyphs in produce_glyphs. */
5474 saved_glyph_row = it->glyph_row;
5475 it->glyph_row = NULL;
5477 while (1)
5479 int x, i, ascent = 0, descent = 0;
5481 /* Stop when ZV or TO_CHARPOS reached. */
5482 if (!get_next_display_element (it)
5483 || ((op & MOVE_TO_POS) != 0
5484 && BUFFERP (it->object)
5485 && IT_CHARPOS (*it) >= to_charpos))
5487 result = MOVE_POS_MATCH_OR_ZV;
5488 break;
5491 /* The call to produce_glyphs will get the metrics of the
5492 display element IT is loaded with. We record in x the
5493 x-position before this display element in case it does not
5494 fit on the line. */
5495 x = it->current_x;
5497 /* Remember the line height so far in case the next element doesn't
5498 fit on the line. */
5499 if (!it->truncate_lines_p)
5501 ascent = it->max_ascent;
5502 descent = it->max_descent;
5505 PRODUCE_GLYPHS (it);
5507 if (it->area != TEXT_AREA)
5509 set_iterator_to_next (it, 1);
5510 continue;
5513 /* The number of glyphs we get back in IT->nglyphs will normally
5514 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5515 character on a terminal frame, or (iii) a line end. For the
5516 second case, IT->nglyphs - 1 padding glyphs will be present
5517 (on X frames, there is only one glyph produced for a
5518 composite character.
5520 The behavior implemented below means, for continuation lines,
5521 that as many spaces of a TAB as fit on the current line are
5522 displayed there. For terminal frames, as many glyphs of a
5523 multi-glyph character are displayed in the current line, too.
5524 This is what the old redisplay code did, and we keep it that
5525 way. Under X, the whole shape of a complex character must
5526 fit on the line or it will be completely displayed in the
5527 next line.
5529 Note that both for tabs and padding glyphs, all glyphs have
5530 the same width. */
5531 if (it->nglyphs)
5533 /* More than one glyph or glyph doesn't fit on line. All
5534 glyphs have the same width. */
5535 int single_glyph_width = it->pixel_width / it->nglyphs;
5536 int new_x;
5538 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5540 new_x = x + single_glyph_width;
5542 /* We want to leave anything reaching TO_X to the caller. */
5543 if ((op & MOVE_TO_X) && new_x > to_x)
5545 it->current_x = x;
5546 result = MOVE_X_REACHED;
5547 break;
5549 else if (/* Lines are continued. */
5550 !it->truncate_lines_p
5551 && (/* And glyph doesn't fit on the line. */
5552 new_x > it->last_visible_x
5553 /* Or it fits exactly and we're on a window
5554 system frame. */
5555 || (new_x == it->last_visible_x
5556 && FRAME_WINDOW_P (it->f))))
5558 if (/* IT->hpos == 0 means the very first glyph
5559 doesn't fit on the line, e.g. a wide image. */
5560 it->hpos == 0
5561 || (new_x == it->last_visible_x
5562 && FRAME_WINDOW_P (it->f)))
5564 ++it->hpos;
5565 it->current_x = new_x;
5566 if (i == it->nglyphs - 1)
5567 set_iterator_to_next (it, 1);
5569 else
5571 it->current_x = x;
5572 it->max_ascent = ascent;
5573 it->max_descent = descent;
5576 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5577 IT_CHARPOS (*it)));
5578 result = MOVE_LINE_CONTINUED;
5579 break;
5581 else if (new_x > it->first_visible_x)
5583 /* Glyph is visible. Increment number of glyphs that
5584 would be displayed. */
5585 ++it->hpos;
5587 else
5589 /* Glyph is completely off the left margin of the display
5590 area. Nothing to do. */
5594 if (result != MOVE_UNDEFINED)
5595 break;
5597 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5599 /* Stop when TO_X specified and reached. This check is
5600 necessary here because of lines consisting of a line end,
5601 only. The line end will not produce any glyphs and we
5602 would never get MOVE_X_REACHED. */
5603 xassert (it->nglyphs == 0);
5604 result = MOVE_X_REACHED;
5605 break;
5608 /* Is this a line end? If yes, we're done. */
5609 if (ITERATOR_AT_END_OF_LINE_P (it))
5611 result = MOVE_NEWLINE_OR_CR;
5612 break;
5615 /* The current display element has been consumed. Advance
5616 to the next. */
5617 set_iterator_to_next (it, 1);
5619 /* Stop if lines are truncated and IT's current x-position is
5620 past the right edge of the window now. */
5621 if (it->truncate_lines_p
5622 && it->current_x >= it->last_visible_x)
5624 result = MOVE_LINE_TRUNCATED;
5625 break;
5629 /* Restore the iterator settings altered at the beginning of this
5630 function. */
5631 it->glyph_row = saved_glyph_row;
5632 return result;
5636 /* Move IT forward until it satisfies one or more of the criteria in
5637 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5639 OP is a bit-mask that specifies where to stop, and in particular,
5640 which of those four position arguments makes a difference. See the
5641 description of enum move_operation_enum.
5643 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5644 screen line, this function will set IT to the next position >
5645 TO_CHARPOS. */
5647 void
5648 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5649 struct it *it;
5650 int to_charpos, to_x, to_y, to_vpos;
5651 int op;
5653 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5654 int line_height;
5655 int reached = 0;
5657 for (;;)
5659 if (op & MOVE_TO_VPOS)
5661 /* If no TO_CHARPOS and no TO_X specified, stop at the
5662 start of the line TO_VPOS. */
5663 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5665 if (it->vpos == to_vpos)
5667 reached = 1;
5668 break;
5670 else
5671 skip = move_it_in_display_line_to (it, -1, -1, 0);
5673 else
5675 /* TO_VPOS >= 0 means stop at TO_X in the line at
5676 TO_VPOS, or at TO_POS, whichever comes first. */
5677 if (it->vpos == to_vpos)
5679 reached = 2;
5680 break;
5683 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5685 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5687 reached = 3;
5688 break;
5690 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5692 /* We have reached TO_X but not in the line we want. */
5693 skip = move_it_in_display_line_to (it, to_charpos,
5694 -1, MOVE_TO_POS);
5695 if (skip == MOVE_POS_MATCH_OR_ZV)
5697 reached = 4;
5698 break;
5703 else if (op & MOVE_TO_Y)
5705 struct it it_backup;
5707 /* TO_Y specified means stop at TO_X in the line containing
5708 TO_Y---or at TO_CHARPOS if this is reached first. The
5709 problem is that we can't really tell whether the line
5710 contains TO_Y before we have completely scanned it, and
5711 this may skip past TO_X. What we do is to first scan to
5712 TO_X.
5714 If TO_X is not specified, use a TO_X of zero. The reason
5715 is to make the outcome of this function more predictable.
5716 If we didn't use TO_X == 0, we would stop at the end of
5717 the line which is probably not what a caller would expect
5718 to happen. */
5719 skip = move_it_in_display_line_to (it, to_charpos,
5720 ((op & MOVE_TO_X)
5721 ? to_x : 0),
5722 (MOVE_TO_X
5723 | (op & MOVE_TO_POS)));
5725 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5726 if (skip == MOVE_POS_MATCH_OR_ZV)
5728 reached = 5;
5729 break;
5732 /* If TO_X was reached, we would like to know whether TO_Y
5733 is in the line. This can only be said if we know the
5734 total line height which requires us to scan the rest of
5735 the line. */
5736 if (skip == MOVE_X_REACHED)
5738 it_backup = *it;
5739 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5740 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5741 op & MOVE_TO_POS);
5742 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5745 /* Now, decide whether TO_Y is in this line. */
5746 line_height = it->max_ascent + it->max_descent;
5747 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5749 if (to_y >= it->current_y
5750 && to_y < it->current_y + line_height)
5752 if (skip == MOVE_X_REACHED)
5753 /* If TO_Y is in this line and TO_X was reached above,
5754 we scanned too far. We have to restore IT's settings
5755 to the ones before skipping. */
5756 *it = it_backup;
5757 reached = 6;
5759 else if (skip == MOVE_X_REACHED)
5761 skip = skip2;
5762 if (skip == MOVE_POS_MATCH_OR_ZV)
5763 reached = 7;
5766 if (reached)
5767 break;
5769 else
5770 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5772 switch (skip)
5774 case MOVE_POS_MATCH_OR_ZV:
5775 reached = 8;
5776 goto out;
5778 case MOVE_NEWLINE_OR_CR:
5779 set_iterator_to_next (it, 1);
5780 it->continuation_lines_width = 0;
5781 break;
5783 case MOVE_LINE_TRUNCATED:
5784 it->continuation_lines_width = 0;
5785 reseat_at_next_visible_line_start (it, 0);
5786 if ((op & MOVE_TO_POS) != 0
5787 && IT_CHARPOS (*it) > to_charpos)
5789 reached = 9;
5790 goto out;
5792 break;
5794 case MOVE_LINE_CONTINUED:
5795 it->continuation_lines_width += it->current_x;
5796 break;
5798 default:
5799 abort ();
5802 /* Reset/increment for the next run. */
5803 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5804 it->current_x = it->hpos = 0;
5805 it->current_y += it->max_ascent + it->max_descent;
5806 ++it->vpos;
5807 last_height = it->max_ascent + it->max_descent;
5808 last_max_ascent = it->max_ascent;
5809 it->max_ascent = it->max_descent = 0;
5812 out:
5814 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5818 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5820 If DY > 0, move IT backward at least that many pixels. DY = 0
5821 means move IT backward to the preceding line start or BEGV. This
5822 function may move over more than DY pixels if IT->current_y - DY
5823 ends up in the middle of a line; in this case IT->current_y will be
5824 set to the top of the line moved to. */
5826 void
5827 move_it_vertically_backward (it, dy)
5828 struct it *it;
5829 int dy;
5831 int nlines, h;
5832 struct it it2, it3;
5833 int start_pos = IT_CHARPOS (*it);
5835 xassert (dy >= 0);
5837 /* Estimate how many newlines we must move back. */
5838 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
5840 /* Set the iterator's position that many lines back. */
5841 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5842 back_to_previous_visible_line_start (it);
5844 /* Reseat the iterator here. When moving backward, we don't want
5845 reseat to skip forward over invisible text, set up the iterator
5846 to deliver from overlay strings at the new position etc. So,
5847 use reseat_1 here. */
5848 reseat_1 (it, it->current.pos, 1);
5850 /* We are now surely at a line start. */
5851 it->current_x = it->hpos = 0;
5852 it->continuation_lines_width = 0;
5854 /* Move forward and see what y-distance we moved. First move to the
5855 start of the next line so that we get its height. We need this
5856 height to be able to tell whether we reached the specified
5857 y-distance. */
5858 it2 = *it;
5859 it2.max_ascent = it2.max_descent = 0;
5860 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5861 MOVE_TO_POS | MOVE_TO_VPOS);
5862 xassert (IT_CHARPOS (*it) >= BEGV);
5863 it3 = it2;
5865 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5866 xassert (IT_CHARPOS (*it) >= BEGV);
5867 /* H is the actual vertical distance from the position in *IT
5868 and the starting position. */
5869 h = it2.current_y - it->current_y;
5870 /* NLINES is the distance in number of lines. */
5871 nlines = it2.vpos - it->vpos;
5873 /* Correct IT's y and vpos position
5874 so that they are relative to the starting point. */
5875 it->vpos -= nlines;
5876 it->current_y -= h;
5878 if (dy == 0)
5880 /* DY == 0 means move to the start of the screen line. The
5881 value of nlines is > 0 if continuation lines were involved. */
5882 if (nlines > 0)
5883 move_it_by_lines (it, nlines, 1);
5884 xassert (IT_CHARPOS (*it) <= start_pos);
5886 else
5888 /* The y-position we try to reach, relative to *IT.
5889 Note that H has been subtracted in front of the if-statement. */
5890 int target_y = it->current_y + h - dy;
5891 int y0 = it3.current_y;
5892 int y1 = line_bottom_y (&it3);
5893 int line_height = y1 - y0;
5895 /* If we did not reach target_y, try to move further backward if
5896 we can. If we moved too far backward, try to move forward. */
5897 if (target_y < it->current_y
5898 /* This is heuristic. In a window that's 3 lines high, with
5899 a line height of 13 pixels each, recentering with point
5900 on the bottom line will try to move -39/2 = 19 pixels
5901 backward. Try to avoid moving into the first line. */
5902 && it->current_y - target_y > line_height / 3 * 2
5903 && IT_CHARPOS (*it) > BEGV)
5905 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5906 target_y - it->current_y));
5907 move_it_vertically (it, target_y - it->current_y);
5908 xassert (IT_CHARPOS (*it) >= BEGV);
5910 else if (target_y >= it->current_y + line_height
5911 && IT_CHARPOS (*it) < ZV)
5913 /* Should move forward by at least one line, maybe more.
5915 Note: Calling move_it_by_lines can be expensive on
5916 terminal frames, where compute_motion is used (via
5917 vmotion) to do the job, when there are very long lines
5918 and truncate-lines is nil. That's the reason for
5919 treating terminal frames specially here. */
5921 if (!FRAME_WINDOW_P (it->f))
5922 move_it_vertically (it, target_y - (it->current_y + line_height));
5923 else
5927 move_it_by_lines (it, 1, 1);
5929 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5932 xassert (IT_CHARPOS (*it) >= BEGV);
5938 /* Move IT by a specified amount of pixel lines DY. DY negative means
5939 move backwards. DY = 0 means move to start of screen line. At the
5940 end, IT will be on the start of a screen line. */
5942 void
5943 move_it_vertically (it, dy)
5944 struct it *it;
5945 int dy;
5947 if (dy <= 0)
5948 move_it_vertically_backward (it, -dy);
5949 else if (dy > 0)
5951 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5952 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5953 MOVE_TO_POS | MOVE_TO_Y);
5954 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5956 /* If buffer ends in ZV without a newline, move to the start of
5957 the line to satisfy the post-condition. */
5958 if (IT_CHARPOS (*it) == ZV
5959 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5960 move_it_by_lines (it, 0, 0);
5965 /* Move iterator IT past the end of the text line it is in. */
5967 void
5968 move_it_past_eol (it)
5969 struct it *it;
5971 enum move_it_result rc;
5973 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5974 if (rc == MOVE_NEWLINE_OR_CR)
5975 set_iterator_to_next (it, 0);
5979 #if 0 /* Currently not used. */
5981 /* Return non-zero if some text between buffer positions START_CHARPOS
5982 and END_CHARPOS is invisible. IT->window is the window for text
5983 property lookup. */
5985 static int
5986 invisible_text_between_p (it, start_charpos, end_charpos)
5987 struct it *it;
5988 int start_charpos, end_charpos;
5990 Lisp_Object prop, limit;
5991 int invisible_found_p;
5993 xassert (it != NULL && start_charpos <= end_charpos);
5995 /* Is text at START invisible? */
5996 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5997 it->window);
5998 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5999 invisible_found_p = 1;
6000 else
6002 limit = Fnext_single_char_property_change (make_number (start_charpos),
6003 Qinvisible, Qnil,
6004 make_number (end_charpos));
6005 invisible_found_p = XFASTINT (limit) < end_charpos;
6008 return invisible_found_p;
6011 #endif /* 0 */
6014 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6015 negative means move up. DVPOS == 0 means move to the start of the
6016 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6017 NEED_Y_P is zero, IT->current_y will be left unchanged.
6019 Further optimization ideas: If we would know that IT->f doesn't use
6020 a face with proportional font, we could be faster for
6021 truncate-lines nil. */
6023 void
6024 move_it_by_lines (it, dvpos, need_y_p)
6025 struct it *it;
6026 int dvpos, need_y_p;
6028 struct position pos;
6030 if (!FRAME_WINDOW_P (it->f))
6032 struct text_pos textpos;
6034 /* We can use vmotion on frames without proportional fonts. */
6035 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6036 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6037 reseat (it, textpos, 1);
6038 it->vpos += pos.vpos;
6039 it->current_y += pos.vpos;
6041 else if (dvpos == 0)
6043 /* DVPOS == 0 means move to the start of the screen line. */
6044 move_it_vertically_backward (it, 0);
6045 xassert (it->current_x == 0 && it->hpos == 0);
6047 else if (dvpos > 0)
6048 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6049 else
6051 struct it it2;
6052 int start_charpos, i;
6054 /* Start at the beginning of the screen line containing IT's
6055 position. */
6056 move_it_vertically_backward (it, 0);
6058 /* Go back -DVPOS visible lines and reseat the iterator there. */
6059 start_charpos = IT_CHARPOS (*it);
6060 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6061 back_to_previous_visible_line_start (it);
6062 reseat (it, it->current.pos, 1);
6063 it->current_x = it->hpos = 0;
6065 /* Above call may have moved too far if continuation lines
6066 are involved. Scan forward and see if it did. */
6067 it2 = *it;
6068 it2.vpos = it2.current_y = 0;
6069 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6070 it->vpos -= it2.vpos;
6071 it->current_y -= it2.current_y;
6072 it->current_x = it->hpos = 0;
6074 /* If we moved too far, move IT some lines forward. */
6075 if (it2.vpos > -dvpos)
6077 int delta = it2.vpos + dvpos;
6078 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6083 /* Return 1 if IT points into the middle of a display vector. */
6086 in_display_vector_p (it)
6087 struct it *it;
6089 return (it->method == next_element_from_display_vector
6090 && it->current.dpvec_index > 0
6091 && it->dpvec + it->current.dpvec_index != it->dpend);
6095 /***********************************************************************
6096 Messages
6097 ***********************************************************************/
6100 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6101 to *Messages*. */
6103 void
6104 add_to_log (format, arg1, arg2)
6105 char *format;
6106 Lisp_Object arg1, arg2;
6108 Lisp_Object args[3];
6109 Lisp_Object msg, fmt;
6110 char *buffer;
6111 int len;
6112 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6114 /* Do nothing if called asynchronously. Inserting text into
6115 a buffer may call after-change-functions and alike and
6116 that would means running Lisp asynchronously. */
6117 if (handling_signal)
6118 return;
6120 fmt = msg = Qnil;
6121 GCPRO4 (fmt, msg, arg1, arg2);
6123 args[0] = fmt = build_string (format);
6124 args[1] = arg1;
6125 args[2] = arg2;
6126 msg = Fformat (3, args);
6128 len = SBYTES (msg) + 1;
6129 buffer = (char *) alloca (len);
6130 bcopy (SDATA (msg), buffer, len);
6132 message_dolog (buffer, len - 1, 1, 0);
6133 UNGCPRO;
6137 /* Output a newline in the *Messages* buffer if "needs" one. */
6139 void
6140 message_log_maybe_newline ()
6142 if (message_log_need_newline)
6143 message_dolog ("", 0, 1, 0);
6147 /* Add a string M of length NBYTES to the message log, optionally
6148 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6149 nonzero, means interpret the contents of M as multibyte. This
6150 function calls low-level routines in order to bypass text property
6151 hooks, etc. which might not be safe to run. */
6153 void
6154 message_dolog (m, nbytes, nlflag, multibyte)
6155 const char *m;
6156 int nbytes, nlflag, multibyte;
6158 if (!NILP (Vmemory_full))
6159 return;
6161 if (!NILP (Vmessage_log_max))
6163 struct buffer *oldbuf;
6164 Lisp_Object oldpoint, oldbegv, oldzv;
6165 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6166 int point_at_end = 0;
6167 int zv_at_end = 0;
6168 Lisp_Object old_deactivate_mark, tem;
6169 struct gcpro gcpro1;
6171 old_deactivate_mark = Vdeactivate_mark;
6172 oldbuf = current_buffer;
6173 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6174 current_buffer->undo_list = Qt;
6176 oldpoint = message_dolog_marker1;
6177 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6178 oldbegv = message_dolog_marker2;
6179 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6180 oldzv = message_dolog_marker3;
6181 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6182 GCPRO1 (old_deactivate_mark);
6184 if (PT == Z)
6185 point_at_end = 1;
6186 if (ZV == Z)
6187 zv_at_end = 1;
6189 BEGV = BEG;
6190 BEGV_BYTE = BEG_BYTE;
6191 ZV = Z;
6192 ZV_BYTE = Z_BYTE;
6193 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6195 /* Insert the string--maybe converting multibyte to single byte
6196 or vice versa, so that all the text fits the buffer. */
6197 if (multibyte
6198 && NILP (current_buffer->enable_multibyte_characters))
6200 int i, c, char_bytes;
6201 unsigned char work[1];
6203 /* Convert a multibyte string to single-byte
6204 for the *Message* buffer. */
6205 for (i = 0; i < nbytes; i += char_bytes)
6207 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6208 work[0] = (SINGLE_BYTE_CHAR_P (c)
6210 : multibyte_char_to_unibyte (c, Qnil));
6211 insert_1_both (work, 1, 1, 1, 0, 0);
6214 else if (! multibyte
6215 && ! NILP (current_buffer->enable_multibyte_characters))
6217 int i, c, char_bytes;
6218 unsigned char *msg = (unsigned char *) m;
6219 unsigned char str[MAX_MULTIBYTE_LENGTH];
6220 /* Convert a single-byte string to multibyte
6221 for the *Message* buffer. */
6222 for (i = 0; i < nbytes; i++)
6224 c = unibyte_char_to_multibyte (msg[i]);
6225 char_bytes = CHAR_STRING (c, str);
6226 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6229 else if (nbytes)
6230 insert_1 (m, nbytes, 1, 0, 0);
6232 if (nlflag)
6234 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6235 insert_1 ("\n", 1, 1, 0, 0);
6237 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6238 this_bol = PT;
6239 this_bol_byte = PT_BYTE;
6241 /* See if this line duplicates the previous one.
6242 If so, combine duplicates. */
6243 if (this_bol > BEG)
6245 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6246 prev_bol = PT;
6247 prev_bol_byte = PT_BYTE;
6249 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6250 this_bol, this_bol_byte);
6251 if (dup)
6253 del_range_both (prev_bol, prev_bol_byte,
6254 this_bol, this_bol_byte, 0);
6255 if (dup > 1)
6257 char dupstr[40];
6258 int duplen;
6260 /* If you change this format, don't forget to also
6261 change message_log_check_duplicate. */
6262 sprintf (dupstr, " [%d times]", dup);
6263 duplen = strlen (dupstr);
6264 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6265 insert_1 (dupstr, duplen, 1, 0, 1);
6270 /* If we have more than the desired maximum number of lines
6271 in the *Messages* buffer now, delete the oldest ones.
6272 This is safe because we don't have undo in this buffer. */
6274 if (NATNUMP (Vmessage_log_max))
6276 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6277 -XFASTINT (Vmessage_log_max) - 1, 0);
6278 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6281 BEGV = XMARKER (oldbegv)->charpos;
6282 BEGV_BYTE = marker_byte_position (oldbegv);
6284 if (zv_at_end)
6286 ZV = Z;
6287 ZV_BYTE = Z_BYTE;
6289 else
6291 ZV = XMARKER (oldzv)->charpos;
6292 ZV_BYTE = marker_byte_position (oldzv);
6295 if (point_at_end)
6296 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6297 else
6298 /* We can't do Fgoto_char (oldpoint) because it will run some
6299 Lisp code. */
6300 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6301 XMARKER (oldpoint)->bytepos);
6303 UNGCPRO;
6304 unchain_marker (XMARKER (oldpoint));
6305 unchain_marker (XMARKER (oldbegv));
6306 unchain_marker (XMARKER (oldzv));
6308 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6309 set_buffer_internal (oldbuf);
6310 if (NILP (tem))
6311 windows_or_buffers_changed = old_windows_or_buffers_changed;
6312 message_log_need_newline = !nlflag;
6313 Vdeactivate_mark = old_deactivate_mark;
6318 /* We are at the end of the buffer after just having inserted a newline.
6319 (Note: We depend on the fact we won't be crossing the gap.)
6320 Check to see if the most recent message looks a lot like the previous one.
6321 Return 0 if different, 1 if the new one should just replace it, or a
6322 value N > 1 if we should also append " [N times]". */
6324 static int
6325 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6326 int prev_bol, this_bol;
6327 int prev_bol_byte, this_bol_byte;
6329 int i;
6330 int len = Z_BYTE - 1 - this_bol_byte;
6331 int seen_dots = 0;
6332 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6333 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6335 for (i = 0; i < len; i++)
6337 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6338 seen_dots = 1;
6339 if (p1[i] != p2[i])
6340 return seen_dots;
6342 p1 += len;
6343 if (*p1 == '\n')
6344 return 2;
6345 if (*p1++ == ' ' && *p1++ == '[')
6347 int n = 0;
6348 while (*p1 >= '0' && *p1 <= '9')
6349 n = n * 10 + *p1++ - '0';
6350 if (strncmp (p1, " times]\n", 8) == 0)
6351 return n+1;
6353 return 0;
6357 /* Display an echo area message M with a specified length of NBYTES
6358 bytes. The string may include null characters. If M is 0, clear
6359 out any existing message, and let the mini-buffer text show
6360 through.
6362 The buffer M must continue to exist until after the echo area gets
6363 cleared or some other message gets displayed there. This means do
6364 not pass text that is stored in a Lisp string; do not pass text in
6365 a buffer that was alloca'd. */
6367 void
6368 message2 (m, nbytes, multibyte)
6369 const char *m;
6370 int nbytes;
6371 int multibyte;
6373 /* First flush out any partial line written with print. */
6374 message_log_maybe_newline ();
6375 if (m)
6376 message_dolog (m, nbytes, 1, multibyte);
6377 message2_nolog (m, nbytes, multibyte);
6381 /* The non-logging counterpart of message2. */
6383 void
6384 message2_nolog (m, nbytes, multibyte)
6385 const char *m;
6386 int nbytes, multibyte;
6388 struct frame *sf = SELECTED_FRAME ();
6389 message_enable_multibyte = multibyte;
6391 if (noninteractive)
6393 if (noninteractive_need_newline)
6394 putc ('\n', stderr);
6395 noninteractive_need_newline = 0;
6396 if (m)
6397 fwrite (m, nbytes, 1, stderr);
6398 if (cursor_in_echo_area == 0)
6399 fprintf (stderr, "\n");
6400 fflush (stderr);
6402 /* A null message buffer means that the frame hasn't really been
6403 initialized yet. Error messages get reported properly by
6404 cmd_error, so this must be just an informative message; toss it. */
6405 else if (INTERACTIVE
6406 && sf->glyphs_initialized_p
6407 && FRAME_MESSAGE_BUF (sf))
6409 Lisp_Object mini_window;
6410 struct frame *f;
6412 /* Get the frame containing the mini-buffer
6413 that the selected frame is using. */
6414 mini_window = FRAME_MINIBUF_WINDOW (sf);
6415 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6417 FRAME_SAMPLE_VISIBILITY (f);
6418 if (FRAME_VISIBLE_P (sf)
6419 && ! FRAME_VISIBLE_P (f))
6420 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6422 if (m)
6424 set_message (m, Qnil, nbytes, multibyte);
6425 if (minibuffer_auto_raise)
6426 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6428 else
6429 clear_message (1, 1);
6431 do_pending_window_change (0);
6432 echo_area_display (1);
6433 do_pending_window_change (0);
6434 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6435 (*frame_up_to_date_hook) (f);
6440 /* Display an echo area message M with a specified length of NBYTES
6441 bytes. The string may include null characters. If M is not a
6442 string, clear out any existing message, and let the mini-buffer
6443 text show through. */
6445 void
6446 message3 (m, nbytes, multibyte)
6447 Lisp_Object m;
6448 int nbytes;
6449 int multibyte;
6451 struct gcpro gcpro1;
6453 GCPRO1 (m);
6455 /* First flush out any partial line written with print. */
6456 message_log_maybe_newline ();
6457 if (STRINGP (m))
6458 message_dolog (SDATA (m), nbytes, 1, multibyte);
6459 message3_nolog (m, nbytes, multibyte);
6461 UNGCPRO;
6465 /* The non-logging version of message3. */
6467 void
6468 message3_nolog (m, nbytes, multibyte)
6469 Lisp_Object m;
6470 int nbytes, multibyte;
6472 struct frame *sf = SELECTED_FRAME ();
6473 message_enable_multibyte = multibyte;
6475 if (noninteractive)
6477 if (noninteractive_need_newline)
6478 putc ('\n', stderr);
6479 noninteractive_need_newline = 0;
6480 if (STRINGP (m))
6481 fwrite (SDATA (m), nbytes, 1, stderr);
6482 if (cursor_in_echo_area == 0)
6483 fprintf (stderr, "\n");
6484 fflush (stderr);
6486 /* A null message buffer means that the frame hasn't really been
6487 initialized yet. Error messages get reported properly by
6488 cmd_error, so this must be just an informative message; toss it. */
6489 else if (INTERACTIVE
6490 && sf->glyphs_initialized_p
6491 && FRAME_MESSAGE_BUF (sf))
6493 Lisp_Object mini_window;
6494 Lisp_Object frame;
6495 struct frame *f;
6497 /* Get the frame containing the mini-buffer
6498 that the selected frame is using. */
6499 mini_window = FRAME_MINIBUF_WINDOW (sf);
6500 frame = XWINDOW (mini_window)->frame;
6501 f = XFRAME (frame);
6503 FRAME_SAMPLE_VISIBILITY (f);
6504 if (FRAME_VISIBLE_P (sf)
6505 && !FRAME_VISIBLE_P (f))
6506 Fmake_frame_visible (frame);
6508 if (STRINGP (m) && SCHARS (m) > 0)
6510 set_message (NULL, m, nbytes, multibyte);
6511 if (minibuffer_auto_raise)
6512 Fraise_frame (frame);
6514 else
6515 clear_message (1, 1);
6517 do_pending_window_change (0);
6518 echo_area_display (1);
6519 do_pending_window_change (0);
6520 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6521 (*frame_up_to_date_hook) (f);
6526 /* Display a null-terminated echo area message M. If M is 0, clear
6527 out any existing message, and let the mini-buffer text show through.
6529 The buffer M must continue to exist until after the echo area gets
6530 cleared or some other message gets displayed there. Do not pass
6531 text that is stored in a Lisp string. Do not pass text in a buffer
6532 that was alloca'd. */
6534 void
6535 message1 (m)
6536 char *m;
6538 message2 (m, (m ? strlen (m) : 0), 0);
6542 /* The non-logging counterpart of message1. */
6544 void
6545 message1_nolog (m)
6546 char *m;
6548 message2_nolog (m, (m ? strlen (m) : 0), 0);
6551 /* Display a message M which contains a single %s
6552 which gets replaced with STRING. */
6554 void
6555 message_with_string (m, string, log)
6556 char *m;
6557 Lisp_Object string;
6558 int log;
6560 CHECK_STRING (string);
6562 if (noninteractive)
6564 if (m)
6566 if (noninteractive_need_newline)
6567 putc ('\n', stderr);
6568 noninteractive_need_newline = 0;
6569 fprintf (stderr, m, SDATA (string));
6570 if (cursor_in_echo_area == 0)
6571 fprintf (stderr, "\n");
6572 fflush (stderr);
6575 else if (INTERACTIVE)
6577 /* The frame whose minibuffer we're going to display the message on.
6578 It may be larger than the selected frame, so we need
6579 to use its buffer, not the selected frame's buffer. */
6580 Lisp_Object mini_window;
6581 struct frame *f, *sf = SELECTED_FRAME ();
6583 /* Get the frame containing the minibuffer
6584 that the selected frame is using. */
6585 mini_window = FRAME_MINIBUF_WINDOW (sf);
6586 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6588 /* A null message buffer means that the frame hasn't really been
6589 initialized yet. Error messages get reported properly by
6590 cmd_error, so this must be just an informative message; toss it. */
6591 if (FRAME_MESSAGE_BUF (f))
6593 Lisp_Object args[2], message;
6594 struct gcpro gcpro1, gcpro2;
6596 args[0] = build_string (m);
6597 args[1] = message = string;
6598 GCPRO2 (args[0], message);
6599 gcpro1.nvars = 2;
6601 message = Fformat (2, args);
6603 if (log)
6604 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6605 else
6606 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6608 UNGCPRO;
6610 /* Print should start at the beginning of the message
6611 buffer next time. */
6612 message_buf_print = 0;
6618 /* Dump an informative message to the minibuf. If M is 0, clear out
6619 any existing message, and let the mini-buffer text show through. */
6621 /* VARARGS 1 */
6622 void
6623 message (m, a1, a2, a3)
6624 char *m;
6625 EMACS_INT a1, a2, a3;
6627 if (noninteractive)
6629 if (m)
6631 if (noninteractive_need_newline)
6632 putc ('\n', stderr);
6633 noninteractive_need_newline = 0;
6634 fprintf (stderr, m, a1, a2, a3);
6635 if (cursor_in_echo_area == 0)
6636 fprintf (stderr, "\n");
6637 fflush (stderr);
6640 else if (INTERACTIVE)
6642 /* The frame whose mini-buffer we're going to display the message
6643 on. It may be larger than the selected frame, so we need to
6644 use its buffer, not the selected frame's buffer. */
6645 Lisp_Object mini_window;
6646 struct frame *f, *sf = SELECTED_FRAME ();
6648 /* Get the frame containing the mini-buffer
6649 that the selected frame is using. */
6650 mini_window = FRAME_MINIBUF_WINDOW (sf);
6651 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6653 /* A null message buffer means that the frame hasn't really been
6654 initialized yet. Error messages get reported properly by
6655 cmd_error, so this must be just an informative message; toss
6656 it. */
6657 if (FRAME_MESSAGE_BUF (f))
6659 if (m)
6661 int len;
6662 #ifdef NO_ARG_ARRAY
6663 char *a[3];
6664 a[0] = (char *) a1;
6665 a[1] = (char *) a2;
6666 a[2] = (char *) a3;
6668 len = doprnt (FRAME_MESSAGE_BUF (f),
6669 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6670 #else
6671 len = doprnt (FRAME_MESSAGE_BUF (f),
6672 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6673 (char **) &a1);
6674 #endif /* NO_ARG_ARRAY */
6676 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6678 else
6679 message1 (0);
6681 /* Print should start at the beginning of the message
6682 buffer next time. */
6683 message_buf_print = 0;
6689 /* The non-logging version of message. */
6691 void
6692 message_nolog (m, a1, a2, a3)
6693 char *m;
6694 EMACS_INT a1, a2, a3;
6696 Lisp_Object old_log_max;
6697 old_log_max = Vmessage_log_max;
6698 Vmessage_log_max = Qnil;
6699 message (m, a1, a2, a3);
6700 Vmessage_log_max = old_log_max;
6704 /* Display the current message in the current mini-buffer. This is
6705 only called from error handlers in process.c, and is not time
6706 critical. */
6708 void
6709 update_echo_area ()
6711 if (!NILP (echo_area_buffer[0]))
6713 Lisp_Object string;
6714 string = Fcurrent_message ();
6715 message3 (string, SBYTES (string),
6716 !NILP (current_buffer->enable_multibyte_characters));
6721 /* Make sure echo area buffers in `echo_buffers' are live.
6722 If they aren't, make new ones. */
6724 static void
6725 ensure_echo_area_buffers ()
6727 int i;
6729 for (i = 0; i < 2; ++i)
6730 if (!BUFFERP (echo_buffer[i])
6731 || NILP (XBUFFER (echo_buffer[i])->name))
6733 char name[30];
6734 Lisp_Object old_buffer;
6735 int j;
6737 old_buffer = echo_buffer[i];
6738 sprintf (name, " *Echo Area %d*", i);
6739 echo_buffer[i] = Fget_buffer_create (build_string (name));
6740 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6742 for (j = 0; j < 2; ++j)
6743 if (EQ (old_buffer, echo_area_buffer[j]))
6744 echo_area_buffer[j] = echo_buffer[i];
6749 /* Call FN with args A1..A4 with either the current or last displayed
6750 echo_area_buffer as current buffer.
6752 WHICH zero means use the current message buffer
6753 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6754 from echo_buffer[] and clear it.
6756 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6757 suitable buffer from echo_buffer[] and clear it.
6759 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6760 that the current message becomes the last displayed one, make
6761 choose a suitable buffer for echo_area_buffer[0], and clear it.
6763 Value is what FN returns. */
6765 static int
6766 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6767 struct window *w;
6768 int which;
6769 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6770 EMACS_INT a1;
6771 Lisp_Object a2;
6772 EMACS_INT a3, a4;
6774 Lisp_Object buffer;
6775 int this_one, the_other, clear_buffer_p, rc;
6776 int count = SPECPDL_INDEX ();
6778 /* If buffers aren't live, make new ones. */
6779 ensure_echo_area_buffers ();
6781 clear_buffer_p = 0;
6783 if (which == 0)
6784 this_one = 0, the_other = 1;
6785 else if (which > 0)
6786 this_one = 1, the_other = 0;
6787 else
6789 this_one = 0, the_other = 1;
6790 clear_buffer_p = 1;
6792 /* We need a fresh one in case the current echo buffer equals
6793 the one containing the last displayed echo area message. */
6794 if (!NILP (echo_area_buffer[this_one])
6795 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6796 echo_area_buffer[this_one] = Qnil;
6799 /* Choose a suitable buffer from echo_buffer[] is we don't
6800 have one. */
6801 if (NILP (echo_area_buffer[this_one]))
6803 echo_area_buffer[this_one]
6804 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6805 ? echo_buffer[the_other]
6806 : echo_buffer[this_one]);
6807 clear_buffer_p = 1;
6810 buffer = echo_area_buffer[this_one];
6812 /* Don't get confused by reusing the buffer used for echoing
6813 for a different purpose. */
6814 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6815 cancel_echoing ();
6817 record_unwind_protect (unwind_with_echo_area_buffer,
6818 with_echo_area_buffer_unwind_data (w));
6820 /* Make the echo area buffer current. Note that for display
6821 purposes, it is not necessary that the displayed window's buffer
6822 == current_buffer, except for text property lookup. So, let's
6823 only set that buffer temporarily here without doing a full
6824 Fset_window_buffer. We must also change w->pointm, though,
6825 because otherwise an assertions in unshow_buffer fails, and Emacs
6826 aborts. */
6827 set_buffer_internal_1 (XBUFFER (buffer));
6828 if (w)
6830 w->buffer = buffer;
6831 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6834 current_buffer->undo_list = Qt;
6835 current_buffer->read_only = Qnil;
6836 specbind (Qinhibit_read_only, Qt);
6837 specbind (Qinhibit_modification_hooks, Qt);
6839 if (clear_buffer_p && Z > BEG)
6840 del_range (BEG, Z);
6842 xassert (BEGV >= BEG);
6843 xassert (ZV <= Z && ZV >= BEGV);
6845 rc = fn (a1, a2, a3, a4);
6847 xassert (BEGV >= BEG);
6848 xassert (ZV <= Z && ZV >= BEGV);
6850 unbind_to (count, Qnil);
6851 return rc;
6855 /* Save state that should be preserved around the call to the function
6856 FN called in with_echo_area_buffer. */
6858 static Lisp_Object
6859 with_echo_area_buffer_unwind_data (w)
6860 struct window *w;
6862 int i = 0;
6863 Lisp_Object vector;
6865 /* Reduce consing by keeping one vector in
6866 Vwith_echo_area_save_vector. */
6867 vector = Vwith_echo_area_save_vector;
6868 Vwith_echo_area_save_vector = Qnil;
6870 if (NILP (vector))
6871 vector = Fmake_vector (make_number (7), Qnil);
6873 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6874 AREF (vector, i) = Vdeactivate_mark, ++i;
6875 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6877 if (w)
6879 XSETWINDOW (AREF (vector, i), w); ++i;
6880 AREF (vector, i) = w->buffer; ++i;
6881 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6882 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6884 else
6886 int end = i + 4;
6887 for (; i < end; ++i)
6888 AREF (vector, i) = Qnil;
6891 xassert (i == ASIZE (vector));
6892 return vector;
6896 /* Restore global state from VECTOR which was created by
6897 with_echo_area_buffer_unwind_data. */
6899 static Lisp_Object
6900 unwind_with_echo_area_buffer (vector)
6901 Lisp_Object vector;
6903 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6904 Vdeactivate_mark = AREF (vector, 1);
6905 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6907 if (WINDOWP (AREF (vector, 3)))
6909 struct window *w;
6910 Lisp_Object buffer, charpos, bytepos;
6912 w = XWINDOW (AREF (vector, 3));
6913 buffer = AREF (vector, 4);
6914 charpos = AREF (vector, 5);
6915 bytepos = AREF (vector, 6);
6917 w->buffer = buffer;
6918 set_marker_both (w->pointm, buffer,
6919 XFASTINT (charpos), XFASTINT (bytepos));
6922 Vwith_echo_area_save_vector = vector;
6923 return Qnil;
6927 /* Set up the echo area for use by print functions. MULTIBYTE_P
6928 non-zero means we will print multibyte. */
6930 void
6931 setup_echo_area_for_printing (multibyte_p)
6932 int multibyte_p;
6934 /* If we can't find an echo area any more, exit. */
6935 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6936 Fkill_emacs (Qnil);
6938 ensure_echo_area_buffers ();
6940 if (!message_buf_print)
6942 /* A message has been output since the last time we printed.
6943 Choose a fresh echo area buffer. */
6944 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6945 echo_area_buffer[0] = echo_buffer[1];
6946 else
6947 echo_area_buffer[0] = echo_buffer[0];
6949 /* Switch to that buffer and clear it. */
6950 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6951 current_buffer->truncate_lines = Qnil;
6953 if (Z > BEG)
6955 int count = SPECPDL_INDEX ();
6956 specbind (Qinhibit_read_only, Qt);
6957 /* Note that undo recording is always disabled. */
6958 del_range (BEG, Z);
6959 unbind_to (count, Qnil);
6961 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6963 /* Set up the buffer for the multibyteness we need. */
6964 if (multibyte_p
6965 != !NILP (current_buffer->enable_multibyte_characters))
6966 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6968 /* Raise the frame containing the echo area. */
6969 if (minibuffer_auto_raise)
6971 struct frame *sf = SELECTED_FRAME ();
6972 Lisp_Object mini_window;
6973 mini_window = FRAME_MINIBUF_WINDOW (sf);
6974 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6977 message_log_maybe_newline ();
6978 message_buf_print = 1;
6980 else
6982 if (NILP (echo_area_buffer[0]))
6984 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6985 echo_area_buffer[0] = echo_buffer[1];
6986 else
6987 echo_area_buffer[0] = echo_buffer[0];
6990 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6992 /* Someone switched buffers between print requests. */
6993 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6994 current_buffer->truncate_lines = Qnil;
7000 /* Display an echo area message in window W. Value is non-zero if W's
7001 height is changed. If display_last_displayed_message_p is
7002 non-zero, display the message that was last displayed, otherwise
7003 display the current message. */
7005 static int
7006 display_echo_area (w)
7007 struct window *w;
7009 int i, no_message_p, window_height_changed_p, count;
7011 /* Temporarily disable garbage collections while displaying the echo
7012 area. This is done because a GC can print a message itself.
7013 That message would modify the echo area buffer's contents while a
7014 redisplay of the buffer is going on, and seriously confuse
7015 redisplay. */
7016 count = inhibit_garbage_collection ();
7018 /* If there is no message, we must call display_echo_area_1
7019 nevertheless because it resizes the window. But we will have to
7020 reset the echo_area_buffer in question to nil at the end because
7021 with_echo_area_buffer will sets it to an empty buffer. */
7022 i = display_last_displayed_message_p ? 1 : 0;
7023 no_message_p = NILP (echo_area_buffer[i]);
7025 window_height_changed_p
7026 = with_echo_area_buffer (w, display_last_displayed_message_p,
7027 display_echo_area_1,
7028 (EMACS_INT) w, Qnil, 0, 0);
7030 if (no_message_p)
7031 echo_area_buffer[i] = Qnil;
7033 unbind_to (count, Qnil);
7034 return window_height_changed_p;
7038 /* Helper for display_echo_area. Display the current buffer which
7039 contains the current echo area message in window W, a mini-window,
7040 a pointer to which is passed in A1. A2..A4 are currently not used.
7041 Change the height of W so that all of the message is displayed.
7042 Value is non-zero if height of W was changed. */
7044 static int
7045 display_echo_area_1 (a1, a2, a3, a4)
7046 EMACS_INT a1;
7047 Lisp_Object a2;
7048 EMACS_INT a3, a4;
7050 struct window *w = (struct window *) a1;
7051 Lisp_Object window;
7052 struct text_pos start;
7053 int window_height_changed_p = 0;
7055 /* Do this before displaying, so that we have a large enough glyph
7056 matrix for the display. */
7057 window_height_changed_p = resize_mini_window (w, 0);
7059 /* Display. */
7060 clear_glyph_matrix (w->desired_matrix);
7061 XSETWINDOW (window, w);
7062 SET_TEXT_POS (start, BEG, BEG_BYTE);
7063 try_window (window, start);
7065 return window_height_changed_p;
7069 /* Resize the echo area window to exactly the size needed for the
7070 currently displayed message, if there is one. If a mini-buffer
7071 is active, don't shrink it. */
7073 void
7074 resize_echo_area_exactly ()
7076 if (BUFFERP (echo_area_buffer[0])
7077 && WINDOWP (echo_area_window))
7079 struct window *w = XWINDOW (echo_area_window);
7080 int resized_p;
7081 Lisp_Object resize_exactly;
7083 if (minibuf_level == 0)
7084 resize_exactly = Qt;
7085 else
7086 resize_exactly = Qnil;
7088 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7089 (EMACS_INT) w, resize_exactly, 0, 0);
7090 if (resized_p)
7092 ++windows_or_buffers_changed;
7093 ++update_mode_lines;
7094 redisplay_internal (0);
7100 /* Callback function for with_echo_area_buffer, when used from
7101 resize_echo_area_exactly. A1 contains a pointer to the window to
7102 resize, EXACTLY non-nil means resize the mini-window exactly to the
7103 size of the text displayed. A3 and A4 are not used. Value is what
7104 resize_mini_window returns. */
7106 static int
7107 resize_mini_window_1 (a1, exactly, a3, a4)
7108 EMACS_INT a1;
7109 Lisp_Object exactly;
7110 EMACS_INT a3, a4;
7112 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7116 /* Resize mini-window W to fit the size of its contents. EXACT:P
7117 means size the window exactly to the size needed. Otherwise, it's
7118 only enlarged until W's buffer is empty. Value is non-zero if
7119 the window height has been changed. */
7122 resize_mini_window (w, exact_p)
7123 struct window *w;
7124 int exact_p;
7126 struct frame *f = XFRAME (w->frame);
7127 int window_height_changed_p = 0;
7129 xassert (MINI_WINDOW_P (w));
7131 /* Don't resize windows while redisplaying a window; it would
7132 confuse redisplay functions when the size of the window they are
7133 displaying changes from under them. Such a resizing can happen,
7134 for instance, when which-func prints a long message while
7135 we are running fontification-functions. We're running these
7136 functions with safe_call which binds inhibit-redisplay to t. */
7137 if (!NILP (Vinhibit_redisplay))
7138 return 0;
7140 /* Nil means don't try to resize. */
7141 if (NILP (Vresize_mini_windows)
7142 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7143 return 0;
7145 if (!FRAME_MINIBUF_ONLY_P (f))
7147 struct it it;
7148 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7149 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7150 int height, max_height;
7151 int unit = FRAME_LINE_HEIGHT (f);
7152 struct text_pos start;
7153 struct buffer *old_current_buffer = NULL;
7155 if (current_buffer != XBUFFER (w->buffer))
7157 old_current_buffer = current_buffer;
7158 set_buffer_internal (XBUFFER (w->buffer));
7161 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7163 /* Compute the max. number of lines specified by the user. */
7164 if (FLOATP (Vmax_mini_window_height))
7165 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7166 else if (INTEGERP (Vmax_mini_window_height))
7167 max_height = XINT (Vmax_mini_window_height);
7168 else
7169 max_height = total_height / 4;
7171 /* Correct that max. height if it's bogus. */
7172 max_height = max (1, max_height);
7173 max_height = min (total_height, max_height);
7175 /* Find out the height of the text in the window. */
7176 if (it.truncate_lines_p)
7177 height = 1;
7178 else
7180 last_height = 0;
7181 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7182 if (it.max_ascent == 0 && it.max_descent == 0)
7183 height = it.current_y + last_height;
7184 else
7185 height = it.current_y + it.max_ascent + it.max_descent;
7186 height -= it.extra_line_spacing;
7187 height = (height + unit - 1) / unit;
7190 /* Compute a suitable window start. */
7191 if (height > max_height)
7193 height = max_height;
7194 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7195 move_it_vertically_backward (&it, (height - 1) * unit);
7196 start = it.current.pos;
7198 else
7199 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7200 SET_MARKER_FROM_TEXT_POS (w->start, start);
7202 if (EQ (Vresize_mini_windows, Qgrow_only))
7204 /* Let it grow only, until we display an empty message, in which
7205 case the window shrinks again. */
7206 if (height > WINDOW_TOTAL_LINES (w))
7208 int old_height = WINDOW_TOTAL_LINES (w);
7209 freeze_window_starts (f, 1);
7210 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7211 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7213 else if (height < WINDOW_TOTAL_LINES (w)
7214 && (exact_p || BEGV == ZV))
7216 int old_height = WINDOW_TOTAL_LINES (w);
7217 freeze_window_starts (f, 0);
7218 shrink_mini_window (w);
7219 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7222 else
7224 /* Always resize to exact size needed. */
7225 if (height > WINDOW_TOTAL_LINES (w))
7227 int old_height = WINDOW_TOTAL_LINES (w);
7228 freeze_window_starts (f, 1);
7229 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7230 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7232 else if (height < WINDOW_TOTAL_LINES (w))
7234 int old_height = WINDOW_TOTAL_LINES (w);
7235 freeze_window_starts (f, 0);
7236 shrink_mini_window (w);
7238 if (height)
7240 freeze_window_starts (f, 1);
7241 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7244 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7248 if (old_current_buffer)
7249 set_buffer_internal (old_current_buffer);
7252 return window_height_changed_p;
7256 /* Value is the current message, a string, or nil if there is no
7257 current message. */
7259 Lisp_Object
7260 current_message ()
7262 Lisp_Object msg;
7264 if (NILP (echo_area_buffer[0]))
7265 msg = Qnil;
7266 else
7268 with_echo_area_buffer (0, 0, current_message_1,
7269 (EMACS_INT) &msg, Qnil, 0, 0);
7270 if (NILP (msg))
7271 echo_area_buffer[0] = Qnil;
7274 return msg;
7278 static int
7279 current_message_1 (a1, a2, a3, a4)
7280 EMACS_INT a1;
7281 Lisp_Object a2;
7282 EMACS_INT a3, a4;
7284 Lisp_Object *msg = (Lisp_Object *) a1;
7286 if (Z > BEG)
7287 *msg = make_buffer_string (BEG, Z, 1);
7288 else
7289 *msg = Qnil;
7290 return 0;
7294 /* Push the current message on Vmessage_stack for later restauration
7295 by restore_message. Value is non-zero if the current message isn't
7296 empty. This is a relatively infrequent operation, so it's not
7297 worth optimizing. */
7300 push_message ()
7302 Lisp_Object msg;
7303 msg = current_message ();
7304 Vmessage_stack = Fcons (msg, Vmessage_stack);
7305 return STRINGP (msg);
7309 /* Restore message display from the top of Vmessage_stack. */
7311 void
7312 restore_message ()
7314 Lisp_Object msg;
7316 xassert (CONSP (Vmessage_stack));
7317 msg = XCAR (Vmessage_stack);
7318 if (STRINGP (msg))
7319 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7320 else
7321 message3_nolog (msg, 0, 0);
7325 /* Handler for record_unwind_protect calling pop_message. */
7327 Lisp_Object
7328 pop_message_unwind (dummy)
7329 Lisp_Object dummy;
7331 pop_message ();
7332 return Qnil;
7335 /* Pop the top-most entry off Vmessage_stack. */
7337 void
7338 pop_message ()
7340 xassert (CONSP (Vmessage_stack));
7341 Vmessage_stack = XCDR (Vmessage_stack);
7345 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7346 exits. If the stack is not empty, we have a missing pop_message
7347 somewhere. */
7349 void
7350 check_message_stack ()
7352 if (!NILP (Vmessage_stack))
7353 abort ();
7357 /* Truncate to NCHARS what will be displayed in the echo area the next
7358 time we display it---but don't redisplay it now. */
7360 void
7361 truncate_echo_area (nchars)
7362 int nchars;
7364 if (nchars == 0)
7365 echo_area_buffer[0] = Qnil;
7366 /* A null message buffer means that the frame hasn't really been
7367 initialized yet. Error messages get reported properly by
7368 cmd_error, so this must be just an informative message; toss it. */
7369 else if (!noninteractive
7370 && INTERACTIVE
7371 && !NILP (echo_area_buffer[0]))
7373 struct frame *sf = SELECTED_FRAME ();
7374 if (FRAME_MESSAGE_BUF (sf))
7375 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7380 /* Helper function for truncate_echo_area. Truncate the current
7381 message to at most NCHARS characters. */
7383 static int
7384 truncate_message_1 (nchars, a2, a3, a4)
7385 EMACS_INT nchars;
7386 Lisp_Object a2;
7387 EMACS_INT a3, a4;
7389 if (BEG + nchars < Z)
7390 del_range (BEG + nchars, Z);
7391 if (Z == BEG)
7392 echo_area_buffer[0] = Qnil;
7393 return 0;
7397 /* Set the current message to a substring of S or STRING.
7399 If STRING is a Lisp string, set the message to the first NBYTES
7400 bytes from STRING. NBYTES zero means use the whole string. If
7401 STRING is multibyte, the message will be displayed multibyte.
7403 If S is not null, set the message to the first LEN bytes of S. LEN
7404 zero means use the whole string. MULTIBYTE_P non-zero means S is
7405 multibyte. Display the message multibyte in that case. */
7407 void
7408 set_message (s, string, nbytes, multibyte_p)
7409 const char *s;
7410 Lisp_Object string;
7411 int nbytes, multibyte_p;
7413 message_enable_multibyte
7414 = ((s && multibyte_p)
7415 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7417 with_echo_area_buffer (0, -1, set_message_1,
7418 (EMACS_INT) s, string, nbytes, multibyte_p);
7419 message_buf_print = 0;
7420 help_echo_showing_p = 0;
7424 /* Helper function for set_message. Arguments have the same meaning
7425 as there, with A1 corresponding to S and A2 corresponding to STRING
7426 This function is called with the echo area buffer being
7427 current. */
7429 static int
7430 set_message_1 (a1, a2, nbytes, multibyte_p)
7431 EMACS_INT a1;
7432 Lisp_Object a2;
7433 EMACS_INT nbytes, multibyte_p;
7435 const char *s = (const char *) a1;
7436 Lisp_Object string = a2;
7438 xassert (BEG == Z);
7440 /* Change multibyteness of the echo buffer appropriately. */
7441 if (message_enable_multibyte
7442 != !NILP (current_buffer->enable_multibyte_characters))
7443 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7445 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7447 /* Insert new message at BEG. */
7448 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7450 if (STRINGP (string))
7452 int nchars;
7454 if (nbytes == 0)
7455 nbytes = SBYTES (string);
7456 nchars = string_byte_to_char (string, nbytes);
7458 /* This function takes care of single/multibyte conversion. We
7459 just have to ensure that the echo area buffer has the right
7460 setting of enable_multibyte_characters. */
7461 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7463 else if (s)
7465 if (nbytes == 0)
7466 nbytes = strlen (s);
7468 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7470 /* Convert from multi-byte to single-byte. */
7471 int i, c, n;
7472 unsigned char work[1];
7474 /* Convert a multibyte string to single-byte. */
7475 for (i = 0; i < nbytes; i += n)
7477 c = string_char_and_length (s + i, nbytes - i, &n);
7478 work[0] = (SINGLE_BYTE_CHAR_P (c)
7480 : multibyte_char_to_unibyte (c, Qnil));
7481 insert_1_both (work, 1, 1, 1, 0, 0);
7484 else if (!multibyte_p
7485 && !NILP (current_buffer->enable_multibyte_characters))
7487 /* Convert from single-byte to multi-byte. */
7488 int i, c, n;
7489 const unsigned char *msg = (const unsigned char *) s;
7490 unsigned char str[MAX_MULTIBYTE_LENGTH];
7492 /* Convert a single-byte string to multibyte. */
7493 for (i = 0; i < nbytes; i++)
7495 c = unibyte_char_to_multibyte (msg[i]);
7496 n = CHAR_STRING (c, str);
7497 insert_1_both (str, 1, n, 1, 0, 0);
7500 else
7501 insert_1 (s, nbytes, 1, 0, 0);
7504 return 0;
7508 /* Clear messages. CURRENT_P non-zero means clear the current
7509 message. LAST_DISPLAYED_P non-zero means clear the message
7510 last displayed. */
7512 void
7513 clear_message (current_p, last_displayed_p)
7514 int current_p, last_displayed_p;
7516 if (current_p)
7518 echo_area_buffer[0] = Qnil;
7519 message_cleared_p = 1;
7522 if (last_displayed_p)
7523 echo_area_buffer[1] = Qnil;
7525 message_buf_print = 0;
7528 /* Clear garbaged frames.
7530 This function is used where the old redisplay called
7531 redraw_garbaged_frames which in turn called redraw_frame which in
7532 turn called clear_frame. The call to clear_frame was a source of
7533 flickering. I believe a clear_frame is not necessary. It should
7534 suffice in the new redisplay to invalidate all current matrices,
7535 and ensure a complete redisplay of all windows. */
7537 static void
7538 clear_garbaged_frames ()
7540 if (frame_garbaged)
7542 Lisp_Object tail, frame;
7543 int changed_count = 0;
7545 FOR_EACH_FRAME (tail, frame)
7547 struct frame *f = XFRAME (frame);
7549 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7551 if (f->resized_p)
7552 Fredraw_frame (frame);
7553 clear_current_matrices (f);
7554 changed_count++;
7555 f->garbaged = 0;
7556 f->resized_p = 0;
7560 frame_garbaged = 0;
7561 if (changed_count)
7562 ++windows_or_buffers_changed;
7567 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7568 is non-zero update selected_frame. Value is non-zero if the
7569 mini-windows height has been changed. */
7571 static int
7572 echo_area_display (update_frame_p)
7573 int update_frame_p;
7575 Lisp_Object mini_window;
7576 struct window *w;
7577 struct frame *f;
7578 int window_height_changed_p = 0;
7579 struct frame *sf = SELECTED_FRAME ();
7581 mini_window = FRAME_MINIBUF_WINDOW (sf);
7582 w = XWINDOW (mini_window);
7583 f = XFRAME (WINDOW_FRAME (w));
7585 /* Don't display if frame is invisible or not yet initialized. */
7586 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7587 return 0;
7589 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7590 #ifndef MAC_OS8
7591 #ifdef HAVE_WINDOW_SYSTEM
7592 /* When Emacs starts, selected_frame may be a visible terminal
7593 frame, even if we run under a window system. If we let this
7594 through, a message would be displayed on the terminal. */
7595 if (EQ (selected_frame, Vterminal_frame)
7596 && !NILP (Vwindow_system))
7597 return 0;
7598 #endif /* HAVE_WINDOW_SYSTEM */
7599 #endif
7601 /* Redraw garbaged frames. */
7602 if (frame_garbaged)
7603 clear_garbaged_frames ();
7605 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7607 echo_area_window = mini_window;
7608 window_height_changed_p = display_echo_area (w);
7609 w->must_be_updated_p = 1;
7611 /* Update the display, unless called from redisplay_internal.
7612 Also don't update the screen during redisplay itself. The
7613 update will happen at the end of redisplay, and an update
7614 here could cause confusion. */
7615 if (update_frame_p && !redisplaying_p)
7617 int n = 0;
7619 /* If the display update has been interrupted by pending
7620 input, update mode lines in the frame. Due to the
7621 pending input, it might have been that redisplay hasn't
7622 been called, so that mode lines above the echo area are
7623 garbaged. This looks odd, so we prevent it here. */
7624 if (!display_completed)
7625 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7627 if (window_height_changed_p
7628 /* Don't do this if Emacs is shutting down. Redisplay
7629 needs to run hooks. */
7630 && !NILP (Vrun_hooks))
7632 /* Must update other windows. Likewise as in other
7633 cases, don't let this update be interrupted by
7634 pending input. */
7635 int count = SPECPDL_INDEX ();
7636 specbind (Qredisplay_dont_pause, Qt);
7637 windows_or_buffers_changed = 1;
7638 redisplay_internal (0);
7639 unbind_to (count, Qnil);
7641 else if (FRAME_WINDOW_P (f) && n == 0)
7643 /* Window configuration is the same as before.
7644 Can do with a display update of the echo area,
7645 unless we displayed some mode lines. */
7646 update_single_window (w, 1);
7647 rif->flush_display (f);
7649 else
7650 update_frame (f, 1, 1);
7652 /* If cursor is in the echo area, make sure that the next
7653 redisplay displays the minibuffer, so that the cursor will
7654 be replaced with what the minibuffer wants. */
7655 if (cursor_in_echo_area)
7656 ++windows_or_buffers_changed;
7659 else if (!EQ (mini_window, selected_window))
7660 windows_or_buffers_changed++;
7662 /* Last displayed message is now the current message. */
7663 echo_area_buffer[1] = echo_area_buffer[0];
7665 /* Prevent redisplay optimization in redisplay_internal by resetting
7666 this_line_start_pos. This is done because the mini-buffer now
7667 displays the message instead of its buffer text. */
7668 if (EQ (mini_window, selected_window))
7669 CHARPOS (this_line_start_pos) = 0;
7671 return window_height_changed_p;
7676 /***********************************************************************
7677 Frame Titles
7678 ***********************************************************************/
7681 /* The frame title buffering code is also used by Fformat_mode_line.
7682 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7684 /* A buffer for constructing frame titles in it; allocated from the
7685 heap in init_xdisp and resized as needed in store_frame_title_char. */
7687 static char *frame_title_buf;
7689 /* The buffer's end, and a current output position in it. */
7691 static char *frame_title_buf_end;
7692 static char *frame_title_ptr;
7695 /* Store a single character C for the frame title in frame_title_buf.
7696 Re-allocate frame_title_buf if necessary. */
7698 static void
7699 #ifdef PROTOTYPES
7700 store_frame_title_char (char c)
7701 #else
7702 store_frame_title_char (c)
7703 char c;
7704 #endif
7706 /* If output position has reached the end of the allocated buffer,
7707 double the buffer's size. */
7708 if (frame_title_ptr == frame_title_buf_end)
7710 int len = frame_title_ptr - frame_title_buf;
7711 int new_size = 2 * len * sizeof *frame_title_buf;
7712 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7713 frame_title_buf_end = frame_title_buf + new_size;
7714 frame_title_ptr = frame_title_buf + len;
7717 *frame_title_ptr++ = c;
7721 /* Store part of a frame title in frame_title_buf, beginning at
7722 frame_title_ptr. STR is the string to store. Do not copy
7723 characters that yield more columns than PRECISION; PRECISION <= 0
7724 means copy the whole string. Pad with spaces until FIELD_WIDTH
7725 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7726 pad. Called from display_mode_element when it is used to build a
7727 frame title. */
7729 static int
7730 store_frame_title (str, field_width, precision)
7731 const unsigned char *str;
7732 int field_width, precision;
7734 int n = 0;
7735 int dummy, nbytes;
7737 /* Copy at most PRECISION chars from STR. */
7738 nbytes = strlen (str);
7739 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7740 while (nbytes--)
7741 store_frame_title_char (*str++);
7743 /* Fill up with spaces until FIELD_WIDTH reached. */
7744 while (field_width > 0
7745 && n < field_width)
7747 store_frame_title_char (' ');
7748 ++n;
7751 return n;
7754 #ifdef HAVE_WINDOW_SYSTEM
7756 /* Set the title of FRAME, if it has changed. The title format is
7757 Vicon_title_format if FRAME is iconified, otherwise it is
7758 frame_title_format. */
7760 static void
7761 x_consider_frame_title (frame)
7762 Lisp_Object frame;
7764 struct frame *f = XFRAME (frame);
7766 if (FRAME_WINDOW_P (f)
7767 || FRAME_MINIBUF_ONLY_P (f)
7768 || f->explicit_name)
7770 /* Do we have more than one visible frame on this X display? */
7771 Lisp_Object tail;
7772 Lisp_Object fmt;
7773 struct buffer *obuf;
7774 int len;
7775 struct it it;
7777 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7779 Lisp_Object other_frame = XCAR (tail);
7780 struct frame *tf = XFRAME (other_frame);
7782 if (tf != f
7783 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7784 && !FRAME_MINIBUF_ONLY_P (tf)
7785 && !EQ (other_frame, tip_frame)
7786 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7787 break;
7790 /* Set global variable indicating that multiple frames exist. */
7791 multiple_frames = CONSP (tail);
7793 /* Switch to the buffer of selected window of the frame. Set up
7794 frame_title_ptr so that display_mode_element will output into it;
7795 then display the title. */
7796 obuf = current_buffer;
7797 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7798 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7799 frame_title_ptr = frame_title_buf;
7800 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7801 NULL, DEFAULT_FACE_ID);
7802 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7803 len = frame_title_ptr - frame_title_buf;
7804 frame_title_ptr = NULL;
7805 set_buffer_internal_1 (obuf);
7807 /* Set the title only if it's changed. This avoids consing in
7808 the common case where it hasn't. (If it turns out that we've
7809 already wasted too much time by walking through the list with
7810 display_mode_element, then we might need to optimize at a
7811 higher level than this.) */
7812 if (! STRINGP (f->name)
7813 || SBYTES (f->name) != len
7814 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7815 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7819 #endif /* not HAVE_WINDOW_SYSTEM */
7824 /***********************************************************************
7825 Menu Bars
7826 ***********************************************************************/
7829 /* Prepare for redisplay by updating menu-bar item lists when
7830 appropriate. This can call eval. */
7832 void
7833 prepare_menu_bars ()
7835 int all_windows;
7836 struct gcpro gcpro1, gcpro2;
7837 struct frame *f;
7838 Lisp_Object tooltip_frame;
7840 #ifdef HAVE_WINDOW_SYSTEM
7841 tooltip_frame = tip_frame;
7842 #else
7843 tooltip_frame = Qnil;
7844 #endif
7846 /* Update all frame titles based on their buffer names, etc. We do
7847 this before the menu bars so that the buffer-menu will show the
7848 up-to-date frame titles. */
7849 #ifdef HAVE_WINDOW_SYSTEM
7850 if (windows_or_buffers_changed || update_mode_lines)
7852 Lisp_Object tail, frame;
7854 FOR_EACH_FRAME (tail, frame)
7856 f = XFRAME (frame);
7857 if (!EQ (frame, tooltip_frame)
7858 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7859 x_consider_frame_title (frame);
7862 #endif /* HAVE_WINDOW_SYSTEM */
7864 /* Update the menu bar item lists, if appropriate. This has to be
7865 done before any actual redisplay or generation of display lines. */
7866 all_windows = (update_mode_lines
7867 || buffer_shared > 1
7868 || windows_or_buffers_changed);
7869 if (all_windows)
7871 Lisp_Object tail, frame;
7872 int count = SPECPDL_INDEX ();
7874 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7876 FOR_EACH_FRAME (tail, frame)
7878 f = XFRAME (frame);
7880 /* Ignore tooltip frame. */
7881 if (EQ (frame, tooltip_frame))
7882 continue;
7884 /* If a window on this frame changed size, report that to
7885 the user and clear the size-change flag. */
7886 if (FRAME_WINDOW_SIZES_CHANGED (f))
7888 Lisp_Object functions;
7890 /* Clear flag first in case we get an error below. */
7891 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7892 functions = Vwindow_size_change_functions;
7893 GCPRO2 (tail, functions);
7895 while (CONSP (functions))
7897 call1 (XCAR (functions), frame);
7898 functions = XCDR (functions);
7900 UNGCPRO;
7903 GCPRO1 (tail);
7904 update_menu_bar (f, 0);
7905 #ifdef HAVE_WINDOW_SYSTEM
7906 update_tool_bar (f, 0);
7907 #endif
7908 UNGCPRO;
7911 unbind_to (count, Qnil);
7913 else
7915 struct frame *sf = SELECTED_FRAME ();
7916 update_menu_bar (sf, 1);
7917 #ifdef HAVE_WINDOW_SYSTEM
7918 update_tool_bar (sf, 1);
7919 #endif
7922 /* Motif needs this. See comment in xmenu.c. Turn it off when
7923 pending_menu_activation is not defined. */
7924 #ifdef USE_X_TOOLKIT
7925 pending_menu_activation = 0;
7926 #endif
7930 /* Update the menu bar item list for frame F. This has to be done
7931 before we start to fill in any display lines, because it can call
7932 eval.
7934 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7936 static void
7937 update_menu_bar (f, save_match_data)
7938 struct frame *f;
7939 int save_match_data;
7941 Lisp_Object window;
7942 register struct window *w;
7944 /* If called recursively during a menu update, do nothing. This can
7945 happen when, for instance, an activate-menubar-hook causes a
7946 redisplay. */
7947 if (inhibit_menubar_update)
7948 return;
7950 window = FRAME_SELECTED_WINDOW (f);
7951 w = XWINDOW (window);
7953 #if 0 /* The if statement below this if statement used to include the
7954 condition !NILP (w->update_mode_line), rather than using
7955 update_mode_lines directly, and this if statement may have
7956 been added to make that condition work. Now the if
7957 statement below matches its comment, this isn't needed. */
7958 if (update_mode_lines)
7959 w->update_mode_line = Qt;
7960 #endif
7962 if (FRAME_WINDOW_P (f)
7964 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7965 || defined (USE_GTK)
7966 FRAME_EXTERNAL_MENU_BAR (f)
7967 #else
7968 FRAME_MENU_BAR_LINES (f) > 0
7969 #endif
7970 : FRAME_MENU_BAR_LINES (f) > 0)
7972 /* If the user has switched buffers or windows, we need to
7973 recompute to reflect the new bindings. But we'll
7974 recompute when update_mode_lines is set too; that means
7975 that people can use force-mode-line-update to request
7976 that the menu bar be recomputed. The adverse effect on
7977 the rest of the redisplay algorithm is about the same as
7978 windows_or_buffers_changed anyway. */
7979 if (windows_or_buffers_changed
7980 /* This used to test w->update_mode_line, but we believe
7981 there is no need to recompute the menu in that case. */
7982 || update_mode_lines
7983 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7984 < BUF_MODIFF (XBUFFER (w->buffer)))
7985 != !NILP (w->last_had_star))
7986 || ((!NILP (Vtransient_mark_mode)
7987 && !NILP (XBUFFER (w->buffer)->mark_active))
7988 != !NILP (w->region_showing)))
7990 struct buffer *prev = current_buffer;
7991 int count = SPECPDL_INDEX ();
7993 specbind (Qinhibit_menubar_update, Qt);
7995 set_buffer_internal_1 (XBUFFER (w->buffer));
7996 if (save_match_data)
7997 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7998 if (NILP (Voverriding_local_map_menu_flag))
8000 specbind (Qoverriding_terminal_local_map, Qnil);
8001 specbind (Qoverriding_local_map, Qnil);
8004 /* Run the Lucid hook. */
8005 safe_run_hooks (Qactivate_menubar_hook);
8007 /* If it has changed current-menubar from previous value,
8008 really recompute the menu-bar from the value. */
8009 if (! NILP (Vlucid_menu_bar_dirty_flag))
8010 call0 (Qrecompute_lucid_menubar);
8012 safe_run_hooks (Qmenu_bar_update_hook);
8013 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8015 /* Redisplay the menu bar in case we changed it. */
8016 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8017 || defined (USE_GTK)
8018 if (FRAME_WINDOW_P (f)
8019 #if defined (MAC_OS)
8020 /* All frames on Mac OS share the same menubar. So only the
8021 selected frame should be allowed to set it. */
8022 && f == SELECTED_FRAME ()
8023 #endif
8025 set_frame_menubar (f, 0, 0);
8026 else
8027 /* On a terminal screen, the menu bar is an ordinary screen
8028 line, and this makes it get updated. */
8029 w->update_mode_line = Qt;
8030 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8031 /* In the non-toolkit version, the menu bar is an ordinary screen
8032 line, and this makes it get updated. */
8033 w->update_mode_line = Qt;
8034 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8036 unbind_to (count, Qnil);
8037 set_buffer_internal_1 (prev);
8044 /***********************************************************************
8045 Output Cursor
8046 ***********************************************************************/
8048 #ifdef HAVE_WINDOW_SYSTEM
8050 /* EXPORT:
8051 Nominal cursor position -- where to draw output.
8052 HPOS and VPOS are window relative glyph matrix coordinates.
8053 X and Y are window relative pixel coordinates. */
8055 struct cursor_pos output_cursor;
8058 /* EXPORT:
8059 Set the global variable output_cursor to CURSOR. All cursor
8060 positions are relative to updated_window. */
8062 void
8063 set_output_cursor (cursor)
8064 struct cursor_pos *cursor;
8066 output_cursor.hpos = cursor->hpos;
8067 output_cursor.vpos = cursor->vpos;
8068 output_cursor.x = cursor->x;
8069 output_cursor.y = cursor->y;
8073 /* EXPORT for RIF:
8074 Set a nominal cursor position.
8076 HPOS and VPOS are column/row positions in a window glyph matrix. X
8077 and Y are window text area relative pixel positions.
8079 If this is done during an update, updated_window will contain the
8080 window that is being updated and the position is the future output
8081 cursor position for that window. If updated_window is null, use
8082 selected_window and display the cursor at the given position. */
8084 void
8085 x_cursor_to (vpos, hpos, y, x)
8086 int vpos, hpos, y, x;
8088 struct window *w;
8090 /* If updated_window is not set, work on selected_window. */
8091 if (updated_window)
8092 w = updated_window;
8093 else
8094 w = XWINDOW (selected_window);
8096 /* Set the output cursor. */
8097 output_cursor.hpos = hpos;
8098 output_cursor.vpos = vpos;
8099 output_cursor.x = x;
8100 output_cursor.y = y;
8102 /* If not called as part of an update, really display the cursor.
8103 This will also set the cursor position of W. */
8104 if (updated_window == NULL)
8106 BLOCK_INPUT;
8107 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8108 if (rif->flush_display_optional)
8109 rif->flush_display_optional (SELECTED_FRAME ());
8110 UNBLOCK_INPUT;
8114 #endif /* HAVE_WINDOW_SYSTEM */
8117 /***********************************************************************
8118 Tool-bars
8119 ***********************************************************************/
8121 #ifdef HAVE_WINDOW_SYSTEM
8123 /* Where the mouse was last time we reported a mouse event. */
8125 FRAME_PTR last_mouse_frame;
8127 /* Tool-bar item index of the item on which a mouse button was pressed
8128 or -1. */
8130 int last_tool_bar_item;
8133 /* Update the tool-bar item list for frame F. This has to be done
8134 before we start to fill in any display lines. Called from
8135 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8136 and restore it here. */
8138 static void
8139 update_tool_bar (f, save_match_data)
8140 struct frame *f;
8141 int save_match_data;
8143 #ifdef USE_GTK
8144 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8145 #else
8146 int do_update = WINDOWP (f->tool_bar_window)
8147 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8148 #endif
8150 if (do_update)
8152 Lisp_Object window;
8153 struct window *w;
8155 window = FRAME_SELECTED_WINDOW (f);
8156 w = XWINDOW (window);
8158 /* If the user has switched buffers or windows, we need to
8159 recompute to reflect the new bindings. But we'll
8160 recompute when update_mode_lines is set too; that means
8161 that people can use force-mode-line-update to request
8162 that the menu bar be recomputed. The adverse effect on
8163 the rest of the redisplay algorithm is about the same as
8164 windows_or_buffers_changed anyway. */
8165 if (windows_or_buffers_changed
8166 || !NILP (w->update_mode_line)
8167 || update_mode_lines
8168 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8169 < BUF_MODIFF (XBUFFER (w->buffer)))
8170 != !NILP (w->last_had_star))
8171 || ((!NILP (Vtransient_mark_mode)
8172 && !NILP (XBUFFER (w->buffer)->mark_active))
8173 != !NILP (w->region_showing)))
8175 struct buffer *prev = current_buffer;
8176 int count = SPECPDL_INDEX ();
8177 Lisp_Object old_tool_bar;
8178 struct gcpro gcpro1;
8180 /* Set current_buffer to the buffer of the selected
8181 window of the frame, so that we get the right local
8182 keymaps. */
8183 set_buffer_internal_1 (XBUFFER (w->buffer));
8185 /* Save match data, if we must. */
8186 if (save_match_data)
8187 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8189 /* Make sure that we don't accidentally use bogus keymaps. */
8190 if (NILP (Voverriding_local_map_menu_flag))
8192 specbind (Qoverriding_terminal_local_map, Qnil);
8193 specbind (Qoverriding_local_map, Qnil);
8196 old_tool_bar = f->tool_bar_items;
8197 GCPRO1 (old_tool_bar);
8199 /* Build desired tool-bar items from keymaps. */
8200 BLOCK_INPUT;
8201 f->tool_bar_items
8202 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8203 UNBLOCK_INPUT;
8205 /* Redisplay the tool-bar if we changed it. */
8206 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8207 w->update_mode_line = Qt;
8209 UNGCPRO;
8211 unbind_to (count, Qnil);
8212 set_buffer_internal_1 (prev);
8218 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8219 F's desired tool-bar contents. F->tool_bar_items must have
8220 been set up previously by calling prepare_menu_bars. */
8222 static void
8223 build_desired_tool_bar_string (f)
8224 struct frame *f;
8226 int i, size, size_needed;
8227 struct gcpro gcpro1, gcpro2, gcpro3;
8228 Lisp_Object image, plist, props;
8230 image = plist = props = Qnil;
8231 GCPRO3 (image, plist, props);
8233 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8234 Otherwise, make a new string. */
8236 /* The size of the string we might be able to reuse. */
8237 size = (STRINGP (f->desired_tool_bar_string)
8238 ? SCHARS (f->desired_tool_bar_string)
8239 : 0);
8241 /* We need one space in the string for each image. */
8242 size_needed = f->n_tool_bar_items;
8244 /* Reuse f->desired_tool_bar_string, if possible. */
8245 if (size < size_needed || NILP (f->desired_tool_bar_string))
8246 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8247 make_number (' '));
8248 else
8250 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8251 Fremove_text_properties (make_number (0), make_number (size),
8252 props, f->desired_tool_bar_string);
8255 /* Put a `display' property on the string for the images to display,
8256 put a `menu_item' property on tool-bar items with a value that
8257 is the index of the item in F's tool-bar item vector. */
8258 for (i = 0; i < f->n_tool_bar_items; ++i)
8260 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8262 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8263 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8264 int hmargin, vmargin, relief, idx, end;
8265 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8267 /* If image is a vector, choose the image according to the
8268 button state. */
8269 image = PROP (TOOL_BAR_ITEM_IMAGES);
8270 if (VECTORP (image))
8272 if (enabled_p)
8273 idx = (selected_p
8274 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8275 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8276 else
8277 idx = (selected_p
8278 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8279 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8281 xassert (ASIZE (image) >= idx);
8282 image = AREF (image, idx);
8284 else
8285 idx = -1;
8287 /* Ignore invalid image specifications. */
8288 if (!valid_image_p (image))
8289 continue;
8291 /* Display the tool-bar button pressed, or depressed. */
8292 plist = Fcopy_sequence (XCDR (image));
8294 /* Compute margin and relief to draw. */
8295 relief = (tool_bar_button_relief >= 0
8296 ? tool_bar_button_relief
8297 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8298 hmargin = vmargin = relief;
8300 if (INTEGERP (Vtool_bar_button_margin)
8301 && XINT (Vtool_bar_button_margin) > 0)
8303 hmargin += XFASTINT (Vtool_bar_button_margin);
8304 vmargin += XFASTINT (Vtool_bar_button_margin);
8306 else if (CONSP (Vtool_bar_button_margin))
8308 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8309 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8310 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8312 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8313 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8314 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8317 if (auto_raise_tool_bar_buttons_p)
8319 /* Add a `:relief' property to the image spec if the item is
8320 selected. */
8321 if (selected_p)
8323 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8324 hmargin -= relief;
8325 vmargin -= relief;
8328 else
8330 /* If image is selected, display it pressed, i.e. with a
8331 negative relief. If it's not selected, display it with a
8332 raised relief. */
8333 plist = Fplist_put (plist, QCrelief,
8334 (selected_p
8335 ? make_number (-relief)
8336 : make_number (relief)));
8337 hmargin -= relief;
8338 vmargin -= relief;
8341 /* Put a margin around the image. */
8342 if (hmargin || vmargin)
8344 if (hmargin == vmargin)
8345 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8346 else
8347 plist = Fplist_put (plist, QCmargin,
8348 Fcons (make_number (hmargin),
8349 make_number (vmargin)));
8352 /* If button is not enabled, and we don't have special images
8353 for the disabled state, make the image appear disabled by
8354 applying an appropriate algorithm to it. */
8355 if (!enabled_p && idx < 0)
8356 plist = Fplist_put (plist, QCconversion, Qdisabled);
8358 /* Put a `display' text property on the string for the image to
8359 display. Put a `menu-item' property on the string that gives
8360 the start of this item's properties in the tool-bar items
8361 vector. */
8362 image = Fcons (Qimage, plist);
8363 props = list4 (Qdisplay, image,
8364 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8366 /* Let the last image hide all remaining spaces in the tool bar
8367 string. The string can be longer than needed when we reuse a
8368 previous string. */
8369 if (i + 1 == f->n_tool_bar_items)
8370 end = SCHARS (f->desired_tool_bar_string);
8371 else
8372 end = i + 1;
8373 Fadd_text_properties (make_number (i), make_number (end),
8374 props, f->desired_tool_bar_string);
8375 #undef PROP
8378 UNGCPRO;
8382 /* Display one line of the tool-bar of frame IT->f. */
8384 static void
8385 display_tool_bar_line (it)
8386 struct it *it;
8388 struct glyph_row *row = it->glyph_row;
8389 int max_x = it->last_visible_x;
8390 struct glyph *last;
8392 prepare_desired_row (row);
8393 row->y = it->current_y;
8395 /* Note that this isn't made use of if the face hasn't a box,
8396 so there's no need to check the face here. */
8397 it->start_of_box_run_p = 1;
8399 while (it->current_x < max_x)
8401 int x_before, x, n_glyphs_before, i, nglyphs;
8403 /* Get the next display element. */
8404 if (!get_next_display_element (it))
8405 break;
8407 /* Produce glyphs. */
8408 x_before = it->current_x;
8409 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8410 PRODUCE_GLYPHS (it);
8412 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8413 i = 0;
8414 x = x_before;
8415 while (i < nglyphs)
8417 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8419 if (x + glyph->pixel_width > max_x)
8421 /* Glyph doesn't fit on line. */
8422 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8423 it->current_x = x;
8424 goto out;
8427 ++it->hpos;
8428 x += glyph->pixel_width;
8429 ++i;
8432 /* Stop at line ends. */
8433 if (ITERATOR_AT_END_OF_LINE_P (it))
8434 break;
8436 set_iterator_to_next (it, 1);
8439 out:;
8441 row->displays_text_p = row->used[TEXT_AREA] != 0;
8442 extend_face_to_end_of_line (it);
8443 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8444 last->right_box_line_p = 1;
8445 if (last == row->glyphs[TEXT_AREA])
8446 last->left_box_line_p = 1;
8447 compute_line_metrics (it);
8449 /* If line is empty, make it occupy the rest of the tool-bar. */
8450 if (!row->displays_text_p)
8452 row->height = row->phys_height = it->last_visible_y - row->y;
8453 row->ascent = row->phys_ascent = 0;
8456 row->full_width_p = 1;
8457 row->continued_p = 0;
8458 row->truncated_on_left_p = 0;
8459 row->truncated_on_right_p = 0;
8461 it->current_x = it->hpos = 0;
8462 it->current_y += row->height;
8463 ++it->vpos;
8464 ++it->glyph_row;
8468 /* Value is the number of screen lines needed to make all tool-bar
8469 items of frame F visible. */
8471 static int
8472 tool_bar_lines_needed (f)
8473 struct frame *f;
8475 struct window *w = XWINDOW (f->tool_bar_window);
8476 struct it it;
8478 /* Initialize an iterator for iteration over
8479 F->desired_tool_bar_string in the tool-bar window of frame F. */
8480 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8481 it.first_visible_x = 0;
8482 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8483 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8485 while (!ITERATOR_AT_END_P (&it))
8487 it.glyph_row = w->desired_matrix->rows;
8488 clear_glyph_row (it.glyph_row);
8489 display_tool_bar_line (&it);
8492 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8496 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8497 0, 1, 0,
8498 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8499 (frame)
8500 Lisp_Object frame;
8502 struct frame *f;
8503 struct window *w;
8504 int nlines = 0;
8506 if (NILP (frame))
8507 frame = selected_frame;
8508 else
8509 CHECK_FRAME (frame);
8510 f = XFRAME (frame);
8512 if (WINDOWP (f->tool_bar_window)
8513 || (w = XWINDOW (f->tool_bar_window),
8514 WINDOW_TOTAL_LINES (w) > 0))
8516 update_tool_bar (f, 1);
8517 if (f->n_tool_bar_items)
8519 build_desired_tool_bar_string (f);
8520 nlines = tool_bar_lines_needed (f);
8524 return make_number (nlines);
8528 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8529 height should be changed. */
8531 static int
8532 redisplay_tool_bar (f)
8533 struct frame *f;
8535 struct window *w;
8536 struct it it;
8537 struct glyph_row *row;
8538 int change_height_p = 0;
8540 #ifdef USE_GTK
8541 if (FRAME_EXTERNAL_TOOL_BAR(f))
8542 update_frame_tool_bar (f);
8543 return 0;
8544 #endif
8546 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8547 do anything. This means you must start with tool-bar-lines
8548 non-zero to get the auto-sizing effect. Or in other words, you
8549 can turn off tool-bars by specifying tool-bar-lines zero. */
8550 if (!WINDOWP (f->tool_bar_window)
8551 || (w = XWINDOW (f->tool_bar_window),
8552 WINDOW_TOTAL_LINES (w) == 0))
8553 return 0;
8555 /* Set up an iterator for the tool-bar window. */
8556 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8557 it.first_visible_x = 0;
8558 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8559 row = it.glyph_row;
8561 /* Build a string that represents the contents of the tool-bar. */
8562 build_desired_tool_bar_string (f);
8563 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8565 /* Display as many lines as needed to display all tool-bar items. */
8566 while (it.current_y < it.last_visible_y)
8567 display_tool_bar_line (&it);
8569 /* It doesn't make much sense to try scrolling in the tool-bar
8570 window, so don't do it. */
8571 w->desired_matrix->no_scrolling_p = 1;
8572 w->must_be_updated_p = 1;
8574 if (auto_resize_tool_bars_p)
8576 int nlines;
8578 /* If we couldn't display everything, change the tool-bar's
8579 height. */
8580 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8581 change_height_p = 1;
8583 /* If there are blank lines at the end, except for a partially
8584 visible blank line at the end that is smaller than
8585 FRAME_LINE_HEIGHT, change the tool-bar's height. */
8586 row = it.glyph_row - 1;
8587 if (!row->displays_text_p
8588 && row->height >= FRAME_LINE_HEIGHT (f))
8589 change_height_p = 1;
8591 /* If row displays tool-bar items, but is partially visible,
8592 change the tool-bar's height. */
8593 if (row->displays_text_p
8594 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8595 change_height_p = 1;
8597 /* Resize windows as needed by changing the `tool-bar-lines'
8598 frame parameter. */
8599 if (change_height_p
8600 && (nlines = tool_bar_lines_needed (f),
8601 nlines != WINDOW_TOTAL_LINES (w)))
8603 extern Lisp_Object Qtool_bar_lines;
8604 Lisp_Object frame;
8605 int old_height = WINDOW_TOTAL_LINES (w);
8607 XSETFRAME (frame, f);
8608 clear_glyph_matrix (w->desired_matrix);
8609 Fmodify_frame_parameters (frame,
8610 Fcons (Fcons (Qtool_bar_lines,
8611 make_number (nlines)),
8612 Qnil));
8613 if (WINDOW_TOTAL_LINES (w) != old_height)
8614 fonts_changed_p = 1;
8618 return change_height_p;
8622 /* Get information about the tool-bar item which is displayed in GLYPH
8623 on frame F. Return in *PROP_IDX the index where tool-bar item
8624 properties start in F->tool_bar_items. Value is zero if
8625 GLYPH doesn't display a tool-bar item. */
8627 static int
8628 tool_bar_item_info (f, glyph, prop_idx)
8629 struct frame *f;
8630 struct glyph *glyph;
8631 int *prop_idx;
8633 Lisp_Object prop;
8634 int success_p;
8635 int charpos;
8637 /* This function can be called asynchronously, which means we must
8638 exclude any possibility that Fget_text_property signals an
8639 error. */
8640 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8641 charpos = max (0, charpos);
8643 /* Get the text property `menu-item' at pos. The value of that
8644 property is the start index of this item's properties in
8645 F->tool_bar_items. */
8646 prop = Fget_text_property (make_number (charpos),
8647 Qmenu_item, f->current_tool_bar_string);
8648 if (INTEGERP (prop))
8650 *prop_idx = XINT (prop);
8651 success_p = 1;
8653 else
8654 success_p = 0;
8656 return success_p;
8660 /* Get information about the tool-bar item at position X/Y on frame F.
8661 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8662 the current matrix of the tool-bar window of F, or NULL if not
8663 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8664 item in F->tool_bar_items. Value is
8666 -1 if X/Y is not on a tool-bar item
8667 0 if X/Y is on the same item that was highlighted before.
8668 1 otherwise. */
8670 static int
8671 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8672 struct frame *f;
8673 int x, y;
8674 struct glyph **glyph;
8675 int *hpos, *vpos, *prop_idx;
8677 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8678 struct window *w = XWINDOW (f->tool_bar_window);
8679 int area;
8681 /* Find the glyph under X/Y. */
8682 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8683 if (*glyph == NULL)
8684 return -1;
8686 /* Get the start of this tool-bar item's properties in
8687 f->tool_bar_items. */
8688 if (!tool_bar_item_info (f, *glyph, prop_idx))
8689 return -1;
8691 /* Is mouse on the highlighted item? */
8692 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8693 && *vpos >= dpyinfo->mouse_face_beg_row
8694 && *vpos <= dpyinfo->mouse_face_end_row
8695 && (*vpos > dpyinfo->mouse_face_beg_row
8696 || *hpos >= dpyinfo->mouse_face_beg_col)
8697 && (*vpos < dpyinfo->mouse_face_end_row
8698 || *hpos < dpyinfo->mouse_face_end_col
8699 || dpyinfo->mouse_face_past_end))
8700 return 0;
8702 return 1;
8706 /* EXPORT:
8707 Handle mouse button event on the tool-bar of frame F, at
8708 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8709 0 for button release. MODIFIERS is event modifiers for button
8710 release. */
8712 void
8713 handle_tool_bar_click (f, x, y, down_p, modifiers)
8714 struct frame *f;
8715 int x, y, down_p;
8716 unsigned int modifiers;
8718 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8719 struct window *w = XWINDOW (f->tool_bar_window);
8720 int hpos, vpos, prop_idx;
8721 struct glyph *glyph;
8722 Lisp_Object enabled_p;
8724 /* If not on the highlighted tool-bar item, return. */
8725 frame_to_window_pixel_xy (w, &x, &y);
8726 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8727 return;
8729 /* If item is disabled, do nothing. */
8730 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8731 if (NILP (enabled_p))
8732 return;
8734 if (down_p)
8736 /* Show item in pressed state. */
8737 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8738 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8739 last_tool_bar_item = prop_idx;
8741 else
8743 Lisp_Object key, frame;
8744 struct input_event event;
8745 EVENT_INIT (event);
8747 /* Show item in released state. */
8748 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8749 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8751 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8753 XSETFRAME (frame, f);
8754 event.kind = TOOL_BAR_EVENT;
8755 event.frame_or_window = frame;
8756 event.arg = frame;
8757 kbd_buffer_store_event (&event);
8759 event.kind = TOOL_BAR_EVENT;
8760 event.frame_or_window = frame;
8761 event.arg = key;
8762 event.modifiers = modifiers;
8763 kbd_buffer_store_event (&event);
8764 last_tool_bar_item = -1;
8769 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8770 tool-bar window-relative coordinates X/Y. Called from
8771 note_mouse_highlight. */
8773 static void
8774 note_tool_bar_highlight (f, x, y)
8775 struct frame *f;
8776 int x, y;
8778 Lisp_Object window = f->tool_bar_window;
8779 struct window *w = XWINDOW (window);
8780 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8781 int hpos, vpos;
8782 struct glyph *glyph;
8783 struct glyph_row *row;
8784 int i;
8785 Lisp_Object enabled_p;
8786 int prop_idx;
8787 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8788 int mouse_down_p, rc;
8790 /* Function note_mouse_highlight is called with negative x(y
8791 values when mouse moves outside of the frame. */
8792 if (x <= 0 || y <= 0)
8794 clear_mouse_face (dpyinfo);
8795 return;
8798 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8799 if (rc < 0)
8801 /* Not on tool-bar item. */
8802 clear_mouse_face (dpyinfo);
8803 return;
8805 else if (rc == 0)
8806 /* On same tool-bar item as before. */
8807 goto set_help_echo;
8809 clear_mouse_face (dpyinfo);
8811 /* Mouse is down, but on different tool-bar item? */
8812 mouse_down_p = (dpyinfo->grabbed
8813 && f == last_mouse_frame
8814 && FRAME_LIVE_P (f));
8815 if (mouse_down_p
8816 && last_tool_bar_item != prop_idx)
8817 return;
8819 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8820 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8822 /* If tool-bar item is not enabled, don't highlight it. */
8823 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8824 if (!NILP (enabled_p))
8826 /* Compute the x-position of the glyph. In front and past the
8827 image is a space. We include this in the highlighted area. */
8828 row = MATRIX_ROW (w->current_matrix, vpos);
8829 for (i = x = 0; i < hpos; ++i)
8830 x += row->glyphs[TEXT_AREA][i].pixel_width;
8832 /* Record this as the current active region. */
8833 dpyinfo->mouse_face_beg_col = hpos;
8834 dpyinfo->mouse_face_beg_row = vpos;
8835 dpyinfo->mouse_face_beg_x = x;
8836 dpyinfo->mouse_face_beg_y = row->y;
8837 dpyinfo->mouse_face_past_end = 0;
8839 dpyinfo->mouse_face_end_col = hpos + 1;
8840 dpyinfo->mouse_face_end_row = vpos;
8841 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8842 dpyinfo->mouse_face_end_y = row->y;
8843 dpyinfo->mouse_face_window = window;
8844 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8846 /* Display it as active. */
8847 show_mouse_face (dpyinfo, draw);
8848 dpyinfo->mouse_face_image_state = draw;
8851 set_help_echo:
8853 /* Set help_echo_string to a help string to display for this tool-bar item.
8854 XTread_socket does the rest. */
8855 help_echo_object = help_echo_window = Qnil;
8856 help_echo_pos = -1;
8857 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8858 if (NILP (help_echo_string))
8859 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8862 #endif /* HAVE_WINDOW_SYSTEM */
8866 /***********************************************************************
8867 Fringes
8868 ***********************************************************************/
8870 #ifdef HAVE_WINDOW_SYSTEM
8872 /* An arrow like this: `<-'. */
8873 static unsigned char left_bits[] = {
8874 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8876 /* Right truncation arrow bitmap `->'. */
8877 static unsigned char right_bits[] = {
8878 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8880 /* Marker for continued lines. */
8881 static unsigned char continued_bits[] = {
8882 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8884 /* Marker for continuation lines. */
8885 static unsigned char continuation_bits[] = {
8886 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8888 /* Overlay arrow bitmap. A triangular arrow. */
8889 static unsigned char ov_bits[] = {
8890 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8892 /* Bitmap drawn to indicate lines not displaying text if
8893 `indicate-empty-lines' is non-nil. */
8894 static unsigned char zv_bits[] = {
8895 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8896 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8897 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8898 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8899 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8900 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8901 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8902 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8904 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8906 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8907 { 8, sizeof (left_bits), 0, left_bits },
8908 { 8, sizeof (right_bits), 0, right_bits },
8909 { 8, sizeof (continued_bits), 0, continued_bits },
8910 { 8, sizeof (continuation_bits), 0, continuation_bits },
8911 { 8, sizeof (ov_bits), 0, ov_bits },
8912 { 8, sizeof (zv_bits), 3, zv_bits }
8916 /* Draw the bitmap WHICH in one of the left or right fringes of
8917 window W. ROW is the glyph row for which to display the bitmap; it
8918 determines the vertical position at which the bitmap has to be
8919 drawn. */
8921 static void
8922 draw_fringe_bitmap (w, row, which, left_p)
8923 struct window *w;
8924 struct glyph_row *row;
8925 enum fringe_bitmap_type which;
8926 int left_p;
8928 struct frame *f = XFRAME (WINDOW_FRAME (w));
8929 struct draw_fringe_bitmap_params p;
8931 /* Convert row to frame coordinates. */
8932 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8934 p.which = which;
8935 p.wd = fringe_bitmaps[which].width;
8937 p.h = fringe_bitmaps[which].height;
8938 p.dh = (fringe_bitmaps[which].period
8939 ? (p.y % fringe_bitmaps[which].period)
8940 : 0);
8941 p.h -= p.dh;
8942 /* Clip bitmap if too high. */
8943 if (p.h > row->height)
8944 p.h = row->height;
8946 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8947 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8949 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8950 the fringe. */
8951 p.bx = -1;
8952 if (left_p)
8954 int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
8955 int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8956 ? LEFT_MARGIN_AREA
8957 : TEXT_AREA));
8958 if (p.wd > wd)
8959 p.wd = wd;
8960 p.x = x - p.wd - (wd - p.wd) / 2;
8962 if (p.wd < wd || row->height > p.h)
8964 /* If W has a vertical border to its left, don't draw over it. */
8965 wd -= ((!WINDOW_LEFTMOST_P (w)
8966 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
8967 ? 1 : 0);
8968 p.bx = x - wd;
8969 p.nx = wd;
8972 else
8974 int x = window_box_right (w,
8975 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
8976 ? RIGHT_MARGIN_AREA
8977 : TEXT_AREA));
8978 int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
8979 if (p.wd > wd)
8980 p.wd = wd;
8981 p.x = x + (wd - p.wd) / 2;
8982 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8983 the fringe. */
8984 if (p.wd < wd || row->height > p.h)
8986 p.bx = x;
8987 p.nx = wd;
8991 if (p.bx >= 0)
8993 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
8995 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8996 p.ny = row->visible_height;
8999 /* Adjust y to the offset in the row to start drawing the bitmap. */
9000 p.y += (row->height - p.h) / 2;
9002 rif->draw_fringe_bitmap (w, row, &p);
9005 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
9006 function with input blocked. */
9008 void
9009 draw_row_fringe_bitmaps (w, row)
9010 struct window *w;
9011 struct glyph_row *row;
9013 enum fringe_bitmap_type bitmap;
9015 xassert (interrupt_input_blocked);
9017 /* If row is completely invisible, because of vscrolling, we
9018 don't have to draw anything. */
9019 if (row->visible_height <= 0)
9020 return;
9022 if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
9024 /* Decide which bitmap to draw in the left fringe. */
9025 if (row->overlay_arrow_p)
9026 bitmap = OVERLAY_ARROW_BITMAP;
9027 else if (row->truncated_on_left_p)
9028 bitmap = LEFT_TRUNCATION_BITMAP;
9029 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
9030 bitmap = CONTINUATION_LINE_BITMAP;
9031 else if (row->indicate_empty_line_p)
9032 bitmap = ZV_LINE_BITMAP;
9033 else
9034 bitmap = NO_FRINGE_BITMAP;
9036 draw_fringe_bitmap (w, row, bitmap, 1);
9039 if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
9041 /* Decide which bitmap to draw in the right fringe. */
9042 if (row->truncated_on_right_p)
9043 bitmap = RIGHT_TRUNCATION_BITMAP;
9044 else if (row->continued_p)
9045 bitmap = CONTINUED_LINE_BITMAP;
9046 else if (row->indicate_empty_line_p && WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
9047 bitmap = ZV_LINE_BITMAP;
9048 else
9049 bitmap = NO_FRINGE_BITMAP;
9051 draw_fringe_bitmap (w, row, bitmap, 0);
9056 /* Compute actual fringe widths */
9058 void
9059 compute_fringe_widths (f, redraw)
9060 struct frame *f;
9061 int redraw;
9063 int o_left = FRAME_LEFT_FRINGE_WIDTH (f);
9064 int o_right = FRAME_RIGHT_FRINGE_WIDTH (f);
9065 int o_cols = FRAME_FRINGE_COLS (f);
9067 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9068 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9069 int left_fringe_width, right_fringe_width;
9071 if (!NILP (left_fringe))
9072 left_fringe = Fcdr (left_fringe);
9073 if (!NILP (right_fringe))
9074 right_fringe = Fcdr (right_fringe);
9076 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9077 XINT (left_fringe));
9078 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9079 XINT (right_fringe));
9081 if (left_fringe_width || right_fringe_width)
9083 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9084 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9085 int conf_wid = left_wid + right_wid;
9086 int font_wid = FRAME_COLUMN_WIDTH (f);
9087 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9088 int real_wid = cols * font_wid;
9089 if (left_wid && right_wid)
9091 if (left_fringe_width < 0)
9093 /* Left fringe width is fixed, adjust right fringe if necessary */
9094 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid;
9095 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9097 else if (right_fringe_width < 0)
9099 /* Right fringe width is fixed, adjust left fringe if necessary */
9100 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9101 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid;
9103 else
9105 /* Adjust both fringes with an equal amount.
9106 Note that we are doing integer arithmetic here, so don't
9107 lose a pixel if the total width is an odd number. */
9108 int fill = real_wid - conf_wid;
9109 FRAME_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9110 FRAME_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9113 else if (left_fringe_width)
9115 FRAME_LEFT_FRINGE_WIDTH (f) = real_wid;
9116 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9118 else
9120 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9121 FRAME_RIGHT_FRINGE_WIDTH (f) = real_wid;
9123 FRAME_FRINGE_COLS (f) = cols;
9125 else
9127 FRAME_LEFT_FRINGE_WIDTH (f) = 0;
9128 FRAME_RIGHT_FRINGE_WIDTH (f) = 0;
9129 FRAME_FRINGE_COLS (f) = 0;
9132 if (redraw && FRAME_VISIBLE_P (f))
9133 if (o_left != FRAME_LEFT_FRINGE_WIDTH (f) ||
9134 o_right != FRAME_RIGHT_FRINGE_WIDTH (f) ||
9135 o_cols != FRAME_FRINGE_COLS (f))
9136 redraw_frame (f);
9139 #endif /* HAVE_WINDOW_SYSTEM */
9143 /************************************************************************
9144 Horizontal scrolling
9145 ************************************************************************/
9147 static int hscroll_window_tree P_ ((Lisp_Object));
9148 static int hscroll_windows P_ ((Lisp_Object));
9150 /* For all leaf windows in the window tree rooted at WINDOW, set their
9151 hscroll value so that PT is (i) visible in the window, and (ii) so
9152 that it is not within a certain margin at the window's left and
9153 right border. Value is non-zero if any window's hscroll has been
9154 changed. */
9156 static int
9157 hscroll_window_tree (window)
9158 Lisp_Object window;
9160 int hscrolled_p = 0;
9161 int hscroll_relative_p = FLOATP (Vhscroll_step);
9162 int hscroll_step_abs = 0;
9163 double hscroll_step_rel = 0;
9165 if (hscroll_relative_p)
9167 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9168 if (hscroll_step_rel < 0)
9170 hscroll_relative_p = 0;
9171 hscroll_step_abs = 0;
9174 else if (INTEGERP (Vhscroll_step))
9176 hscroll_step_abs = XINT (Vhscroll_step);
9177 if (hscroll_step_abs < 0)
9178 hscroll_step_abs = 0;
9180 else
9181 hscroll_step_abs = 0;
9183 while (WINDOWP (window))
9185 struct window *w = XWINDOW (window);
9187 if (WINDOWP (w->hchild))
9188 hscrolled_p |= hscroll_window_tree (w->hchild);
9189 else if (WINDOWP (w->vchild))
9190 hscrolled_p |= hscroll_window_tree (w->vchild);
9191 else if (w->cursor.vpos >= 0)
9193 int h_margin;
9194 int text_area_width;
9195 struct glyph_row *current_cursor_row
9196 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9197 struct glyph_row *desired_cursor_row
9198 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9199 struct glyph_row *cursor_row
9200 = (desired_cursor_row->enabled_p
9201 ? desired_cursor_row
9202 : current_cursor_row);
9204 text_area_width = window_box_width (w, TEXT_AREA);
9206 /* Scroll when cursor is inside this scroll margin. */
9207 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9209 if ((XFASTINT (w->hscroll)
9210 && w->cursor.x <= h_margin)
9211 || (cursor_row->enabled_p
9212 && cursor_row->truncated_on_right_p
9213 && (w->cursor.x >= text_area_width - h_margin)))
9215 struct it it;
9216 int hscroll;
9217 struct buffer *saved_current_buffer;
9218 int pt;
9219 int wanted_x;
9221 /* Find point in a display of infinite width. */
9222 saved_current_buffer = current_buffer;
9223 current_buffer = XBUFFER (w->buffer);
9225 if (w == XWINDOW (selected_window))
9226 pt = BUF_PT (current_buffer);
9227 else
9229 pt = marker_position (w->pointm);
9230 pt = max (BEGV, pt);
9231 pt = min (ZV, pt);
9234 /* Move iterator to pt starting at cursor_row->start in
9235 a line with infinite width. */
9236 init_to_row_start (&it, w, cursor_row);
9237 it.last_visible_x = INFINITY;
9238 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9239 current_buffer = saved_current_buffer;
9241 /* Position cursor in window. */
9242 if (!hscroll_relative_p && hscroll_step_abs == 0)
9243 hscroll = max (0, it.current_x - text_area_width / 2)
9244 / FRAME_COLUMN_WIDTH (it.f);
9245 else if (w->cursor.x >= text_area_width - h_margin)
9247 if (hscroll_relative_p)
9248 wanted_x = text_area_width * (1 - hscroll_step_rel)
9249 - h_margin;
9250 else
9251 wanted_x = text_area_width
9252 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9253 - h_margin;
9254 hscroll
9255 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9257 else
9259 if (hscroll_relative_p)
9260 wanted_x = text_area_width * hscroll_step_rel
9261 + h_margin;
9262 else
9263 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9264 + h_margin;
9265 hscroll
9266 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9268 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9270 /* Don't call Fset_window_hscroll if value hasn't
9271 changed because it will prevent redisplay
9272 optimizations. */
9273 if (XFASTINT (w->hscroll) != hscroll)
9275 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9276 w->hscroll = make_number (hscroll);
9277 hscrolled_p = 1;
9282 window = w->next;
9285 /* Value is non-zero if hscroll of any leaf window has been changed. */
9286 return hscrolled_p;
9290 /* Set hscroll so that cursor is visible and not inside horizontal
9291 scroll margins for all windows in the tree rooted at WINDOW. See
9292 also hscroll_window_tree above. Value is non-zero if any window's
9293 hscroll has been changed. If it has, desired matrices on the frame
9294 of WINDOW are cleared. */
9296 static int
9297 hscroll_windows (window)
9298 Lisp_Object window;
9300 int hscrolled_p;
9302 if (automatic_hscrolling_p)
9304 hscrolled_p = hscroll_window_tree (window);
9305 if (hscrolled_p)
9306 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9308 else
9309 hscrolled_p = 0;
9310 return hscrolled_p;
9315 /************************************************************************
9316 Redisplay
9317 ************************************************************************/
9319 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9320 to a non-zero value. This is sometimes handy to have in a debugger
9321 session. */
9323 #if GLYPH_DEBUG
9325 /* First and last unchanged row for try_window_id. */
9327 int debug_first_unchanged_at_end_vpos;
9328 int debug_last_unchanged_at_beg_vpos;
9330 /* Delta vpos and y. */
9332 int debug_dvpos, debug_dy;
9334 /* Delta in characters and bytes for try_window_id. */
9336 int debug_delta, debug_delta_bytes;
9338 /* Values of window_end_pos and window_end_vpos at the end of
9339 try_window_id. */
9341 EMACS_INT debug_end_pos, debug_end_vpos;
9343 /* Append a string to W->desired_matrix->method. FMT is a printf
9344 format string. A1...A9 are a supplement for a variable-length
9345 argument list. If trace_redisplay_p is non-zero also printf the
9346 resulting string to stderr. */
9348 static void
9349 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9350 struct window *w;
9351 char *fmt;
9352 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9354 char buffer[512];
9355 char *method = w->desired_matrix->method;
9356 int len = strlen (method);
9357 int size = sizeof w->desired_matrix->method;
9358 int remaining = size - len - 1;
9360 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9361 if (len && remaining)
9363 method[len] = '|';
9364 --remaining, ++len;
9367 strncpy (method + len, buffer, remaining);
9369 if (trace_redisplay_p)
9370 fprintf (stderr, "%p (%s): %s\n",
9372 ((BUFFERP (w->buffer)
9373 && STRINGP (XBUFFER (w->buffer)->name))
9374 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9375 : "no buffer"),
9376 buffer);
9379 #endif /* GLYPH_DEBUG */
9382 /* Value is non-zero if all changes in window W, which displays
9383 current_buffer, are in the text between START and END. START is a
9384 buffer position, END is given as a distance from Z. Used in
9385 redisplay_internal for display optimization. */
9387 static INLINE int
9388 text_outside_line_unchanged_p (w, start, end)
9389 struct window *w;
9390 int start, end;
9392 int unchanged_p = 1;
9394 /* If text or overlays have changed, see where. */
9395 if (XFASTINT (w->last_modified) < MODIFF
9396 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9398 /* Gap in the line? */
9399 if (GPT < start || Z - GPT < end)
9400 unchanged_p = 0;
9402 /* Changes start in front of the line, or end after it? */
9403 if (unchanged_p
9404 && (BEG_UNCHANGED < start - 1
9405 || END_UNCHANGED < end))
9406 unchanged_p = 0;
9408 /* If selective display, can't optimize if changes start at the
9409 beginning of the line. */
9410 if (unchanged_p
9411 && INTEGERP (current_buffer->selective_display)
9412 && XINT (current_buffer->selective_display) > 0
9413 && (BEG_UNCHANGED < start || GPT <= start))
9414 unchanged_p = 0;
9416 /* If there are overlays at the start or end of the line, these
9417 may have overlay strings with newlines in them. A change at
9418 START, for instance, may actually concern the display of such
9419 overlay strings as well, and they are displayed on different
9420 lines. So, quickly rule out this case. (For the future, it
9421 might be desirable to implement something more telling than
9422 just BEG/END_UNCHANGED.) */
9423 if (unchanged_p)
9425 if (BEG + BEG_UNCHANGED == start
9426 && overlay_touches_p (start))
9427 unchanged_p = 0;
9428 if (END_UNCHANGED == end
9429 && overlay_touches_p (Z - end))
9430 unchanged_p = 0;
9434 return unchanged_p;
9438 /* Do a frame update, taking possible shortcuts into account. This is
9439 the main external entry point for redisplay.
9441 If the last redisplay displayed an echo area message and that message
9442 is no longer requested, we clear the echo area or bring back the
9443 mini-buffer if that is in use. */
9445 void
9446 redisplay ()
9448 redisplay_internal (0);
9452 /* Return 1 if point moved out of or into a composition. Otherwise
9453 return 0. PREV_BUF and PREV_PT are the last point buffer and
9454 position. BUF and PT are the current point buffer and position. */
9457 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9458 struct buffer *prev_buf, *buf;
9459 int prev_pt, pt;
9461 int start, end;
9462 Lisp_Object prop;
9463 Lisp_Object buffer;
9465 XSETBUFFER (buffer, buf);
9466 /* Check a composition at the last point if point moved within the
9467 same buffer. */
9468 if (prev_buf == buf)
9470 if (prev_pt == pt)
9471 /* Point didn't move. */
9472 return 0;
9474 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9475 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9476 && COMPOSITION_VALID_P (start, end, prop)
9477 && start < prev_pt && end > prev_pt)
9478 /* The last point was within the composition. Return 1 iff
9479 point moved out of the composition. */
9480 return (pt <= start || pt >= end);
9483 /* Check a composition at the current point. */
9484 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9485 && find_composition (pt, -1, &start, &end, &prop, buffer)
9486 && COMPOSITION_VALID_P (start, end, prop)
9487 && start < pt && end > pt);
9491 /* Reconsider the setting of B->clip_changed which is displayed
9492 in window W. */
9494 static INLINE void
9495 reconsider_clip_changes (w, b)
9496 struct window *w;
9497 struct buffer *b;
9499 if (b->clip_changed
9500 && !NILP (w->window_end_valid)
9501 && w->current_matrix->buffer == b
9502 && w->current_matrix->zv == BUF_ZV (b)
9503 && w->current_matrix->begv == BUF_BEGV (b))
9504 b->clip_changed = 0;
9506 /* If display wasn't paused, and W is not a tool bar window, see if
9507 point has been moved into or out of a composition. In that case,
9508 we set b->clip_changed to 1 to force updating the screen. If
9509 b->clip_changed has already been set to 1, we can skip this
9510 check. */
9511 if (!b->clip_changed
9512 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9514 int pt;
9516 if (w == XWINDOW (selected_window))
9517 pt = BUF_PT (current_buffer);
9518 else
9519 pt = marker_position (w->pointm);
9521 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9522 || pt != XINT (w->last_point))
9523 && check_point_in_composition (w->current_matrix->buffer,
9524 XINT (w->last_point),
9525 XBUFFER (w->buffer), pt))
9526 b->clip_changed = 1;
9530 #define STOP_POLLING \
9531 do { if (! polling_stopped_here) stop_polling (); \
9532 polling_stopped_here = 1; } while (0)
9534 #define RESUME_POLLING \
9535 do { if (polling_stopped_here) start_polling (); \
9536 polling_stopped_here = 0; } while (0)
9539 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9540 response to any user action; therefore, we should preserve the echo
9541 area. (Actually, our caller does that job.) Perhaps in the future
9542 avoid recentering windows if it is not necessary; currently that
9543 causes some problems. */
9545 static void
9546 redisplay_internal (preserve_echo_area)
9547 int preserve_echo_area;
9549 struct window *w = XWINDOW (selected_window);
9550 struct frame *f = XFRAME (w->frame);
9551 int pause;
9552 int must_finish = 0;
9553 struct text_pos tlbufpos, tlendpos;
9554 int number_of_visible_frames;
9555 int count;
9556 struct frame *sf = SELECTED_FRAME ();
9557 int polling_stopped_here = 0;
9559 /* Non-zero means redisplay has to consider all windows on all
9560 frames. Zero means, only selected_window is considered. */
9561 int consider_all_windows_p;
9563 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9565 /* No redisplay if running in batch mode or frame is not yet fully
9566 initialized, or redisplay is explicitly turned off by setting
9567 Vinhibit_redisplay. */
9568 if (noninteractive
9569 || !NILP (Vinhibit_redisplay)
9570 || !f->glyphs_initialized_p)
9571 return;
9573 /* The flag redisplay_performed_directly_p is set by
9574 direct_output_for_insert when it already did the whole screen
9575 update necessary. */
9576 if (redisplay_performed_directly_p)
9578 redisplay_performed_directly_p = 0;
9579 if (!hscroll_windows (selected_window))
9580 return;
9583 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9584 if (popup_activated ())
9585 return;
9586 #endif
9588 /* I don't think this happens but let's be paranoid. */
9589 if (redisplaying_p)
9590 return;
9592 /* Record a function that resets redisplaying_p to its old value
9593 when we leave this function. */
9594 count = SPECPDL_INDEX ();
9595 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9596 ++redisplaying_p;
9597 specbind (Qinhibit_free_realized_faces, Qnil);
9599 retry:
9600 pause = 0;
9601 reconsider_clip_changes (w, current_buffer);
9603 /* If new fonts have been loaded that make a glyph matrix adjustment
9604 necessary, do it. */
9605 if (fonts_changed_p)
9607 adjust_glyphs (NULL);
9608 ++windows_or_buffers_changed;
9609 fonts_changed_p = 0;
9612 /* If face_change_count is non-zero, init_iterator will free all
9613 realized faces, which includes the faces referenced from current
9614 matrices. So, we can't reuse current matrices in this case. */
9615 if (face_change_count)
9616 ++windows_or_buffers_changed;
9618 if (! FRAME_WINDOW_P (sf)
9619 && previous_terminal_frame != sf)
9621 /* Since frames on an ASCII terminal share the same display
9622 area, displaying a different frame means redisplay the whole
9623 thing. */
9624 windows_or_buffers_changed++;
9625 SET_FRAME_GARBAGED (sf);
9626 XSETFRAME (Vterminal_frame, sf);
9628 previous_terminal_frame = sf;
9630 /* Set the visible flags for all frames. Do this before checking
9631 for resized or garbaged frames; they want to know if their frames
9632 are visible. See the comment in frame.h for
9633 FRAME_SAMPLE_VISIBILITY. */
9635 Lisp_Object tail, frame;
9637 number_of_visible_frames = 0;
9639 FOR_EACH_FRAME (tail, frame)
9641 struct frame *f = XFRAME (frame);
9643 FRAME_SAMPLE_VISIBILITY (f);
9644 if (FRAME_VISIBLE_P (f))
9645 ++number_of_visible_frames;
9646 clear_desired_matrices (f);
9650 /* Notice any pending interrupt request to change frame size. */
9651 do_pending_window_change (1);
9653 /* Clear frames marked as garbaged. */
9654 if (frame_garbaged)
9655 clear_garbaged_frames ();
9657 /* Build menubar and tool-bar items. */
9658 prepare_menu_bars ();
9660 if (windows_or_buffers_changed)
9661 update_mode_lines++;
9663 /* Detect case that we need to write or remove a star in the mode line. */
9664 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9666 w->update_mode_line = Qt;
9667 if (buffer_shared > 1)
9668 update_mode_lines++;
9671 /* If %c is in the mode line, update it if needed. */
9672 if (!NILP (w->column_number_displayed)
9673 /* This alternative quickly identifies a common case
9674 where no change is needed. */
9675 && !(PT == XFASTINT (w->last_point)
9676 && XFASTINT (w->last_modified) >= MODIFF
9677 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9678 && (XFASTINT (w->column_number_displayed)
9679 != (int) current_column ())) /* iftc */
9680 w->update_mode_line = Qt;
9682 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9684 /* The variable buffer_shared is set in redisplay_window and
9685 indicates that we redisplay a buffer in different windows. See
9686 there. */
9687 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9688 || cursor_type_changed);
9690 /* If specs for an arrow have changed, do thorough redisplay
9691 to ensure we remove any arrow that should no longer exist. */
9692 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9693 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9694 consider_all_windows_p = windows_or_buffers_changed = 1;
9696 /* Normally the message* functions will have already displayed and
9697 updated the echo area, but the frame may have been trashed, or
9698 the update may have been preempted, so display the echo area
9699 again here. Checking message_cleared_p captures the case that
9700 the echo area should be cleared. */
9701 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9702 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9703 || (message_cleared_p
9704 && minibuf_level == 0
9705 /* If the mini-window is currently selected, this means the
9706 echo-area doesn't show through. */
9707 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9709 int window_height_changed_p = echo_area_display (0);
9710 must_finish = 1;
9712 /* If we don't display the current message, don't clear the
9713 message_cleared_p flag, because, if we did, we wouldn't clear
9714 the echo area in the next redisplay which doesn't preserve
9715 the echo area. */
9716 if (!display_last_displayed_message_p)
9717 message_cleared_p = 0;
9719 if (fonts_changed_p)
9720 goto retry;
9721 else if (window_height_changed_p)
9723 consider_all_windows_p = 1;
9724 ++update_mode_lines;
9725 ++windows_or_buffers_changed;
9727 /* If window configuration was changed, frames may have been
9728 marked garbaged. Clear them or we will experience
9729 surprises wrt scrolling. */
9730 if (frame_garbaged)
9731 clear_garbaged_frames ();
9734 else if (EQ (selected_window, minibuf_window)
9735 && (current_buffer->clip_changed
9736 || XFASTINT (w->last_modified) < MODIFF
9737 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9738 && resize_mini_window (w, 0))
9740 /* Resized active mini-window to fit the size of what it is
9741 showing if its contents might have changed. */
9742 must_finish = 1;
9743 consider_all_windows_p = 1;
9744 ++windows_or_buffers_changed;
9745 ++update_mode_lines;
9747 /* If window configuration was changed, frames may have been
9748 marked garbaged. Clear them or we will experience
9749 surprises wrt scrolling. */
9750 if (frame_garbaged)
9751 clear_garbaged_frames ();
9755 /* If showing the region, and mark has changed, we must redisplay
9756 the whole window. The assignment to this_line_start_pos prevents
9757 the optimization directly below this if-statement. */
9758 if (((!NILP (Vtransient_mark_mode)
9759 && !NILP (XBUFFER (w->buffer)->mark_active))
9760 != !NILP (w->region_showing))
9761 || (!NILP (w->region_showing)
9762 && !EQ (w->region_showing,
9763 Fmarker_position (XBUFFER (w->buffer)->mark))))
9764 CHARPOS (this_line_start_pos) = 0;
9766 /* Optimize the case that only the line containing the cursor in the
9767 selected window has changed. Variables starting with this_ are
9768 set in display_line and record information about the line
9769 containing the cursor. */
9770 tlbufpos = this_line_start_pos;
9771 tlendpos = this_line_end_pos;
9772 if (!consider_all_windows_p
9773 && CHARPOS (tlbufpos) > 0
9774 && NILP (w->update_mode_line)
9775 && !current_buffer->clip_changed
9776 && !current_buffer->prevent_redisplay_optimizations_p
9777 && FRAME_VISIBLE_P (XFRAME (w->frame))
9778 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9779 /* Make sure recorded data applies to current buffer, etc. */
9780 && this_line_buffer == current_buffer
9781 && current_buffer == XBUFFER (w->buffer)
9782 && NILP (w->force_start)
9783 && NILP (w->optional_new_start)
9784 /* Point must be on the line that we have info recorded about. */
9785 && PT >= CHARPOS (tlbufpos)
9786 && PT <= Z - CHARPOS (tlendpos)
9787 /* All text outside that line, including its final newline,
9788 must be unchanged */
9789 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9790 CHARPOS (tlendpos)))
9792 if (CHARPOS (tlbufpos) > BEGV
9793 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9794 && (CHARPOS (tlbufpos) == ZV
9795 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9796 /* Former continuation line has disappeared by becoming empty */
9797 goto cancel;
9798 else if (XFASTINT (w->last_modified) < MODIFF
9799 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9800 || MINI_WINDOW_P (w))
9802 /* We have to handle the case of continuation around a
9803 wide-column character (See the comment in indent.c around
9804 line 885).
9806 For instance, in the following case:
9808 -------- Insert --------
9809 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9810 J_I_ ==> J_I_ `^^' are cursors.
9811 ^^ ^^
9812 -------- --------
9814 As we have to redraw the line above, we should goto cancel. */
9816 struct it it;
9817 int line_height_before = this_line_pixel_height;
9819 /* Note that start_display will handle the case that the
9820 line starting at tlbufpos is a continuation lines. */
9821 start_display (&it, w, tlbufpos);
9823 /* Implementation note: It this still necessary? */
9824 if (it.current_x != this_line_start_x)
9825 goto cancel;
9827 TRACE ((stderr, "trying display optimization 1\n"));
9828 w->cursor.vpos = -1;
9829 overlay_arrow_seen = 0;
9830 it.vpos = this_line_vpos;
9831 it.current_y = this_line_y;
9832 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9833 display_line (&it);
9835 /* If line contains point, is not continued,
9836 and ends at same distance from eob as before, we win */
9837 if (w->cursor.vpos >= 0
9838 /* Line is not continued, otherwise this_line_start_pos
9839 would have been set to 0 in display_line. */
9840 && CHARPOS (this_line_start_pos)
9841 /* Line ends as before. */
9842 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9843 /* Line has same height as before. Otherwise other lines
9844 would have to be shifted up or down. */
9845 && this_line_pixel_height == line_height_before)
9847 /* If this is not the window's last line, we must adjust
9848 the charstarts of the lines below. */
9849 if (it.current_y < it.last_visible_y)
9851 struct glyph_row *row
9852 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9853 int delta, delta_bytes;
9855 if (Z - CHARPOS (tlendpos) == ZV)
9857 /* This line ends at end of (accessible part of)
9858 buffer. There is no newline to count. */
9859 delta = (Z
9860 - CHARPOS (tlendpos)
9861 - MATRIX_ROW_START_CHARPOS (row));
9862 delta_bytes = (Z_BYTE
9863 - BYTEPOS (tlendpos)
9864 - MATRIX_ROW_START_BYTEPOS (row));
9866 else
9868 /* This line ends in a newline. Must take
9869 account of the newline and the rest of the
9870 text that follows. */
9871 delta = (Z
9872 - CHARPOS (tlendpos)
9873 - MATRIX_ROW_START_CHARPOS (row));
9874 delta_bytes = (Z_BYTE
9875 - BYTEPOS (tlendpos)
9876 - MATRIX_ROW_START_BYTEPOS (row));
9879 increment_matrix_positions (w->current_matrix,
9880 this_line_vpos + 1,
9881 w->current_matrix->nrows,
9882 delta, delta_bytes);
9885 /* If this row displays text now but previously didn't,
9886 or vice versa, w->window_end_vpos may have to be
9887 adjusted. */
9888 if ((it.glyph_row - 1)->displays_text_p)
9890 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9891 XSETINT (w->window_end_vpos, this_line_vpos);
9893 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9894 && this_line_vpos > 0)
9895 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9896 w->window_end_valid = Qnil;
9898 /* Update hint: No need to try to scroll in update_window. */
9899 w->desired_matrix->no_scrolling_p = 1;
9901 #if GLYPH_DEBUG
9902 *w->desired_matrix->method = 0;
9903 debug_method_add (w, "optimization 1");
9904 #endif
9905 goto update;
9907 else
9908 goto cancel;
9910 else if (/* Cursor position hasn't changed. */
9911 PT == XFASTINT (w->last_point)
9912 /* Make sure the cursor was last displayed
9913 in this window. Otherwise we have to reposition it. */
9914 && 0 <= w->cursor.vpos
9915 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
9917 if (!must_finish)
9919 do_pending_window_change (1);
9921 /* We used to always goto end_of_redisplay here, but this
9922 isn't enough if we have a blinking cursor. */
9923 if (w->cursor_off_p == w->last_cursor_off_p)
9924 goto end_of_redisplay;
9926 goto update;
9928 /* If highlighting the region, or if the cursor is in the echo area,
9929 then we can't just move the cursor. */
9930 else if (! (!NILP (Vtransient_mark_mode)
9931 && !NILP (current_buffer->mark_active))
9932 && (EQ (selected_window, current_buffer->last_selected_window)
9933 || highlight_nonselected_windows)
9934 && NILP (w->region_showing)
9935 && NILP (Vshow_trailing_whitespace)
9936 && !cursor_in_echo_area)
9938 struct it it;
9939 struct glyph_row *row;
9941 /* Skip from tlbufpos to PT and see where it is. Note that
9942 PT may be in invisible text. If so, we will end at the
9943 next visible position. */
9944 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9945 NULL, DEFAULT_FACE_ID);
9946 it.current_x = this_line_start_x;
9947 it.current_y = this_line_y;
9948 it.vpos = this_line_vpos;
9950 /* The call to move_it_to stops in front of PT, but
9951 moves over before-strings. */
9952 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9954 if (it.vpos == this_line_vpos
9955 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9956 row->enabled_p))
9958 xassert (this_line_vpos == it.vpos);
9959 xassert (this_line_y == it.current_y);
9960 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9961 #if GLYPH_DEBUG
9962 *w->desired_matrix->method = 0;
9963 debug_method_add (w, "optimization 3");
9964 #endif
9965 goto update;
9967 else
9968 goto cancel;
9971 cancel:
9972 /* Text changed drastically or point moved off of line. */
9973 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9976 CHARPOS (this_line_start_pos) = 0;
9977 consider_all_windows_p |= buffer_shared > 1;
9978 ++clear_face_cache_count;
9981 /* Build desired matrices, and update the display. If
9982 consider_all_windows_p is non-zero, do it for all windows on all
9983 frames. Otherwise do it for selected_window, only. */
9985 if (consider_all_windows_p)
9987 Lisp_Object tail, frame;
9988 int i, n = 0, size = 50;
9989 struct frame **updated
9990 = (struct frame **) alloca (size * sizeof *updated);
9992 /* Clear the face cache eventually. */
9993 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9995 clear_face_cache (0);
9996 clear_face_cache_count = 0;
9999 /* Recompute # windows showing selected buffer. This will be
10000 incremented each time such a window is displayed. */
10001 buffer_shared = 0;
10003 FOR_EACH_FRAME (tail, frame)
10005 struct frame *f = XFRAME (frame);
10007 if (FRAME_WINDOW_P (f) || f == sf)
10009 #ifdef HAVE_WINDOW_SYSTEM
10010 if (clear_face_cache_count % 50 == 0
10011 && FRAME_WINDOW_P (f))
10012 clear_image_cache (f, 0);
10013 #endif /* HAVE_WINDOW_SYSTEM */
10015 /* Mark all the scroll bars to be removed; we'll redeem
10016 the ones we want when we redisplay their windows. */
10017 if (condemn_scroll_bars_hook)
10018 condemn_scroll_bars_hook (f);
10020 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10021 redisplay_windows (FRAME_ROOT_WINDOW (f));
10023 /* Any scroll bars which redisplay_windows should have
10024 nuked should now go away. */
10025 if (judge_scroll_bars_hook)
10026 judge_scroll_bars_hook (f);
10028 /* If fonts changed, display again. */
10029 /* ??? rms: I suspect it is a mistake to jump all the way
10030 back to retry here. It should just retry this frame. */
10031 if (fonts_changed_p)
10032 goto retry;
10034 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10036 /* See if we have to hscroll. */
10037 if (hscroll_windows (f->root_window))
10038 goto retry;
10040 /* Prevent various kinds of signals during display
10041 update. stdio is not robust about handling
10042 signals, which can cause an apparent I/O
10043 error. */
10044 if (interrupt_input)
10045 unrequest_sigio ();
10046 STOP_POLLING;
10048 /* Update the display. */
10049 set_window_update_flags (XWINDOW (f->root_window), 1);
10050 pause |= update_frame (f, 0, 0);
10051 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10052 if (pause)
10053 break;
10054 #endif
10056 if (n == size)
10058 int nbytes = size * sizeof *updated;
10059 struct frame **p = (struct frame **) alloca (2 * nbytes);
10060 bcopy (updated, p, nbytes);
10061 size *= 2;
10064 updated[n++] = f;
10069 if (!pause)
10071 /* Do the mark_window_display_accurate after all windows have
10072 been redisplayed because this call resets flags in buffers
10073 which are needed for proper redisplay. */
10074 for (i = 0; i < n; ++i)
10076 struct frame *f = updated[i];
10077 mark_window_display_accurate (f->root_window, 1);
10078 if (frame_up_to_date_hook)
10079 frame_up_to_date_hook (f);
10083 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10085 Lisp_Object mini_window;
10086 struct frame *mini_frame;
10088 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10089 /* Use list_of_error, not Qerror, so that
10090 we catch only errors and don't run the debugger. */
10091 internal_condition_case_1 (redisplay_window_1, selected_window,
10092 list_of_error,
10093 redisplay_window_error);
10095 /* Compare desired and current matrices, perform output. */
10097 update:
10098 /* If fonts changed, display again. */
10099 if (fonts_changed_p)
10100 goto retry;
10102 /* Prevent various kinds of signals during display update.
10103 stdio is not robust about handling signals,
10104 which can cause an apparent I/O error. */
10105 if (interrupt_input)
10106 unrequest_sigio ();
10107 STOP_POLLING;
10109 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10111 if (hscroll_windows (selected_window))
10112 goto retry;
10114 XWINDOW (selected_window)->must_be_updated_p = 1;
10115 pause = update_frame (sf, 0, 0);
10118 /* We may have called echo_area_display at the top of this
10119 function. If the echo area is on another frame, that may
10120 have put text on a frame other than the selected one, so the
10121 above call to update_frame would not have caught it. Catch
10122 it here. */
10123 mini_window = FRAME_MINIBUF_WINDOW (sf);
10124 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10126 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10128 XWINDOW (mini_window)->must_be_updated_p = 1;
10129 pause |= update_frame (mini_frame, 0, 0);
10130 if (!pause && hscroll_windows (mini_window))
10131 goto retry;
10135 /* If display was paused because of pending input, make sure we do a
10136 thorough update the next time. */
10137 if (pause)
10139 /* Prevent the optimization at the beginning of
10140 redisplay_internal that tries a single-line update of the
10141 line containing the cursor in the selected window. */
10142 CHARPOS (this_line_start_pos) = 0;
10144 /* Let the overlay arrow be updated the next time. */
10145 if (!NILP (last_arrow_position))
10147 last_arrow_position = Qt;
10148 last_arrow_string = Qt;
10151 /* If we pause after scrolling, some rows in the current
10152 matrices of some windows are not valid. */
10153 if (!WINDOW_FULL_WIDTH_P (w)
10154 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10155 update_mode_lines = 1;
10157 else
10159 if (!consider_all_windows_p)
10161 /* This has already been done above if
10162 consider_all_windows_p is set. */
10163 mark_window_display_accurate_1 (w, 1);
10165 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10166 last_arrow_string = Voverlay_arrow_string;
10168 if (frame_up_to_date_hook != 0)
10169 frame_up_to_date_hook (sf);
10172 update_mode_lines = 0;
10173 windows_or_buffers_changed = 0;
10174 cursor_type_changed = 0;
10177 /* Start SIGIO interrupts coming again. Having them off during the
10178 code above makes it less likely one will discard output, but not
10179 impossible, since there might be stuff in the system buffer here.
10180 But it is much hairier to try to do anything about that. */
10181 if (interrupt_input)
10182 request_sigio ();
10183 RESUME_POLLING;
10185 /* If a frame has become visible which was not before, redisplay
10186 again, so that we display it. Expose events for such a frame
10187 (which it gets when becoming visible) don't call the parts of
10188 redisplay constructing glyphs, so simply exposing a frame won't
10189 display anything in this case. So, we have to display these
10190 frames here explicitly. */
10191 if (!pause)
10193 Lisp_Object tail, frame;
10194 int new_count = 0;
10196 FOR_EACH_FRAME (tail, frame)
10198 int this_is_visible = 0;
10200 if (XFRAME (frame)->visible)
10201 this_is_visible = 1;
10202 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10203 if (XFRAME (frame)->visible)
10204 this_is_visible = 1;
10206 if (this_is_visible)
10207 new_count++;
10210 if (new_count != number_of_visible_frames)
10211 windows_or_buffers_changed++;
10214 /* Change frame size now if a change is pending. */
10215 do_pending_window_change (1);
10217 /* If we just did a pending size change, or have additional
10218 visible frames, redisplay again. */
10219 if (windows_or_buffers_changed && !pause)
10220 goto retry;
10222 end_of_redisplay:
10223 unbind_to (count, Qnil);
10224 RESUME_POLLING;
10228 /* Redisplay, but leave alone any recent echo area message unless
10229 another message has been requested in its place.
10231 This is useful in situations where you need to redisplay but no
10232 user action has occurred, making it inappropriate for the message
10233 area to be cleared. See tracking_off and
10234 wait_reading_process_input for examples of these situations.
10236 FROM_WHERE is an integer saying from where this function was
10237 called. This is useful for debugging. */
10239 void
10240 redisplay_preserve_echo_area (from_where)
10241 int from_where;
10243 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10245 if (!NILP (echo_area_buffer[1]))
10247 /* We have a previously displayed message, but no current
10248 message. Redisplay the previous message. */
10249 display_last_displayed_message_p = 1;
10250 redisplay_internal (1);
10251 display_last_displayed_message_p = 0;
10253 else
10254 redisplay_internal (1);
10258 /* Function registered with record_unwind_protect in
10259 redisplay_internal. Reset redisplaying_p to the value it had
10260 before redisplay_internal was called, and clear
10261 prevent_freeing_realized_faces_p. */
10263 static Lisp_Object
10264 unwind_redisplay (old_redisplaying_p)
10265 Lisp_Object old_redisplaying_p;
10267 redisplaying_p = XFASTINT (old_redisplaying_p);
10268 return Qnil;
10272 /* Mark the display of window W as accurate or inaccurate. If
10273 ACCURATE_P is non-zero mark display of W as accurate. If
10274 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10275 redisplay_internal is called. */
10277 static void
10278 mark_window_display_accurate_1 (w, accurate_p)
10279 struct window *w;
10280 int accurate_p;
10282 if (BUFFERP (w->buffer))
10284 struct buffer *b = XBUFFER (w->buffer);
10286 w->last_modified
10287 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10288 w->last_overlay_modified
10289 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10290 w->last_had_star
10291 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10293 if (accurate_p)
10295 b->clip_changed = 0;
10296 b->prevent_redisplay_optimizations_p = 0;
10298 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10299 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10300 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10301 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10303 w->current_matrix->buffer = b;
10304 w->current_matrix->begv = BUF_BEGV (b);
10305 w->current_matrix->zv = BUF_ZV (b);
10307 w->last_cursor = w->cursor;
10308 w->last_cursor_off_p = w->cursor_off_p;
10310 if (w == XWINDOW (selected_window))
10311 w->last_point = make_number (BUF_PT (b));
10312 else
10313 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10317 if (accurate_p)
10319 w->window_end_valid = w->buffer;
10320 #if 0 /* This is incorrect with variable-height lines. */
10321 xassert (XINT (w->window_end_vpos)
10322 < (WINDOW_TOTAL_LINES (w)
10323 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10324 #endif
10325 w->update_mode_line = Qnil;
10330 /* Mark the display of windows in the window tree rooted at WINDOW as
10331 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10332 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10333 be redisplayed the next time redisplay_internal is called. */
10335 void
10336 mark_window_display_accurate (window, accurate_p)
10337 Lisp_Object window;
10338 int accurate_p;
10340 struct window *w;
10342 for (; !NILP (window); window = w->next)
10344 w = XWINDOW (window);
10345 mark_window_display_accurate_1 (w, accurate_p);
10347 if (!NILP (w->vchild))
10348 mark_window_display_accurate (w->vchild, accurate_p);
10349 if (!NILP (w->hchild))
10350 mark_window_display_accurate (w->hchild, accurate_p);
10353 if (accurate_p)
10355 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10356 last_arrow_string = Voverlay_arrow_string;
10358 else
10360 /* Force a thorough redisplay the next time by setting
10361 last_arrow_position and last_arrow_string to t, which is
10362 unequal to any useful value of Voverlay_arrow_... */
10363 last_arrow_position = Qt;
10364 last_arrow_string = Qt;
10369 /* Return value in display table DP (Lisp_Char_Table *) for character
10370 C. Since a display table doesn't have any parent, we don't have to
10371 follow parent. Do not call this function directly but use the
10372 macro DISP_CHAR_VECTOR. */
10374 Lisp_Object
10375 disp_char_vector (dp, c)
10376 struct Lisp_Char_Table *dp;
10377 int c;
10379 int code[4], i;
10380 Lisp_Object val;
10382 if (SINGLE_BYTE_CHAR_P (c))
10383 return (dp->contents[c]);
10385 SPLIT_CHAR (c, code[0], code[1], code[2]);
10386 if (code[1] < 32)
10387 code[1] = -1;
10388 else if (code[2] < 32)
10389 code[2] = -1;
10391 /* Here, the possible range of code[0] (== charset ID) is
10392 128..max_charset. Since the top level char table contains data
10393 for multibyte characters after 256th element, we must increment
10394 code[0] by 128 to get a correct index. */
10395 code[0] += 128;
10396 code[3] = -1; /* anchor */
10398 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10400 val = dp->contents[code[i]];
10401 if (!SUB_CHAR_TABLE_P (val))
10402 return (NILP (val) ? dp->defalt : val);
10405 /* Here, val is a sub char table. We return the default value of
10406 it. */
10407 return (dp->defalt);
10412 /***********************************************************************
10413 Window Redisplay
10414 ***********************************************************************/
10416 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10418 static void
10419 redisplay_windows (window)
10420 Lisp_Object window;
10422 while (!NILP (window))
10424 struct window *w = XWINDOW (window);
10426 if (!NILP (w->hchild))
10427 redisplay_windows (w->hchild);
10428 else if (!NILP (w->vchild))
10429 redisplay_windows (w->vchild);
10430 else
10432 displayed_buffer = XBUFFER (w->buffer);
10433 /* Use list_of_error, not Qerror, so that
10434 we catch only errors and don't run the debugger. */
10435 internal_condition_case_1 (redisplay_window_0, window,
10436 list_of_error,
10437 redisplay_window_error);
10440 window = w->next;
10444 static Lisp_Object
10445 redisplay_window_error ()
10447 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10448 return Qnil;
10451 static Lisp_Object
10452 redisplay_window_0 (window)
10453 Lisp_Object window;
10455 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10456 redisplay_window (window, 0);
10457 return Qnil;
10460 static Lisp_Object
10461 redisplay_window_1 (window)
10462 Lisp_Object window;
10464 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10465 redisplay_window (window, 1);
10466 return Qnil;
10470 /* Increment GLYPH until it reaches END or CONDITION fails while
10471 adding (GLYPH)->pixel_width to X. */
10473 #define SKIP_GLYPHS(glyph, end, x, condition) \
10474 do \
10476 (x) += (glyph)->pixel_width; \
10477 ++(glyph); \
10479 while ((glyph) < (end) && (condition))
10482 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10483 DELTA is the number of bytes by which positions recorded in ROW
10484 differ from current buffer positions. */
10486 void
10487 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10488 struct window *w;
10489 struct glyph_row *row;
10490 struct glyph_matrix *matrix;
10491 int delta, delta_bytes, dy, dvpos;
10493 struct glyph *glyph = row->glyphs[TEXT_AREA];
10494 struct glyph *end = glyph + row->used[TEXT_AREA];
10495 /* The first glyph that starts a sequence of glyphs from string. */
10496 struct glyph *string_start;
10497 /* The X coordinate of string_start. */
10498 int string_start_x;
10499 /* The last known character position. */
10500 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10501 /* The last known character position before string_start. */
10502 int string_before_pos;
10503 int x = row->x;
10504 int pt_old = PT - delta;
10506 /* Skip over glyphs not having an object at the start of the row.
10507 These are special glyphs like truncation marks on terminal
10508 frames. */
10509 if (row->displays_text_p)
10510 while (glyph < end
10511 && INTEGERP (glyph->object)
10512 && glyph->charpos < 0)
10514 x += glyph->pixel_width;
10515 ++glyph;
10518 string_start = NULL;
10519 while (glyph < end
10520 && !INTEGERP (glyph->object)
10521 && (!BUFFERP (glyph->object)
10522 || (last_pos = glyph->charpos) < pt_old))
10524 if (! STRINGP (glyph->object))
10526 string_start = NULL;
10527 x += glyph->pixel_width;
10528 ++glyph;
10530 else
10532 string_before_pos = last_pos;
10533 string_start = glyph;
10534 string_start_x = x;
10535 /* Skip all glyphs from string. */
10536 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10540 if (string_start
10541 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10543 /* We may have skipped over point because the previous glyphs
10544 are from string. As there's no easy way to know the
10545 character position of the current glyph, find the correct
10546 glyph on point by scanning from string_start again. */
10547 Lisp_Object limit;
10548 Lisp_Object string;
10549 int pos;
10551 limit = make_number (pt_old + 1);
10552 end = glyph;
10553 glyph = string_start;
10554 x = string_start_x;
10555 string = glyph->object;
10556 pos = string_buffer_position (w, string, string_before_pos);
10557 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10558 because we always put cursor after overlay strings. */
10559 while (pos == 0 && glyph < end)
10561 string = glyph->object;
10562 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10563 if (glyph < end)
10564 pos = string_buffer_position (w, glyph->object, string_before_pos);
10567 while (glyph < end)
10569 pos = XINT (Fnext_single_char_property_change
10570 (make_number (pos), Qdisplay, Qnil, limit));
10571 if (pos > pt_old)
10572 break;
10573 /* Skip glyphs from the same string. */
10574 string = glyph->object;
10575 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10576 /* Skip glyphs from an overlay. */
10577 while (glyph < end
10578 && ! string_buffer_position (w, glyph->object, pos))
10580 string = glyph->object;
10581 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10586 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10587 w->cursor.x = x;
10588 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10589 w->cursor.y = row->y + dy;
10591 if (w == XWINDOW (selected_window))
10593 if (!row->continued_p
10594 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10595 && row->x == 0)
10597 this_line_buffer = XBUFFER (w->buffer);
10599 CHARPOS (this_line_start_pos)
10600 = MATRIX_ROW_START_CHARPOS (row) + delta;
10601 BYTEPOS (this_line_start_pos)
10602 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10604 CHARPOS (this_line_end_pos)
10605 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10606 BYTEPOS (this_line_end_pos)
10607 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10609 this_line_y = w->cursor.y;
10610 this_line_pixel_height = row->height;
10611 this_line_vpos = w->cursor.vpos;
10612 this_line_start_x = row->x;
10614 else
10615 CHARPOS (this_line_start_pos) = 0;
10620 /* Run window scroll functions, if any, for WINDOW with new window
10621 start STARTP. Sets the window start of WINDOW to that position.
10623 We assume that the window's buffer is really current. */
10625 static INLINE struct text_pos
10626 run_window_scroll_functions (window, startp)
10627 Lisp_Object window;
10628 struct text_pos startp;
10630 struct window *w = XWINDOW (window);
10631 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10633 if (current_buffer != XBUFFER (w->buffer))
10634 abort ();
10636 if (!NILP (Vwindow_scroll_functions))
10638 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10639 make_number (CHARPOS (startp)));
10640 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10641 /* In case the hook functions switch buffers. */
10642 if (current_buffer != XBUFFER (w->buffer))
10643 set_buffer_internal_1 (XBUFFER (w->buffer));
10646 return startp;
10650 /* Make sure the line containing the cursor is fully visible.
10651 A value of 1 means there is nothing to be done.
10652 (Either the line is fully visible, or it cannot be made so,
10653 or we cannot tell.)
10654 A value of 0 means the caller should do scrolling
10655 as if point had gone off the screen. */
10657 static int
10658 make_cursor_line_fully_visible (w)
10659 struct window *w;
10661 struct glyph_matrix *matrix;
10662 struct glyph_row *row;
10663 int window_height;
10665 /* It's not always possible to find the cursor, e.g, when a window
10666 is full of overlay strings. Don't do anything in that case. */
10667 if (w->cursor.vpos < 0)
10668 return 1;
10670 matrix = w->desired_matrix;
10671 row = MATRIX_ROW (matrix, w->cursor.vpos);
10673 /* If the cursor row is not partially visible, there's nothing to do. */
10674 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10675 return 1;
10677 /* If the row the cursor is in is taller than the window's height,
10678 it's not clear what to do, so do nothing. */
10679 window_height = window_box_height (w);
10680 if (row->height >= window_height)
10681 return 1;
10683 return 0;
10685 #if 0
10686 /* This code used to try to scroll the window just enough to make
10687 the line visible. It returned 0 to say that the caller should
10688 allocate larger glyph matrices. */
10690 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10692 int dy = row->height - row->visible_height;
10693 w->vscroll = 0;
10694 w->cursor.y += dy;
10695 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10697 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10699 int dy = - (row->height - row->visible_height);
10700 w->vscroll = dy;
10701 w->cursor.y += dy;
10702 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10705 /* When we change the cursor y-position of the selected window,
10706 change this_line_y as well so that the display optimization for
10707 the cursor line of the selected window in redisplay_internal uses
10708 the correct y-position. */
10709 if (w == XWINDOW (selected_window))
10710 this_line_y = w->cursor.y;
10712 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10713 redisplay with larger matrices. */
10714 if (matrix->nrows < required_matrix_height (w))
10716 fonts_changed_p = 1;
10717 return 0;
10720 return 1;
10721 #endif /* 0 */
10725 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10726 non-zero means only WINDOW is redisplayed in redisplay_internal.
10727 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10728 in redisplay_window to bring a partially visible line into view in
10729 the case that only the cursor has moved.
10731 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10732 last screen line's vertical height extends past the end of the screen.
10734 Value is
10736 1 if scrolling succeeded
10738 0 if scrolling didn't find point.
10740 -1 if new fonts have been loaded so that we must interrupt
10741 redisplay, adjust glyph matrices, and try again. */
10743 enum
10745 SCROLLING_SUCCESS,
10746 SCROLLING_FAILED,
10747 SCROLLING_NEED_LARGER_MATRICES
10750 static int
10751 try_scrolling (window, just_this_one_p, scroll_conservatively,
10752 scroll_step, temp_scroll_step, last_line_misfit)
10753 Lisp_Object window;
10754 int just_this_one_p;
10755 EMACS_INT scroll_conservatively, scroll_step;
10756 int temp_scroll_step;
10757 int last_line_misfit;
10759 struct window *w = XWINDOW (window);
10760 struct frame *f = XFRAME (w->frame);
10761 struct text_pos scroll_margin_pos;
10762 struct text_pos pos;
10763 struct text_pos startp;
10764 struct it it;
10765 Lisp_Object window_end;
10766 int this_scroll_margin;
10767 int dy = 0;
10768 int scroll_max;
10769 int rc;
10770 int amount_to_scroll = 0;
10771 Lisp_Object aggressive;
10772 int height;
10773 int end_scroll_margin;
10775 #if GLYPH_DEBUG
10776 debug_method_add (w, "try_scrolling");
10777 #endif
10779 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10781 /* Compute scroll margin height in pixels. We scroll when point is
10782 within this distance from the top or bottom of the window. */
10783 if (scroll_margin > 0)
10785 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
10786 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
10788 else
10789 this_scroll_margin = 0;
10791 /* Compute how much we should try to scroll maximally to bring point
10792 into view. */
10793 if (scroll_step || scroll_conservatively || temp_scroll_step)
10794 scroll_max = max (scroll_step,
10795 max (scroll_conservatively, temp_scroll_step));
10796 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10797 || NUMBERP (current_buffer->scroll_up_aggressively))
10798 /* We're trying to scroll because of aggressive scrolling
10799 but no scroll_step is set. Choose an arbitrary one. Maybe
10800 there should be a variable for this. */
10801 scroll_max = 10;
10802 else
10803 scroll_max = 0;
10804 scroll_max *= FRAME_LINE_HEIGHT (f);
10806 /* Decide whether we have to scroll down. Start at the window end
10807 and move this_scroll_margin up to find the position of the scroll
10808 margin. */
10809 window_end = Fwindow_end (window, Qt);
10811 too_near_end:
10813 CHARPOS (scroll_margin_pos) = XINT (window_end);
10814 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10816 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10817 if (end_scroll_margin)
10819 start_display (&it, w, scroll_margin_pos);
10820 move_it_vertically (&it, - end_scroll_margin);
10821 scroll_margin_pos = it.current.pos;
10824 if (PT >= CHARPOS (scroll_margin_pos))
10826 int y0;
10828 /* Point is in the scroll margin at the bottom of the window, or
10829 below. Compute a new window start that makes point visible. */
10831 /* Compute the distance from the scroll margin to PT.
10832 Give up if the distance is greater than scroll_max. */
10833 start_display (&it, w, scroll_margin_pos);
10834 y0 = it.current_y;
10835 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10836 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10838 /* To make point visible, we have to move the window start
10839 down so that the line the cursor is in is visible, which
10840 means we have to add in the height of the cursor line. */
10841 dy = line_bottom_y (&it) - y0;
10843 if (dy > scroll_max)
10844 return SCROLLING_FAILED;
10846 /* Move the window start down. If scrolling conservatively,
10847 move it just enough down to make point visible. If
10848 scroll_step is set, move it down by scroll_step. */
10849 start_display (&it, w, startp);
10851 if (scroll_conservatively)
10852 /* Set AMOUNT_TO_SCROLL to at least one line,
10853 and at most scroll_conservatively lines. */
10854 amount_to_scroll
10855 = min (max (dy, FRAME_LINE_HEIGHT (f)),
10856 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
10857 else if (scroll_step || temp_scroll_step)
10858 amount_to_scroll = scroll_max;
10859 else
10861 aggressive = current_buffer->scroll_up_aggressively;
10862 height = WINDOW_BOX_TEXT_HEIGHT (w);
10863 if (NUMBERP (aggressive))
10864 amount_to_scroll = XFLOATINT (aggressive) * height;
10867 if (amount_to_scroll <= 0)
10868 return SCROLLING_FAILED;
10870 /* If moving by amount_to_scroll leaves STARTP unchanged,
10871 move it down one screen line. */
10873 move_it_vertically (&it, amount_to_scroll);
10874 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10875 move_it_by_lines (&it, 1, 1);
10876 startp = it.current.pos;
10878 else
10880 /* See if point is inside the scroll margin at the top of the
10881 window. */
10882 scroll_margin_pos = startp;
10883 if (this_scroll_margin)
10885 start_display (&it, w, startp);
10886 move_it_vertically (&it, this_scroll_margin);
10887 scroll_margin_pos = it.current.pos;
10890 if (PT < CHARPOS (scroll_margin_pos))
10892 /* Point is in the scroll margin at the top of the window or
10893 above what is displayed in the window. */
10894 int y0;
10896 /* Compute the vertical distance from PT to the scroll
10897 margin position. Give up if distance is greater than
10898 scroll_max. */
10899 SET_TEXT_POS (pos, PT, PT_BYTE);
10900 start_display (&it, w, pos);
10901 y0 = it.current_y;
10902 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10903 it.last_visible_y, -1,
10904 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10905 dy = it.current_y - y0;
10906 if (dy > scroll_max)
10907 return SCROLLING_FAILED;
10909 /* Compute new window start. */
10910 start_display (&it, w, startp);
10912 if (scroll_conservatively)
10913 amount_to_scroll =
10914 max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
10915 else if (scroll_step || temp_scroll_step)
10916 amount_to_scroll = scroll_max;
10917 else
10919 aggressive = current_buffer->scroll_down_aggressively;
10920 height = WINDOW_BOX_TEXT_HEIGHT (w);
10921 if (NUMBERP (aggressive))
10922 amount_to_scroll = XFLOATINT (aggressive) * height;
10925 if (amount_to_scroll <= 0)
10926 return SCROLLING_FAILED;
10928 move_it_vertically (&it, - amount_to_scroll);
10929 startp = it.current.pos;
10933 /* Run window scroll functions. */
10934 startp = run_window_scroll_functions (window, startp);
10936 /* Display the window. Give up if new fonts are loaded, or if point
10937 doesn't appear. */
10938 if (!try_window (window, startp))
10939 rc = SCROLLING_NEED_LARGER_MATRICES;
10940 else if (w->cursor.vpos < 0)
10942 clear_glyph_matrix (w->desired_matrix);
10943 rc = SCROLLING_FAILED;
10945 else
10947 /* Maybe forget recorded base line for line number display. */
10948 if (!just_this_one_p
10949 || current_buffer->clip_changed
10950 || BEG_UNCHANGED < CHARPOS (startp))
10951 w->base_line_number = Qnil;
10953 /* If cursor ends up on a partially visible line,
10954 treat that as being off the bottom of the screen. */
10955 if (! make_cursor_line_fully_visible (w))
10957 clear_glyph_matrix (w->desired_matrix);
10958 last_line_misfit = 1;
10959 goto too_near_end;
10961 rc = SCROLLING_SUCCESS;
10964 return rc;
10968 /* Compute a suitable window start for window W if display of W starts
10969 on a continuation line. Value is non-zero if a new window start
10970 was computed.
10972 The new window start will be computed, based on W's width, starting
10973 from the start of the continued line. It is the start of the
10974 screen line with the minimum distance from the old start W->start. */
10976 static int
10977 compute_window_start_on_continuation_line (w)
10978 struct window *w;
10980 struct text_pos pos, start_pos;
10981 int window_start_changed_p = 0;
10983 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10985 /* If window start is on a continuation line... Window start may be
10986 < BEGV in case there's invisible text at the start of the
10987 buffer (M-x rmail, for example). */
10988 if (CHARPOS (start_pos) > BEGV
10989 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10991 struct it it;
10992 struct glyph_row *row;
10994 /* Handle the case that the window start is out of range. */
10995 if (CHARPOS (start_pos) < BEGV)
10996 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10997 else if (CHARPOS (start_pos) > ZV)
10998 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11000 /* Find the start of the continued line. This should be fast
11001 because scan_buffer is fast (newline cache). */
11002 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11003 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11004 row, DEFAULT_FACE_ID);
11005 reseat_at_previous_visible_line_start (&it);
11007 /* If the line start is "too far" away from the window start,
11008 say it takes too much time to compute a new window start. */
11009 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11010 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11012 int min_distance, distance;
11014 /* Move forward by display lines to find the new window
11015 start. If window width was enlarged, the new start can
11016 be expected to be > the old start. If window width was
11017 decreased, the new window start will be < the old start.
11018 So, we're looking for the display line start with the
11019 minimum distance from the old window start. */
11020 pos = it.current.pos;
11021 min_distance = INFINITY;
11022 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11023 distance < min_distance)
11025 min_distance = distance;
11026 pos = it.current.pos;
11027 move_it_by_lines (&it, 1, 0);
11030 /* Set the window start there. */
11031 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11032 window_start_changed_p = 1;
11036 return window_start_changed_p;
11040 /* Try cursor movement in case text has not changed in window WINDOW,
11041 with window start STARTP. Value is
11043 CURSOR_MOVEMENT_SUCCESS if successful
11045 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11047 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11048 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11049 we want to scroll as if scroll-step were set to 1. See the code.
11051 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11052 which case we have to abort this redisplay, and adjust matrices
11053 first. */
11055 enum
11057 CURSOR_MOVEMENT_SUCCESS,
11058 CURSOR_MOVEMENT_CANNOT_BE_USED,
11059 CURSOR_MOVEMENT_MUST_SCROLL,
11060 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11063 static int
11064 try_cursor_movement (window, startp, scroll_step)
11065 Lisp_Object window;
11066 struct text_pos startp;
11067 int *scroll_step;
11069 struct window *w = XWINDOW (window);
11070 struct frame *f = XFRAME (w->frame);
11071 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11073 #if GLYPH_DEBUG
11074 if (inhibit_try_cursor_movement)
11075 return rc;
11076 #endif
11078 /* Handle case where text has not changed, only point, and it has
11079 not moved off the frame. */
11080 if (/* Point may be in this window. */
11081 PT >= CHARPOS (startp)
11082 /* Selective display hasn't changed. */
11083 && !current_buffer->clip_changed
11084 /* Function force-mode-line-update is used to force a thorough
11085 redisplay. It sets either windows_or_buffers_changed or
11086 update_mode_lines. So don't take a shortcut here for these
11087 cases. */
11088 && !update_mode_lines
11089 && !windows_or_buffers_changed
11090 && !cursor_type_changed
11091 /* Can't use this case if highlighting a region. When a
11092 region exists, cursor movement has to do more than just
11093 set the cursor. */
11094 && !(!NILP (Vtransient_mark_mode)
11095 && !NILP (current_buffer->mark_active))
11096 && NILP (w->region_showing)
11097 && NILP (Vshow_trailing_whitespace)
11098 /* Right after splitting windows, last_point may be nil. */
11099 && INTEGERP (w->last_point)
11100 /* This code is not used for mini-buffer for the sake of the case
11101 of redisplaying to replace an echo area message; since in
11102 that case the mini-buffer contents per se are usually
11103 unchanged. This code is of no real use in the mini-buffer
11104 since the handling of this_line_start_pos, etc., in redisplay
11105 handles the same cases. */
11106 && !EQ (window, minibuf_window)
11107 /* When splitting windows or for new windows, it happens that
11108 redisplay is called with a nil window_end_vpos or one being
11109 larger than the window. This should really be fixed in
11110 window.c. I don't have this on my list, now, so we do
11111 approximately the same as the old redisplay code. --gerd. */
11112 && INTEGERP (w->window_end_vpos)
11113 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11114 && (FRAME_WINDOW_P (f)
11115 || !MARKERP (Voverlay_arrow_position)
11116 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11118 int this_scroll_margin;
11119 struct glyph_row *row = NULL;
11121 #if GLYPH_DEBUG
11122 debug_method_add (w, "cursor movement");
11123 #endif
11125 /* Scroll if point within this distance from the top or bottom
11126 of the window. This is a pixel value. */
11127 this_scroll_margin = max (0, scroll_margin);
11128 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11129 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11131 /* Start with the row the cursor was displayed during the last
11132 not paused redisplay. Give up if that row is not valid. */
11133 if (w->last_cursor.vpos < 0
11134 || w->last_cursor.vpos >= w->current_matrix->nrows)
11135 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11136 else
11138 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11139 if (row->mode_line_p)
11140 ++row;
11141 if (!row->enabled_p)
11142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11145 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11147 int scroll_p = 0;
11148 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11150 if (PT > XFASTINT (w->last_point))
11152 /* Point has moved forward. */
11153 while (MATRIX_ROW_END_CHARPOS (row) < PT
11154 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11156 xassert (row->enabled_p);
11157 ++row;
11160 /* The end position of a row equals the start position
11161 of the next row. If PT is there, we would rather
11162 display it in the next line. */
11163 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11164 && MATRIX_ROW_END_CHARPOS (row) == PT
11165 && !cursor_row_p (w, row))
11166 ++row;
11168 /* If within the scroll margin, scroll. Note that
11169 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11170 the next line would be drawn, and that
11171 this_scroll_margin can be zero. */
11172 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11173 || PT > MATRIX_ROW_END_CHARPOS (row)
11174 /* Line is completely visible last line in window
11175 and PT is to be set in the next line. */
11176 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11177 && PT == MATRIX_ROW_END_CHARPOS (row)
11178 && !row->ends_at_zv_p
11179 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11180 scroll_p = 1;
11182 else if (PT < XFASTINT (w->last_point))
11184 /* Cursor has to be moved backward. Note that PT >=
11185 CHARPOS (startp) because of the outer
11186 if-statement. */
11187 while (!row->mode_line_p
11188 && (MATRIX_ROW_START_CHARPOS (row) > PT
11189 || (MATRIX_ROW_START_CHARPOS (row) == PT
11190 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11191 && (row->y > this_scroll_margin
11192 || CHARPOS (startp) == BEGV))
11194 xassert (row->enabled_p);
11195 --row;
11198 /* Consider the following case: Window starts at BEGV,
11199 there is invisible, intangible text at BEGV, so that
11200 display starts at some point START > BEGV. It can
11201 happen that we are called with PT somewhere between
11202 BEGV and START. Try to handle that case. */
11203 if (row < w->current_matrix->rows
11204 || row->mode_line_p)
11206 row = w->current_matrix->rows;
11207 if (row->mode_line_p)
11208 ++row;
11211 /* Due to newlines in overlay strings, we may have to
11212 skip forward over overlay strings. */
11213 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11214 && MATRIX_ROW_END_CHARPOS (row) == PT
11215 && !cursor_row_p (w, row))
11216 ++row;
11218 /* If within the scroll margin, scroll. */
11219 if (row->y < this_scroll_margin
11220 && CHARPOS (startp) != BEGV)
11221 scroll_p = 1;
11224 if (PT < MATRIX_ROW_START_CHARPOS (row)
11225 || PT > MATRIX_ROW_END_CHARPOS (row))
11227 /* if PT is not in the glyph row, give up. */
11228 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11230 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11232 if (PT == MATRIX_ROW_END_CHARPOS (row)
11233 && !row->ends_at_zv_p
11234 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11235 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11236 else if (row->height > window_box_height (w))
11238 /* If we end up in a partially visible line, let's
11239 make it fully visible, except when it's taller
11240 than the window, in which case we can't do much
11241 about it. */
11242 *scroll_step = 1;
11243 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11245 else
11247 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11248 if (!make_cursor_line_fully_visible (w))
11249 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11250 else
11251 rc = CURSOR_MOVEMENT_SUCCESS;
11254 else if (scroll_p)
11255 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11256 else
11258 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11259 rc = CURSOR_MOVEMENT_SUCCESS;
11264 return rc;
11267 void
11268 set_vertical_scroll_bar (w)
11269 struct window *w;
11271 int start, end, whole;
11273 /* Calculate the start and end positions for the current window.
11274 At some point, it would be nice to choose between scrollbars
11275 which reflect the whole buffer size, with special markers
11276 indicating narrowing, and scrollbars which reflect only the
11277 visible region.
11279 Note that mini-buffers sometimes aren't displaying any text. */
11280 if (!MINI_WINDOW_P (w)
11281 || (w == XWINDOW (minibuf_window)
11282 && NILP (echo_area_buffer[0])))
11284 struct buffer *buf = XBUFFER (w->buffer);
11285 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11286 start = marker_position (w->start) - BUF_BEGV (buf);
11287 /* I don't think this is guaranteed to be right. For the
11288 moment, we'll pretend it is. */
11289 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11291 if (end < start)
11292 end = start;
11293 if (whole < (end - start))
11294 whole = end - start;
11296 else
11297 start = end = whole = 0;
11299 /* Indicate what this scroll bar ought to be displaying now. */
11300 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11303 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11304 selected_window is redisplayed.
11306 We can return without actually redisplaying the window if
11307 fonts_changed_p is nonzero. In that case, redisplay_internal will
11308 retry. */
11310 static void
11311 redisplay_window (window, just_this_one_p)
11312 Lisp_Object window;
11313 int just_this_one_p;
11315 struct window *w = XWINDOW (window);
11316 struct frame *f = XFRAME (w->frame);
11317 struct buffer *buffer = XBUFFER (w->buffer);
11318 struct buffer *old = current_buffer;
11319 struct text_pos lpoint, opoint, startp;
11320 int update_mode_line;
11321 int tem;
11322 struct it it;
11323 /* Record it now because it's overwritten. */
11324 int current_matrix_up_to_date_p = 0;
11325 /* This is less strict than current_matrix_up_to_date_p.
11326 It indictes that the buffer contents and narrowing are unchanged. */
11327 int buffer_unchanged_p = 0;
11328 int temp_scroll_step = 0;
11329 int count = SPECPDL_INDEX ();
11330 int rc;
11331 int centering_position;
11332 int last_line_misfit = 0;
11334 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11335 opoint = lpoint;
11337 /* W must be a leaf window here. */
11338 xassert (!NILP (w->buffer));
11339 #if GLYPH_DEBUG
11340 *w->desired_matrix->method = 0;
11341 #endif
11343 specbind (Qinhibit_point_motion_hooks, Qt);
11345 reconsider_clip_changes (w, buffer);
11347 /* Has the mode line to be updated? */
11348 update_mode_line = (!NILP (w->update_mode_line)
11349 || update_mode_lines
11350 || buffer->clip_changed
11351 || buffer->prevent_redisplay_optimizations_p);
11353 if (MINI_WINDOW_P (w))
11355 if (w == XWINDOW (echo_area_window)
11356 && !NILP (echo_area_buffer[0]))
11358 if (update_mode_line)
11359 /* We may have to update a tty frame's menu bar or a
11360 tool-bar. Example `M-x C-h C-h C-g'. */
11361 goto finish_menu_bars;
11362 else
11363 /* We've already displayed the echo area glyphs in this window. */
11364 goto finish_scroll_bars;
11366 else if ((w != XWINDOW (minibuf_window)
11367 || minibuf_level == 0)
11368 /* When buffer is nonempty, redisplay window normally. */
11369 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11370 /* Quail displays non-mini buffers in minibuffer window.
11371 In that case, redisplay the window normally. */
11372 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11374 /* W is a mini-buffer window, but it's not active, so clear
11375 it. */
11376 int yb = window_text_bottom_y (w);
11377 struct glyph_row *row;
11378 int y;
11380 for (y = 0, row = w->desired_matrix->rows;
11381 y < yb;
11382 y += row->height, ++row)
11383 blank_row (w, row, y);
11384 goto finish_scroll_bars;
11387 clear_glyph_matrix (w->desired_matrix);
11390 /* Otherwise set up data on this window; select its buffer and point
11391 value. */
11392 /* Really select the buffer, for the sake of buffer-local
11393 variables. */
11394 set_buffer_internal_1 (XBUFFER (w->buffer));
11395 SET_TEXT_POS (opoint, PT, PT_BYTE);
11397 current_matrix_up_to_date_p
11398 = (!NILP (w->window_end_valid)
11399 && !current_buffer->clip_changed
11400 && !current_buffer->prevent_redisplay_optimizations_p
11401 && XFASTINT (w->last_modified) >= MODIFF
11402 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11404 buffer_unchanged_p
11405 = (!NILP (w->window_end_valid)
11406 && !current_buffer->clip_changed
11407 && XFASTINT (w->last_modified) >= MODIFF
11408 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11410 /* When windows_or_buffers_changed is non-zero, we can't rely on
11411 the window end being valid, so set it to nil there. */
11412 if (windows_or_buffers_changed)
11414 /* If window starts on a continuation line, maybe adjust the
11415 window start in case the window's width changed. */
11416 if (XMARKER (w->start)->buffer == current_buffer)
11417 compute_window_start_on_continuation_line (w);
11419 w->window_end_valid = Qnil;
11422 /* Some sanity checks. */
11423 CHECK_WINDOW_END (w);
11424 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11425 abort ();
11426 if (BYTEPOS (opoint) < CHARPOS (opoint))
11427 abort ();
11429 /* If %c is in mode line, update it if needed. */
11430 if (!NILP (w->column_number_displayed)
11431 /* This alternative quickly identifies a common case
11432 where no change is needed. */
11433 && !(PT == XFASTINT (w->last_point)
11434 && XFASTINT (w->last_modified) >= MODIFF
11435 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11436 && (XFASTINT (w->column_number_displayed)
11437 != (int) current_column ())) /* iftc */
11438 update_mode_line = 1;
11440 /* Count number of windows showing the selected buffer. An indirect
11441 buffer counts as its base buffer. */
11442 if (!just_this_one_p)
11444 struct buffer *current_base, *window_base;
11445 current_base = current_buffer;
11446 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11447 if (current_base->base_buffer)
11448 current_base = current_base->base_buffer;
11449 if (window_base->base_buffer)
11450 window_base = window_base->base_buffer;
11451 if (current_base == window_base)
11452 buffer_shared++;
11455 /* Point refers normally to the selected window. For any other
11456 window, set up appropriate value. */
11457 if (!EQ (window, selected_window))
11459 int new_pt = XMARKER (w->pointm)->charpos;
11460 int new_pt_byte = marker_byte_position (w->pointm);
11461 if (new_pt < BEGV)
11463 new_pt = BEGV;
11464 new_pt_byte = BEGV_BYTE;
11465 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11467 else if (new_pt > (ZV - 1))
11469 new_pt = ZV;
11470 new_pt_byte = ZV_BYTE;
11471 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11474 /* We don't use SET_PT so that the point-motion hooks don't run. */
11475 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11478 /* If any of the character widths specified in the display table
11479 have changed, invalidate the width run cache. It's true that
11480 this may be a bit late to catch such changes, but the rest of
11481 redisplay goes (non-fatally) haywire when the display table is
11482 changed, so why should we worry about doing any better? */
11483 if (current_buffer->width_run_cache)
11485 struct Lisp_Char_Table *disptab = buffer_display_table ();
11487 if (! disptab_matches_widthtab (disptab,
11488 XVECTOR (current_buffer->width_table)))
11490 invalidate_region_cache (current_buffer,
11491 current_buffer->width_run_cache,
11492 BEG, Z);
11493 recompute_width_table (current_buffer, disptab);
11497 /* If window-start is screwed up, choose a new one. */
11498 if (XMARKER (w->start)->buffer != current_buffer)
11499 goto recenter;
11501 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11503 /* If someone specified a new starting point but did not insist,
11504 check whether it can be used. */
11505 if (!NILP (w->optional_new_start)
11506 && CHARPOS (startp) >= BEGV
11507 && CHARPOS (startp) <= ZV)
11509 w->optional_new_start = Qnil;
11510 start_display (&it, w, startp);
11511 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11512 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11513 if (IT_CHARPOS (it) == PT)
11514 w->force_start = Qt;
11517 /* Handle case where place to start displaying has been specified,
11518 unless the specified location is outside the accessible range. */
11519 if (!NILP (w->force_start)
11520 || w->frozen_window_start_p)
11522 /* We set this later on if we have to adjust point. */
11523 int new_vpos = -1;
11525 w->force_start = Qnil;
11526 w->vscroll = 0;
11527 w->window_end_valid = Qnil;
11529 /* Forget any recorded base line for line number display. */
11530 if (!buffer_unchanged_p)
11531 w->base_line_number = Qnil;
11533 /* Redisplay the mode line. Select the buffer properly for that.
11534 Also, run the hook window-scroll-functions
11535 because we have scrolled. */
11536 /* Note, we do this after clearing force_start because
11537 if there's an error, it is better to forget about force_start
11538 than to get into an infinite loop calling the hook functions
11539 and having them get more errors. */
11540 if (!update_mode_line
11541 || ! NILP (Vwindow_scroll_functions))
11543 update_mode_line = 1;
11544 w->update_mode_line = Qt;
11545 startp = run_window_scroll_functions (window, startp);
11548 w->last_modified = make_number (0);
11549 w->last_overlay_modified = make_number (0);
11550 if (CHARPOS (startp) < BEGV)
11551 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11552 else if (CHARPOS (startp) > ZV)
11553 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11555 /* Redisplay, then check if cursor has been set during the
11556 redisplay. Give up if new fonts were loaded. */
11557 if (!try_window (window, startp))
11559 w->force_start = Qt;
11560 clear_glyph_matrix (w->desired_matrix);
11561 goto need_larger_matrices;
11564 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11566 /* If point does not appear, try to move point so it does
11567 appear. The desired matrix has been built above, so we
11568 can use it here. */
11569 new_vpos = window_box_height (w) / 2;
11572 if (!make_cursor_line_fully_visible (w))
11574 /* Point does appear, but on a line partly visible at end of window.
11575 Move it back to a fully-visible line. */
11576 new_vpos = window_box_height (w);
11579 /* If we need to move point for either of the above reasons,
11580 now actually do it. */
11581 if (new_vpos >= 0)
11583 struct glyph_row *row;
11585 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11586 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11587 ++row;
11589 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11590 MATRIX_ROW_START_BYTEPOS (row));
11592 if (w != XWINDOW (selected_window))
11593 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11594 else if (current_buffer == old)
11595 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11597 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11599 /* If we are highlighting the region, then we just changed
11600 the region, so redisplay to show it. */
11601 if (!NILP (Vtransient_mark_mode)
11602 && !NILP (current_buffer->mark_active))
11604 clear_glyph_matrix (w->desired_matrix);
11605 if (!try_window (window, startp))
11606 goto need_larger_matrices;
11610 #if GLYPH_DEBUG
11611 debug_method_add (w, "forced window start");
11612 #endif
11613 goto done;
11616 /* Handle case where text has not changed, only point, and it has
11617 not moved off the frame, and we are not retrying after hscroll.
11618 (current_matrix_up_to_date_p is nonzero when retrying.) */
11619 if (current_matrix_up_to_date_p
11620 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11621 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11623 switch (rc)
11625 case CURSOR_MOVEMENT_SUCCESS:
11626 goto done;
11628 #if 0 /* try_cursor_movement never returns this value. */
11629 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11630 goto need_larger_matrices;
11631 #endif
11633 case CURSOR_MOVEMENT_MUST_SCROLL:
11634 goto try_to_scroll;
11636 default:
11637 abort ();
11640 /* If current starting point was originally the beginning of a line
11641 but no longer is, find a new starting point. */
11642 else if (!NILP (w->start_at_line_beg)
11643 && !(CHARPOS (startp) <= BEGV
11644 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11646 #if GLYPH_DEBUG
11647 debug_method_add (w, "recenter 1");
11648 #endif
11649 goto recenter;
11652 /* Try scrolling with try_window_id. Value is > 0 if update has
11653 been done, it is -1 if we know that the same window start will
11654 not work. It is 0 if unsuccessful for some other reason. */
11655 else if ((tem = try_window_id (w)) != 0)
11657 #if GLYPH_DEBUG
11658 debug_method_add (w, "try_window_id %d", tem);
11659 #endif
11661 if (fonts_changed_p)
11662 goto need_larger_matrices;
11663 if (tem > 0)
11664 goto done;
11666 /* Otherwise try_window_id has returned -1 which means that we
11667 don't want the alternative below this comment to execute. */
11669 else if (CHARPOS (startp) >= BEGV
11670 && CHARPOS (startp) <= ZV
11671 && PT >= CHARPOS (startp)
11672 && (CHARPOS (startp) < ZV
11673 /* Avoid starting at end of buffer. */
11674 || CHARPOS (startp) == BEGV
11675 || (XFASTINT (w->last_modified) >= MODIFF
11676 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11678 #if GLYPH_DEBUG
11679 debug_method_add (w, "same window start");
11680 #endif
11682 /* Try to redisplay starting at same place as before.
11683 If point has not moved off frame, accept the results. */
11684 if (!current_matrix_up_to_date_p
11685 /* Don't use try_window_reusing_current_matrix in this case
11686 because a window scroll function can have changed the
11687 buffer. */
11688 || !NILP (Vwindow_scroll_functions)
11689 || MINI_WINDOW_P (w)
11690 || !try_window_reusing_current_matrix (w))
11692 IF_DEBUG (debug_method_add (w, "1"));
11693 try_window (window, startp);
11696 if (fonts_changed_p)
11697 goto need_larger_matrices;
11699 if (w->cursor.vpos >= 0)
11701 if (!just_this_one_p
11702 || current_buffer->clip_changed
11703 || BEG_UNCHANGED < CHARPOS (startp))
11704 /* Forget any recorded base line for line number display. */
11705 w->base_line_number = Qnil;
11707 if (!make_cursor_line_fully_visible (w))
11709 clear_glyph_matrix (w->desired_matrix);
11710 last_line_misfit = 1;
11712 /* Drop through and scroll. */
11713 else
11714 goto done;
11716 else
11717 clear_glyph_matrix (w->desired_matrix);
11720 try_to_scroll:
11722 w->last_modified = make_number (0);
11723 w->last_overlay_modified = make_number (0);
11725 /* Redisplay the mode line. Select the buffer properly for that. */
11726 if (!update_mode_line)
11728 update_mode_line = 1;
11729 w->update_mode_line = Qt;
11732 /* Try to scroll by specified few lines. */
11733 if ((scroll_conservatively
11734 || scroll_step
11735 || temp_scroll_step
11736 || NUMBERP (current_buffer->scroll_up_aggressively)
11737 || NUMBERP (current_buffer->scroll_down_aggressively))
11738 && !current_buffer->clip_changed
11739 && CHARPOS (startp) >= BEGV
11740 && CHARPOS (startp) <= ZV)
11742 /* The function returns -1 if new fonts were loaded, 1 if
11743 successful, 0 if not successful. */
11744 int rc = try_scrolling (window, just_this_one_p,
11745 scroll_conservatively,
11746 scroll_step,
11747 temp_scroll_step, last_line_misfit);
11748 switch (rc)
11750 case SCROLLING_SUCCESS:
11751 goto done;
11753 case SCROLLING_NEED_LARGER_MATRICES:
11754 goto need_larger_matrices;
11756 case SCROLLING_FAILED:
11757 break;
11759 default:
11760 abort ();
11764 /* Finally, just choose place to start which centers point */
11766 recenter:
11767 centering_position = window_box_height (w) / 2;
11769 point_at_top:
11770 /* Jump here with centering_position already set to 0. */
11772 #if GLYPH_DEBUG
11773 debug_method_add (w, "recenter");
11774 #endif
11776 /* w->vscroll = 0; */
11778 /* Forget any previously recorded base line for line number display. */
11779 if (!buffer_unchanged_p)
11780 w->base_line_number = Qnil;
11782 /* Move backward half the height of the window. */
11783 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11784 it.current_y = it.last_visible_y;
11785 move_it_vertically_backward (&it, centering_position);
11786 xassert (IT_CHARPOS (it) >= BEGV);
11788 /* The function move_it_vertically_backward may move over more
11789 than the specified y-distance. If it->w is small, e.g. a
11790 mini-buffer window, we may end up in front of the window's
11791 display area. Start displaying at the start of the line
11792 containing PT in this case. */
11793 if (it.current_y <= 0)
11795 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11796 move_it_vertically (&it, 0);
11797 xassert (IT_CHARPOS (it) <= PT);
11798 it.current_y = 0;
11801 it.current_x = it.hpos = 0;
11803 /* Set startp here explicitly in case that helps avoid an infinite loop
11804 in case the window-scroll-functions functions get errors. */
11805 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11807 /* Run scroll hooks. */
11808 startp = run_window_scroll_functions (window, it.current.pos);
11810 /* Redisplay the window. */
11811 if (!current_matrix_up_to_date_p
11812 || windows_or_buffers_changed
11813 || cursor_type_changed
11814 /* Don't use try_window_reusing_current_matrix in this case
11815 because it can have changed the buffer. */
11816 || !NILP (Vwindow_scroll_functions)
11817 || !just_this_one_p
11818 || MINI_WINDOW_P (w)
11819 || !try_window_reusing_current_matrix (w))
11820 try_window (window, startp);
11822 /* If new fonts have been loaded (due to fontsets), give up. We
11823 have to start a new redisplay since we need to re-adjust glyph
11824 matrices. */
11825 if (fonts_changed_p)
11826 goto need_larger_matrices;
11828 /* If cursor did not appear assume that the middle of the window is
11829 in the first line of the window. Do it again with the next line.
11830 (Imagine a window of height 100, displaying two lines of height
11831 60. Moving back 50 from it->last_visible_y will end in the first
11832 line.) */
11833 if (w->cursor.vpos < 0)
11835 if (!NILP (w->window_end_valid)
11836 && PT >= Z - XFASTINT (w->window_end_pos))
11838 clear_glyph_matrix (w->desired_matrix);
11839 move_it_by_lines (&it, 1, 0);
11840 try_window (window, it.current.pos);
11842 else if (PT < IT_CHARPOS (it))
11844 clear_glyph_matrix (w->desired_matrix);
11845 move_it_by_lines (&it, -1, 0);
11846 try_window (window, it.current.pos);
11848 else
11850 /* Not much we can do about it. */
11854 /* Consider the following case: Window starts at BEGV, there is
11855 invisible, intangible text at BEGV, so that display starts at
11856 some point START > BEGV. It can happen that we are called with
11857 PT somewhere between BEGV and START. Try to handle that case. */
11858 if (w->cursor.vpos < 0)
11860 struct glyph_row *row = w->current_matrix->rows;
11861 if (row->mode_line_p)
11862 ++row;
11863 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11866 if (!make_cursor_line_fully_visible (w))
11868 /* If vscroll is enabled, disable it and try again. */
11869 if (w->vscroll)
11871 w->vscroll = 0;
11872 clear_glyph_matrix (w->desired_matrix);
11873 goto recenter;
11876 /* If centering point failed to make the whole line visible,
11877 put point at the top instead. That has to make the whole line
11878 visible, if it can be done. */
11879 centering_position = 0;
11880 goto point_at_top;
11883 done:
11885 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11886 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11887 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11888 ? Qt : Qnil);
11890 /* Display the mode line, if we must. */
11891 if ((update_mode_line
11892 /* If window not full width, must redo its mode line
11893 if (a) the window to its side is being redone and
11894 (b) we do a frame-based redisplay. This is a consequence
11895 of how inverted lines are drawn in frame-based redisplay. */
11896 || (!just_this_one_p
11897 && !FRAME_WINDOW_P (f)
11898 && !WINDOW_FULL_WIDTH_P (w))
11899 /* Line number to display. */
11900 || INTEGERP (w->base_line_pos)
11901 /* Column number is displayed and different from the one displayed. */
11902 || (!NILP (w->column_number_displayed)
11903 && (XFASTINT (w->column_number_displayed)
11904 != (int) current_column ()))) /* iftc */
11905 /* This means that the window has a mode line. */
11906 && (WINDOW_WANTS_MODELINE_P (w)
11907 || WINDOW_WANTS_HEADER_LINE_P (w)))
11909 display_mode_lines (w);
11911 /* If mode line height has changed, arrange for a thorough
11912 immediate redisplay using the correct mode line height. */
11913 if (WINDOW_WANTS_MODELINE_P (w)
11914 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11916 fonts_changed_p = 1;
11917 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11918 = DESIRED_MODE_LINE_HEIGHT (w);
11921 /* If top line height has changed, arrange for a thorough
11922 immediate redisplay using the correct mode line height. */
11923 if (WINDOW_WANTS_HEADER_LINE_P (w)
11924 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11926 fonts_changed_p = 1;
11927 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11928 = DESIRED_HEADER_LINE_HEIGHT (w);
11931 if (fonts_changed_p)
11932 goto need_larger_matrices;
11935 if (!line_number_displayed
11936 && !BUFFERP (w->base_line_pos))
11938 w->base_line_pos = Qnil;
11939 w->base_line_number = Qnil;
11942 finish_menu_bars:
11944 /* When we reach a frame's selected window, redo the frame's menu bar. */
11945 if (update_mode_line
11946 && EQ (FRAME_SELECTED_WINDOW (f), window))
11948 int redisplay_menu_p = 0;
11949 int redisplay_tool_bar_p = 0;
11951 if (FRAME_WINDOW_P (f))
11953 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11954 || defined (USE_GTK)
11955 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11956 #else
11957 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11958 #endif
11960 else
11961 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11963 if (redisplay_menu_p)
11964 display_menu_bar (w);
11966 #ifdef HAVE_WINDOW_SYSTEM
11967 #ifdef USE_GTK
11968 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11969 #else
11970 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11971 && (FRAME_TOOL_BAR_LINES (f) > 0
11972 || auto_resize_tool_bars_p);
11974 #endif
11976 if (redisplay_tool_bar_p)
11977 redisplay_tool_bar (f);
11978 #endif
11981 /* We go to this label, with fonts_changed_p nonzero,
11982 if it is necessary to try again using larger glyph matrices.
11983 We have to redeem the scroll bar even in this case,
11984 because the loop in redisplay_internal expects that. */
11985 need_larger_matrices:
11987 finish_scroll_bars:
11989 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
11991 /* Set the thumb's position and size. */
11992 set_vertical_scroll_bar (w);
11994 /* Note that we actually used the scroll bar attached to this
11995 window, so it shouldn't be deleted at the end of redisplay. */
11996 redeem_scroll_bar_hook (w);
11999 /* Restore current_buffer and value of point in it. */
12000 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12001 set_buffer_internal_1 (old);
12002 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12004 unbind_to (count, Qnil);
12008 /* Build the complete desired matrix of WINDOW with a window start
12009 buffer position POS. Value is non-zero if successful. It is zero
12010 if fonts were loaded during redisplay which makes re-adjusting
12011 glyph matrices necessary. */
12014 try_window (window, pos)
12015 Lisp_Object window;
12016 struct text_pos pos;
12018 struct window *w = XWINDOW (window);
12019 struct it it;
12020 struct glyph_row *last_text_row = NULL;
12022 /* Make POS the new window start. */
12023 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12025 /* Mark cursor position as unknown. No overlay arrow seen. */
12026 w->cursor.vpos = -1;
12027 overlay_arrow_seen = 0;
12029 /* Initialize iterator and info to start at POS. */
12030 start_display (&it, w, pos);
12032 /* Display all lines of W. */
12033 while (it.current_y < it.last_visible_y)
12035 if (display_line (&it))
12036 last_text_row = it.glyph_row - 1;
12037 if (fonts_changed_p)
12038 return 0;
12041 /* If bottom moved off end of frame, change mode line percentage. */
12042 if (XFASTINT (w->window_end_pos) <= 0
12043 && Z != IT_CHARPOS (it))
12044 w->update_mode_line = Qt;
12046 /* Set window_end_pos to the offset of the last character displayed
12047 on the window from the end of current_buffer. Set
12048 window_end_vpos to its row number. */
12049 if (last_text_row)
12051 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12052 w->window_end_bytepos
12053 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12054 w->window_end_pos
12055 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12056 w->window_end_vpos
12057 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12058 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12059 ->displays_text_p);
12061 else
12063 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12064 w->window_end_pos = make_number (Z - ZV);
12065 w->window_end_vpos = make_number (0);
12068 /* But that is not valid info until redisplay finishes. */
12069 w->window_end_valid = Qnil;
12070 return 1;
12075 /************************************************************************
12076 Window redisplay reusing current matrix when buffer has not changed
12077 ************************************************************************/
12079 /* Try redisplay of window W showing an unchanged buffer with a
12080 different window start than the last time it was displayed by
12081 reusing its current matrix. Value is non-zero if successful.
12082 W->start is the new window start. */
12084 static int
12085 try_window_reusing_current_matrix (w)
12086 struct window *w;
12088 struct frame *f = XFRAME (w->frame);
12089 struct glyph_row *row, *bottom_row;
12090 struct it it;
12091 struct run run;
12092 struct text_pos start, new_start;
12093 int nrows_scrolled, i;
12094 struct glyph_row *last_text_row;
12095 struct glyph_row *last_reused_text_row;
12096 struct glyph_row *start_row;
12097 int start_vpos, min_y, max_y;
12099 #if GLYPH_DEBUG
12100 if (inhibit_try_window_reusing)
12101 return 0;
12102 #endif
12104 if (/* This function doesn't handle terminal frames. */
12105 !FRAME_WINDOW_P (f)
12106 /* Don't try to reuse the display if windows have been split
12107 or such. */
12108 || windows_or_buffers_changed
12109 || cursor_type_changed)
12110 return 0;
12112 /* Can't do this if region may have changed. */
12113 if ((!NILP (Vtransient_mark_mode)
12114 && !NILP (current_buffer->mark_active))
12115 || !NILP (w->region_showing)
12116 || !NILP (Vshow_trailing_whitespace))
12117 return 0;
12119 /* If top-line visibility has changed, give up. */
12120 if (WINDOW_WANTS_HEADER_LINE_P (w)
12121 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12122 return 0;
12124 /* Give up if old or new display is scrolled vertically. We could
12125 make this function handle this, but right now it doesn't. */
12126 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12127 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12128 return 0;
12130 /* The variable new_start now holds the new window start. The old
12131 start `start' can be determined from the current matrix. */
12132 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12133 start = start_row->start.pos;
12134 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12136 /* Clear the desired matrix for the display below. */
12137 clear_glyph_matrix (w->desired_matrix);
12139 if (CHARPOS (new_start) <= CHARPOS (start))
12141 int first_row_y;
12143 /* Don't use this method if the display starts with an ellipsis
12144 displayed for invisible text. It's not easy to handle that case
12145 below, and it's certainly not worth the effort since this is
12146 not a frequent case. */
12147 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12148 return 0;
12150 IF_DEBUG (debug_method_add (w, "twu1"));
12152 /* Display up to a row that can be reused. The variable
12153 last_text_row is set to the last row displayed that displays
12154 text. Note that it.vpos == 0 if or if not there is a
12155 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12156 start_display (&it, w, new_start);
12157 first_row_y = it.current_y;
12158 w->cursor.vpos = -1;
12159 last_text_row = last_reused_text_row = NULL;
12161 while (it.current_y < it.last_visible_y
12162 && IT_CHARPOS (it) < CHARPOS (start)
12163 && !fonts_changed_p)
12164 if (display_line (&it))
12165 last_text_row = it.glyph_row - 1;
12167 /* A value of current_y < last_visible_y means that we stopped
12168 at the previous window start, which in turn means that we
12169 have at least one reusable row. */
12170 if (it.current_y < it.last_visible_y)
12172 /* IT.vpos always starts from 0; it counts text lines. */
12173 nrows_scrolled = it.vpos;
12175 /* Find PT if not already found in the lines displayed. */
12176 if (w->cursor.vpos < 0)
12178 int dy = it.current_y - first_row_y;
12180 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12181 row = row_containing_pos (w, PT, row, NULL, dy);
12182 if (row)
12183 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12184 dy, nrows_scrolled);
12185 else
12187 clear_glyph_matrix (w->desired_matrix);
12188 return 0;
12192 /* Scroll the display. Do it before the current matrix is
12193 changed. The problem here is that update has not yet
12194 run, i.e. part of the current matrix is not up to date.
12195 scroll_run_hook will clear the cursor, and use the
12196 current matrix to get the height of the row the cursor is
12197 in. */
12198 run.current_y = first_row_y;
12199 run.desired_y = it.current_y;
12200 run.height = it.last_visible_y - it.current_y;
12202 if (run.height > 0 && run.current_y != run.desired_y)
12204 update_begin (f);
12205 rif->update_window_begin_hook (w);
12206 rif->clear_window_mouse_face (w);
12207 rif->scroll_run_hook (w, &run);
12208 rif->update_window_end_hook (w, 0, 0);
12209 update_end (f);
12212 /* Shift current matrix down by nrows_scrolled lines. */
12213 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12214 rotate_matrix (w->current_matrix,
12215 start_vpos,
12216 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12217 nrows_scrolled);
12219 /* Disable lines that must be updated. */
12220 for (i = 0; i < it.vpos; ++i)
12221 (start_row + i)->enabled_p = 0;
12223 /* Re-compute Y positions. */
12224 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12225 max_y = it.last_visible_y;
12226 for (row = start_row + nrows_scrolled;
12227 row < bottom_row;
12228 ++row)
12230 row->y = it.current_y;
12231 row->visible_height = row->height;
12233 if (row->y < min_y)
12234 row->visible_height -= min_y - row->y;
12235 if (row->y + row->height > max_y)
12236 row->visible_height -= row->y + row->height - max_y;
12238 it.current_y += row->height;
12240 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12241 last_reused_text_row = row;
12242 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12243 break;
12246 /* Disable lines in the current matrix which are now
12247 below the window. */
12248 for (++row; row < bottom_row; ++row)
12249 row->enabled_p = 0;
12252 /* Update window_end_pos etc.; last_reused_text_row is the last
12253 reused row from the current matrix containing text, if any.
12254 The value of last_text_row is the last displayed line
12255 containing text. */
12256 if (last_reused_text_row)
12258 w->window_end_bytepos
12259 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12260 w->window_end_pos
12261 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12262 w->window_end_vpos
12263 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12264 w->current_matrix));
12266 else if (last_text_row)
12268 w->window_end_bytepos
12269 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12270 w->window_end_pos
12271 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12272 w->window_end_vpos
12273 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12275 else
12277 /* This window must be completely empty. */
12278 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12279 w->window_end_pos = make_number (Z - ZV);
12280 w->window_end_vpos = make_number (0);
12282 w->window_end_valid = Qnil;
12284 /* Update hint: don't try scrolling again in update_window. */
12285 w->desired_matrix->no_scrolling_p = 1;
12287 #if GLYPH_DEBUG
12288 debug_method_add (w, "try_window_reusing_current_matrix 1");
12289 #endif
12290 return 1;
12292 else if (CHARPOS (new_start) > CHARPOS (start))
12294 struct glyph_row *pt_row, *row;
12295 struct glyph_row *first_reusable_row;
12296 struct glyph_row *first_row_to_display;
12297 int dy;
12298 int yb = window_text_bottom_y (w);
12300 /* Find the row starting at new_start, if there is one. Don't
12301 reuse a partially visible line at the end. */
12302 first_reusable_row = start_row;
12303 while (first_reusable_row->enabled_p
12304 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12305 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12306 < CHARPOS (new_start)))
12307 ++first_reusable_row;
12309 /* Give up if there is no row to reuse. */
12310 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12311 || !first_reusable_row->enabled_p
12312 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12313 != CHARPOS (new_start)))
12314 return 0;
12316 /* We can reuse fully visible rows beginning with
12317 first_reusable_row to the end of the window. Set
12318 first_row_to_display to the first row that cannot be reused.
12319 Set pt_row to the row containing point, if there is any. */
12320 pt_row = NULL;
12321 for (first_row_to_display = first_reusable_row;
12322 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12323 ++first_row_to_display)
12325 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12326 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12327 pt_row = first_row_to_display;
12330 /* Start displaying at the start of first_row_to_display. */
12331 xassert (first_row_to_display->y < yb);
12332 init_to_row_start (&it, w, first_row_to_display);
12334 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12335 - start_vpos);
12336 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12337 - nrows_scrolled);
12338 it.current_y = (first_row_to_display->y - first_reusable_row->y
12339 + WINDOW_HEADER_LINE_HEIGHT (w));
12341 /* Display lines beginning with first_row_to_display in the
12342 desired matrix. Set last_text_row to the last row displayed
12343 that displays text. */
12344 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12345 if (pt_row == NULL)
12346 w->cursor.vpos = -1;
12347 last_text_row = NULL;
12348 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12349 if (display_line (&it))
12350 last_text_row = it.glyph_row - 1;
12352 /* Give up If point isn't in a row displayed or reused. */
12353 if (w->cursor.vpos < 0)
12355 clear_glyph_matrix (w->desired_matrix);
12356 return 0;
12359 /* If point is in a reused row, adjust y and vpos of the cursor
12360 position. */
12361 if (pt_row)
12363 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12364 w->current_matrix);
12365 w->cursor.y -= first_reusable_row->y;
12368 /* Scroll the display. */
12369 run.current_y = first_reusable_row->y;
12370 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12371 run.height = it.last_visible_y - run.current_y;
12372 dy = run.current_y - run.desired_y;
12374 if (run.height)
12376 struct frame *f = XFRAME (WINDOW_FRAME (w));
12377 update_begin (f);
12378 rif->update_window_begin_hook (w);
12379 rif->clear_window_mouse_face (w);
12380 rif->scroll_run_hook (w, &run);
12381 rif->update_window_end_hook (w, 0, 0);
12382 update_end (f);
12385 /* Adjust Y positions of reused rows. */
12386 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12387 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12388 max_y = it.last_visible_y;
12389 for (row = first_reusable_row; row < first_row_to_display; ++row)
12391 row->y -= dy;
12392 row->visible_height = row->height;
12393 if (row->y < min_y)
12394 row->visible_height -= min_y - row->y;
12395 if (row->y + row->height > max_y)
12396 row->visible_height -= row->y + row->height - max_y;
12399 /* Scroll the current matrix. */
12400 xassert (nrows_scrolled > 0);
12401 rotate_matrix (w->current_matrix,
12402 start_vpos,
12403 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12404 -nrows_scrolled);
12406 /* Disable rows not reused. */
12407 for (row -= nrows_scrolled; row < bottom_row; ++row)
12408 row->enabled_p = 0;
12410 /* Adjust window end. A null value of last_text_row means that
12411 the window end is in reused rows which in turn means that
12412 only its vpos can have changed. */
12413 if (last_text_row)
12415 w->window_end_bytepos
12416 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12417 w->window_end_pos
12418 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12419 w->window_end_vpos
12420 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12422 else
12424 w->window_end_vpos
12425 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12428 w->window_end_valid = Qnil;
12429 w->desired_matrix->no_scrolling_p = 1;
12431 #if GLYPH_DEBUG
12432 debug_method_add (w, "try_window_reusing_current_matrix 2");
12433 #endif
12434 return 1;
12437 return 0;
12442 /************************************************************************
12443 Window redisplay reusing current matrix when buffer has changed
12444 ************************************************************************/
12446 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12447 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12448 int *, int *));
12449 static struct glyph_row *
12450 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12451 struct glyph_row *));
12454 /* Return the last row in MATRIX displaying text. If row START is
12455 non-null, start searching with that row. IT gives the dimensions
12456 of the display. Value is null if matrix is empty; otherwise it is
12457 a pointer to the row found. */
12459 static struct glyph_row *
12460 find_last_row_displaying_text (matrix, it, start)
12461 struct glyph_matrix *matrix;
12462 struct it *it;
12463 struct glyph_row *start;
12465 struct glyph_row *row, *row_found;
12467 /* Set row_found to the last row in IT->w's current matrix
12468 displaying text. The loop looks funny but think of partially
12469 visible lines. */
12470 row_found = NULL;
12471 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12472 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12474 xassert (row->enabled_p);
12475 row_found = row;
12476 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12477 break;
12478 ++row;
12481 return row_found;
12485 /* Return the last row in the current matrix of W that is not affected
12486 by changes at the start of current_buffer that occurred since W's
12487 current matrix was built. Value is null if no such row exists.
12489 BEG_UNCHANGED us the number of characters unchanged at the start of
12490 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12491 first changed character in current_buffer. Characters at positions <
12492 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12493 when the current matrix was built. */
12495 static struct glyph_row *
12496 find_last_unchanged_at_beg_row (w)
12497 struct window *w;
12499 int first_changed_pos = BEG + BEG_UNCHANGED;
12500 struct glyph_row *row;
12501 struct glyph_row *row_found = NULL;
12502 int yb = window_text_bottom_y (w);
12504 /* Find the last row displaying unchanged text. */
12505 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12506 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12507 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12509 if (/* If row ends before first_changed_pos, it is unchanged,
12510 except in some case. */
12511 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12512 /* When row ends in ZV and we write at ZV it is not
12513 unchanged. */
12514 && !row->ends_at_zv_p
12515 /* When first_changed_pos is the end of a continued line,
12516 row is not unchanged because it may be no longer
12517 continued. */
12518 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12519 && row->continued_p))
12520 row_found = row;
12522 /* Stop if last visible row. */
12523 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12524 break;
12526 ++row;
12529 return row_found;
12533 /* Find the first glyph row in the current matrix of W that is not
12534 affected by changes at the end of current_buffer since the
12535 time W's current matrix was built.
12537 Return in *DELTA the number of chars by which buffer positions in
12538 unchanged text at the end of current_buffer must be adjusted.
12540 Return in *DELTA_BYTES the corresponding number of bytes.
12542 Value is null if no such row exists, i.e. all rows are affected by
12543 changes. */
12545 static struct glyph_row *
12546 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12547 struct window *w;
12548 int *delta, *delta_bytes;
12550 struct glyph_row *row;
12551 struct glyph_row *row_found = NULL;
12553 *delta = *delta_bytes = 0;
12555 /* Display must not have been paused, otherwise the current matrix
12556 is not up to date. */
12557 if (NILP (w->window_end_valid))
12558 abort ();
12560 /* A value of window_end_pos >= END_UNCHANGED means that the window
12561 end is in the range of changed text. If so, there is no
12562 unchanged row at the end of W's current matrix. */
12563 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12564 return NULL;
12566 /* Set row to the last row in W's current matrix displaying text. */
12567 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12569 /* If matrix is entirely empty, no unchanged row exists. */
12570 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12572 /* The value of row is the last glyph row in the matrix having a
12573 meaningful buffer position in it. The end position of row
12574 corresponds to window_end_pos. This allows us to translate
12575 buffer positions in the current matrix to current buffer
12576 positions for characters not in changed text. */
12577 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12578 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12579 int last_unchanged_pos, last_unchanged_pos_old;
12580 struct glyph_row *first_text_row
12581 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12583 *delta = Z - Z_old;
12584 *delta_bytes = Z_BYTE - Z_BYTE_old;
12586 /* Set last_unchanged_pos to the buffer position of the last
12587 character in the buffer that has not been changed. Z is the
12588 index + 1 of the last character in current_buffer, i.e. by
12589 subtracting END_UNCHANGED we get the index of the last
12590 unchanged character, and we have to add BEG to get its buffer
12591 position. */
12592 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12593 last_unchanged_pos_old = last_unchanged_pos - *delta;
12595 /* Search backward from ROW for a row displaying a line that
12596 starts at a minimum position >= last_unchanged_pos_old. */
12597 for (; row > first_text_row; --row)
12599 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12600 abort ();
12602 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12603 row_found = row;
12607 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12608 abort ();
12610 return row_found;
12614 /* Make sure that glyph rows in the current matrix of window W
12615 reference the same glyph memory as corresponding rows in the
12616 frame's frame matrix. This function is called after scrolling W's
12617 current matrix on a terminal frame in try_window_id and
12618 try_window_reusing_current_matrix. */
12620 static void
12621 sync_frame_with_window_matrix_rows (w)
12622 struct window *w;
12624 struct frame *f = XFRAME (w->frame);
12625 struct glyph_row *window_row, *window_row_end, *frame_row;
12627 /* Preconditions: W must be a leaf window and full-width. Its frame
12628 must have a frame matrix. */
12629 xassert (NILP (w->hchild) && NILP (w->vchild));
12630 xassert (WINDOW_FULL_WIDTH_P (w));
12631 xassert (!FRAME_WINDOW_P (f));
12633 /* If W is a full-width window, glyph pointers in W's current matrix
12634 have, by definition, to be the same as glyph pointers in the
12635 corresponding frame matrix. Note that frame matrices have no
12636 marginal areas (see build_frame_matrix). */
12637 window_row = w->current_matrix->rows;
12638 window_row_end = window_row + w->current_matrix->nrows;
12639 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
12640 while (window_row < window_row_end)
12642 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12643 struct glyph *end = window_row->glyphs[LAST_AREA];
12645 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12646 frame_row->glyphs[TEXT_AREA] = start;
12647 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12648 frame_row->glyphs[LAST_AREA] = end;
12650 /* Disable frame rows whose corresponding window rows have
12651 been disabled in try_window_id. */
12652 if (!window_row->enabled_p)
12653 frame_row->enabled_p = 0;
12655 ++window_row, ++frame_row;
12660 /* Find the glyph row in window W containing CHARPOS. Consider all
12661 rows between START and END (not inclusive). END null means search
12662 all rows to the end of the display area of W. Value is the row
12663 containing CHARPOS or null. */
12665 struct glyph_row *
12666 row_containing_pos (w, charpos, start, end, dy)
12667 struct window *w;
12668 int charpos;
12669 struct glyph_row *start, *end;
12670 int dy;
12672 struct glyph_row *row = start;
12673 int last_y;
12675 /* If we happen to start on a header-line, skip that. */
12676 if (row->mode_line_p)
12677 ++row;
12679 if ((end && row >= end) || !row->enabled_p)
12680 return NULL;
12682 last_y = window_text_bottom_y (w) - dy;
12684 while (1)
12686 /* Give up if we have gone too far. */
12687 if (end && row >= end)
12688 return NULL;
12689 /* This formerly returned if they were equal.
12690 I think that both quantities are of a "last plus one" type;
12691 if so, when they are equal, the row is within the screen. -- rms. */
12692 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12693 return NULL;
12695 /* If it is in this row, return this row. */
12696 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12697 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12698 /* The end position of a row equals the start
12699 position of the next row. If CHARPOS is there, we
12700 would rather display it in the next line, except
12701 when this line ends in ZV. */
12702 && !row->ends_at_zv_p
12703 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12704 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12705 return row;
12706 ++row;
12711 /* Try to redisplay window W by reusing its existing display. W's
12712 current matrix must be up to date when this function is called,
12713 i.e. window_end_valid must not be nil.
12715 Value is
12717 1 if display has been updated
12718 0 if otherwise unsuccessful
12719 -1 if redisplay with same window start is known not to succeed
12721 The following steps are performed:
12723 1. Find the last row in the current matrix of W that is not
12724 affected by changes at the start of current_buffer. If no such row
12725 is found, give up.
12727 2. Find the first row in W's current matrix that is not affected by
12728 changes at the end of current_buffer. Maybe there is no such row.
12730 3. Display lines beginning with the row + 1 found in step 1 to the
12731 row found in step 2 or, if step 2 didn't find a row, to the end of
12732 the window.
12734 4. If cursor is not known to appear on the window, give up.
12736 5. If display stopped at the row found in step 2, scroll the
12737 display and current matrix as needed.
12739 6. Maybe display some lines at the end of W, if we must. This can
12740 happen under various circumstances, like a partially visible line
12741 becoming fully visible, or because newly displayed lines are displayed
12742 in smaller font sizes.
12744 7. Update W's window end information. */
12746 static int
12747 try_window_id (w)
12748 struct window *w;
12750 struct frame *f = XFRAME (w->frame);
12751 struct glyph_matrix *current_matrix = w->current_matrix;
12752 struct glyph_matrix *desired_matrix = w->desired_matrix;
12753 struct glyph_row *last_unchanged_at_beg_row;
12754 struct glyph_row *first_unchanged_at_end_row;
12755 struct glyph_row *row;
12756 struct glyph_row *bottom_row;
12757 int bottom_vpos;
12758 struct it it;
12759 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12760 struct text_pos start_pos;
12761 struct run run;
12762 int first_unchanged_at_end_vpos = 0;
12763 struct glyph_row *last_text_row, *last_text_row_at_end;
12764 struct text_pos start;
12765 int first_changed_charpos, last_changed_charpos;
12767 #if GLYPH_DEBUG
12768 if (inhibit_try_window_id)
12769 return 0;
12770 #endif
12772 /* This is handy for debugging. */
12773 #if 0
12774 #define GIVE_UP(X) \
12775 do { \
12776 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12777 return 0; \
12778 } while (0)
12779 #else
12780 #define GIVE_UP(X) return 0
12781 #endif
12783 SET_TEXT_POS_FROM_MARKER (start, w->start);
12785 /* Don't use this for mini-windows because these can show
12786 messages and mini-buffers, and we don't handle that here. */
12787 if (MINI_WINDOW_P (w))
12788 GIVE_UP (1);
12790 /* This flag is used to prevent redisplay optimizations. */
12791 if (windows_or_buffers_changed || cursor_type_changed)
12792 GIVE_UP (2);
12794 /* Verify that narrowing has not changed.
12795 Also verify that we were not told to prevent redisplay optimizations.
12796 It would be nice to further
12797 reduce the number of cases where this prevents try_window_id. */
12798 if (current_buffer->clip_changed
12799 || current_buffer->prevent_redisplay_optimizations_p)
12800 GIVE_UP (3);
12802 /* Window must either use window-based redisplay or be full width. */
12803 if (!FRAME_WINDOW_P (f)
12804 && (!line_ins_del_ok
12805 || !WINDOW_FULL_WIDTH_P (w)))
12806 GIVE_UP (4);
12808 /* Give up if point is not known NOT to appear in W. */
12809 if (PT < CHARPOS (start))
12810 GIVE_UP (5);
12812 /* Another way to prevent redisplay optimizations. */
12813 if (XFASTINT (w->last_modified) == 0)
12814 GIVE_UP (6);
12816 /* Verify that window is not hscrolled. */
12817 if (XFASTINT (w->hscroll) != 0)
12818 GIVE_UP (7);
12820 /* Verify that display wasn't paused. */
12821 if (NILP (w->window_end_valid))
12822 GIVE_UP (8);
12824 /* Can't use this if highlighting a region because a cursor movement
12825 will do more than just set the cursor. */
12826 if (!NILP (Vtransient_mark_mode)
12827 && !NILP (current_buffer->mark_active))
12828 GIVE_UP (9);
12830 /* Likewise if highlighting trailing whitespace. */
12831 if (!NILP (Vshow_trailing_whitespace))
12832 GIVE_UP (11);
12834 /* Likewise if showing a region. */
12835 if (!NILP (w->region_showing))
12836 GIVE_UP (10);
12838 /* Can use this if overlay arrow position and or string have changed. */
12839 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12840 || !EQ (last_arrow_string, Voverlay_arrow_string))
12841 GIVE_UP (12);
12844 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12845 only if buffer has really changed. The reason is that the gap is
12846 initially at Z for freshly visited files. The code below would
12847 set end_unchanged to 0 in that case. */
12848 if (MODIFF > SAVE_MODIFF
12849 /* This seems to happen sometimes after saving a buffer. */
12850 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12852 if (GPT - BEG < BEG_UNCHANGED)
12853 BEG_UNCHANGED = GPT - BEG;
12854 if (Z - GPT < END_UNCHANGED)
12855 END_UNCHANGED = Z - GPT;
12858 /* The position of the first and last character that has been changed. */
12859 first_changed_charpos = BEG + BEG_UNCHANGED;
12860 last_changed_charpos = Z - END_UNCHANGED;
12862 /* If window starts after a line end, and the last change is in
12863 front of that newline, then changes don't affect the display.
12864 This case happens with stealth-fontification. Note that although
12865 the display is unchanged, glyph positions in the matrix have to
12866 be adjusted, of course. */
12867 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12868 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12869 && ((last_changed_charpos < CHARPOS (start)
12870 && CHARPOS (start) == BEGV)
12871 || (last_changed_charpos < CHARPOS (start) - 1
12872 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12874 int Z_old, delta, Z_BYTE_old, delta_bytes;
12875 struct glyph_row *r0;
12877 /* Compute how many chars/bytes have been added to or removed
12878 from the buffer. */
12879 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12880 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12881 delta = Z - Z_old;
12882 delta_bytes = Z_BYTE - Z_BYTE_old;
12884 /* Give up if PT is not in the window. Note that it already has
12885 been checked at the start of try_window_id that PT is not in
12886 front of the window start. */
12887 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12888 GIVE_UP (13);
12890 /* If window start is unchanged, we can reuse the whole matrix
12891 as is, after adjusting glyph positions. No need to compute
12892 the window end again, since its offset from Z hasn't changed. */
12893 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12894 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12895 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
12896 /* PT must not be in a partially visible line. */
12897 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
12898 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12900 /* Adjust positions in the glyph matrix. */
12901 if (delta || delta_bytes)
12903 struct glyph_row *r1
12904 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12905 increment_matrix_positions (w->current_matrix,
12906 MATRIX_ROW_VPOS (r0, current_matrix),
12907 MATRIX_ROW_VPOS (r1, current_matrix),
12908 delta, delta_bytes);
12911 /* Set the cursor. */
12912 row = row_containing_pos (w, PT, r0, NULL, 0);
12913 if (row)
12914 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12915 else
12916 abort ();
12917 return 1;
12921 /* Handle the case that changes are all below what is displayed in
12922 the window, and that PT is in the window. This shortcut cannot
12923 be taken if ZV is visible in the window, and text has been added
12924 there that is visible in the window. */
12925 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12926 /* ZV is not visible in the window, or there are no
12927 changes at ZV, actually. */
12928 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12929 || first_changed_charpos == last_changed_charpos))
12931 struct glyph_row *r0;
12933 /* Give up if PT is not in the window. Note that it already has
12934 been checked at the start of try_window_id that PT is not in
12935 front of the window start. */
12936 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12937 GIVE_UP (14);
12939 /* If window start is unchanged, we can reuse the whole matrix
12940 as is, without changing glyph positions since no text has
12941 been added/removed in front of the window end. */
12942 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12943 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
12944 /* PT must not be in a partially visible line. */
12945 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
12946 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
12948 /* We have to compute the window end anew since text
12949 can have been added/removed after it. */
12950 w->window_end_pos
12951 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12952 w->window_end_bytepos
12953 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12955 /* Set the cursor. */
12956 row = row_containing_pos (w, PT, r0, NULL, 0);
12957 if (row)
12958 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12959 else
12960 abort ();
12961 return 2;
12965 /* Give up if window start is in the changed area.
12967 The condition used to read
12969 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12971 but why that was tested escapes me at the moment. */
12972 if (CHARPOS (start) >= first_changed_charpos
12973 && CHARPOS (start) <= last_changed_charpos)
12974 GIVE_UP (15);
12976 /* Check that window start agrees with the start of the first glyph
12977 row in its current matrix. Check this after we know the window
12978 start is not in changed text, otherwise positions would not be
12979 comparable. */
12980 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12981 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12982 GIVE_UP (16);
12984 /* Give up if the window ends in strings. Overlay strings
12985 at the end are difficult to handle, so don't try. */
12986 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12987 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12988 GIVE_UP (20);
12990 /* Compute the position at which we have to start displaying new
12991 lines. Some of the lines at the top of the window might be
12992 reusable because they are not displaying changed text. Find the
12993 last row in W's current matrix not affected by changes at the
12994 start of current_buffer. Value is null if changes start in the
12995 first line of window. */
12996 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12997 if (last_unchanged_at_beg_row)
12999 /* Avoid starting to display in the moddle of a character, a TAB
13000 for instance. This is easier than to set up the iterator
13001 exactly, and it's not a frequent case, so the additional
13002 effort wouldn't really pay off. */
13003 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13004 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13005 && last_unchanged_at_beg_row > w->current_matrix->rows)
13006 --last_unchanged_at_beg_row;
13008 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13009 GIVE_UP (17);
13011 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13012 GIVE_UP (18);
13013 start_pos = it.current.pos;
13015 /* Start displaying new lines in the desired matrix at the same
13016 vpos we would use in the current matrix, i.e. below
13017 last_unchanged_at_beg_row. */
13018 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13019 current_matrix);
13020 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13021 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13023 xassert (it.hpos == 0 && it.current_x == 0);
13025 else
13027 /* There are no reusable lines at the start of the window.
13028 Start displaying in the first line. */
13029 start_display (&it, w, start);
13030 start_pos = it.current.pos;
13033 /* Find the first row that is not affected by changes at the end of
13034 the buffer. Value will be null if there is no unchanged row, in
13035 which case we must redisplay to the end of the window. delta
13036 will be set to the value by which buffer positions beginning with
13037 first_unchanged_at_end_row have to be adjusted due to text
13038 changes. */
13039 first_unchanged_at_end_row
13040 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13041 IF_DEBUG (debug_delta = delta);
13042 IF_DEBUG (debug_delta_bytes = delta_bytes);
13044 /* Set stop_pos to the buffer position up to which we will have to
13045 display new lines. If first_unchanged_at_end_row != NULL, this
13046 is the buffer position of the start of the line displayed in that
13047 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13048 that we don't stop at a buffer position. */
13049 stop_pos = 0;
13050 if (first_unchanged_at_end_row)
13052 xassert (last_unchanged_at_beg_row == NULL
13053 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13055 /* If this is a continuation line, move forward to the next one
13056 that isn't. Changes in lines above affect this line.
13057 Caution: this may move first_unchanged_at_end_row to a row
13058 not displaying text. */
13059 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13060 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13061 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13062 < it.last_visible_y))
13063 ++first_unchanged_at_end_row;
13065 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13066 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13067 >= it.last_visible_y))
13068 first_unchanged_at_end_row = NULL;
13069 else
13071 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13072 + delta);
13073 first_unchanged_at_end_vpos
13074 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13075 xassert (stop_pos >= Z - END_UNCHANGED);
13078 else if (last_unchanged_at_beg_row == NULL)
13079 GIVE_UP (19);
13082 #if GLYPH_DEBUG
13084 /* Either there is no unchanged row at the end, or the one we have
13085 now displays text. This is a necessary condition for the window
13086 end pos calculation at the end of this function. */
13087 xassert (first_unchanged_at_end_row == NULL
13088 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13090 debug_last_unchanged_at_beg_vpos
13091 = (last_unchanged_at_beg_row
13092 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13093 : -1);
13094 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13096 #endif /* GLYPH_DEBUG != 0 */
13099 /* Display new lines. Set last_text_row to the last new line
13100 displayed which has text on it, i.e. might end up as being the
13101 line where the window_end_vpos is. */
13102 w->cursor.vpos = -1;
13103 last_text_row = NULL;
13104 overlay_arrow_seen = 0;
13105 while (it.current_y < it.last_visible_y
13106 && !fonts_changed_p
13107 && (first_unchanged_at_end_row == NULL
13108 || IT_CHARPOS (it) < stop_pos))
13110 if (display_line (&it))
13111 last_text_row = it.glyph_row - 1;
13114 if (fonts_changed_p)
13115 return -1;
13118 /* Compute differences in buffer positions, y-positions etc. for
13119 lines reused at the bottom of the window. Compute what we can
13120 scroll. */
13121 if (first_unchanged_at_end_row
13122 /* No lines reused because we displayed everything up to the
13123 bottom of the window. */
13124 && it.current_y < it.last_visible_y)
13126 dvpos = (it.vpos
13127 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13128 current_matrix));
13129 dy = it.current_y - first_unchanged_at_end_row->y;
13130 run.current_y = first_unchanged_at_end_row->y;
13131 run.desired_y = run.current_y + dy;
13132 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13134 else
13136 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13137 first_unchanged_at_end_row = NULL;
13139 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13142 /* Find the cursor if not already found. We have to decide whether
13143 PT will appear on this window (it sometimes doesn't, but this is
13144 not a very frequent case.) This decision has to be made before
13145 the current matrix is altered. A value of cursor.vpos < 0 means
13146 that PT is either in one of the lines beginning at
13147 first_unchanged_at_end_row or below the window. Don't care for
13148 lines that might be displayed later at the window end; as
13149 mentioned, this is not a frequent case. */
13150 if (w->cursor.vpos < 0)
13152 /* Cursor in unchanged rows at the top? */
13153 if (PT < CHARPOS (start_pos)
13154 && last_unchanged_at_beg_row)
13156 row = row_containing_pos (w, PT,
13157 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13158 last_unchanged_at_beg_row + 1, 0);
13159 if (row)
13160 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13163 /* Start from first_unchanged_at_end_row looking for PT. */
13164 else if (first_unchanged_at_end_row)
13166 row = row_containing_pos (w, PT - delta,
13167 first_unchanged_at_end_row, NULL, 0);
13168 if (row)
13169 set_cursor_from_row (w, row, w->current_matrix, delta,
13170 delta_bytes, dy, dvpos);
13173 /* Give up if cursor was not found. */
13174 if (w->cursor.vpos < 0)
13176 clear_glyph_matrix (w->desired_matrix);
13177 return -1;
13181 /* Don't let the cursor end in the scroll margins. */
13183 int this_scroll_margin, cursor_height;
13185 this_scroll_margin = max (0, scroll_margin);
13186 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13187 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13188 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13190 if ((w->cursor.y < this_scroll_margin
13191 && CHARPOS (start) > BEGV)
13192 /* Don't take scroll margin into account at the bottom because
13193 old redisplay didn't do it either. */
13194 || w->cursor.y + cursor_height > it.last_visible_y)
13196 w->cursor.vpos = -1;
13197 clear_glyph_matrix (w->desired_matrix);
13198 return -1;
13202 /* Scroll the display. Do it before changing the current matrix so
13203 that xterm.c doesn't get confused about where the cursor glyph is
13204 found. */
13205 if (dy && run.height)
13207 update_begin (f);
13209 if (FRAME_WINDOW_P (f))
13211 rif->update_window_begin_hook (w);
13212 rif->clear_window_mouse_face (w);
13213 rif->scroll_run_hook (w, &run);
13214 rif->update_window_end_hook (w, 0, 0);
13216 else
13218 /* Terminal frame. In this case, dvpos gives the number of
13219 lines to scroll by; dvpos < 0 means scroll up. */
13220 int first_unchanged_at_end_vpos
13221 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13222 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13223 int end = (WINDOW_TOP_EDGE_LINE (w)
13224 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13225 + window_internal_height (w));
13227 /* Perform the operation on the screen. */
13228 if (dvpos > 0)
13230 /* Scroll last_unchanged_at_beg_row to the end of the
13231 window down dvpos lines. */
13232 set_terminal_window (end);
13234 /* On dumb terminals delete dvpos lines at the end
13235 before inserting dvpos empty lines. */
13236 if (!scroll_region_ok)
13237 ins_del_lines (end - dvpos, -dvpos);
13239 /* Insert dvpos empty lines in front of
13240 last_unchanged_at_beg_row. */
13241 ins_del_lines (from, dvpos);
13243 else if (dvpos < 0)
13245 /* Scroll up last_unchanged_at_beg_vpos to the end of
13246 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13247 set_terminal_window (end);
13249 /* Delete dvpos lines in front of
13250 last_unchanged_at_beg_vpos. ins_del_lines will set
13251 the cursor to the given vpos and emit |dvpos| delete
13252 line sequences. */
13253 ins_del_lines (from + dvpos, dvpos);
13255 /* On a dumb terminal insert dvpos empty lines at the
13256 end. */
13257 if (!scroll_region_ok)
13258 ins_del_lines (end + dvpos, -dvpos);
13261 set_terminal_window (0);
13264 update_end (f);
13267 /* Shift reused rows of the current matrix to the right position.
13268 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13269 text. */
13270 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13271 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13272 if (dvpos < 0)
13274 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13275 bottom_vpos, dvpos);
13276 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13277 bottom_vpos, 0);
13279 else if (dvpos > 0)
13281 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13282 bottom_vpos, dvpos);
13283 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13284 first_unchanged_at_end_vpos + dvpos, 0);
13287 /* For frame-based redisplay, make sure that current frame and window
13288 matrix are in sync with respect to glyph memory. */
13289 if (!FRAME_WINDOW_P (f))
13290 sync_frame_with_window_matrix_rows (w);
13292 /* Adjust buffer positions in reused rows. */
13293 if (delta)
13294 increment_matrix_positions (current_matrix,
13295 first_unchanged_at_end_vpos + dvpos,
13296 bottom_vpos, delta, delta_bytes);
13298 /* Adjust Y positions. */
13299 if (dy)
13300 shift_glyph_matrix (w, current_matrix,
13301 first_unchanged_at_end_vpos + dvpos,
13302 bottom_vpos, dy);
13304 if (first_unchanged_at_end_row)
13305 first_unchanged_at_end_row += dvpos;
13307 /* If scrolling up, there may be some lines to display at the end of
13308 the window. */
13309 last_text_row_at_end = NULL;
13310 if (dy < 0)
13312 /* Scrolling up can leave for example a partially visible line
13313 at the end of the window to be redisplayed. */
13314 /* Set last_row to the glyph row in the current matrix where the
13315 window end line is found. It has been moved up or down in
13316 the matrix by dvpos. */
13317 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13318 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13320 /* If last_row is the window end line, it should display text. */
13321 xassert (last_row->displays_text_p);
13323 /* If window end line was partially visible before, begin
13324 displaying at that line. Otherwise begin displaying with the
13325 line following it. */
13326 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13328 init_to_row_start (&it, w, last_row);
13329 it.vpos = last_vpos;
13330 it.current_y = last_row->y;
13332 else
13334 init_to_row_end (&it, w, last_row);
13335 it.vpos = 1 + last_vpos;
13336 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13337 ++last_row;
13340 /* We may start in a continuation line. If so, we have to
13341 get the right continuation_lines_width and current_x. */
13342 it.continuation_lines_width = last_row->continuation_lines_width;
13343 it.hpos = it.current_x = 0;
13345 /* Display the rest of the lines at the window end. */
13346 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13347 while (it.current_y < it.last_visible_y
13348 && !fonts_changed_p)
13350 /* Is it always sure that the display agrees with lines in
13351 the current matrix? I don't think so, so we mark rows
13352 displayed invalid in the current matrix by setting their
13353 enabled_p flag to zero. */
13354 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13355 if (display_line (&it))
13356 last_text_row_at_end = it.glyph_row - 1;
13360 /* Update window_end_pos and window_end_vpos. */
13361 if (first_unchanged_at_end_row
13362 && first_unchanged_at_end_row->y < it.last_visible_y
13363 && !last_text_row_at_end)
13365 /* Window end line if one of the preserved rows from the current
13366 matrix. Set row to the last row displaying text in current
13367 matrix starting at first_unchanged_at_end_row, after
13368 scrolling. */
13369 xassert (first_unchanged_at_end_row->displays_text_p);
13370 row = find_last_row_displaying_text (w->current_matrix, &it,
13371 first_unchanged_at_end_row);
13372 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13374 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13375 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13376 w->window_end_vpos
13377 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13378 xassert (w->window_end_bytepos >= 0);
13379 IF_DEBUG (debug_method_add (w, "A"));
13381 else if (last_text_row_at_end)
13383 w->window_end_pos
13384 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13385 w->window_end_bytepos
13386 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13387 w->window_end_vpos
13388 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13389 xassert (w->window_end_bytepos >= 0);
13390 IF_DEBUG (debug_method_add (w, "B"));
13392 else if (last_text_row)
13394 /* We have displayed either to the end of the window or at the
13395 end of the window, i.e. the last row with text is to be found
13396 in the desired matrix. */
13397 w->window_end_pos
13398 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13399 w->window_end_bytepos
13400 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13401 w->window_end_vpos
13402 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13403 xassert (w->window_end_bytepos >= 0);
13405 else if (first_unchanged_at_end_row == NULL
13406 && last_text_row == NULL
13407 && last_text_row_at_end == NULL)
13409 /* Displayed to end of window, but no line containing text was
13410 displayed. Lines were deleted at the end of the window. */
13411 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13412 int vpos = XFASTINT (w->window_end_vpos);
13413 struct glyph_row *current_row = current_matrix->rows + vpos;
13414 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13416 for (row = NULL;
13417 row == NULL && vpos >= first_vpos;
13418 --vpos, --current_row, --desired_row)
13420 if (desired_row->enabled_p)
13422 if (desired_row->displays_text_p)
13423 row = desired_row;
13425 else if (current_row->displays_text_p)
13426 row = current_row;
13429 xassert (row != NULL);
13430 w->window_end_vpos = make_number (vpos + 1);
13431 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13432 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13433 xassert (w->window_end_bytepos >= 0);
13434 IF_DEBUG (debug_method_add (w, "C"));
13436 else
13437 abort ();
13439 #if 0 /* This leads to problems, for instance when the cursor is
13440 at ZV, and the cursor line displays no text. */
13441 /* Disable rows below what's displayed in the window. This makes
13442 debugging easier. */
13443 enable_glyph_matrix_rows (current_matrix,
13444 XFASTINT (w->window_end_vpos) + 1,
13445 bottom_vpos, 0);
13446 #endif
13448 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13449 debug_end_vpos = XFASTINT (w->window_end_vpos));
13451 /* Record that display has not been completed. */
13452 w->window_end_valid = Qnil;
13453 w->desired_matrix->no_scrolling_p = 1;
13454 return 3;
13456 #undef GIVE_UP
13461 /***********************************************************************
13462 More debugging support
13463 ***********************************************************************/
13465 #if GLYPH_DEBUG
13467 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13468 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13469 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13472 /* Dump the contents of glyph matrix MATRIX on stderr.
13474 GLYPHS 0 means don't show glyph contents.
13475 GLYPHS 1 means show glyphs in short form
13476 GLYPHS > 1 means show glyphs in long form. */
13478 void
13479 dump_glyph_matrix (matrix, glyphs)
13480 struct glyph_matrix *matrix;
13481 int glyphs;
13483 int i;
13484 for (i = 0; i < matrix->nrows; ++i)
13485 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13489 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13490 the glyph row and area where the glyph comes from. */
13492 void
13493 dump_glyph (row, glyph, area)
13494 struct glyph_row *row;
13495 struct glyph *glyph;
13496 int area;
13498 if (glyph->type == CHAR_GLYPH)
13500 fprintf (stderr,
13501 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13502 glyph - row->glyphs[TEXT_AREA],
13503 'C',
13504 glyph->charpos,
13505 (BUFFERP (glyph->object)
13506 ? 'B'
13507 : (STRINGP (glyph->object)
13508 ? 'S'
13509 : '-')),
13510 glyph->pixel_width,
13511 glyph->u.ch,
13512 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13513 ? glyph->u.ch
13514 : '.'),
13515 glyph->face_id,
13516 glyph->left_box_line_p,
13517 glyph->right_box_line_p);
13519 else if (glyph->type == STRETCH_GLYPH)
13521 fprintf (stderr,
13522 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13523 glyph - row->glyphs[TEXT_AREA],
13524 'S',
13525 glyph->charpos,
13526 (BUFFERP (glyph->object)
13527 ? 'B'
13528 : (STRINGP (glyph->object)
13529 ? 'S'
13530 : '-')),
13531 glyph->pixel_width,
13533 '.',
13534 glyph->face_id,
13535 glyph->left_box_line_p,
13536 glyph->right_box_line_p);
13538 else if (glyph->type == IMAGE_GLYPH)
13540 fprintf (stderr,
13541 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13542 glyph - row->glyphs[TEXT_AREA],
13543 'I',
13544 glyph->charpos,
13545 (BUFFERP (glyph->object)
13546 ? 'B'
13547 : (STRINGP (glyph->object)
13548 ? 'S'
13549 : '-')),
13550 glyph->pixel_width,
13551 glyph->u.img_id,
13552 '.',
13553 glyph->face_id,
13554 glyph->left_box_line_p,
13555 glyph->right_box_line_p);
13560 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13561 GLYPHS 0 means don't show glyph contents.
13562 GLYPHS 1 means show glyphs in short form
13563 GLYPHS > 1 means show glyphs in long form. */
13565 void
13566 dump_glyph_row (row, vpos, glyphs)
13567 struct glyph_row *row;
13568 int vpos, glyphs;
13570 if (glyphs != 1)
13572 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13573 fprintf (stderr, "=======================================================================\n");
13575 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13576 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13577 vpos,
13578 MATRIX_ROW_START_CHARPOS (row),
13579 MATRIX_ROW_END_CHARPOS (row),
13580 row->used[TEXT_AREA],
13581 row->contains_overlapping_glyphs_p,
13582 row->enabled_p,
13583 row->truncated_on_left_p,
13584 row->truncated_on_right_p,
13585 row->overlay_arrow_p,
13586 row->continued_p,
13587 MATRIX_ROW_CONTINUATION_LINE_P (row),
13588 row->displays_text_p,
13589 row->ends_at_zv_p,
13590 row->fill_line_p,
13591 row->ends_in_middle_of_char_p,
13592 row->starts_in_middle_of_char_p,
13593 row->mouse_face_p,
13594 row->x,
13595 row->y,
13596 row->pixel_width,
13597 row->height,
13598 row->visible_height,
13599 row->ascent,
13600 row->phys_ascent);
13601 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13602 row->end.overlay_string_index,
13603 row->continuation_lines_width);
13604 fprintf (stderr, "%9d %5d\n",
13605 CHARPOS (row->start.string_pos),
13606 CHARPOS (row->end.string_pos));
13607 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13608 row->end.dpvec_index);
13611 if (glyphs > 1)
13613 int area;
13615 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13617 struct glyph *glyph = row->glyphs[area];
13618 struct glyph *glyph_end = glyph + row->used[area];
13620 /* Glyph for a line end in text. */
13621 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13622 ++glyph_end;
13624 if (glyph < glyph_end)
13625 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13627 for (; glyph < glyph_end; ++glyph)
13628 dump_glyph (row, glyph, area);
13631 else if (glyphs == 1)
13633 int area;
13635 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13637 char *s = (char *) alloca (row->used[area] + 1);
13638 int i;
13640 for (i = 0; i < row->used[area]; ++i)
13642 struct glyph *glyph = row->glyphs[area] + i;
13643 if (glyph->type == CHAR_GLYPH
13644 && glyph->u.ch < 0x80
13645 && glyph->u.ch >= ' ')
13646 s[i] = glyph->u.ch;
13647 else
13648 s[i] = '.';
13651 s[i] = '\0';
13652 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13658 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13659 Sdump_glyph_matrix, 0, 1, "p",
13660 doc: /* Dump the current matrix of the selected window to stderr.
13661 Shows contents of glyph row structures. With non-nil
13662 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13663 glyphs in short form, otherwise show glyphs in long form. */)
13664 (glyphs)
13665 Lisp_Object glyphs;
13667 struct window *w = XWINDOW (selected_window);
13668 struct buffer *buffer = XBUFFER (w->buffer);
13670 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13671 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13672 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13673 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13674 fprintf (stderr, "=============================================\n");
13675 dump_glyph_matrix (w->current_matrix,
13676 NILP (glyphs) ? 0 : XINT (glyphs));
13677 return Qnil;
13681 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13682 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13685 struct frame *f = XFRAME (selected_frame);
13686 dump_glyph_matrix (f->current_matrix, 1);
13687 return Qnil;
13691 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13692 doc: /* Dump glyph row ROW to stderr.
13693 GLYPH 0 means don't dump glyphs.
13694 GLYPH 1 means dump glyphs in short form.
13695 GLYPH > 1 or omitted means dump glyphs in long form. */)
13696 (row, glyphs)
13697 Lisp_Object row, glyphs;
13699 struct glyph_matrix *matrix;
13700 int vpos;
13702 CHECK_NUMBER (row);
13703 matrix = XWINDOW (selected_window)->current_matrix;
13704 vpos = XINT (row);
13705 if (vpos >= 0 && vpos < matrix->nrows)
13706 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13707 vpos,
13708 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13709 return Qnil;
13713 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13714 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13715 GLYPH 0 means don't dump glyphs.
13716 GLYPH 1 means dump glyphs in short form.
13717 GLYPH > 1 or omitted means dump glyphs in long form. */)
13718 (row, glyphs)
13719 Lisp_Object row, glyphs;
13721 struct frame *sf = SELECTED_FRAME ();
13722 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13723 int vpos;
13725 CHECK_NUMBER (row);
13726 vpos = XINT (row);
13727 if (vpos >= 0 && vpos < m->nrows)
13728 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13729 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13730 return Qnil;
13734 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13735 doc: /* Toggle tracing of redisplay.
13736 With ARG, turn tracing on if and only if ARG is positive. */)
13737 (arg)
13738 Lisp_Object arg;
13740 if (NILP (arg))
13741 trace_redisplay_p = !trace_redisplay_p;
13742 else
13744 arg = Fprefix_numeric_value (arg);
13745 trace_redisplay_p = XINT (arg) > 0;
13748 return Qnil;
13752 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13753 doc: /* Like `format', but print result to stderr.
13754 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13755 (nargs, args)
13756 int nargs;
13757 Lisp_Object *args;
13759 Lisp_Object s = Fformat (nargs, args);
13760 fprintf (stderr, "%s", SDATA (s));
13761 return Qnil;
13764 #endif /* GLYPH_DEBUG */
13768 /***********************************************************************
13769 Building Desired Matrix Rows
13770 ***********************************************************************/
13772 /* Return a temporary glyph row holding the glyphs of an overlay
13773 arrow. Only used for non-window-redisplay windows. */
13775 static struct glyph_row *
13776 get_overlay_arrow_glyph_row (w)
13777 struct window *w;
13779 struct frame *f = XFRAME (WINDOW_FRAME (w));
13780 struct buffer *buffer = XBUFFER (w->buffer);
13781 struct buffer *old = current_buffer;
13782 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13783 int arrow_len = SCHARS (Voverlay_arrow_string);
13784 const unsigned char *arrow_end = arrow_string + arrow_len;
13785 const unsigned char *p;
13786 struct it it;
13787 int multibyte_p;
13788 int n_glyphs_before;
13790 set_buffer_temp (buffer);
13791 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13792 it.glyph_row->used[TEXT_AREA] = 0;
13793 SET_TEXT_POS (it.position, 0, 0);
13795 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13796 p = arrow_string;
13797 while (p < arrow_end)
13799 Lisp_Object face, ilisp;
13801 /* Get the next character. */
13802 if (multibyte_p)
13803 it.c = string_char_and_length (p, arrow_len, &it.len);
13804 else
13805 it.c = *p, it.len = 1;
13806 p += it.len;
13808 /* Get its face. */
13809 ilisp = make_number (p - arrow_string);
13810 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13811 it.face_id = compute_char_face (f, it.c, face);
13813 /* Compute its width, get its glyphs. */
13814 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13815 SET_TEXT_POS (it.position, -1, -1);
13816 PRODUCE_GLYPHS (&it);
13818 /* If this character doesn't fit any more in the line, we have
13819 to remove some glyphs. */
13820 if (it.current_x > it.last_visible_x)
13822 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13823 break;
13827 set_buffer_temp (old);
13828 return it.glyph_row;
13832 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13833 glyphs are only inserted for terminal frames since we can't really
13834 win with truncation glyphs when partially visible glyphs are
13835 involved. Which glyphs to insert is determined by
13836 produce_special_glyphs. */
13838 static void
13839 insert_left_trunc_glyphs (it)
13840 struct it *it;
13842 struct it truncate_it;
13843 struct glyph *from, *end, *to, *toend;
13845 xassert (!FRAME_WINDOW_P (it->f));
13847 /* Get the truncation glyphs. */
13848 truncate_it = *it;
13849 truncate_it.current_x = 0;
13850 truncate_it.face_id = DEFAULT_FACE_ID;
13851 truncate_it.glyph_row = &scratch_glyph_row;
13852 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13853 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13854 truncate_it.object = make_number (0);
13855 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13857 /* Overwrite glyphs from IT with truncation glyphs. */
13858 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13859 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13860 to = it->glyph_row->glyphs[TEXT_AREA];
13861 toend = to + it->glyph_row->used[TEXT_AREA];
13863 while (from < end)
13864 *to++ = *from++;
13866 /* There may be padding glyphs left over. Overwrite them too. */
13867 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13869 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13870 while (from < end)
13871 *to++ = *from++;
13874 if (to > toend)
13875 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13879 /* Compute the pixel height and width of IT->glyph_row.
13881 Most of the time, ascent and height of a display line will be equal
13882 to the max_ascent and max_height values of the display iterator
13883 structure. This is not the case if
13885 1. We hit ZV without displaying anything. In this case, max_ascent
13886 and max_height will be zero.
13888 2. We have some glyphs that don't contribute to the line height.
13889 (The glyph row flag contributes_to_line_height_p is for future
13890 pixmap extensions).
13892 The first case is easily covered by using default values because in
13893 these cases, the line height does not really matter, except that it
13894 must not be zero. */
13896 static void
13897 compute_line_metrics (it)
13898 struct it *it;
13900 struct glyph_row *row = it->glyph_row;
13901 int area, i;
13903 if (FRAME_WINDOW_P (it->f))
13905 int i, min_y, max_y;
13907 /* The line may consist of one space only, that was added to
13908 place the cursor on it. If so, the row's height hasn't been
13909 computed yet. */
13910 if (row->height == 0)
13912 if (it->max_ascent + it->max_descent == 0)
13913 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
13914 row->ascent = it->max_ascent;
13915 row->height = it->max_ascent + it->max_descent;
13916 row->phys_ascent = it->max_phys_ascent;
13917 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13920 /* Compute the width of this line. */
13921 row->pixel_width = row->x;
13922 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13923 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13925 xassert (row->pixel_width >= 0);
13926 xassert (row->ascent >= 0 && row->height > 0);
13928 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13929 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13931 /* If first line's physical ascent is larger than its logical
13932 ascent, use the physical ascent, and make the row taller.
13933 This makes accented characters fully visible. */
13934 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13935 && row->phys_ascent > row->ascent)
13937 row->height += row->phys_ascent - row->ascent;
13938 row->ascent = row->phys_ascent;
13941 /* Compute how much of the line is visible. */
13942 row->visible_height = row->height;
13944 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
13945 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
13947 if (row->y < min_y)
13948 row->visible_height -= min_y - row->y;
13949 if (row->y + row->height > max_y)
13950 row->visible_height -= row->y + row->height - max_y;
13952 else
13954 row->pixel_width = row->used[TEXT_AREA];
13955 if (row->continued_p)
13956 row->pixel_width -= it->continuation_pixel_width;
13957 else if (row->truncated_on_right_p)
13958 row->pixel_width -= it->truncation_pixel_width;
13959 row->ascent = row->phys_ascent = 0;
13960 row->height = row->phys_height = row->visible_height = 1;
13963 /* Compute a hash code for this row. */
13964 row->hash = 0;
13965 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13966 for (i = 0; i < row->used[area]; ++i)
13967 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13968 + row->glyphs[area][i].u.val
13969 + row->glyphs[area][i].face_id
13970 + row->glyphs[area][i].padding_p
13971 + (row->glyphs[area][i].type << 2));
13973 it->max_ascent = it->max_descent = 0;
13974 it->max_phys_ascent = it->max_phys_descent = 0;
13978 /* Append one space to the glyph row of iterator IT if doing a
13979 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13980 space have the default face, otherwise let it have the same face as
13981 IT->face_id. Value is non-zero if a space was added.
13983 This function is called to make sure that there is always one glyph
13984 at the end of a glyph row that the cursor can be set on under
13985 window-systems. (If there weren't such a glyph we would not know
13986 how wide and tall a box cursor should be displayed).
13988 At the same time this space let's a nicely handle clearing to the
13989 end of the line if the row ends in italic text. */
13991 static int
13992 append_space (it, default_face_p)
13993 struct it *it;
13994 int default_face_p;
13996 if (FRAME_WINDOW_P (it->f))
13998 int n = it->glyph_row->used[TEXT_AREA];
14000 if (it->glyph_row->glyphs[TEXT_AREA] + n
14001 < it->glyph_row->glyphs[1 + TEXT_AREA])
14003 /* Save some values that must not be changed.
14004 Must save IT->c and IT->len because otherwise
14005 ITERATOR_AT_END_P wouldn't work anymore after
14006 append_space has been called. */
14007 enum display_element_type saved_what = it->what;
14008 int saved_c = it->c, saved_len = it->len;
14009 int saved_x = it->current_x;
14010 int saved_face_id = it->face_id;
14011 struct text_pos saved_pos;
14012 Lisp_Object saved_object;
14013 struct face *face;
14015 saved_object = it->object;
14016 saved_pos = it->position;
14018 it->what = IT_CHARACTER;
14019 bzero (&it->position, sizeof it->position);
14020 it->object = make_number (0);
14021 it->c = ' ';
14022 it->len = 1;
14024 if (default_face_p)
14025 it->face_id = DEFAULT_FACE_ID;
14026 else if (it->face_before_selective_p)
14027 it->face_id = it->saved_face_id;
14028 face = FACE_FROM_ID (it->f, it->face_id);
14029 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14031 PRODUCE_GLYPHS (it);
14033 it->current_x = saved_x;
14034 it->object = saved_object;
14035 it->position = saved_pos;
14036 it->what = saved_what;
14037 it->face_id = saved_face_id;
14038 it->len = saved_len;
14039 it->c = saved_c;
14040 return 1;
14044 return 0;
14048 /* Extend the face of the last glyph in the text area of IT->glyph_row
14049 to the end of the display line. Called from display_line.
14050 If the glyph row is empty, add a space glyph to it so that we
14051 know the face to draw. Set the glyph row flag fill_line_p. */
14053 static void
14054 extend_face_to_end_of_line (it)
14055 struct it *it;
14057 struct face *face;
14058 struct frame *f = it->f;
14060 /* If line is already filled, do nothing. */
14061 if (it->current_x >= it->last_visible_x)
14062 return;
14064 /* Face extension extends the background and box of IT->face_id
14065 to the end of the line. If the background equals the background
14066 of the frame, we don't have to do anything. */
14067 if (it->face_before_selective_p)
14068 face = FACE_FROM_ID (it->f, it->saved_face_id);
14069 else
14070 face = FACE_FROM_ID (f, it->face_id);
14072 if (FRAME_WINDOW_P (f)
14073 && face->box == FACE_NO_BOX
14074 && face->background == FRAME_BACKGROUND_PIXEL (f)
14075 && !face->stipple)
14076 return;
14078 /* Set the glyph row flag indicating that the face of the last glyph
14079 in the text area has to be drawn to the end of the text area. */
14080 it->glyph_row->fill_line_p = 1;
14082 /* If current character of IT is not ASCII, make sure we have the
14083 ASCII face. This will be automatically undone the next time
14084 get_next_display_element returns a multibyte character. Note
14085 that the character will always be single byte in unibyte text. */
14086 if (!SINGLE_BYTE_CHAR_P (it->c))
14088 it->face_id = FACE_FOR_CHAR (f, face, 0);
14091 if (FRAME_WINDOW_P (f))
14093 /* If the row is empty, add a space with the current face of IT,
14094 so that we know which face to draw. */
14095 if (it->glyph_row->used[TEXT_AREA] == 0)
14097 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14098 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14099 it->glyph_row->used[TEXT_AREA] = 1;
14102 else
14104 /* Save some values that must not be changed. */
14105 int saved_x = it->current_x;
14106 struct text_pos saved_pos;
14107 Lisp_Object saved_object;
14108 enum display_element_type saved_what = it->what;
14109 int saved_face_id = it->face_id;
14111 saved_object = it->object;
14112 saved_pos = it->position;
14114 it->what = IT_CHARACTER;
14115 bzero (&it->position, sizeof it->position);
14116 it->object = make_number (0);
14117 it->c = ' ';
14118 it->len = 1;
14119 it->face_id = face->id;
14121 PRODUCE_GLYPHS (it);
14123 while (it->current_x <= it->last_visible_x)
14124 PRODUCE_GLYPHS (it);
14126 /* Don't count these blanks really. It would let us insert a left
14127 truncation glyph below and make us set the cursor on them, maybe. */
14128 it->current_x = saved_x;
14129 it->object = saved_object;
14130 it->position = saved_pos;
14131 it->what = saved_what;
14132 it->face_id = saved_face_id;
14137 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14138 trailing whitespace. */
14140 static int
14141 trailing_whitespace_p (charpos)
14142 int charpos;
14144 int bytepos = CHAR_TO_BYTE (charpos);
14145 int c = 0;
14147 while (bytepos < ZV_BYTE
14148 && (c = FETCH_CHAR (bytepos),
14149 c == ' ' || c == '\t'))
14150 ++bytepos;
14152 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14154 if (bytepos != PT_BYTE)
14155 return 1;
14157 return 0;
14161 /* Highlight trailing whitespace, if any, in ROW. */
14163 void
14164 highlight_trailing_whitespace (f, row)
14165 struct frame *f;
14166 struct glyph_row *row;
14168 int used = row->used[TEXT_AREA];
14170 if (used)
14172 struct glyph *start = row->glyphs[TEXT_AREA];
14173 struct glyph *glyph = start + used - 1;
14175 /* Skip over glyphs inserted to display the cursor at the
14176 end of a line, for extending the face of the last glyph
14177 to the end of the line on terminals, and for truncation
14178 and continuation glyphs. */
14179 while (glyph >= start
14180 && glyph->type == CHAR_GLYPH
14181 && INTEGERP (glyph->object))
14182 --glyph;
14184 /* If last glyph is a space or stretch, and it's trailing
14185 whitespace, set the face of all trailing whitespace glyphs in
14186 IT->glyph_row to `trailing-whitespace'. */
14187 if (glyph >= start
14188 && BUFFERP (glyph->object)
14189 && (glyph->type == STRETCH_GLYPH
14190 || (glyph->type == CHAR_GLYPH
14191 && glyph->u.ch == ' '))
14192 && trailing_whitespace_p (glyph->charpos))
14194 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14196 while (glyph >= start
14197 && BUFFERP (glyph->object)
14198 && (glyph->type == STRETCH_GLYPH
14199 || (glyph->type == CHAR_GLYPH
14200 && glyph->u.ch == ' ')))
14201 (glyph--)->face_id = face_id;
14207 /* Value is non-zero if glyph row ROW in window W should be
14208 used to hold the cursor. */
14210 static int
14211 cursor_row_p (w, row)
14212 struct window *w;
14213 struct glyph_row *row;
14215 int cursor_row_p = 1;
14217 if (PT == MATRIX_ROW_END_CHARPOS (row))
14219 /* If the row ends with a newline from a string, we don't want
14220 the cursor there (if the row is continued it doesn't end in a
14221 newline). */
14222 if (CHARPOS (row->end.string_pos) >= 0
14223 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14224 cursor_row_p = row->continued_p;
14226 /* If the row ends at ZV, display the cursor at the end of that
14227 row instead of at the start of the row below. */
14228 else if (row->ends_at_zv_p)
14229 cursor_row_p = 1;
14230 else
14231 cursor_row_p = 0;
14234 return cursor_row_p;
14238 /* Construct the glyph row IT->glyph_row in the desired matrix of
14239 IT->w from text at the current position of IT. See dispextern.h
14240 for an overview of struct it. Value is non-zero if
14241 IT->glyph_row displays text, as opposed to a line displaying ZV
14242 only. */
14244 static int
14245 display_line (it)
14246 struct it *it;
14248 struct glyph_row *row = it->glyph_row;
14250 /* We always start displaying at hpos zero even if hscrolled. */
14251 xassert (it->hpos == 0 && it->current_x == 0);
14253 /* We must not display in a row that's not a text row. */
14254 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14255 < it->w->desired_matrix->nrows);
14257 /* Is IT->w showing the region? */
14258 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14260 /* Clear the result glyph row and enable it. */
14261 prepare_desired_row (row);
14263 row->y = it->current_y;
14264 row->start = it->current;
14265 row->continuation_lines_width = it->continuation_lines_width;
14266 row->displays_text_p = 1;
14267 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14268 it->starts_in_middle_of_char_p = 0;
14270 /* Arrange the overlays nicely for our purposes. Usually, we call
14271 display_line on only one line at a time, in which case this
14272 can't really hurt too much, or we call it on lines which appear
14273 one after another in the buffer, in which case all calls to
14274 recenter_overlay_lists but the first will be pretty cheap. */
14275 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14277 /* Move over display elements that are not visible because we are
14278 hscrolled. This may stop at an x-position < IT->first_visible_x
14279 if the first glyph is partially visible or if we hit a line end. */
14280 if (it->current_x < it->first_visible_x)
14281 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14282 MOVE_TO_POS | MOVE_TO_X);
14284 /* Get the initial row height. This is either the height of the
14285 text hscrolled, if there is any, or zero. */
14286 row->ascent = it->max_ascent;
14287 row->height = it->max_ascent + it->max_descent;
14288 row->phys_ascent = it->max_phys_ascent;
14289 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14291 /* Loop generating characters. The loop is left with IT on the next
14292 character to display. */
14293 while (1)
14295 int n_glyphs_before, hpos_before, x_before;
14296 int x, i, nglyphs;
14297 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14299 /* Retrieve the next thing to display. Value is zero if end of
14300 buffer reached. */
14301 if (!get_next_display_element (it))
14303 /* Maybe add a space at the end of this line that is used to
14304 display the cursor there under X. Set the charpos of the
14305 first glyph of blank lines not corresponding to any text
14306 to -1. */
14307 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14308 || row->used[TEXT_AREA] == 0)
14310 row->glyphs[TEXT_AREA]->charpos = -1;
14311 row->displays_text_p = 0;
14313 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14314 && (!MINI_WINDOW_P (it->w)
14315 || (minibuf_level && EQ (it->window, minibuf_window))))
14316 row->indicate_empty_line_p = 1;
14319 it->continuation_lines_width = 0;
14320 row->ends_at_zv_p = 1;
14321 break;
14324 /* Now, get the metrics of what we want to display. This also
14325 generates glyphs in `row' (which is IT->glyph_row). */
14326 n_glyphs_before = row->used[TEXT_AREA];
14327 x = it->current_x;
14329 /* Remember the line height so far in case the next element doesn't
14330 fit on the line. */
14331 if (!it->truncate_lines_p)
14333 ascent = it->max_ascent;
14334 descent = it->max_descent;
14335 phys_ascent = it->max_phys_ascent;
14336 phys_descent = it->max_phys_descent;
14339 PRODUCE_GLYPHS (it);
14341 /* If this display element was in marginal areas, continue with
14342 the next one. */
14343 if (it->area != TEXT_AREA)
14345 row->ascent = max (row->ascent, it->max_ascent);
14346 row->height = max (row->height, it->max_ascent + it->max_descent);
14347 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14348 row->phys_height = max (row->phys_height,
14349 it->max_phys_ascent + it->max_phys_descent);
14350 set_iterator_to_next (it, 1);
14351 continue;
14354 /* Does the display element fit on the line? If we truncate
14355 lines, we should draw past the right edge of the window. If
14356 we don't truncate, we want to stop so that we can display the
14357 continuation glyph before the right margin. If lines are
14358 continued, there are two possible strategies for characters
14359 resulting in more than 1 glyph (e.g. tabs): Display as many
14360 glyphs as possible in this line and leave the rest for the
14361 continuation line, or display the whole element in the next
14362 line. Original redisplay did the former, so we do it also. */
14363 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14364 hpos_before = it->hpos;
14365 x_before = x;
14367 if (/* Not a newline. */
14368 nglyphs > 0
14369 /* Glyphs produced fit entirely in the line. */
14370 && it->current_x < it->last_visible_x)
14372 it->hpos += nglyphs;
14373 row->ascent = max (row->ascent, it->max_ascent);
14374 row->height = max (row->height, it->max_ascent + it->max_descent);
14375 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14376 row->phys_height = max (row->phys_height,
14377 it->max_phys_ascent + it->max_phys_descent);
14378 if (it->current_x - it->pixel_width < it->first_visible_x)
14379 row->x = x - it->first_visible_x;
14381 else
14383 int new_x;
14384 struct glyph *glyph;
14386 for (i = 0; i < nglyphs; ++i, x = new_x)
14388 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14389 new_x = x + glyph->pixel_width;
14391 if (/* Lines are continued. */
14392 !it->truncate_lines_p
14393 && (/* Glyph doesn't fit on the line. */
14394 new_x > it->last_visible_x
14395 /* Or it fits exactly on a window system frame. */
14396 || (new_x == it->last_visible_x
14397 && FRAME_WINDOW_P (it->f))))
14399 /* End of a continued line. */
14401 if (it->hpos == 0
14402 || (new_x == it->last_visible_x
14403 && FRAME_WINDOW_P (it->f)))
14405 /* Current glyph is the only one on the line or
14406 fits exactly on the line. We must continue
14407 the line because we can't draw the cursor
14408 after the glyph. */
14409 row->continued_p = 1;
14410 it->current_x = new_x;
14411 it->continuation_lines_width += new_x;
14412 ++it->hpos;
14413 if (i == nglyphs - 1)
14414 set_iterator_to_next (it, 1);
14416 else if (CHAR_GLYPH_PADDING_P (*glyph)
14417 && !FRAME_WINDOW_P (it->f))
14419 /* A padding glyph that doesn't fit on this line.
14420 This means the whole character doesn't fit
14421 on the line. */
14422 row->used[TEXT_AREA] = n_glyphs_before;
14424 /* Fill the rest of the row with continuation
14425 glyphs like in 20.x. */
14426 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14427 < row->glyphs[1 + TEXT_AREA])
14428 produce_special_glyphs (it, IT_CONTINUATION);
14430 row->continued_p = 1;
14431 it->current_x = x_before;
14432 it->continuation_lines_width += x_before;
14434 /* Restore the height to what it was before the
14435 element not fitting on the line. */
14436 it->max_ascent = ascent;
14437 it->max_descent = descent;
14438 it->max_phys_ascent = phys_ascent;
14439 it->max_phys_descent = phys_descent;
14441 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14443 /* A TAB that extends past the right edge of the
14444 window. This produces a single glyph on
14445 window system frames. We leave the glyph in
14446 this row and let it fill the row, but don't
14447 consume the TAB. */
14448 it->continuation_lines_width += it->last_visible_x;
14449 row->ends_in_middle_of_char_p = 1;
14450 row->continued_p = 1;
14451 glyph->pixel_width = it->last_visible_x - x;
14452 it->starts_in_middle_of_char_p = 1;
14454 else
14456 /* Something other than a TAB that draws past
14457 the right edge of the window. Restore
14458 positions to values before the element. */
14459 row->used[TEXT_AREA] = n_glyphs_before + i;
14461 /* Display continuation glyphs. */
14462 if (!FRAME_WINDOW_P (it->f))
14463 produce_special_glyphs (it, IT_CONTINUATION);
14464 row->continued_p = 1;
14466 it->continuation_lines_width += x;
14468 if (nglyphs > 1 && i > 0)
14470 row->ends_in_middle_of_char_p = 1;
14471 it->starts_in_middle_of_char_p = 1;
14474 /* Restore the height to what it was before the
14475 element not fitting on the line. */
14476 it->max_ascent = ascent;
14477 it->max_descent = descent;
14478 it->max_phys_ascent = phys_ascent;
14479 it->max_phys_descent = phys_descent;
14482 break;
14484 else if (new_x > it->first_visible_x)
14486 /* Increment number of glyphs actually displayed. */
14487 ++it->hpos;
14489 if (x < it->first_visible_x)
14490 /* Glyph is partially visible, i.e. row starts at
14491 negative X position. */
14492 row->x = x - it->first_visible_x;
14494 else
14496 /* Glyph is completely off the left margin of the
14497 window. This should not happen because of the
14498 move_it_in_display_line at the start of this
14499 function, unless the text display area of the
14500 window is empty. */
14501 xassert (it->first_visible_x <= it->last_visible_x);
14505 row->ascent = max (row->ascent, it->max_ascent);
14506 row->height = max (row->height, it->max_ascent + it->max_descent);
14507 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14508 row->phys_height = max (row->phys_height,
14509 it->max_phys_ascent + it->max_phys_descent);
14511 /* End of this display line if row is continued. */
14512 if (row->continued_p)
14513 break;
14516 /* Is this a line end? If yes, we're also done, after making
14517 sure that a non-default face is extended up to the right
14518 margin of the window. */
14519 if (ITERATOR_AT_END_OF_LINE_P (it))
14521 int used_before = row->used[TEXT_AREA];
14523 row->ends_in_newline_from_string_p = STRINGP (it->object);
14525 /* Add a space at the end of the line that is used to
14526 display the cursor there. */
14527 append_space (it, 0);
14529 /* Extend the face to the end of the line. */
14530 extend_face_to_end_of_line (it);
14532 /* Make sure we have the position. */
14533 if (used_before == 0)
14534 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14536 /* Consume the line end. This skips over invisible lines. */
14537 set_iterator_to_next (it, 1);
14538 it->continuation_lines_width = 0;
14539 break;
14542 /* Proceed with next display element. Note that this skips
14543 over lines invisible because of selective display. */
14544 set_iterator_to_next (it, 1);
14546 /* If we truncate lines, we are done when the last displayed
14547 glyphs reach past the right margin of the window. */
14548 if (it->truncate_lines_p
14549 && (FRAME_WINDOW_P (it->f)
14550 ? (it->current_x >= it->last_visible_x)
14551 : (it->current_x > it->last_visible_x)))
14553 /* Maybe add truncation glyphs. */
14554 if (!FRAME_WINDOW_P (it->f))
14556 int i, n;
14558 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14559 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14560 break;
14562 for (n = row->used[TEXT_AREA]; i < n; ++i)
14564 row->used[TEXT_AREA] = i;
14565 produce_special_glyphs (it, IT_TRUNCATION);
14569 row->truncated_on_right_p = 1;
14570 it->continuation_lines_width = 0;
14571 reseat_at_next_visible_line_start (it, 0);
14572 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14573 it->hpos = hpos_before;
14574 it->current_x = x_before;
14575 break;
14579 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14580 at the left window margin. */
14581 if (it->first_visible_x
14582 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14584 if (!FRAME_WINDOW_P (it->f))
14585 insert_left_trunc_glyphs (it);
14586 row->truncated_on_left_p = 1;
14589 /* If the start of this line is the overlay arrow-position, then
14590 mark this glyph row as the one containing the overlay arrow.
14591 This is clearly a mess with variable size fonts. It would be
14592 better to let it be displayed like cursors under X. */
14593 if (MARKERP (Voverlay_arrow_position)
14594 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14595 && (MATRIX_ROW_START_CHARPOS (row)
14596 == marker_position (Voverlay_arrow_position))
14597 && STRINGP (Voverlay_arrow_string)
14598 && ! overlay_arrow_seen)
14600 /* Overlay arrow in window redisplay is a fringe bitmap. */
14601 if (!FRAME_WINDOW_P (it->f))
14603 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14604 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14605 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14606 struct glyph *p = row->glyphs[TEXT_AREA];
14607 struct glyph *p2, *end;
14609 /* Copy the arrow glyphs. */
14610 while (glyph < arrow_end)
14611 *p++ = *glyph++;
14613 /* Throw away padding glyphs. */
14614 p2 = p;
14615 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14616 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14617 ++p2;
14618 if (p2 > p)
14620 while (p2 < end)
14621 *p++ = *p2++;
14622 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14626 overlay_arrow_seen = 1;
14627 row->overlay_arrow_p = 1;
14630 /* Compute pixel dimensions of this line. */
14631 compute_line_metrics (it);
14633 /* Remember the position at which this line ends. */
14634 row->end = it->current;
14636 /* Maybe set the cursor. */
14637 if (it->w->cursor.vpos < 0
14638 && PT >= MATRIX_ROW_START_CHARPOS (row)
14639 && PT <= MATRIX_ROW_END_CHARPOS (row)
14640 && cursor_row_p (it->w, row))
14641 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14643 /* Highlight trailing whitespace. */
14644 if (!NILP (Vshow_trailing_whitespace))
14645 highlight_trailing_whitespace (it->f, it->glyph_row);
14647 /* Prepare for the next line. This line starts horizontally at (X
14648 HPOS) = (0 0). Vertical positions are incremented. As a
14649 convenience for the caller, IT->glyph_row is set to the next
14650 row to be used. */
14651 it->current_x = it->hpos = 0;
14652 it->current_y += row->height;
14653 ++it->vpos;
14654 ++it->glyph_row;
14655 return row->displays_text_p;
14660 /***********************************************************************
14661 Menu Bar
14662 ***********************************************************************/
14664 /* Redisplay the menu bar in the frame for window W.
14666 The menu bar of X frames that don't have X toolkit support is
14667 displayed in a special window W->frame->menu_bar_window.
14669 The menu bar of terminal frames is treated specially as far as
14670 glyph matrices are concerned. Menu bar lines are not part of
14671 windows, so the update is done directly on the frame matrix rows
14672 for the menu bar. */
14674 static void
14675 display_menu_bar (w)
14676 struct window *w;
14678 struct frame *f = XFRAME (WINDOW_FRAME (w));
14679 struct it it;
14680 Lisp_Object items;
14681 int i;
14683 /* Don't do all this for graphical frames. */
14684 #ifdef HAVE_NTGUI
14685 if (!NILP (Vwindow_system))
14686 return;
14687 #endif
14688 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14689 if (FRAME_X_P (f))
14690 return;
14691 #endif
14692 #ifdef MAC_OS
14693 if (FRAME_MAC_P (f))
14694 return;
14695 #endif
14697 #ifdef USE_X_TOOLKIT
14698 xassert (!FRAME_WINDOW_P (f));
14699 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14700 it.first_visible_x = 0;
14701 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14702 #else /* not USE_X_TOOLKIT */
14703 if (FRAME_WINDOW_P (f))
14705 /* Menu bar lines are displayed in the desired matrix of the
14706 dummy window menu_bar_window. */
14707 struct window *menu_w;
14708 xassert (WINDOWP (f->menu_bar_window));
14709 menu_w = XWINDOW (f->menu_bar_window);
14710 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14711 MENU_FACE_ID);
14712 it.first_visible_x = 0;
14713 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
14715 else
14717 /* This is a TTY frame, i.e. character hpos/vpos are used as
14718 pixel x/y. */
14719 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14720 MENU_FACE_ID);
14721 it.first_visible_x = 0;
14722 it.last_visible_x = FRAME_COLS (f);
14724 #endif /* not USE_X_TOOLKIT */
14726 if (! mode_line_inverse_video)
14727 /* Force the menu-bar to be displayed in the default face. */
14728 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14730 /* Clear all rows of the menu bar. */
14731 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14733 struct glyph_row *row = it.glyph_row + i;
14734 clear_glyph_row (row);
14735 row->enabled_p = 1;
14736 row->full_width_p = 1;
14739 /* Display all items of the menu bar. */
14740 items = FRAME_MENU_BAR_ITEMS (it.f);
14741 for (i = 0; i < XVECTOR (items)->size; i += 4)
14743 Lisp_Object string;
14745 /* Stop at nil string. */
14746 string = AREF (items, i + 1);
14747 if (NILP (string))
14748 break;
14750 /* Remember where item was displayed. */
14751 AREF (items, i + 3) = make_number (it.hpos);
14753 /* Display the item, pad with one space. */
14754 if (it.current_x < it.last_visible_x)
14755 display_string (NULL, string, Qnil, 0, 0, &it,
14756 SCHARS (string) + 1, 0, 0, -1);
14759 /* Fill out the line with spaces. */
14760 if (it.current_x < it.last_visible_x)
14761 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14763 /* Compute the total height of the lines. */
14764 compute_line_metrics (&it);
14769 /***********************************************************************
14770 Mode Line
14771 ***********************************************************************/
14773 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14774 FORCE is non-zero, redisplay mode lines unconditionally.
14775 Otherwise, redisplay only mode lines that are garbaged. Value is
14776 the number of windows whose mode lines were redisplayed. */
14778 static int
14779 redisplay_mode_lines (window, force)
14780 Lisp_Object window;
14781 int force;
14783 int nwindows = 0;
14785 while (!NILP (window))
14787 struct window *w = XWINDOW (window);
14789 if (WINDOWP (w->hchild))
14790 nwindows += redisplay_mode_lines (w->hchild, force);
14791 else if (WINDOWP (w->vchild))
14792 nwindows += redisplay_mode_lines (w->vchild, force);
14793 else if (force
14794 || FRAME_GARBAGED_P (XFRAME (w->frame))
14795 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14797 struct text_pos lpoint;
14798 struct buffer *old = current_buffer;
14800 /* Set the window's buffer for the mode line display. */
14801 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14802 set_buffer_internal_1 (XBUFFER (w->buffer));
14804 /* Point refers normally to the selected window. For any
14805 other window, set up appropriate value. */
14806 if (!EQ (window, selected_window))
14808 struct text_pos pt;
14810 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14811 if (CHARPOS (pt) < BEGV)
14812 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14813 else if (CHARPOS (pt) > (ZV - 1))
14814 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14815 else
14816 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14819 /* Display mode lines. */
14820 clear_glyph_matrix (w->desired_matrix);
14821 if (display_mode_lines (w))
14823 ++nwindows;
14824 w->must_be_updated_p = 1;
14827 /* Restore old settings. */
14828 set_buffer_internal_1 (old);
14829 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14832 window = w->next;
14835 return nwindows;
14839 /* Display the mode and/or top line of window W. Value is the number
14840 of mode lines displayed. */
14842 static int
14843 display_mode_lines (w)
14844 struct window *w;
14846 Lisp_Object old_selected_window, old_selected_frame;
14847 int n = 0;
14849 old_selected_frame = selected_frame;
14850 selected_frame = w->frame;
14851 old_selected_window = selected_window;
14852 XSETWINDOW (selected_window, w);
14854 /* These will be set while the mode line specs are processed. */
14855 line_number_displayed = 0;
14856 w->column_number_displayed = Qnil;
14858 if (WINDOW_WANTS_MODELINE_P (w))
14860 struct window *sel_w = XWINDOW (old_selected_window);
14862 /* Select mode line face based on the real selected window. */
14863 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14864 current_buffer->mode_line_format);
14865 ++n;
14868 if (WINDOW_WANTS_HEADER_LINE_P (w))
14870 display_mode_line (w, HEADER_LINE_FACE_ID,
14871 current_buffer->header_line_format);
14872 ++n;
14875 selected_frame = old_selected_frame;
14876 selected_window = old_selected_window;
14877 return n;
14881 /* Display mode or top line of window W. FACE_ID specifies which line
14882 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14883 FORMAT is the mode line format to display. Value is the pixel
14884 height of the mode line displayed. */
14886 static int
14887 display_mode_line (w, face_id, format)
14888 struct window *w;
14889 enum face_id face_id;
14890 Lisp_Object format;
14892 struct it it;
14893 struct face *face;
14895 init_iterator (&it, w, -1, -1, NULL, face_id);
14896 prepare_desired_row (it.glyph_row);
14898 if (! mode_line_inverse_video)
14899 /* Force the mode-line to be displayed in the default face. */
14900 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14902 /* Temporarily make frame's keyboard the current kboard so that
14903 kboard-local variables in the mode_line_format will get the right
14904 values. */
14905 push_frame_kboard (it.f);
14906 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14907 pop_frame_kboard ();
14909 /* Fill up with spaces. */
14910 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14912 compute_line_metrics (&it);
14913 it.glyph_row->full_width_p = 1;
14914 it.glyph_row->mode_line_p = 1;
14915 it.glyph_row->continued_p = 0;
14916 it.glyph_row->truncated_on_left_p = 0;
14917 it.glyph_row->truncated_on_right_p = 0;
14919 /* Make a 3D mode-line have a shadow at its right end. */
14920 face = FACE_FROM_ID (it.f, face_id);
14921 extend_face_to_end_of_line (&it);
14922 if (face->box != FACE_NO_BOX)
14924 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14925 + it.glyph_row->used[TEXT_AREA] - 1);
14926 last->right_box_line_p = 1;
14929 return it.glyph_row->height;
14932 /* Alist that caches the results of :propertize.
14933 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14934 Lisp_Object mode_line_proptrans_alist;
14936 /* List of strings making up the mode-line. */
14937 Lisp_Object mode_line_string_list;
14939 /* Base face property when building propertized mode line string. */
14940 static Lisp_Object mode_line_string_face;
14941 static Lisp_Object mode_line_string_face_prop;
14944 /* Contribute ELT to the mode line for window IT->w. How it
14945 translates into text depends on its data type.
14947 IT describes the display environment in which we display, as usual.
14949 DEPTH is the depth in recursion. It is used to prevent
14950 infinite recursion here.
14952 FIELD_WIDTH is the number of characters the display of ELT should
14953 occupy in the mode line, and PRECISION is the maximum number of
14954 characters to display from ELT's representation. See
14955 display_string for details.
14957 Returns the hpos of the end of the text generated by ELT.
14959 PROPS is a property list to add to any string we encounter.
14961 If RISKY is nonzero, remove (disregard) any properties in any string
14962 we encounter, and ignore :eval and :propertize.
14964 If the global variable `frame_title_ptr' is non-NULL, then the output
14965 is passed to `store_frame_title' instead of `display_string'. */
14967 static int
14968 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14969 struct it *it;
14970 int depth;
14971 int field_width, precision;
14972 Lisp_Object elt, props;
14973 int risky;
14975 int n = 0, field, prec;
14976 int literal = 0;
14978 tail_recurse:
14979 if (depth > 100)
14980 elt = build_string ("*too-deep*");
14982 depth++;
14984 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14986 case Lisp_String:
14988 /* A string: output it and check for %-constructs within it. */
14989 unsigned char c;
14990 const unsigned char *this, *lisp_string;
14992 if (!NILP (props) || risky)
14994 Lisp_Object oprops, aelt;
14995 oprops = Ftext_properties_at (make_number (0), elt);
14997 if (NILP (Fequal (props, oprops)) || risky)
14999 /* If the starting string has properties,
15000 merge the specified ones onto the existing ones. */
15001 if (! NILP (oprops) && !risky)
15003 Lisp_Object tem;
15005 oprops = Fcopy_sequence (oprops);
15006 tem = props;
15007 while (CONSP (tem))
15009 oprops = Fplist_put (oprops, XCAR (tem),
15010 XCAR (XCDR (tem)));
15011 tem = XCDR (XCDR (tem));
15013 props = oprops;
15016 aelt = Fassoc (elt, mode_line_proptrans_alist);
15017 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15019 mode_line_proptrans_alist
15020 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15021 elt = XCAR (aelt);
15023 else
15025 Lisp_Object tem;
15027 elt = Fcopy_sequence (elt);
15028 Fset_text_properties (make_number (0), Flength (elt),
15029 props, elt);
15030 /* Add this item to mode_line_proptrans_alist. */
15031 mode_line_proptrans_alist
15032 = Fcons (Fcons (elt, props),
15033 mode_line_proptrans_alist);
15034 /* Truncate mode_line_proptrans_alist
15035 to at most 50 elements. */
15036 tem = Fnthcdr (make_number (50),
15037 mode_line_proptrans_alist);
15038 if (! NILP (tem))
15039 XSETCDR (tem, Qnil);
15044 this = SDATA (elt);
15045 lisp_string = this;
15047 if (literal)
15049 prec = precision - n;
15050 if (frame_title_ptr)
15051 n += store_frame_title (SDATA (elt), -1, prec);
15052 else if (!NILP (mode_line_string_list))
15053 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15054 else
15055 n += display_string (NULL, elt, Qnil, 0, 0, it,
15056 0, prec, 0, STRING_MULTIBYTE (elt));
15058 break;
15061 while ((precision <= 0 || n < precision)
15062 && *this
15063 && (frame_title_ptr
15064 || !NILP (mode_line_string_list)
15065 || it->current_x < it->last_visible_x))
15067 const unsigned char *last = this;
15069 /* Advance to end of string or next format specifier. */
15070 while ((c = *this++) != '\0' && c != '%')
15073 if (this - 1 != last)
15075 /* Output to end of string or up to '%'. Field width
15076 is length of string. Don't output more than
15077 PRECISION allows us. */
15078 --this;
15080 prec = chars_in_text (last, this - last);
15081 if (precision > 0 && prec > precision - n)
15082 prec = precision - n;
15084 if (frame_title_ptr)
15085 n += store_frame_title (last, 0, prec);
15086 else if (!NILP (mode_line_string_list))
15088 int bytepos = last - lisp_string;
15089 int charpos = string_byte_to_char (elt, bytepos);
15090 n += store_mode_line_string (NULL,
15091 Fsubstring (elt, make_number (charpos),
15092 make_number (charpos + prec)),
15093 0, 0, 0, Qnil);
15095 else
15097 int bytepos = last - lisp_string;
15098 int charpos = string_byte_to_char (elt, bytepos);
15099 n += display_string (NULL, elt, Qnil, 0, charpos,
15100 it, 0, prec, 0,
15101 STRING_MULTIBYTE (elt));
15104 else /* c == '%' */
15106 const unsigned char *percent_position = this;
15108 /* Get the specified minimum width. Zero means
15109 don't pad. */
15110 field = 0;
15111 while ((c = *this++) >= '0' && c <= '9')
15112 field = field * 10 + c - '0';
15114 /* Don't pad beyond the total padding allowed. */
15115 if (field_width - n > 0 && field > field_width - n)
15116 field = field_width - n;
15118 /* Note that either PRECISION <= 0 or N < PRECISION. */
15119 prec = precision - n;
15121 if (c == 'M')
15122 n += display_mode_element (it, depth, field, prec,
15123 Vglobal_mode_string, props,
15124 risky);
15125 else if (c != 0)
15127 int multibyte;
15128 int bytepos, charpos;
15129 unsigned char *spec;
15131 bytepos = percent_position - lisp_string;
15132 charpos = (STRING_MULTIBYTE (elt)
15133 ? string_byte_to_char (elt, bytepos)
15134 : bytepos);
15136 spec
15137 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15139 if (frame_title_ptr)
15140 n += store_frame_title (spec, field, prec);
15141 else if (!NILP (mode_line_string_list))
15143 int len = strlen (spec);
15144 Lisp_Object tem = make_string (spec, len);
15145 props = Ftext_properties_at (make_number (charpos), elt);
15146 /* Should only keep face property in props */
15147 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15149 else
15151 int nglyphs_before, nwritten;
15153 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15154 nwritten = display_string (spec, Qnil, elt,
15155 charpos, 0, it,
15156 field, prec, 0,
15157 multibyte);
15159 /* Assign to the glyphs written above the
15160 string where the `%x' came from, position
15161 of the `%'. */
15162 if (nwritten > 0)
15164 struct glyph *glyph
15165 = (it->glyph_row->glyphs[TEXT_AREA]
15166 + nglyphs_before);
15167 int i;
15169 for (i = 0; i < nwritten; ++i)
15171 glyph[i].object = elt;
15172 glyph[i].charpos = charpos;
15175 n += nwritten;
15179 else /* c == 0 */
15180 break;
15184 break;
15186 case Lisp_Symbol:
15187 /* A symbol: process the value of the symbol recursively
15188 as if it appeared here directly. Avoid error if symbol void.
15189 Special case: if value of symbol is a string, output the string
15190 literally. */
15192 register Lisp_Object tem;
15194 /* If the variable is not marked as risky to set
15195 then its contents are risky to use. */
15196 if (NILP (Fget (elt, Qrisky_local_variable)))
15197 risky = 1;
15199 tem = Fboundp (elt);
15200 if (!NILP (tem))
15202 tem = Fsymbol_value (elt);
15203 /* If value is a string, output that string literally:
15204 don't check for % within it. */
15205 if (STRINGP (tem))
15206 literal = 1;
15208 if (!EQ (tem, elt))
15210 /* Give up right away for nil or t. */
15211 elt = tem;
15212 goto tail_recurse;
15216 break;
15218 case Lisp_Cons:
15220 register Lisp_Object car, tem;
15222 /* A cons cell: five distinct cases.
15223 If first element is :eval or :propertize, do something special.
15224 If first element is a string or a cons, process all the elements
15225 and effectively concatenate them.
15226 If first element is a negative number, truncate displaying cdr to
15227 at most that many characters. If positive, pad (with spaces)
15228 to at least that many characters.
15229 If first element is a symbol, process the cadr or caddr recursively
15230 according to whether the symbol's value is non-nil or nil. */
15231 car = XCAR (elt);
15232 if (EQ (car, QCeval))
15234 /* An element of the form (:eval FORM) means evaluate FORM
15235 and use the result as mode line elements. */
15237 if (risky)
15238 break;
15240 if (CONSP (XCDR (elt)))
15242 Lisp_Object spec;
15243 spec = safe_eval (XCAR (XCDR (elt)));
15244 n += display_mode_element (it, depth, field_width - n,
15245 precision - n, spec, props,
15246 risky);
15249 else if (EQ (car, QCpropertize))
15251 /* An element of the form (:propertize ELT PROPS...)
15252 means display ELT but applying properties PROPS. */
15254 if (risky)
15255 break;
15257 if (CONSP (XCDR (elt)))
15258 n += display_mode_element (it, depth, field_width - n,
15259 precision - n, XCAR (XCDR (elt)),
15260 XCDR (XCDR (elt)), risky);
15262 else if (SYMBOLP (car))
15264 tem = Fboundp (car);
15265 elt = XCDR (elt);
15266 if (!CONSP (elt))
15267 goto invalid;
15268 /* elt is now the cdr, and we know it is a cons cell.
15269 Use its car if CAR has a non-nil value. */
15270 if (!NILP (tem))
15272 tem = Fsymbol_value (car);
15273 if (!NILP (tem))
15275 elt = XCAR (elt);
15276 goto tail_recurse;
15279 /* Symbol's value is nil (or symbol is unbound)
15280 Get the cddr of the original list
15281 and if possible find the caddr and use that. */
15282 elt = XCDR (elt);
15283 if (NILP (elt))
15284 break;
15285 else if (!CONSP (elt))
15286 goto invalid;
15287 elt = XCAR (elt);
15288 goto tail_recurse;
15290 else if (INTEGERP (car))
15292 register int lim = XINT (car);
15293 elt = XCDR (elt);
15294 if (lim < 0)
15296 /* Negative int means reduce maximum width. */
15297 if (precision <= 0)
15298 precision = -lim;
15299 else
15300 precision = min (precision, -lim);
15302 else if (lim > 0)
15304 /* Padding specified. Don't let it be more than
15305 current maximum. */
15306 if (precision > 0)
15307 lim = min (precision, lim);
15309 /* If that's more padding than already wanted, queue it.
15310 But don't reduce padding already specified even if
15311 that is beyond the current truncation point. */
15312 field_width = max (lim, field_width);
15314 goto tail_recurse;
15316 else if (STRINGP (car) || CONSP (car))
15318 register int limit = 50;
15319 /* Limit is to protect against circular lists. */
15320 while (CONSP (elt)
15321 && --limit > 0
15322 && (precision <= 0 || n < precision))
15324 n += display_mode_element (it, depth, field_width - n,
15325 precision - n, XCAR (elt),
15326 props, risky);
15327 elt = XCDR (elt);
15331 break;
15333 default:
15334 invalid:
15335 elt = build_string ("*invalid*");
15336 goto tail_recurse;
15339 /* Pad to FIELD_WIDTH. */
15340 if (field_width > 0 && n < field_width)
15342 if (frame_title_ptr)
15343 n += store_frame_title ("", field_width - n, 0);
15344 else if (!NILP (mode_line_string_list))
15345 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15346 else
15347 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15348 0, 0, 0);
15351 return n;
15354 /* Store a mode-line string element in mode_line_string_list.
15356 If STRING is non-null, display that C string. Otherwise, the Lisp
15357 string LISP_STRING is displayed.
15359 FIELD_WIDTH is the minimum number of output glyphs to produce.
15360 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15361 with spaces. FIELD_WIDTH <= 0 means don't pad.
15363 PRECISION is the maximum number of characters to output from
15364 STRING. PRECISION <= 0 means don't truncate the string.
15366 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15367 properties to the string.
15369 PROPS are the properties to add to the string.
15370 The mode_line_string_face face property is always added to the string.
15373 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15374 char *string;
15375 Lisp_Object lisp_string;
15376 int copy_string;
15377 int field_width;
15378 int precision;
15379 Lisp_Object props;
15381 int len;
15382 int n = 0;
15384 if (string != NULL)
15386 len = strlen (string);
15387 if (precision > 0 && len > precision)
15388 len = precision;
15389 lisp_string = make_string (string, len);
15390 if (NILP (props))
15391 props = mode_line_string_face_prop;
15392 else if (!NILP (mode_line_string_face))
15394 Lisp_Object face = Fplist_get (props, Qface);
15395 props = Fcopy_sequence (props);
15396 if (NILP (face))
15397 face = mode_line_string_face;
15398 else
15399 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15400 props = Fplist_put (props, Qface, face);
15402 Fadd_text_properties (make_number (0), make_number (len),
15403 props, lisp_string);
15405 else
15407 len = XFASTINT (Flength (lisp_string));
15408 if (precision > 0 && len > precision)
15410 len = precision;
15411 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15412 precision = -1;
15414 if (!NILP (mode_line_string_face))
15416 Lisp_Object face;
15417 if (NILP (props))
15418 props = Ftext_properties_at (make_number (0), lisp_string);
15419 face = Fplist_get (props, Qface);
15420 if (NILP (face))
15421 face = mode_line_string_face;
15422 else
15423 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15424 props = Fcons (Qface, Fcons (face, Qnil));
15425 if (copy_string)
15426 lisp_string = Fcopy_sequence (lisp_string);
15428 if (!NILP (props))
15429 Fadd_text_properties (make_number (0), make_number (len),
15430 props, lisp_string);
15433 if (len > 0)
15435 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15436 n += len;
15439 if (field_width > len)
15441 field_width -= len;
15442 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15443 if (!NILP (props))
15444 Fadd_text_properties (make_number (0), make_number (field_width),
15445 props, lisp_string);
15446 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15447 n += field_width;
15450 return n;
15454 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15455 0, 3, 0,
15456 doc: /* Return the mode-line of selected window as a string.
15457 First optional arg FORMAT specifies a different format string (see
15458 `mode-line-format' for details) to use. If FORMAT is t, return
15459 the buffer's header-line. Second optional arg WINDOW specifies a
15460 different window to use as the context for the formatting.
15461 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15462 (format, window, no_props)
15463 Lisp_Object format, window, no_props;
15465 struct it it;
15466 int len;
15467 struct window *w;
15468 struct buffer *old_buffer = NULL;
15469 enum face_id face_id = DEFAULT_FACE_ID;
15471 if (NILP (window))
15472 window = selected_window;
15473 CHECK_WINDOW (window);
15474 w = XWINDOW (window);
15475 CHECK_BUFFER (w->buffer);
15477 if (XBUFFER (w->buffer) != current_buffer)
15479 old_buffer = current_buffer;
15480 set_buffer_internal_1 (XBUFFER (w->buffer));
15483 if (NILP (format) || EQ (format, Qt))
15485 face_id = NILP (format)
15486 ? CURRENT_MODE_LINE_FACE_ID (w) :
15487 HEADER_LINE_FACE_ID;
15488 format = NILP (format)
15489 ? current_buffer->mode_line_format
15490 : current_buffer->header_line_format;
15493 init_iterator (&it, w, -1, -1, NULL, face_id);
15495 if (NILP (no_props))
15497 mode_line_string_face =
15498 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15499 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15500 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15502 mode_line_string_face_prop =
15503 NILP (mode_line_string_face) ? Qnil :
15504 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15506 /* We need a dummy last element in mode_line_string_list to
15507 indicate we are building the propertized mode-line string.
15508 Using mode_line_string_face_prop here GC protects it. */
15509 mode_line_string_list =
15510 Fcons (mode_line_string_face_prop, Qnil);
15511 frame_title_ptr = NULL;
15513 else
15515 mode_line_string_face_prop = Qnil;
15516 mode_line_string_list = Qnil;
15517 frame_title_ptr = frame_title_buf;
15520 push_frame_kboard (it.f);
15521 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15522 pop_frame_kboard ();
15524 if (old_buffer)
15525 set_buffer_internal_1 (old_buffer);
15527 if (NILP (no_props))
15529 Lisp_Object str;
15530 mode_line_string_list = Fnreverse (mode_line_string_list);
15531 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15532 make_string ("", 0));
15533 mode_line_string_face_prop = Qnil;
15534 mode_line_string_list = Qnil;
15535 return str;
15538 len = frame_title_ptr - frame_title_buf;
15539 if (len > 0 && frame_title_ptr[-1] == '-')
15541 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15542 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15544 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15545 if (len > frame_title_ptr - frame_title_buf)
15546 len = frame_title_ptr - frame_title_buf;
15549 frame_title_ptr = NULL;
15550 return make_string (frame_title_buf, len);
15553 /* Write a null-terminated, right justified decimal representation of
15554 the positive integer D to BUF using a minimal field width WIDTH. */
15556 static void
15557 pint2str (buf, width, d)
15558 register char *buf;
15559 register int width;
15560 register int d;
15562 register char *p = buf;
15564 if (d <= 0)
15565 *p++ = '0';
15566 else
15568 while (d > 0)
15570 *p++ = d % 10 + '0';
15571 d /= 10;
15575 for (width -= (int) (p - buf); width > 0; --width)
15576 *p++ = ' ';
15577 *p-- = '\0';
15578 while (p > buf)
15580 d = *buf;
15581 *buf++ = *p;
15582 *p-- = d;
15586 /* Write a null-terminated, right justified decimal and "human
15587 readable" representation of the nonnegative integer D to BUF using
15588 a minimal field width WIDTH. D should be smaller than 999.5e24. */
15590 static const char power_letter[] =
15592 0, /* not used */
15593 'k', /* kilo */
15594 'M', /* mega */
15595 'G', /* giga */
15596 'T', /* tera */
15597 'P', /* peta */
15598 'E', /* exa */
15599 'Z', /* zetta */
15600 'Y' /* yotta */
15603 static void
15604 pint2hrstr (buf, width, d)
15605 char *buf;
15606 int width;
15607 int d;
15609 /* We aim to represent the nonnegative integer D as
15610 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
15611 int quotient = d;
15612 int remainder = 0;
15613 /* -1 means: do not use TENTHS. */
15614 int tenths = -1;
15615 int exponent = 0;
15617 /* Length of QUOTIENT.TENTHS as a string. */
15618 int length;
15620 char * psuffix;
15621 char * p;
15623 if (1000 <= quotient)
15625 /* Scale to the appropriate EXPONENT. */
15628 remainder = quotient % 1000;
15629 quotient /= 1000;
15630 exponent++;
15632 while (1000 <= quotient);
15634 /* Round to nearest and decide whether to use TENTHS or not. */
15635 if (quotient <= 9)
15637 tenths = remainder / 100;
15638 if (50 <= remainder % 100)
15639 if (tenths < 9)
15640 tenths++;
15641 else
15643 quotient++;
15644 if (quotient == 10)
15645 tenths = -1;
15646 else
15647 tenths = 0;
15650 else
15651 if (500 <= remainder)
15652 if (quotient < 999)
15653 quotient++;
15654 else
15656 quotient = 1;
15657 exponent++;
15658 tenths = 0;
15662 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
15663 if (tenths == -1 && quotient <= 99)
15664 if (quotient <= 9)
15665 length = 1;
15666 else
15667 length = 2;
15668 else
15669 length = 3;
15670 p = psuffix = buf + max (width, length);
15672 /* Print EXPONENT. */
15673 if (exponent)
15674 *psuffix++ = power_letter[exponent];
15675 *psuffix = '\0';
15677 /* Print TENTHS. */
15678 if (tenths >= 0)
15680 *--p = '0' + tenths;
15681 *--p = '.';
15684 /* Print QUOTIENT. */
15687 int digit = quotient % 10;
15688 *--p = '0' + digit;
15690 while ((quotient /= 10) != 0);
15692 /* Print leading spaces. */
15693 while (buf < p)
15694 *--p = ' ';
15697 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15698 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15699 type of CODING_SYSTEM. Return updated pointer into BUF. */
15701 static unsigned char invalid_eol_type[] = "(*invalid*)";
15703 static char *
15704 decode_mode_spec_coding (coding_system, buf, eol_flag)
15705 Lisp_Object coding_system;
15706 register char *buf;
15707 int eol_flag;
15709 Lisp_Object val;
15710 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15711 const unsigned char *eol_str;
15712 int eol_str_len;
15713 /* The EOL conversion we are using. */
15714 Lisp_Object eoltype;
15716 val = Fget (coding_system, Qcoding_system);
15717 eoltype = Qnil;
15719 if (!VECTORP (val)) /* Not yet decided. */
15721 if (multibyte)
15722 *buf++ = '-';
15723 if (eol_flag)
15724 eoltype = eol_mnemonic_undecided;
15725 /* Don't mention EOL conversion if it isn't decided. */
15727 else
15729 Lisp_Object eolvalue;
15731 eolvalue = Fget (coding_system, Qeol_type);
15733 if (multibyte)
15734 *buf++ = XFASTINT (AREF (val, 1));
15736 if (eol_flag)
15738 /* The EOL conversion that is normal on this system. */
15740 if (NILP (eolvalue)) /* Not yet decided. */
15741 eoltype = eol_mnemonic_undecided;
15742 else if (VECTORP (eolvalue)) /* Not yet decided. */
15743 eoltype = eol_mnemonic_undecided;
15744 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15745 eoltype = (XFASTINT (eolvalue) == 0
15746 ? eol_mnemonic_unix
15747 : (XFASTINT (eolvalue) == 1
15748 ? eol_mnemonic_dos : eol_mnemonic_mac));
15752 if (eol_flag)
15754 /* Mention the EOL conversion if it is not the usual one. */
15755 if (STRINGP (eoltype))
15757 eol_str = SDATA (eoltype);
15758 eol_str_len = SBYTES (eoltype);
15760 else if (INTEGERP (eoltype)
15761 && CHAR_VALID_P (XINT (eoltype), 0))
15763 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15764 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15765 eol_str = tmp;
15767 else
15769 eol_str = invalid_eol_type;
15770 eol_str_len = sizeof (invalid_eol_type) - 1;
15772 bcopy (eol_str, buf, eol_str_len);
15773 buf += eol_str_len;
15776 return buf;
15779 /* Return a string for the output of a mode line %-spec for window W,
15780 generated by character C. PRECISION >= 0 means don't return a
15781 string longer than that value. FIELD_WIDTH > 0 means pad the
15782 string returned with spaces to that value. Return 1 in *MULTIBYTE
15783 if the result is multibyte text. */
15785 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15787 static char *
15788 decode_mode_spec (w, c, field_width, precision, multibyte)
15789 struct window *w;
15790 register int c;
15791 int field_width, precision;
15792 int *multibyte;
15794 Lisp_Object obj;
15795 struct frame *f = XFRAME (WINDOW_FRAME (w));
15796 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15797 struct buffer *b = XBUFFER (w->buffer);
15799 obj = Qnil;
15800 *multibyte = 0;
15802 switch (c)
15804 case '*':
15805 if (!NILP (b->read_only))
15806 return "%";
15807 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15808 return "*";
15809 return "-";
15811 case '+':
15812 /* This differs from %* only for a modified read-only buffer. */
15813 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15814 return "*";
15815 if (!NILP (b->read_only))
15816 return "%";
15817 return "-";
15819 case '&':
15820 /* This differs from %* in ignoring read-only-ness. */
15821 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15822 return "*";
15823 return "-";
15825 case '%':
15826 return "%";
15828 case '[':
15830 int i;
15831 char *p;
15833 if (command_loop_level > 5)
15834 return "[[[... ";
15835 p = decode_mode_spec_buf;
15836 for (i = 0; i < command_loop_level; i++)
15837 *p++ = '[';
15838 *p = 0;
15839 return decode_mode_spec_buf;
15842 case ']':
15844 int i;
15845 char *p;
15847 if (command_loop_level > 5)
15848 return " ...]]]";
15849 p = decode_mode_spec_buf;
15850 for (i = 0; i < command_loop_level; i++)
15851 *p++ = ']';
15852 *p = 0;
15853 return decode_mode_spec_buf;
15856 case '-':
15858 register int i;
15860 /* Let lots_of_dashes be a string of infinite length. */
15861 if (!NILP (mode_line_string_list))
15862 return "--";
15863 if (field_width <= 0
15864 || field_width > sizeof (lots_of_dashes))
15866 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15867 decode_mode_spec_buf[i] = '-';
15868 decode_mode_spec_buf[i] = '\0';
15869 return decode_mode_spec_buf;
15871 else
15872 return lots_of_dashes;
15875 case 'b':
15876 obj = b->name;
15877 break;
15879 case 'c':
15881 int col = (int) current_column (); /* iftc */
15882 w->column_number_displayed = make_number (col);
15883 pint2str (decode_mode_spec_buf, field_width, col);
15884 return decode_mode_spec_buf;
15887 case 'F':
15888 /* %F displays the frame name. */
15889 if (!NILP (f->title))
15890 return (char *) SDATA (f->title);
15891 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15892 return (char *) SDATA (f->name);
15893 return "Emacs";
15895 case 'f':
15896 obj = b->filename;
15897 break;
15899 case 'i':
15901 int size = ZV - BEGV;
15902 pint2str (decode_mode_spec_buf, field_width, size);
15903 return decode_mode_spec_buf;
15906 case 'I':
15908 int size = ZV - BEGV;
15909 pint2hrstr (decode_mode_spec_buf, field_width, size);
15910 return decode_mode_spec_buf;
15913 case 'l':
15915 int startpos = XMARKER (w->start)->charpos;
15916 int startpos_byte = marker_byte_position (w->start);
15917 int line, linepos, linepos_byte, topline;
15918 int nlines, junk;
15919 int height = WINDOW_TOTAL_LINES (w);
15921 /* If we decided that this buffer isn't suitable for line numbers,
15922 don't forget that too fast. */
15923 if (EQ (w->base_line_pos, w->buffer))
15924 goto no_value;
15925 /* But do forget it, if the window shows a different buffer now. */
15926 else if (BUFFERP (w->base_line_pos))
15927 w->base_line_pos = Qnil;
15929 /* If the buffer is very big, don't waste time. */
15930 if (INTEGERP (Vline_number_display_limit)
15931 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15933 w->base_line_pos = Qnil;
15934 w->base_line_number = Qnil;
15935 goto no_value;
15938 if (!NILP (w->base_line_number)
15939 && !NILP (w->base_line_pos)
15940 && XFASTINT (w->base_line_pos) <= startpos)
15942 line = XFASTINT (w->base_line_number);
15943 linepos = XFASTINT (w->base_line_pos);
15944 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15946 else
15948 line = 1;
15949 linepos = BUF_BEGV (b);
15950 linepos_byte = BUF_BEGV_BYTE (b);
15953 /* Count lines from base line to window start position. */
15954 nlines = display_count_lines (linepos, linepos_byte,
15955 startpos_byte,
15956 startpos, &junk);
15958 topline = nlines + line;
15960 /* Determine a new base line, if the old one is too close
15961 or too far away, or if we did not have one.
15962 "Too close" means it's plausible a scroll-down would
15963 go back past it. */
15964 if (startpos == BUF_BEGV (b))
15966 w->base_line_number = make_number (topline);
15967 w->base_line_pos = make_number (BUF_BEGV (b));
15969 else if (nlines < height + 25 || nlines > height * 3 + 50
15970 || linepos == BUF_BEGV (b))
15972 int limit = BUF_BEGV (b);
15973 int limit_byte = BUF_BEGV_BYTE (b);
15974 int position;
15975 int distance = (height * 2 + 30) * line_number_display_limit_width;
15977 if (startpos - distance > limit)
15979 limit = startpos - distance;
15980 limit_byte = CHAR_TO_BYTE (limit);
15983 nlines = display_count_lines (startpos, startpos_byte,
15984 limit_byte,
15985 - (height * 2 + 30),
15986 &position);
15987 /* If we couldn't find the lines we wanted within
15988 line_number_display_limit_width chars per line,
15989 give up on line numbers for this window. */
15990 if (position == limit_byte && limit == startpos - distance)
15992 w->base_line_pos = w->buffer;
15993 w->base_line_number = Qnil;
15994 goto no_value;
15997 w->base_line_number = make_number (topline - nlines);
15998 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16001 /* Now count lines from the start pos to point. */
16002 nlines = display_count_lines (startpos, startpos_byte,
16003 PT_BYTE, PT, &junk);
16005 /* Record that we did display the line number. */
16006 line_number_displayed = 1;
16008 /* Make the string to show. */
16009 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16010 return decode_mode_spec_buf;
16011 no_value:
16013 char* p = decode_mode_spec_buf;
16014 int pad = field_width - 2;
16015 while (pad-- > 0)
16016 *p++ = ' ';
16017 *p++ = '?';
16018 *p++ = '?';
16019 *p = '\0';
16020 return decode_mode_spec_buf;
16023 break;
16025 case 'm':
16026 obj = b->mode_name;
16027 break;
16029 case 'n':
16030 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16031 return " Narrow";
16032 break;
16034 case 'p':
16036 int pos = marker_position (w->start);
16037 int total = BUF_ZV (b) - BUF_BEGV (b);
16039 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16041 if (pos <= BUF_BEGV (b))
16042 return "All";
16043 else
16044 return "Bottom";
16046 else if (pos <= BUF_BEGV (b))
16047 return "Top";
16048 else
16050 if (total > 1000000)
16051 /* Do it differently for a large value, to avoid overflow. */
16052 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16053 else
16054 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16055 /* We can't normally display a 3-digit number,
16056 so get us a 2-digit number that is close. */
16057 if (total == 100)
16058 total = 99;
16059 sprintf (decode_mode_spec_buf, "%2d%%", total);
16060 return decode_mode_spec_buf;
16064 /* Display percentage of size above the bottom of the screen. */
16065 case 'P':
16067 int toppos = marker_position (w->start);
16068 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16069 int total = BUF_ZV (b) - BUF_BEGV (b);
16071 if (botpos >= BUF_ZV (b))
16073 if (toppos <= BUF_BEGV (b))
16074 return "All";
16075 else
16076 return "Bottom";
16078 else
16080 if (total > 1000000)
16081 /* Do it differently for a large value, to avoid overflow. */
16082 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16083 else
16084 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16085 /* We can't normally display a 3-digit number,
16086 so get us a 2-digit number that is close. */
16087 if (total == 100)
16088 total = 99;
16089 if (toppos <= BUF_BEGV (b))
16090 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16091 else
16092 sprintf (decode_mode_spec_buf, "%2d%%", total);
16093 return decode_mode_spec_buf;
16097 case 's':
16098 /* status of process */
16099 obj = Fget_buffer_process (w->buffer);
16100 if (NILP (obj))
16101 return "no process";
16102 #ifdef subprocesses
16103 obj = Fsymbol_name (Fprocess_status (obj));
16104 #endif
16105 break;
16107 case 't': /* indicate TEXT or BINARY */
16108 #ifdef MODE_LINE_BINARY_TEXT
16109 return MODE_LINE_BINARY_TEXT (b);
16110 #else
16111 return "T";
16112 #endif
16114 case 'z':
16115 /* coding-system (not including end-of-line format) */
16116 case 'Z':
16117 /* coding-system (including end-of-line type) */
16119 int eol_flag = (c == 'Z');
16120 char *p = decode_mode_spec_buf;
16122 if (! FRAME_WINDOW_P (f))
16124 /* No need to mention EOL here--the terminal never needs
16125 to do EOL conversion. */
16126 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16127 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16129 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16130 p, eol_flag);
16132 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16133 #ifdef subprocesses
16134 obj = Fget_buffer_process (Fcurrent_buffer ());
16135 if (PROCESSP (obj))
16137 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16138 p, eol_flag);
16139 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16140 p, eol_flag);
16142 #endif /* subprocesses */
16143 #endif /* 0 */
16144 *p = 0;
16145 return decode_mode_spec_buf;
16149 if (STRINGP (obj))
16151 *multibyte = STRING_MULTIBYTE (obj);
16152 return (char *) SDATA (obj);
16154 else
16155 return "";
16159 /* Count up to COUNT lines starting from START / START_BYTE.
16160 But don't go beyond LIMIT_BYTE.
16161 Return the number of lines thus found (always nonnegative).
16163 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16165 static int
16166 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16167 int start, start_byte, limit_byte, count;
16168 int *byte_pos_ptr;
16170 register unsigned char *cursor;
16171 unsigned char *base;
16173 register int ceiling;
16174 register unsigned char *ceiling_addr;
16175 int orig_count = count;
16177 /* If we are not in selective display mode,
16178 check only for newlines. */
16179 int selective_display = (!NILP (current_buffer->selective_display)
16180 && !INTEGERP (current_buffer->selective_display));
16182 if (count > 0)
16184 while (start_byte < limit_byte)
16186 ceiling = BUFFER_CEILING_OF (start_byte);
16187 ceiling = min (limit_byte - 1, ceiling);
16188 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16189 base = (cursor = BYTE_POS_ADDR (start_byte));
16190 while (1)
16192 if (selective_display)
16193 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16195 else
16196 while (*cursor != '\n' && ++cursor != ceiling_addr)
16199 if (cursor != ceiling_addr)
16201 if (--count == 0)
16203 start_byte += cursor - base + 1;
16204 *byte_pos_ptr = start_byte;
16205 return orig_count;
16207 else
16208 if (++cursor == ceiling_addr)
16209 break;
16211 else
16212 break;
16214 start_byte += cursor - base;
16217 else
16219 while (start_byte > limit_byte)
16221 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16222 ceiling = max (limit_byte, ceiling);
16223 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16224 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16225 while (1)
16227 if (selective_display)
16228 while (--cursor != ceiling_addr
16229 && *cursor != '\n' && *cursor != 015)
16231 else
16232 while (--cursor != ceiling_addr && *cursor != '\n')
16235 if (cursor != ceiling_addr)
16237 if (++count == 0)
16239 start_byte += cursor - base + 1;
16240 *byte_pos_ptr = start_byte;
16241 /* When scanning backwards, we should
16242 not count the newline posterior to which we stop. */
16243 return - orig_count - 1;
16246 else
16247 break;
16249 /* Here we add 1 to compensate for the last decrement
16250 of CURSOR, which took it past the valid range. */
16251 start_byte += cursor - base + 1;
16255 *byte_pos_ptr = limit_byte;
16257 if (count < 0)
16258 return - orig_count + count;
16259 return orig_count - count;
16265 /***********************************************************************
16266 Displaying strings
16267 ***********************************************************************/
16269 /* Display a NUL-terminated string, starting with index START.
16271 If STRING is non-null, display that C string. Otherwise, the Lisp
16272 string LISP_STRING is displayed.
16274 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16275 FACE_STRING. Display STRING or LISP_STRING with the face at
16276 FACE_STRING_POS in FACE_STRING:
16278 Display the string in the environment given by IT, but use the
16279 standard display table, temporarily.
16281 FIELD_WIDTH is the minimum number of output glyphs to produce.
16282 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16283 with spaces. If STRING has more characters, more than FIELD_WIDTH
16284 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16286 PRECISION is the maximum number of characters to output from
16287 STRING. PRECISION < 0 means don't truncate the string.
16289 This is roughly equivalent to printf format specifiers:
16291 FIELD_WIDTH PRECISION PRINTF
16292 ----------------------------------------
16293 -1 -1 %s
16294 -1 10 %.10s
16295 10 -1 %10s
16296 20 10 %20.10s
16298 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16299 display them, and < 0 means obey the current buffer's value of
16300 enable_multibyte_characters.
16302 Value is the number of glyphs produced. */
16304 static int
16305 display_string (string, lisp_string, face_string, face_string_pos,
16306 start, it, field_width, precision, max_x, multibyte)
16307 unsigned char *string;
16308 Lisp_Object lisp_string;
16309 Lisp_Object face_string;
16310 int face_string_pos;
16311 int start;
16312 struct it *it;
16313 int field_width, precision, max_x;
16314 int multibyte;
16316 int hpos_at_start = it->hpos;
16317 int saved_face_id = it->face_id;
16318 struct glyph_row *row = it->glyph_row;
16320 /* Initialize the iterator IT for iteration over STRING beginning
16321 with index START. */
16322 reseat_to_string (it, string, lisp_string, start,
16323 precision, field_width, multibyte);
16325 /* If displaying STRING, set up the face of the iterator
16326 from LISP_STRING, if that's given. */
16327 if (STRINGP (face_string))
16329 int endptr;
16330 struct face *face;
16332 it->face_id
16333 = face_at_string_position (it->w, face_string, face_string_pos,
16334 0, it->region_beg_charpos,
16335 it->region_end_charpos,
16336 &endptr, it->base_face_id, 0);
16337 face = FACE_FROM_ID (it->f, it->face_id);
16338 it->face_box_p = face->box != FACE_NO_BOX;
16341 /* Set max_x to the maximum allowed X position. Don't let it go
16342 beyond the right edge of the window. */
16343 if (max_x <= 0)
16344 max_x = it->last_visible_x;
16345 else
16346 max_x = min (max_x, it->last_visible_x);
16348 /* Skip over display elements that are not visible. because IT->w is
16349 hscrolled. */
16350 if (it->current_x < it->first_visible_x)
16351 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16352 MOVE_TO_POS | MOVE_TO_X);
16354 row->ascent = it->max_ascent;
16355 row->height = it->max_ascent + it->max_descent;
16356 row->phys_ascent = it->max_phys_ascent;
16357 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16359 /* This condition is for the case that we are called with current_x
16360 past last_visible_x. */
16361 while (it->current_x < max_x)
16363 int x_before, x, n_glyphs_before, i, nglyphs;
16365 /* Get the next display element. */
16366 if (!get_next_display_element (it))
16367 break;
16369 /* Produce glyphs. */
16370 x_before = it->current_x;
16371 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16372 PRODUCE_GLYPHS (it);
16374 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16375 i = 0;
16376 x = x_before;
16377 while (i < nglyphs)
16379 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16381 if (!it->truncate_lines_p
16382 && x + glyph->pixel_width > max_x)
16384 /* End of continued line or max_x reached. */
16385 if (CHAR_GLYPH_PADDING_P (*glyph))
16387 /* A wide character is unbreakable. */
16388 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16389 it->current_x = x_before;
16391 else
16393 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16394 it->current_x = x;
16396 break;
16398 else if (x + glyph->pixel_width > it->first_visible_x)
16400 /* Glyph is at least partially visible. */
16401 ++it->hpos;
16402 if (x < it->first_visible_x)
16403 it->glyph_row->x = x - it->first_visible_x;
16405 else
16407 /* Glyph is off the left margin of the display area.
16408 Should not happen. */
16409 abort ();
16412 row->ascent = max (row->ascent, it->max_ascent);
16413 row->height = max (row->height, it->max_ascent + it->max_descent);
16414 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16415 row->phys_height = max (row->phys_height,
16416 it->max_phys_ascent + it->max_phys_descent);
16417 x += glyph->pixel_width;
16418 ++i;
16421 /* Stop if max_x reached. */
16422 if (i < nglyphs)
16423 break;
16425 /* Stop at line ends. */
16426 if (ITERATOR_AT_END_OF_LINE_P (it))
16428 it->continuation_lines_width = 0;
16429 break;
16432 set_iterator_to_next (it, 1);
16434 /* Stop if truncating at the right edge. */
16435 if (it->truncate_lines_p
16436 && it->current_x >= it->last_visible_x)
16438 /* Add truncation mark, but don't do it if the line is
16439 truncated at a padding space. */
16440 if (IT_CHARPOS (*it) < it->string_nchars)
16442 if (!FRAME_WINDOW_P (it->f))
16444 int i, n;
16446 if (it->current_x > it->last_visible_x)
16448 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16449 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16450 break;
16451 for (n = row->used[TEXT_AREA]; i < n; ++i)
16453 row->used[TEXT_AREA] = i;
16454 produce_special_glyphs (it, IT_TRUNCATION);
16457 produce_special_glyphs (it, IT_TRUNCATION);
16459 it->glyph_row->truncated_on_right_p = 1;
16461 break;
16465 /* Maybe insert a truncation at the left. */
16466 if (it->first_visible_x
16467 && IT_CHARPOS (*it) > 0)
16469 if (!FRAME_WINDOW_P (it->f))
16470 insert_left_trunc_glyphs (it);
16471 it->glyph_row->truncated_on_left_p = 1;
16474 it->face_id = saved_face_id;
16476 /* Value is number of columns displayed. */
16477 return it->hpos - hpos_at_start;
16482 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16483 appears as an element of LIST or as the car of an element of LIST.
16484 If PROPVAL is a list, compare each element against LIST in that
16485 way, and return 1/2 if any element of PROPVAL is found in LIST.
16486 Otherwise return 0. This function cannot quit.
16487 The return value is 2 if the text is invisible but with an ellipsis
16488 and 1 if it's invisible and without an ellipsis. */
16491 invisible_p (propval, list)
16492 register Lisp_Object propval;
16493 Lisp_Object list;
16495 register Lisp_Object tail, proptail;
16497 for (tail = list; CONSP (tail); tail = XCDR (tail))
16499 register Lisp_Object tem;
16500 tem = XCAR (tail);
16501 if (EQ (propval, tem))
16502 return 1;
16503 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16504 return NILP (XCDR (tem)) ? 1 : 2;
16507 if (CONSP (propval))
16509 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16511 Lisp_Object propelt;
16512 propelt = XCAR (proptail);
16513 for (tail = list; CONSP (tail); tail = XCDR (tail))
16515 register Lisp_Object tem;
16516 tem = XCAR (tail);
16517 if (EQ (propelt, tem))
16518 return 1;
16519 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16520 return NILP (XCDR (tem)) ? 1 : 2;
16525 return 0;
16529 /***********************************************************************
16530 Glyph Display
16531 ***********************************************************************/
16533 #ifdef HAVE_WINDOW_SYSTEM
16535 #if GLYPH_DEBUG
16537 void
16538 dump_glyph_string (s)
16539 struct glyph_string *s;
16541 fprintf (stderr, "glyph string\n");
16542 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16543 s->x, s->y, s->width, s->height);
16544 fprintf (stderr, " ybase = %d\n", s->ybase);
16545 fprintf (stderr, " hl = %d\n", s->hl);
16546 fprintf (stderr, " left overhang = %d, right = %d\n",
16547 s->left_overhang, s->right_overhang);
16548 fprintf (stderr, " nchars = %d\n", s->nchars);
16549 fprintf (stderr, " extends to end of line = %d\n",
16550 s->extends_to_end_of_line_p);
16551 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16552 fprintf (stderr, " bg width = %d\n", s->background_width);
16555 #endif /* GLYPH_DEBUG */
16557 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16558 of XChar2b structures for S; it can't be allocated in
16559 init_glyph_string because it must be allocated via `alloca'. W
16560 is the window on which S is drawn. ROW and AREA are the glyph row
16561 and area within the row from which S is constructed. START is the
16562 index of the first glyph structure covered by S. HL is a
16563 face-override for drawing S. */
16565 #ifdef HAVE_NTGUI
16566 #define OPTIONAL_HDC(hdc) hdc,
16567 #define DECLARE_HDC(hdc) HDC hdc;
16568 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16569 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16570 #endif
16572 #ifndef OPTIONAL_HDC
16573 #define OPTIONAL_HDC(hdc)
16574 #define DECLARE_HDC(hdc)
16575 #define ALLOCATE_HDC(hdc, f)
16576 #define RELEASE_HDC(hdc, f)
16577 #endif
16579 static void
16580 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16581 struct glyph_string *s;
16582 DECLARE_HDC (hdc)
16583 XChar2b *char2b;
16584 struct window *w;
16585 struct glyph_row *row;
16586 enum glyph_row_area area;
16587 int start;
16588 enum draw_glyphs_face hl;
16590 bzero (s, sizeof *s);
16591 s->w = w;
16592 s->f = XFRAME (w->frame);
16593 #ifdef HAVE_NTGUI
16594 s->hdc = hdc;
16595 #endif
16596 s->display = FRAME_X_DISPLAY (s->f);
16597 s->window = FRAME_X_WINDOW (s->f);
16598 s->char2b = char2b;
16599 s->hl = hl;
16600 s->row = row;
16601 s->area = area;
16602 s->first_glyph = row->glyphs[area] + start;
16603 s->height = row->height;
16604 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16606 /* Display the internal border below the tool-bar window. */
16607 if (s->w == XWINDOW (s->f->tool_bar_window))
16608 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16610 s->ybase = s->y + row->ascent;
16614 /* Append the list of glyph strings with head H and tail T to the list
16615 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16617 static INLINE void
16618 append_glyph_string_lists (head, tail, h, t)
16619 struct glyph_string **head, **tail;
16620 struct glyph_string *h, *t;
16622 if (h)
16624 if (*head)
16625 (*tail)->next = h;
16626 else
16627 *head = h;
16628 h->prev = *tail;
16629 *tail = t;
16634 /* Prepend the list of glyph strings with head H and tail T to the
16635 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16636 result. */
16638 static INLINE void
16639 prepend_glyph_string_lists (head, tail, h, t)
16640 struct glyph_string **head, **tail;
16641 struct glyph_string *h, *t;
16643 if (h)
16645 if (*head)
16646 (*head)->prev = t;
16647 else
16648 *tail = t;
16649 t->next = *head;
16650 *head = h;
16655 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16656 Set *HEAD and *TAIL to the resulting list. */
16658 static INLINE void
16659 append_glyph_string (head, tail, s)
16660 struct glyph_string **head, **tail;
16661 struct glyph_string *s;
16663 s->next = s->prev = NULL;
16664 append_glyph_string_lists (head, tail, s, s);
16668 /* Get face and two-byte form of character glyph GLYPH on frame F.
16669 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16670 a pointer to a realized face that is ready for display. */
16672 static INLINE struct face *
16673 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16674 struct frame *f;
16675 struct glyph *glyph;
16676 XChar2b *char2b;
16677 int *two_byte_p;
16679 struct face *face;
16681 xassert (glyph->type == CHAR_GLYPH);
16682 face = FACE_FROM_ID (f, glyph->face_id);
16684 if (two_byte_p)
16685 *two_byte_p = 0;
16687 if (!glyph->multibyte_p)
16689 /* Unibyte case. We don't have to encode, but we have to make
16690 sure to use a face suitable for unibyte. */
16691 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16693 else if (glyph->u.ch < 128
16694 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16696 /* Case of ASCII in a face known to fit ASCII. */
16697 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16699 else
16701 int c1, c2, charset;
16703 /* Split characters into bytes. If c2 is -1 afterwards, C is
16704 really a one-byte character so that byte1 is zero. */
16705 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16706 if (c2 > 0)
16707 STORE_XCHAR2B (char2b, c1, c2);
16708 else
16709 STORE_XCHAR2B (char2b, 0, c1);
16711 /* Maybe encode the character in *CHAR2B. */
16712 if (charset != CHARSET_ASCII)
16714 struct font_info *font_info
16715 = FONT_INFO_FROM_ID (f, face->font_info_id);
16716 if (font_info)
16717 glyph->font_type
16718 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16722 /* Make sure X resources of the face are allocated. */
16723 xassert (face != NULL);
16724 PREPARE_FACE_FOR_DISPLAY (f, face);
16725 return face;
16729 /* Fill glyph string S with composition components specified by S->cmp.
16731 FACES is an array of faces for all components of this composition.
16732 S->gidx is the index of the first component for S.
16733 OVERLAPS_P non-zero means S should draw the foreground only, and
16734 use its physical height for clipping.
16736 Value is the index of a component not in S. */
16738 static int
16739 fill_composite_glyph_string (s, faces, overlaps_p)
16740 struct glyph_string *s;
16741 struct face **faces;
16742 int overlaps_p;
16744 int i;
16746 xassert (s);
16748 s->for_overlaps_p = overlaps_p;
16750 s->face = faces[s->gidx];
16751 s->font = s->face->font;
16752 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16754 /* For all glyphs of this composition, starting at the offset
16755 S->gidx, until we reach the end of the definition or encounter a
16756 glyph that requires the different face, add it to S. */
16757 ++s->nchars;
16758 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16759 ++s->nchars;
16761 /* All glyph strings for the same composition has the same width,
16762 i.e. the width set for the first component of the composition. */
16764 s->width = s->first_glyph->pixel_width;
16766 /* If the specified font could not be loaded, use the frame's
16767 default font, but record the fact that we couldn't load it in
16768 the glyph string so that we can draw rectangles for the
16769 characters of the glyph string. */
16770 if (s->font == NULL)
16772 s->font_not_found_p = 1;
16773 s->font = FRAME_FONT (s->f);
16776 /* Adjust base line for subscript/superscript text. */
16777 s->ybase += s->first_glyph->voffset;
16779 xassert (s->face && s->face->gc);
16781 /* This glyph string must always be drawn with 16-bit functions. */
16782 s->two_byte_p = 1;
16784 return s->gidx + s->nchars;
16788 /* Fill glyph string S from a sequence of character glyphs.
16790 FACE_ID is the face id of the string. START is the index of the
16791 first glyph to consider, END is the index of the last + 1.
16792 OVERLAPS_P non-zero means S should draw the foreground only, and
16793 use its physical height for clipping.
16795 Value is the index of the first glyph not in S. */
16797 static int
16798 fill_glyph_string (s, face_id, start, end, overlaps_p)
16799 struct glyph_string *s;
16800 int face_id;
16801 int start, end, overlaps_p;
16803 struct glyph *glyph, *last;
16804 int voffset;
16805 int glyph_not_available_p;
16807 xassert (s->f == XFRAME (s->w->frame));
16808 xassert (s->nchars == 0);
16809 xassert (start >= 0 && end > start);
16811 s->for_overlaps_p = overlaps_p,
16812 glyph = s->row->glyphs[s->area] + start;
16813 last = s->row->glyphs[s->area] + end;
16814 voffset = glyph->voffset;
16816 glyph_not_available_p = glyph->glyph_not_available_p;
16818 while (glyph < last
16819 && glyph->type == CHAR_GLYPH
16820 && glyph->voffset == voffset
16821 /* Same face id implies same font, nowadays. */
16822 && glyph->face_id == face_id
16823 && glyph->glyph_not_available_p == glyph_not_available_p)
16825 int two_byte_p;
16827 s->face = get_glyph_face_and_encoding (s->f, glyph,
16828 s->char2b + s->nchars,
16829 &two_byte_p);
16830 s->two_byte_p = two_byte_p;
16831 ++s->nchars;
16832 xassert (s->nchars <= end - start);
16833 s->width += glyph->pixel_width;
16834 ++glyph;
16837 s->font = s->face->font;
16838 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16840 /* If the specified font could not be loaded, use the frame's font,
16841 but record the fact that we couldn't load it in
16842 S->font_not_found_p so that we can draw rectangles for the
16843 characters of the glyph string. */
16844 if (s->font == NULL || glyph_not_available_p)
16846 s->font_not_found_p = 1;
16847 s->font = FRAME_FONT (s->f);
16850 /* Adjust base line for subscript/superscript text. */
16851 s->ybase += voffset;
16853 xassert (s->face && s->face->gc);
16854 return glyph - s->row->glyphs[s->area];
16858 /* Fill glyph string S from image glyph S->first_glyph. */
16860 static void
16861 fill_image_glyph_string (s)
16862 struct glyph_string *s;
16864 xassert (s->first_glyph->type == IMAGE_GLYPH);
16865 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16866 xassert (s->img);
16867 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16868 s->font = s->face->font;
16869 s->width = s->first_glyph->pixel_width;
16871 /* Adjust base line for subscript/superscript text. */
16872 s->ybase += s->first_glyph->voffset;
16876 /* Fill glyph string S from a sequence of stretch glyphs.
16878 ROW is the glyph row in which the glyphs are found, AREA is the
16879 area within the row. START is the index of the first glyph to
16880 consider, END is the index of the last + 1.
16882 Value is the index of the first glyph not in S. */
16884 static int
16885 fill_stretch_glyph_string (s, row, area, start, end)
16886 struct glyph_string *s;
16887 struct glyph_row *row;
16888 enum glyph_row_area area;
16889 int start, end;
16891 struct glyph *glyph, *last;
16892 int voffset, face_id;
16894 xassert (s->first_glyph->type == STRETCH_GLYPH);
16896 glyph = s->row->glyphs[s->area] + start;
16897 last = s->row->glyphs[s->area] + end;
16898 face_id = glyph->face_id;
16899 s->face = FACE_FROM_ID (s->f, face_id);
16900 s->font = s->face->font;
16901 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16902 s->width = glyph->pixel_width;
16903 voffset = glyph->voffset;
16905 for (++glyph;
16906 (glyph < last
16907 && glyph->type == STRETCH_GLYPH
16908 && glyph->voffset == voffset
16909 && glyph->face_id == face_id);
16910 ++glyph)
16911 s->width += glyph->pixel_width;
16913 /* Adjust base line for subscript/superscript text. */
16914 s->ybase += voffset;
16916 /* The case that face->gc == 0 is handled when drawing the glyph
16917 string by calling PREPARE_FACE_FOR_DISPLAY. */
16918 xassert (s->face);
16919 return glyph - s->row->glyphs[s->area];
16923 /* EXPORT for RIF:
16924 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16925 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16926 assumed to be zero. */
16928 void
16929 x_get_glyph_overhangs (glyph, f, left, right)
16930 struct glyph *glyph;
16931 struct frame *f;
16932 int *left, *right;
16934 *left = *right = 0;
16936 if (glyph->type == CHAR_GLYPH)
16938 XFontStruct *font;
16939 struct face *face;
16940 struct font_info *font_info;
16941 XChar2b char2b;
16942 XCharStruct *pcm;
16944 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16945 font = face->font;
16946 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16947 if (font /* ++KFS: Should this be font_info ? */
16948 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16950 if (pcm->rbearing > pcm->width)
16951 *right = pcm->rbearing - pcm->width;
16952 if (pcm->lbearing < 0)
16953 *left = -pcm->lbearing;
16959 /* Return the index of the first glyph preceding glyph string S that
16960 is overwritten by S because of S's left overhang. Value is -1
16961 if no glyphs are overwritten. */
16963 static int
16964 left_overwritten (s)
16965 struct glyph_string *s;
16967 int k;
16969 if (s->left_overhang)
16971 int x = 0, i;
16972 struct glyph *glyphs = s->row->glyphs[s->area];
16973 int first = s->first_glyph - glyphs;
16975 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16976 x -= glyphs[i].pixel_width;
16978 k = i + 1;
16980 else
16981 k = -1;
16983 return k;
16987 /* Return the index of the first glyph preceding glyph string S that
16988 is overwriting S because of its right overhang. Value is -1 if no
16989 glyph in front of S overwrites S. */
16991 static int
16992 left_overwriting (s)
16993 struct glyph_string *s;
16995 int i, k, x;
16996 struct glyph *glyphs = s->row->glyphs[s->area];
16997 int first = s->first_glyph - glyphs;
16999 k = -1;
17000 x = 0;
17001 for (i = first - 1; i >= 0; --i)
17003 int left, right;
17004 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17005 if (x + right > 0)
17006 k = i;
17007 x -= glyphs[i].pixel_width;
17010 return k;
17014 /* Return the index of the last glyph following glyph string S that is
17015 not overwritten by S because of S's right overhang. Value is -1 if
17016 no such glyph is found. */
17018 static int
17019 right_overwritten (s)
17020 struct glyph_string *s;
17022 int k = -1;
17024 if (s->right_overhang)
17026 int x = 0, i;
17027 struct glyph *glyphs = s->row->glyphs[s->area];
17028 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17029 int end = s->row->used[s->area];
17031 for (i = first; i < end && s->right_overhang > x; ++i)
17032 x += glyphs[i].pixel_width;
17034 k = i;
17037 return k;
17041 /* Return the index of the last glyph following glyph string S that
17042 overwrites S because of its left overhang. Value is negative
17043 if no such glyph is found. */
17045 static int
17046 right_overwriting (s)
17047 struct glyph_string *s;
17049 int i, k, x;
17050 int end = s->row->used[s->area];
17051 struct glyph *glyphs = s->row->glyphs[s->area];
17052 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17054 k = -1;
17055 x = 0;
17056 for (i = first; i < end; ++i)
17058 int left, right;
17059 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17060 if (x - left < 0)
17061 k = i;
17062 x += glyphs[i].pixel_width;
17065 return k;
17069 /* Get face and two-byte form of character C in face FACE_ID on frame
17070 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17071 means we want to display multibyte text. DISPLAY_P non-zero means
17072 make sure that X resources for the face returned are allocated.
17073 Value is a pointer to a realized face that is ready for display if
17074 DISPLAY_P is non-zero. */
17076 static INLINE struct face *
17077 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17078 struct frame *f;
17079 int c, face_id;
17080 XChar2b *char2b;
17081 int multibyte_p, display_p;
17083 struct face *face = FACE_FROM_ID (f, face_id);
17085 if (!multibyte_p)
17087 /* Unibyte case. We don't have to encode, but we have to make
17088 sure to use a face suitable for unibyte. */
17089 STORE_XCHAR2B (char2b, 0, c);
17090 face_id = FACE_FOR_CHAR (f, face, c);
17091 face = FACE_FROM_ID (f, face_id);
17093 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17095 /* Case of ASCII in a face known to fit ASCII. */
17096 STORE_XCHAR2B (char2b, 0, c);
17098 else
17100 int c1, c2, charset;
17102 /* Split characters into bytes. If c2 is -1 afterwards, C is
17103 really a one-byte character so that byte1 is zero. */
17104 SPLIT_CHAR (c, charset, c1, c2);
17105 if (c2 > 0)
17106 STORE_XCHAR2B (char2b, c1, c2);
17107 else
17108 STORE_XCHAR2B (char2b, 0, c1);
17110 /* Maybe encode the character in *CHAR2B. */
17111 if (face->font != NULL)
17113 struct font_info *font_info
17114 = FONT_INFO_FROM_ID (f, face->font_info_id);
17115 if (font_info)
17116 rif->encode_char (c, char2b, font_info, 0);
17120 /* Make sure X resources of the face are allocated. */
17121 #ifdef HAVE_X_WINDOWS
17122 if (display_p)
17123 #endif
17125 xassert (face != NULL);
17126 PREPARE_FACE_FOR_DISPLAY (f, face);
17129 return face;
17133 /* Set background width of glyph string S. START is the index of the
17134 first glyph following S. LAST_X is the right-most x-position + 1
17135 in the drawing area. */
17137 static INLINE void
17138 set_glyph_string_background_width (s, start, last_x)
17139 struct glyph_string *s;
17140 int start;
17141 int last_x;
17143 /* If the face of this glyph string has to be drawn to the end of
17144 the drawing area, set S->extends_to_end_of_line_p. */
17145 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
17147 if (start == s->row->used[s->area]
17148 && s->area == TEXT_AREA
17149 && ((s->hl == DRAW_NORMAL_TEXT
17150 && (s->row->fill_line_p
17151 || s->face->background != default_face->background
17152 || s->face->stipple != default_face->stipple
17153 || s->row->mouse_face_p))
17154 || s->hl == DRAW_MOUSE_FACE
17155 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
17156 && s->row->fill_line_p)))
17157 s->extends_to_end_of_line_p = 1;
17159 /* If S extends its face to the end of the line, set its
17160 background_width to the distance to the right edge of the drawing
17161 area. */
17162 if (s->extends_to_end_of_line_p)
17163 s->background_width = last_x - s->x + 1;
17164 else
17165 s->background_width = s->width;
17169 /* Compute overhangs and x-positions for glyph string S and its
17170 predecessors, or successors. X is the starting x-position for S.
17171 BACKWARD_P non-zero means process predecessors. */
17173 static void
17174 compute_overhangs_and_x (s, x, backward_p)
17175 struct glyph_string *s;
17176 int x;
17177 int backward_p;
17179 if (backward_p)
17181 while (s)
17183 if (rif->compute_glyph_string_overhangs)
17184 rif->compute_glyph_string_overhangs (s);
17185 x -= s->width;
17186 s->x = x;
17187 s = s->prev;
17190 else
17192 while (s)
17194 if (rif->compute_glyph_string_overhangs)
17195 rif->compute_glyph_string_overhangs (s);
17196 s->x = x;
17197 x += s->width;
17198 s = s->next;
17205 /* The following macros are only called from draw_glyphs below.
17206 They reference the following parameters of that function directly:
17207 `w', `row', `area', and `overlap_p'
17208 as well as the following local variables:
17209 `s', `f', and `hdc' (in W32) */
17211 #ifdef HAVE_NTGUI
17212 /* On W32, silently add local `hdc' variable to argument list of
17213 init_glyph_string. */
17214 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17215 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17216 #else
17217 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17218 init_glyph_string (s, char2b, w, row, area, start, hl)
17219 #endif
17221 /* Add a glyph string for a stretch glyph to the list of strings
17222 between HEAD and TAIL. START is the index of the stretch glyph in
17223 row area AREA of glyph row ROW. END is the index of the last glyph
17224 in that glyph row area. X is the current output position assigned
17225 to the new glyph string constructed. HL overrides that face of the
17226 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17227 is the right-most x-position of the drawing area. */
17229 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17230 and below -- keep them on one line. */
17231 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17232 do \
17234 s = (struct glyph_string *) alloca (sizeof *s); \
17235 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17236 START = fill_stretch_glyph_string (s, row, area, START, END); \
17237 append_glyph_string (&HEAD, &TAIL, s); \
17238 s->x = (X); \
17240 while (0)
17243 /* Add a glyph string for an image glyph to the list of strings
17244 between HEAD and TAIL. START is the index of the image glyph in
17245 row area AREA of glyph row ROW. END is the index of the last glyph
17246 in that glyph row area. X is the current output position assigned
17247 to the new glyph string constructed. HL overrides that face of the
17248 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17249 is the right-most x-position of the drawing area. */
17251 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17252 do \
17254 s = (struct glyph_string *) alloca (sizeof *s); \
17255 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17256 fill_image_glyph_string (s); \
17257 append_glyph_string (&HEAD, &TAIL, s); \
17258 ++START; \
17259 s->x = (X); \
17261 while (0)
17264 /* Add a glyph string for a sequence of character glyphs to the list
17265 of strings between HEAD and TAIL. START is the index of the first
17266 glyph in row area AREA of glyph row ROW that is part of the new
17267 glyph string. END is the index of the last glyph in that glyph row
17268 area. X is the current output position assigned to the new glyph
17269 string constructed. HL overrides that face of the glyph; e.g. it
17270 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17271 right-most x-position of the drawing area. */
17273 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17274 do \
17276 int c, face_id; \
17277 XChar2b *char2b; \
17279 c = (row)->glyphs[area][START].u.ch; \
17280 face_id = (row)->glyphs[area][START].face_id; \
17282 s = (struct glyph_string *) alloca (sizeof *s); \
17283 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17284 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17285 append_glyph_string (&HEAD, &TAIL, s); \
17286 s->x = (X); \
17287 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17289 while (0)
17292 /* Add a glyph string for a composite sequence to the list of strings
17293 between HEAD and TAIL. START is the index of the first glyph in
17294 row area AREA of glyph row ROW that is part of the new glyph
17295 string. END is the index of the last glyph in that glyph row area.
17296 X is the current output position assigned to the new glyph string
17297 constructed. HL overrides that face of the glyph; e.g. it is
17298 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17299 x-position of the drawing area. */
17301 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17302 do { \
17303 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17304 int face_id = (row)->glyphs[area][START].face_id; \
17305 struct face *base_face = FACE_FROM_ID (f, face_id); \
17306 struct composition *cmp = composition_table[cmp_id]; \
17307 int glyph_len = cmp->glyph_len; \
17308 XChar2b *char2b; \
17309 struct face **faces; \
17310 struct glyph_string *first_s = NULL; \
17311 int n; \
17313 base_face = base_face->ascii_face; \
17314 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17315 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17316 /* At first, fill in `char2b' and `faces'. */ \
17317 for (n = 0; n < glyph_len; n++) \
17319 int c = COMPOSITION_GLYPH (cmp, n); \
17320 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17321 faces[n] = FACE_FROM_ID (f, this_face_id); \
17322 get_char_face_and_encoding (f, c, this_face_id, \
17323 char2b + n, 1, 1); \
17326 /* Make glyph_strings for each glyph sequence that is drawable by \
17327 the same face, and append them to HEAD/TAIL. */ \
17328 for (n = 0; n < cmp->glyph_len;) \
17330 s = (struct glyph_string *) alloca (sizeof *s); \
17331 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17332 append_glyph_string (&(HEAD), &(TAIL), s); \
17333 s->cmp = cmp; \
17334 s->gidx = n; \
17335 s->x = (X); \
17337 if (n == 0) \
17338 first_s = s; \
17340 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17343 ++START; \
17344 s = first_s; \
17345 } while (0)
17348 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17349 of AREA of glyph row ROW on window W between indices START and END.
17350 HL overrides the face for drawing glyph strings, e.g. it is
17351 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17352 x-positions of the drawing area.
17354 This is an ugly monster macro construct because we must use alloca
17355 to allocate glyph strings (because draw_glyphs can be called
17356 asynchronously). */
17358 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17359 do \
17361 HEAD = TAIL = NULL; \
17362 while (START < END) \
17364 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17365 switch (first_glyph->type) \
17367 case CHAR_GLYPH: \
17368 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17369 HL, X, LAST_X); \
17370 break; \
17372 case COMPOSITE_GLYPH: \
17373 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17374 HL, X, LAST_X); \
17375 break; \
17377 case STRETCH_GLYPH: \
17378 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17379 HL, X, LAST_X); \
17380 break; \
17382 case IMAGE_GLYPH: \
17383 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17384 HL, X, LAST_X); \
17385 break; \
17387 default: \
17388 abort (); \
17391 set_glyph_string_background_width (s, START, LAST_X); \
17392 (X) += s->width; \
17395 while (0)
17398 /* Draw glyphs between START and END in AREA of ROW on window W,
17399 starting at x-position X. X is relative to AREA in W. HL is a
17400 face-override with the following meaning:
17402 DRAW_NORMAL_TEXT draw normally
17403 DRAW_CURSOR draw in cursor face
17404 DRAW_MOUSE_FACE draw in mouse face.
17405 DRAW_INVERSE_VIDEO draw in mode line face
17406 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17407 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17409 If OVERLAPS_P is non-zero, draw only the foreground of characters
17410 and clip to the physical height of ROW.
17412 Value is the x-position reached, relative to AREA of W. */
17414 static int
17415 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17416 struct window *w;
17417 int x;
17418 struct glyph_row *row;
17419 enum glyph_row_area area;
17420 int start, end;
17421 enum draw_glyphs_face hl;
17422 int overlaps_p;
17424 struct glyph_string *head, *tail;
17425 struct glyph_string *s;
17426 int last_x, area_width;
17427 int x_reached;
17428 int i, j;
17429 struct frame *f = XFRAME (WINDOW_FRAME (w));
17430 DECLARE_HDC (hdc);
17432 ALLOCATE_HDC (hdc, f);
17434 /* Let's rather be paranoid than getting a SEGV. */
17435 end = min (end, row->used[area]);
17436 start = max (0, start);
17437 start = min (end, start);
17439 /* Translate X to frame coordinates. Set last_x to the right
17440 end of the drawing area. */
17441 if (row->full_width_p)
17443 /* X is relative to the left edge of W, without scroll bars
17444 or fringes. */
17445 x += WINDOW_LEFT_EDGE_X (w);
17446 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
17448 else
17450 int area_left = window_box_left (w, area);
17451 x += area_left;
17452 area_width = window_box_width (w, area);
17453 last_x = area_left + area_width;
17456 /* Build a doubly-linked list of glyph_string structures between
17457 head and tail from what we have to draw. Note that the macro
17458 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17459 the reason we use a separate variable `i'. */
17460 i = start;
17461 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17462 if (tail)
17463 x_reached = tail->x + tail->background_width;
17464 else
17465 x_reached = x;
17467 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17468 the row, redraw some glyphs in front or following the glyph
17469 strings built above. */
17470 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17472 int dummy_x = 0;
17473 struct glyph_string *h, *t;
17475 /* Compute overhangs for all glyph strings. */
17476 if (rif->compute_glyph_string_overhangs)
17477 for (s = head; s; s = s->next)
17478 rif->compute_glyph_string_overhangs (s);
17480 /* Prepend glyph strings for glyphs in front of the first glyph
17481 string that are overwritten because of the first glyph
17482 string's left overhang. The background of all strings
17483 prepended must be drawn because the first glyph string
17484 draws over it. */
17485 i = left_overwritten (head);
17486 if (i >= 0)
17488 j = i;
17489 BUILD_GLYPH_STRINGS (j, start, h, t,
17490 DRAW_NORMAL_TEXT, dummy_x, last_x);
17491 start = i;
17492 compute_overhangs_and_x (t, head->x, 1);
17493 prepend_glyph_string_lists (&head, &tail, h, t);
17496 /* Prepend glyph strings for glyphs in front of the first glyph
17497 string that overwrite that glyph string because of their
17498 right overhang. For these strings, only the foreground must
17499 be drawn, because it draws over the glyph string at `head'.
17500 The background must not be drawn because this would overwrite
17501 right overhangs of preceding glyphs for which no glyph
17502 strings exist. */
17503 i = left_overwriting (head);
17504 if (i >= 0)
17506 BUILD_GLYPH_STRINGS (i, start, h, t,
17507 DRAW_NORMAL_TEXT, dummy_x, last_x);
17508 for (s = h; s; s = s->next)
17509 s->background_filled_p = 1;
17510 compute_overhangs_and_x (t, head->x, 1);
17511 prepend_glyph_string_lists (&head, &tail, h, t);
17514 /* Append glyphs strings for glyphs following the last glyph
17515 string tail that are overwritten by tail. The background of
17516 these strings has to be drawn because tail's foreground draws
17517 over it. */
17518 i = right_overwritten (tail);
17519 if (i >= 0)
17521 BUILD_GLYPH_STRINGS (end, i, h, t,
17522 DRAW_NORMAL_TEXT, x, last_x);
17523 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17524 append_glyph_string_lists (&head, &tail, h, t);
17527 /* Append glyph strings for glyphs following the last glyph
17528 string tail that overwrite tail. The foreground of such
17529 glyphs has to be drawn because it writes into the background
17530 of tail. The background must not be drawn because it could
17531 paint over the foreground of following glyphs. */
17532 i = right_overwriting (tail);
17533 if (i >= 0)
17535 BUILD_GLYPH_STRINGS (end, i, h, t,
17536 DRAW_NORMAL_TEXT, x, last_x);
17537 for (s = h; s; s = s->next)
17538 s->background_filled_p = 1;
17539 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17540 append_glyph_string_lists (&head, &tail, h, t);
17544 /* Draw all strings. */
17545 for (s = head; s; s = s->next)
17546 rif->draw_glyph_string (s);
17548 if (area == TEXT_AREA
17549 && !row->full_width_p
17550 /* When drawing overlapping rows, only the glyph strings'
17551 foreground is drawn, which doesn't erase a cursor
17552 completely. */
17553 && !overlaps_p)
17555 int x0 = head ? head->x : x;
17556 int x1 = tail ? tail->x + tail->background_width : x;
17558 int text_left = window_box_left (w, TEXT_AREA);
17559 x0 -= text_left;
17560 x1 -= text_left;
17562 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
17563 row->y, MATRIX_ROW_BOTTOM_Y (row));
17566 /* Value is the x-position up to which drawn, relative to AREA of W.
17567 This doesn't include parts drawn because of overhangs. */
17568 if (row->full_width_p)
17569 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17570 else
17571 x_reached -= window_box_left (w, area);
17573 RELEASE_HDC (hdc, f);
17575 return x_reached;
17579 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17580 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17582 static INLINE void
17583 append_glyph (it)
17584 struct it *it;
17586 struct glyph *glyph;
17587 enum glyph_row_area area = it->area;
17589 xassert (it->glyph_row);
17590 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17592 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17593 if (glyph < it->glyph_row->glyphs[area + 1])
17595 glyph->charpos = CHARPOS (it->position);
17596 glyph->object = it->object;
17597 glyph->pixel_width = it->pixel_width;
17598 glyph->voffset = it->voffset;
17599 glyph->type = CHAR_GLYPH;
17600 glyph->multibyte_p = it->multibyte_p;
17601 glyph->left_box_line_p = it->start_of_box_run_p;
17602 glyph->right_box_line_p = it->end_of_box_run_p;
17603 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17604 || it->phys_descent > it->descent);
17605 glyph->padding_p = 0;
17606 glyph->glyph_not_available_p = it->glyph_not_available_p;
17607 glyph->face_id = it->face_id;
17608 glyph->u.ch = it->char_to_display;
17609 glyph->font_type = FONT_TYPE_UNKNOWN;
17610 ++it->glyph_row->used[area];
17614 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17615 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17617 static INLINE void
17618 append_composite_glyph (it)
17619 struct it *it;
17621 struct glyph *glyph;
17622 enum glyph_row_area area = it->area;
17624 xassert (it->glyph_row);
17626 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17627 if (glyph < it->glyph_row->glyphs[area + 1])
17629 glyph->charpos = CHARPOS (it->position);
17630 glyph->object = it->object;
17631 glyph->pixel_width = it->pixel_width;
17632 glyph->voffset = it->voffset;
17633 glyph->type = COMPOSITE_GLYPH;
17634 glyph->multibyte_p = it->multibyte_p;
17635 glyph->left_box_line_p = it->start_of_box_run_p;
17636 glyph->right_box_line_p = it->end_of_box_run_p;
17637 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17638 || it->phys_descent > it->descent);
17639 glyph->padding_p = 0;
17640 glyph->glyph_not_available_p = 0;
17641 glyph->face_id = it->face_id;
17642 glyph->u.cmp_id = it->cmp_id;
17643 glyph->font_type = FONT_TYPE_UNKNOWN;
17644 ++it->glyph_row->used[area];
17649 /* Change IT->ascent and IT->height according to the setting of
17650 IT->voffset. */
17652 static INLINE void
17653 take_vertical_position_into_account (it)
17654 struct it *it;
17656 if (it->voffset)
17658 if (it->voffset < 0)
17659 /* Increase the ascent so that we can display the text higher
17660 in the line. */
17661 it->ascent += abs (it->voffset);
17662 else
17663 /* Increase the descent so that we can display the text lower
17664 in the line. */
17665 it->descent += it->voffset;
17670 /* Produce glyphs/get display metrics for the image IT is loaded with.
17671 See the description of struct display_iterator in dispextern.h for
17672 an overview of struct display_iterator. */
17674 static void
17675 produce_image_glyph (it)
17676 struct it *it;
17678 struct image *img;
17679 struct face *face;
17681 xassert (it->what == IT_IMAGE);
17683 face = FACE_FROM_ID (it->f, it->face_id);
17684 img = IMAGE_FROM_ID (it->f, it->image_id);
17685 xassert (img);
17687 /* Make sure X resources of the face and image are loaded. */
17688 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17689 prepare_image_for_display (it->f, img);
17691 it->ascent = it->phys_ascent = image_ascent (img, face);
17692 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17693 it->pixel_width = img->width + 2 * img->hmargin;
17695 it->nglyphs = 1;
17697 if (face->box != FACE_NO_BOX)
17699 if (face->box_line_width > 0)
17701 it->ascent += face->box_line_width;
17702 it->descent += face->box_line_width;
17705 if (it->start_of_box_run_p)
17706 it->pixel_width += abs (face->box_line_width);
17707 if (it->end_of_box_run_p)
17708 it->pixel_width += abs (face->box_line_width);
17711 take_vertical_position_into_account (it);
17713 if (it->glyph_row)
17715 struct glyph *glyph;
17716 enum glyph_row_area area = it->area;
17718 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17719 if (glyph < it->glyph_row->glyphs[area + 1])
17721 glyph->charpos = CHARPOS (it->position);
17722 glyph->object = it->object;
17723 glyph->pixel_width = it->pixel_width;
17724 glyph->voffset = it->voffset;
17725 glyph->type = IMAGE_GLYPH;
17726 glyph->multibyte_p = it->multibyte_p;
17727 glyph->left_box_line_p = it->start_of_box_run_p;
17728 glyph->right_box_line_p = it->end_of_box_run_p;
17729 glyph->overlaps_vertically_p = 0;
17730 glyph->padding_p = 0;
17731 glyph->glyph_not_available_p = 0;
17732 glyph->face_id = it->face_id;
17733 glyph->u.img_id = img->id;
17734 glyph->font_type = FONT_TYPE_UNKNOWN;
17735 ++it->glyph_row->used[area];
17741 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17742 of the glyph, WIDTH and HEIGHT are the width and height of the
17743 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17744 ascent of the glyph (0 <= ASCENT <= 1). */
17746 static void
17747 append_stretch_glyph (it, object, width, height, ascent)
17748 struct it *it;
17749 Lisp_Object object;
17750 int width, height;
17751 double ascent;
17753 struct glyph *glyph;
17754 enum glyph_row_area area = it->area;
17756 xassert (ascent >= 0 && ascent <= 1);
17758 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17759 if (glyph < it->glyph_row->glyphs[area + 1])
17761 glyph->charpos = CHARPOS (it->position);
17762 glyph->object = object;
17763 glyph->pixel_width = width;
17764 glyph->voffset = it->voffset;
17765 glyph->type = STRETCH_GLYPH;
17766 glyph->multibyte_p = it->multibyte_p;
17767 glyph->left_box_line_p = it->start_of_box_run_p;
17768 glyph->right_box_line_p = it->end_of_box_run_p;
17769 glyph->overlaps_vertically_p = 0;
17770 glyph->padding_p = 0;
17771 glyph->glyph_not_available_p = 0;
17772 glyph->face_id = it->face_id;
17773 glyph->u.stretch.ascent = height * ascent;
17774 glyph->u.stretch.height = height;
17775 glyph->font_type = FONT_TYPE_UNKNOWN;
17776 ++it->glyph_row->used[area];
17781 /* Produce a stretch glyph for iterator IT. IT->object is the value
17782 of the glyph property displayed. The value must be a list
17783 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17784 being recognized:
17786 1. `:width WIDTH' specifies that the space should be WIDTH *
17787 canonical char width wide. WIDTH may be an integer or floating
17788 point number.
17790 2. `:relative-width FACTOR' specifies that the width of the stretch
17791 should be computed from the width of the first character having the
17792 `glyph' property, and should be FACTOR times that width.
17794 3. `:align-to HPOS' specifies that the space should be wide enough
17795 to reach HPOS, a value in canonical character units.
17797 Exactly one of the above pairs must be present.
17799 4. `:height HEIGHT' specifies that the height of the stretch produced
17800 should be HEIGHT, measured in canonical character units.
17802 5. `:relative-height FACTOR' specifies that the height of the
17803 stretch should be FACTOR times the height of the characters having
17804 the glyph property.
17806 Either none or exactly one of 4 or 5 must be present.
17808 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17809 of the stretch should be used for the ascent of the stretch.
17810 ASCENT must be in the range 0 <= ASCENT <= 100. */
17812 #define NUMVAL(X) \
17813 ((INTEGERP (X) || FLOATP (X)) \
17814 ? XFLOATINT (X) \
17815 : - 1)
17818 static void
17819 produce_stretch_glyph (it)
17820 struct it *it;
17822 /* (space :width WIDTH :height HEIGHT. */
17823 Lisp_Object prop, plist;
17824 int width = 0, height = 0;
17825 double ascent = 0;
17826 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17827 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17829 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17831 /* List should start with `space'. */
17832 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17833 plist = XCDR (it->object);
17835 /* Compute the width of the stretch. */
17836 if (prop = Fplist_get (plist, QCwidth),
17837 NUMVAL (prop) > 0)
17838 /* Absolute width `:width WIDTH' specified and valid. */
17839 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f);
17840 else if (prop = Fplist_get (plist, QCrelative_width),
17841 NUMVAL (prop) > 0)
17843 /* Relative width `:relative-width FACTOR' specified and valid.
17844 Compute the width of the characters having the `glyph'
17845 property. */
17846 struct it it2;
17847 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17849 it2 = *it;
17850 if (it->multibyte_p)
17852 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17853 - IT_BYTEPOS (*it));
17854 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17856 else
17857 it2.c = *p, it2.len = 1;
17859 it2.glyph_row = NULL;
17860 it2.what = IT_CHARACTER;
17861 x_produce_glyphs (&it2);
17862 width = NUMVAL (prop) * it2.pixel_width;
17864 else if (prop = Fplist_get (plist, QCalign_to),
17865 NUMVAL (prop) > 0)
17866 width = NUMVAL (prop) * FRAME_COLUMN_WIDTH (it->f) - it->current_x;
17867 else
17868 /* Nothing specified -> width defaults to canonical char width. */
17869 width = FRAME_COLUMN_WIDTH (it->f);
17871 /* Compute height. */
17872 if (prop = Fplist_get (plist, QCheight),
17873 NUMVAL (prop) > 0)
17874 height = NUMVAL (prop) * FRAME_LINE_HEIGHT (it->f);
17875 else if (prop = Fplist_get (plist, QCrelative_height),
17876 NUMVAL (prop) > 0)
17877 height = FONT_HEIGHT (font) * NUMVAL (prop);
17878 else
17879 height = FONT_HEIGHT (font);
17881 /* Compute percentage of height used for ascent. If
17882 `:ascent ASCENT' is present and valid, use that. Otherwise,
17883 derive the ascent from the font in use. */
17884 if (prop = Fplist_get (plist, QCascent),
17885 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17886 ascent = NUMVAL (prop) / 100.0;
17887 else
17888 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17890 if (width <= 0)
17891 width = 1;
17892 if (height <= 0)
17893 height = 1;
17895 if (it->glyph_row)
17897 Lisp_Object object = it->stack[it->sp - 1].string;
17898 if (!STRINGP (object))
17899 object = it->w->buffer;
17900 append_stretch_glyph (it, object, width, height, ascent);
17903 it->pixel_width = width;
17904 it->ascent = it->phys_ascent = height * ascent;
17905 it->descent = it->phys_descent = height - it->ascent;
17906 it->nglyphs = 1;
17908 if (face->box != FACE_NO_BOX)
17910 if (face->box_line_width > 0)
17912 it->ascent += face->box_line_width;
17913 it->descent += face->box_line_width;
17916 if (it->start_of_box_run_p)
17917 it->pixel_width += abs (face->box_line_width);
17918 if (it->end_of_box_run_p)
17919 it->pixel_width += abs (face->box_line_width);
17922 take_vertical_position_into_account (it);
17925 /* RIF:
17926 Produce glyphs/get display metrics for the display element IT is
17927 loaded with. See the description of struct display_iterator in
17928 dispextern.h for an overview of struct display_iterator. */
17930 void
17931 x_produce_glyphs (it)
17932 struct it *it;
17934 it->glyph_not_available_p = 0;
17936 if (it->what == IT_CHARACTER)
17938 XChar2b char2b;
17939 XFontStruct *font;
17940 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17941 XCharStruct *pcm;
17942 int font_not_found_p;
17943 struct font_info *font_info;
17944 int boff; /* baseline offset */
17945 /* We may change it->multibyte_p upon unibyte<->multibyte
17946 conversion. So, save the current value now and restore it
17947 later.
17949 Note: It seems that we don't have to record multibyte_p in
17950 struct glyph because the character code itself tells if or
17951 not the character is multibyte. Thus, in the future, we must
17952 consider eliminating the field `multibyte_p' in the struct
17953 glyph. */
17954 int saved_multibyte_p = it->multibyte_p;
17956 /* Maybe translate single-byte characters to multibyte, or the
17957 other way. */
17958 it->char_to_display = it->c;
17959 if (!ASCII_BYTE_P (it->c))
17961 if (unibyte_display_via_language_environment
17962 && SINGLE_BYTE_CHAR_P (it->c)
17963 && (it->c >= 0240
17964 || !NILP (Vnonascii_translation_table)))
17966 it->char_to_display = unibyte_char_to_multibyte (it->c);
17967 it->multibyte_p = 1;
17968 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17969 face = FACE_FROM_ID (it->f, it->face_id);
17971 else if (!SINGLE_BYTE_CHAR_P (it->c)
17972 && !it->multibyte_p)
17974 it->multibyte_p = 1;
17975 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17976 face = FACE_FROM_ID (it->f, it->face_id);
17980 /* Get font to use. Encode IT->char_to_display. */
17981 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17982 &char2b, it->multibyte_p, 0);
17983 font = face->font;
17985 /* When no suitable font found, use the default font. */
17986 font_not_found_p = font == NULL;
17987 if (font_not_found_p)
17989 font = FRAME_FONT (it->f);
17990 boff = FRAME_BASELINE_OFFSET (it->f);
17991 font_info = NULL;
17993 else
17995 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17996 boff = font_info->baseline_offset;
17997 if (font_info->vertical_centering)
17998 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18001 if (it->char_to_display >= ' '
18002 && (!it->multibyte_p || it->char_to_display < 128))
18004 /* Either unibyte or ASCII. */
18005 int stretched_p;
18007 it->nglyphs = 1;
18009 pcm = rif->per_char_metric (font, &char2b,
18010 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
18011 it->ascent = FONT_BASE (font) + boff;
18012 it->descent = FONT_DESCENT (font) - boff;
18014 if (pcm)
18016 it->phys_ascent = pcm->ascent + boff;
18017 it->phys_descent = pcm->descent - boff;
18018 it->pixel_width = pcm->width;
18020 else
18022 it->glyph_not_available_p = 1;
18023 it->phys_ascent = FONT_BASE (font) + boff;
18024 it->phys_descent = FONT_DESCENT (font) - boff;
18025 it->pixel_width = FONT_WIDTH (font);
18028 /* If this is a space inside a region of text with
18029 `space-width' property, change its width. */
18030 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
18031 if (stretched_p)
18032 it->pixel_width *= XFLOATINT (it->space_width);
18034 /* If face has a box, add the box thickness to the character
18035 height. If character has a box line to the left and/or
18036 right, add the box line width to the character's width. */
18037 if (face->box != FACE_NO_BOX)
18039 int thick = face->box_line_width;
18041 if (thick > 0)
18043 it->ascent += thick;
18044 it->descent += thick;
18046 else
18047 thick = -thick;
18049 if (it->start_of_box_run_p)
18050 it->pixel_width += thick;
18051 if (it->end_of_box_run_p)
18052 it->pixel_width += thick;
18055 /* If face has an overline, add the height of the overline
18056 (1 pixel) and a 1 pixel margin to the character height. */
18057 if (face->overline_p)
18058 it->ascent += 2;
18060 take_vertical_position_into_account (it);
18062 /* If we have to actually produce glyphs, do it. */
18063 if (it->glyph_row)
18065 if (stretched_p)
18067 /* Translate a space with a `space-width' property
18068 into a stretch glyph. */
18069 double ascent = (double) FONT_BASE (font)
18070 / FONT_HEIGHT (font);
18071 append_stretch_glyph (it, it->object, it->pixel_width,
18072 it->ascent + it->descent, ascent);
18074 else
18075 append_glyph (it);
18077 /* If characters with lbearing or rbearing are displayed
18078 in this line, record that fact in a flag of the
18079 glyph row. This is used to optimize X output code. */
18080 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
18081 it->glyph_row->contains_overlapping_glyphs_p = 1;
18084 else if (it->char_to_display == '\n')
18086 /* A newline has no width but we need the height of the line. */
18087 it->pixel_width = 0;
18088 it->nglyphs = 0;
18089 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18090 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18092 if (face->box != FACE_NO_BOX
18093 && face->box_line_width > 0)
18095 it->ascent += face->box_line_width;
18096 it->descent += face->box_line_width;
18099 else if (it->char_to_display == '\t')
18101 int tab_width = it->tab_width * FRAME_COLUMN_WIDTH (it->f);
18102 int x = it->current_x + it->continuation_lines_width;
18103 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
18105 /* If the distance from the current position to the next tab
18106 stop is less than a canonical character width, use the
18107 tab stop after that. */
18108 if (next_tab_x - x < FRAME_COLUMN_WIDTH (it->f))
18109 next_tab_x += tab_width;
18111 it->pixel_width = next_tab_x - x;
18112 it->nglyphs = 1;
18113 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
18114 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
18116 if (it->glyph_row)
18118 double ascent = (double) it->ascent / (it->ascent + it->descent);
18119 append_stretch_glyph (it, it->object, it->pixel_width,
18120 it->ascent + it->descent, ascent);
18123 else
18125 /* A multi-byte character. Assume that the display width of the
18126 character is the width of the character multiplied by the
18127 width of the font. */
18129 /* If we found a font, this font should give us the right
18130 metrics. If we didn't find a font, use the frame's
18131 default font and calculate the width of the character
18132 from the charset width; this is what old redisplay code
18133 did. */
18135 pcm = rif->per_char_metric (font, &char2b,
18136 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
18138 if (font_not_found_p || !pcm)
18140 int charset = CHAR_CHARSET (it->char_to_display);
18142 it->glyph_not_available_p = 1;
18143 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
18144 * CHARSET_WIDTH (charset));
18145 it->phys_ascent = FONT_BASE (font) + boff;
18146 it->phys_descent = FONT_DESCENT (font) - boff;
18148 else
18150 it->pixel_width = pcm->width;
18151 it->phys_ascent = pcm->ascent + boff;
18152 it->phys_descent = pcm->descent - boff;
18153 if (it->glyph_row
18154 && (pcm->lbearing < 0
18155 || pcm->rbearing > pcm->width))
18156 it->glyph_row->contains_overlapping_glyphs_p = 1;
18158 it->nglyphs = 1;
18159 it->ascent = FONT_BASE (font) + boff;
18160 it->descent = FONT_DESCENT (font) - boff;
18161 if (face->box != FACE_NO_BOX)
18163 int thick = face->box_line_width;
18165 if (thick > 0)
18167 it->ascent += thick;
18168 it->descent += thick;
18170 else
18171 thick = - thick;
18173 if (it->start_of_box_run_p)
18174 it->pixel_width += thick;
18175 if (it->end_of_box_run_p)
18176 it->pixel_width += thick;
18179 /* If face has an overline, add the height of the overline
18180 (1 pixel) and a 1 pixel margin to the character height. */
18181 if (face->overline_p)
18182 it->ascent += 2;
18184 take_vertical_position_into_account (it);
18186 if (it->glyph_row)
18187 append_glyph (it);
18189 it->multibyte_p = saved_multibyte_p;
18191 else if (it->what == IT_COMPOSITION)
18193 /* Note: A composition is represented as one glyph in the
18194 glyph matrix. There are no padding glyphs. */
18195 XChar2b char2b;
18196 XFontStruct *font;
18197 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18198 XCharStruct *pcm;
18199 int font_not_found_p;
18200 struct font_info *font_info;
18201 int boff; /* baseline offset */
18202 struct composition *cmp = composition_table[it->cmp_id];
18204 /* Maybe translate single-byte characters to multibyte. */
18205 it->char_to_display = it->c;
18206 if (unibyte_display_via_language_environment
18207 && SINGLE_BYTE_CHAR_P (it->c)
18208 && (it->c >= 0240
18209 || (it->c >= 0200
18210 && !NILP (Vnonascii_translation_table))))
18212 it->char_to_display = unibyte_char_to_multibyte (it->c);
18215 /* Get face and font to use. Encode IT->char_to_display. */
18216 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18217 face = FACE_FROM_ID (it->f, it->face_id);
18218 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18219 &char2b, it->multibyte_p, 0);
18220 font = face->font;
18222 /* When no suitable font found, use the default font. */
18223 font_not_found_p = font == NULL;
18224 if (font_not_found_p)
18226 font = FRAME_FONT (it->f);
18227 boff = FRAME_BASELINE_OFFSET (it->f);
18228 font_info = NULL;
18230 else
18232 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18233 boff = font_info->baseline_offset;
18234 if (font_info->vertical_centering)
18235 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18238 /* There are no padding glyphs, so there is only one glyph to
18239 produce for the composition. Important is that pixel_width,
18240 ascent and descent are the values of what is drawn by
18241 draw_glyphs (i.e. the values of the overall glyphs composed). */
18242 it->nglyphs = 1;
18244 /* If we have not yet calculated pixel size data of glyphs of
18245 the composition for the current face font, calculate them
18246 now. Theoretically, we have to check all fonts for the
18247 glyphs, but that requires much time and memory space. So,
18248 here we check only the font of the first glyph. This leads
18249 to incorrect display very rarely, and C-l (recenter) can
18250 correct the display anyway. */
18251 if (cmp->font != (void *) font)
18253 /* Ascent and descent of the font of the first character of
18254 this composition (adjusted by baseline offset). Ascent
18255 and descent of overall glyphs should not be less than
18256 them respectively. */
18257 int font_ascent = FONT_BASE (font) + boff;
18258 int font_descent = FONT_DESCENT (font) - boff;
18259 /* Bounding box of the overall glyphs. */
18260 int leftmost, rightmost, lowest, highest;
18261 int i, width, ascent, descent;
18263 cmp->font = (void *) font;
18265 /* Initialize the bounding box. */
18266 if (font_info
18267 && (pcm = rif->per_char_metric (font, &char2b,
18268 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18270 width = pcm->width;
18271 ascent = pcm->ascent;
18272 descent = pcm->descent;
18274 else
18276 width = FONT_WIDTH (font);
18277 ascent = FONT_BASE (font);
18278 descent = FONT_DESCENT (font);
18281 rightmost = width;
18282 lowest = - descent + boff;
18283 highest = ascent + boff;
18284 leftmost = 0;
18286 if (font_info
18287 && font_info->default_ascent
18288 && CHAR_TABLE_P (Vuse_default_ascent)
18289 && !NILP (Faref (Vuse_default_ascent,
18290 make_number (it->char_to_display))))
18291 highest = font_info->default_ascent + boff;
18293 /* Draw the first glyph at the normal position. It may be
18294 shifted to right later if some other glyphs are drawn at
18295 the left. */
18296 cmp->offsets[0] = 0;
18297 cmp->offsets[1] = boff;
18299 /* Set cmp->offsets for the remaining glyphs. */
18300 for (i = 1; i < cmp->glyph_len; i++)
18302 int left, right, btm, top;
18303 int ch = COMPOSITION_GLYPH (cmp, i);
18304 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18306 face = FACE_FROM_ID (it->f, face_id);
18307 get_char_face_and_encoding (it->f, ch, face->id,
18308 &char2b, it->multibyte_p, 0);
18309 font = face->font;
18310 if (font == NULL)
18312 font = FRAME_FONT (it->f);
18313 boff = FRAME_BASELINE_OFFSET (it->f);
18314 font_info = NULL;
18316 else
18318 font_info
18319 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18320 boff = font_info->baseline_offset;
18321 if (font_info->vertical_centering)
18322 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18325 if (font_info
18326 && (pcm = rif->per_char_metric (font, &char2b,
18327 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18329 width = pcm->width;
18330 ascent = pcm->ascent;
18331 descent = pcm->descent;
18333 else
18335 width = FONT_WIDTH (font);
18336 ascent = 1;
18337 descent = 0;
18340 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18342 /* Relative composition with or without
18343 alternate chars. */
18344 left = (leftmost + rightmost - width) / 2;
18345 btm = - descent + boff;
18346 if (font_info && font_info->relative_compose
18347 && (! CHAR_TABLE_P (Vignore_relative_composition)
18348 || NILP (Faref (Vignore_relative_composition,
18349 make_number (ch)))))
18352 if (- descent >= font_info->relative_compose)
18353 /* One extra pixel between two glyphs. */
18354 btm = highest + 1;
18355 else if (ascent <= 0)
18356 /* One extra pixel between two glyphs. */
18357 btm = lowest - 1 - ascent - descent;
18360 else
18362 /* A composition rule is specified by an integer
18363 value that encodes global and new reference
18364 points (GREF and NREF). GREF and NREF are
18365 specified by numbers as below:
18367 0---1---2 -- ascent
18371 9--10--11 -- center
18373 ---3---4---5--- baseline
18375 6---7---8 -- descent
18377 int rule = COMPOSITION_RULE (cmp, i);
18378 int gref, nref, grefx, grefy, nrefx, nrefy;
18380 COMPOSITION_DECODE_RULE (rule, gref, nref);
18381 grefx = gref % 3, nrefx = nref % 3;
18382 grefy = gref / 3, nrefy = nref / 3;
18384 left = (leftmost
18385 + grefx * (rightmost - leftmost) / 2
18386 - nrefx * width / 2);
18387 btm = ((grefy == 0 ? highest
18388 : grefy == 1 ? 0
18389 : grefy == 2 ? lowest
18390 : (highest + lowest) / 2)
18391 - (nrefy == 0 ? ascent + descent
18392 : nrefy == 1 ? descent - boff
18393 : nrefy == 2 ? 0
18394 : (ascent + descent) / 2));
18397 cmp->offsets[i * 2] = left;
18398 cmp->offsets[i * 2 + 1] = btm + descent;
18400 /* Update the bounding box of the overall glyphs. */
18401 right = left + width;
18402 top = btm + descent + ascent;
18403 if (left < leftmost)
18404 leftmost = left;
18405 if (right > rightmost)
18406 rightmost = right;
18407 if (top > highest)
18408 highest = top;
18409 if (btm < lowest)
18410 lowest = btm;
18413 /* If there are glyphs whose x-offsets are negative,
18414 shift all glyphs to the right and make all x-offsets
18415 non-negative. */
18416 if (leftmost < 0)
18418 for (i = 0; i < cmp->glyph_len; i++)
18419 cmp->offsets[i * 2] -= leftmost;
18420 rightmost -= leftmost;
18423 cmp->pixel_width = rightmost;
18424 cmp->ascent = highest;
18425 cmp->descent = - lowest;
18426 if (cmp->ascent < font_ascent)
18427 cmp->ascent = font_ascent;
18428 if (cmp->descent < font_descent)
18429 cmp->descent = font_descent;
18432 it->pixel_width = cmp->pixel_width;
18433 it->ascent = it->phys_ascent = cmp->ascent;
18434 it->descent = it->phys_descent = cmp->descent;
18436 if (face->box != FACE_NO_BOX)
18438 int thick = face->box_line_width;
18440 if (thick > 0)
18442 it->ascent += thick;
18443 it->descent += thick;
18445 else
18446 thick = - thick;
18448 if (it->start_of_box_run_p)
18449 it->pixel_width += thick;
18450 if (it->end_of_box_run_p)
18451 it->pixel_width += thick;
18454 /* If face has an overline, add the height of the overline
18455 (1 pixel) and a 1 pixel margin to the character height. */
18456 if (face->overline_p)
18457 it->ascent += 2;
18459 take_vertical_position_into_account (it);
18461 if (it->glyph_row)
18462 append_composite_glyph (it);
18464 else if (it->what == IT_IMAGE)
18465 produce_image_glyph (it);
18466 else if (it->what == IT_STRETCH)
18467 produce_stretch_glyph (it);
18469 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18470 because this isn't true for images with `:ascent 100'. */
18471 xassert (it->ascent >= 0 && it->descent >= 0);
18472 if (it->area == TEXT_AREA)
18473 it->current_x += it->pixel_width;
18475 it->descent += it->extra_line_spacing;
18477 it->max_ascent = max (it->max_ascent, it->ascent);
18478 it->max_descent = max (it->max_descent, it->descent);
18479 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18480 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18483 /* EXPORT for RIF:
18484 Output LEN glyphs starting at START at the nominal cursor position.
18485 Advance the nominal cursor over the text. The global variable
18486 updated_window contains the window being updated, updated_row is
18487 the glyph row being updated, and updated_area is the area of that
18488 row being updated. */
18490 void
18491 x_write_glyphs (start, len)
18492 struct glyph *start;
18493 int len;
18495 int x, hpos;
18497 xassert (updated_window && updated_row);
18498 BLOCK_INPUT;
18500 /* Write glyphs. */
18502 hpos = start - updated_row->glyphs[updated_area];
18503 x = draw_glyphs (updated_window, output_cursor.x,
18504 updated_row, updated_area,
18505 hpos, hpos + len,
18506 DRAW_NORMAL_TEXT, 0);
18508 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18509 if (updated_area == TEXT_AREA
18510 && updated_window->phys_cursor_on_p
18511 && updated_window->phys_cursor.vpos == output_cursor.vpos
18512 && updated_window->phys_cursor.hpos >= hpos
18513 && updated_window->phys_cursor.hpos < hpos + len)
18514 updated_window->phys_cursor_on_p = 0;
18516 UNBLOCK_INPUT;
18518 /* Advance the output cursor. */
18519 output_cursor.hpos += len;
18520 output_cursor.x = x;
18524 /* EXPORT for RIF:
18525 Insert LEN glyphs from START at the nominal cursor position. */
18527 void
18528 x_insert_glyphs (start, len)
18529 struct glyph *start;
18530 int len;
18532 struct frame *f;
18533 struct window *w;
18534 int line_height, shift_by_width, shifted_region_width;
18535 struct glyph_row *row;
18536 struct glyph *glyph;
18537 int frame_x, frame_y, hpos;
18539 xassert (updated_window && updated_row);
18540 BLOCK_INPUT;
18541 w = updated_window;
18542 f = XFRAME (WINDOW_FRAME (w));
18544 /* Get the height of the line we are in. */
18545 row = updated_row;
18546 line_height = row->height;
18548 /* Get the width of the glyphs to insert. */
18549 shift_by_width = 0;
18550 for (glyph = start; glyph < start + len; ++glyph)
18551 shift_by_width += glyph->pixel_width;
18553 /* Get the width of the region to shift right. */
18554 shifted_region_width = (window_box_width (w, updated_area)
18555 - output_cursor.x
18556 - shift_by_width);
18558 /* Shift right. */
18559 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18560 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18562 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18563 line_height, shift_by_width);
18565 /* Write the glyphs. */
18566 hpos = start - row->glyphs[updated_area];
18567 draw_glyphs (w, output_cursor.x, row, updated_area,
18568 hpos, hpos + len,
18569 DRAW_NORMAL_TEXT, 0);
18571 /* Advance the output cursor. */
18572 output_cursor.hpos += len;
18573 output_cursor.x += shift_by_width;
18574 UNBLOCK_INPUT;
18578 /* EXPORT for RIF:
18579 Erase the current text line from the nominal cursor position
18580 (inclusive) to pixel column TO_X (exclusive). The idea is that
18581 everything from TO_X onward is already erased.
18583 TO_X is a pixel position relative to updated_area of
18584 updated_window. TO_X == -1 means clear to the end of this area. */
18586 void
18587 x_clear_end_of_line (to_x)
18588 int to_x;
18590 struct frame *f;
18591 struct window *w = updated_window;
18592 int max_x, min_y, max_y;
18593 int from_x, from_y, to_y;
18595 xassert (updated_window && updated_row);
18596 f = XFRAME (w->frame);
18598 if (updated_row->full_width_p)
18599 max_x = WINDOW_TOTAL_WIDTH (w);
18600 else
18601 max_x = window_box_width (w, updated_area);
18602 max_y = window_text_bottom_y (w);
18604 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18605 of window. For TO_X > 0, truncate to end of drawing area. */
18606 if (to_x == 0)
18607 return;
18608 else if (to_x < 0)
18609 to_x = max_x;
18610 else
18611 to_x = min (to_x, max_x);
18613 to_y = min (max_y, output_cursor.y + updated_row->height);
18615 /* Notice if the cursor will be cleared by this operation. */
18616 if (!updated_row->full_width_p)
18617 notice_overwritten_cursor (w, updated_area,
18618 output_cursor.x, -1,
18619 updated_row->y,
18620 MATRIX_ROW_BOTTOM_Y (updated_row));
18622 from_x = output_cursor.x;
18624 /* Translate to frame coordinates. */
18625 if (updated_row->full_width_p)
18627 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18628 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18630 else
18632 int area_left = window_box_left (w, updated_area);
18633 from_x += area_left;
18634 to_x += area_left;
18637 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18638 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18639 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18641 /* Prevent inadvertently clearing to end of the X window. */
18642 if (to_x > from_x && to_y > from_y)
18644 BLOCK_INPUT;
18645 rif->clear_frame_area (f, from_x, from_y,
18646 to_x - from_x, to_y - from_y);
18647 UNBLOCK_INPUT;
18651 #endif /* HAVE_WINDOW_SYSTEM */
18655 /***********************************************************************
18656 Cursor types
18657 ***********************************************************************/
18659 /* Value is the internal representation of the specified cursor type
18660 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18661 of the bar cursor. */
18663 enum text_cursor_kinds
18664 get_specified_cursor_type (arg, width)
18665 Lisp_Object arg;
18666 int *width;
18668 enum text_cursor_kinds type;
18670 if (NILP (arg))
18671 return NO_CURSOR;
18673 if (EQ (arg, Qbox))
18674 return FILLED_BOX_CURSOR;
18676 if (EQ (arg, Qhollow))
18677 return HOLLOW_BOX_CURSOR;
18679 if (EQ (arg, Qbar))
18681 *width = 2;
18682 return BAR_CURSOR;
18685 if (CONSP (arg)
18686 && EQ (XCAR (arg), Qbar)
18687 && INTEGERP (XCDR (arg))
18688 && XINT (XCDR (arg)) >= 0)
18690 *width = XINT (XCDR (arg));
18691 return BAR_CURSOR;
18694 if (EQ (arg, Qhbar))
18696 *width = 2;
18697 return HBAR_CURSOR;
18700 if (CONSP (arg)
18701 && EQ (XCAR (arg), Qhbar)
18702 && INTEGERP (XCDR (arg))
18703 && XINT (XCDR (arg)) >= 0)
18705 *width = XINT (XCDR (arg));
18706 return HBAR_CURSOR;
18709 /* Treat anything unknown as "hollow box cursor".
18710 It was bad to signal an error; people have trouble fixing
18711 .Xdefaults with Emacs, when it has something bad in it. */
18712 type = HOLLOW_BOX_CURSOR;
18714 return type;
18717 /* Set the default cursor types for specified frame. */
18718 void
18719 set_frame_cursor_types (f, arg)
18720 struct frame *f;
18721 Lisp_Object arg;
18723 int width;
18724 Lisp_Object tem;
18726 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18727 FRAME_CURSOR_WIDTH (f) = width;
18729 /* By default, set up the blink-off state depending on the on-state. */
18731 tem = Fassoc (arg, Vblink_cursor_alist);
18732 if (!NILP (tem))
18734 FRAME_BLINK_OFF_CURSOR (f)
18735 = get_specified_cursor_type (XCDR (tem), &width);
18736 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18738 else
18739 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18743 /* Return the cursor we want to be displayed in window W. Return
18744 width of bar/hbar cursor through WIDTH arg. Return with
18745 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18746 (i.e. if the `system caret' should track this cursor).
18748 In a mini-buffer window, we want the cursor only to appear if we
18749 are reading input from this window. For the selected window, we
18750 want the cursor type given by the frame parameter or buffer local
18751 setting of cursor-type. If explicitly marked off, draw no cursor.
18752 In all other cases, we want a hollow box cursor. */
18754 enum text_cursor_kinds
18755 get_window_cursor_type (w, width, active_cursor)
18756 struct window *w;
18757 int *width;
18758 int *active_cursor;
18760 struct frame *f = XFRAME (w->frame);
18761 struct buffer *b = XBUFFER (w->buffer);
18762 int cursor_type = DEFAULT_CURSOR;
18763 Lisp_Object alt_cursor;
18764 int non_selected = 0;
18766 *active_cursor = 1;
18768 /* Echo area */
18769 if (cursor_in_echo_area
18770 && FRAME_HAS_MINIBUF_P (f)
18771 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18773 if (w == XWINDOW (echo_area_window))
18775 *width = FRAME_CURSOR_WIDTH (f);
18776 return FRAME_DESIRED_CURSOR (f);
18779 *active_cursor = 0;
18780 non_selected = 1;
18783 /* Nonselected window or nonselected frame. */
18784 else if (w != XWINDOW (f->selected_window)
18785 #ifdef HAVE_WINDOW_SYSTEM
18786 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18787 #endif
18790 *active_cursor = 0;
18792 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18793 return NO_CURSOR;
18795 non_selected = 1;
18798 /* Never display a cursor in a window in which cursor-type is nil. */
18799 if (NILP (b->cursor_type))
18800 return NO_CURSOR;
18802 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18803 if (non_selected)
18805 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18806 return get_specified_cursor_type (alt_cursor, width);
18809 /* Get the normal cursor type for this window. */
18810 if (EQ (b->cursor_type, Qt))
18812 cursor_type = FRAME_DESIRED_CURSOR (f);
18813 *width = FRAME_CURSOR_WIDTH (f);
18815 else
18816 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18818 /* Use normal cursor if not blinked off. */
18819 if (!w->cursor_off_p)
18820 return cursor_type;
18822 /* Cursor is blinked off, so determine how to "toggle" it. */
18824 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18825 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18826 return get_specified_cursor_type (XCDR (alt_cursor), width);
18828 /* Then see if frame has specified a specific blink off cursor type. */
18829 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18831 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18832 return FRAME_BLINK_OFF_CURSOR (f);
18835 #if 0
18836 /* Some people liked having a permanently visible blinking cursor,
18837 while others had very strong opinions against it. So it was
18838 decided to remove it. KFS 2003-09-03 */
18840 /* Finally perform built-in cursor blinking:
18841 filled box <-> hollow box
18842 wide [h]bar <-> narrow [h]bar
18843 narrow [h]bar <-> no cursor
18844 other type <-> no cursor */
18846 if (cursor_type == FILLED_BOX_CURSOR)
18847 return HOLLOW_BOX_CURSOR;
18849 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18851 *width = 1;
18852 return cursor_type;
18854 #endif
18856 return NO_CURSOR;
18860 #ifdef HAVE_WINDOW_SYSTEM
18862 /* Notice when the text cursor of window W has been completely
18863 overwritten by a drawing operation that outputs glyphs in AREA
18864 starting at X0 and ending at X1 in the line starting at Y0 and
18865 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18866 the rest of the line after X0 has been written. Y coordinates
18867 are window-relative. */
18869 static void
18870 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18871 struct window *w;
18872 enum glyph_row_area area;
18873 int x0, y0, x1, y1;
18875 if (area == TEXT_AREA && w->phys_cursor_on_p)
18877 int cx0 = w->phys_cursor.x;
18878 int cx1 = cx0 + w->phys_cursor_width;
18879 int cy0 = w->phys_cursor.y;
18880 int cy1 = cy0 + w->phys_cursor_height;
18882 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18884 /* The cursor image will be completely removed from the
18885 screen if the output area intersects the cursor area in
18886 y-direction. When we draw in [y0 y1[, and some part of
18887 the cursor is at y < y0, that part must have been drawn
18888 before. When scrolling, the cursor is erased before
18889 actually scrolling, so we don't come here. When not
18890 scrolling, the rows above the old cursor row must have
18891 changed, and in this case these rows must have written
18892 over the cursor image.
18894 Likewise if part of the cursor is below y1, with the
18895 exception of the cursor being in the first blank row at
18896 the buffer and window end because update_text_area
18897 doesn't draw that row. (Except when it does, but
18898 that's handled in update_text_area.) */
18900 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18901 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18902 w->phys_cursor_on_p = 0;
18907 #endif /* HAVE_WINDOW_SYSTEM */
18910 /************************************************************************
18911 Mouse Face
18912 ************************************************************************/
18914 #ifdef HAVE_WINDOW_SYSTEM
18916 /* EXPORT for RIF:
18917 Fix the display of area AREA of overlapping row ROW in window W. */
18919 void
18920 x_fix_overlapping_area (w, row, area)
18921 struct window *w;
18922 struct glyph_row *row;
18923 enum glyph_row_area area;
18925 int i, x;
18927 BLOCK_INPUT;
18929 x = 0;
18930 for (i = 0; i < row->used[area];)
18932 if (row->glyphs[area][i].overlaps_vertically_p)
18934 int start = i, start_x = x;
18938 x += row->glyphs[area][i].pixel_width;
18939 ++i;
18941 while (i < row->used[area]
18942 && row->glyphs[area][i].overlaps_vertically_p);
18944 draw_glyphs (w, start_x, row, area,
18945 start, i,
18946 DRAW_NORMAL_TEXT, 1);
18948 else
18950 x += row->glyphs[area][i].pixel_width;
18951 ++i;
18955 UNBLOCK_INPUT;
18959 /* EXPORT:
18960 Draw the cursor glyph of window W in glyph row ROW. See the
18961 comment of draw_glyphs for the meaning of HL. */
18963 void
18964 draw_phys_cursor_glyph (w, row, hl)
18965 struct window *w;
18966 struct glyph_row *row;
18967 enum draw_glyphs_face hl;
18969 /* If cursor hpos is out of bounds, don't draw garbage. This can
18970 happen in mini-buffer windows when switching between echo area
18971 glyphs and mini-buffer. */
18972 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18974 int on_p = w->phys_cursor_on_p;
18975 int x1;
18976 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18977 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18978 hl, 0);
18979 w->phys_cursor_on_p = on_p;
18981 if (hl == DRAW_CURSOR)
18982 w->phys_cursor_width = x1 - w->phys_cursor.x;
18983 /* When we erase the cursor, and ROW is overlapped by other
18984 rows, make sure that these overlapping parts of other rows
18985 are redrawn. */
18986 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18988 if (row > w->current_matrix->rows
18989 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18990 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18992 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18993 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18994 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
19000 /* EXPORT:
19001 Erase the image of a cursor of window W from the screen. */
19003 void
19004 erase_phys_cursor (w)
19005 struct window *w;
19007 struct frame *f = XFRAME (w->frame);
19008 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19009 int hpos = w->phys_cursor.hpos;
19010 int vpos = w->phys_cursor.vpos;
19011 int mouse_face_here_p = 0;
19012 struct glyph_matrix *active_glyphs = w->current_matrix;
19013 struct glyph_row *cursor_row;
19014 struct glyph *cursor_glyph;
19015 enum draw_glyphs_face hl;
19017 /* No cursor displayed or row invalidated => nothing to do on the
19018 screen. */
19019 if (w->phys_cursor_type == NO_CURSOR)
19020 goto mark_cursor_off;
19022 /* VPOS >= active_glyphs->nrows means that window has been resized.
19023 Don't bother to erase the cursor. */
19024 if (vpos >= active_glyphs->nrows)
19025 goto mark_cursor_off;
19027 /* If row containing cursor is marked invalid, there is nothing we
19028 can do. */
19029 cursor_row = MATRIX_ROW (active_glyphs, vpos);
19030 if (!cursor_row->enabled_p)
19031 goto mark_cursor_off;
19033 /* If row is completely invisible, don't attempt to delete a cursor which
19034 isn't there. This can happen if cursor is at top of a window, and
19035 we switch to a buffer with a header line in that window. */
19036 if (cursor_row->visible_height <= 0)
19037 goto mark_cursor_off;
19039 /* This can happen when the new row is shorter than the old one.
19040 In this case, either draw_glyphs or clear_end_of_line
19041 should have cleared the cursor. Note that we wouldn't be
19042 able to erase the cursor in this case because we don't have a
19043 cursor glyph at hand. */
19044 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
19045 goto mark_cursor_off;
19047 /* If the cursor is in the mouse face area, redisplay that when
19048 we clear the cursor. */
19049 if (! NILP (dpyinfo->mouse_face_window)
19050 && w == XWINDOW (dpyinfo->mouse_face_window)
19051 && (vpos > dpyinfo->mouse_face_beg_row
19052 || (vpos == dpyinfo->mouse_face_beg_row
19053 && hpos >= dpyinfo->mouse_face_beg_col))
19054 && (vpos < dpyinfo->mouse_face_end_row
19055 || (vpos == dpyinfo->mouse_face_end_row
19056 && hpos < dpyinfo->mouse_face_end_col))
19057 /* Don't redraw the cursor's spot in mouse face if it is at the
19058 end of a line (on a newline). The cursor appears there, but
19059 mouse highlighting does not. */
19060 && cursor_row->used[TEXT_AREA] > hpos)
19061 mouse_face_here_p = 1;
19063 /* Maybe clear the display under the cursor. */
19064 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
19066 int x, y;
19067 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
19069 cursor_glyph = get_phys_cursor_glyph (w);
19070 if (cursor_glyph == NULL)
19071 goto mark_cursor_off;
19073 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
19074 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
19076 rif->clear_frame_area (f, x, y,
19077 cursor_glyph->pixel_width, cursor_row->visible_height);
19080 /* Erase the cursor by redrawing the character underneath it. */
19081 if (mouse_face_here_p)
19082 hl = DRAW_MOUSE_FACE;
19083 else
19084 hl = DRAW_NORMAL_TEXT;
19085 draw_phys_cursor_glyph (w, cursor_row, hl);
19087 mark_cursor_off:
19088 w->phys_cursor_on_p = 0;
19089 w->phys_cursor_type = NO_CURSOR;
19093 /* EXPORT:
19094 Display or clear cursor of window W. If ON is zero, clear the
19095 cursor. If it is non-zero, display the cursor. If ON is nonzero,
19096 where to put the cursor is specified by HPOS, VPOS, X and Y. */
19098 void
19099 display_and_set_cursor (w, on, hpos, vpos, x, y)
19100 struct window *w;
19101 int on, hpos, vpos, x, y;
19103 struct frame *f = XFRAME (w->frame);
19104 int new_cursor_type;
19105 int new_cursor_width;
19106 int active_cursor;
19107 struct glyph_matrix *current_glyphs;
19108 struct glyph_row *glyph_row;
19109 struct glyph *glyph;
19111 /* This is pointless on invisible frames, and dangerous on garbaged
19112 windows and frames; in the latter case, the frame or window may
19113 be in the midst of changing its size, and x and y may be off the
19114 window. */
19115 if (! FRAME_VISIBLE_P (f)
19116 || FRAME_GARBAGED_P (f)
19117 || vpos >= w->current_matrix->nrows
19118 || hpos >= w->current_matrix->matrix_w)
19119 return;
19121 /* If cursor is off and we want it off, return quickly. */
19122 if (!on && !w->phys_cursor_on_p)
19123 return;
19125 current_glyphs = w->current_matrix;
19126 glyph_row = MATRIX_ROW (current_glyphs, vpos);
19127 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
19129 /* If cursor row is not enabled, we don't really know where to
19130 display the cursor. */
19131 if (!glyph_row->enabled_p)
19133 w->phys_cursor_on_p = 0;
19134 return;
19137 xassert (interrupt_input_blocked);
19139 /* Set new_cursor_type to the cursor we want to be displayed. */
19140 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
19142 /* If cursor is currently being shown and we don't want it to be or
19143 it is in the wrong place, or the cursor type is not what we want,
19144 erase it. */
19145 if (w->phys_cursor_on_p
19146 && (!on
19147 || w->phys_cursor.x != x
19148 || w->phys_cursor.y != y
19149 || new_cursor_type != w->phys_cursor_type
19150 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19151 && new_cursor_width != w->phys_cursor_width)))
19152 erase_phys_cursor (w);
19154 /* Don't check phys_cursor_on_p here because that flag is only set
19155 to zero in some cases where we know that the cursor has been
19156 completely erased, to avoid the extra work of erasing the cursor
19157 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19158 still not be visible, or it has only been partly erased. */
19159 if (on)
19161 w->phys_cursor_ascent = glyph_row->ascent;
19162 w->phys_cursor_height = glyph_row->height;
19164 /* Set phys_cursor_.* before x_draw_.* is called because some
19165 of them may need the information. */
19166 w->phys_cursor.x = x;
19167 w->phys_cursor.y = glyph_row->y;
19168 w->phys_cursor.hpos = hpos;
19169 w->phys_cursor.vpos = vpos;
19172 rif->draw_window_cursor (w, glyph_row, x, y,
19173 new_cursor_type, new_cursor_width,
19174 on, active_cursor);
19178 /* Switch the display of W's cursor on or off, according to the value
19179 of ON. */
19181 static void
19182 update_window_cursor (w, on)
19183 struct window *w;
19184 int on;
19186 /* Don't update cursor in windows whose frame is in the process
19187 of being deleted. */
19188 if (w->current_matrix)
19190 BLOCK_INPUT;
19191 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19192 w->phys_cursor.x, w->phys_cursor.y);
19193 UNBLOCK_INPUT;
19198 /* Call update_window_cursor with parameter ON_P on all leaf windows
19199 in the window tree rooted at W. */
19201 static void
19202 update_cursor_in_window_tree (w, on_p)
19203 struct window *w;
19204 int on_p;
19206 while (w)
19208 if (!NILP (w->hchild))
19209 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19210 else if (!NILP (w->vchild))
19211 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19212 else
19213 update_window_cursor (w, on_p);
19215 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19220 /* EXPORT:
19221 Display the cursor on window W, or clear it, according to ON_P.
19222 Don't change the cursor's position. */
19224 void
19225 x_update_cursor (f, on_p)
19226 struct frame *f;
19227 int on_p;
19229 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19233 /* EXPORT:
19234 Clear the cursor of window W to background color, and mark the
19235 cursor as not shown. This is used when the text where the cursor
19236 is is about to be rewritten. */
19238 void
19239 x_clear_cursor (w)
19240 struct window *w;
19242 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19243 update_window_cursor (w, 0);
19247 /* EXPORT:
19248 Display the active region described by mouse_face_* according to DRAW. */
19250 void
19251 show_mouse_face (dpyinfo, draw)
19252 Display_Info *dpyinfo;
19253 enum draw_glyphs_face draw;
19255 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19256 struct frame *f = XFRAME (WINDOW_FRAME (w));
19258 if (/* If window is in the process of being destroyed, don't bother
19259 to do anything. */
19260 w->current_matrix != NULL
19261 /* Don't update mouse highlight if hidden */
19262 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19263 /* Recognize when we are called to operate on rows that don't exist
19264 anymore. This can happen when a window is split. */
19265 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19267 int phys_cursor_on_p = w->phys_cursor_on_p;
19268 struct glyph_row *row, *first, *last;
19270 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19271 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19273 for (row = first; row <= last && row->enabled_p; ++row)
19275 int start_hpos, end_hpos, start_x;
19277 /* For all but the first row, the highlight starts at column 0. */
19278 if (row == first)
19280 start_hpos = dpyinfo->mouse_face_beg_col;
19281 start_x = dpyinfo->mouse_face_beg_x;
19283 else
19285 start_hpos = 0;
19286 start_x = 0;
19289 if (row == last)
19290 end_hpos = dpyinfo->mouse_face_end_col;
19291 else
19292 end_hpos = row->used[TEXT_AREA];
19294 if (end_hpos > start_hpos)
19296 draw_glyphs (w, start_x, row, TEXT_AREA,
19297 start_hpos, end_hpos,
19298 draw, 0);
19300 row->mouse_face_p
19301 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19305 /* When we've written over the cursor, arrange for it to
19306 be displayed again. */
19307 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19309 BLOCK_INPUT;
19310 display_and_set_cursor (w, 1,
19311 w->phys_cursor.hpos, w->phys_cursor.vpos,
19312 w->phys_cursor.x, w->phys_cursor.y);
19313 UNBLOCK_INPUT;
19317 /* Change the mouse cursor. */
19318 if (draw == DRAW_NORMAL_TEXT)
19319 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19320 else if (draw == DRAW_MOUSE_FACE)
19321 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19322 else
19323 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19326 /* EXPORT:
19327 Clear out the mouse-highlighted active region.
19328 Redraw it un-highlighted first. Value is non-zero if mouse
19329 face was actually drawn unhighlighted. */
19332 clear_mouse_face (dpyinfo)
19333 Display_Info *dpyinfo;
19335 int cleared = 0;
19337 if (!NILP (dpyinfo->mouse_face_window))
19339 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19340 cleared = 1;
19343 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19344 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19345 dpyinfo->mouse_face_window = Qnil;
19346 dpyinfo->mouse_face_overlay = Qnil;
19347 return cleared;
19351 /* EXPORT:
19352 Non-zero if physical cursor of window W is within mouse face. */
19355 cursor_in_mouse_face_p (w)
19356 struct window *w;
19358 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19359 int in_mouse_face = 0;
19361 if (WINDOWP (dpyinfo->mouse_face_window)
19362 && XWINDOW (dpyinfo->mouse_face_window) == w)
19364 int hpos = w->phys_cursor.hpos;
19365 int vpos = w->phys_cursor.vpos;
19367 if (vpos >= dpyinfo->mouse_face_beg_row
19368 && vpos <= dpyinfo->mouse_face_end_row
19369 && (vpos > dpyinfo->mouse_face_beg_row
19370 || hpos >= dpyinfo->mouse_face_beg_col)
19371 && (vpos < dpyinfo->mouse_face_end_row
19372 || hpos < dpyinfo->mouse_face_end_col
19373 || dpyinfo->mouse_face_past_end))
19374 in_mouse_face = 1;
19377 return in_mouse_face;
19383 /* Find the glyph matrix position of buffer position CHARPOS in window
19384 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19385 current glyphs must be up to date. If CHARPOS is above window
19386 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19387 of last line in W. In the row containing CHARPOS, stop before glyphs
19388 having STOP as object. */
19390 #if 1 /* This is a version of fast_find_position that's more correct
19391 in the presence of hscrolling, for example. I didn't install
19392 it right away because the problem fixed is minor, it failed
19393 in 20.x as well, and I think it's too risky to install
19394 so near the release of 21.1. 2001-09-25 gerd. */
19396 static int
19397 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19398 struct window *w;
19399 int charpos;
19400 int *hpos, *vpos, *x, *y;
19401 Lisp_Object stop;
19403 struct glyph_row *row, *first;
19404 struct glyph *glyph, *end;
19405 int past_end = 0;
19407 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19408 row = row_containing_pos (w, charpos, first, NULL, 0);
19409 if (row == NULL)
19411 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19413 *x = *y = *hpos = *vpos = 0;
19414 return 0;
19416 else
19418 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19419 past_end = 1;
19423 *x = row->x;
19424 *y = row->y;
19425 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19427 glyph = row->glyphs[TEXT_AREA];
19428 end = glyph + row->used[TEXT_AREA];
19430 /* Skip over glyphs not having an object at the start of the row.
19431 These are special glyphs like truncation marks on terminal
19432 frames. */
19433 if (row->displays_text_p)
19434 while (glyph < end
19435 && INTEGERP (glyph->object)
19436 && !EQ (stop, glyph->object)
19437 && glyph->charpos < 0)
19439 *x += glyph->pixel_width;
19440 ++glyph;
19443 while (glyph < end
19444 && !INTEGERP (glyph->object)
19445 && !EQ (stop, glyph->object)
19446 && (!BUFFERP (glyph->object)
19447 || glyph->charpos < charpos))
19449 *x += glyph->pixel_width;
19450 ++glyph;
19453 *hpos = glyph - row->glyphs[TEXT_AREA];
19454 return past_end;
19457 #else /* not 1 */
19459 static int
19460 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19461 struct window *w;
19462 int pos;
19463 int *hpos, *vpos, *x, *y;
19464 Lisp_Object stop;
19466 int i;
19467 int lastcol;
19468 int maybe_next_line_p = 0;
19469 int line_start_position;
19470 int yb = window_text_bottom_y (w);
19471 struct glyph_row *row, *best_row;
19472 int row_vpos, best_row_vpos;
19473 int current_x;
19475 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19476 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19478 while (row->y < yb)
19480 if (row->used[TEXT_AREA])
19481 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19482 else
19483 line_start_position = 0;
19485 if (line_start_position > pos)
19486 break;
19487 /* If the position sought is the end of the buffer,
19488 don't include the blank lines at the bottom of the window. */
19489 else if (line_start_position == pos
19490 && pos == BUF_ZV (XBUFFER (w->buffer)))
19492 maybe_next_line_p = 1;
19493 break;
19495 else if (line_start_position > 0)
19497 best_row = row;
19498 best_row_vpos = row_vpos;
19501 if (row->y + row->height >= yb)
19502 break;
19504 ++row;
19505 ++row_vpos;
19508 /* Find the right column within BEST_ROW. */
19509 lastcol = 0;
19510 current_x = best_row->x;
19511 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19513 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19514 int charpos = glyph->charpos;
19516 if (BUFFERP (glyph->object))
19518 if (charpos == pos)
19520 *hpos = i;
19521 *vpos = best_row_vpos;
19522 *x = current_x;
19523 *y = best_row->y;
19524 return 1;
19526 else if (charpos > pos)
19527 break;
19529 else if (EQ (glyph->object, stop))
19530 break;
19532 if (charpos > 0)
19533 lastcol = i;
19534 current_x += glyph->pixel_width;
19537 /* If we're looking for the end of the buffer,
19538 and we didn't find it in the line we scanned,
19539 use the start of the following line. */
19540 if (maybe_next_line_p)
19542 ++best_row;
19543 ++best_row_vpos;
19544 lastcol = 0;
19545 current_x = best_row->x;
19548 *vpos = best_row_vpos;
19549 *hpos = lastcol + 1;
19550 *x = current_x;
19551 *y = best_row->y;
19552 return 0;
19555 #endif /* not 1 */
19558 /* Find the position of the glyph for position POS in OBJECT in
19559 window W's current matrix, and return in *X, *Y the pixel
19560 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19562 RIGHT_P non-zero means return the position of the right edge of the
19563 glyph, RIGHT_P zero means return the left edge position.
19565 If no glyph for POS exists in the matrix, return the position of
19566 the glyph with the next smaller position that is in the matrix, if
19567 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19568 exists in the matrix, return the position of the glyph with the
19569 next larger position in OBJECT.
19571 Value is non-zero if a glyph was found. */
19573 static int
19574 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19575 struct window *w;
19576 int pos;
19577 Lisp_Object object;
19578 int *hpos, *vpos, *x, *y;
19579 int right_p;
19581 int yb = window_text_bottom_y (w);
19582 struct glyph_row *r;
19583 struct glyph *best_glyph = NULL;
19584 struct glyph_row *best_row = NULL;
19585 int best_x = 0;
19587 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19588 r->enabled_p && r->y < yb;
19589 ++r)
19591 struct glyph *g = r->glyphs[TEXT_AREA];
19592 struct glyph *e = g + r->used[TEXT_AREA];
19593 int gx;
19595 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19596 if (EQ (g->object, object))
19598 if (g->charpos == pos)
19600 best_glyph = g;
19601 best_x = gx;
19602 best_row = r;
19603 goto found;
19605 else if (best_glyph == NULL
19606 || ((abs (g->charpos - pos)
19607 < abs (best_glyph->charpos - pos))
19608 && (right_p
19609 ? g->charpos < pos
19610 : g->charpos > pos)))
19612 best_glyph = g;
19613 best_x = gx;
19614 best_row = r;
19619 found:
19621 if (best_glyph)
19623 *x = best_x;
19624 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19626 if (right_p)
19628 *x += best_glyph->pixel_width;
19629 ++*hpos;
19632 *y = best_row->y;
19633 *vpos = best_row - w->current_matrix->rows;
19636 return best_glyph != NULL;
19640 /* Take proper action when mouse has moved to the mode or header line
19641 or marginal area AREA of window W, x-position X and y-position Y.
19642 X is relative to the start of the text display area of W, so the
19643 width of bitmap areas and scroll bars must be subtracted to get a
19644 position relative to the start of the mode line. */
19646 static void
19647 note_mode_line_or_margin_highlight (w, x, y, area)
19648 struct window *w;
19649 int x, y;
19650 enum window_part area;
19652 struct frame *f = XFRAME (w->frame);
19653 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19654 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19655 int charpos;
19656 Lisp_Object string, help, map, pos;
19658 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19659 string = mode_line_string (w, x, y, area, &charpos);
19660 else
19661 string = marginal_area_string (w, x, y, area, &charpos);
19663 if (STRINGP (string))
19665 pos = make_number (charpos);
19667 /* If we're on a string with `help-echo' text property, arrange
19668 for the help to be displayed. This is done by setting the
19669 global variable help_echo_string to the help string. */
19670 help = Fget_text_property (pos, Qhelp_echo, string);
19671 if (!NILP (help))
19673 help_echo_string = help;
19674 XSETWINDOW (help_echo_window, w);
19675 help_echo_object = string;
19676 help_echo_pos = charpos;
19679 /* Change the mouse pointer according to what is under X/Y. */
19680 map = Fget_text_property (pos, Qlocal_map, string);
19681 if (!KEYMAPP (map))
19682 map = Fget_text_property (pos, Qkeymap, string);
19683 if (KEYMAPP (map))
19684 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19687 rif->define_frame_cursor (f, cursor);
19691 /* EXPORT:
19692 Take proper action when the mouse has moved to position X, Y on
19693 frame F as regards highlighting characters that have mouse-face
19694 properties. Also de-highlighting chars where the mouse was before.
19695 X and Y can be negative or out of range. */
19697 void
19698 note_mouse_highlight (f, x, y)
19699 struct frame *f;
19700 int x, y;
19702 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19703 enum window_part part;
19704 Lisp_Object window;
19705 struct window *w;
19706 Cursor cursor = No_Cursor;
19707 struct buffer *b;
19709 /* When a menu is active, don't highlight because this looks odd. */
19710 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19711 if (popup_activated ())
19712 return;
19713 #endif
19715 if (NILP (Vmouse_highlight)
19716 || !f->glyphs_initialized_p)
19717 return;
19719 dpyinfo->mouse_face_mouse_x = x;
19720 dpyinfo->mouse_face_mouse_y = y;
19721 dpyinfo->mouse_face_mouse_frame = f;
19723 if (dpyinfo->mouse_face_defer)
19724 return;
19726 if (gc_in_progress)
19728 dpyinfo->mouse_face_deferred_gc = 1;
19729 return;
19732 /* Which window is that in? */
19733 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
19735 /* If we were displaying active text in another window, clear that. */
19736 if (! EQ (window, dpyinfo->mouse_face_window))
19737 clear_mouse_face (dpyinfo);
19739 /* Not on a window -> return. */
19740 if (!WINDOWP (window))
19741 return;
19743 /* Reset help_echo_string. It will get recomputed below. */
19744 /* ++KFS: X version didn't do this, but it looks harmless. */
19745 help_echo_string = Qnil;
19747 /* Convert to window-relative pixel coordinates. */
19748 w = XWINDOW (window);
19749 frame_to_window_pixel_xy (w, &x, &y);
19751 /* Handle tool-bar window differently since it doesn't display a
19752 buffer. */
19753 if (EQ (window, f->tool_bar_window))
19755 note_tool_bar_highlight (f, x, y);
19756 return;
19759 /* Mouse is on the mode, header line or margin? */
19760 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19761 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19763 note_mode_line_or_margin_highlight (w, x, y, part);
19764 return;
19767 if (part == ON_VERTICAL_BORDER)
19768 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19769 else
19770 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19772 /* Are we in a window whose display is up to date?
19773 And verify the buffer's text has not changed. */
19774 b = XBUFFER (w->buffer);
19775 if (part == ON_TEXT
19776 && EQ (w->window_end_valid, w->buffer)
19777 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19778 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19780 int hpos, vpos, pos, i, area;
19781 struct glyph *glyph;
19782 Lisp_Object object;
19783 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19784 Lisp_Object *overlay_vec = NULL;
19785 int len, noverlays;
19786 struct buffer *obuf;
19787 int obegv, ozv, same_region;
19789 /* Find the glyph under X/Y. */
19790 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19792 /* Clear mouse face if X/Y not over text. */
19793 if (glyph == NULL
19794 || area != TEXT_AREA
19795 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19797 #if defined (HAVE_NTGUI)
19798 /* ++KFS: Why is this necessary on W32 ? */
19799 clear_mouse_face (dpyinfo);
19800 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19801 #else
19802 if (clear_mouse_face (dpyinfo))
19803 cursor = No_Cursor;
19804 #endif
19805 goto set_cursor;
19808 pos = glyph->charpos;
19809 object = glyph->object;
19810 if (!STRINGP (object) && !BUFFERP (object))
19811 goto set_cursor;
19813 /* If we get an out-of-range value, return now; avoid an error. */
19814 if (BUFFERP (object) && pos > BUF_Z (b))
19815 goto set_cursor;
19817 /* Make the window's buffer temporarily current for
19818 overlays_at and compute_char_face. */
19819 obuf = current_buffer;
19820 current_buffer = b;
19821 obegv = BEGV;
19822 ozv = ZV;
19823 BEGV = BEG;
19824 ZV = Z;
19826 /* Is this char mouse-active or does it have help-echo? */
19827 position = make_number (pos);
19829 if (BUFFERP (object))
19831 /* Put all the overlays we want in a vector in overlay_vec.
19832 Store the length in len. If there are more than 10, make
19833 enough space for all, and try again. */
19834 len = 10;
19835 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19836 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19837 if (noverlays > len)
19839 len = noverlays;
19840 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19841 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19844 /* Sort overlays into increasing priority order. */
19845 noverlays = sort_overlays (overlay_vec, noverlays, w);
19847 else
19848 noverlays = 0;
19850 same_region = (EQ (window, dpyinfo->mouse_face_window)
19851 && vpos >= dpyinfo->mouse_face_beg_row
19852 && vpos <= dpyinfo->mouse_face_end_row
19853 && (vpos > dpyinfo->mouse_face_beg_row
19854 || hpos >= dpyinfo->mouse_face_beg_col)
19855 && (vpos < dpyinfo->mouse_face_end_row
19856 || hpos < dpyinfo->mouse_face_end_col
19857 || dpyinfo->mouse_face_past_end));
19859 if (same_region)
19860 cursor = No_Cursor;
19862 /* Check mouse-face highlighting. */
19863 if (! same_region
19864 /* If there exists an overlay with mouse-face overlapping
19865 the one we are currently highlighting, we have to
19866 check if we enter the overlapping overlay, and then
19867 highlight only that. */
19868 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19869 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19871 /* Find the highest priority overlay that has a mouse-face
19872 property. */
19873 overlay = Qnil;
19874 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19876 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19877 if (!NILP (mouse_face))
19878 overlay = overlay_vec[i];
19881 /* If we're actually highlighting the same overlay as
19882 before, there's no need to do that again. */
19883 if (!NILP (overlay)
19884 && EQ (overlay, dpyinfo->mouse_face_overlay))
19885 goto check_help_echo;
19887 dpyinfo->mouse_face_overlay = overlay;
19889 /* Clear the display of the old active region, if any. */
19890 if (clear_mouse_face (dpyinfo))
19891 cursor = No_Cursor;
19893 /* If no overlay applies, get a text property. */
19894 if (NILP (overlay))
19895 mouse_face = Fget_text_property (position, Qmouse_face, object);
19897 /* Handle the overlay case. */
19898 if (!NILP (overlay))
19900 /* Find the range of text around this char that
19901 should be active. */
19902 Lisp_Object before, after;
19903 int ignore;
19905 before = Foverlay_start (overlay);
19906 after = Foverlay_end (overlay);
19907 /* Record this as the current active region. */
19908 fast_find_position (w, XFASTINT (before),
19909 &dpyinfo->mouse_face_beg_col,
19910 &dpyinfo->mouse_face_beg_row,
19911 &dpyinfo->mouse_face_beg_x,
19912 &dpyinfo->mouse_face_beg_y, Qnil);
19914 dpyinfo->mouse_face_past_end
19915 = !fast_find_position (w, XFASTINT (after),
19916 &dpyinfo->mouse_face_end_col,
19917 &dpyinfo->mouse_face_end_row,
19918 &dpyinfo->mouse_face_end_x,
19919 &dpyinfo->mouse_face_end_y, Qnil);
19920 dpyinfo->mouse_face_window = window;
19922 dpyinfo->mouse_face_face_id
19923 = face_at_buffer_position (w, pos, 0, 0,
19924 &ignore, pos + 1,
19925 !dpyinfo->mouse_face_hidden);
19927 /* Display it as active. */
19928 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19929 cursor = No_Cursor;
19931 /* Handle the text property case. */
19932 else if (!NILP (mouse_face) && BUFFERP (object))
19934 /* Find the range of text around this char that
19935 should be active. */
19936 Lisp_Object before, after, beginning, end;
19937 int ignore;
19939 beginning = Fmarker_position (w->start);
19940 end = make_number (BUF_Z (XBUFFER (object))
19941 - XFASTINT (w->window_end_pos));
19942 before
19943 = Fprevious_single_property_change (make_number (pos + 1),
19944 Qmouse_face,
19945 object, beginning);
19946 after
19947 = Fnext_single_property_change (position, Qmouse_face,
19948 object, end);
19950 /* Record this as the current active region. */
19951 fast_find_position (w, XFASTINT (before),
19952 &dpyinfo->mouse_face_beg_col,
19953 &dpyinfo->mouse_face_beg_row,
19954 &dpyinfo->mouse_face_beg_x,
19955 &dpyinfo->mouse_face_beg_y, Qnil);
19956 dpyinfo->mouse_face_past_end
19957 = !fast_find_position (w, XFASTINT (after),
19958 &dpyinfo->mouse_face_end_col,
19959 &dpyinfo->mouse_face_end_row,
19960 &dpyinfo->mouse_face_end_x,
19961 &dpyinfo->mouse_face_end_y, Qnil);
19962 dpyinfo->mouse_face_window = window;
19964 if (BUFFERP (object))
19965 dpyinfo->mouse_face_face_id
19966 = face_at_buffer_position (w, pos, 0, 0,
19967 &ignore, pos + 1,
19968 !dpyinfo->mouse_face_hidden);
19970 /* Display it as active. */
19971 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19972 cursor = No_Cursor;
19974 else if (!NILP (mouse_face) && STRINGP (object))
19976 Lisp_Object b, e;
19977 int ignore;
19979 b = Fprevious_single_property_change (make_number (pos + 1),
19980 Qmouse_face,
19981 object, Qnil);
19982 e = Fnext_single_property_change (position, Qmouse_face,
19983 object, Qnil);
19984 if (NILP (b))
19985 b = make_number (0);
19986 if (NILP (e))
19987 e = make_number (SCHARS (object) - 1);
19988 fast_find_string_pos (w, XINT (b), object,
19989 &dpyinfo->mouse_face_beg_col,
19990 &dpyinfo->mouse_face_beg_row,
19991 &dpyinfo->mouse_face_beg_x,
19992 &dpyinfo->mouse_face_beg_y, 0);
19993 fast_find_string_pos (w, XINT (e), object,
19994 &dpyinfo->mouse_face_end_col,
19995 &dpyinfo->mouse_face_end_row,
19996 &dpyinfo->mouse_face_end_x,
19997 &dpyinfo->mouse_face_end_y, 1);
19998 dpyinfo->mouse_face_past_end = 0;
19999 dpyinfo->mouse_face_window = window;
20000 dpyinfo->mouse_face_face_id
20001 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
20002 glyph->face_id, 1);
20003 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20004 cursor = No_Cursor;
20006 else if (STRINGP (object) && NILP (mouse_face))
20008 /* A string which doesn't have mouse-face, but
20009 the text ``under'' it might have. */
20010 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
20011 int start = MATRIX_ROW_START_CHARPOS (r);
20013 pos = string_buffer_position (w, object, start);
20014 if (pos > 0)
20015 mouse_face = get_char_property_and_overlay (make_number (pos),
20016 Qmouse_face,
20017 w->buffer,
20018 &overlay);
20019 if (!NILP (mouse_face) && !NILP (overlay))
20021 Lisp_Object before = Foverlay_start (overlay);
20022 Lisp_Object after = Foverlay_end (overlay);
20023 int ignore;
20025 /* Note that we might not be able to find position
20026 BEFORE in the glyph matrix if the overlay is
20027 entirely covered by a `display' property. In
20028 this case, we overshoot. So let's stop in
20029 the glyph matrix before glyphs for OBJECT. */
20030 fast_find_position (w, XFASTINT (before),
20031 &dpyinfo->mouse_face_beg_col,
20032 &dpyinfo->mouse_face_beg_row,
20033 &dpyinfo->mouse_face_beg_x,
20034 &dpyinfo->mouse_face_beg_y,
20035 object);
20037 dpyinfo->mouse_face_past_end
20038 = !fast_find_position (w, XFASTINT (after),
20039 &dpyinfo->mouse_face_end_col,
20040 &dpyinfo->mouse_face_end_row,
20041 &dpyinfo->mouse_face_end_x,
20042 &dpyinfo->mouse_face_end_y,
20043 Qnil);
20044 dpyinfo->mouse_face_window = window;
20045 dpyinfo->mouse_face_face_id
20046 = face_at_buffer_position (w, pos, 0, 0,
20047 &ignore, pos + 1,
20048 !dpyinfo->mouse_face_hidden);
20050 /* Display it as active. */
20051 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
20052 cursor = No_Cursor;
20057 check_help_echo:
20059 /* Look for a `help-echo' property. */
20061 Lisp_Object help, overlay;
20063 /* Check overlays first. */
20064 help = overlay = Qnil;
20065 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
20067 overlay = overlay_vec[i];
20068 help = Foverlay_get (overlay, Qhelp_echo);
20071 if (!NILP (help))
20073 help_echo_string = help;
20074 help_echo_window = window;
20075 help_echo_object = overlay;
20076 help_echo_pos = pos;
20078 else
20080 Lisp_Object object = glyph->object;
20081 int charpos = glyph->charpos;
20083 /* Try text properties. */
20084 if (STRINGP (object)
20085 && charpos >= 0
20086 && charpos < SCHARS (object))
20088 help = Fget_text_property (make_number (charpos),
20089 Qhelp_echo, object);
20090 if (NILP (help))
20092 /* If the string itself doesn't specify a help-echo,
20093 see if the buffer text ``under'' it does. */
20094 struct glyph_row *r
20095 = MATRIX_ROW (w->current_matrix, vpos);
20096 int start = MATRIX_ROW_START_CHARPOS (r);
20097 int pos = string_buffer_position (w, object, start);
20098 if (pos > 0)
20100 help = Fget_char_property (make_number (pos),
20101 Qhelp_echo, w->buffer);
20102 if (!NILP (help))
20104 charpos = pos;
20105 object = w->buffer;
20110 else if (BUFFERP (object)
20111 && charpos >= BEGV
20112 && charpos < ZV)
20113 help = Fget_text_property (make_number (charpos), Qhelp_echo,
20114 object);
20116 if (!NILP (help))
20118 help_echo_string = help;
20119 help_echo_window = window;
20120 help_echo_object = object;
20121 help_echo_pos = charpos;
20126 BEGV = obegv;
20127 ZV = ozv;
20128 current_buffer = obuf;
20131 set_cursor:
20133 #ifndef HAVE_CARBON
20134 if (cursor != No_Cursor)
20135 #else
20136 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
20137 #endif
20138 rif->define_frame_cursor (f, cursor);
20142 /* EXPORT for RIF:
20143 Clear any mouse-face on window W. This function is part of the
20144 redisplay interface, and is called from try_window_id and similar
20145 functions to ensure the mouse-highlight is off. */
20147 void
20148 x_clear_window_mouse_face (w)
20149 struct window *w;
20151 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20152 Lisp_Object window;
20154 BLOCK_INPUT;
20155 XSETWINDOW (window, w);
20156 if (EQ (window, dpyinfo->mouse_face_window))
20157 clear_mouse_face (dpyinfo);
20158 UNBLOCK_INPUT;
20162 /* EXPORT:
20163 Just discard the mouse face information for frame F, if any.
20164 This is used when the size of F is changed. */
20166 void
20167 cancel_mouse_face (f)
20168 struct frame *f;
20170 Lisp_Object window;
20171 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20173 window = dpyinfo->mouse_face_window;
20174 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20176 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20177 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20178 dpyinfo->mouse_face_window = Qnil;
20183 #endif /* HAVE_WINDOW_SYSTEM */
20186 /***********************************************************************
20187 Exposure Events
20188 ***********************************************************************/
20190 #ifdef HAVE_WINDOW_SYSTEM
20192 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20193 which intersects rectangle R. R is in window-relative coordinates. */
20195 static void
20196 expose_area (w, row, r, area)
20197 struct window *w;
20198 struct glyph_row *row;
20199 XRectangle *r;
20200 enum glyph_row_area area;
20202 struct glyph *first = row->glyphs[area];
20203 struct glyph *end = row->glyphs[area] + row->used[area];
20204 struct glyph *last;
20205 int first_x, start_x, x;
20207 if (area == TEXT_AREA && row->fill_line_p)
20208 /* If row extends face to end of line write the whole line. */
20209 draw_glyphs (w, 0, row, area,
20210 0, row->used[area],
20211 DRAW_NORMAL_TEXT, 0);
20212 else
20214 /* Set START_X to the window-relative start position for drawing glyphs of
20215 AREA. The first glyph of the text area can be partially visible.
20216 The first glyphs of other areas cannot. */
20217 start_x = window_box_left_offset (w, area);
20218 if (area == TEXT_AREA)
20219 start_x += row->x;
20220 x = start_x;
20222 /* Find the first glyph that must be redrawn. */
20223 while (first < end
20224 && x + first->pixel_width < r->x)
20226 x += first->pixel_width;
20227 ++first;
20230 /* Find the last one. */
20231 last = first;
20232 first_x = x;
20233 while (last < end
20234 && x < r->x + r->width)
20236 x += last->pixel_width;
20237 ++last;
20240 /* Repaint. */
20241 if (last > first)
20242 draw_glyphs (w, first_x - start_x, row, area,
20243 first - row->glyphs[area], last - row->glyphs[area],
20244 DRAW_NORMAL_TEXT, 0);
20249 /* Redraw the parts of the glyph row ROW on window W intersecting
20250 rectangle R. R is in window-relative coordinates. Value is
20251 non-zero if mouse-face was overwritten. */
20253 static int
20254 expose_line (w, row, r)
20255 struct window *w;
20256 struct glyph_row *row;
20257 XRectangle *r;
20259 xassert (row->enabled_p);
20261 if (row->mode_line_p || w->pseudo_window_p)
20262 draw_glyphs (w, 0, row, TEXT_AREA,
20263 0, row->used[TEXT_AREA],
20264 DRAW_NORMAL_TEXT, 0);
20265 else
20267 if (row->used[LEFT_MARGIN_AREA])
20268 expose_area (w, row, r, LEFT_MARGIN_AREA);
20269 if (row->used[TEXT_AREA])
20270 expose_area (w, row, r, TEXT_AREA);
20271 if (row->used[RIGHT_MARGIN_AREA])
20272 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20273 draw_row_fringe_bitmaps (w, row);
20276 return row->mouse_face_p;
20280 /* Redraw those parts of glyphs rows during expose event handling that
20281 overlap other rows. Redrawing of an exposed line writes over parts
20282 of lines overlapping that exposed line; this function fixes that.
20284 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20285 row in W's current matrix that is exposed and overlaps other rows.
20286 LAST_OVERLAPPING_ROW is the last such row. */
20288 static void
20289 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20290 struct window *w;
20291 struct glyph_row *first_overlapping_row;
20292 struct glyph_row *last_overlapping_row;
20294 struct glyph_row *row;
20296 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20297 if (row->overlapping_p)
20299 xassert (row->enabled_p && !row->mode_line_p);
20301 if (row->used[LEFT_MARGIN_AREA])
20302 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20304 if (row->used[TEXT_AREA])
20305 x_fix_overlapping_area (w, row, TEXT_AREA);
20307 if (row->used[RIGHT_MARGIN_AREA])
20308 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20313 /* Return non-zero if W's cursor intersects rectangle R. */
20315 static int
20316 phys_cursor_in_rect_p (w, r)
20317 struct window *w;
20318 XRectangle *r;
20320 XRectangle cr, result;
20321 struct glyph *cursor_glyph;
20323 cursor_glyph = get_phys_cursor_glyph (w);
20324 if (cursor_glyph)
20326 /* r is relative to W's box, but w->phys_cursor.x is relative
20327 to left edge of W's TEXT area. Adjust it. */
20328 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
20329 cr.y = w->phys_cursor.y;
20330 cr.width = cursor_glyph->pixel_width;
20331 cr.height = w->phys_cursor_height;
20332 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20333 I assume the effect is the same -- and this is portable. */
20334 return x_intersect_rectangles (&cr, r, &result);
20336 else
20337 return 0;
20341 /* EXPORT:
20342 Draw a vertical window border to the right of window W if W doesn't
20343 have vertical scroll bars. */
20345 void
20346 x_draw_vertical_border (w)
20347 struct window *w;
20349 /* We could do better, if we knew what type of scroll-bar the adjacent
20350 windows (on either side) have... But we don't :-(
20351 However, I think this works ok. ++KFS 2003-04-25 */
20353 /* Redraw borders between horizontally adjacent windows. Don't
20354 do it for frames with vertical scroll bars because either the
20355 right scroll bar of a window, or the left scroll bar of its
20356 neighbor will suffice as a border. */
20357 if (!WINDOW_RIGHTMOST_P (w)
20358 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
20360 int x0, x1, y0, y1;
20362 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20363 y1 -= 1;
20365 rif->draw_vertical_window_border (w, x1, y0, y1);
20367 else if (!WINDOW_LEFTMOST_P (w)
20368 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
20370 int x0, x1, y0, y1;
20372 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20373 y1 -= 1;
20375 rif->draw_vertical_window_border (w, x0, y0, y1);
20380 /* Redraw the part of window W intersection rectangle FR. Pixel
20381 coordinates in FR are frame-relative. Call this function with
20382 input blocked. Value is non-zero if the exposure overwrites
20383 mouse-face. */
20385 static int
20386 expose_window (w, fr)
20387 struct window *w;
20388 XRectangle *fr;
20390 struct frame *f = XFRAME (w->frame);
20391 XRectangle wr, r;
20392 int mouse_face_overwritten_p = 0;
20394 /* If window is not yet fully initialized, do nothing. This can
20395 happen when toolkit scroll bars are used and a window is split.
20396 Reconfiguring the scroll bar will generate an expose for a newly
20397 created window. */
20398 if (w->current_matrix == NULL)
20399 return 0;
20401 /* When we're currently updating the window, display and current
20402 matrix usually don't agree. Arrange for a thorough display
20403 later. */
20404 if (w == updated_window)
20406 SET_FRAME_GARBAGED (f);
20407 return 0;
20410 /* Frame-relative pixel rectangle of W. */
20411 wr.x = WINDOW_LEFT_EDGE_X (w);
20412 wr.y = WINDOW_TOP_EDGE_Y (w);
20413 wr.width = WINDOW_TOTAL_WIDTH (w);
20414 wr.height = WINDOW_TOTAL_HEIGHT (w);
20416 if (x_intersect_rectangles (fr, &wr, &r))
20418 int yb = window_text_bottom_y (w);
20419 struct glyph_row *row;
20420 int cursor_cleared_p;
20421 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20423 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20424 r.x, r.y, r.width, r.height));
20426 /* Convert to window coordinates. */
20427 r.x -= WINDOW_LEFT_EDGE_X (w);
20428 r.y -= WINDOW_TOP_EDGE_Y (w);
20430 /* Turn off the cursor. */
20431 if (!w->pseudo_window_p
20432 && phys_cursor_in_rect_p (w, &r))
20434 x_clear_cursor (w);
20435 cursor_cleared_p = 1;
20437 else
20438 cursor_cleared_p = 0;
20440 /* Update lines intersecting rectangle R. */
20441 first_overlapping_row = last_overlapping_row = NULL;
20442 for (row = w->current_matrix->rows;
20443 row->enabled_p;
20444 ++row)
20446 int y0 = row->y;
20447 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20449 if ((y0 >= r.y && y0 < r.y + r.height)
20450 || (y1 > r.y && y1 < r.y + r.height)
20451 || (r.y >= y0 && r.y < y1)
20452 || (r.y + r.height > y0 && r.y + r.height < y1))
20454 if (row->overlapping_p)
20456 if (first_overlapping_row == NULL)
20457 first_overlapping_row = row;
20458 last_overlapping_row = row;
20461 if (expose_line (w, row, &r))
20462 mouse_face_overwritten_p = 1;
20465 if (y1 >= yb)
20466 break;
20469 /* Display the mode line if there is one. */
20470 if (WINDOW_WANTS_MODELINE_P (w)
20471 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20472 row->enabled_p)
20473 && row->y < r.y + r.height)
20475 if (expose_line (w, row, &r))
20476 mouse_face_overwritten_p = 1;
20479 if (!w->pseudo_window_p)
20481 /* Fix the display of overlapping rows. */
20482 if (first_overlapping_row)
20483 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20485 /* Draw border between windows. */
20486 x_draw_vertical_border (w);
20488 /* Turn the cursor on again. */
20489 if (cursor_cleared_p)
20490 update_window_cursor (w, 1);
20494 #ifdef HAVE_CARBON
20495 /* Display scroll bar for this window. */
20496 if (!NILP (w->vertical_scroll_bar))
20498 /* ++KFS:
20499 If this doesn't work here (maybe some header files are missing),
20500 make a function in macterm.c and call it to do the job! */
20501 ControlHandle ch
20502 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20504 Draw1Control (ch);
20506 #endif
20508 return mouse_face_overwritten_p;
20513 /* Redraw (parts) of all windows in the window tree rooted at W that
20514 intersect R. R contains frame pixel coordinates. Value is
20515 non-zero if the exposure overwrites mouse-face. */
20517 static int
20518 expose_window_tree (w, r)
20519 struct window *w;
20520 XRectangle *r;
20522 struct frame *f = XFRAME (w->frame);
20523 int mouse_face_overwritten_p = 0;
20525 while (w && !FRAME_GARBAGED_P (f))
20527 if (!NILP (w->hchild))
20528 mouse_face_overwritten_p
20529 |= expose_window_tree (XWINDOW (w->hchild), r);
20530 else if (!NILP (w->vchild))
20531 mouse_face_overwritten_p
20532 |= expose_window_tree (XWINDOW (w->vchild), r);
20533 else
20534 mouse_face_overwritten_p |= expose_window (w, r);
20536 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20539 return mouse_face_overwritten_p;
20543 /* EXPORT:
20544 Redisplay an exposed area of frame F. X and Y are the upper-left
20545 corner of the exposed rectangle. W and H are width and height of
20546 the exposed area. All are pixel values. W or H zero means redraw
20547 the entire frame. */
20549 void
20550 expose_frame (f, x, y, w, h)
20551 struct frame *f;
20552 int x, y, w, h;
20554 XRectangle r;
20555 int mouse_face_overwritten_p = 0;
20557 TRACE ((stderr, "expose_frame "));
20559 /* No need to redraw if frame will be redrawn soon. */
20560 if (FRAME_GARBAGED_P (f))
20562 TRACE ((stderr, " garbaged\n"));
20563 return;
20566 #ifdef HAVE_CARBON
20567 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20568 or deactivated here, for unknown reasons, activated scroll bars
20569 are shown in deactivated frames in some instances. */
20570 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20571 activate_scroll_bars (f);
20572 else
20573 deactivate_scroll_bars (f);
20574 #endif
20576 /* If basic faces haven't been realized yet, there is no point in
20577 trying to redraw anything. This can happen when we get an expose
20578 event while Emacs is starting, e.g. by moving another window. */
20579 if (FRAME_FACE_CACHE (f) == NULL
20580 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20582 TRACE ((stderr, " no faces\n"));
20583 return;
20586 if (w == 0 || h == 0)
20588 r.x = r.y = 0;
20589 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
20590 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
20592 else
20594 r.x = x;
20595 r.y = y;
20596 r.width = w;
20597 r.height = h;
20600 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20601 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20603 if (WINDOWP (f->tool_bar_window))
20604 mouse_face_overwritten_p
20605 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20607 #ifdef HAVE_X_WINDOWS
20608 #ifndef MSDOS
20609 #ifndef USE_X_TOOLKIT
20610 if (WINDOWP (f->menu_bar_window))
20611 mouse_face_overwritten_p
20612 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20613 #endif /* not USE_X_TOOLKIT */
20614 #endif
20615 #endif
20617 /* Some window managers support a focus-follows-mouse style with
20618 delayed raising of frames. Imagine a partially obscured frame,
20619 and moving the mouse into partially obscured mouse-face on that
20620 frame. The visible part of the mouse-face will be highlighted,
20621 then the WM raises the obscured frame. With at least one WM, KDE
20622 2.1, Emacs is not getting any event for the raising of the frame
20623 (even tried with SubstructureRedirectMask), only Expose events.
20624 These expose events will draw text normally, i.e. not
20625 highlighted. Which means we must redo the highlight here.
20626 Subsume it under ``we love X''. --gerd 2001-08-15 */
20627 /* Included in Windows version because Windows most likely does not
20628 do the right thing if any third party tool offers
20629 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20630 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20632 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20633 if (f == dpyinfo->mouse_face_mouse_frame)
20635 int x = dpyinfo->mouse_face_mouse_x;
20636 int y = dpyinfo->mouse_face_mouse_y;
20637 clear_mouse_face (dpyinfo);
20638 note_mouse_highlight (f, x, y);
20644 /* EXPORT:
20645 Determine the intersection of two rectangles R1 and R2. Return
20646 the intersection in *RESULT. Value is non-zero if RESULT is not
20647 empty. */
20650 x_intersect_rectangles (r1, r2, result)
20651 XRectangle *r1, *r2, *result;
20653 XRectangle *left, *right;
20654 XRectangle *upper, *lower;
20655 int intersection_p = 0;
20657 /* Rearrange so that R1 is the left-most rectangle. */
20658 if (r1->x < r2->x)
20659 left = r1, right = r2;
20660 else
20661 left = r2, right = r1;
20663 /* X0 of the intersection is right.x0, if this is inside R1,
20664 otherwise there is no intersection. */
20665 if (right->x <= left->x + left->width)
20667 result->x = right->x;
20669 /* The right end of the intersection is the minimum of the
20670 the right ends of left and right. */
20671 result->width = (min (left->x + left->width, right->x + right->width)
20672 - result->x);
20674 /* Same game for Y. */
20675 if (r1->y < r2->y)
20676 upper = r1, lower = r2;
20677 else
20678 upper = r2, lower = r1;
20680 /* The upper end of the intersection is lower.y0, if this is inside
20681 of upper. Otherwise, there is no intersection. */
20682 if (lower->y <= upper->y + upper->height)
20684 result->y = lower->y;
20686 /* The lower end of the intersection is the minimum of the lower
20687 ends of upper and lower. */
20688 result->height = (min (lower->y + lower->height,
20689 upper->y + upper->height)
20690 - result->y);
20691 intersection_p = 1;
20695 return intersection_p;
20698 #endif /* HAVE_WINDOW_SYSTEM */
20701 /***********************************************************************
20702 Initialization
20703 ***********************************************************************/
20705 void
20706 syms_of_xdisp ()
20708 Vwith_echo_area_save_vector = Qnil;
20709 staticpro (&Vwith_echo_area_save_vector);
20711 Vmessage_stack = Qnil;
20712 staticpro (&Vmessage_stack);
20714 Qinhibit_redisplay = intern ("inhibit-redisplay");
20715 staticpro (&Qinhibit_redisplay);
20717 message_dolog_marker1 = Fmake_marker ();
20718 staticpro (&message_dolog_marker1);
20719 message_dolog_marker2 = Fmake_marker ();
20720 staticpro (&message_dolog_marker2);
20721 message_dolog_marker3 = Fmake_marker ();
20722 staticpro (&message_dolog_marker3);
20724 #if GLYPH_DEBUG
20725 defsubr (&Sdump_frame_glyph_matrix);
20726 defsubr (&Sdump_glyph_matrix);
20727 defsubr (&Sdump_glyph_row);
20728 defsubr (&Sdump_tool_bar_row);
20729 defsubr (&Strace_redisplay);
20730 defsubr (&Strace_to_stderr);
20731 #endif
20732 #ifdef HAVE_WINDOW_SYSTEM
20733 defsubr (&Stool_bar_lines_needed);
20734 #endif
20735 defsubr (&Sformat_mode_line);
20737 staticpro (&Qmenu_bar_update_hook);
20738 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20740 staticpro (&Qoverriding_terminal_local_map);
20741 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20743 staticpro (&Qoverriding_local_map);
20744 Qoverriding_local_map = intern ("overriding-local-map");
20746 staticpro (&Qwindow_scroll_functions);
20747 Qwindow_scroll_functions = intern ("window-scroll-functions");
20749 staticpro (&Qredisplay_end_trigger_functions);
20750 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20752 staticpro (&Qinhibit_point_motion_hooks);
20753 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20755 QCdata = intern (":data");
20756 staticpro (&QCdata);
20757 Qdisplay = intern ("display");
20758 staticpro (&Qdisplay);
20759 Qspace_width = intern ("space-width");
20760 staticpro (&Qspace_width);
20761 Qraise = intern ("raise");
20762 staticpro (&Qraise);
20763 Qspace = intern ("space");
20764 staticpro (&Qspace);
20765 Qmargin = intern ("margin");
20766 staticpro (&Qmargin);
20767 Qleft_margin = intern ("left-margin");
20768 staticpro (&Qleft_margin);
20769 Qright_margin = intern ("right-margin");
20770 staticpro (&Qright_margin);
20771 Qalign_to = intern ("align-to");
20772 staticpro (&Qalign_to);
20773 QCalign_to = intern (":align-to");
20774 staticpro (&QCalign_to);
20775 Qrelative_width = intern ("relative-width");
20776 staticpro (&Qrelative_width);
20777 QCrelative_width = intern (":relative-width");
20778 staticpro (&QCrelative_width);
20779 QCrelative_height = intern (":relative-height");
20780 staticpro (&QCrelative_height);
20781 QCeval = intern (":eval");
20782 staticpro (&QCeval);
20783 QCpropertize = intern (":propertize");
20784 staticpro (&QCpropertize);
20785 QCfile = intern (":file");
20786 staticpro (&QCfile);
20787 Qfontified = intern ("fontified");
20788 staticpro (&Qfontified);
20789 Qfontification_functions = intern ("fontification-functions");
20790 staticpro (&Qfontification_functions);
20791 Qtrailing_whitespace = intern ("trailing-whitespace");
20792 staticpro (&Qtrailing_whitespace);
20793 Qimage = intern ("image");
20794 staticpro (&Qimage);
20795 Qmessage_truncate_lines = intern ("message-truncate-lines");
20796 staticpro (&Qmessage_truncate_lines);
20797 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20798 staticpro (&Qcursor_in_non_selected_windows);
20799 Qgrow_only = intern ("grow-only");
20800 staticpro (&Qgrow_only);
20801 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20802 staticpro (&Qinhibit_menubar_update);
20803 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20804 staticpro (&Qinhibit_eval_during_redisplay);
20805 Qposition = intern ("position");
20806 staticpro (&Qposition);
20807 Qbuffer_position = intern ("buffer-position");
20808 staticpro (&Qbuffer_position);
20809 Qobject = intern ("object");
20810 staticpro (&Qobject);
20811 Qbar = intern ("bar");
20812 staticpro (&Qbar);
20813 Qhbar = intern ("hbar");
20814 staticpro (&Qhbar);
20815 Qbox = intern ("box");
20816 staticpro (&Qbox);
20817 Qhollow = intern ("hollow");
20818 staticpro (&Qhollow);
20819 Qrisky_local_variable = intern ("risky-local-variable");
20820 staticpro (&Qrisky_local_variable);
20821 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20822 staticpro (&Qinhibit_free_realized_faces);
20824 list_of_error = Fcons (intern ("error"), Qnil);
20825 staticpro (&list_of_error);
20827 last_arrow_position = Qnil;
20828 last_arrow_string = Qnil;
20829 staticpro (&last_arrow_position);
20830 staticpro (&last_arrow_string);
20832 echo_buffer[0] = echo_buffer[1] = Qnil;
20833 staticpro (&echo_buffer[0]);
20834 staticpro (&echo_buffer[1]);
20836 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20837 staticpro (&echo_area_buffer[0]);
20838 staticpro (&echo_area_buffer[1]);
20840 Vmessages_buffer_name = build_string ("*Messages*");
20841 staticpro (&Vmessages_buffer_name);
20843 mode_line_proptrans_alist = Qnil;
20844 staticpro (&mode_line_proptrans_alist);
20846 mode_line_string_list = Qnil;
20847 staticpro (&mode_line_string_list);
20849 help_echo_string = Qnil;
20850 staticpro (&help_echo_string);
20851 help_echo_object = Qnil;
20852 staticpro (&help_echo_object);
20853 help_echo_window = Qnil;
20854 staticpro (&help_echo_window);
20855 previous_help_echo_string = Qnil;
20856 staticpro (&previous_help_echo_string);
20857 help_echo_pos = -1;
20859 #ifdef HAVE_WINDOW_SYSTEM
20860 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20861 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20862 For example, if a block cursor is over a tab, it will be drawn as
20863 wide as that tab on the display. */);
20864 x_stretch_cursor_p = 0;
20865 #endif
20867 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20868 doc: /* Non-nil means highlight trailing whitespace.
20869 The face used for trailing whitespace is `trailing-whitespace'. */);
20870 Vshow_trailing_whitespace = Qnil;
20872 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20873 doc: /* Non-nil means don't actually do any redisplay.
20874 This is used for internal purposes. */);
20875 Vinhibit_redisplay = Qnil;
20877 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20878 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20879 Vglobal_mode_string = Qnil;
20881 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20882 doc: /* Marker for where to display an arrow on top of the buffer text.
20883 This must be the beginning of a line in order to work.
20884 See also `overlay-arrow-string'. */);
20885 Voverlay_arrow_position = Qnil;
20887 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20888 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20889 Voverlay_arrow_string = Qnil;
20891 DEFVAR_INT ("scroll-step", &scroll_step,
20892 doc: /* *The number of lines to try scrolling a window by when point moves out.
20893 If that fails to bring point back on frame, point is centered instead.
20894 If this is zero, point is always centered after it moves off frame.
20895 If you want scrolling to always be a line at a time, you should set
20896 `scroll-conservatively' to a large value rather than set this to 1. */);
20898 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20899 doc: /* *Scroll up to this many lines, to bring point back on screen.
20900 A value of zero means to scroll the text to center point vertically
20901 in the window. */);
20902 scroll_conservatively = 0;
20904 DEFVAR_INT ("scroll-margin", &scroll_margin,
20905 doc: /* *Number of lines of margin at the top and bottom of a window.
20906 Recenter the window whenever point gets within this many lines
20907 of the top or bottom of the window. */);
20908 scroll_margin = 0;
20910 #if GLYPH_DEBUG
20911 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20912 #endif
20914 DEFVAR_BOOL ("truncate-partial-width-windows",
20915 &truncate_partial_width_windows,
20916 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20917 truncate_partial_width_windows = 1;
20919 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20920 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20921 Any other value means to use the appropriate face, `mode-line',
20922 `header-line', or `menu' respectively. */);
20923 mode_line_inverse_video = 1;
20925 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20926 doc: /* *Maximum buffer size for which line number should be displayed.
20927 If the buffer is bigger than this, the line number does not appear
20928 in the mode line. A value of nil means no limit. */);
20929 Vline_number_display_limit = Qnil;
20931 DEFVAR_INT ("line-number-display-limit-width",
20932 &line_number_display_limit_width,
20933 doc: /* *Maximum line width (in characters) for line number display.
20934 If the average length of the lines near point is bigger than this, then the
20935 line number may be omitted from the mode line. */);
20936 line_number_display_limit_width = 200;
20938 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20939 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20940 highlight_nonselected_windows = 0;
20942 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20943 doc: /* Non-nil if more than one frame is visible on this display.
20944 Minibuffer-only frames don't count, but iconified frames do.
20945 This variable is not guaranteed to be accurate except while processing
20946 `frame-title-format' and `icon-title-format'. */);
20948 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20949 doc: /* Template for displaying the title bar of visible frames.
20950 \(Assuming the window manager supports this feature.)
20951 This variable has the same structure as `mode-line-format' (which see),
20952 and is used only on frames for which no explicit name has been set
20953 \(see `modify-frame-parameters'). */);
20954 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20955 doc: /* Template for displaying the title bar of an iconified frame.
20956 \(Assuming the window manager supports this feature.)
20957 This variable has the same structure as `mode-line-format' (which see),
20958 and is used only on frames for which no explicit name has been set
20959 \(see `modify-frame-parameters'). */);
20960 Vicon_title_format
20961 = Vframe_title_format
20962 = Fcons (intern ("multiple-frames"),
20963 Fcons (build_string ("%b"),
20964 Fcons (Fcons (empty_string,
20965 Fcons (intern ("invocation-name"),
20966 Fcons (build_string ("@"),
20967 Fcons (intern ("system-name"),
20968 Qnil)))),
20969 Qnil)));
20971 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20972 doc: /* Maximum number of lines to keep in the message log buffer.
20973 If nil, disable message logging. If t, log messages but don't truncate
20974 the buffer when it becomes large. */);
20975 Vmessage_log_max = make_number (50);
20977 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20978 doc: /* Functions called before redisplay, if window sizes have changed.
20979 The value should be a list of functions that take one argument.
20980 Just before redisplay, for each frame, if any of its windows have changed
20981 size since the last redisplay, or have been split or deleted,
20982 all the functions in the list are called, with the frame as argument. */);
20983 Vwindow_size_change_functions = Qnil;
20985 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20986 doc: /* List of Functions to call before redisplaying a window with scrolling.
20987 Each function is called with two arguments, the window
20988 and its new display-start position. Note that the value of `window-end'
20989 is not valid when these functions are called. */);
20990 Vwindow_scroll_functions = Qnil;
20992 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20993 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20994 mouse_autoselect_window = 0;
20996 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20997 doc: /* *Non-nil means automatically resize tool-bars.
20998 This increases a tool-bar's height if not all tool-bar items are visible.
20999 It decreases a tool-bar's height when it would display blank lines
21000 otherwise. */);
21001 auto_resize_tool_bars_p = 1;
21003 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
21004 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
21005 auto_raise_tool_bar_buttons_p = 1;
21007 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
21008 doc: /* *Margin around tool-bar buttons in pixels.
21009 If an integer, use that for both horizontal and vertical margins.
21010 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
21011 HORZ specifying the horizontal margin, and VERT specifying the
21012 vertical margin. */);
21013 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
21015 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
21016 doc: /* *Relief thickness of tool-bar buttons. */);
21017 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
21019 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
21020 doc: /* List of functions to call to fontify regions of text.
21021 Each function is called with one argument POS. Functions must
21022 fontify a region starting at POS in the current buffer, and give
21023 fontified regions the property `fontified'. */);
21024 Vfontification_functions = Qnil;
21025 Fmake_variable_buffer_local (Qfontification_functions);
21027 DEFVAR_BOOL ("unibyte-display-via-language-environment",
21028 &unibyte_display_via_language_environment,
21029 doc: /* *Non-nil means display unibyte text according to language environment.
21030 Specifically this means that unibyte non-ASCII characters
21031 are displayed by converting them to the equivalent multibyte characters
21032 according to the current language environment. As a result, they are
21033 displayed according to the current fontset. */);
21034 unibyte_display_via_language_environment = 0;
21036 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
21037 doc: /* *Maximum height for resizing mini-windows.
21038 If a float, it specifies a fraction of the mini-window frame's height.
21039 If an integer, it specifies a number of lines. */);
21040 Vmax_mini_window_height = make_float (0.25);
21042 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
21043 doc: /* *How to resize mini-windows.
21044 A value of nil means don't automatically resize mini-windows.
21045 A value of t means resize them to fit the text displayed in them.
21046 A value of `grow-only', the default, means let mini-windows grow
21047 only, until their display becomes empty, at which point the windows
21048 go back to their normal size. */);
21049 Vresize_mini_windows = Qgrow_only;
21051 DEFVAR_LISP ("cursor-in-non-selected-windows",
21052 &Vcursor_in_non_selected_windows,
21053 doc: /* *Cursor type to display in non-selected windows.
21054 t means to use hollow box cursor. See `cursor-type' for other values. */);
21055 Vcursor_in_non_selected_windows = Qt;
21057 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
21058 doc: /* Alist specifying how to blink the cursor off.
21059 Each element has the form (ON-STATE . OFF-STATE). Whenever the
21060 `cursor-type' frame-parameter or variable equals ON-STATE,
21061 comparing using `equal', Emacs uses OFF-STATE to specify
21062 how to blink it off. */);
21063 Vblink_cursor_alist = Qnil;
21065 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
21066 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
21067 automatic_hscrolling_p = 1;
21069 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
21070 doc: /* *How many columns away from the window edge point is allowed to get
21071 before automatic hscrolling will horizontally scroll the window. */);
21072 hscroll_margin = 5;
21074 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
21075 doc: /* *How many columns to scroll the window when point gets too close to the edge.
21076 When point is less than `automatic-hscroll-margin' columns from the window
21077 edge, automatic hscrolling will scroll the window by the amount of columns
21078 determined by this variable. If its value is a positive integer, scroll that
21079 many columns. If it's a positive floating-point number, it specifies the
21080 fraction of the window's width to scroll. If it's nil or zero, point will be
21081 centered horizontally after the scroll. Any other value, including negative
21082 numbers, are treated as if the value were zero.
21084 Automatic hscrolling always moves point outside the scroll margin, so if
21085 point was more than scroll step columns inside the margin, the window will
21086 scroll more than the value given by the scroll step.
21088 Note that the lower bound for automatic hscrolling specified by `scroll-left'
21089 and `scroll-right' overrides this variable's effect. */);
21090 Vhscroll_step = make_number (0);
21092 DEFVAR_LISP ("image-types", &Vimage_types,
21093 doc: /* List of supported image types.
21094 Each element of the list is a symbol for a supported image type. */);
21095 Vimage_types = Qnil;
21097 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
21098 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
21099 Bind this around calls to `message' to let it take effect. */);
21100 message_truncate_lines = 0;
21102 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
21103 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
21104 Can be used to update submenus whose contents should vary. */);
21105 Vmenu_bar_update_hook = Qnil;
21107 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
21108 doc: /* Non-nil means don't update menu bars. Internal use only. */);
21109 inhibit_menubar_update = 0;
21111 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
21112 doc: /* Non-nil means don't eval Lisp during redisplay. */);
21113 inhibit_eval_during_redisplay = 0;
21115 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
21116 doc: /* Non-nil means don't free realized faces. Internal use only. */);
21117 inhibit_free_realized_faces = 0;
21119 #if GLYPH_DEBUG
21120 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
21121 doc: /* Inhibit try_window_id display optimization. */);
21122 inhibit_try_window_id = 0;
21124 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
21125 doc: /* Inhibit try_window_reusing display optimization. */);
21126 inhibit_try_window_reusing = 0;
21128 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
21129 doc: /* Inhibit try_cursor_movement display optimization. */);
21130 inhibit_try_cursor_movement = 0;
21131 #endif /* GLYPH_DEBUG */
21135 /* Initialize this module when Emacs starts. */
21137 void
21138 init_xdisp ()
21140 Lisp_Object root_window;
21141 struct window *mini_w;
21143 current_header_line_height = current_mode_line_height = -1;
21145 CHARPOS (this_line_start_pos) = 0;
21147 mini_w = XWINDOW (minibuf_window);
21148 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21150 if (!noninteractive)
21152 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21153 int i;
21155 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
21156 set_window_height (root_window,
21157 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
21159 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
21160 set_window_height (minibuf_window, 1, 0);
21162 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
21163 mini_w->total_cols = make_number (FRAME_COLS (f));
21165 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21166 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21167 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21169 /* The default ellipsis glyphs `...'. */
21170 for (i = 0; i < 3; ++i)
21171 default_invis_vector[i] = make_number ('.');
21175 /* Allocate the buffer for frame titles.
21176 Also used for `format-mode-line'. */
21177 int size = 100;
21178 frame_title_buf = (char *) xmalloc (size);
21179 frame_title_buf_end = frame_title_buf + size;
21180 frame_title_ptr = NULL;
21183 help_echo_showing_p = 0;
21187 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
21188 (do not change this comment) */