(f90-font-lock-keywords-2): Use override for
[emacs.git] / src / xdisp.c
blob940db9ced315d55f9cd17eb3659d4d52b35bdb74
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985,86,87,88,93,94,95,97,98,99, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
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 CANON_Y_UNIT, which means that
428 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 struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
776 struct text_pos));
777 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
778 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
779 static void store_frame_title_char P_ ((char));
780 static int store_frame_title P_ ((const unsigned char *, int, int));
781 static void x_consider_frame_title P_ ((Lisp_Object));
782 static void handle_stop P_ ((struct it *));
783 static int tool_bar_lines_needed P_ ((struct frame *));
784 static int single_display_prop_intangible_p P_ ((Lisp_Object));
785 static void ensure_echo_area_buffers P_ ((void));
786 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
787 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
788 static int with_echo_area_buffer P_ ((struct window *, int,
789 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
790 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
791 static void clear_garbaged_frames P_ ((void));
792 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
793 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
794 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
795 static int display_echo_area P_ ((struct window *));
796 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
797 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
798 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
799 static int string_char_and_length P_ ((const unsigned char *, int, int *));
800 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
801 struct text_pos));
802 static int compute_window_start_on_continuation_line P_ ((struct window *));
803 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
804 static void insert_left_trunc_glyphs P_ ((struct it *));
805 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
806 static void extend_face_to_end_of_line P_ ((struct it *));
807 static int append_space P_ ((struct it *, int));
808 static int make_cursor_line_fully_visible P_ ((struct window *));
809 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
810 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
811 static int trailing_whitespace_p P_ ((int));
812 static int message_log_check_duplicate P_ ((int, int, int, int));
813 static void push_it P_ ((struct it *));
814 static void pop_it P_ ((struct it *));
815 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
816 static void redisplay_internal P_ ((int));
817 static int echo_area_display P_ ((int));
818 static void redisplay_windows P_ ((Lisp_Object));
819 static void redisplay_window P_ ((Lisp_Object, int));
820 static Lisp_Object redisplay_window_error ();
821 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
822 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
823 static void update_menu_bar P_ ((struct frame *, int));
824 static int try_window_reusing_current_matrix P_ ((struct window *));
825 static int try_window_id P_ ((struct window *));
826 static int display_line P_ ((struct it *));
827 static int display_mode_lines P_ ((struct window *));
828 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
829 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
830 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
831 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
832 static void display_menu_bar P_ ((struct window *));
833 static int display_count_lines P_ ((int, int, int, int, int *));
834 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
835 int, int, struct it *, int, int, int, int));
836 static void compute_line_metrics P_ ((struct it *));
837 static void run_redisplay_end_trigger_hook P_ ((struct it *));
838 static int get_overlay_strings P_ ((struct it *, int));
839 static void next_overlay_string P_ ((struct it *));
840 static void reseat P_ ((struct it *, struct text_pos, int));
841 static void reseat_1 P_ ((struct it *, struct text_pos, int));
842 static void back_to_previous_visible_line_start P_ ((struct it *));
843 static void reseat_at_previous_visible_line_start P_ ((struct it *));
844 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
845 static int next_element_from_display_vector P_ ((struct it *));
846 static int next_element_from_string P_ ((struct it *));
847 static int next_element_from_c_string P_ ((struct it *));
848 static int next_element_from_buffer P_ ((struct it *));
849 static int next_element_from_composition P_ ((struct it *));
850 static int next_element_from_image P_ ((struct it *));
851 static int next_element_from_stretch P_ ((struct it *));
852 static void load_overlay_strings P_ ((struct it *, int));
853 static int init_from_display_pos P_ ((struct it *, struct window *,
854 struct display_pos *));
855 static void reseat_to_string P_ ((struct it *, unsigned char *,
856 Lisp_Object, int, int, int, int));
857 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
858 int, int, int));
859 void move_it_vertically_backward P_ ((struct it *, int));
860 static void init_to_row_start P_ ((struct it *, struct window *,
861 struct glyph_row *));
862 static int init_to_row_end P_ ((struct it *, struct window *,
863 struct glyph_row *));
864 static void back_to_previous_line_start P_ ((struct it *));
865 static int forward_to_next_line_start P_ ((struct it *, int *));
866 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
867 Lisp_Object, int));
868 static struct text_pos string_pos P_ ((int, Lisp_Object));
869 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
870 static int number_of_chars P_ ((unsigned char *, int));
871 static void compute_stop_pos P_ ((struct it *));
872 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
873 Lisp_Object));
874 static int face_before_or_after_it_pos P_ ((struct it *, int));
875 static int next_overlay_change P_ ((int));
876 static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
877 Lisp_Object, struct text_pos *,
878 int));
879 static int underlying_face_id P_ ((struct it *));
880 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
881 struct window *));
883 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
884 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
886 #ifdef HAVE_WINDOW_SYSTEM
888 static void update_tool_bar P_ ((struct frame *, int));
889 static void build_desired_tool_bar_string P_ ((struct frame *f));
890 static int redisplay_tool_bar P_ ((struct frame *));
891 static void display_tool_bar_line P_ ((struct it *));
892 static void notice_overwritten_cursor P_ ((struct window *,
893 enum glyph_row_area,
894 int, int, int, int));
898 #endif /* HAVE_WINDOW_SYSTEM */
901 /***********************************************************************
902 Window display dimensions
903 ***********************************************************************/
905 /* Return the bottom boundary y-position for text lines in window W.
906 This is the first y position at which a line cannot start.
907 It is relative to the top of the window.
909 This is the height of W minus the height of a mode line, if any. */
911 INLINE int
912 window_text_bottom_y (w)
913 struct window *w;
915 struct frame *f = XFRAME (w->frame);
916 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
918 if (WINDOW_WANTS_MODELINE_P (w))
919 height -= CURRENT_MODE_LINE_HEIGHT (w);
920 return height;
924 /* Return the pixel width of display area AREA of window W. AREA < 0
925 means return the total width of W, not including fringes to
926 the left and right of the window. */
928 INLINE int
929 window_box_width (w, area)
930 struct window *w;
931 int area;
933 struct frame *f = XFRAME (w->frame);
934 int width = XFASTINT (w->width);
936 if (!w->pseudo_window_p)
938 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FRINGE_COLS (f);
940 if (area == TEXT_AREA)
942 if (INTEGERP (w->left_margin_width))
943 width -= XFASTINT (w->left_margin_width);
944 if (INTEGERP (w->right_margin_width))
945 width -= XFASTINT (w->right_margin_width);
947 else if (area == LEFT_MARGIN_AREA)
948 width = (INTEGERP (w->left_margin_width)
949 ? XFASTINT (w->left_margin_width) : 0);
950 else if (area == RIGHT_MARGIN_AREA)
951 width = (INTEGERP (w->right_margin_width)
952 ? XFASTINT (w->right_margin_width) : 0);
955 return width * CANON_X_UNIT (f);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
962 INLINE int
963 window_box_height (w)
964 struct window *w;
966 struct frame *f = XFRAME (w->frame);
967 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
969 xassert (height >= 0);
971 /* Note: the code below that determines the mode-line/header-line
972 height is essentially the same as that contained in the macro
973 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
974 the appropriate glyph row has its `mode_line_p' flag set,
975 and if it doesn't, uses estimate_mode_line_height instead. */
977 if (WINDOW_WANTS_MODELINE_P (w))
979 struct glyph_row *ml_row
980 = (w->current_matrix && w->current_matrix->rows
981 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
982 : 0);
983 if (ml_row && ml_row->mode_line_p)
984 height -= ml_row->height;
985 else
986 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
989 if (WINDOW_WANTS_HEADER_LINE_P (w))
991 struct glyph_row *hl_row
992 = (w->current_matrix && w->current_matrix->rows
993 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
994 : 0);
995 if (hl_row && hl_row->mode_line_p)
996 height -= hl_row->height;
997 else
998 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1001 /* With a very small font and a mode-line that's taller than
1002 default, we might end up with a negative height. */
1003 return max (0, height);
1007 /* Return the frame-relative coordinate of the left edge of display
1008 area AREA of window W. AREA < 0 means return the left edge of the
1009 whole window, to the right of the left fringe of W. */
1011 INLINE int
1012 window_box_left (w, area)
1013 struct window *w;
1014 int area;
1016 struct frame *f = XFRAME (w->frame);
1017 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
1019 if (!w->pseudo_window_p)
1021 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
1022 + FRAME_LEFT_FRINGE_WIDTH (f));
1024 if (area == TEXT_AREA)
1025 x += window_box_width (w, LEFT_MARGIN_AREA);
1026 else if (area == RIGHT_MARGIN_AREA)
1027 x += (window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA));
1031 return x;
1035 /* Return the frame-relative coordinate of the right edge of display
1036 area AREA of window W. AREA < 0 means return the left edge of the
1037 whole window, to the left of the right fringe of W. */
1039 INLINE int
1040 window_box_right (w, area)
1041 struct window *w;
1042 int area;
1044 return window_box_left (w, area) + window_box_width (w, area);
1048 /* Get the bounding box of the display area AREA of window W, without
1049 mode lines, in frame-relative coordinates. AREA < 0 means the
1050 whole window, not including the left and right fringes of
1051 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1052 coordinates of the upper-left corner of the box. Return in
1053 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1055 INLINE void
1056 window_box (w, area, box_x, box_y, box_width, box_height)
1057 struct window *w;
1058 int area;
1059 int *box_x, *box_y, *box_width, *box_height;
1061 struct frame *f = XFRAME (w->frame);
1063 *box_width = window_box_width (w, area);
1064 *box_height = window_box_height (w);
1065 *box_x = window_box_left (w, area);
1066 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
1067 + XFASTINT (w->top) * CANON_Y_UNIT (f));
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1069 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1073 /* Get the bounding box of the display area AREA of window W, without
1074 mode lines. AREA < 0 means the whole window, not including the
1075 left and right fringe of the window. Return in *TOP_LEFT_X
1076 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1077 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1078 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1079 box. */
1081 INLINE void
1082 window_box_edges (w, area, top_left_x, top_left_y,
1083 bottom_right_x, bottom_right_y)
1084 struct window *w;
1085 int area;
1086 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1088 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1089 bottom_right_y);
1090 *bottom_right_x += *top_left_x;
1091 *bottom_right_y += *top_left_y;
1096 /***********************************************************************
1097 Utilities
1098 ***********************************************************************/
1100 /* Return the bottom y-position of the line the iterator IT is in.
1101 This can modify IT's settings. */
1104 line_bottom_y (it)
1105 struct it *it;
1107 int line_height = it->max_ascent + it->max_descent;
1108 int line_top_y = it->current_y;
1110 if (line_height == 0)
1112 if (last_height)
1113 line_height = last_height;
1114 else if (IT_CHARPOS (*it) < ZV)
1116 move_it_by_lines (it, 1, 1);
1117 line_height = (it->max_ascent || it->max_descent
1118 ? it->max_ascent + it->max_descent
1119 : last_height);
1121 else
1123 struct glyph_row *row = it->glyph_row;
1125 /* Use the default character height. */
1126 it->glyph_row = NULL;
1127 it->what = IT_CHARACTER;
1128 it->c = ' ';
1129 it->len = 1;
1130 PRODUCE_GLYPHS (it);
1131 line_height = it->ascent + it->descent;
1132 it->glyph_row = row;
1136 return line_top_y + line_height;
1140 /* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1141 1 if POS is visible and the line containing POS is fully visible.
1142 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1143 and header-lines heights. */
1146 pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
1147 struct window *w;
1148 int charpos, *fully, exact_mode_line_heights_p;
1150 struct it it;
1151 struct text_pos top;
1152 int visible_p;
1153 struct buffer *old_buffer = NULL;
1155 if (XBUFFER (w->buffer) != current_buffer)
1157 old_buffer = current_buffer;
1158 set_buffer_internal_1 (XBUFFER (w->buffer));
1161 *fully = visible_p = 0;
1162 SET_TEXT_POS_FROM_MARKER (top, w->start);
1164 /* Compute exact mode line heights, if requested. */
1165 if (exact_mode_line_heights_p)
1167 if (WINDOW_WANTS_MODELINE_P (w))
1168 current_mode_line_height
1169 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1170 current_buffer->mode_line_format);
1172 if (WINDOW_WANTS_HEADER_LINE_P (w))
1173 current_header_line_height
1174 = display_mode_line (w, HEADER_LINE_FACE_ID,
1175 current_buffer->header_line_format);
1178 start_display (&it, w, top);
1179 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1180 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1182 /* Note that we may overshoot because of invisible text. */
1183 if (IT_CHARPOS (it) >= charpos)
1185 int top_y = it.current_y;
1186 int bottom_y = line_bottom_y (&it);
1187 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
1189 if (top_y < window_top_y)
1190 visible_p = bottom_y > window_top_y;
1191 else if (top_y < it.last_visible_y)
1193 visible_p = 1;
1194 *fully = bottom_y <= it.last_visible_y;
1197 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1199 move_it_by_lines (&it, 1, 0);
1200 if (charpos < IT_CHARPOS (it))
1202 visible_p = 1;
1203 *fully = 0;
1207 if (old_buffer)
1208 set_buffer_internal_1 (old_buffer);
1210 current_header_line_height = current_mode_line_height = -1;
1211 return visible_p;
1215 /* Return the next character from STR which is MAXLEN bytes long.
1216 Return in *LEN the length of the character. This is like
1217 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1218 we find one, we return a `?', but with the length of the invalid
1219 character. */
1221 static INLINE int
1222 string_char_and_length (str, maxlen, len)
1223 const unsigned char *str;
1224 int maxlen, *len;
1226 int c;
1228 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1229 if (!CHAR_VALID_P (c, 1))
1230 /* We may not change the length here because other places in Emacs
1231 don't use this function, i.e. they silently accept invalid
1232 characters. */
1233 c = '?';
1235 return c;
1240 /* Given a position POS containing a valid character and byte position
1241 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1243 static struct text_pos
1244 string_pos_nchars_ahead (pos, string, nchars)
1245 struct text_pos pos;
1246 Lisp_Object string;
1247 int nchars;
1249 xassert (STRINGP (string) && nchars >= 0);
1251 if (STRING_MULTIBYTE (string))
1253 int rest = SBYTES (string) - BYTEPOS (pos);
1254 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1255 int len;
1257 while (nchars--)
1259 string_char_and_length (p, rest, &len);
1260 p += len, rest -= len;
1261 xassert (rest >= 0);
1262 CHARPOS (pos) += 1;
1263 BYTEPOS (pos) += len;
1266 else
1267 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1269 return pos;
1273 /* Value is the text position, i.e. character and byte position,
1274 for character position CHARPOS in STRING. */
1276 static INLINE struct text_pos
1277 string_pos (charpos, string)
1278 int charpos;
1279 Lisp_Object string;
1281 struct text_pos pos;
1282 xassert (STRINGP (string));
1283 xassert (charpos >= 0);
1284 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1285 return pos;
1289 /* Value is a text position, i.e. character and byte position, for
1290 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1291 means recognize multibyte characters. */
1293 static struct text_pos
1294 c_string_pos (charpos, s, multibyte_p)
1295 int charpos;
1296 unsigned char *s;
1297 int multibyte_p;
1299 struct text_pos pos;
1301 xassert (s != NULL);
1302 xassert (charpos >= 0);
1304 if (multibyte_p)
1306 int rest = strlen (s), len;
1308 SET_TEXT_POS (pos, 0, 0);
1309 while (charpos--)
1311 string_char_and_length (s, rest, &len);
1312 s += len, rest -= len;
1313 xassert (rest >= 0);
1314 CHARPOS (pos) += 1;
1315 BYTEPOS (pos) += len;
1318 else
1319 SET_TEXT_POS (pos, charpos, charpos);
1321 return pos;
1325 /* Value is the number of characters in C string S. MULTIBYTE_P
1326 non-zero means recognize multibyte characters. */
1328 static int
1329 number_of_chars (s, multibyte_p)
1330 unsigned char *s;
1331 int multibyte_p;
1333 int nchars;
1335 if (multibyte_p)
1337 int rest = strlen (s), len;
1338 unsigned char *p = (unsigned char *) s;
1340 for (nchars = 0; rest > 0; ++nchars)
1342 string_char_and_length (p, rest, &len);
1343 rest -= len, p += len;
1346 else
1347 nchars = strlen (s);
1349 return nchars;
1353 /* Compute byte position NEWPOS->bytepos corresponding to
1354 NEWPOS->charpos. POS is a known position in string STRING.
1355 NEWPOS->charpos must be >= POS.charpos. */
1357 static void
1358 compute_string_pos (newpos, pos, string)
1359 struct text_pos *newpos, pos;
1360 Lisp_Object string;
1362 xassert (STRINGP (string));
1363 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1365 if (STRING_MULTIBYTE (string))
1366 *newpos = string_pos_nchars_ahead (pos, string,
1367 CHARPOS (*newpos) - CHARPOS (pos));
1368 else
1369 BYTEPOS (*newpos) = CHARPOS (*newpos);
1372 /* EXPORT:
1373 Return an estimation of the pixel height of mode or top lines on
1374 frame F. FACE_ID specifies what line's height to estimate. */
1377 estimate_mode_line_height (f, face_id)
1378 struct frame *f;
1379 enum face_id face_id;
1381 #ifdef HAVE_WINDOW_SYSTEM
1382 if (FRAME_WINDOW_P (f))
1384 int height = FONT_HEIGHT (FRAME_FONT (f));
1386 /* This function is called so early when Emacs starts that the face
1387 cache and mode line face are not yet initialized. */
1388 if (FRAME_FACE_CACHE (f))
1390 struct face *face = FACE_FROM_ID (f, face_id);
1391 if (face)
1393 if (face->font)
1394 height = FONT_HEIGHT (face->font);
1395 if (face->box_line_width > 0)
1396 height += 2 * face->box_line_width;
1400 return height;
1402 #endif
1404 return 1;
1407 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1408 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1409 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1410 not force the value into range. */
1412 void
1413 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1414 FRAME_PTR f;
1415 register int pix_x, pix_y;
1416 int *x, *y;
1417 NativeRectangle *bounds;
1418 int noclip;
1421 #ifdef HAVE_WINDOW_SYSTEM
1422 if (FRAME_WINDOW_P (f))
1424 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
1425 even for negative values. */
1426 if (pix_x < 0)
1427 pix_x -= FONT_WIDTH (FRAME_FONT (f)) - 1;
1428 if (pix_y < 0)
1429 pix_y -= FRAME_X_OUTPUT(f)->line_height - 1;
1431 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
1432 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
1434 if (bounds)
1435 STORE_NATIVE_RECT (*bounds,
1436 CHAR_TO_PIXEL_COL (f, pix_x),
1437 CHAR_TO_PIXEL_ROW (f, pix_y),
1438 FONT_WIDTH (FRAME_FONT (f)) - 1,
1439 FRAME_X_OUTPUT (f)->line_height - 1);
1441 if (!noclip)
1443 if (pix_x < 0)
1444 pix_x = 0;
1445 else if (pix_x > FRAME_WINDOW_WIDTH (f))
1446 pix_x = FRAME_WINDOW_WIDTH (f);
1448 if (pix_y < 0)
1449 pix_y = 0;
1450 else if (pix_y > f->height)
1451 pix_y = f->height;
1454 #endif
1456 *x = pix_x;
1457 *y = pix_y;
1461 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1462 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1463 can't tell the positions because W's display is not up to date,
1464 return 0. */
1467 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1468 struct window *w;
1469 int hpos, vpos;
1470 int *frame_x, *frame_y;
1472 #ifdef HAVE_WINDOW_SYSTEM
1473 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1475 int success_p;
1477 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1478 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1480 if (display_completed)
1482 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1483 struct glyph *glyph = row->glyphs[TEXT_AREA];
1484 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1486 hpos = row->x;
1487 vpos = row->y;
1488 while (glyph < end)
1490 hpos += glyph->pixel_width;
1491 ++glyph;
1494 success_p = 1;
1496 else
1498 hpos = vpos = 0;
1499 success_p = 0;
1502 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1503 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1504 return success_p;
1506 #endif
1508 *frame_x = hpos;
1509 *frame_y = vpos;
1510 return 1;
1514 #ifdef HAVE_WINDOW_SYSTEM
1516 /* Find the glyph under window-relative coordinates X/Y in window W.
1517 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1518 strings. Return in *HPOS and *VPOS the row and column number of
1519 the glyph found. Return in *AREA the glyph area containing X.
1520 Value is a pointer to the glyph found or null if X/Y is not on
1521 text, or we can't tell because W's current matrix is not up to
1522 date. */
1524 static struct glyph *
1525 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
1526 struct window *w;
1527 int x, y;
1528 int *hpos, *vpos, *area;
1529 int buffer_only_p;
1531 struct glyph *glyph, *end;
1532 struct glyph_row *row = NULL;
1533 int x0, i, left_area_width;
1535 /* Find row containing Y. Give up if some row is not enabled. */
1536 for (i = 0; i < w->current_matrix->nrows; ++i)
1538 row = MATRIX_ROW (w->current_matrix, i);
1539 if (!row->enabled_p)
1540 return NULL;
1541 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1542 break;
1545 *vpos = i;
1546 *hpos = 0;
1548 /* Give up if Y is not in the window. */
1549 if (i == w->current_matrix->nrows)
1550 return NULL;
1552 /* Get the glyph area containing X. */
1553 if (w->pseudo_window_p)
1555 *area = TEXT_AREA;
1556 x0 = 0;
1558 else
1560 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
1561 if (x < left_area_width)
1563 *area = LEFT_MARGIN_AREA;
1564 x0 = 0;
1566 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
1568 *area = TEXT_AREA;
1569 x0 = row->x + left_area_width;
1571 else
1573 *area = RIGHT_MARGIN_AREA;
1574 x0 = left_area_width + window_box_width (w, TEXT_AREA);
1578 /* Find glyph containing X. */
1579 glyph = row->glyphs[*area];
1580 end = glyph + row->used[*area];
1581 while (glyph < end)
1583 if (x < x0 + glyph->pixel_width)
1585 if (w->pseudo_window_p)
1586 break;
1587 else if (!buffer_only_p || BUFFERP (glyph->object))
1588 break;
1591 x0 += glyph->pixel_width;
1592 ++glyph;
1595 if (glyph == end)
1596 return NULL;
1598 *hpos = glyph - row->glyphs[*area];
1599 return glyph;
1603 /* EXPORT:
1604 Convert frame-relative x/y to coordinates relative to window W.
1605 Takes pseudo-windows into account. */
1607 void
1608 frame_to_window_pixel_xy (w, x, y)
1609 struct window *w;
1610 int *x, *y;
1612 if (w->pseudo_window_p)
1614 /* A pseudo-window is always full-width, and starts at the
1615 left edge of the frame, plus a frame border. */
1616 struct frame *f = XFRAME (w->frame);
1617 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
1618 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1620 else
1622 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
1623 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1627 /* EXPORT:
1628 Return in *R the clipping rectangle for glyph string S. */
1630 void
1631 get_glyph_string_clip_rect (s, nr)
1632 struct glyph_string *s;
1633 NativeRectangle *nr;
1635 XRectangle r;
1637 if (s->row->full_width_p)
1639 /* Draw full-width. X coordinates are relative to S->w->left. */
1640 int canon_x = CANON_X_UNIT (s->f);
1642 r.x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
1643 r.width = XFASTINT (s->w->width) * canon_x;
1645 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
1647 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
1648 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
1649 r.x -= width;
1652 r.x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
1654 /* Unless displaying a mode or menu bar line, which are always
1655 fully visible, clip to the visible part of the row. */
1656 if (s->w->pseudo_window_p)
1657 r.height = s->row->visible_height;
1658 else
1659 r.height = s->height;
1661 else
1663 /* This is a text line that may be partially visible. */
1664 r.x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
1665 r.width = window_box_width (s->w, s->area);
1666 r.height = s->row->visible_height;
1669 /* If S draws overlapping rows, it's sufficient to use the top and
1670 bottom of the window for clipping because this glyph string
1671 intentionally draws over other lines. */
1672 if (s->for_overlaps_p)
1674 r.y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1675 r.height = window_text_bottom_y (s->w) - r.y;
1677 else
1679 /* Don't use S->y for clipping because it doesn't take partially
1680 visible lines into account. For example, it can be negative for
1681 partially visible lines at the top of a window. */
1682 if (!s->row->full_width_p
1683 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1684 r.y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
1685 else
1686 r.y = max (0, s->row->y);
1688 /* If drawing a tool-bar window, draw it over the internal border
1689 at the top of the window. */
1690 if (s->w == XWINDOW (s->f->tool_bar_window))
1691 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1694 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1696 #ifdef HAVE_NTGUI
1697 /* ++KFS: From W32 port, but it looks ok for all platforms to me. */
1698 /* If drawing the cursor, don't let glyph draw outside its
1699 advertised boundaries. Cleartype does this under some circumstances. */
1700 if (s->hl == DRAW_CURSOR)
1702 if (s->x > r.x)
1704 r.width -= s->x - r.x;
1705 r.x = s->x;
1707 r.width = min (r.width, s->first_glyph->pixel_width);
1709 #endif
1711 #ifdef CONVERT_FROM_XRECT
1712 CONVERT_FROM_XRECT (r, *nr);
1713 #else
1714 *nr = r;
1715 #endif
1718 #endif /* HAVE_WINDOW_SYSTEM */
1721 /***********************************************************************
1722 Lisp form evaluation
1723 ***********************************************************************/
1725 /* Error handler for safe_eval and safe_call. */
1727 static Lisp_Object
1728 safe_eval_handler (arg)
1729 Lisp_Object arg;
1731 add_to_log ("Error during redisplay: %s", arg, Qnil);
1732 return Qnil;
1736 /* Evaluate SEXPR and return the result, or nil if something went
1737 wrong. Prevent redisplay during the evaluation. */
1739 Lisp_Object
1740 safe_eval (sexpr)
1741 Lisp_Object sexpr;
1743 Lisp_Object val;
1745 if (inhibit_eval_during_redisplay)
1746 val = Qnil;
1747 else
1749 int count = SPECPDL_INDEX ();
1750 struct gcpro gcpro1;
1752 GCPRO1 (sexpr);
1753 specbind (Qinhibit_redisplay, Qt);
1754 /* Use Qt to ensure debugger does not run,
1755 so there is no possibility of wanting to redisplay. */
1756 val = internal_condition_case_1 (Feval, sexpr, Qt,
1757 safe_eval_handler);
1758 UNGCPRO;
1759 val = unbind_to (count, val);
1762 return val;
1766 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1767 Return the result, or nil if something went wrong. Prevent
1768 redisplay during the evaluation. */
1770 Lisp_Object
1771 safe_call (nargs, args)
1772 int nargs;
1773 Lisp_Object *args;
1775 Lisp_Object val;
1777 if (inhibit_eval_during_redisplay)
1778 val = Qnil;
1779 else
1781 int count = SPECPDL_INDEX ();
1782 struct gcpro gcpro1;
1784 GCPRO1 (args[0]);
1785 gcpro1.nvars = nargs;
1786 specbind (Qinhibit_redisplay, Qt);
1787 /* Use Qt to ensure debugger does not run,
1788 so there is no possibility of wanting to redisplay. */
1789 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
1790 safe_eval_handler);
1791 UNGCPRO;
1792 val = unbind_to (count, val);
1795 return val;
1799 /* Call function FN with one argument ARG.
1800 Return the result, or nil if something went wrong. */
1802 Lisp_Object
1803 safe_call1 (fn, arg)
1804 Lisp_Object fn, arg;
1806 Lisp_Object args[2];
1807 args[0] = fn;
1808 args[1] = arg;
1809 return safe_call (2, args);
1814 /***********************************************************************
1815 Debugging
1816 ***********************************************************************/
1818 #if 0
1820 /* Define CHECK_IT to perform sanity checks on iterators.
1821 This is for debugging. It is too slow to do unconditionally. */
1823 static void
1824 check_it (it)
1825 struct it *it;
1827 if (it->method == next_element_from_string)
1829 xassert (STRINGP (it->string));
1830 xassert (IT_STRING_CHARPOS (*it) >= 0);
1832 else if (it->method == next_element_from_buffer)
1834 /* Check that character and byte positions agree. */
1835 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1838 if (it->dpvec)
1839 xassert (it->current.dpvec_index >= 0);
1840 else
1841 xassert (it->current.dpvec_index < 0);
1844 #define CHECK_IT(IT) check_it ((IT))
1846 #else /* not 0 */
1848 #define CHECK_IT(IT) (void) 0
1850 #endif /* not 0 */
1853 #if GLYPH_DEBUG
1855 /* Check that the window end of window W is what we expect it
1856 to be---the last row in the current matrix displaying text. */
1858 static void
1859 check_window_end (w)
1860 struct window *w;
1862 if (!MINI_WINDOW_P (w)
1863 && !NILP (w->window_end_valid))
1865 struct glyph_row *row;
1866 xassert ((row = MATRIX_ROW (w->current_matrix,
1867 XFASTINT (w->window_end_vpos)),
1868 !row->enabled_p
1869 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1870 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1874 #define CHECK_WINDOW_END(W) check_window_end ((W))
1876 #else /* not GLYPH_DEBUG */
1878 #define CHECK_WINDOW_END(W) (void) 0
1880 #endif /* not GLYPH_DEBUG */
1884 /***********************************************************************
1885 Iterator initialization
1886 ***********************************************************************/
1888 /* Initialize IT for displaying current_buffer in window W, starting
1889 at character position CHARPOS. CHARPOS < 0 means that no buffer
1890 position is specified which is useful when the iterator is assigned
1891 a position later. BYTEPOS is the byte position corresponding to
1892 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
1894 If ROW is not null, calls to produce_glyphs with IT as parameter
1895 will produce glyphs in that row.
1897 BASE_FACE_ID is the id of a base face to use. It must be one of
1898 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
1899 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
1900 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
1902 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
1903 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
1904 will be initialized to use the corresponding mode line glyph row of
1905 the desired matrix of W. */
1907 void
1908 init_iterator (it, w, charpos, bytepos, row, base_face_id)
1909 struct it *it;
1910 struct window *w;
1911 int charpos, bytepos;
1912 struct glyph_row *row;
1913 enum face_id base_face_id;
1915 int highlight_region_p;
1917 /* Some precondition checks. */
1918 xassert (w != NULL && it != NULL);
1919 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
1920 && charpos <= ZV));
1922 /* If face attributes have been changed since the last redisplay,
1923 free realized faces now because they depend on face definitions
1924 that might have changed. Don't free faces while there might be
1925 desired matrices pending which reference these faces. */
1926 if (face_change_count && !inhibit_free_realized_faces)
1928 face_change_count = 0;
1929 free_all_realized_faces (Qnil);
1932 /* Use one of the mode line rows of W's desired matrix if
1933 appropriate. */
1934 if (row == NULL)
1936 if (base_face_id == MODE_LINE_FACE_ID
1937 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
1938 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
1939 else if (base_face_id == HEADER_LINE_FACE_ID)
1940 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
1943 /* Clear IT. */
1944 bzero (it, sizeof *it);
1945 it->current.overlay_string_index = -1;
1946 it->current.dpvec_index = -1;
1947 it->base_face_id = base_face_id;
1949 /* The window in which we iterate over current_buffer: */
1950 XSETWINDOW (it->window, w);
1951 it->w = w;
1952 it->f = XFRAME (w->frame);
1954 /* Extra space between lines (on window systems only). */
1955 if (base_face_id == DEFAULT_FACE_ID
1956 && FRAME_WINDOW_P (it->f))
1958 if (NATNUMP (current_buffer->extra_line_spacing))
1959 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1960 else if (it->f->extra_line_spacing > 0)
1961 it->extra_line_spacing = it->f->extra_line_spacing;
1964 /* If realized faces have been removed, e.g. because of face
1965 attribute changes of named faces, recompute them. When running
1966 in batch mode, the face cache of Vterminal_frame is null. If
1967 we happen to get called, make a dummy face cache. */
1968 if (
1969 #ifndef WINDOWSNT
1970 noninteractive &&
1971 #endif
1972 FRAME_FACE_CACHE (it->f) == NULL)
1973 init_frame_faces (it->f);
1974 if (FRAME_FACE_CACHE (it->f)->used == 0)
1975 recompute_basic_faces (it->f);
1977 /* Current value of the `space-width', and 'height' properties. */
1978 it->space_width = Qnil;
1979 it->font_height = Qnil;
1981 /* Are control characters displayed as `^C'? */
1982 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1984 /* -1 means everything between a CR and the following line end
1985 is invisible. >0 means lines indented more than this value are
1986 invisible. */
1987 it->selective = (INTEGERP (current_buffer->selective_display)
1988 ? XFASTINT (current_buffer->selective_display)
1989 : (!NILP (current_buffer->selective_display)
1990 ? -1 : 0));
1991 it->selective_display_ellipsis_p
1992 = !NILP (current_buffer->selective_display_ellipses);
1994 /* Display table to use. */
1995 it->dp = window_display_table (w);
1997 /* Are multibyte characters enabled in current_buffer? */
1998 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2000 /* Non-zero if we should highlight the region. */
2001 highlight_region_p
2002 = (!NILP (Vtransient_mark_mode)
2003 && !NILP (current_buffer->mark_active)
2004 && XMARKER (current_buffer->mark)->buffer != 0);
2006 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2007 start and end of a visible region in window IT->w. Set both to
2008 -1 to indicate no region. */
2009 if (highlight_region_p
2010 /* Maybe highlight only in selected window. */
2011 && (/* Either show region everywhere. */
2012 highlight_nonselected_windows
2013 /* Or show region in the selected window. */
2014 || w == XWINDOW (selected_window)
2015 /* Or show the region if we are in the mini-buffer and W is
2016 the window the mini-buffer refers to. */
2017 || (MINI_WINDOW_P (XWINDOW (selected_window))
2018 && WINDOWP (minibuf_selected_window)
2019 && w == XWINDOW (minibuf_selected_window))))
2021 int charpos = marker_position (current_buffer->mark);
2022 it->region_beg_charpos = min (PT, charpos);
2023 it->region_end_charpos = max (PT, charpos);
2025 else
2026 it->region_beg_charpos = it->region_end_charpos = -1;
2028 /* Get the position at which the redisplay_end_trigger hook should
2029 be run, if it is to be run at all. */
2030 if (MARKERP (w->redisplay_end_trigger)
2031 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2032 it->redisplay_end_trigger_charpos
2033 = marker_position (w->redisplay_end_trigger);
2034 else if (INTEGERP (w->redisplay_end_trigger))
2035 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2037 /* Correct bogus values of tab_width. */
2038 it->tab_width = XINT (current_buffer->tab_width);
2039 if (it->tab_width <= 0 || it->tab_width > 1000)
2040 it->tab_width = 8;
2042 /* Are lines in the display truncated? */
2043 it->truncate_lines_p
2044 = (base_face_id != DEFAULT_FACE_ID
2045 || XINT (it->w->hscroll)
2046 || (truncate_partial_width_windows
2047 && !WINDOW_FULL_WIDTH_P (it->w))
2048 || !NILP (current_buffer->truncate_lines));
2050 /* Get dimensions of truncation and continuation glyphs. These are
2051 displayed as fringe bitmaps under X, so we don't need them for such
2052 frames. */
2053 if (!FRAME_WINDOW_P (it->f))
2055 if (it->truncate_lines_p)
2057 /* We will need the truncation glyph. */
2058 xassert (it->glyph_row == NULL);
2059 produce_special_glyphs (it, IT_TRUNCATION);
2060 it->truncation_pixel_width = it->pixel_width;
2062 else
2064 /* We will need the continuation glyph. */
2065 xassert (it->glyph_row == NULL);
2066 produce_special_glyphs (it, IT_CONTINUATION);
2067 it->continuation_pixel_width = it->pixel_width;
2070 /* Reset these values to zero because the produce_special_glyphs
2071 above has changed them. */
2072 it->pixel_width = it->ascent = it->descent = 0;
2073 it->phys_ascent = it->phys_descent = 0;
2076 /* Set this after getting the dimensions of truncation and
2077 continuation glyphs, so that we don't produce glyphs when calling
2078 produce_special_glyphs, above. */
2079 it->glyph_row = row;
2080 it->area = TEXT_AREA;
2082 /* Get the dimensions of the display area. The display area
2083 consists of the visible window area plus a horizontally scrolled
2084 part to the left of the window. All x-values are relative to the
2085 start of this total display area. */
2086 if (base_face_id != DEFAULT_FACE_ID)
2088 /* Mode lines, menu bar in terminal frames. */
2089 it->first_visible_x = 0;
2090 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
2092 else
2094 it->first_visible_x
2095 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
2096 it->last_visible_x = (it->first_visible_x
2097 + window_box_width (w, TEXT_AREA));
2099 /* If we truncate lines, leave room for the truncator glyph(s) at
2100 the right margin. Otherwise, leave room for the continuation
2101 glyph(s). Truncation and continuation glyphs are not inserted
2102 for window-based redisplay. */
2103 if (!FRAME_WINDOW_P (it->f))
2105 if (it->truncate_lines_p)
2106 it->last_visible_x -= it->truncation_pixel_width;
2107 else
2108 it->last_visible_x -= it->continuation_pixel_width;
2111 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2112 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
2115 /* Leave room for a border glyph. */
2116 if (!FRAME_WINDOW_P (it->f)
2117 && !WINDOW_RIGHTMOST_P (it->w))
2118 it->last_visible_x -= 1;
2120 it->last_visible_y = window_text_bottom_y (w);
2122 /* For mode lines and alike, arrange for the first glyph having a
2123 left box line if the face specifies a box. */
2124 if (base_face_id != DEFAULT_FACE_ID)
2126 struct face *face;
2128 it->face_id = base_face_id;
2130 /* If we have a boxed mode line, make the first character appear
2131 with a left box line. */
2132 face = FACE_FROM_ID (it->f, base_face_id);
2133 if (face->box != FACE_NO_BOX)
2134 it->start_of_box_run_p = 1;
2137 /* If a buffer position was specified, set the iterator there,
2138 getting overlays and face properties from that position. */
2139 if (charpos >= BUF_BEG (current_buffer))
2141 it->end_charpos = ZV;
2142 it->face_id = -1;
2143 IT_CHARPOS (*it) = charpos;
2145 /* Compute byte position if not specified. */
2146 if (bytepos < charpos)
2147 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2148 else
2149 IT_BYTEPOS (*it) = bytepos;
2151 /* Compute faces etc. */
2152 reseat (it, it->current.pos, 1);
2155 CHECK_IT (it);
2159 /* Initialize IT for the display of window W with window start POS. */
2161 void
2162 start_display (it, w, pos)
2163 struct it *it;
2164 struct window *w;
2165 struct text_pos pos;
2167 struct glyph_row *row;
2168 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2170 row = w->desired_matrix->rows + first_vpos;
2171 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2173 if (!it->truncate_lines_p)
2175 int start_at_line_beg_p;
2176 int first_y = it->current_y;
2178 /* If window start is not at a line start, skip forward to POS to
2179 get the correct continuation lines width. */
2180 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2181 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2182 if (!start_at_line_beg_p)
2184 int new_x;
2186 reseat_at_previous_visible_line_start (it);
2187 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2189 new_x = it->current_x + it->pixel_width;
2191 /* If lines are continued, this line may end in the middle
2192 of a multi-glyph character (e.g. a control character
2193 displayed as \003, or in the middle of an overlay
2194 string). In this case move_it_to above will not have
2195 taken us to the start of the continuation line but to the
2196 end of the continued line. */
2197 if (it->current_x > 0
2198 && !it->truncate_lines_p /* Lines are continued. */
2199 && (/* And glyph doesn't fit on the line. */
2200 new_x > it->last_visible_x
2201 /* Or it fits exactly and we're on a window
2202 system frame. */
2203 || (new_x == it->last_visible_x
2204 && FRAME_WINDOW_P (it->f))))
2206 if (it->current.dpvec_index >= 0
2207 || it->current.overlay_string_index >= 0)
2209 set_iterator_to_next (it, 1);
2210 move_it_in_display_line_to (it, -1, -1, 0);
2213 it->continuation_lines_width += it->current_x;
2216 /* We're starting a new display line, not affected by the
2217 height of the continued line, so clear the appropriate
2218 fields in the iterator structure. */
2219 it->max_ascent = it->max_descent = 0;
2220 it->max_phys_ascent = it->max_phys_descent = 0;
2222 it->current_y = first_y;
2223 it->vpos = 0;
2224 it->current_x = it->hpos = 0;
2228 #if 0 /* Don't assert the following because start_display is sometimes
2229 called intentionally with a window start that is not at a
2230 line start. Please leave this code in as a comment. */
2232 /* Window start should be on a line start, now. */
2233 xassert (it->continuation_lines_width
2234 || IT_CHARPOS (it) == BEGV
2235 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2236 #endif /* 0 */
2240 /* Return 1 if POS is a position in ellipses displayed for invisible
2241 text. W is the window we display, for text property lookup. */
2243 static int
2244 in_ellipses_for_invisible_text_p (pos, w)
2245 struct display_pos *pos;
2246 struct window *w;
2248 Lisp_Object prop, window;
2249 int ellipses_p = 0;
2250 int charpos = CHARPOS (pos->pos);
2252 /* If POS specifies a position in a display vector, this might
2253 be for an ellipsis displayed for invisible text. We won't
2254 get the iterator set up for delivering that ellipsis unless
2255 we make sure that it gets aware of the invisible text. */
2256 if (pos->dpvec_index >= 0
2257 && pos->overlay_string_index < 0
2258 && CHARPOS (pos->string_pos) < 0
2259 && charpos > BEGV
2260 && (XSETWINDOW (window, w),
2261 prop = Fget_char_property (make_number (charpos),
2262 Qinvisible, window),
2263 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2265 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2266 window);
2267 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2270 return ellipses_p;
2274 /* Initialize IT for stepping through current_buffer in window W,
2275 starting at position POS that includes overlay string and display
2276 vector/ control character translation position information. Value
2277 is zero if there are overlay strings with newlines at POS. */
2279 static int
2280 init_from_display_pos (it, w, pos)
2281 struct it *it;
2282 struct window *w;
2283 struct display_pos *pos;
2285 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2286 int i, overlay_strings_with_newlines = 0;
2288 /* If POS specifies a position in a display vector, this might
2289 be for an ellipsis displayed for invisible text. We won't
2290 get the iterator set up for delivering that ellipsis unless
2291 we make sure that it gets aware of the invisible text. */
2292 if (in_ellipses_for_invisible_text_p (pos, w))
2294 --charpos;
2295 bytepos = 0;
2298 /* Keep in mind: the call to reseat in init_iterator skips invisible
2299 text, so we might end up at a position different from POS. This
2300 is only a problem when POS is a row start after a newline and an
2301 overlay starts there with an after-string, and the overlay has an
2302 invisible property. Since we don't skip invisible text in
2303 display_line and elsewhere immediately after consuming the
2304 newline before the row start, such a POS will not be in a string,
2305 but the call to init_iterator below will move us to the
2306 after-string. */
2307 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2309 for (i = 0; i < it->n_overlay_strings; ++i)
2311 const char *s = SDATA (it->overlay_strings[i]);
2312 const char *e = s + SBYTES (it->overlay_strings[i]);
2314 while (s < e && *s != '\n')
2315 ++s;
2317 if (s < e)
2319 overlay_strings_with_newlines = 1;
2320 break;
2324 /* If position is within an overlay string, set up IT to the right
2325 overlay string. */
2326 if (pos->overlay_string_index >= 0)
2328 int relative_index;
2330 /* If the first overlay string happens to have a `display'
2331 property for an image, the iterator will be set up for that
2332 image, and we have to undo that setup first before we can
2333 correct the overlay string index. */
2334 if (it->method == next_element_from_image)
2335 pop_it (it);
2337 /* We already have the first chunk of overlay strings in
2338 IT->overlay_strings. Load more until the one for
2339 pos->overlay_string_index is in IT->overlay_strings. */
2340 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2342 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2343 it->current.overlay_string_index = 0;
2344 while (n--)
2346 load_overlay_strings (it, 0);
2347 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2351 it->current.overlay_string_index = pos->overlay_string_index;
2352 relative_index = (it->current.overlay_string_index
2353 % OVERLAY_STRING_CHUNK_SIZE);
2354 it->string = it->overlay_strings[relative_index];
2355 xassert (STRINGP (it->string));
2356 it->current.string_pos = pos->string_pos;
2357 it->method = next_element_from_string;
2360 #if 0 /* This is bogus because POS not having an overlay string
2361 position does not mean it's after the string. Example: A
2362 line starting with a before-string and initialization of IT
2363 to the previous row's end position. */
2364 else if (it->current.overlay_string_index >= 0)
2366 /* If POS says we're already after an overlay string ending at
2367 POS, make sure to pop the iterator because it will be in
2368 front of that overlay string. When POS is ZV, we've thereby
2369 also ``processed'' overlay strings at ZV. */
2370 while (it->sp)
2371 pop_it (it);
2372 it->current.overlay_string_index = -1;
2373 it->method = next_element_from_buffer;
2374 if (CHARPOS (pos->pos) == ZV)
2375 it->overlay_strings_at_end_processed_p = 1;
2377 #endif /* 0 */
2379 if (CHARPOS (pos->string_pos) >= 0)
2381 /* Recorded position is not in an overlay string, but in another
2382 string. This can only be a string from a `display' property.
2383 IT should already be filled with that string. */
2384 it->current.string_pos = pos->string_pos;
2385 xassert (STRINGP (it->string));
2388 /* Restore position in display vector translations, control
2389 character translations or ellipses. */
2390 if (pos->dpvec_index >= 0)
2392 if (it->dpvec == NULL)
2393 get_next_display_element (it);
2394 xassert (it->dpvec && it->current.dpvec_index == 0);
2395 it->current.dpvec_index = pos->dpvec_index;
2398 CHECK_IT (it);
2399 return !overlay_strings_with_newlines;
2403 /* Initialize IT for stepping through current_buffer in window W
2404 starting at ROW->start. */
2406 static void
2407 init_to_row_start (it, w, row)
2408 struct it *it;
2409 struct window *w;
2410 struct glyph_row *row;
2412 init_from_display_pos (it, w, &row->start);
2413 it->continuation_lines_width = row->continuation_lines_width;
2414 CHECK_IT (it);
2418 /* Initialize IT for stepping through current_buffer in window W
2419 starting in the line following ROW, i.e. starting at ROW->end.
2420 Value is zero if there are overlay strings with newlines at ROW's
2421 end position. */
2423 static int
2424 init_to_row_end (it, w, row)
2425 struct it *it;
2426 struct window *w;
2427 struct glyph_row *row;
2429 int success = 0;
2431 if (init_from_display_pos (it, w, &row->end))
2433 if (row->continued_p)
2434 it->continuation_lines_width
2435 = row->continuation_lines_width + row->pixel_width;
2436 CHECK_IT (it);
2437 success = 1;
2440 return success;
2446 /***********************************************************************
2447 Text properties
2448 ***********************************************************************/
2450 /* Called when IT reaches IT->stop_charpos. Handle text property and
2451 overlay changes. Set IT->stop_charpos to the next position where
2452 to stop. */
2454 static void
2455 handle_stop (it)
2456 struct it *it;
2458 enum prop_handled handled;
2459 int handle_overlay_change_p = 1;
2460 struct props *p;
2462 it->dpvec = NULL;
2463 it->current.dpvec_index = -1;
2467 handled = HANDLED_NORMALLY;
2469 /* Call text property handlers. */
2470 for (p = it_props; p->handler; ++p)
2472 handled = p->handler (it);
2474 if (handled == HANDLED_RECOMPUTE_PROPS)
2475 break;
2476 else if (handled == HANDLED_RETURN)
2477 return;
2478 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2479 handle_overlay_change_p = 0;
2482 if (handled != HANDLED_RECOMPUTE_PROPS)
2484 /* Don't check for overlay strings below when set to deliver
2485 characters from a display vector. */
2486 if (it->method == next_element_from_display_vector)
2487 handle_overlay_change_p = 0;
2489 /* Handle overlay changes. */
2490 if (handle_overlay_change_p)
2491 handled = handle_overlay_change (it);
2493 /* Determine where to stop next. */
2494 if (handled == HANDLED_NORMALLY)
2495 compute_stop_pos (it);
2498 while (handled == HANDLED_RECOMPUTE_PROPS);
2502 /* Compute IT->stop_charpos from text property and overlay change
2503 information for IT's current position. */
2505 static void
2506 compute_stop_pos (it)
2507 struct it *it;
2509 register INTERVAL iv, next_iv;
2510 Lisp_Object object, limit, position;
2512 /* If nowhere else, stop at the end. */
2513 it->stop_charpos = it->end_charpos;
2515 if (STRINGP (it->string))
2517 /* Strings are usually short, so don't limit the search for
2518 properties. */
2519 object = it->string;
2520 limit = Qnil;
2521 position = make_number (IT_STRING_CHARPOS (*it));
2523 else
2525 int charpos;
2527 /* If next overlay change is in front of the current stop pos
2528 (which is IT->end_charpos), stop there. Note: value of
2529 next_overlay_change is point-max if no overlay change
2530 follows. */
2531 charpos = next_overlay_change (IT_CHARPOS (*it));
2532 if (charpos < it->stop_charpos)
2533 it->stop_charpos = charpos;
2535 /* If showing the region, we have to stop at the region
2536 start or end because the face might change there. */
2537 if (it->region_beg_charpos > 0)
2539 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2540 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2541 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2542 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2545 /* Set up variables for computing the stop position from text
2546 property changes. */
2547 XSETBUFFER (object, current_buffer);
2548 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2549 position = make_number (IT_CHARPOS (*it));
2553 /* Get the interval containing IT's position. Value is a null
2554 interval if there isn't such an interval. */
2555 iv = validate_interval_range (object, &position, &position, 0);
2556 if (!NULL_INTERVAL_P (iv))
2558 Lisp_Object values_here[LAST_PROP_IDX];
2559 struct props *p;
2561 /* Get properties here. */
2562 for (p = it_props; p->handler; ++p)
2563 values_here[p->idx] = textget (iv->plist, *p->name);
2565 /* Look for an interval following iv that has different
2566 properties. */
2567 for (next_iv = next_interval (iv);
2568 (!NULL_INTERVAL_P (next_iv)
2569 && (NILP (limit)
2570 || XFASTINT (limit) > next_iv->position));
2571 next_iv = next_interval (next_iv))
2573 for (p = it_props; p->handler; ++p)
2575 Lisp_Object new_value;
2577 new_value = textget (next_iv->plist, *p->name);
2578 if (!EQ (values_here[p->idx], new_value))
2579 break;
2582 if (p->handler)
2583 break;
2586 if (!NULL_INTERVAL_P (next_iv))
2588 if (INTEGERP (limit)
2589 && next_iv->position >= XFASTINT (limit))
2590 /* No text property change up to limit. */
2591 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2592 else
2593 /* Text properties change in next_iv. */
2594 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2598 xassert (STRINGP (it->string)
2599 || (it->stop_charpos >= BEGV
2600 && it->stop_charpos >= IT_CHARPOS (*it)));
2604 /* Return the position of the next overlay change after POS in
2605 current_buffer. Value is point-max if no overlay change
2606 follows. This is like `next-overlay-change' but doesn't use
2607 xmalloc. */
2609 static int
2610 next_overlay_change (pos)
2611 int pos;
2613 int noverlays;
2614 int endpos;
2615 Lisp_Object *overlays;
2616 int len;
2617 int i;
2619 /* Get all overlays at the given position. */
2620 len = 10;
2621 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2622 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2623 if (noverlays > len)
2625 len = noverlays;
2626 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
2627 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
2630 /* If any of these overlays ends before endpos,
2631 use its ending point instead. */
2632 for (i = 0; i < noverlays; ++i)
2634 Lisp_Object oend;
2635 int oendpos;
2637 oend = OVERLAY_END (overlays[i]);
2638 oendpos = OVERLAY_POSITION (oend);
2639 endpos = min (endpos, oendpos);
2642 return endpos;
2647 /***********************************************************************
2648 Fontification
2649 ***********************************************************************/
2651 /* Handle changes in the `fontified' property of the current buffer by
2652 calling hook functions from Qfontification_functions to fontify
2653 regions of text. */
2655 static enum prop_handled
2656 handle_fontified_prop (it)
2657 struct it *it;
2659 Lisp_Object prop, pos;
2660 enum prop_handled handled = HANDLED_NORMALLY;
2662 /* Get the value of the `fontified' property at IT's current buffer
2663 position. (The `fontified' property doesn't have a special
2664 meaning in strings.) If the value is nil, call functions from
2665 Qfontification_functions. */
2666 if (!STRINGP (it->string)
2667 && it->s == NULL
2668 && !NILP (Vfontification_functions)
2669 && !NILP (Vrun_hooks)
2670 && (pos = make_number (IT_CHARPOS (*it)),
2671 prop = Fget_char_property (pos, Qfontified, Qnil),
2672 NILP (prop)))
2674 int count = SPECPDL_INDEX ();
2675 Lisp_Object val;
2677 val = Vfontification_functions;
2678 specbind (Qfontification_functions, Qnil);
2680 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2681 safe_call1 (val, pos);
2682 else
2684 Lisp_Object globals, fn;
2685 struct gcpro gcpro1, gcpro2;
2687 globals = Qnil;
2688 GCPRO2 (val, globals);
2690 for (; CONSP (val); val = XCDR (val))
2692 fn = XCAR (val);
2694 if (EQ (fn, Qt))
2696 /* A value of t indicates this hook has a local
2697 binding; it means to run the global binding too.
2698 In a global value, t should not occur. If it
2699 does, we must ignore it to avoid an endless
2700 loop. */
2701 for (globals = Fdefault_value (Qfontification_functions);
2702 CONSP (globals);
2703 globals = XCDR (globals))
2705 fn = XCAR (globals);
2706 if (!EQ (fn, Qt))
2707 safe_call1 (fn, pos);
2710 else
2711 safe_call1 (fn, pos);
2714 UNGCPRO;
2717 unbind_to (count, Qnil);
2719 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2720 something. This avoids an endless loop if they failed to
2721 fontify the text for which reason ever. */
2722 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2723 handled = HANDLED_RECOMPUTE_PROPS;
2726 return handled;
2731 /***********************************************************************
2732 Faces
2733 ***********************************************************************/
2735 /* Set up iterator IT from face properties at its current position.
2736 Called from handle_stop. */
2738 static enum prop_handled
2739 handle_face_prop (it)
2740 struct it *it;
2742 int new_face_id, next_stop;
2744 if (!STRINGP (it->string))
2746 new_face_id
2747 = face_at_buffer_position (it->w,
2748 IT_CHARPOS (*it),
2749 it->region_beg_charpos,
2750 it->region_end_charpos,
2751 &next_stop,
2752 (IT_CHARPOS (*it)
2753 + TEXT_PROP_DISTANCE_LIMIT),
2756 /* Is this a start of a run of characters with box face?
2757 Caveat: this can be called for a freshly initialized
2758 iterator; face_id is -1 in this case. We know that the new
2759 face will not change until limit, i.e. if the new face has a
2760 box, all characters up to limit will have one. But, as
2761 usual, we don't know whether limit is really the end. */
2762 if (new_face_id != it->face_id)
2764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2766 /* If new face has a box but old face has not, this is
2767 the start of a run of characters with box, i.e. it has
2768 a shadow on the left side. The value of face_id of the
2769 iterator will be -1 if this is the initial call that gets
2770 the face. In this case, we have to look in front of IT's
2771 position and see whether there is a face != new_face_id. */
2772 it->start_of_box_run_p
2773 = (new_face->box != FACE_NO_BOX
2774 && (it->face_id >= 0
2775 || IT_CHARPOS (*it) == BEG
2776 || new_face_id != face_before_it_pos (it)));
2777 it->face_box_p = new_face->box != FACE_NO_BOX;
2780 else
2782 int base_face_id, bufpos;
2784 if (it->current.overlay_string_index >= 0)
2785 bufpos = IT_CHARPOS (*it);
2786 else
2787 bufpos = 0;
2789 /* For strings from a buffer, i.e. overlay strings or strings
2790 from a `display' property, use the face at IT's current
2791 buffer position as the base face to merge with, so that
2792 overlay strings appear in the same face as surrounding
2793 text, unless they specify their own faces. */
2794 base_face_id = underlying_face_id (it);
2796 new_face_id = face_at_string_position (it->w,
2797 it->string,
2798 IT_STRING_CHARPOS (*it),
2799 bufpos,
2800 it->region_beg_charpos,
2801 it->region_end_charpos,
2802 &next_stop,
2803 base_face_id, 0);
2805 #if 0 /* This shouldn't be neccessary. Let's check it. */
2806 /* If IT is used to display a mode line we would really like to
2807 use the mode line face instead of the frame's default face. */
2808 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2809 && new_face_id == DEFAULT_FACE_ID)
2810 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
2811 #endif
2813 /* Is this a start of a run of characters with box? Caveat:
2814 this can be called for a freshly allocated iterator; face_id
2815 is -1 is this case. We know that the new face will not
2816 change until the next check pos, i.e. if the new face has a
2817 box, all characters up to that position will have a
2818 box. But, as usual, we don't know whether that position
2819 is really the end. */
2820 if (new_face_id != it->face_id)
2822 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2823 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2825 /* If new face has a box but old face hasn't, this is the
2826 start of a run of characters with box, i.e. it has a
2827 shadow on the left side. */
2828 it->start_of_box_run_p
2829 = new_face->box && (old_face == NULL || !old_face->box);
2830 it->face_box_p = new_face->box != FACE_NO_BOX;
2834 it->face_id = new_face_id;
2835 return HANDLED_NORMALLY;
2839 /* Return the ID of the face ``underlying'' IT's current position,
2840 which is in a string. If the iterator is associated with a
2841 buffer, return the face at IT's current buffer position.
2842 Otherwise, use the iterator's base_face_id. */
2844 static int
2845 underlying_face_id (it)
2846 struct it *it;
2848 int face_id = it->base_face_id, i;
2850 xassert (STRINGP (it->string));
2852 for (i = it->sp - 1; i >= 0; --i)
2853 if (NILP (it->stack[i].string))
2854 face_id = it->stack[i].face_id;
2856 return face_id;
2860 /* Compute the face one character before or after the current position
2861 of IT. BEFORE_P non-zero means get the face in front of IT's
2862 position. Value is the id of the face. */
2864 static int
2865 face_before_or_after_it_pos (it, before_p)
2866 struct it *it;
2867 int before_p;
2869 int face_id, limit;
2870 int next_check_charpos;
2871 struct text_pos pos;
2873 xassert (it->s == NULL);
2875 if (STRINGP (it->string))
2877 int bufpos, base_face_id;
2879 /* No face change past the end of the string (for the case
2880 we are padding with spaces). No face change before the
2881 string start. */
2882 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
2883 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2884 return it->face_id;
2886 /* Set pos to the position before or after IT's current position. */
2887 if (before_p)
2888 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2889 else
2890 /* For composition, we must check the character after the
2891 composition. */
2892 pos = (it->what == IT_COMPOSITION
2893 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2894 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
2896 if (it->current.overlay_string_index >= 0)
2897 bufpos = IT_CHARPOS (*it);
2898 else
2899 bufpos = 0;
2901 base_face_id = underlying_face_id (it);
2903 /* Get the face for ASCII, or unibyte. */
2904 face_id = face_at_string_position (it->w,
2905 it->string,
2906 CHARPOS (pos),
2907 bufpos,
2908 it->region_beg_charpos,
2909 it->region_end_charpos,
2910 &next_check_charpos,
2911 base_face_id, 0);
2913 /* Correct the face for charsets different from ASCII. Do it
2914 for the multibyte case only. The face returned above is
2915 suitable for unibyte text if IT->string is unibyte. */
2916 if (STRING_MULTIBYTE (it->string))
2918 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
2919 int rest = SBYTES (it->string) - BYTEPOS (pos);
2920 int c, len;
2921 struct face *face = FACE_FROM_ID (it->f, face_id);
2923 c = string_char_and_length (p, rest, &len);
2924 face_id = FACE_FOR_CHAR (it->f, face, c);
2927 else
2929 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2930 || (IT_CHARPOS (*it) <= BEGV && before_p))
2931 return it->face_id;
2933 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2934 pos = it->current.pos;
2936 if (before_p)
2937 DEC_TEXT_POS (pos, it->multibyte_p);
2938 else
2940 if (it->what == IT_COMPOSITION)
2941 /* For composition, we must check the position after the
2942 composition. */
2943 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2944 else
2945 INC_TEXT_POS (pos, it->multibyte_p);
2948 /* Determine face for CHARSET_ASCII, or unibyte. */
2949 face_id = face_at_buffer_position (it->w,
2950 CHARPOS (pos),
2951 it->region_beg_charpos,
2952 it->region_end_charpos,
2953 &next_check_charpos,
2954 limit, 0);
2956 /* Correct the face for charsets different from ASCII. Do it
2957 for the multibyte case only. The face returned above is
2958 suitable for unibyte text if current_buffer is unibyte. */
2959 if (it->multibyte_p)
2961 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
2962 struct face *face = FACE_FROM_ID (it->f, face_id);
2963 face_id = FACE_FOR_CHAR (it->f, face, c);
2967 return face_id;
2972 /***********************************************************************
2973 Invisible text
2974 ***********************************************************************/
2976 /* Set up iterator IT from invisible properties at its current
2977 position. Called from handle_stop. */
2979 static enum prop_handled
2980 handle_invisible_prop (it)
2981 struct it *it;
2983 enum prop_handled handled = HANDLED_NORMALLY;
2985 if (STRINGP (it->string))
2987 extern Lisp_Object Qinvisible;
2988 Lisp_Object prop, end_charpos, limit, charpos;
2990 /* Get the value of the invisible text property at the
2991 current position. Value will be nil if there is no such
2992 property. */
2993 charpos = make_number (IT_STRING_CHARPOS (*it));
2994 prop = Fget_text_property (charpos, Qinvisible, it->string);
2996 if (!NILP (prop)
2997 && IT_STRING_CHARPOS (*it) < it->end_charpos)
2999 handled = HANDLED_RECOMPUTE_PROPS;
3001 /* Get the position at which the next change of the
3002 invisible text property can be found in IT->string.
3003 Value will be nil if the property value is the same for
3004 all the rest of IT->string. */
3005 XSETINT (limit, SCHARS (it->string));
3006 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3007 it->string, limit);
3009 /* Text at current position is invisible. The next
3010 change in the property is at position end_charpos.
3011 Move IT's current position to that position. */
3012 if (INTEGERP (end_charpos)
3013 && XFASTINT (end_charpos) < XFASTINT (limit))
3015 struct text_pos old;
3016 old = it->current.string_pos;
3017 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3018 compute_string_pos (&it->current.string_pos, old, it->string);
3020 else
3022 /* The rest of the string is invisible. If this is an
3023 overlay string, proceed with the next overlay string
3024 or whatever comes and return a character from there. */
3025 if (it->current.overlay_string_index >= 0)
3027 next_overlay_string (it);
3028 /* Don't check for overlay strings when we just
3029 finished processing them. */
3030 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3032 else
3034 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3035 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3040 else
3042 int invis_p, newpos, next_stop, start_charpos;
3043 Lisp_Object pos, prop, overlay;
3045 /* First of all, is there invisible text at this position? */
3046 start_charpos = IT_CHARPOS (*it);
3047 pos = make_number (IT_CHARPOS (*it));
3048 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3049 &overlay);
3050 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3052 /* If we are on invisible text, skip over it. */
3053 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3055 /* Record whether we have to display an ellipsis for the
3056 invisible text. */
3057 int display_ellipsis_p = invis_p == 2;
3059 handled = HANDLED_RECOMPUTE_PROPS;
3061 /* Loop skipping over invisible text. The loop is left at
3062 ZV or with IT on the first char being visible again. */
3065 /* Try to skip some invisible text. Return value is the
3066 position reached which can be equal to IT's position
3067 if there is nothing invisible here. This skips both
3068 over invisible text properties and overlays with
3069 invisible property. */
3070 newpos = skip_invisible (IT_CHARPOS (*it),
3071 &next_stop, ZV, it->window);
3073 /* If we skipped nothing at all we weren't at invisible
3074 text in the first place. If everything to the end of
3075 the buffer was skipped, end the loop. */
3076 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3077 invis_p = 0;
3078 else
3080 /* We skipped some characters but not necessarily
3081 all there are. Check if we ended up on visible
3082 text. Fget_char_property returns the property of
3083 the char before the given position, i.e. if we
3084 get invis_p = 0, this means that the char at
3085 newpos is visible. */
3086 pos = make_number (newpos);
3087 prop = Fget_char_property (pos, Qinvisible, it->window);
3088 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3091 /* If we ended up on invisible text, proceed to
3092 skip starting with next_stop. */
3093 if (invis_p)
3094 IT_CHARPOS (*it) = next_stop;
3096 while (invis_p);
3098 /* The position newpos is now either ZV or on visible text. */
3099 IT_CHARPOS (*it) = newpos;
3100 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3102 /* If there are before-strings at the start of invisible
3103 text, and the text is invisible because of a text
3104 property, arrange to show before-strings because 20.x did
3105 it that way. (If the text is invisible because of an
3106 overlay property instead of a text property, this is
3107 already handled in the overlay code.) */
3108 if (NILP (overlay)
3109 && get_overlay_strings (it, start_charpos))
3111 handled = HANDLED_RECOMPUTE_PROPS;
3112 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3114 else if (display_ellipsis_p)
3115 setup_for_ellipsis (it);
3119 return handled;
3123 /* Make iterator IT return `...' next. */
3125 static void
3126 setup_for_ellipsis (it)
3127 struct it *it;
3129 if (it->dp
3130 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3132 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3133 it->dpvec = v->contents;
3134 it->dpend = v->contents + v->size;
3136 else
3138 /* Default `...'. */
3139 it->dpvec = default_invis_vector;
3140 it->dpend = default_invis_vector + 3;
3143 /* The ellipsis display does not replace the display of the
3144 character at the new position. Indicate this by setting
3145 IT->dpvec_char_len to zero. */
3146 it->dpvec_char_len = 0;
3148 it->current.dpvec_index = 0;
3149 it->method = next_element_from_display_vector;
3154 /***********************************************************************
3155 'display' property
3156 ***********************************************************************/
3158 /* Set up iterator IT from `display' property at its current position.
3159 Called from handle_stop. */
3161 static enum prop_handled
3162 handle_display_prop (it)
3163 struct it *it;
3165 Lisp_Object prop, object;
3166 struct text_pos *position;
3167 int display_replaced_p = 0;
3169 if (STRINGP (it->string))
3171 object = it->string;
3172 position = &it->current.string_pos;
3174 else
3176 object = it->w->buffer;
3177 position = &it->current.pos;
3180 /* Reset those iterator values set from display property values. */
3181 it->font_height = Qnil;
3182 it->space_width = Qnil;
3183 it->voffset = 0;
3185 /* We don't support recursive `display' properties, i.e. string
3186 values that have a string `display' property, that have a string
3187 `display' property etc. */
3188 if (!it->string_from_display_prop_p)
3189 it->area = TEXT_AREA;
3191 prop = Fget_char_property (make_number (position->charpos),
3192 Qdisplay, object);
3193 if (NILP (prop))
3194 return HANDLED_NORMALLY;
3196 if (CONSP (prop)
3197 /* Simple properties. */
3198 && !EQ (XCAR (prop), Qimage)
3199 && !EQ (XCAR (prop), Qspace)
3200 && !EQ (XCAR (prop), Qwhen)
3201 && !EQ (XCAR (prop), Qspace_width)
3202 && !EQ (XCAR (prop), Qheight)
3203 && !EQ (XCAR (prop), Qraise)
3204 /* Marginal area specifications. */
3205 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3206 && !NILP (XCAR (prop)))
3208 for (; CONSP (prop); prop = XCDR (prop))
3210 if (handle_single_display_prop (it, XCAR (prop), object,
3211 position, display_replaced_p))
3212 display_replaced_p = 1;
3215 else if (VECTORP (prop))
3217 int i;
3218 for (i = 0; i < ASIZE (prop); ++i)
3219 if (handle_single_display_prop (it, AREF (prop, i), object,
3220 position, display_replaced_p))
3221 display_replaced_p = 1;
3223 else
3225 if (handle_single_display_prop (it, prop, object, position, 0))
3226 display_replaced_p = 1;
3229 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3233 /* Value is the position of the end of the `display' property starting
3234 at START_POS in OBJECT. */
3236 static struct text_pos
3237 display_prop_end (it, object, start_pos)
3238 struct it *it;
3239 Lisp_Object object;
3240 struct text_pos start_pos;
3242 Lisp_Object end;
3243 struct text_pos end_pos;
3245 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3246 Qdisplay, object, Qnil);
3247 CHARPOS (end_pos) = XFASTINT (end);
3248 if (STRINGP (object))
3249 compute_string_pos (&end_pos, start_pos, it->string);
3250 else
3251 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3253 return end_pos;
3257 /* Set up IT from a single `display' sub-property value PROP. OBJECT
3258 is the object in which the `display' property was found. *POSITION
3259 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3260 means that we previously saw a display sub-property which already
3261 replaced text display with something else, for example an image;
3262 ignore such properties after the first one has been processed.
3264 If PROP is a `space' or `image' sub-property, set *POSITION to the
3265 end position of the `display' property.
3267 Value is non-zero if something was found which replaces the display
3268 of buffer or string text. */
3270 static int
3271 handle_single_display_prop (it, prop, object, position,
3272 display_replaced_before_p)
3273 struct it *it;
3274 Lisp_Object prop;
3275 Lisp_Object object;
3276 struct text_pos *position;
3277 int display_replaced_before_p;
3279 Lisp_Object value;
3280 int replaces_text_display_p = 0;
3281 Lisp_Object form;
3283 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
3284 evaluated. If the result is nil, VALUE is ignored. */
3285 form = Qt;
3286 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3288 prop = XCDR (prop);
3289 if (!CONSP (prop))
3290 return 0;
3291 form = XCAR (prop);
3292 prop = XCDR (prop);
3295 if (!NILP (form) && !EQ (form, Qt))
3297 int count = SPECPDL_INDEX ();
3298 struct gcpro gcpro1;
3300 /* Bind `object' to the object having the `display' property, a
3301 buffer or string. Bind `position' to the position in the
3302 object where the property was found, and `buffer-position'
3303 to the current position in the buffer. */
3304 specbind (Qobject, object);
3305 specbind (Qposition, make_number (CHARPOS (*position)));
3306 specbind (Qbuffer_position,
3307 make_number (STRINGP (object)
3308 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3309 GCPRO1 (form);
3310 form = safe_eval (form);
3311 UNGCPRO;
3312 unbind_to (count, Qnil);
3315 if (NILP (form))
3316 return 0;
3318 if (CONSP (prop)
3319 && EQ (XCAR (prop), Qheight)
3320 && CONSP (XCDR (prop)))
3322 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3323 return 0;
3325 /* `(height HEIGHT)'. */
3326 it->font_height = XCAR (XCDR (prop));
3327 if (!NILP (it->font_height))
3329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3330 int new_height = -1;
3332 if (CONSP (it->font_height)
3333 && (EQ (XCAR (it->font_height), Qplus)
3334 || EQ (XCAR (it->font_height), Qminus))
3335 && CONSP (XCDR (it->font_height))
3336 && INTEGERP (XCAR (XCDR (it->font_height))))
3338 /* `(+ N)' or `(- N)' where N is an integer. */
3339 int steps = XINT (XCAR (XCDR (it->font_height)));
3340 if (EQ (XCAR (it->font_height), Qplus))
3341 steps = - steps;
3342 it->face_id = smaller_face (it->f, it->face_id, steps);
3344 else if (FUNCTIONP (it->font_height))
3346 /* Call function with current height as argument.
3347 Value is the new height. */
3348 Lisp_Object height;
3349 height = safe_call1 (it->font_height,
3350 face->lface[LFACE_HEIGHT_INDEX]);
3351 if (NUMBERP (height))
3352 new_height = XFLOATINT (height);
3354 else if (NUMBERP (it->font_height))
3356 /* Value is a multiple of the canonical char height. */
3357 struct face *face;
3359 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3360 new_height = (XFLOATINT (it->font_height)
3361 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3363 else
3365 /* Evaluate IT->font_height with `height' bound to the
3366 current specified height to get the new height. */
3367 Lisp_Object value;
3368 int count = SPECPDL_INDEX ();
3370 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3371 value = safe_eval (it->font_height);
3372 unbind_to (count, Qnil);
3374 if (NUMBERP (value))
3375 new_height = XFLOATINT (value);
3378 if (new_height > 0)
3379 it->face_id = face_with_height (it->f, it->face_id, new_height);
3382 else if (CONSP (prop)
3383 && EQ (XCAR (prop), Qspace_width)
3384 && CONSP (XCDR (prop)))
3386 /* `(space_width WIDTH)'. */
3387 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3388 return 0;
3390 value = XCAR (XCDR (prop));
3391 if (NUMBERP (value) && XFLOATINT (value) > 0)
3392 it->space_width = value;
3394 else if (CONSP (prop)
3395 && EQ (XCAR (prop), Qraise)
3396 && CONSP (XCDR (prop)))
3398 /* `(raise FACTOR)'. */
3399 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3400 return 0;
3402 #ifdef HAVE_WINDOW_SYSTEM
3403 value = XCAR (XCDR (prop));
3404 if (NUMBERP (value))
3406 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3407 it->voffset = - (XFLOATINT (value)
3408 * (FONT_HEIGHT (face->font)));
3410 #endif /* HAVE_WINDOW_SYSTEM */
3412 else if (!it->string_from_display_prop_p)
3414 /* `((margin left-margin) VALUE)' or `((margin right-margin)
3415 VALUE) or `((margin nil) VALUE)' or VALUE. */
3416 Lisp_Object location, value;
3417 struct text_pos start_pos;
3418 int valid_p;
3420 /* Characters having this form of property are not displayed, so
3421 we have to find the end of the property. */
3422 start_pos = *position;
3423 *position = display_prop_end (it, object, start_pos);
3424 value = Qnil;
3426 /* Let's stop at the new position and assume that all
3427 text properties change there. */
3428 it->stop_charpos = position->charpos;
3430 location = Qunbound;
3431 if (CONSP (prop) && CONSP (XCAR (prop)))
3433 Lisp_Object tem;
3435 value = XCDR (prop);
3436 if (CONSP (value))
3437 value = XCAR (value);
3439 tem = XCAR (prop);
3440 if (EQ (XCAR (tem), Qmargin)
3441 && (tem = XCDR (tem),
3442 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3443 (NILP (tem)
3444 || EQ (tem, Qleft_margin)
3445 || EQ (tem, Qright_margin))))
3446 location = tem;
3449 if (EQ (location, Qunbound))
3451 location = Qnil;
3452 value = prop;
3455 #ifdef HAVE_WINDOW_SYSTEM
3456 if (FRAME_TERMCAP_P (it->f))
3457 valid_p = STRINGP (value);
3458 else
3459 valid_p = (STRINGP (value)
3460 || (CONSP (value) && EQ (XCAR (value), Qspace))
3461 || valid_image_p (value));
3462 #else /* not HAVE_WINDOW_SYSTEM */
3463 valid_p = STRINGP (value);
3464 #endif /* not HAVE_WINDOW_SYSTEM */
3466 if ((EQ (location, Qleft_margin)
3467 || EQ (location, Qright_margin)
3468 || NILP (location))
3469 && valid_p
3470 && !display_replaced_before_p)
3472 replaces_text_display_p = 1;
3474 /* Save current settings of IT so that we can restore them
3475 when we are finished with the glyph property value. */
3476 push_it (it);
3478 if (NILP (location))
3479 it->area = TEXT_AREA;
3480 else if (EQ (location, Qleft_margin))
3481 it->area = LEFT_MARGIN_AREA;
3482 else
3483 it->area = RIGHT_MARGIN_AREA;
3485 if (STRINGP (value))
3487 it->string = value;
3488 it->multibyte_p = STRING_MULTIBYTE (it->string);
3489 it->current.overlay_string_index = -1;
3490 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3491 it->end_charpos = it->string_nchars = SCHARS (it->string);
3492 it->method = next_element_from_string;
3493 it->stop_charpos = 0;
3494 it->string_from_display_prop_p = 1;
3495 /* Say that we haven't consumed the characters with
3496 `display' property yet. The call to pop_it in
3497 set_iterator_to_next will clean this up. */
3498 *position = start_pos;
3500 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3502 it->method = next_element_from_stretch;
3503 it->object = value;
3504 it->current.pos = it->position = start_pos;
3506 #ifdef HAVE_WINDOW_SYSTEM
3507 else
3509 it->what = IT_IMAGE;
3510 it->image_id = lookup_image (it->f, value);
3511 it->position = start_pos;
3512 it->object = NILP (object) ? it->w->buffer : object;
3513 it->method = next_element_from_image;
3515 /* Say that we haven't consumed the characters with
3516 `display' property yet. The call to pop_it in
3517 set_iterator_to_next will clean this up. */
3518 *position = start_pos;
3520 #endif /* HAVE_WINDOW_SYSTEM */
3522 else
3523 /* Invalid property or property not supported. Restore
3524 the position to what it was before. */
3525 *position = start_pos;
3528 return replaces_text_display_p;
3532 /* Check if PROP is a display sub-property value whose text should be
3533 treated as intangible. */
3535 static int
3536 single_display_prop_intangible_p (prop)
3537 Lisp_Object prop;
3539 /* Skip over `when FORM'. */
3540 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3542 prop = XCDR (prop);
3543 if (!CONSP (prop))
3544 return 0;
3545 prop = XCDR (prop);
3548 if (STRINGP (prop))
3549 return 1;
3551 if (!CONSP (prop))
3552 return 0;
3554 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3555 we don't need to treat text as intangible. */
3556 if (EQ (XCAR (prop), Qmargin))
3558 prop = XCDR (prop);
3559 if (!CONSP (prop))
3560 return 0;
3562 prop = XCDR (prop);
3563 if (!CONSP (prop)
3564 || EQ (XCAR (prop), Qleft_margin)
3565 || EQ (XCAR (prop), Qright_margin))
3566 return 0;
3569 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3573 /* Check if PROP is a display property value whose text should be
3574 treated as intangible. */
3577 display_prop_intangible_p (prop)
3578 Lisp_Object prop;
3580 if (CONSP (prop)
3581 && CONSP (XCAR (prop))
3582 && !EQ (Qmargin, XCAR (XCAR (prop))))
3584 /* A list of sub-properties. */
3585 while (CONSP (prop))
3587 if (single_display_prop_intangible_p (XCAR (prop)))
3588 return 1;
3589 prop = XCDR (prop);
3592 else if (VECTORP (prop))
3594 /* A vector of sub-properties. */
3595 int i;
3596 for (i = 0; i < ASIZE (prop); ++i)
3597 if (single_display_prop_intangible_p (AREF (prop, i)))
3598 return 1;
3600 else
3601 return single_display_prop_intangible_p (prop);
3603 return 0;
3607 /* Return 1 if PROP is a display sub-property value containing STRING. */
3609 static int
3610 single_display_prop_string_p (prop, string)
3611 Lisp_Object prop, string;
3613 if (EQ (string, prop))
3614 return 1;
3616 /* Skip over `when FORM'. */
3617 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3619 prop = XCDR (prop);
3620 if (!CONSP (prop))
3621 return 0;
3622 prop = XCDR (prop);
3625 if (CONSP (prop))
3626 /* Skip over `margin LOCATION'. */
3627 if (EQ (XCAR (prop), Qmargin))
3629 prop = XCDR (prop);
3630 if (!CONSP (prop))
3631 return 0;
3633 prop = XCDR (prop);
3634 if (!CONSP (prop))
3635 return 0;
3638 return CONSP (prop) && EQ (XCAR (prop), string);
3642 /* Return 1 if STRING appears in the `display' property PROP. */
3644 static int
3645 display_prop_string_p (prop, string)
3646 Lisp_Object prop, string;
3648 if (CONSP (prop)
3649 && CONSP (XCAR (prop))
3650 && !EQ (Qmargin, XCAR (XCAR (prop))))
3652 /* A list of sub-properties. */
3653 while (CONSP (prop))
3655 if (single_display_prop_string_p (XCAR (prop), string))
3656 return 1;
3657 prop = XCDR (prop);
3660 else if (VECTORP (prop))
3662 /* A vector of sub-properties. */
3663 int i;
3664 for (i = 0; i < ASIZE (prop); ++i)
3665 if (single_display_prop_string_p (AREF (prop, i), string))
3666 return 1;
3668 else
3669 return single_display_prop_string_p (prop, string);
3671 return 0;
3675 /* Determine from which buffer position in W's buffer STRING comes
3676 from. AROUND_CHARPOS is an approximate position where it could
3677 be from. Value is the buffer position or 0 if it couldn't be
3678 determined.
3680 W's buffer must be current.
3682 This function is necessary because we don't record buffer positions
3683 in glyphs generated from strings (to keep struct glyph small).
3684 This function may only use code that doesn't eval because it is
3685 called asynchronously from note_mouse_highlight. */
3688 string_buffer_position (w, string, around_charpos)
3689 struct window *w;
3690 Lisp_Object string;
3691 int around_charpos;
3693 Lisp_Object limit, prop, pos;
3694 const int MAX_DISTANCE = 1000;
3695 int found = 0;
3697 pos = make_number (around_charpos);
3698 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3699 while (!found && !EQ (pos, limit))
3701 prop = Fget_char_property (pos, Qdisplay, Qnil);
3702 if (!NILP (prop) && display_prop_string_p (prop, string))
3703 found = 1;
3704 else
3705 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
3708 if (!found)
3710 pos = make_number (around_charpos);
3711 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3712 while (!found && !EQ (pos, limit))
3714 prop = Fget_char_property (pos, Qdisplay, Qnil);
3715 if (!NILP (prop) && display_prop_string_p (prop, string))
3716 found = 1;
3717 else
3718 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
3719 limit);
3723 return found ? XINT (pos) : 0;
3728 /***********************************************************************
3729 `composition' property
3730 ***********************************************************************/
3732 /* Set up iterator IT from `composition' property at its current
3733 position. Called from handle_stop. */
3735 static enum prop_handled
3736 handle_composition_prop (it)
3737 struct it *it;
3739 Lisp_Object prop, string;
3740 int pos, pos_byte, end;
3741 enum prop_handled handled = HANDLED_NORMALLY;
3743 if (STRINGP (it->string))
3745 pos = IT_STRING_CHARPOS (*it);
3746 pos_byte = IT_STRING_BYTEPOS (*it);
3747 string = it->string;
3749 else
3751 pos = IT_CHARPOS (*it);
3752 pos_byte = IT_BYTEPOS (*it);
3753 string = Qnil;
3756 /* If there's a valid composition and point is not inside of the
3757 composition (in the case that the composition is from the current
3758 buffer), draw a glyph composed from the composition components. */
3759 if (find_composition (pos, -1, &pos, &end, &prop, string)
3760 && COMPOSITION_VALID_P (pos, end, prop)
3761 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3763 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3765 if (id >= 0)
3767 it->method = next_element_from_composition;
3768 it->cmp_id = id;
3769 it->cmp_len = COMPOSITION_LENGTH (prop);
3770 /* For a terminal, draw only the first character of the
3771 components. */
3772 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3773 it->len = (STRINGP (it->string)
3774 ? string_char_to_byte (it->string, end)
3775 : CHAR_TO_BYTE (end)) - pos_byte;
3776 it->stop_charpos = end;
3777 handled = HANDLED_RETURN;
3781 return handled;
3786 /***********************************************************************
3787 Overlay strings
3788 ***********************************************************************/
3790 /* The following structure is used to record overlay strings for
3791 later sorting in load_overlay_strings. */
3793 struct overlay_entry
3795 Lisp_Object overlay;
3796 Lisp_Object string;
3797 int priority;
3798 int after_string_p;
3802 /* Set up iterator IT from overlay strings at its current position.
3803 Called from handle_stop. */
3805 static enum prop_handled
3806 handle_overlay_change (it)
3807 struct it *it;
3809 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
3810 return HANDLED_RECOMPUTE_PROPS;
3811 else
3812 return HANDLED_NORMALLY;
3816 /* Set up the next overlay string for delivery by IT, if there is an
3817 overlay string to deliver. Called by set_iterator_to_next when the
3818 end of the current overlay string is reached. If there are more
3819 overlay strings to display, IT->string and
3820 IT->current.overlay_string_index are set appropriately here.
3821 Otherwise IT->string is set to nil. */
3823 static void
3824 next_overlay_string (it)
3825 struct it *it;
3827 ++it->current.overlay_string_index;
3828 if (it->current.overlay_string_index == it->n_overlay_strings)
3830 /* No more overlay strings. Restore IT's settings to what
3831 they were before overlay strings were processed, and
3832 continue to deliver from current_buffer. */
3833 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
3835 pop_it (it);
3836 xassert (it->stop_charpos >= BEGV
3837 && it->stop_charpos <= it->end_charpos);
3838 it->string = Qnil;
3839 it->current.overlay_string_index = -1;
3840 SET_TEXT_POS (it->current.string_pos, -1, -1);
3841 it->n_overlay_strings = 0;
3842 it->method = next_element_from_buffer;
3844 /* If we're at the end of the buffer, record that we have
3845 processed the overlay strings there already, so that
3846 next_element_from_buffer doesn't try it again. */
3847 if (IT_CHARPOS (*it) >= it->end_charpos)
3848 it->overlay_strings_at_end_processed_p = 1;
3850 /* If we have to display `...' for invisible text, set
3851 the iterator up for that. */
3852 if (display_ellipsis_p)
3853 setup_for_ellipsis (it);
3855 else
3857 /* There are more overlay strings to process. If
3858 IT->current.overlay_string_index has advanced to a position
3859 where we must load IT->overlay_strings with more strings, do
3860 it. */
3861 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3863 if (it->current.overlay_string_index && i == 0)
3864 load_overlay_strings (it, 0);
3866 /* Initialize IT to deliver display elements from the overlay
3867 string. */
3868 it->string = it->overlay_strings[i];
3869 it->multibyte_p = STRING_MULTIBYTE (it->string);
3870 SET_TEXT_POS (it->current.string_pos, 0, 0);
3871 it->method = next_element_from_string;
3872 it->stop_charpos = 0;
3875 CHECK_IT (it);
3879 /* Compare two overlay_entry structures E1 and E2. Used as a
3880 comparison function for qsort in load_overlay_strings. Overlay
3881 strings for the same position are sorted so that
3883 1. All after-strings come in front of before-strings, except
3884 when they come from the same overlay.
3886 2. Within after-strings, strings are sorted so that overlay strings
3887 from overlays with higher priorities come first.
3889 2. Within before-strings, strings are sorted so that overlay
3890 strings from overlays with higher priorities come last.
3892 Value is analogous to strcmp. */
3895 static int
3896 compare_overlay_entries (e1, e2)
3897 void *e1, *e2;
3899 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3900 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3901 int result;
3903 if (entry1->after_string_p != entry2->after_string_p)
3905 /* Let after-strings appear in front of before-strings if
3906 they come from different overlays. */
3907 if (EQ (entry1->overlay, entry2->overlay))
3908 result = entry1->after_string_p ? 1 : -1;
3909 else
3910 result = entry1->after_string_p ? -1 : 1;
3912 else if (entry1->after_string_p)
3913 /* After-strings sorted in order of decreasing priority. */
3914 result = entry2->priority - entry1->priority;
3915 else
3916 /* Before-strings sorted in order of increasing priority. */
3917 result = entry1->priority - entry2->priority;
3919 return result;
3923 /* Load the vector IT->overlay_strings with overlay strings from IT's
3924 current buffer position, or from CHARPOS if that is > 0. Set
3925 IT->n_overlays to the total number of overlay strings found.
3927 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3928 a time. On entry into load_overlay_strings,
3929 IT->current.overlay_string_index gives the number of overlay
3930 strings that have already been loaded by previous calls to this
3931 function.
3933 IT->add_overlay_start contains an additional overlay start
3934 position to consider for taking overlay strings from, if non-zero.
3935 This position comes into play when the overlay has an `invisible'
3936 property, and both before and after-strings. When we've skipped to
3937 the end of the overlay, because of its `invisible' property, we
3938 nevertheless want its before-string to appear.
3939 IT->add_overlay_start will contain the overlay start position
3940 in this case.
3942 Overlay strings are sorted so that after-string strings come in
3943 front of before-string strings. Within before and after-strings,
3944 strings are sorted by overlay priority. See also function
3945 compare_overlay_entries. */
3947 static void
3948 load_overlay_strings (it, charpos)
3949 struct it *it;
3950 int charpos;
3952 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
3953 Lisp_Object ov, overlay, window, str, invisible;
3954 int start, end;
3955 int size = 20;
3956 int n = 0, i, j, invis_p;
3957 struct overlay_entry *entries
3958 = (struct overlay_entry *) alloca (size * sizeof *entries);
3960 if (charpos <= 0)
3961 charpos = IT_CHARPOS (*it);
3963 /* Append the overlay string STRING of overlay OVERLAY to vector
3964 `entries' which has size `size' and currently contains `n'
3965 elements. AFTER_P non-zero means STRING is an after-string of
3966 OVERLAY. */
3967 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3968 do \
3970 Lisp_Object priority; \
3972 if (n == size) \
3974 int new_size = 2 * size; \
3975 struct overlay_entry *old = entries; \
3976 entries = \
3977 (struct overlay_entry *) alloca (new_size \
3978 * sizeof *entries); \
3979 bcopy (old, entries, size * sizeof *entries); \
3980 size = new_size; \
3983 entries[n].string = (STRING); \
3984 entries[n].overlay = (OVERLAY); \
3985 priority = Foverlay_get ((OVERLAY), Qpriority); \
3986 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
3987 entries[n].after_string_p = (AFTER_P); \
3988 ++n; \
3990 while (0)
3992 /* Process overlay before the overlay center. */
3993 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
3995 overlay = XCAR (ov);
3996 xassert (OVERLAYP (overlay));
3997 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3998 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4000 if (end < charpos)
4001 break;
4003 /* Skip this overlay if it doesn't start or end at IT's current
4004 position. */
4005 if (end != charpos && start != charpos)
4006 continue;
4008 /* Skip this overlay if it doesn't apply to IT->w. */
4009 window = Foverlay_get (overlay, Qwindow);
4010 if (WINDOWP (window) && XWINDOW (window) != it->w)
4011 continue;
4013 /* If the text ``under'' the overlay is invisible, both before-
4014 and after-strings from this overlay are visible; start and
4015 end position are indistinguishable. */
4016 invisible = Foverlay_get (overlay, Qinvisible);
4017 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4019 /* If overlay has a non-empty before-string, record it. */
4020 if ((start == charpos || (end == charpos && invis_p))
4021 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4022 && SCHARS (str))
4023 RECORD_OVERLAY_STRING (overlay, str, 0);
4025 /* If overlay has a non-empty after-string, record it. */
4026 if ((end == charpos || (start == charpos && invis_p))
4027 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4028 && SCHARS (str))
4029 RECORD_OVERLAY_STRING (overlay, str, 1);
4032 /* Process overlays after the overlay center. */
4033 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
4035 overlay = XCAR (ov);
4036 xassert (OVERLAYP (overlay));
4037 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4038 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4040 if (start > charpos)
4041 break;
4043 /* Skip this overlay if it doesn't start or end at IT's current
4044 position. */
4045 if (end != charpos && start != charpos)
4046 continue;
4048 /* Skip this overlay if it doesn't apply to IT->w. */
4049 window = Foverlay_get (overlay, Qwindow);
4050 if (WINDOWP (window) && XWINDOW (window) != it->w)
4051 continue;
4053 /* If the text ``under'' the overlay is invisible, it has a zero
4054 dimension, and both before- and after-strings apply. */
4055 invisible = Foverlay_get (overlay, Qinvisible);
4056 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4058 /* If overlay has a non-empty before-string, record it. */
4059 if ((start == charpos || (end == charpos && invis_p))
4060 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4061 && SCHARS (str))
4062 RECORD_OVERLAY_STRING (overlay, str, 0);
4064 /* If overlay has a non-empty after-string, record it. */
4065 if ((end == charpos || (start == charpos && invis_p))
4066 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4067 && SCHARS (str))
4068 RECORD_OVERLAY_STRING (overlay, str, 1);
4071 #undef RECORD_OVERLAY_STRING
4073 /* Sort entries. */
4074 if (n > 1)
4075 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4077 /* Record the total number of strings to process. */
4078 it->n_overlay_strings = n;
4080 /* IT->current.overlay_string_index is the number of overlay strings
4081 that have already been consumed by IT. Copy some of the
4082 remaining overlay strings to IT->overlay_strings. */
4083 i = 0;
4084 j = it->current.overlay_string_index;
4085 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4086 it->overlay_strings[i++] = entries[j++].string;
4088 CHECK_IT (it);
4092 /* Get the first chunk of overlay strings at IT's current buffer
4093 position, or at CHARPOS if that is > 0. Value is non-zero if at
4094 least one overlay string was found. */
4096 static int
4097 get_overlay_strings (it, charpos)
4098 struct it *it;
4099 int charpos;
4101 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4102 process. This fills IT->overlay_strings with strings, and sets
4103 IT->n_overlay_strings to the total number of strings to process.
4104 IT->pos.overlay_string_index has to be set temporarily to zero
4105 because load_overlay_strings needs this; it must be set to -1
4106 when no overlay strings are found because a zero value would
4107 indicate a position in the first overlay string. */
4108 it->current.overlay_string_index = 0;
4109 load_overlay_strings (it, charpos);
4111 /* If we found overlay strings, set up IT to deliver display
4112 elements from the first one. Otherwise set up IT to deliver
4113 from current_buffer. */
4114 if (it->n_overlay_strings)
4116 /* Make sure we know settings in current_buffer, so that we can
4117 restore meaningful values when we're done with the overlay
4118 strings. */
4119 compute_stop_pos (it);
4120 xassert (it->face_id >= 0);
4122 /* Save IT's settings. They are restored after all overlay
4123 strings have been processed. */
4124 xassert (it->sp == 0);
4125 push_it (it);
4127 /* Set up IT to deliver display elements from the first overlay
4128 string. */
4129 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4130 it->string = it->overlay_strings[0];
4131 it->stop_charpos = 0;
4132 xassert (STRINGP (it->string));
4133 it->end_charpos = SCHARS (it->string);
4134 it->multibyte_p = STRING_MULTIBYTE (it->string);
4135 it->method = next_element_from_string;
4137 else
4139 it->string = Qnil;
4140 it->current.overlay_string_index = -1;
4141 it->method = next_element_from_buffer;
4144 CHECK_IT (it);
4146 /* Value is non-zero if we found at least one overlay string. */
4147 return STRINGP (it->string);
4152 /***********************************************************************
4153 Saving and restoring state
4154 ***********************************************************************/
4156 /* Save current settings of IT on IT->stack. Called, for example,
4157 before setting up IT for an overlay string, to be able to restore
4158 IT's settings to what they were after the overlay string has been
4159 processed. */
4161 static void
4162 push_it (it)
4163 struct it *it;
4165 struct iterator_stack_entry *p;
4167 xassert (it->sp < 2);
4168 p = it->stack + it->sp;
4170 p->stop_charpos = it->stop_charpos;
4171 xassert (it->face_id >= 0);
4172 p->face_id = it->face_id;
4173 p->string = it->string;
4174 p->pos = it->current;
4175 p->end_charpos = it->end_charpos;
4176 p->string_nchars = it->string_nchars;
4177 p->area = it->area;
4178 p->multibyte_p = it->multibyte_p;
4179 p->space_width = it->space_width;
4180 p->font_height = it->font_height;
4181 p->voffset = it->voffset;
4182 p->string_from_display_prop_p = it->string_from_display_prop_p;
4183 p->display_ellipsis_p = 0;
4184 ++it->sp;
4188 /* Restore IT's settings from IT->stack. Called, for example, when no
4189 more overlay strings must be processed, and we return to delivering
4190 display elements from a buffer, or when the end of a string from a
4191 `display' property is reached and we return to delivering display
4192 elements from an overlay string, or from a buffer. */
4194 static void
4195 pop_it (it)
4196 struct it *it;
4198 struct iterator_stack_entry *p;
4200 xassert (it->sp > 0);
4201 --it->sp;
4202 p = it->stack + it->sp;
4203 it->stop_charpos = p->stop_charpos;
4204 it->face_id = p->face_id;
4205 it->string = p->string;
4206 it->current = p->pos;
4207 it->end_charpos = p->end_charpos;
4208 it->string_nchars = p->string_nchars;
4209 it->area = p->area;
4210 it->multibyte_p = p->multibyte_p;
4211 it->space_width = p->space_width;
4212 it->font_height = p->font_height;
4213 it->voffset = p->voffset;
4214 it->string_from_display_prop_p = p->string_from_display_prop_p;
4219 /***********************************************************************
4220 Moving over lines
4221 ***********************************************************************/
4223 /* Set IT's current position to the previous line start. */
4225 static void
4226 back_to_previous_line_start (it)
4227 struct it *it;
4229 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4230 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4234 /* Move IT to the next line start.
4236 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4237 we skipped over part of the text (as opposed to moving the iterator
4238 continuously over the text). Otherwise, don't change the value
4239 of *SKIPPED_P.
4241 Newlines may come from buffer text, overlay strings, or strings
4242 displayed via the `display' property. That's the reason we can't
4243 simply use find_next_newline_no_quit.
4245 Note that this function may not skip over invisible text that is so
4246 because of text properties and immediately follows a newline. If
4247 it would, function reseat_at_next_visible_line_start, when called
4248 from set_iterator_to_next, would effectively make invisible
4249 characters following a newline part of the wrong glyph row, which
4250 leads to wrong cursor motion. */
4252 static int
4253 forward_to_next_line_start (it, skipped_p)
4254 struct it *it;
4255 int *skipped_p;
4257 int old_selective, newline_found_p, n;
4258 const int MAX_NEWLINE_DISTANCE = 500;
4260 /* If already on a newline, just consume it to avoid unintended
4261 skipping over invisible text below. */
4262 if (it->what == IT_CHARACTER
4263 && it->c == '\n'
4264 && CHARPOS (it->position) == IT_CHARPOS (*it))
4266 set_iterator_to_next (it, 0);
4267 it->c = 0;
4268 return 1;
4271 /* Don't handle selective display in the following. It's (a)
4272 unnecessary because it's done by the caller, and (b) leads to an
4273 infinite recursion because next_element_from_ellipsis indirectly
4274 calls this function. */
4275 old_selective = it->selective;
4276 it->selective = 0;
4278 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4279 from buffer text. */
4280 for (n = newline_found_p = 0;
4281 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4282 n += STRINGP (it->string) ? 0 : 1)
4284 if (!get_next_display_element (it))
4285 return 0;
4286 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4287 set_iterator_to_next (it, 0);
4290 /* If we didn't find a newline near enough, see if we can use a
4291 short-cut. */
4292 if (!newline_found_p)
4294 int start = IT_CHARPOS (*it);
4295 int limit = find_next_newline_no_quit (start, 1);
4296 Lisp_Object pos;
4298 xassert (!STRINGP (it->string));
4300 /* If there isn't any `display' property in sight, and no
4301 overlays, we can just use the position of the newline in
4302 buffer text. */
4303 if (it->stop_charpos >= limit
4304 || ((pos = Fnext_single_property_change (make_number (start),
4305 Qdisplay,
4306 Qnil, make_number (limit)),
4307 NILP (pos))
4308 && next_overlay_change (start) == ZV))
4310 IT_CHARPOS (*it) = limit;
4311 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4312 *skipped_p = newline_found_p = 1;
4314 else
4316 while (get_next_display_element (it)
4317 && !newline_found_p)
4319 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4320 set_iterator_to_next (it, 0);
4325 it->selective = old_selective;
4326 return newline_found_p;
4330 /* Set IT's current position to the previous visible line start. Skip
4331 invisible text that is so either due to text properties or due to
4332 selective display. Caution: this does not change IT->current_x and
4333 IT->hpos. */
4335 static void
4336 back_to_previous_visible_line_start (it)
4337 struct it *it;
4339 int visible_p = 0;
4341 /* Go back one newline if not on BEGV already. */
4342 if (IT_CHARPOS (*it) > BEGV)
4343 back_to_previous_line_start (it);
4345 /* Move over lines that are invisible because of selective display
4346 or text properties. */
4347 while (IT_CHARPOS (*it) > BEGV
4348 && !visible_p)
4350 visible_p = 1;
4352 /* If selective > 0, then lines indented more than that values
4353 are invisible. */
4354 if (it->selective > 0
4355 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4356 (double) it->selective)) /* iftc */
4357 visible_p = 0;
4358 else
4360 Lisp_Object prop;
4362 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
4363 Qinvisible, it->window);
4364 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4365 visible_p = 0;
4368 /* Back one more newline if the current one is invisible. */
4369 if (!visible_p)
4370 back_to_previous_line_start (it);
4373 xassert (IT_CHARPOS (*it) >= BEGV);
4374 xassert (IT_CHARPOS (*it) == BEGV
4375 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4376 CHECK_IT (it);
4380 /* Reseat iterator IT at the previous visible line start. Skip
4381 invisible text that is so either due to text properties or due to
4382 selective display. At the end, update IT's overlay information,
4383 face information etc. */
4385 static void
4386 reseat_at_previous_visible_line_start (it)
4387 struct it *it;
4389 back_to_previous_visible_line_start (it);
4390 reseat (it, it->current.pos, 1);
4391 CHECK_IT (it);
4395 /* Reseat iterator IT on the next visible line start in the current
4396 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4397 preceding the line start. Skip over invisible text that is so
4398 because of selective display. Compute faces, overlays etc at the
4399 new position. Note that this function does not skip over text that
4400 is invisible because of text properties. */
4402 static void
4403 reseat_at_next_visible_line_start (it, on_newline_p)
4404 struct it *it;
4405 int on_newline_p;
4407 int newline_found_p, skipped_p = 0;
4409 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4411 /* Skip over lines that are invisible because they are indented
4412 more than the value of IT->selective. */
4413 if (it->selective > 0)
4414 while (IT_CHARPOS (*it) < ZV
4415 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4416 (double) it->selective)) /* iftc */
4418 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4419 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4422 /* Position on the newline if that's what's requested. */
4423 if (on_newline_p && newline_found_p)
4425 if (STRINGP (it->string))
4427 if (IT_STRING_CHARPOS (*it) > 0)
4429 --IT_STRING_CHARPOS (*it);
4430 --IT_STRING_BYTEPOS (*it);
4433 else if (IT_CHARPOS (*it) > BEGV)
4435 --IT_CHARPOS (*it);
4436 --IT_BYTEPOS (*it);
4437 reseat (it, it->current.pos, 0);
4440 else if (skipped_p)
4441 reseat (it, it->current.pos, 0);
4443 CHECK_IT (it);
4448 /***********************************************************************
4449 Changing an iterator's position
4450 ***********************************************************************/
4452 /* Change IT's current position to POS in current_buffer. If FORCE_P
4453 is non-zero, always check for text properties at the new position.
4454 Otherwise, text properties are only looked up if POS >=
4455 IT->check_charpos of a property. */
4457 static void
4458 reseat (it, pos, force_p)
4459 struct it *it;
4460 struct text_pos pos;
4461 int force_p;
4463 int original_pos = IT_CHARPOS (*it);
4465 reseat_1 (it, pos, 0);
4467 /* Determine where to check text properties. Avoid doing it
4468 where possible because text property lookup is very expensive. */
4469 if (force_p
4470 || CHARPOS (pos) > it->stop_charpos
4471 || CHARPOS (pos) < original_pos)
4472 handle_stop (it);
4474 CHECK_IT (it);
4478 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4479 IT->stop_pos to POS, also. */
4481 static void
4482 reseat_1 (it, pos, set_stop_p)
4483 struct it *it;
4484 struct text_pos pos;
4485 int set_stop_p;
4487 /* Don't call this function when scanning a C string. */
4488 xassert (it->s == NULL);
4490 /* POS must be a reasonable value. */
4491 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4493 it->current.pos = it->position = pos;
4494 XSETBUFFER (it->object, current_buffer);
4495 it->end_charpos = ZV;
4496 it->dpvec = NULL;
4497 it->current.dpvec_index = -1;
4498 it->current.overlay_string_index = -1;
4499 IT_STRING_CHARPOS (*it) = -1;
4500 IT_STRING_BYTEPOS (*it) = -1;
4501 it->string = Qnil;
4502 it->method = next_element_from_buffer;
4503 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4504 it->sp = 0;
4505 it->face_before_selective_p = 0;
4507 if (set_stop_p)
4508 it->stop_charpos = CHARPOS (pos);
4512 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4513 If S is non-null, it is a C string to iterate over. Otherwise,
4514 STRING gives a Lisp string to iterate over.
4516 If PRECISION > 0, don't return more then PRECISION number of
4517 characters from the string.
4519 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4520 characters have been returned. FIELD_WIDTH < 0 means an infinite
4521 field width.
4523 MULTIBYTE = 0 means disable processing of multibyte characters,
4524 MULTIBYTE > 0 means enable it,
4525 MULTIBYTE < 0 means use IT->multibyte_p.
4527 IT must be initialized via a prior call to init_iterator before
4528 calling this function. */
4530 static void
4531 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4532 struct it *it;
4533 unsigned char *s;
4534 Lisp_Object string;
4535 int charpos;
4536 int precision, field_width, multibyte;
4538 /* No region in strings. */
4539 it->region_beg_charpos = it->region_end_charpos = -1;
4541 /* No text property checks performed by default, but see below. */
4542 it->stop_charpos = -1;
4544 /* Set iterator position and end position. */
4545 bzero (&it->current, sizeof it->current);
4546 it->current.overlay_string_index = -1;
4547 it->current.dpvec_index = -1;
4548 xassert (charpos >= 0);
4550 /* If STRING is specified, use its multibyteness, otherwise use the
4551 setting of MULTIBYTE, if specified. */
4552 if (multibyte >= 0)
4553 it->multibyte_p = multibyte > 0;
4555 if (s == NULL)
4557 xassert (STRINGP (string));
4558 it->string = string;
4559 it->s = NULL;
4560 it->end_charpos = it->string_nchars = SCHARS (string);
4561 it->method = next_element_from_string;
4562 it->current.string_pos = string_pos (charpos, string);
4564 else
4566 it->s = s;
4567 it->string = Qnil;
4569 /* Note that we use IT->current.pos, not it->current.string_pos,
4570 for displaying C strings. */
4571 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4572 if (it->multibyte_p)
4574 it->current.pos = c_string_pos (charpos, s, 1);
4575 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4577 else
4579 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4580 it->end_charpos = it->string_nchars = strlen (s);
4583 it->method = next_element_from_c_string;
4586 /* PRECISION > 0 means don't return more than PRECISION characters
4587 from the string. */
4588 if (precision > 0 && it->end_charpos - charpos > precision)
4589 it->end_charpos = it->string_nchars = charpos + precision;
4591 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4592 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4593 FIELD_WIDTH < 0 means infinite field width. This is useful for
4594 padding with `-' at the end of a mode line. */
4595 if (field_width < 0)
4596 field_width = INFINITY;
4597 if (field_width > it->end_charpos - charpos)
4598 it->end_charpos = charpos + field_width;
4600 /* Use the standard display table for displaying strings. */
4601 if (DISP_TABLE_P (Vstandard_display_table))
4602 it->dp = XCHAR_TABLE (Vstandard_display_table);
4604 it->stop_charpos = charpos;
4605 CHECK_IT (it);
4610 /***********************************************************************
4611 Iteration
4612 ***********************************************************************/
4614 /* Load IT's display element fields with information about the next
4615 display element from the current position of IT. Value is zero if
4616 end of buffer (or C string) is reached. */
4619 get_next_display_element (it)
4620 struct it *it;
4622 /* Non-zero means that we found a display element. Zero means that
4623 we hit the end of what we iterate over. Performance note: the
4624 function pointer `method' used here turns out to be faster than
4625 using a sequence of if-statements. */
4626 int success_p = (*it->method) (it);
4628 if (it->what == IT_CHARACTER)
4630 /* Map via display table or translate control characters.
4631 IT->c, IT->len etc. have been set to the next character by
4632 the function call above. If we have a display table, and it
4633 contains an entry for IT->c, translate it. Don't do this if
4634 IT->c itself comes from a display table, otherwise we could
4635 end up in an infinite recursion. (An alternative could be to
4636 count the recursion depth of this function and signal an
4637 error when a certain maximum depth is reached.) Is it worth
4638 it? */
4639 if (success_p && it->dpvec == NULL)
4641 Lisp_Object dv;
4643 if (it->dp
4644 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4645 VECTORP (dv)))
4647 struct Lisp_Vector *v = XVECTOR (dv);
4649 /* Return the first character from the display table
4650 entry, if not empty. If empty, don't display the
4651 current character. */
4652 if (v->size)
4654 it->dpvec_char_len = it->len;
4655 it->dpvec = v->contents;
4656 it->dpend = v->contents + v->size;
4657 it->current.dpvec_index = 0;
4658 it->method = next_element_from_display_vector;
4659 success_p = get_next_display_element (it);
4661 else
4663 set_iterator_to_next (it, 0);
4664 success_p = get_next_display_element (it);
4668 /* Translate control characters into `\003' or `^C' form.
4669 Control characters coming from a display table entry are
4670 currently not translated because we use IT->dpvec to hold
4671 the translation. This could easily be changed but I
4672 don't believe that it is worth doing.
4674 If it->multibyte_p is nonzero, eight-bit characters and
4675 non-printable multibyte characters are also translated to
4676 octal form.
4678 If it->multibyte_p is zero, eight-bit characters that
4679 don't have corresponding multibyte char code are also
4680 translated to octal form. */
4681 else if ((it->c < ' '
4682 && (it->area != TEXT_AREA
4683 || (it->c != '\n' && it->c != '\t')))
4684 || (it->multibyte_p
4685 ? ((it->c >= 127
4686 && it->len == 1)
4687 || !CHAR_PRINTABLE_P (it->c))
4688 : (it->c >= 127
4689 && it->c == unibyte_char_to_multibyte (it->c))))
4691 /* IT->c is a control character which must be displayed
4692 either as '\003' or as `^C' where the '\\' and '^'
4693 can be defined in the display table. Fill
4694 IT->ctl_chars with glyphs for what we have to
4695 display. Then, set IT->dpvec to these glyphs. */
4696 GLYPH g;
4698 if (it->c < 128 && it->ctl_arrow_p)
4700 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4701 if (it->dp
4702 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4703 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4704 g = XINT (DISP_CTRL_GLYPH (it->dp));
4705 else
4706 g = FAST_MAKE_GLYPH ('^', 0);
4707 XSETINT (it->ctl_chars[0], g);
4709 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4710 XSETINT (it->ctl_chars[1], g);
4712 /* Set up IT->dpvec and return first character from it. */
4713 it->dpvec_char_len = it->len;
4714 it->dpvec = it->ctl_chars;
4715 it->dpend = it->dpvec + 2;
4716 it->current.dpvec_index = 0;
4717 it->method = next_element_from_display_vector;
4718 get_next_display_element (it);
4720 else
4722 unsigned char str[MAX_MULTIBYTE_LENGTH];
4723 int len;
4724 int i;
4725 GLYPH escape_glyph;
4727 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4728 if (it->dp
4729 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4730 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
4731 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
4732 else
4733 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4735 if (SINGLE_BYTE_CHAR_P (it->c))
4736 str[0] = it->c, len = 1;
4737 else
4739 len = CHAR_STRING_NO_SIGNAL (it->c, str);
4740 if (len < 0)
4742 /* It's an invalid character, which
4743 shouldn't happen actually, but due to
4744 bugs it may happen. Let's print the char
4745 as is, there's not much meaningful we can
4746 do with it. */
4747 str[0] = it->c;
4748 str[1] = it->c >> 8;
4749 str[2] = it->c >> 16;
4750 str[3] = it->c >> 24;
4751 len = 4;
4755 for (i = 0; i < len; i++)
4757 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4758 /* Insert three more glyphs into IT->ctl_chars for
4759 the octal display of the character. */
4760 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4761 XSETINT (it->ctl_chars[i * 4 + 1], g);
4762 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4763 XSETINT (it->ctl_chars[i * 4 + 2], g);
4764 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4765 XSETINT (it->ctl_chars[i * 4 + 3], g);
4768 /* Set up IT->dpvec and return the first character
4769 from it. */
4770 it->dpvec_char_len = it->len;
4771 it->dpvec = it->ctl_chars;
4772 it->dpend = it->dpvec + len * 4;
4773 it->current.dpvec_index = 0;
4774 it->method = next_element_from_display_vector;
4775 get_next_display_element (it);
4780 /* Adjust face id for a multibyte character. There are no
4781 multibyte character in unibyte text. */
4782 if (it->multibyte_p
4783 && success_p
4784 && FRAME_WINDOW_P (it->f))
4786 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4787 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
4791 /* Is this character the last one of a run of characters with
4792 box? If yes, set IT->end_of_box_run_p to 1. */
4793 if (it->face_box_p
4794 && it->s == NULL)
4796 int face_id;
4797 struct face *face;
4799 it->end_of_box_run_p
4800 = ((face_id = face_after_it_pos (it),
4801 face_id != it->face_id)
4802 && (face = FACE_FROM_ID (it->f, face_id),
4803 face->box == FACE_NO_BOX));
4806 /* Value is 0 if end of buffer or string reached. */
4807 return success_p;
4811 /* Move IT to the next display element.
4813 RESEAT_P non-zero means if called on a newline in buffer text,
4814 skip to the next visible line start.
4816 Functions get_next_display_element and set_iterator_to_next are
4817 separate because I find this arrangement easier to handle than a
4818 get_next_display_element function that also increments IT's
4819 position. The way it is we can first look at an iterator's current
4820 display element, decide whether it fits on a line, and if it does,
4821 increment the iterator position. The other way around we probably
4822 would either need a flag indicating whether the iterator has to be
4823 incremented the next time, or we would have to implement a
4824 decrement position function which would not be easy to write. */
4826 void
4827 set_iterator_to_next (it, reseat_p)
4828 struct it *it;
4829 int reseat_p;
4831 /* Reset flags indicating start and end of a sequence of characters
4832 with box. Reset them at the start of this function because
4833 moving the iterator to a new position might set them. */
4834 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4836 if (it->method == next_element_from_buffer)
4838 /* The current display element of IT is a character from
4839 current_buffer. Advance in the buffer, and maybe skip over
4840 invisible lines that are so because of selective display. */
4841 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
4842 reseat_at_next_visible_line_start (it, 0);
4843 else
4845 xassert (it->len != 0);
4846 IT_BYTEPOS (*it) += it->len;
4847 IT_CHARPOS (*it) += 1;
4848 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4851 else if (it->method == next_element_from_composition)
4853 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4854 if (STRINGP (it->string))
4856 IT_STRING_BYTEPOS (*it) += it->len;
4857 IT_STRING_CHARPOS (*it) += it->cmp_len;
4858 it->method = next_element_from_string;
4859 goto consider_string_end;
4861 else
4863 IT_BYTEPOS (*it) += it->len;
4864 IT_CHARPOS (*it) += it->cmp_len;
4865 it->method = next_element_from_buffer;
4868 else if (it->method == next_element_from_c_string)
4870 /* Current display element of IT is from a C string. */
4871 IT_BYTEPOS (*it) += it->len;
4872 IT_CHARPOS (*it) += 1;
4874 else if (it->method == next_element_from_display_vector)
4876 /* Current display element of IT is from a display table entry.
4877 Advance in the display table definition. Reset it to null if
4878 end reached, and continue with characters from buffers/
4879 strings. */
4880 ++it->current.dpvec_index;
4882 /* Restore face of the iterator to what they were before the
4883 display vector entry (these entries may contain faces). */
4884 it->face_id = it->saved_face_id;
4886 if (it->dpvec + it->current.dpvec_index == it->dpend)
4888 if (it->s)
4889 it->method = next_element_from_c_string;
4890 else if (STRINGP (it->string))
4891 it->method = next_element_from_string;
4892 else
4893 it->method = next_element_from_buffer;
4895 it->dpvec = NULL;
4896 it->current.dpvec_index = -1;
4898 /* Skip over characters which were displayed via IT->dpvec. */
4899 if (it->dpvec_char_len < 0)
4900 reseat_at_next_visible_line_start (it, 1);
4901 else if (it->dpvec_char_len > 0)
4903 it->len = it->dpvec_char_len;
4904 set_iterator_to_next (it, reseat_p);
4908 else if (it->method == next_element_from_string)
4910 /* Current display element is a character from a Lisp string. */
4911 xassert (it->s == NULL && STRINGP (it->string));
4912 IT_STRING_BYTEPOS (*it) += it->len;
4913 IT_STRING_CHARPOS (*it) += 1;
4915 consider_string_end:
4917 if (it->current.overlay_string_index >= 0)
4919 /* IT->string is an overlay string. Advance to the
4920 next, if there is one. */
4921 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
4922 next_overlay_string (it);
4924 else
4926 /* IT->string is not an overlay string. If we reached
4927 its end, and there is something on IT->stack, proceed
4928 with what is on the stack. This can be either another
4929 string, this time an overlay string, or a buffer. */
4930 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
4931 && it->sp > 0)
4933 pop_it (it);
4934 if (!STRINGP (it->string))
4935 it->method = next_element_from_buffer;
4936 else
4937 goto consider_string_end;
4941 else if (it->method == next_element_from_image
4942 || it->method == next_element_from_stretch)
4944 /* The position etc with which we have to proceed are on
4945 the stack. The position may be at the end of a string,
4946 if the `display' property takes up the whole string. */
4947 pop_it (it);
4948 it->image_id = 0;
4949 if (STRINGP (it->string))
4951 it->method = next_element_from_string;
4952 goto consider_string_end;
4954 else
4955 it->method = next_element_from_buffer;
4957 else
4958 /* There are no other methods defined, so this should be a bug. */
4959 abort ();
4961 xassert (it->method != next_element_from_string
4962 || (STRINGP (it->string)
4963 && IT_STRING_CHARPOS (*it) >= 0));
4967 /* Load IT's display element fields with information about the next
4968 display element which comes from a display table entry or from the
4969 result of translating a control character to one of the forms `^C'
4970 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4972 static int
4973 next_element_from_display_vector (it)
4974 struct it *it;
4976 /* Precondition. */
4977 xassert (it->dpvec && it->current.dpvec_index >= 0);
4979 /* Remember the current face id in case glyphs specify faces.
4980 IT's face is restored in set_iterator_to_next. */
4981 it->saved_face_id = it->face_id;
4983 if (INTEGERP (*it->dpvec)
4984 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4986 int lface_id;
4987 GLYPH g;
4989 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4990 it->c = FAST_GLYPH_CHAR (g);
4991 it->len = CHAR_BYTES (it->c);
4993 /* The entry may contain a face id to use. Such a face id is
4994 the id of a Lisp face, not a realized face. A face id of
4995 zero means no face is specified. */
4996 lface_id = FAST_GLYPH_FACE (g);
4997 if (lface_id)
4999 /* The function returns -1 if lface_id is invalid. */
5000 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
5001 if (face_id >= 0)
5002 it->face_id = face_id;
5005 else
5006 /* Display table entry is invalid. Return a space. */
5007 it->c = ' ', it->len = 1;
5009 /* Don't change position and object of the iterator here. They are
5010 still the values of the character that had this display table
5011 entry or was translated, and that's what we want. */
5012 it->what = IT_CHARACTER;
5013 return 1;
5017 /* Load IT with the next display element from Lisp string IT->string.
5018 IT->current.string_pos is the current position within the string.
5019 If IT->current.overlay_string_index >= 0, the Lisp string is an
5020 overlay string. */
5022 static int
5023 next_element_from_string (it)
5024 struct it *it;
5026 struct text_pos position;
5028 xassert (STRINGP (it->string));
5029 xassert (IT_STRING_CHARPOS (*it) >= 0);
5030 position = it->current.string_pos;
5032 /* Time to check for invisible text? */
5033 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5034 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5036 handle_stop (it);
5038 /* Since a handler may have changed IT->method, we must
5039 recurse here. */
5040 return get_next_display_element (it);
5043 if (it->current.overlay_string_index >= 0)
5045 /* Get the next character from an overlay string. In overlay
5046 strings, There is no field width or padding with spaces to
5047 do. */
5048 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5050 it->what = IT_EOB;
5051 return 0;
5053 else if (STRING_MULTIBYTE (it->string))
5055 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5056 const unsigned char *s = (SDATA (it->string)
5057 + IT_STRING_BYTEPOS (*it));
5058 it->c = string_char_and_length (s, remaining, &it->len);
5060 else
5062 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5063 it->len = 1;
5066 else
5068 /* Get the next character from a Lisp string that is not an
5069 overlay string. Such strings come from the mode line, for
5070 example. We may have to pad with spaces, or truncate the
5071 string. See also next_element_from_c_string. */
5072 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5074 it->what = IT_EOB;
5075 return 0;
5077 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5079 /* Pad with spaces. */
5080 it->c = ' ', it->len = 1;
5081 CHARPOS (position) = BYTEPOS (position) = -1;
5083 else if (STRING_MULTIBYTE (it->string))
5085 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5086 const unsigned char *s = (SDATA (it->string)
5087 + IT_STRING_BYTEPOS (*it));
5088 it->c = string_char_and_length (s, maxlen, &it->len);
5090 else
5092 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5093 it->len = 1;
5097 /* Record what we have and where it came from. Note that we store a
5098 buffer position in IT->position although it could arguably be a
5099 string position. */
5100 it->what = IT_CHARACTER;
5101 it->object = it->string;
5102 it->position = position;
5103 return 1;
5107 /* Load IT with next display element from C string IT->s.
5108 IT->string_nchars is the maximum number of characters to return
5109 from the string. IT->end_charpos may be greater than
5110 IT->string_nchars when this function is called, in which case we
5111 may have to return padding spaces. Value is zero if end of string
5112 reached, including padding spaces. */
5114 static int
5115 next_element_from_c_string (it)
5116 struct it *it;
5118 int success_p = 1;
5120 xassert (it->s);
5121 it->what = IT_CHARACTER;
5122 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5123 it->object = Qnil;
5125 /* IT's position can be greater IT->string_nchars in case a field
5126 width or precision has been specified when the iterator was
5127 initialized. */
5128 if (IT_CHARPOS (*it) >= it->end_charpos)
5130 /* End of the game. */
5131 it->what = IT_EOB;
5132 success_p = 0;
5134 else if (IT_CHARPOS (*it) >= it->string_nchars)
5136 /* Pad with spaces. */
5137 it->c = ' ', it->len = 1;
5138 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5140 else if (it->multibyte_p)
5142 /* Implementation note: The calls to strlen apparently aren't a
5143 performance problem because there is no noticeable performance
5144 difference between Emacs running in unibyte or multibyte mode. */
5145 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5146 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5147 maxlen, &it->len);
5149 else
5150 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5152 return success_p;
5156 /* Set up IT to return characters from an ellipsis, if appropriate.
5157 The definition of the ellipsis glyphs may come from a display table
5158 entry. This function Fills IT with the first glyph from the
5159 ellipsis if an ellipsis is to be displayed. */
5161 static int
5162 next_element_from_ellipsis (it)
5163 struct it *it;
5165 if (it->selective_display_ellipsis_p)
5167 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
5169 /* Use the display table definition for `...'. Invalid glyphs
5170 will be handled by the method returning elements from dpvec. */
5171 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
5172 it->dpvec_char_len = it->len;
5173 it->dpvec = v->contents;
5174 it->dpend = v->contents + v->size;
5175 it->current.dpvec_index = 0;
5176 it->method = next_element_from_display_vector;
5178 else
5180 /* Use default `...' which is stored in default_invis_vector. */
5181 it->dpvec_char_len = it->len;
5182 it->dpvec = default_invis_vector;
5183 it->dpend = default_invis_vector + 3;
5184 it->current.dpvec_index = 0;
5185 it->method = next_element_from_display_vector;
5188 else
5190 /* The face at the current position may be different from the
5191 face we find after the invisible text. Remember what it
5192 was in IT->saved_face_id, and signal that it's there by
5193 setting face_before_selective_p. */
5194 it->saved_face_id = it->face_id;
5195 it->method = next_element_from_buffer;
5196 reseat_at_next_visible_line_start (it, 1);
5197 it->face_before_selective_p = 1;
5200 return get_next_display_element (it);
5204 /* Deliver an image display element. The iterator IT is already
5205 filled with image information (done in handle_display_prop). Value
5206 is always 1. */
5209 static int
5210 next_element_from_image (it)
5211 struct it *it;
5213 it->what = IT_IMAGE;
5214 return 1;
5218 /* Fill iterator IT with next display element from a stretch glyph
5219 property. IT->object is the value of the text property. Value is
5220 always 1. */
5222 static int
5223 next_element_from_stretch (it)
5224 struct it *it;
5226 it->what = IT_STRETCH;
5227 return 1;
5231 /* Load IT with the next display element from current_buffer. Value
5232 is zero if end of buffer reached. IT->stop_charpos is the next
5233 position at which to stop and check for text properties or buffer
5234 end. */
5236 static int
5237 next_element_from_buffer (it)
5238 struct it *it;
5240 int success_p = 1;
5242 /* Check this assumption, otherwise, we would never enter the
5243 if-statement, below. */
5244 xassert (IT_CHARPOS (*it) >= BEGV
5245 && IT_CHARPOS (*it) <= it->stop_charpos);
5247 if (IT_CHARPOS (*it) >= it->stop_charpos)
5249 if (IT_CHARPOS (*it) >= it->end_charpos)
5251 int overlay_strings_follow_p;
5253 /* End of the game, except when overlay strings follow that
5254 haven't been returned yet. */
5255 if (it->overlay_strings_at_end_processed_p)
5256 overlay_strings_follow_p = 0;
5257 else
5259 it->overlay_strings_at_end_processed_p = 1;
5260 overlay_strings_follow_p = get_overlay_strings (it, 0);
5263 if (overlay_strings_follow_p)
5264 success_p = get_next_display_element (it);
5265 else
5267 it->what = IT_EOB;
5268 it->position = it->current.pos;
5269 success_p = 0;
5272 else
5274 handle_stop (it);
5275 return get_next_display_element (it);
5278 else
5280 /* No face changes, overlays etc. in sight, so just return a
5281 character from current_buffer. */
5282 unsigned char *p;
5284 /* Maybe run the redisplay end trigger hook. Performance note:
5285 This doesn't seem to cost measurable time. */
5286 if (it->redisplay_end_trigger_charpos
5287 && it->glyph_row
5288 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5289 run_redisplay_end_trigger_hook (it);
5291 /* Get the next character, maybe multibyte. */
5292 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5293 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5295 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5296 - IT_BYTEPOS (*it));
5297 it->c = string_char_and_length (p, maxlen, &it->len);
5299 else
5300 it->c = *p, it->len = 1;
5302 /* Record what we have and where it came from. */
5303 it->what = IT_CHARACTER;;
5304 it->object = it->w->buffer;
5305 it->position = it->current.pos;
5307 /* Normally we return the character found above, except when we
5308 really want to return an ellipsis for selective display. */
5309 if (it->selective)
5311 if (it->c == '\n')
5313 /* A value of selective > 0 means hide lines indented more
5314 than that number of columns. */
5315 if (it->selective > 0
5316 && IT_CHARPOS (*it) + 1 < ZV
5317 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5318 IT_BYTEPOS (*it) + 1,
5319 (double) it->selective)) /* iftc */
5321 success_p = next_element_from_ellipsis (it);
5322 it->dpvec_char_len = -1;
5325 else if (it->c == '\r' && it->selective == -1)
5327 /* A value of selective == -1 means that everything from the
5328 CR to the end of the line is invisible, with maybe an
5329 ellipsis displayed for it. */
5330 success_p = next_element_from_ellipsis (it);
5331 it->dpvec_char_len = -1;
5336 /* Value is zero if end of buffer reached. */
5337 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5338 return success_p;
5342 /* Run the redisplay end trigger hook for IT. */
5344 static void
5345 run_redisplay_end_trigger_hook (it)
5346 struct it *it;
5348 Lisp_Object args[3];
5350 /* IT->glyph_row should be non-null, i.e. we should be actually
5351 displaying something, or otherwise we should not run the hook. */
5352 xassert (it->glyph_row);
5354 /* Set up hook arguments. */
5355 args[0] = Qredisplay_end_trigger_functions;
5356 args[1] = it->window;
5357 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5358 it->redisplay_end_trigger_charpos = 0;
5360 /* Since we are *trying* to run these functions, don't try to run
5361 them again, even if they get an error. */
5362 it->w->redisplay_end_trigger = Qnil;
5363 Frun_hook_with_args (3, args);
5365 /* Notice if it changed the face of the character we are on. */
5366 handle_face_prop (it);
5370 /* Deliver a composition display element. The iterator IT is already
5371 filled with composition information (done in
5372 handle_composition_prop). Value is always 1. */
5374 static int
5375 next_element_from_composition (it)
5376 struct it *it;
5378 it->what = IT_COMPOSITION;
5379 it->position = (STRINGP (it->string)
5380 ? it->current.string_pos
5381 : it->current.pos);
5382 return 1;
5387 /***********************************************************************
5388 Moving an iterator without producing glyphs
5389 ***********************************************************************/
5391 /* Move iterator IT to a specified buffer or X position within one
5392 line on the display without producing glyphs.
5394 OP should be a bit mask including some or all of these bits:
5395 MOVE_TO_X: Stop on reaching x-position TO_X.
5396 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5397 Regardless of OP's value, stop in reaching the end of the display line.
5399 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5400 This means, in particular, that TO_X includes window's horizontal
5401 scroll amount.
5403 The return value has several possible values that
5404 say what condition caused the scan to stop:
5406 MOVE_POS_MATCH_OR_ZV
5407 - when TO_POS or ZV was reached.
5409 MOVE_X_REACHED
5410 -when TO_X was reached before TO_POS or ZV were reached.
5412 MOVE_LINE_CONTINUED
5413 - when we reached the end of the display area and the line must
5414 be continued.
5416 MOVE_LINE_TRUNCATED
5417 - when we reached the end of the display area and the line is
5418 truncated.
5420 MOVE_NEWLINE_OR_CR
5421 - when we stopped at a line end, i.e. a newline or a CR and selective
5422 display is on. */
5424 static enum move_it_result
5425 move_it_in_display_line_to (it, to_charpos, to_x, op)
5426 struct it *it;
5427 int to_charpos, to_x, op;
5429 enum move_it_result result = MOVE_UNDEFINED;
5430 struct glyph_row *saved_glyph_row;
5432 /* Don't produce glyphs in produce_glyphs. */
5433 saved_glyph_row = it->glyph_row;
5434 it->glyph_row = NULL;
5436 while (1)
5438 int x, i, ascent = 0, descent = 0;
5440 /* Stop when ZV or TO_CHARPOS reached. */
5441 if (!get_next_display_element (it)
5442 || ((op & MOVE_TO_POS) != 0
5443 && BUFFERP (it->object)
5444 && IT_CHARPOS (*it) >= to_charpos))
5446 result = MOVE_POS_MATCH_OR_ZV;
5447 break;
5450 /* The call to produce_glyphs will get the metrics of the
5451 display element IT is loaded with. We record in x the
5452 x-position before this display element in case it does not
5453 fit on the line. */
5454 x = it->current_x;
5456 /* Remember the line height so far in case the next element doesn't
5457 fit on the line. */
5458 if (!it->truncate_lines_p)
5460 ascent = it->max_ascent;
5461 descent = it->max_descent;
5464 PRODUCE_GLYPHS (it);
5466 if (it->area != TEXT_AREA)
5468 set_iterator_to_next (it, 1);
5469 continue;
5472 /* The number of glyphs we get back in IT->nglyphs will normally
5473 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5474 character on a terminal frame, or (iii) a line end. For the
5475 second case, IT->nglyphs - 1 padding glyphs will be present
5476 (on X frames, there is only one glyph produced for a
5477 composite character.
5479 The behavior implemented below means, for continuation lines,
5480 that as many spaces of a TAB as fit on the current line are
5481 displayed there. For terminal frames, as many glyphs of a
5482 multi-glyph character are displayed in the current line, too.
5483 This is what the old redisplay code did, and we keep it that
5484 way. Under X, the whole shape of a complex character must
5485 fit on the line or it will be completely displayed in the
5486 next line.
5488 Note that both for tabs and padding glyphs, all glyphs have
5489 the same width. */
5490 if (it->nglyphs)
5492 /* More than one glyph or glyph doesn't fit on line. All
5493 glyphs have the same width. */
5494 int single_glyph_width = it->pixel_width / it->nglyphs;
5495 int new_x;
5497 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5499 new_x = x + single_glyph_width;
5501 /* We want to leave anything reaching TO_X to the caller. */
5502 if ((op & MOVE_TO_X) && new_x > to_x)
5504 it->current_x = x;
5505 result = MOVE_X_REACHED;
5506 break;
5508 else if (/* Lines are continued. */
5509 !it->truncate_lines_p
5510 && (/* And glyph doesn't fit on the line. */
5511 new_x > it->last_visible_x
5512 /* Or it fits exactly and we're on a window
5513 system frame. */
5514 || (new_x == it->last_visible_x
5515 && FRAME_WINDOW_P (it->f))))
5517 if (/* IT->hpos == 0 means the very first glyph
5518 doesn't fit on the line, e.g. a wide image. */
5519 it->hpos == 0
5520 || (new_x == it->last_visible_x
5521 && FRAME_WINDOW_P (it->f)))
5523 ++it->hpos;
5524 it->current_x = new_x;
5525 if (i == it->nglyphs - 1)
5526 set_iterator_to_next (it, 1);
5528 else
5530 it->current_x = x;
5531 it->max_ascent = ascent;
5532 it->max_descent = descent;
5535 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5536 IT_CHARPOS (*it)));
5537 result = MOVE_LINE_CONTINUED;
5538 break;
5540 else if (new_x > it->first_visible_x)
5542 /* Glyph is visible. Increment number of glyphs that
5543 would be displayed. */
5544 ++it->hpos;
5546 else
5548 /* Glyph is completely off the left margin of the display
5549 area. Nothing to do. */
5553 if (result != MOVE_UNDEFINED)
5554 break;
5556 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5558 /* Stop when TO_X specified and reached. This check is
5559 necessary here because of lines consisting of a line end,
5560 only. The line end will not produce any glyphs and we
5561 would never get MOVE_X_REACHED. */
5562 xassert (it->nglyphs == 0);
5563 result = MOVE_X_REACHED;
5564 break;
5567 /* Is this a line end? If yes, we're done. */
5568 if (ITERATOR_AT_END_OF_LINE_P (it))
5570 result = MOVE_NEWLINE_OR_CR;
5571 break;
5574 /* The current display element has been consumed. Advance
5575 to the next. */
5576 set_iterator_to_next (it, 1);
5578 /* Stop if lines are truncated and IT's current x-position is
5579 past the right edge of the window now. */
5580 if (it->truncate_lines_p
5581 && it->current_x >= it->last_visible_x)
5583 result = MOVE_LINE_TRUNCATED;
5584 break;
5588 /* Restore the iterator settings altered at the beginning of this
5589 function. */
5590 it->glyph_row = saved_glyph_row;
5591 return result;
5595 /* Move IT forward until it satisfies one or more of the criteria in
5596 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
5598 OP is a bit-mask that specifies where to stop, and in particular,
5599 which of those four position arguments makes a difference. See the
5600 description of enum move_operation_enum.
5602 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5603 screen line, this function will set IT to the next position >
5604 TO_CHARPOS. */
5606 void
5607 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5608 struct it *it;
5609 int to_charpos, to_x, to_y, to_vpos;
5610 int op;
5612 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5613 int line_height;
5614 int reached = 0;
5616 for (;;)
5618 if (op & MOVE_TO_VPOS)
5620 /* If no TO_CHARPOS and no TO_X specified, stop at the
5621 start of the line TO_VPOS. */
5622 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5624 if (it->vpos == to_vpos)
5626 reached = 1;
5627 break;
5629 else
5630 skip = move_it_in_display_line_to (it, -1, -1, 0);
5632 else
5634 /* TO_VPOS >= 0 means stop at TO_X in the line at
5635 TO_VPOS, or at TO_POS, whichever comes first. */
5636 if (it->vpos == to_vpos)
5638 reached = 2;
5639 break;
5642 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5644 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
5646 reached = 3;
5647 break;
5649 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5651 /* We have reached TO_X but not in the line we want. */
5652 skip = move_it_in_display_line_to (it, to_charpos,
5653 -1, MOVE_TO_POS);
5654 if (skip == MOVE_POS_MATCH_OR_ZV)
5656 reached = 4;
5657 break;
5662 else if (op & MOVE_TO_Y)
5664 struct it it_backup;
5666 /* TO_Y specified means stop at TO_X in the line containing
5667 TO_Y---or at TO_CHARPOS if this is reached first. The
5668 problem is that we can't really tell whether the line
5669 contains TO_Y before we have completely scanned it, and
5670 this may skip past TO_X. What we do is to first scan to
5671 TO_X.
5673 If TO_X is not specified, use a TO_X of zero. The reason
5674 is to make the outcome of this function more predictable.
5675 If we didn't use TO_X == 0, we would stop at the end of
5676 the line which is probably not what a caller would expect
5677 to happen. */
5678 skip = move_it_in_display_line_to (it, to_charpos,
5679 ((op & MOVE_TO_X)
5680 ? to_x : 0),
5681 (MOVE_TO_X
5682 | (op & MOVE_TO_POS)));
5684 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5685 if (skip == MOVE_POS_MATCH_OR_ZV)
5687 reached = 5;
5688 break;
5691 /* If TO_X was reached, we would like to know whether TO_Y
5692 is in the line. This can only be said if we know the
5693 total line height which requires us to scan the rest of
5694 the line. */
5695 if (skip == MOVE_X_REACHED)
5697 it_backup = *it;
5698 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5699 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5700 op & MOVE_TO_POS);
5701 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5704 /* Now, decide whether TO_Y is in this line. */
5705 line_height = it->max_ascent + it->max_descent;
5706 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5708 if (to_y >= it->current_y
5709 && to_y < it->current_y + line_height)
5711 if (skip == MOVE_X_REACHED)
5712 /* If TO_Y is in this line and TO_X was reached above,
5713 we scanned too far. We have to restore IT's settings
5714 to the ones before skipping. */
5715 *it = it_backup;
5716 reached = 6;
5718 else if (skip == MOVE_X_REACHED)
5720 skip = skip2;
5721 if (skip == MOVE_POS_MATCH_OR_ZV)
5722 reached = 7;
5725 if (reached)
5726 break;
5728 else
5729 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5731 switch (skip)
5733 case MOVE_POS_MATCH_OR_ZV:
5734 reached = 8;
5735 goto out;
5737 case MOVE_NEWLINE_OR_CR:
5738 set_iterator_to_next (it, 1);
5739 it->continuation_lines_width = 0;
5740 break;
5742 case MOVE_LINE_TRUNCATED:
5743 it->continuation_lines_width = 0;
5744 reseat_at_next_visible_line_start (it, 0);
5745 if ((op & MOVE_TO_POS) != 0
5746 && IT_CHARPOS (*it) > to_charpos)
5748 reached = 9;
5749 goto out;
5751 break;
5753 case MOVE_LINE_CONTINUED:
5754 it->continuation_lines_width += it->current_x;
5755 break;
5757 default:
5758 abort ();
5761 /* Reset/increment for the next run. */
5762 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5763 it->current_x = it->hpos = 0;
5764 it->current_y += it->max_ascent + it->max_descent;
5765 ++it->vpos;
5766 last_height = it->max_ascent + it->max_descent;
5767 last_max_ascent = it->max_ascent;
5768 it->max_ascent = it->max_descent = 0;
5771 out:
5773 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5777 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5779 If DY > 0, move IT backward at least that many pixels. DY = 0
5780 means move IT backward to the preceding line start or BEGV. This
5781 function may move over more than DY pixels if IT->current_y - DY
5782 ends up in the middle of a line; in this case IT->current_y will be
5783 set to the top of the line moved to. */
5785 void
5786 move_it_vertically_backward (it, dy)
5787 struct it *it;
5788 int dy;
5790 int nlines, h;
5791 struct it it2, it3;
5792 int start_pos = IT_CHARPOS (*it);
5794 xassert (dy >= 0);
5796 /* Estimate how many newlines we must move back. */
5797 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5799 /* Set the iterator's position that many lines back. */
5800 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5801 back_to_previous_visible_line_start (it);
5803 /* Reseat the iterator here. When moving backward, we don't want
5804 reseat to skip forward over invisible text, set up the iterator
5805 to deliver from overlay strings at the new position etc. So,
5806 use reseat_1 here. */
5807 reseat_1 (it, it->current.pos, 1);
5809 /* We are now surely at a line start. */
5810 it->current_x = it->hpos = 0;
5811 it->continuation_lines_width = 0;
5813 /* Move forward and see what y-distance we moved. First move to the
5814 start of the next line so that we get its height. We need this
5815 height to be able to tell whether we reached the specified
5816 y-distance. */
5817 it2 = *it;
5818 it2.max_ascent = it2.max_descent = 0;
5819 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5820 MOVE_TO_POS | MOVE_TO_VPOS);
5821 xassert (IT_CHARPOS (*it) >= BEGV);
5822 it3 = it2;
5824 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5825 xassert (IT_CHARPOS (*it) >= BEGV);
5826 h = it2.current_y - it->current_y;
5827 nlines = it2.vpos - it->vpos;
5829 /* Correct IT's y and vpos position. */
5830 it->vpos -= nlines;
5831 it->current_y -= h;
5833 if (dy == 0)
5835 /* DY == 0 means move to the start of the screen line. The
5836 value of nlines is > 0 if continuation lines were involved. */
5837 if (nlines > 0)
5838 move_it_by_lines (it, nlines, 1);
5839 xassert (IT_CHARPOS (*it) <= start_pos);
5841 else if (nlines)
5843 /* The y-position we try to reach. Note that h has been
5844 subtracted in front of the if-statement. */
5845 int target_y = it->current_y + h - dy;
5846 int y0 = it3.current_y;
5847 int y1 = line_bottom_y (&it3);
5848 int line_height = y1 - y0;
5850 /* If we did not reach target_y, try to move further backward if
5851 we can. If we moved too far backward, try to move forward. */
5852 if (target_y < it->current_y
5853 /* This is heuristic. In a window that's 3 lines high, with
5854 a line height of 13 pixels each, recentering with point
5855 on the bottom line will try to move -39/2 = 19 pixels
5856 backward. Try to avoid moving into the first line. */
5857 && it->current_y - target_y > line_height / 3 * 2
5858 && IT_CHARPOS (*it) > BEGV)
5860 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
5861 target_y - it->current_y));
5862 move_it_vertically (it, target_y - it->current_y);
5863 xassert (IT_CHARPOS (*it) >= BEGV);
5865 else if (target_y >= it->current_y + line_height
5866 && IT_CHARPOS (*it) < ZV)
5868 /* Should move forward by at least one line, maybe more.
5870 Note: Calling move_it_by_lines can be expensive on
5871 terminal frames, where compute_motion is used (via
5872 vmotion) to do the job, when there are very long lines
5873 and truncate-lines is nil. That's the reason for
5874 treating terminal frames specially here. */
5876 if (!FRAME_WINDOW_P (it->f))
5877 move_it_vertically (it, target_y - (it->current_y + line_height));
5878 else
5882 move_it_by_lines (it, 1, 1);
5884 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
5887 xassert (IT_CHARPOS (*it) >= BEGV);
5893 /* Move IT by a specified amount of pixel lines DY. DY negative means
5894 move backwards. DY = 0 means move to start of screen line. At the
5895 end, IT will be on the start of a screen line. */
5897 void
5898 move_it_vertically (it, dy)
5899 struct it *it;
5900 int dy;
5902 if (dy <= 0)
5903 move_it_vertically_backward (it, -dy);
5904 else if (dy > 0)
5906 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5907 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5908 MOVE_TO_POS | MOVE_TO_Y);
5909 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5911 /* If buffer ends in ZV without a newline, move to the start of
5912 the line to satisfy the post-condition. */
5913 if (IT_CHARPOS (*it) == ZV
5914 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5915 move_it_by_lines (it, 0, 0);
5920 /* Move iterator IT past the end of the text line it is in. */
5922 void
5923 move_it_past_eol (it)
5924 struct it *it;
5926 enum move_it_result rc;
5928 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5929 if (rc == MOVE_NEWLINE_OR_CR)
5930 set_iterator_to_next (it, 0);
5934 #if 0 /* Currently not used. */
5936 /* Return non-zero if some text between buffer positions START_CHARPOS
5937 and END_CHARPOS is invisible. IT->window is the window for text
5938 property lookup. */
5940 static int
5941 invisible_text_between_p (it, start_charpos, end_charpos)
5942 struct it *it;
5943 int start_charpos, end_charpos;
5945 Lisp_Object prop, limit;
5946 int invisible_found_p;
5948 xassert (it != NULL && start_charpos <= end_charpos);
5950 /* Is text at START invisible? */
5951 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5952 it->window);
5953 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5954 invisible_found_p = 1;
5955 else
5957 limit = Fnext_single_char_property_change (make_number (start_charpos),
5958 Qinvisible, Qnil,
5959 make_number (end_charpos));
5960 invisible_found_p = XFASTINT (limit) < end_charpos;
5963 return invisible_found_p;
5966 #endif /* 0 */
5969 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
5970 negative means move up. DVPOS == 0 means move to the start of the
5971 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5972 NEED_Y_P is zero, IT->current_y will be left unchanged.
5974 Further optimization ideas: If we would know that IT->f doesn't use
5975 a face with proportional font, we could be faster for
5976 truncate-lines nil. */
5978 void
5979 move_it_by_lines (it, dvpos, need_y_p)
5980 struct it *it;
5981 int dvpos, need_y_p;
5983 struct position pos;
5985 if (!FRAME_WINDOW_P (it->f))
5987 struct text_pos textpos;
5989 /* We can use vmotion on frames without proportional fonts. */
5990 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5991 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5992 reseat (it, textpos, 1);
5993 it->vpos += pos.vpos;
5994 it->current_y += pos.vpos;
5996 else if (dvpos == 0)
5998 /* DVPOS == 0 means move to the start of the screen line. */
5999 move_it_vertically_backward (it, 0);
6000 xassert (it->current_x == 0 && it->hpos == 0);
6002 else if (dvpos > 0)
6003 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6004 else
6006 struct it it2;
6007 int start_charpos, i;
6009 /* Start at the beginning of the screen line containing IT's
6010 position. */
6011 move_it_vertically_backward (it, 0);
6013 /* Go back -DVPOS visible lines and reseat the iterator there. */
6014 start_charpos = IT_CHARPOS (*it);
6015 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6016 back_to_previous_visible_line_start (it);
6017 reseat (it, it->current.pos, 1);
6018 it->current_x = it->hpos = 0;
6020 /* Above call may have moved too far if continuation lines
6021 are involved. Scan forward and see if it did. */
6022 it2 = *it;
6023 it2.vpos = it2.current_y = 0;
6024 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6025 it->vpos -= it2.vpos;
6026 it->current_y -= it2.current_y;
6027 it->current_x = it->hpos = 0;
6029 /* If we moved too far, move IT some lines forward. */
6030 if (it2.vpos > -dvpos)
6032 int delta = it2.vpos + dvpos;
6033 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6038 /* Return 1 if IT points into the middle of a display vector. */
6041 in_display_vector_p (it)
6042 struct it *it;
6044 return (it->method == next_element_from_display_vector
6045 && it->current.dpvec_index > 0
6046 && it->dpvec + it->current.dpvec_index != it->dpend);
6050 /***********************************************************************
6051 Messages
6052 ***********************************************************************/
6055 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6056 to *Messages*. */
6058 void
6059 add_to_log (format, arg1, arg2)
6060 char *format;
6061 Lisp_Object arg1, arg2;
6063 Lisp_Object args[3];
6064 Lisp_Object msg, fmt;
6065 char *buffer;
6066 int len;
6067 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6069 /* Do nothing if called asynchronously. Inserting text into
6070 a buffer may call after-change-functions and alike and
6071 that would means running Lisp asynchronously. */
6072 if (handling_signal)
6073 return;
6075 fmt = msg = Qnil;
6076 GCPRO4 (fmt, msg, arg1, arg2);
6078 args[0] = fmt = build_string (format);
6079 args[1] = arg1;
6080 args[2] = arg2;
6081 msg = Fformat (3, args);
6083 len = SBYTES (msg) + 1;
6084 buffer = (char *) alloca (len);
6085 bcopy (SDATA (msg), buffer, len);
6087 message_dolog (buffer, len - 1, 1, 0);
6088 UNGCPRO;
6092 /* Output a newline in the *Messages* buffer if "needs" one. */
6094 void
6095 message_log_maybe_newline ()
6097 if (message_log_need_newline)
6098 message_dolog ("", 0, 1, 0);
6102 /* Add a string M of length NBYTES to the message log, optionally
6103 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6104 nonzero, means interpret the contents of M as multibyte. This
6105 function calls low-level routines in order to bypass text property
6106 hooks, etc. which might not be safe to run. */
6108 void
6109 message_dolog (m, nbytes, nlflag, multibyte)
6110 const char *m;
6111 int nbytes, nlflag, multibyte;
6113 if (!NILP (Vmemory_full))
6114 return;
6116 if (!NILP (Vmessage_log_max))
6118 struct buffer *oldbuf;
6119 Lisp_Object oldpoint, oldbegv, oldzv;
6120 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6121 int point_at_end = 0;
6122 int zv_at_end = 0;
6123 Lisp_Object old_deactivate_mark, tem;
6124 struct gcpro gcpro1;
6126 old_deactivate_mark = Vdeactivate_mark;
6127 oldbuf = current_buffer;
6128 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6129 current_buffer->undo_list = Qt;
6131 oldpoint = message_dolog_marker1;
6132 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6133 oldbegv = message_dolog_marker2;
6134 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6135 oldzv = message_dolog_marker3;
6136 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6137 GCPRO1 (old_deactivate_mark);
6139 if (PT == Z)
6140 point_at_end = 1;
6141 if (ZV == Z)
6142 zv_at_end = 1;
6144 BEGV = BEG;
6145 BEGV_BYTE = BEG_BYTE;
6146 ZV = Z;
6147 ZV_BYTE = Z_BYTE;
6148 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6150 /* Insert the string--maybe converting multibyte to single byte
6151 or vice versa, so that all the text fits the buffer. */
6152 if (multibyte
6153 && NILP (current_buffer->enable_multibyte_characters))
6155 int i, c, char_bytes;
6156 unsigned char work[1];
6158 /* Convert a multibyte string to single-byte
6159 for the *Message* buffer. */
6160 for (i = 0; i < nbytes; i += char_bytes)
6162 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6163 work[0] = (SINGLE_BYTE_CHAR_P (c)
6165 : multibyte_char_to_unibyte (c, Qnil));
6166 insert_1_both (work, 1, 1, 1, 0, 0);
6169 else if (! multibyte
6170 && ! NILP (current_buffer->enable_multibyte_characters))
6172 int i, c, char_bytes;
6173 unsigned char *msg = (unsigned char *) m;
6174 unsigned char str[MAX_MULTIBYTE_LENGTH];
6175 /* Convert a single-byte string to multibyte
6176 for the *Message* buffer. */
6177 for (i = 0; i < nbytes; i++)
6179 c = unibyte_char_to_multibyte (msg[i]);
6180 char_bytes = CHAR_STRING (c, str);
6181 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6184 else if (nbytes)
6185 insert_1 (m, nbytes, 1, 0, 0);
6187 if (nlflag)
6189 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6190 insert_1 ("\n", 1, 1, 0, 0);
6192 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6193 this_bol = PT;
6194 this_bol_byte = PT_BYTE;
6196 /* See if this line duplicates the previous one.
6197 If so, combine duplicates. */
6198 if (this_bol > BEG)
6200 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6201 prev_bol = PT;
6202 prev_bol_byte = PT_BYTE;
6204 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6205 this_bol, this_bol_byte);
6206 if (dup)
6208 del_range_both (prev_bol, prev_bol_byte,
6209 this_bol, this_bol_byte, 0);
6210 if (dup > 1)
6212 char dupstr[40];
6213 int duplen;
6215 /* If you change this format, don't forget to also
6216 change message_log_check_duplicate. */
6217 sprintf (dupstr, " [%d times]", dup);
6218 duplen = strlen (dupstr);
6219 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6220 insert_1 (dupstr, duplen, 1, 0, 1);
6225 /* If we have more than the desired maximum number of lines
6226 in the *Messages* buffer now, delete the oldest ones.
6227 This is safe because we don't have undo in this buffer. */
6229 if (NATNUMP (Vmessage_log_max))
6231 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6232 -XFASTINT (Vmessage_log_max) - 1, 0);
6233 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6236 BEGV = XMARKER (oldbegv)->charpos;
6237 BEGV_BYTE = marker_byte_position (oldbegv);
6239 if (zv_at_end)
6241 ZV = Z;
6242 ZV_BYTE = Z_BYTE;
6244 else
6246 ZV = XMARKER (oldzv)->charpos;
6247 ZV_BYTE = marker_byte_position (oldzv);
6250 if (point_at_end)
6251 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6252 else
6253 /* We can't do Fgoto_char (oldpoint) because it will run some
6254 Lisp code. */
6255 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6256 XMARKER (oldpoint)->bytepos);
6258 UNGCPRO;
6259 unchain_marker (oldpoint);
6260 unchain_marker (oldbegv);
6261 unchain_marker (oldzv);
6263 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6264 set_buffer_internal (oldbuf);
6265 if (NILP (tem))
6266 windows_or_buffers_changed = old_windows_or_buffers_changed;
6267 message_log_need_newline = !nlflag;
6268 Vdeactivate_mark = old_deactivate_mark;
6273 /* We are at the end of the buffer after just having inserted a newline.
6274 (Note: We depend on the fact we won't be crossing the gap.)
6275 Check to see if the most recent message looks a lot like the previous one.
6276 Return 0 if different, 1 if the new one should just replace it, or a
6277 value N > 1 if we should also append " [N times]". */
6279 static int
6280 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6281 int prev_bol, this_bol;
6282 int prev_bol_byte, this_bol_byte;
6284 int i;
6285 int len = Z_BYTE - 1 - this_bol_byte;
6286 int seen_dots = 0;
6287 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6288 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6290 for (i = 0; i < len; i++)
6292 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6293 seen_dots = 1;
6294 if (p1[i] != p2[i])
6295 return seen_dots;
6297 p1 += len;
6298 if (*p1 == '\n')
6299 return 2;
6300 if (*p1++ == ' ' && *p1++ == '[')
6302 int n = 0;
6303 while (*p1 >= '0' && *p1 <= '9')
6304 n = n * 10 + *p1++ - '0';
6305 if (strncmp (p1, " times]\n", 8) == 0)
6306 return n+1;
6308 return 0;
6312 /* Display an echo area message M with a specified length of NBYTES
6313 bytes. The string may include null characters. If M is 0, clear
6314 out any existing message, and let the mini-buffer text show
6315 through.
6317 The buffer M must continue to exist until after the echo area gets
6318 cleared or some other message gets displayed there. This means do
6319 not pass text that is stored in a Lisp string; do not pass text in
6320 a buffer that was alloca'd. */
6322 void
6323 message2 (m, nbytes, multibyte)
6324 const char *m;
6325 int nbytes;
6326 int multibyte;
6328 /* First flush out any partial line written with print. */
6329 message_log_maybe_newline ();
6330 if (m)
6331 message_dolog (m, nbytes, 1, multibyte);
6332 message2_nolog (m, nbytes, multibyte);
6336 /* The non-logging counterpart of message2. */
6338 void
6339 message2_nolog (m, nbytes, multibyte)
6340 const char *m;
6341 int nbytes, multibyte;
6343 struct frame *sf = SELECTED_FRAME ();
6344 message_enable_multibyte = multibyte;
6346 if (noninteractive)
6348 if (noninteractive_need_newline)
6349 putc ('\n', stderr);
6350 noninteractive_need_newline = 0;
6351 if (m)
6352 fwrite (m, nbytes, 1, stderr);
6353 if (cursor_in_echo_area == 0)
6354 fprintf (stderr, "\n");
6355 fflush (stderr);
6357 /* A null message buffer means that the frame hasn't really been
6358 initialized yet. Error messages get reported properly by
6359 cmd_error, so this must be just an informative message; toss it. */
6360 else if (INTERACTIVE
6361 && sf->glyphs_initialized_p
6362 && FRAME_MESSAGE_BUF (sf))
6364 Lisp_Object mini_window;
6365 struct frame *f;
6367 /* Get the frame containing the mini-buffer
6368 that the selected frame is using. */
6369 mini_window = FRAME_MINIBUF_WINDOW (sf);
6370 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6372 FRAME_SAMPLE_VISIBILITY (f);
6373 if (FRAME_VISIBLE_P (sf)
6374 && ! FRAME_VISIBLE_P (f))
6375 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6377 if (m)
6379 set_message (m, Qnil, nbytes, multibyte);
6380 if (minibuffer_auto_raise)
6381 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6383 else
6384 clear_message (1, 1);
6386 do_pending_window_change (0);
6387 echo_area_display (1);
6388 do_pending_window_change (0);
6389 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6390 (*frame_up_to_date_hook) (f);
6395 /* Display an echo area message M with a specified length of NBYTES
6396 bytes. The string may include null characters. If M is not a
6397 string, clear out any existing message, and let the mini-buffer
6398 text show through. */
6400 void
6401 message3 (m, nbytes, multibyte)
6402 Lisp_Object m;
6403 int nbytes;
6404 int multibyte;
6406 struct gcpro gcpro1;
6408 GCPRO1 (m);
6410 /* First flush out any partial line written with print. */
6411 message_log_maybe_newline ();
6412 if (STRINGP (m))
6413 message_dolog (SDATA (m), nbytes, 1, multibyte);
6414 message3_nolog (m, nbytes, multibyte);
6416 UNGCPRO;
6420 /* The non-logging version of message3. */
6422 void
6423 message3_nolog (m, nbytes, multibyte)
6424 Lisp_Object m;
6425 int nbytes, multibyte;
6427 struct frame *sf = SELECTED_FRAME ();
6428 message_enable_multibyte = multibyte;
6430 if (noninteractive)
6432 if (noninteractive_need_newline)
6433 putc ('\n', stderr);
6434 noninteractive_need_newline = 0;
6435 if (STRINGP (m))
6436 fwrite (SDATA (m), nbytes, 1, stderr);
6437 if (cursor_in_echo_area == 0)
6438 fprintf (stderr, "\n");
6439 fflush (stderr);
6441 /* A null message buffer means that the frame hasn't really been
6442 initialized yet. Error messages get reported properly by
6443 cmd_error, so this must be just an informative message; toss it. */
6444 else if (INTERACTIVE
6445 && sf->glyphs_initialized_p
6446 && FRAME_MESSAGE_BUF (sf))
6448 Lisp_Object mini_window;
6449 Lisp_Object frame;
6450 struct frame *f;
6452 /* Get the frame containing the mini-buffer
6453 that the selected frame is using. */
6454 mini_window = FRAME_MINIBUF_WINDOW (sf);
6455 frame = XWINDOW (mini_window)->frame;
6456 f = XFRAME (frame);
6458 FRAME_SAMPLE_VISIBILITY (f);
6459 if (FRAME_VISIBLE_P (sf)
6460 && !FRAME_VISIBLE_P (f))
6461 Fmake_frame_visible (frame);
6463 if (STRINGP (m) && SCHARS (m) > 0)
6465 set_message (NULL, m, nbytes, multibyte);
6466 if (minibuffer_auto_raise)
6467 Fraise_frame (frame);
6469 else
6470 clear_message (1, 1);
6472 do_pending_window_change (0);
6473 echo_area_display (1);
6474 do_pending_window_change (0);
6475 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6476 (*frame_up_to_date_hook) (f);
6481 /* Display a null-terminated echo area message M. If M is 0, clear
6482 out any existing message, and let the mini-buffer text show through.
6484 The buffer M must continue to exist until after the echo area gets
6485 cleared or some other message gets displayed there. Do not pass
6486 text that is stored in a Lisp string. Do not pass text in a buffer
6487 that was alloca'd. */
6489 void
6490 message1 (m)
6491 char *m;
6493 message2 (m, (m ? strlen (m) : 0), 0);
6497 /* The non-logging counterpart of message1. */
6499 void
6500 message1_nolog (m)
6501 char *m;
6503 message2_nolog (m, (m ? strlen (m) : 0), 0);
6506 /* Display a message M which contains a single %s
6507 which gets replaced with STRING. */
6509 void
6510 message_with_string (m, string, log)
6511 char *m;
6512 Lisp_Object string;
6513 int log;
6515 CHECK_STRING (string);
6517 if (noninteractive)
6519 if (m)
6521 if (noninteractive_need_newline)
6522 putc ('\n', stderr);
6523 noninteractive_need_newline = 0;
6524 fprintf (stderr, m, SDATA (string));
6525 if (cursor_in_echo_area == 0)
6526 fprintf (stderr, "\n");
6527 fflush (stderr);
6530 else if (INTERACTIVE)
6532 /* The frame whose minibuffer we're going to display the message on.
6533 It may be larger than the selected frame, so we need
6534 to use its buffer, not the selected frame's buffer. */
6535 Lisp_Object mini_window;
6536 struct frame *f, *sf = SELECTED_FRAME ();
6538 /* Get the frame containing the minibuffer
6539 that the selected frame is using. */
6540 mini_window = FRAME_MINIBUF_WINDOW (sf);
6541 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6543 /* A null message buffer means that the frame hasn't really been
6544 initialized yet. Error messages get reported properly by
6545 cmd_error, so this must be just an informative message; toss it. */
6546 if (FRAME_MESSAGE_BUF (f))
6548 Lisp_Object args[2], message;
6549 struct gcpro gcpro1, gcpro2;
6551 args[0] = build_string (m);
6552 args[1] = message = string;
6553 GCPRO2 (args[0], message);
6554 gcpro1.nvars = 2;
6556 message = Fformat (2, args);
6558 if (log)
6559 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
6560 else
6561 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
6563 UNGCPRO;
6565 /* Print should start at the beginning of the message
6566 buffer next time. */
6567 message_buf_print = 0;
6573 /* Dump an informative message to the minibuf. If M is 0, clear out
6574 any existing message, and let the mini-buffer text show through. */
6576 /* VARARGS 1 */
6577 void
6578 message (m, a1, a2, a3)
6579 char *m;
6580 EMACS_INT a1, a2, a3;
6582 if (noninteractive)
6584 if (m)
6586 if (noninteractive_need_newline)
6587 putc ('\n', stderr);
6588 noninteractive_need_newline = 0;
6589 fprintf (stderr, m, a1, a2, a3);
6590 if (cursor_in_echo_area == 0)
6591 fprintf (stderr, "\n");
6592 fflush (stderr);
6595 else if (INTERACTIVE)
6597 /* The frame whose mini-buffer we're going to display the message
6598 on. It may be larger than the selected frame, so we need to
6599 use its buffer, not the selected frame's buffer. */
6600 Lisp_Object mini_window;
6601 struct frame *f, *sf = SELECTED_FRAME ();
6603 /* Get the frame containing the mini-buffer
6604 that the selected frame is using. */
6605 mini_window = FRAME_MINIBUF_WINDOW (sf);
6606 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6608 /* A null message buffer means that the frame hasn't really been
6609 initialized yet. Error messages get reported properly by
6610 cmd_error, so this must be just an informative message; toss
6611 it. */
6612 if (FRAME_MESSAGE_BUF (f))
6614 if (m)
6616 int len;
6617 #ifdef NO_ARG_ARRAY
6618 char *a[3];
6619 a[0] = (char *) a1;
6620 a[1] = (char *) a2;
6621 a[2] = (char *) a3;
6623 len = doprnt (FRAME_MESSAGE_BUF (f),
6624 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
6625 #else
6626 len = doprnt (FRAME_MESSAGE_BUF (f),
6627 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
6628 (char **) &a1);
6629 #endif /* NO_ARG_ARRAY */
6631 message2 (FRAME_MESSAGE_BUF (f), len, 0);
6633 else
6634 message1 (0);
6636 /* Print should start at the beginning of the message
6637 buffer next time. */
6638 message_buf_print = 0;
6644 /* The non-logging version of message. */
6646 void
6647 message_nolog (m, a1, a2, a3)
6648 char *m;
6649 EMACS_INT a1, a2, a3;
6651 Lisp_Object old_log_max;
6652 old_log_max = Vmessage_log_max;
6653 Vmessage_log_max = Qnil;
6654 message (m, a1, a2, a3);
6655 Vmessage_log_max = old_log_max;
6659 /* Display the current message in the current mini-buffer. This is
6660 only called from error handlers in process.c, and is not time
6661 critical. */
6663 void
6664 update_echo_area ()
6666 if (!NILP (echo_area_buffer[0]))
6668 Lisp_Object string;
6669 string = Fcurrent_message ();
6670 message3 (string, SBYTES (string),
6671 !NILP (current_buffer->enable_multibyte_characters));
6676 /* Make sure echo area buffers in `echo_buffers' are live.
6677 If they aren't, make new ones. */
6679 static void
6680 ensure_echo_area_buffers ()
6682 int i;
6684 for (i = 0; i < 2; ++i)
6685 if (!BUFFERP (echo_buffer[i])
6686 || NILP (XBUFFER (echo_buffer[i])->name))
6688 char name[30];
6689 Lisp_Object old_buffer;
6690 int j;
6692 old_buffer = echo_buffer[i];
6693 sprintf (name, " *Echo Area %d*", i);
6694 echo_buffer[i] = Fget_buffer_create (build_string (name));
6695 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
6697 for (j = 0; j < 2; ++j)
6698 if (EQ (old_buffer, echo_area_buffer[j]))
6699 echo_area_buffer[j] = echo_buffer[i];
6704 /* Call FN with args A1..A4 with either the current or last displayed
6705 echo_area_buffer as current buffer.
6707 WHICH zero means use the current message buffer
6708 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6709 from echo_buffer[] and clear it.
6711 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6712 suitable buffer from echo_buffer[] and clear it.
6714 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6715 that the current message becomes the last displayed one, make
6716 choose a suitable buffer for echo_area_buffer[0], and clear it.
6718 Value is what FN returns. */
6720 static int
6721 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
6722 struct window *w;
6723 int which;
6724 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6725 EMACS_INT a1;
6726 Lisp_Object a2;
6727 EMACS_INT a3, a4;
6729 Lisp_Object buffer;
6730 int this_one, the_other, clear_buffer_p, rc;
6731 int count = SPECPDL_INDEX ();
6733 /* If buffers aren't live, make new ones. */
6734 ensure_echo_area_buffers ();
6736 clear_buffer_p = 0;
6738 if (which == 0)
6739 this_one = 0, the_other = 1;
6740 else if (which > 0)
6741 this_one = 1, the_other = 0;
6742 else
6744 this_one = 0, the_other = 1;
6745 clear_buffer_p = 1;
6747 /* We need a fresh one in case the current echo buffer equals
6748 the one containing the last displayed echo area message. */
6749 if (!NILP (echo_area_buffer[this_one])
6750 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6751 echo_area_buffer[this_one] = Qnil;
6754 /* Choose a suitable buffer from echo_buffer[] is we don't
6755 have one. */
6756 if (NILP (echo_area_buffer[this_one]))
6758 echo_area_buffer[this_one]
6759 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6760 ? echo_buffer[the_other]
6761 : echo_buffer[this_one]);
6762 clear_buffer_p = 1;
6765 buffer = echo_area_buffer[this_one];
6767 /* Don't get confused by reusing the buffer used for echoing
6768 for a different purpose. */
6769 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
6770 cancel_echoing ();
6772 record_unwind_protect (unwind_with_echo_area_buffer,
6773 with_echo_area_buffer_unwind_data (w));
6775 /* Make the echo area buffer current. Note that for display
6776 purposes, it is not necessary that the displayed window's buffer
6777 == current_buffer, except for text property lookup. So, let's
6778 only set that buffer temporarily here without doing a full
6779 Fset_window_buffer. We must also change w->pointm, though,
6780 because otherwise an assertions in unshow_buffer fails, and Emacs
6781 aborts. */
6782 set_buffer_internal_1 (XBUFFER (buffer));
6783 if (w)
6785 w->buffer = buffer;
6786 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6789 current_buffer->undo_list = Qt;
6790 current_buffer->read_only = Qnil;
6791 specbind (Qinhibit_read_only, Qt);
6792 specbind (Qinhibit_modification_hooks, Qt);
6794 if (clear_buffer_p && Z > BEG)
6795 del_range (BEG, Z);
6797 xassert (BEGV >= BEG);
6798 xassert (ZV <= Z && ZV >= BEGV);
6800 rc = fn (a1, a2, a3, a4);
6802 xassert (BEGV >= BEG);
6803 xassert (ZV <= Z && ZV >= BEGV);
6805 unbind_to (count, Qnil);
6806 return rc;
6810 /* Save state that should be preserved around the call to the function
6811 FN called in with_echo_area_buffer. */
6813 static Lisp_Object
6814 with_echo_area_buffer_unwind_data (w)
6815 struct window *w;
6817 int i = 0;
6818 Lisp_Object vector;
6820 /* Reduce consing by keeping one vector in
6821 Vwith_echo_area_save_vector. */
6822 vector = Vwith_echo_area_save_vector;
6823 Vwith_echo_area_save_vector = Qnil;
6825 if (NILP (vector))
6826 vector = Fmake_vector (make_number (7), Qnil);
6828 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6829 AREF (vector, i) = Vdeactivate_mark, ++i;
6830 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
6832 if (w)
6834 XSETWINDOW (AREF (vector, i), w); ++i;
6835 AREF (vector, i) = w->buffer; ++i;
6836 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6837 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
6839 else
6841 int end = i + 4;
6842 for (; i < end; ++i)
6843 AREF (vector, i) = Qnil;
6846 xassert (i == ASIZE (vector));
6847 return vector;
6851 /* Restore global state from VECTOR which was created by
6852 with_echo_area_buffer_unwind_data. */
6854 static Lisp_Object
6855 unwind_with_echo_area_buffer (vector)
6856 Lisp_Object vector;
6858 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6859 Vdeactivate_mark = AREF (vector, 1);
6860 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
6862 if (WINDOWP (AREF (vector, 3)))
6864 struct window *w;
6865 Lisp_Object buffer, charpos, bytepos;
6867 w = XWINDOW (AREF (vector, 3));
6868 buffer = AREF (vector, 4);
6869 charpos = AREF (vector, 5);
6870 bytepos = AREF (vector, 6);
6872 w->buffer = buffer;
6873 set_marker_both (w->pointm, buffer,
6874 XFASTINT (charpos), XFASTINT (bytepos));
6877 Vwith_echo_area_save_vector = vector;
6878 return Qnil;
6882 /* Set up the echo area for use by print functions. MULTIBYTE_P
6883 non-zero means we will print multibyte. */
6885 void
6886 setup_echo_area_for_printing (multibyte_p)
6887 int multibyte_p;
6889 /* If we can't find an echo area any more, exit. */
6890 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
6891 Fkill_emacs (Qnil);
6893 ensure_echo_area_buffers ();
6895 if (!message_buf_print)
6897 /* A message has been output since the last time we printed.
6898 Choose a fresh echo area buffer. */
6899 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6900 echo_area_buffer[0] = echo_buffer[1];
6901 else
6902 echo_area_buffer[0] = echo_buffer[0];
6904 /* Switch to that buffer and clear it. */
6905 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6906 current_buffer->truncate_lines = Qnil;
6908 if (Z > BEG)
6910 int count = SPECPDL_INDEX ();
6911 specbind (Qinhibit_read_only, Qt);
6912 /* Note that undo recording is always disabled. */
6913 del_range (BEG, Z);
6914 unbind_to (count, Qnil);
6916 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6918 /* Set up the buffer for the multibyteness we need. */
6919 if (multibyte_p
6920 != !NILP (current_buffer->enable_multibyte_characters))
6921 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6923 /* Raise the frame containing the echo area. */
6924 if (minibuffer_auto_raise)
6926 struct frame *sf = SELECTED_FRAME ();
6927 Lisp_Object mini_window;
6928 mini_window = FRAME_MINIBUF_WINDOW (sf);
6929 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6932 message_log_maybe_newline ();
6933 message_buf_print = 1;
6935 else
6937 if (NILP (echo_area_buffer[0]))
6939 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6940 echo_area_buffer[0] = echo_buffer[1];
6941 else
6942 echo_area_buffer[0] = echo_buffer[0];
6945 if (current_buffer != XBUFFER (echo_area_buffer[0]))
6947 /* Someone switched buffers between print requests. */
6948 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6949 current_buffer->truncate_lines = Qnil;
6955 /* Display an echo area message in window W. Value is non-zero if W's
6956 height is changed. If display_last_displayed_message_p is
6957 non-zero, display the message that was last displayed, otherwise
6958 display the current message. */
6960 static int
6961 display_echo_area (w)
6962 struct window *w;
6964 int i, no_message_p, window_height_changed_p, count;
6966 /* Temporarily disable garbage collections while displaying the echo
6967 area. This is done because a GC can print a message itself.
6968 That message would modify the echo area buffer's contents while a
6969 redisplay of the buffer is going on, and seriously confuse
6970 redisplay. */
6971 count = inhibit_garbage_collection ();
6973 /* If there is no message, we must call display_echo_area_1
6974 nevertheless because it resizes the window. But we will have to
6975 reset the echo_area_buffer in question to nil at the end because
6976 with_echo_area_buffer will sets it to an empty buffer. */
6977 i = display_last_displayed_message_p ? 1 : 0;
6978 no_message_p = NILP (echo_area_buffer[i]);
6980 window_height_changed_p
6981 = with_echo_area_buffer (w, display_last_displayed_message_p,
6982 display_echo_area_1,
6983 (EMACS_INT) w, Qnil, 0, 0);
6985 if (no_message_p)
6986 echo_area_buffer[i] = Qnil;
6988 unbind_to (count, Qnil);
6989 return window_height_changed_p;
6993 /* Helper for display_echo_area. Display the current buffer which
6994 contains the current echo area message in window W, a mini-window,
6995 a pointer to which is passed in A1. A2..A4 are currently not used.
6996 Change the height of W so that all of the message is displayed.
6997 Value is non-zero if height of W was changed. */
6999 static int
7000 display_echo_area_1 (a1, a2, a3, a4)
7001 EMACS_INT a1;
7002 Lisp_Object a2;
7003 EMACS_INT a3, a4;
7005 struct window *w = (struct window *) a1;
7006 Lisp_Object window;
7007 struct text_pos start;
7008 int window_height_changed_p = 0;
7010 /* Do this before displaying, so that we have a large enough glyph
7011 matrix for the display. */
7012 window_height_changed_p = resize_mini_window (w, 0);
7014 /* Display. */
7015 clear_glyph_matrix (w->desired_matrix);
7016 XSETWINDOW (window, w);
7017 SET_TEXT_POS (start, BEG, BEG_BYTE);
7018 try_window (window, start);
7020 return window_height_changed_p;
7024 /* Resize the echo area window to exactly the size needed for the
7025 currently displayed message, if there is one. If a mini-buffer
7026 is active, don't shrink it. */
7028 void
7029 resize_echo_area_exactly ()
7031 if (BUFFERP (echo_area_buffer[0])
7032 && WINDOWP (echo_area_window))
7034 struct window *w = XWINDOW (echo_area_window);
7035 int resized_p;
7036 Lisp_Object resize_exactly;
7038 if (minibuf_level == 0)
7039 resize_exactly = Qt;
7040 else
7041 resize_exactly = Qnil;
7043 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7044 (EMACS_INT) w, resize_exactly, 0, 0);
7045 if (resized_p)
7047 ++windows_or_buffers_changed;
7048 ++update_mode_lines;
7049 redisplay_internal (0);
7055 /* Callback function for with_echo_area_buffer, when used from
7056 resize_echo_area_exactly. A1 contains a pointer to the window to
7057 resize, EXACTLY non-nil means resize the mini-window exactly to the
7058 size of the text displayed. A3 and A4 are not used. Value is what
7059 resize_mini_window returns. */
7061 static int
7062 resize_mini_window_1 (a1, exactly, a3, a4)
7063 EMACS_INT a1;
7064 Lisp_Object exactly;
7065 EMACS_INT a3, a4;
7067 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7071 /* Resize mini-window W to fit the size of its contents. EXACT:P
7072 means size the window exactly to the size needed. Otherwise, it's
7073 only enlarged until W's buffer is empty. Value is non-zero if
7074 the window height has been changed. */
7077 resize_mini_window (w, exact_p)
7078 struct window *w;
7079 int exact_p;
7081 struct frame *f = XFRAME (w->frame);
7082 int window_height_changed_p = 0;
7084 xassert (MINI_WINDOW_P (w));
7086 /* Don't resize windows while redisplaying a window; it would
7087 confuse redisplay functions when the size of the window they are
7088 displaying changes from under them. Such a resizing can happen,
7089 for instance, when which-func prints a long message while
7090 we are running fontification-functions. We're running these
7091 functions with safe_call which binds inhibit-redisplay to t. */
7092 if (!NILP (Vinhibit_redisplay))
7093 return 0;
7095 /* Nil means don't try to resize. */
7096 if (NILP (Vresize_mini_windows)
7097 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7098 return 0;
7100 if (!FRAME_MINIBUF_ONLY_P (f))
7102 struct it it;
7103 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7104 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
7105 int height, max_height;
7106 int unit = CANON_Y_UNIT (f);
7107 struct text_pos start;
7108 struct buffer *old_current_buffer = NULL;
7110 if (current_buffer != XBUFFER (w->buffer))
7112 old_current_buffer = current_buffer;
7113 set_buffer_internal (XBUFFER (w->buffer));
7116 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7118 /* Compute the max. number of lines specified by the user. */
7119 if (FLOATP (Vmax_mini_window_height))
7120 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_HEIGHT (f);
7121 else if (INTEGERP (Vmax_mini_window_height))
7122 max_height = XINT (Vmax_mini_window_height);
7123 else
7124 max_height = total_height / 4;
7126 /* Correct that max. height if it's bogus. */
7127 max_height = max (1, max_height);
7128 max_height = min (total_height, max_height);
7130 /* Find out the height of the text in the window. */
7131 if (it.truncate_lines_p)
7132 height = 1;
7133 else
7135 last_height = 0;
7136 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7137 if (it.max_ascent == 0 && it.max_descent == 0)
7138 height = it.current_y + last_height;
7139 else
7140 height = it.current_y + it.max_ascent + it.max_descent;
7141 height -= it.extra_line_spacing;
7142 height = (height + unit - 1) / unit;
7145 /* Compute a suitable window start. */
7146 if (height > max_height)
7148 height = max_height;
7149 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7150 move_it_vertically_backward (&it, (height - 1) * unit);
7151 start = it.current.pos;
7153 else
7154 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7155 SET_MARKER_FROM_TEXT_POS (w->start, start);
7157 if (EQ (Vresize_mini_windows, Qgrow_only))
7159 /* Let it grow only, until we display an empty message, in which
7160 case the window shrinks again. */
7161 if (height > XFASTINT (w->height))
7163 int old_height = XFASTINT (w->height);
7164 freeze_window_starts (f, 1);
7165 grow_mini_window (w, height - XFASTINT (w->height));
7166 window_height_changed_p = XFASTINT (w->height) != old_height;
7168 else if (height < XFASTINT (w->height)
7169 && (exact_p || BEGV == ZV))
7171 int old_height = XFASTINT (w->height);
7172 freeze_window_starts (f, 0);
7173 shrink_mini_window (w);
7174 window_height_changed_p = XFASTINT (w->height) != old_height;
7177 else
7179 /* Always resize to exact size needed. */
7180 if (height > XFASTINT (w->height))
7182 int old_height = XFASTINT (w->height);
7183 freeze_window_starts (f, 1);
7184 grow_mini_window (w, height - XFASTINT (w->height));
7185 window_height_changed_p = XFASTINT (w->height) != old_height;
7187 else if (height < XFASTINT (w->height))
7189 int old_height = XFASTINT (w->height);
7190 freeze_window_starts (f, 0);
7191 shrink_mini_window (w);
7193 if (height)
7195 freeze_window_starts (f, 1);
7196 grow_mini_window (w, height - XFASTINT (w->height));
7199 window_height_changed_p = XFASTINT (w->height) != old_height;
7203 if (old_current_buffer)
7204 set_buffer_internal (old_current_buffer);
7207 return window_height_changed_p;
7211 /* Value is the current message, a string, or nil if there is no
7212 current message. */
7214 Lisp_Object
7215 current_message ()
7217 Lisp_Object msg;
7219 if (NILP (echo_area_buffer[0]))
7220 msg = Qnil;
7221 else
7223 with_echo_area_buffer (0, 0, current_message_1,
7224 (EMACS_INT) &msg, Qnil, 0, 0);
7225 if (NILP (msg))
7226 echo_area_buffer[0] = Qnil;
7229 return msg;
7233 static int
7234 current_message_1 (a1, a2, a3, a4)
7235 EMACS_INT a1;
7236 Lisp_Object a2;
7237 EMACS_INT a3, a4;
7239 Lisp_Object *msg = (Lisp_Object *) a1;
7241 if (Z > BEG)
7242 *msg = make_buffer_string (BEG, Z, 1);
7243 else
7244 *msg = Qnil;
7245 return 0;
7249 /* Push the current message on Vmessage_stack for later restauration
7250 by restore_message. Value is non-zero if the current message isn't
7251 empty. This is a relatively infrequent operation, so it's not
7252 worth optimizing. */
7255 push_message ()
7257 Lisp_Object msg;
7258 msg = current_message ();
7259 Vmessage_stack = Fcons (msg, Vmessage_stack);
7260 return STRINGP (msg);
7264 /* Restore message display from the top of Vmessage_stack. */
7266 void
7267 restore_message ()
7269 Lisp_Object msg;
7271 xassert (CONSP (Vmessage_stack));
7272 msg = XCAR (Vmessage_stack);
7273 if (STRINGP (msg))
7274 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7275 else
7276 message3_nolog (msg, 0, 0);
7280 /* Handler for record_unwind_protect calling pop_message. */
7282 Lisp_Object
7283 pop_message_unwind (dummy)
7284 Lisp_Object dummy;
7286 pop_message ();
7287 return Qnil;
7290 /* Pop the top-most entry off Vmessage_stack. */
7292 void
7293 pop_message ()
7295 xassert (CONSP (Vmessage_stack));
7296 Vmessage_stack = XCDR (Vmessage_stack);
7300 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7301 exits. If the stack is not empty, we have a missing pop_message
7302 somewhere. */
7304 void
7305 check_message_stack ()
7307 if (!NILP (Vmessage_stack))
7308 abort ();
7312 /* Truncate to NCHARS what will be displayed in the echo area the next
7313 time we display it---but don't redisplay it now. */
7315 void
7316 truncate_echo_area (nchars)
7317 int nchars;
7319 if (nchars == 0)
7320 echo_area_buffer[0] = Qnil;
7321 /* A null message buffer means that the frame hasn't really been
7322 initialized yet. Error messages get reported properly by
7323 cmd_error, so this must be just an informative message; toss it. */
7324 else if (!noninteractive
7325 && INTERACTIVE
7326 && !NILP (echo_area_buffer[0]))
7328 struct frame *sf = SELECTED_FRAME ();
7329 if (FRAME_MESSAGE_BUF (sf))
7330 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7335 /* Helper function for truncate_echo_area. Truncate the current
7336 message to at most NCHARS characters. */
7338 static int
7339 truncate_message_1 (nchars, a2, a3, a4)
7340 EMACS_INT nchars;
7341 Lisp_Object a2;
7342 EMACS_INT a3, a4;
7344 if (BEG + nchars < Z)
7345 del_range (BEG + nchars, Z);
7346 if (Z == BEG)
7347 echo_area_buffer[0] = Qnil;
7348 return 0;
7352 /* Set the current message to a substring of S or STRING.
7354 If STRING is a Lisp string, set the message to the first NBYTES
7355 bytes from STRING. NBYTES zero means use the whole string. If
7356 STRING is multibyte, the message will be displayed multibyte.
7358 If S is not null, set the message to the first LEN bytes of S. LEN
7359 zero means use the whole string. MULTIBYTE_P non-zero means S is
7360 multibyte. Display the message multibyte in that case. */
7362 void
7363 set_message (s, string, nbytes, multibyte_p)
7364 const char *s;
7365 Lisp_Object string;
7366 int nbytes, multibyte_p;
7368 message_enable_multibyte
7369 = ((s && multibyte_p)
7370 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7372 with_echo_area_buffer (0, -1, set_message_1,
7373 (EMACS_INT) s, string, nbytes, multibyte_p);
7374 message_buf_print = 0;
7375 help_echo_showing_p = 0;
7379 /* Helper function for set_message. Arguments have the same meaning
7380 as there, with A1 corresponding to S and A2 corresponding to STRING
7381 This function is called with the echo area buffer being
7382 current. */
7384 static int
7385 set_message_1 (a1, a2, nbytes, multibyte_p)
7386 EMACS_INT a1;
7387 Lisp_Object a2;
7388 EMACS_INT nbytes, multibyte_p;
7390 const char *s = (const char *) a1;
7391 Lisp_Object string = a2;
7393 xassert (BEG == Z);
7395 /* Change multibyteness of the echo buffer appropriately. */
7396 if (message_enable_multibyte
7397 != !NILP (current_buffer->enable_multibyte_characters))
7398 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7400 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7402 /* Insert new message at BEG. */
7403 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7405 if (STRINGP (string))
7407 int nchars;
7409 if (nbytes == 0)
7410 nbytes = SBYTES (string);
7411 nchars = string_byte_to_char (string, nbytes);
7413 /* This function takes care of single/multibyte conversion. We
7414 just have to ensure that the echo area buffer has the right
7415 setting of enable_multibyte_characters. */
7416 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7418 else if (s)
7420 if (nbytes == 0)
7421 nbytes = strlen (s);
7423 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7425 /* Convert from multi-byte to single-byte. */
7426 int i, c, n;
7427 unsigned char work[1];
7429 /* Convert a multibyte string to single-byte. */
7430 for (i = 0; i < nbytes; i += n)
7432 c = string_char_and_length (s + i, nbytes - i, &n);
7433 work[0] = (SINGLE_BYTE_CHAR_P (c)
7435 : multibyte_char_to_unibyte (c, Qnil));
7436 insert_1_both (work, 1, 1, 1, 0, 0);
7439 else if (!multibyte_p
7440 && !NILP (current_buffer->enable_multibyte_characters))
7442 /* Convert from single-byte to multi-byte. */
7443 int i, c, n;
7444 const unsigned char *msg = (const unsigned char *) s;
7445 unsigned char str[MAX_MULTIBYTE_LENGTH];
7447 /* Convert a single-byte string to multibyte. */
7448 for (i = 0; i < nbytes; i++)
7450 c = unibyte_char_to_multibyte (msg[i]);
7451 n = CHAR_STRING (c, str);
7452 insert_1_both (str, 1, n, 1, 0, 0);
7455 else
7456 insert_1 (s, nbytes, 1, 0, 0);
7459 return 0;
7463 /* Clear messages. CURRENT_P non-zero means clear the current
7464 message. LAST_DISPLAYED_P non-zero means clear the message
7465 last displayed. */
7467 void
7468 clear_message (current_p, last_displayed_p)
7469 int current_p, last_displayed_p;
7471 if (current_p)
7473 echo_area_buffer[0] = Qnil;
7474 message_cleared_p = 1;
7477 if (last_displayed_p)
7478 echo_area_buffer[1] = Qnil;
7480 message_buf_print = 0;
7483 /* Clear garbaged frames.
7485 This function is used where the old redisplay called
7486 redraw_garbaged_frames which in turn called redraw_frame which in
7487 turn called clear_frame. The call to clear_frame was a source of
7488 flickering. I believe a clear_frame is not necessary. It should
7489 suffice in the new redisplay to invalidate all current matrices,
7490 and ensure a complete redisplay of all windows. */
7492 static void
7493 clear_garbaged_frames ()
7495 if (frame_garbaged)
7497 Lisp_Object tail, frame;
7498 int changed_count = 0;
7500 FOR_EACH_FRAME (tail, frame)
7502 struct frame *f = XFRAME (frame);
7504 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7506 if (f->resized_p)
7507 Fredraw_frame (frame);
7508 clear_current_matrices (f);
7509 changed_count++;
7510 f->garbaged = 0;
7511 f->resized_p = 0;
7515 frame_garbaged = 0;
7516 if (changed_count)
7517 ++windows_or_buffers_changed;
7522 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7523 is non-zero update selected_frame. Value is non-zero if the
7524 mini-windows height has been changed. */
7526 static int
7527 echo_area_display (update_frame_p)
7528 int update_frame_p;
7530 Lisp_Object mini_window;
7531 struct window *w;
7532 struct frame *f;
7533 int window_height_changed_p = 0;
7534 struct frame *sf = SELECTED_FRAME ();
7536 mini_window = FRAME_MINIBUF_WINDOW (sf);
7537 w = XWINDOW (mini_window);
7538 f = XFRAME (WINDOW_FRAME (w));
7540 /* Don't display if frame is invisible or not yet initialized. */
7541 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
7542 return 0;
7544 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
7545 #ifndef MAC_OS8
7546 #ifdef HAVE_WINDOW_SYSTEM
7547 /* When Emacs starts, selected_frame may be a visible terminal
7548 frame, even if we run under a window system. If we let this
7549 through, a message would be displayed on the terminal. */
7550 if (EQ (selected_frame, Vterminal_frame)
7551 && !NILP (Vwindow_system))
7552 return 0;
7553 #endif /* HAVE_WINDOW_SYSTEM */
7554 #endif
7556 /* Redraw garbaged frames. */
7557 if (frame_garbaged)
7558 clear_garbaged_frames ();
7560 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
7562 echo_area_window = mini_window;
7563 window_height_changed_p = display_echo_area (w);
7564 w->must_be_updated_p = 1;
7566 /* Update the display, unless called from redisplay_internal.
7567 Also don't update the screen during redisplay itself. The
7568 update will happen at the end of redisplay, and an update
7569 here could cause confusion. */
7570 if (update_frame_p && !redisplaying_p)
7572 int n = 0;
7574 /* If the display update has been interrupted by pending
7575 input, update mode lines in the frame. Due to the
7576 pending input, it might have been that redisplay hasn't
7577 been called, so that mode lines above the echo area are
7578 garbaged. This looks odd, so we prevent it here. */
7579 if (!display_completed)
7580 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
7582 if (window_height_changed_p
7583 /* Don't do this if Emacs is shutting down. Redisplay
7584 needs to run hooks. */
7585 && !NILP (Vrun_hooks))
7587 /* Must update other windows. Likewise as in other
7588 cases, don't let this update be interrupted by
7589 pending input. */
7590 int count = SPECPDL_INDEX ();
7591 specbind (Qredisplay_dont_pause, Qt);
7592 windows_or_buffers_changed = 1;
7593 redisplay_internal (0);
7594 unbind_to (count, Qnil);
7596 else if (FRAME_WINDOW_P (f) && n == 0)
7598 /* Window configuration is the same as before.
7599 Can do with a display update of the echo area,
7600 unless we displayed some mode lines. */
7601 update_single_window (w, 1);
7602 rif->flush_display (f);
7604 else
7605 update_frame (f, 1, 1);
7607 /* If cursor is in the echo area, make sure that the next
7608 redisplay displays the minibuffer, so that the cursor will
7609 be replaced with what the minibuffer wants. */
7610 if (cursor_in_echo_area)
7611 ++windows_or_buffers_changed;
7614 else if (!EQ (mini_window, selected_window))
7615 windows_or_buffers_changed++;
7617 /* Last displayed message is now the current message. */
7618 echo_area_buffer[1] = echo_area_buffer[0];
7620 /* Prevent redisplay optimization in redisplay_internal by resetting
7621 this_line_start_pos. This is done because the mini-buffer now
7622 displays the message instead of its buffer text. */
7623 if (EQ (mini_window, selected_window))
7624 CHARPOS (this_line_start_pos) = 0;
7626 return window_height_changed_p;
7631 /***********************************************************************
7632 Frame Titles
7633 ***********************************************************************/
7636 /* The frame title buffering code is also used by Fformat_mode_line.
7637 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
7639 /* A buffer for constructing frame titles in it; allocated from the
7640 heap in init_xdisp and resized as needed in store_frame_title_char. */
7642 static char *frame_title_buf;
7644 /* The buffer's end, and a current output position in it. */
7646 static char *frame_title_buf_end;
7647 static char *frame_title_ptr;
7650 /* Store a single character C for the frame title in frame_title_buf.
7651 Re-allocate frame_title_buf if necessary. */
7653 static void
7654 #ifdef PROTOTYPES
7655 store_frame_title_char (char c)
7656 #else
7657 store_frame_title_char (c)
7658 char c;
7659 #endif
7661 /* If output position has reached the end of the allocated buffer,
7662 double the buffer's size. */
7663 if (frame_title_ptr == frame_title_buf_end)
7665 int len = frame_title_ptr - frame_title_buf;
7666 int new_size = 2 * len * sizeof *frame_title_buf;
7667 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
7668 frame_title_buf_end = frame_title_buf + new_size;
7669 frame_title_ptr = frame_title_buf + len;
7672 *frame_title_ptr++ = c;
7676 /* Store part of a frame title in frame_title_buf, beginning at
7677 frame_title_ptr. STR is the string to store. Do not copy
7678 characters that yield more columns than PRECISION; PRECISION <= 0
7679 means copy the whole string. Pad with spaces until FIELD_WIDTH
7680 number of characters have been copied; FIELD_WIDTH <= 0 means don't
7681 pad. Called from display_mode_element when it is used to build a
7682 frame title. */
7684 static int
7685 store_frame_title (str, field_width, precision)
7686 const unsigned char *str;
7687 int field_width, precision;
7689 int n = 0;
7690 int dummy, nbytes;
7692 /* Copy at most PRECISION chars from STR. */
7693 nbytes = strlen (str);
7694 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
7695 while (nbytes--)
7696 store_frame_title_char (*str++);
7698 /* Fill up with spaces until FIELD_WIDTH reached. */
7699 while (field_width > 0
7700 && n < field_width)
7702 store_frame_title_char (' ');
7703 ++n;
7706 return n;
7709 #ifdef HAVE_WINDOW_SYSTEM
7711 /* Set the title of FRAME, if it has changed. The title format is
7712 Vicon_title_format if FRAME is iconified, otherwise it is
7713 frame_title_format. */
7715 static void
7716 x_consider_frame_title (frame)
7717 Lisp_Object frame;
7719 struct frame *f = XFRAME (frame);
7721 if (FRAME_WINDOW_P (f)
7722 || FRAME_MINIBUF_ONLY_P (f)
7723 || f->explicit_name)
7725 /* Do we have more than one visible frame on this X display? */
7726 Lisp_Object tail;
7727 Lisp_Object fmt;
7728 struct buffer *obuf;
7729 int len;
7730 struct it it;
7732 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
7734 Lisp_Object other_frame = XCAR (tail);
7735 struct frame *tf = XFRAME (other_frame);
7737 if (tf != f
7738 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7739 && !FRAME_MINIBUF_ONLY_P (tf)
7740 && !EQ (other_frame, tip_frame)
7741 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7742 break;
7745 /* Set global variable indicating that multiple frames exist. */
7746 multiple_frames = CONSP (tail);
7748 /* Switch to the buffer of selected window of the frame. Set up
7749 frame_title_ptr so that display_mode_element will output into it;
7750 then display the title. */
7751 obuf = current_buffer;
7752 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
7753 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7754 frame_title_ptr = frame_title_buf;
7755 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7756 NULL, DEFAULT_FACE_ID);
7757 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
7758 len = frame_title_ptr - frame_title_buf;
7759 frame_title_ptr = NULL;
7760 set_buffer_internal_1 (obuf);
7762 /* Set the title only if it's changed. This avoids consing in
7763 the common case where it hasn't. (If it turns out that we've
7764 already wasted too much time by walking through the list with
7765 display_mode_element, then we might need to optimize at a
7766 higher level than this.) */
7767 if (! STRINGP (f->name)
7768 || SBYTES (f->name) != len
7769 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
7770 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7774 #endif /* not HAVE_WINDOW_SYSTEM */
7779 /***********************************************************************
7780 Menu Bars
7781 ***********************************************************************/
7784 /* Prepare for redisplay by updating menu-bar item lists when
7785 appropriate. This can call eval. */
7787 void
7788 prepare_menu_bars ()
7790 int all_windows;
7791 struct gcpro gcpro1, gcpro2;
7792 struct frame *f;
7793 Lisp_Object tooltip_frame;
7795 #ifdef HAVE_WINDOW_SYSTEM
7796 tooltip_frame = tip_frame;
7797 #else
7798 tooltip_frame = Qnil;
7799 #endif
7801 /* Update all frame titles based on their buffer names, etc. We do
7802 this before the menu bars so that the buffer-menu will show the
7803 up-to-date frame titles. */
7804 #ifdef HAVE_WINDOW_SYSTEM
7805 if (windows_or_buffers_changed || update_mode_lines)
7807 Lisp_Object tail, frame;
7809 FOR_EACH_FRAME (tail, frame)
7811 f = XFRAME (frame);
7812 if (!EQ (frame, tooltip_frame)
7813 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7814 x_consider_frame_title (frame);
7817 #endif /* HAVE_WINDOW_SYSTEM */
7819 /* Update the menu bar item lists, if appropriate. This has to be
7820 done before any actual redisplay or generation of display lines. */
7821 all_windows = (update_mode_lines
7822 || buffer_shared > 1
7823 || windows_or_buffers_changed);
7824 if (all_windows)
7826 Lisp_Object tail, frame;
7827 int count = SPECPDL_INDEX ();
7829 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7831 FOR_EACH_FRAME (tail, frame)
7833 f = XFRAME (frame);
7835 /* Ignore tooltip frame. */
7836 if (EQ (frame, tooltip_frame))
7837 continue;
7839 /* If a window on this frame changed size, report that to
7840 the user and clear the size-change flag. */
7841 if (FRAME_WINDOW_SIZES_CHANGED (f))
7843 Lisp_Object functions;
7845 /* Clear flag first in case we get an error below. */
7846 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7847 functions = Vwindow_size_change_functions;
7848 GCPRO2 (tail, functions);
7850 while (CONSP (functions))
7852 call1 (XCAR (functions), frame);
7853 functions = XCDR (functions);
7855 UNGCPRO;
7858 GCPRO1 (tail);
7859 update_menu_bar (f, 0);
7860 #ifdef HAVE_WINDOW_SYSTEM
7861 update_tool_bar (f, 0);
7862 #endif
7863 UNGCPRO;
7866 unbind_to (count, Qnil);
7868 else
7870 struct frame *sf = SELECTED_FRAME ();
7871 update_menu_bar (sf, 1);
7872 #ifdef HAVE_WINDOW_SYSTEM
7873 update_tool_bar (sf, 1);
7874 #endif
7877 /* Motif needs this. See comment in xmenu.c. Turn it off when
7878 pending_menu_activation is not defined. */
7879 #ifdef USE_X_TOOLKIT
7880 pending_menu_activation = 0;
7881 #endif
7885 /* Update the menu bar item list for frame F. This has to be done
7886 before we start to fill in any display lines, because it can call
7887 eval.
7889 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7891 static void
7892 update_menu_bar (f, save_match_data)
7893 struct frame *f;
7894 int save_match_data;
7896 Lisp_Object window;
7897 register struct window *w;
7899 /* If called recursively during a menu update, do nothing. This can
7900 happen when, for instance, an activate-menubar-hook causes a
7901 redisplay. */
7902 if (inhibit_menubar_update)
7903 return;
7905 window = FRAME_SELECTED_WINDOW (f);
7906 w = XWINDOW (window);
7908 #if 0 /* The if statement below this if statement used to include the
7909 condition !NILP (w->update_mode_line), rather than using
7910 update_mode_lines directly, and this if statement may have
7911 been added to make that condition work. Now the if
7912 statement below matches its comment, this isn't needed. */
7913 if (update_mode_lines)
7914 w->update_mode_line = Qt;
7915 #endif
7917 if (FRAME_WINDOW_P (f)
7919 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7920 || defined (USE_GTK)
7921 FRAME_EXTERNAL_MENU_BAR (f)
7922 #else
7923 FRAME_MENU_BAR_LINES (f) > 0
7924 #endif
7925 : FRAME_MENU_BAR_LINES (f) > 0)
7927 /* If the user has switched buffers or windows, we need to
7928 recompute to reflect the new bindings. But we'll
7929 recompute when update_mode_lines is set too; that means
7930 that people can use force-mode-line-update to request
7931 that the menu bar be recomputed. The adverse effect on
7932 the rest of the redisplay algorithm is about the same as
7933 windows_or_buffers_changed anyway. */
7934 if (windows_or_buffers_changed
7935 /* This used to test w->update_mode_line, but we believe
7936 there is no need to recompute the menu in that case. */
7937 || update_mode_lines
7938 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7939 < BUF_MODIFF (XBUFFER (w->buffer)))
7940 != !NILP (w->last_had_star))
7941 || ((!NILP (Vtransient_mark_mode)
7942 && !NILP (XBUFFER (w->buffer)->mark_active))
7943 != !NILP (w->region_showing)))
7945 struct buffer *prev = current_buffer;
7946 int count = SPECPDL_INDEX ();
7948 specbind (Qinhibit_menubar_update, Qt);
7950 set_buffer_internal_1 (XBUFFER (w->buffer));
7951 if (save_match_data)
7952 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7953 if (NILP (Voverriding_local_map_menu_flag))
7955 specbind (Qoverriding_terminal_local_map, Qnil);
7956 specbind (Qoverriding_local_map, Qnil);
7959 /* Run the Lucid hook. */
7960 safe_run_hooks (Qactivate_menubar_hook);
7962 /* If it has changed current-menubar from previous value,
7963 really recompute the menu-bar from the value. */
7964 if (! NILP (Vlucid_menu_bar_dirty_flag))
7965 call0 (Qrecompute_lucid_menubar);
7967 safe_run_hooks (Qmenu_bar_update_hook);
7968 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7970 /* Redisplay the menu bar in case we changed it. */
7971 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
7972 || defined (USE_GTK)
7973 if (FRAME_WINDOW_P (f)
7974 #if defined (MAC_OS)
7975 /* All frames on Mac OS share the same menubar. So only the
7976 selected frame should be allowed to set it. */
7977 && f == SELECTED_FRAME ()
7978 #endif
7980 set_frame_menubar (f, 0, 0);
7981 else
7982 /* On a terminal screen, the menu bar is an ordinary screen
7983 line, and this makes it get updated. */
7984 w->update_mode_line = Qt;
7985 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7986 /* In the non-toolkit version, the menu bar is an ordinary screen
7987 line, and this makes it get updated. */
7988 w->update_mode_line = Qt;
7989 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
7991 unbind_to (count, Qnil);
7992 set_buffer_internal_1 (prev);
7999 /***********************************************************************
8000 Output Cursor
8001 ***********************************************************************/
8003 #ifdef HAVE_WINDOW_SYSTEM
8005 /* EXPORT:
8006 Nominal cursor position -- where to draw output.
8007 HPOS and VPOS are window relative glyph matrix coordinates.
8008 X and Y are window relative pixel coordinates. */
8010 struct cursor_pos output_cursor;
8013 /* EXPORT:
8014 Set the global variable output_cursor to CURSOR. All cursor
8015 positions are relative to updated_window. */
8017 void
8018 set_output_cursor (cursor)
8019 struct cursor_pos *cursor;
8021 output_cursor.hpos = cursor->hpos;
8022 output_cursor.vpos = cursor->vpos;
8023 output_cursor.x = cursor->x;
8024 output_cursor.y = cursor->y;
8028 /* EXPORT for RIF:
8029 Set a nominal cursor position.
8031 HPOS and VPOS are column/row positions in a window glyph matrix. X
8032 and Y are window text area relative pixel positions.
8034 If this is done during an update, updated_window will contain the
8035 window that is being updated and the position is the future output
8036 cursor position for that window. If updated_window is null, use
8037 selected_window and display the cursor at the given position. */
8039 void
8040 x_cursor_to (vpos, hpos, y, x)
8041 int vpos, hpos, y, x;
8043 struct window *w;
8045 /* If updated_window is not set, work on selected_window. */
8046 if (updated_window)
8047 w = updated_window;
8048 else
8049 w = XWINDOW (selected_window);
8051 /* Set the output cursor. */
8052 output_cursor.hpos = hpos;
8053 output_cursor.vpos = vpos;
8054 output_cursor.x = x;
8055 output_cursor.y = y;
8057 /* If not called as part of an update, really display the cursor.
8058 This will also set the cursor position of W. */
8059 if (updated_window == NULL)
8061 BLOCK_INPUT;
8062 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8063 if (rif->flush_display_optional)
8064 rif->flush_display_optional (SELECTED_FRAME ());
8065 UNBLOCK_INPUT;
8069 #endif /* HAVE_WINDOW_SYSTEM */
8072 /***********************************************************************
8073 Tool-bars
8074 ***********************************************************************/
8076 #ifdef HAVE_WINDOW_SYSTEM
8078 /* Where the mouse was last time we reported a mouse event. */
8080 FRAME_PTR last_mouse_frame;
8082 /* Tool-bar item index of the item on which a mouse button was pressed
8083 or -1. */
8085 int last_tool_bar_item;
8088 /* Update the tool-bar item list for frame F. This has to be done
8089 before we start to fill in any display lines. Called from
8090 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8091 and restore it here. */
8093 static void
8094 update_tool_bar (f, save_match_data)
8095 struct frame *f;
8096 int save_match_data;
8098 #ifdef USE_GTK
8099 int do_update = FRAME_EXTERNAL_TOOL_BAR(f);
8100 #else
8101 int do_update = WINDOWP (f->tool_bar_window)
8102 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0;
8103 #endif
8105 if (do_update)
8107 Lisp_Object window;
8108 struct window *w;
8110 window = FRAME_SELECTED_WINDOW (f);
8111 w = XWINDOW (window);
8113 /* If the user has switched buffers or windows, we need to
8114 recompute to reflect the new bindings. But we'll
8115 recompute when update_mode_lines is set too; that means
8116 that people can use force-mode-line-update to request
8117 that the menu bar be recomputed. The adverse effect on
8118 the rest of the redisplay algorithm is about the same as
8119 windows_or_buffers_changed anyway. */
8120 if (windows_or_buffers_changed
8121 || !NILP (w->update_mode_line)
8122 || update_mode_lines
8123 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8124 < BUF_MODIFF (XBUFFER (w->buffer)))
8125 != !NILP (w->last_had_star))
8126 || ((!NILP (Vtransient_mark_mode)
8127 && !NILP (XBUFFER (w->buffer)->mark_active))
8128 != !NILP (w->region_showing)))
8130 struct buffer *prev = current_buffer;
8131 int count = SPECPDL_INDEX ();
8132 Lisp_Object old_tool_bar;
8133 struct gcpro gcpro1;
8135 /* Set current_buffer to the buffer of the selected
8136 window of the frame, so that we get the right local
8137 keymaps. */
8138 set_buffer_internal_1 (XBUFFER (w->buffer));
8140 /* Save match data, if we must. */
8141 if (save_match_data)
8142 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8144 /* Make sure that we don't accidentally use bogus keymaps. */
8145 if (NILP (Voverriding_local_map_menu_flag))
8147 specbind (Qoverriding_terminal_local_map, Qnil);
8148 specbind (Qoverriding_local_map, Qnil);
8151 old_tool_bar = f->tool_bar_items;
8152 GCPRO1 (old_tool_bar);
8154 /* Build desired tool-bar items from keymaps. */
8155 f->tool_bar_items
8156 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
8158 /* Redisplay the tool-bar if we changed it. */
8159 if (! NILP (Fequal (old_tool_bar, f->tool_bar_items)))
8160 w->update_mode_line = Qt;
8162 unbind_to (count, Qnil);
8163 set_buffer_internal_1 (prev);
8169 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8170 F's desired tool-bar contents. F->tool_bar_items must have
8171 been set up previously by calling prepare_menu_bars. */
8173 static void
8174 build_desired_tool_bar_string (f)
8175 struct frame *f;
8177 int i, size, size_needed;
8178 struct gcpro gcpro1, gcpro2, gcpro3;
8179 Lisp_Object image, plist, props;
8181 image = plist = props = Qnil;
8182 GCPRO3 (image, plist, props);
8184 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8185 Otherwise, make a new string. */
8187 /* The size of the string we might be able to reuse. */
8188 size = (STRINGP (f->desired_tool_bar_string)
8189 ? SCHARS (f->desired_tool_bar_string)
8190 : 0);
8192 /* We need one space in the string for each image. */
8193 size_needed = f->n_tool_bar_items;
8195 /* Reuse f->desired_tool_bar_string, if possible. */
8196 if (size < size_needed || NILP (f->desired_tool_bar_string))
8197 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8198 make_number (' '));
8199 else
8201 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8202 Fremove_text_properties (make_number (0), make_number (size),
8203 props, f->desired_tool_bar_string);
8206 /* Put a `display' property on the string for the images to display,
8207 put a `menu_item' property on tool-bar items with a value that
8208 is the index of the item in F's tool-bar item vector. */
8209 for (i = 0; i < f->n_tool_bar_items; ++i)
8211 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8213 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8214 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8215 int hmargin, vmargin, relief, idx, end;
8216 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
8218 /* If image is a vector, choose the image according to the
8219 button state. */
8220 image = PROP (TOOL_BAR_ITEM_IMAGES);
8221 if (VECTORP (image))
8223 if (enabled_p)
8224 idx = (selected_p
8225 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8226 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8227 else
8228 idx = (selected_p
8229 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8230 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8232 xassert (ASIZE (image) >= idx);
8233 image = AREF (image, idx);
8235 else
8236 idx = -1;
8238 /* Ignore invalid image specifications. */
8239 if (!valid_image_p (image))
8240 continue;
8242 /* Display the tool-bar button pressed, or depressed. */
8243 plist = Fcopy_sequence (XCDR (image));
8245 /* Compute margin and relief to draw. */
8246 relief = (tool_bar_button_relief >= 0
8247 ? tool_bar_button_relief
8248 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8249 hmargin = vmargin = relief;
8251 if (INTEGERP (Vtool_bar_button_margin)
8252 && XINT (Vtool_bar_button_margin) > 0)
8254 hmargin += XFASTINT (Vtool_bar_button_margin);
8255 vmargin += XFASTINT (Vtool_bar_button_margin);
8257 else if (CONSP (Vtool_bar_button_margin))
8259 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8260 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8261 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8263 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8264 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8265 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8268 if (auto_raise_tool_bar_buttons_p)
8270 /* Add a `:relief' property to the image spec if the item is
8271 selected. */
8272 if (selected_p)
8274 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8275 hmargin -= relief;
8276 vmargin -= relief;
8279 else
8281 /* If image is selected, display it pressed, i.e. with a
8282 negative relief. If it's not selected, display it with a
8283 raised relief. */
8284 plist = Fplist_put (plist, QCrelief,
8285 (selected_p
8286 ? make_number (-relief)
8287 : make_number (relief)));
8288 hmargin -= relief;
8289 vmargin -= relief;
8292 /* Put a margin around the image. */
8293 if (hmargin || vmargin)
8295 if (hmargin == vmargin)
8296 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8297 else
8298 plist = Fplist_put (plist, QCmargin,
8299 Fcons (make_number (hmargin),
8300 make_number (vmargin)));
8303 /* If button is not enabled, and we don't have special images
8304 for the disabled state, make the image appear disabled by
8305 applying an appropriate algorithm to it. */
8306 if (!enabled_p && idx < 0)
8307 plist = Fplist_put (plist, QCconversion, Qdisabled);
8309 /* Put a `display' text property on the string for the image to
8310 display. Put a `menu-item' property on the string that gives
8311 the start of this item's properties in the tool-bar items
8312 vector. */
8313 image = Fcons (Qimage, plist);
8314 props = list4 (Qdisplay, image,
8315 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8317 /* Let the last image hide all remaining spaces in the tool bar
8318 string. The string can be longer than needed when we reuse a
8319 previous string. */
8320 if (i + 1 == f->n_tool_bar_items)
8321 end = SCHARS (f->desired_tool_bar_string);
8322 else
8323 end = i + 1;
8324 Fadd_text_properties (make_number (i), make_number (end),
8325 props, f->desired_tool_bar_string);
8326 #undef PROP
8329 UNGCPRO;
8333 /* Display one line of the tool-bar of frame IT->f. */
8335 static void
8336 display_tool_bar_line (it)
8337 struct it *it;
8339 struct glyph_row *row = it->glyph_row;
8340 int max_x = it->last_visible_x;
8341 struct glyph *last;
8343 prepare_desired_row (row);
8344 row->y = it->current_y;
8346 /* Note that this isn't made use of if the face hasn't a box,
8347 so there's no need to check the face here. */
8348 it->start_of_box_run_p = 1;
8350 while (it->current_x < max_x)
8352 int x_before, x, n_glyphs_before, i, nglyphs;
8354 /* Get the next display element. */
8355 if (!get_next_display_element (it))
8356 break;
8358 /* Produce glyphs. */
8359 x_before = it->current_x;
8360 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8361 PRODUCE_GLYPHS (it);
8363 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8364 i = 0;
8365 x = x_before;
8366 while (i < nglyphs)
8368 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8370 if (x + glyph->pixel_width > max_x)
8372 /* Glyph doesn't fit on line. */
8373 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8374 it->current_x = x;
8375 goto out;
8378 ++it->hpos;
8379 x += glyph->pixel_width;
8380 ++i;
8383 /* Stop at line ends. */
8384 if (ITERATOR_AT_END_OF_LINE_P (it))
8385 break;
8387 set_iterator_to_next (it, 1);
8390 out:;
8392 row->displays_text_p = row->used[TEXT_AREA] != 0;
8393 extend_face_to_end_of_line (it);
8394 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8395 last->right_box_line_p = 1;
8396 if (last == row->glyphs[TEXT_AREA])
8397 last->left_box_line_p = 1;
8398 compute_line_metrics (it);
8400 /* If line is empty, make it occupy the rest of the tool-bar. */
8401 if (!row->displays_text_p)
8403 row->height = row->phys_height = it->last_visible_y - row->y;
8404 row->ascent = row->phys_ascent = 0;
8407 row->full_width_p = 1;
8408 row->continued_p = 0;
8409 row->truncated_on_left_p = 0;
8410 row->truncated_on_right_p = 0;
8412 it->current_x = it->hpos = 0;
8413 it->current_y += row->height;
8414 ++it->vpos;
8415 ++it->glyph_row;
8419 /* Value is the number of screen lines needed to make all tool-bar
8420 items of frame F visible. */
8422 static int
8423 tool_bar_lines_needed (f)
8424 struct frame *f;
8426 struct window *w = XWINDOW (f->tool_bar_window);
8427 struct it it;
8429 /* Initialize an iterator for iteration over
8430 F->desired_tool_bar_string in the tool-bar window of frame F. */
8431 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8432 it.first_visible_x = 0;
8433 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8434 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8436 while (!ITERATOR_AT_END_P (&it))
8438 it.glyph_row = w->desired_matrix->rows;
8439 clear_glyph_row (it.glyph_row);
8440 display_tool_bar_line (&it);
8443 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
8447 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8448 0, 1, 0,
8449 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8450 (frame)
8451 Lisp_Object frame;
8453 struct frame *f;
8454 struct window *w;
8455 int nlines = 0;
8457 if (NILP (frame))
8458 frame = selected_frame;
8459 else
8460 CHECK_FRAME (frame);
8461 f = XFRAME (frame);
8463 if (WINDOWP (f->tool_bar_window)
8464 || (w = XWINDOW (f->tool_bar_window),
8465 XFASTINT (w->height) > 0))
8467 update_tool_bar (f, 1);
8468 if (f->n_tool_bar_items)
8470 build_desired_tool_bar_string (f);
8471 nlines = tool_bar_lines_needed (f);
8475 return make_number (nlines);
8479 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8480 height should be changed. */
8482 static int
8483 redisplay_tool_bar (f)
8484 struct frame *f;
8486 struct window *w;
8487 struct it it;
8488 struct glyph_row *row;
8489 int change_height_p = 0;
8491 #ifdef USE_GTK
8492 if (FRAME_EXTERNAL_TOOL_BAR(f))
8493 update_frame_tool_bar (f);
8494 return 0;
8495 #endif
8497 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8498 do anything. This means you must start with tool-bar-lines
8499 non-zero to get the auto-sizing effect. Or in other words, you
8500 can turn off tool-bars by specifying tool-bar-lines zero. */
8501 if (!WINDOWP (f->tool_bar_window)
8502 || (w = XWINDOW (f->tool_bar_window),
8503 XFASTINT (w->height) == 0))
8504 return 0;
8506 /* Set up an iterator for the tool-bar window. */
8507 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8508 it.first_visible_x = 0;
8509 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
8510 row = it.glyph_row;
8512 /* Build a string that represents the contents of the tool-bar. */
8513 build_desired_tool_bar_string (f);
8514 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8516 /* Display as many lines as needed to display all tool-bar items. */
8517 while (it.current_y < it.last_visible_y)
8518 display_tool_bar_line (&it);
8520 /* It doesn't make much sense to try scrolling in the tool-bar
8521 window, so don't do it. */
8522 w->desired_matrix->no_scrolling_p = 1;
8523 w->must_be_updated_p = 1;
8525 if (auto_resize_tool_bars_p)
8527 int nlines;
8529 /* If we couldn't display everything, change the tool-bar's
8530 height. */
8531 if (IT_STRING_CHARPOS (it) < it.end_charpos)
8532 change_height_p = 1;
8534 /* If there are blank lines at the end, except for a partially
8535 visible blank line at the end that is smaller than
8536 CANON_Y_UNIT, change the tool-bar's height. */
8537 row = it.glyph_row - 1;
8538 if (!row->displays_text_p
8539 && row->height >= CANON_Y_UNIT (f))
8540 change_height_p = 1;
8542 /* If row displays tool-bar items, but is partially visible,
8543 change the tool-bar's height. */
8544 if (row->displays_text_p
8545 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
8546 change_height_p = 1;
8548 /* Resize windows as needed by changing the `tool-bar-lines'
8549 frame parameter. */
8550 if (change_height_p
8551 && (nlines = tool_bar_lines_needed (f),
8552 nlines != XFASTINT (w->height)))
8554 extern Lisp_Object Qtool_bar_lines;
8555 Lisp_Object frame;
8556 int old_height = XFASTINT (w->height);
8558 XSETFRAME (frame, f);
8559 clear_glyph_matrix (w->desired_matrix);
8560 Fmodify_frame_parameters (frame,
8561 Fcons (Fcons (Qtool_bar_lines,
8562 make_number (nlines)),
8563 Qnil));
8564 if (XFASTINT (w->height) != old_height)
8565 fonts_changed_p = 1;
8569 return change_height_p;
8573 /* Get information about the tool-bar item which is displayed in GLYPH
8574 on frame F. Return in *PROP_IDX the index where tool-bar item
8575 properties start in F->tool_bar_items. Value is zero if
8576 GLYPH doesn't display a tool-bar item. */
8578 static int
8579 tool_bar_item_info (f, glyph, prop_idx)
8580 struct frame *f;
8581 struct glyph *glyph;
8582 int *prop_idx;
8584 Lisp_Object prop;
8585 int success_p;
8586 int charpos;
8588 /* This function can be called asynchronously, which means we must
8589 exclude any possibility that Fget_text_property signals an
8590 error. */
8591 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
8592 charpos = max (0, charpos);
8594 /* Get the text property `menu-item' at pos. The value of that
8595 property is the start index of this item's properties in
8596 F->tool_bar_items. */
8597 prop = Fget_text_property (make_number (charpos),
8598 Qmenu_item, f->current_tool_bar_string);
8599 if (INTEGERP (prop))
8601 *prop_idx = XINT (prop);
8602 success_p = 1;
8604 else
8605 success_p = 0;
8607 return success_p;
8611 /* Get information about the tool-bar item at position X/Y on frame F.
8612 Return in *GLYPH a pointer to the glyph of the tool-bar item in
8613 the current matrix of the tool-bar window of F, or NULL if not
8614 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
8615 item in F->tool_bar_items. Value is
8617 -1 if X/Y is not on a tool-bar item
8618 0 if X/Y is on the same item that was highlighted before.
8619 1 otherwise. */
8621 static int
8622 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
8623 struct frame *f;
8624 int x, y;
8625 struct glyph **glyph;
8626 int *hpos, *vpos, *prop_idx;
8628 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8629 struct window *w = XWINDOW (f->tool_bar_window);
8630 int area;
8632 /* Find the glyph under X/Y. */
8633 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
8634 if (*glyph == NULL)
8635 return -1;
8637 /* Get the start of this tool-bar item's properties in
8638 f->tool_bar_items. */
8639 if (!tool_bar_item_info (f, *glyph, prop_idx))
8640 return -1;
8642 /* Is mouse on the highlighted item? */
8643 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
8644 && *vpos >= dpyinfo->mouse_face_beg_row
8645 && *vpos <= dpyinfo->mouse_face_end_row
8646 && (*vpos > dpyinfo->mouse_face_beg_row
8647 || *hpos >= dpyinfo->mouse_face_beg_col)
8648 && (*vpos < dpyinfo->mouse_face_end_row
8649 || *hpos < dpyinfo->mouse_face_end_col
8650 || dpyinfo->mouse_face_past_end))
8651 return 0;
8653 return 1;
8657 /* EXPORT:
8658 Handle mouse button event on the tool-bar of frame F, at
8659 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
8660 0 for button release. MODIFIERS is event modifiers for button
8661 release. */
8663 void
8664 handle_tool_bar_click (f, x, y, down_p, modifiers)
8665 struct frame *f;
8666 int x, y, down_p;
8667 unsigned int modifiers;
8669 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8670 struct window *w = XWINDOW (f->tool_bar_window);
8671 int hpos, vpos, prop_idx;
8672 struct glyph *glyph;
8673 Lisp_Object enabled_p;
8675 /* If not on the highlighted tool-bar item, return. */
8676 frame_to_window_pixel_xy (w, &x, &y);
8677 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
8678 return;
8680 /* If item is disabled, do nothing. */
8681 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8682 if (NILP (enabled_p))
8683 return;
8685 if (down_p)
8687 /* Show item in pressed state. */
8688 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
8689 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
8690 last_tool_bar_item = prop_idx;
8692 else
8694 Lisp_Object key, frame;
8695 struct input_event event;
8697 /* Show item in released state. */
8698 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
8699 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
8701 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
8703 XSETFRAME (frame, f);
8704 event.kind = TOOL_BAR_EVENT;
8705 event.frame_or_window = frame;
8706 event.arg = frame;
8707 kbd_buffer_store_event (&event);
8709 event.kind = TOOL_BAR_EVENT;
8710 event.frame_or_window = frame;
8711 event.arg = key;
8712 event.modifiers = modifiers;
8713 kbd_buffer_store_event (&event);
8714 last_tool_bar_item = -1;
8719 /* Possibly highlight a tool-bar item on frame F when mouse moves to
8720 tool-bar window-relative coordinates X/Y. Called from
8721 note_mouse_highlight. */
8723 static void
8724 note_tool_bar_highlight (f, x, y)
8725 struct frame *f;
8726 int x, y;
8728 Lisp_Object window = f->tool_bar_window;
8729 struct window *w = XWINDOW (window);
8730 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8731 int hpos, vpos;
8732 struct glyph *glyph;
8733 struct glyph_row *row;
8734 int i;
8735 Lisp_Object enabled_p;
8736 int prop_idx;
8737 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
8738 int mouse_down_p, rc;
8740 /* Function note_mouse_highlight is called with negative x(y
8741 values when mouse moves outside of the frame. */
8742 if (x <= 0 || y <= 0)
8744 clear_mouse_face (dpyinfo);
8745 return;
8748 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
8749 if (rc < 0)
8751 /* Not on tool-bar item. */
8752 clear_mouse_face (dpyinfo);
8753 return;
8755 else if (rc == 0)
8756 /* On same tool-bar item as before. */
8757 goto set_help_echo;
8759 clear_mouse_face (dpyinfo);
8761 /* Mouse is down, but on different tool-bar item? */
8762 mouse_down_p = (dpyinfo->grabbed
8763 && f == last_mouse_frame
8764 && FRAME_LIVE_P (f));
8765 if (mouse_down_p
8766 && last_tool_bar_item != prop_idx)
8767 return;
8769 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
8770 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
8772 /* If tool-bar item is not enabled, don't highlight it. */
8773 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
8774 if (!NILP (enabled_p))
8776 /* Compute the x-position of the glyph. In front and past the
8777 image is a space. We include this is the highlighted area. */
8778 row = MATRIX_ROW (w->current_matrix, vpos);
8779 for (i = x = 0; i < hpos; ++i)
8780 x += row->glyphs[TEXT_AREA][i].pixel_width;
8782 /* Record this as the current active region. */
8783 dpyinfo->mouse_face_beg_col = hpos;
8784 dpyinfo->mouse_face_beg_row = vpos;
8785 dpyinfo->mouse_face_beg_x = x;
8786 dpyinfo->mouse_face_beg_y = row->y;
8787 dpyinfo->mouse_face_past_end = 0;
8789 dpyinfo->mouse_face_end_col = hpos + 1;
8790 dpyinfo->mouse_face_end_row = vpos;
8791 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
8792 dpyinfo->mouse_face_end_y = row->y;
8793 dpyinfo->mouse_face_window = window;
8794 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
8796 /* Display it as active. */
8797 show_mouse_face (dpyinfo, draw);
8798 dpyinfo->mouse_face_image_state = draw;
8801 set_help_echo:
8803 /* Set help_echo_string to a help string to display for this tool-bar item.
8804 XTread_socket does the rest. */
8805 help_echo_object = help_echo_window = Qnil;
8806 help_echo_pos = -1;
8807 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
8808 if (NILP (help_echo_string))
8809 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
8812 #endif /* HAVE_WINDOW_SYSTEM */
8816 /***********************************************************************
8817 Fringes
8818 ***********************************************************************/
8820 #ifdef HAVE_WINDOW_SYSTEM
8822 /* An arrow like this: `<-'. */
8823 static unsigned char left_bits[] = {
8824 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
8826 /* Right truncation arrow bitmap `->'. */
8827 static unsigned char right_bits[] = {
8828 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
8830 /* Marker for continued lines. */
8831 static unsigned char continued_bits[] = {
8832 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
8834 /* Marker for continuation lines. */
8835 static unsigned char continuation_bits[] = {
8836 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
8838 /* Overlay arrow bitmap. A triangular arrow. */
8839 static unsigned char ov_bits[] = {
8840 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
8842 /* Bitmap drawn to indicate lines not displaying text if
8843 `indicate-empty-lines' is non-nil. */
8844 static unsigned char zv_bits[] = {
8845 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8846 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8847 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8848 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8849 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8850 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8851 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
8852 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
8854 struct fringe_bitmap fringe_bitmaps[MAX_FRINGE_BITMAPS] =
8856 { 0, 0, 0, NULL /* NO_FRINGE_BITMAP */ },
8857 { 8, sizeof (left_bits), 0, left_bits },
8858 { 8, sizeof (right_bits), 0, right_bits },
8859 { 8, sizeof (continued_bits), 0, continued_bits },
8860 { 8, sizeof (continuation_bits), 0, continuation_bits },
8861 { 8, sizeof (ov_bits), 0, ov_bits },
8862 { 8, sizeof (zv_bits), 3, zv_bits }
8866 /* Draw the bitmap WHICH in one of the left or right fringes of
8867 window W. ROW is the glyph row for which to display the bitmap; it
8868 determines the vertical position at which the bitmap has to be
8869 drawn. */
8871 static void
8872 draw_fringe_bitmap (w, row, which, left_p)
8873 struct window *w;
8874 struct glyph_row *row;
8875 enum fringe_bitmap_type which;
8876 int left_p;
8878 struct frame *f = XFRAME (WINDOW_FRAME (w));
8879 struct draw_fringe_bitmap_params p;
8881 /* Convert row to frame coordinates. */
8882 p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
8884 p.which = which;
8885 p.wd = fringe_bitmaps[which].width;
8887 p.h = fringe_bitmaps[which].height;
8888 p.dh = (fringe_bitmaps[which].period
8889 ? (p.y % fringe_bitmaps[which].period)
8890 : 0);
8891 p.h -= p.dh;
8892 /* Clip bitmap if too high. */
8893 if (p.h > row->height)
8894 p.h = row->height;
8896 p.face = FACE_FROM_ID (f, FRINGE_FACE_ID);
8897 PREPARE_FACE_FOR_DISPLAY (f, p.face);
8899 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
8900 the fringe. */
8901 p.bx = -1;
8902 if (left_p)
8904 if (p.wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
8905 p.wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
8906 p.x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
8907 - p.wd
8908 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - p.wd) / 2);
8909 if (p.wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > p.h)
8911 /* If W has a vertical border to its left, don't draw over it. */
8912 int border = ((XFASTINT (w->left) > 0
8913 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8914 ? 1 : 0);
8915 p.bx = (window_box_left (w, -1)
8916 - FRAME_X_LEFT_FRINGE_WIDTH (f)
8917 + border);
8918 p.nx = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
8921 else
8923 if (p.wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
8924 p.wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8925 p.x = (window_box_right (w, -1)
8926 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - p.wd) / 2);
8927 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
8928 the fringe. */
8929 if (p.wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > p.h)
8931 p.bx = window_box_right (w, -1);
8932 p.nx = FRAME_X_RIGHT_FRINGE_WIDTH (f);
8936 if (p.bx >= 0)
8938 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
8940 p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
8941 p.ny = row->visible_height;
8944 /* Adjust y to the offset in the row to start drawing the bitmap. */
8945 p.y += (row->height - p.h) / 2;
8947 rif->draw_fringe_bitmap (w, row, &p);
8950 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
8951 function with input blocked. */
8953 void
8954 draw_row_fringe_bitmaps (w, row)
8955 struct window *w;
8956 struct glyph_row *row;
8958 struct frame *f = XFRAME (w->frame);
8959 enum fringe_bitmap_type bitmap;
8961 xassert (interrupt_input_blocked);
8963 /* If row is completely invisible, because of vscrolling, we
8964 don't have to draw anything. */
8965 if (row->visible_height <= 0)
8966 return;
8968 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
8970 /* Decide which bitmap to draw in the left fringe. */
8971 if (row->overlay_arrow_p)
8972 bitmap = OVERLAY_ARROW_BITMAP;
8973 else if (row->truncated_on_left_p)
8974 bitmap = LEFT_TRUNCATION_BITMAP;
8975 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
8976 bitmap = CONTINUATION_LINE_BITMAP;
8977 else if (row->indicate_empty_line_p)
8978 bitmap = ZV_LINE_BITMAP;
8979 else
8980 bitmap = NO_FRINGE_BITMAP;
8982 draw_fringe_bitmap (w, row, bitmap, 1);
8985 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
8987 /* Decide which bitmap to draw in the right fringe. */
8988 if (row->truncated_on_right_p)
8989 bitmap = RIGHT_TRUNCATION_BITMAP;
8990 else if (row->continued_p)
8991 bitmap = CONTINUED_LINE_BITMAP;
8992 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
8993 bitmap = ZV_LINE_BITMAP;
8994 else
8995 bitmap = NO_FRINGE_BITMAP;
8997 draw_fringe_bitmap (w, row, bitmap, 0);
9002 /* Compute actual fringe widths */
9004 void
9005 compute_fringe_widths (f, redraw)
9006 struct frame *f;
9007 int redraw;
9009 int o_left = FRAME_X_LEFT_FRINGE_WIDTH (f);
9010 int o_right = FRAME_X_RIGHT_FRINGE_WIDTH (f);
9011 int o_cols = FRAME_X_FRINGE_COLS (f);
9013 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
9014 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
9015 int left_fringe_width, right_fringe_width;
9017 if (!NILP (left_fringe))
9018 left_fringe = Fcdr (left_fringe);
9019 if (!NILP (right_fringe))
9020 right_fringe = Fcdr (right_fringe);
9022 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
9023 XINT (left_fringe));
9024 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
9025 XINT (right_fringe));
9027 if (left_fringe_width || right_fringe_width)
9029 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
9030 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
9031 int conf_wid = left_wid + right_wid;
9032 int font_wid = FONT_WIDTH (FRAME_FONT (f));
9033 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
9034 int real_wid = cols * font_wid;
9035 if (left_wid && right_wid)
9037 if (left_fringe_width < 0)
9039 /* Left fringe width is fixed, adjust right fringe if necessary */
9040 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid;
9041 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid - left_wid;
9043 else if (right_fringe_width < 0)
9045 /* Right fringe width is fixed, adjust left fringe if necessary */
9046 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid - right_wid;
9047 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid;
9049 else
9051 /* Adjust both fringes with an equal amount.
9052 Note that we are doing integer arithmetic here, so don't
9053 lose a pixel if the total width is an odd number. */
9054 int fill = real_wid - conf_wid;
9055 FRAME_X_LEFT_FRINGE_WIDTH (f) = left_wid + fill/2;
9056 FRAME_X_RIGHT_FRINGE_WIDTH (f) = right_wid + fill - fill/2;
9059 else if (left_fringe_width)
9061 FRAME_X_LEFT_FRINGE_WIDTH (f) = real_wid;
9062 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
9064 else
9066 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
9067 FRAME_X_RIGHT_FRINGE_WIDTH (f) = real_wid;
9069 FRAME_X_FRINGE_COLS (f) = cols;
9070 FRAME_X_FRINGE_WIDTH (f) = real_wid;
9072 else
9074 FRAME_X_LEFT_FRINGE_WIDTH (f) = 0;
9075 FRAME_X_RIGHT_FRINGE_WIDTH (f) = 0;
9076 FRAME_X_FRINGE_COLS (f) = 0;
9077 FRAME_X_FRINGE_WIDTH (f) = 0;
9080 if (redraw && FRAME_VISIBLE_P (f))
9081 if (o_left != FRAME_X_LEFT_FRINGE_WIDTH (f) ||
9082 o_right != FRAME_X_RIGHT_FRINGE_WIDTH (f) ||
9083 o_cols != FRAME_X_FRINGE_COLS (f))
9084 redraw_frame (f);
9087 #endif /* HAVE_WINDOW_SYSTEM */
9091 /************************************************************************
9092 Horizontal scrolling
9093 ************************************************************************/
9095 static int hscroll_window_tree P_ ((Lisp_Object));
9096 static int hscroll_windows P_ ((Lisp_Object));
9098 /* For all leaf windows in the window tree rooted at WINDOW, set their
9099 hscroll value so that PT is (i) visible in the window, and (ii) so
9100 that it is not within a certain margin at the window's left and
9101 right border. Value is non-zero if any window's hscroll has been
9102 changed. */
9104 static int
9105 hscroll_window_tree (window)
9106 Lisp_Object window;
9108 int hscrolled_p = 0;
9109 int hscroll_relative_p = FLOATP (Vhscroll_step);
9110 int hscroll_step_abs = 0;
9111 double hscroll_step_rel = 0;
9113 if (hscroll_relative_p)
9115 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9116 if (hscroll_step_rel < 0)
9118 hscroll_relative_p = 0;
9119 hscroll_step_abs = 0;
9122 else if (INTEGERP (Vhscroll_step))
9124 hscroll_step_abs = XINT (Vhscroll_step);
9125 if (hscroll_step_abs < 0)
9126 hscroll_step_abs = 0;
9128 else
9129 hscroll_step_abs = 0;
9131 while (WINDOWP (window))
9133 struct window *w = XWINDOW (window);
9135 if (WINDOWP (w->hchild))
9136 hscrolled_p |= hscroll_window_tree (w->hchild);
9137 else if (WINDOWP (w->vchild))
9138 hscrolled_p |= hscroll_window_tree (w->vchild);
9139 else if (w->cursor.vpos >= 0)
9141 int h_margin, text_area_x, text_area_y;
9142 int text_area_width, text_area_height;
9143 struct glyph_row *current_cursor_row
9144 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9145 struct glyph_row *desired_cursor_row
9146 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9147 struct glyph_row *cursor_row
9148 = (desired_cursor_row->enabled_p
9149 ? desired_cursor_row
9150 : current_cursor_row);
9152 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
9153 &text_area_width, &text_area_height);
9155 /* Scroll when cursor is inside this scroll margin. */
9156 h_margin = hscroll_margin * CANON_X_UNIT (XFRAME (w->frame));
9158 if ((XFASTINT (w->hscroll)
9159 && w->cursor.x <= h_margin)
9160 || (cursor_row->enabled_p
9161 && cursor_row->truncated_on_right_p
9162 && (w->cursor.x >= text_area_width - h_margin)))
9164 struct it it;
9165 int hscroll;
9166 struct buffer *saved_current_buffer;
9167 int pt;
9168 int wanted_x;
9170 /* Find point in a display of infinite width. */
9171 saved_current_buffer = current_buffer;
9172 current_buffer = XBUFFER (w->buffer);
9174 if (w == XWINDOW (selected_window))
9175 pt = BUF_PT (current_buffer);
9176 else
9178 pt = marker_position (w->pointm);
9179 pt = max (BEGV, pt);
9180 pt = min (ZV, pt);
9183 /* Move iterator to pt starting at cursor_row->start in
9184 a line with infinite width. */
9185 init_to_row_start (&it, w, cursor_row);
9186 it.last_visible_x = INFINITY;
9187 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9188 current_buffer = saved_current_buffer;
9190 /* Position cursor in window. */
9191 if (!hscroll_relative_p && hscroll_step_abs == 0)
9192 hscroll = max (0, it.current_x - text_area_width / 2)
9193 / CANON_X_UNIT (it.f);
9194 else if (w->cursor.x >= text_area_width - h_margin)
9196 if (hscroll_relative_p)
9197 wanted_x = text_area_width * (1 - hscroll_step_rel)
9198 - h_margin;
9199 else
9200 wanted_x = text_area_width
9201 - hscroll_step_abs * CANON_X_UNIT (it.f)
9202 - h_margin;
9203 hscroll
9204 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
9206 else
9208 if (hscroll_relative_p)
9209 wanted_x = text_area_width * hscroll_step_rel
9210 + h_margin;
9211 else
9212 wanted_x = hscroll_step_abs * CANON_X_UNIT (it.f)
9213 + h_margin;
9214 hscroll
9215 = max (0, it.current_x - wanted_x) / CANON_X_UNIT (it.f);
9217 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9219 /* Don't call Fset_window_hscroll if value hasn't
9220 changed because it will prevent redisplay
9221 optimizations. */
9222 if (XFASTINT (w->hscroll) != hscroll)
9224 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9225 w->hscroll = make_number (hscroll);
9226 hscrolled_p = 1;
9231 window = w->next;
9234 /* Value is non-zero if hscroll of any leaf window has been changed. */
9235 return hscrolled_p;
9239 /* Set hscroll so that cursor is visible and not inside horizontal
9240 scroll margins for all windows in the tree rooted at WINDOW. See
9241 also hscroll_window_tree above. Value is non-zero if any window's
9242 hscroll has been changed. If it has, desired matrices on the frame
9243 of WINDOW are cleared. */
9245 static int
9246 hscroll_windows (window)
9247 Lisp_Object window;
9249 int hscrolled_p;
9251 if (automatic_hscrolling_p)
9253 hscrolled_p = hscroll_window_tree (window);
9254 if (hscrolled_p)
9255 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9257 else
9258 hscrolled_p = 0;
9259 return hscrolled_p;
9264 /************************************************************************
9265 Redisplay
9266 ************************************************************************/
9268 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9269 to a non-zero value. This is sometimes handy to have in a debugger
9270 session. */
9272 #if GLYPH_DEBUG
9274 /* First and last unchanged row for try_window_id. */
9276 int debug_first_unchanged_at_end_vpos;
9277 int debug_last_unchanged_at_beg_vpos;
9279 /* Delta vpos and y. */
9281 int debug_dvpos, debug_dy;
9283 /* Delta in characters and bytes for try_window_id. */
9285 int debug_delta, debug_delta_bytes;
9287 /* Values of window_end_pos and window_end_vpos at the end of
9288 try_window_id. */
9290 EMACS_INT debug_end_pos, debug_end_vpos;
9292 /* Append a string to W->desired_matrix->method. FMT is a printf
9293 format string. A1...A9 are a supplement for a variable-length
9294 argument list. If trace_redisplay_p is non-zero also printf the
9295 resulting string to stderr. */
9297 static void
9298 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9299 struct window *w;
9300 char *fmt;
9301 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9303 char buffer[512];
9304 char *method = w->desired_matrix->method;
9305 int len = strlen (method);
9306 int size = sizeof w->desired_matrix->method;
9307 int remaining = size - len - 1;
9309 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9310 if (len && remaining)
9312 method[len] = '|';
9313 --remaining, ++len;
9316 strncpy (method + len, buffer, remaining);
9318 if (trace_redisplay_p)
9319 fprintf (stderr, "%p (%s): %s\n",
9321 ((BUFFERP (w->buffer)
9322 && STRINGP (XBUFFER (w->buffer)->name))
9323 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9324 : "no buffer"),
9325 buffer);
9328 #endif /* GLYPH_DEBUG */
9331 /* Value is non-zero if all changes in window W, which displays
9332 current_buffer, are in the text between START and END. START is a
9333 buffer position, END is given as a distance from Z. Used in
9334 redisplay_internal for display optimization. */
9336 static INLINE int
9337 text_outside_line_unchanged_p (w, start, end)
9338 struct window *w;
9339 int start, end;
9341 int unchanged_p = 1;
9343 /* If text or overlays have changed, see where. */
9344 if (XFASTINT (w->last_modified) < MODIFF
9345 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9347 /* Gap in the line? */
9348 if (GPT < start || Z - GPT < end)
9349 unchanged_p = 0;
9351 /* Changes start in front of the line, or end after it? */
9352 if (unchanged_p
9353 && (BEG_UNCHANGED < start - 1
9354 || END_UNCHANGED < end))
9355 unchanged_p = 0;
9357 /* If selective display, can't optimize if changes start at the
9358 beginning of the line. */
9359 if (unchanged_p
9360 && INTEGERP (current_buffer->selective_display)
9361 && XINT (current_buffer->selective_display) > 0
9362 && (BEG_UNCHANGED < start || GPT <= start))
9363 unchanged_p = 0;
9365 /* If there are overlays at the start or end of the line, these
9366 may have overlay strings with newlines in them. A change at
9367 START, for instance, may actually concern the display of such
9368 overlay strings as well, and they are displayed on different
9369 lines. So, quickly rule out this case. (For the future, it
9370 might be desirable to implement something more telling than
9371 just BEG/END_UNCHANGED.) */
9372 if (unchanged_p)
9374 if (BEG + BEG_UNCHANGED == start
9375 && overlay_touches_p (start))
9376 unchanged_p = 0;
9377 if (END_UNCHANGED == end
9378 && overlay_touches_p (Z - end))
9379 unchanged_p = 0;
9383 return unchanged_p;
9387 /* Do a frame update, taking possible shortcuts into account. This is
9388 the main external entry point for redisplay.
9390 If the last redisplay displayed an echo area message and that message
9391 is no longer requested, we clear the echo area or bring back the
9392 mini-buffer if that is in use. */
9394 void
9395 redisplay ()
9397 redisplay_internal (0);
9401 /* Return 1 if point moved out of or into a composition. Otherwise
9402 return 0. PREV_BUF and PREV_PT are the last point buffer and
9403 position. BUF and PT are the current point buffer and position. */
9406 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9407 struct buffer *prev_buf, *buf;
9408 int prev_pt, pt;
9410 int start, end;
9411 Lisp_Object prop;
9412 Lisp_Object buffer;
9414 XSETBUFFER (buffer, buf);
9415 /* Check a composition at the last point if point moved within the
9416 same buffer. */
9417 if (prev_buf == buf)
9419 if (prev_pt == pt)
9420 /* Point didn't move. */
9421 return 0;
9423 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9424 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9425 && COMPOSITION_VALID_P (start, end, prop)
9426 && start < prev_pt && end > prev_pt)
9427 /* The last point was within the composition. Return 1 iff
9428 point moved out of the composition. */
9429 return (pt <= start || pt >= end);
9432 /* Check a composition at the current point. */
9433 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9434 && find_composition (pt, -1, &start, &end, &prop, buffer)
9435 && COMPOSITION_VALID_P (start, end, prop)
9436 && start < pt && end > pt);
9440 /* Reconsider the setting of B->clip_changed which is displayed
9441 in window W. */
9443 static INLINE void
9444 reconsider_clip_changes (w, b)
9445 struct window *w;
9446 struct buffer *b;
9448 if (b->clip_changed
9449 && !NILP (w->window_end_valid)
9450 && w->current_matrix->buffer == b
9451 && w->current_matrix->zv == BUF_ZV (b)
9452 && w->current_matrix->begv == BUF_BEGV (b))
9453 b->clip_changed = 0;
9455 /* If display wasn't paused, and W is not a tool bar window, see if
9456 point has been moved into or out of a composition. In that case,
9457 we set b->clip_changed to 1 to force updating the screen. If
9458 b->clip_changed has already been set to 1, we can skip this
9459 check. */
9460 if (!b->clip_changed
9461 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9463 int pt;
9465 if (w == XWINDOW (selected_window))
9466 pt = BUF_PT (current_buffer);
9467 else
9468 pt = marker_position (w->pointm);
9470 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9471 || pt != XINT (w->last_point))
9472 && check_point_in_composition (w->current_matrix->buffer,
9473 XINT (w->last_point),
9474 XBUFFER (w->buffer), pt))
9475 b->clip_changed = 1;
9479 #define STOP_POLLING \
9480 do { if (! polling_stopped_here) stop_polling (); \
9481 polling_stopped_here = 1; } while (0)
9483 #define RESUME_POLLING \
9484 do { if (polling_stopped_here) start_polling (); \
9485 polling_stopped_here = 0; } while (0)
9488 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9489 response to any user action; therefore, we should preserve the echo
9490 area. (Actually, our caller does that job.) Perhaps in the future
9491 avoid recentering windows if it is not necessary; currently that
9492 causes some problems. */
9494 static void
9495 redisplay_internal (preserve_echo_area)
9496 int preserve_echo_area;
9498 struct window *w = XWINDOW (selected_window);
9499 struct frame *f = XFRAME (w->frame);
9500 int pause;
9501 int must_finish = 0;
9502 struct text_pos tlbufpos, tlendpos;
9503 int number_of_visible_frames;
9504 int count;
9505 struct frame *sf = SELECTED_FRAME ();
9506 int polling_stopped_here = 0;
9508 /* Non-zero means redisplay has to consider all windows on all
9509 frames. Zero means, only selected_window is considered. */
9510 int consider_all_windows_p;
9512 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9514 /* No redisplay if running in batch mode or frame is not yet fully
9515 initialized, or redisplay is explicitly turned off by setting
9516 Vinhibit_redisplay. */
9517 if (noninteractive
9518 || !NILP (Vinhibit_redisplay)
9519 || !f->glyphs_initialized_p)
9520 return;
9522 /* The flag redisplay_performed_directly_p is set by
9523 direct_output_for_insert when it already did the whole screen
9524 update necessary. */
9525 if (redisplay_performed_directly_p)
9527 redisplay_performed_directly_p = 0;
9528 if (!hscroll_windows (selected_window))
9529 return;
9532 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9533 if (popup_activated ())
9534 return;
9535 #endif
9537 /* I don't think this happens but let's be paranoid. */
9538 if (redisplaying_p)
9539 return;
9541 /* Record a function that resets redisplaying_p to its old value
9542 when we leave this function. */
9543 count = SPECPDL_INDEX ();
9544 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
9545 ++redisplaying_p;
9546 specbind (Qinhibit_free_realized_faces, Qnil);
9548 retry:
9549 pause = 0;
9550 reconsider_clip_changes (w, current_buffer);
9552 /* If new fonts have been loaded that make a glyph matrix adjustment
9553 necessary, do it. */
9554 if (fonts_changed_p)
9556 adjust_glyphs (NULL);
9557 ++windows_or_buffers_changed;
9558 fonts_changed_p = 0;
9561 /* If face_change_count is non-zero, init_iterator will free all
9562 realized faces, which includes the faces referenced from current
9563 matrices. So, we can't reuse current matrices in this case. */
9564 if (face_change_count)
9565 ++windows_or_buffers_changed;
9567 if (! FRAME_WINDOW_P (sf)
9568 && previous_terminal_frame != sf)
9570 /* Since frames on an ASCII terminal share the same display
9571 area, displaying a different frame means redisplay the whole
9572 thing. */
9573 windows_or_buffers_changed++;
9574 SET_FRAME_GARBAGED (sf);
9575 XSETFRAME (Vterminal_frame, sf);
9577 previous_terminal_frame = sf;
9579 /* Set the visible flags for all frames. Do this before checking
9580 for resized or garbaged frames; they want to know if their frames
9581 are visible. See the comment in frame.h for
9582 FRAME_SAMPLE_VISIBILITY. */
9584 Lisp_Object tail, frame;
9586 number_of_visible_frames = 0;
9588 FOR_EACH_FRAME (tail, frame)
9590 struct frame *f = XFRAME (frame);
9592 FRAME_SAMPLE_VISIBILITY (f);
9593 if (FRAME_VISIBLE_P (f))
9594 ++number_of_visible_frames;
9595 clear_desired_matrices (f);
9599 /* Notice any pending interrupt request to change frame size. */
9600 do_pending_window_change (1);
9602 /* Clear frames marked as garbaged. */
9603 if (frame_garbaged)
9604 clear_garbaged_frames ();
9606 /* Build menubar and tool-bar items. */
9607 prepare_menu_bars ();
9609 if (windows_or_buffers_changed)
9610 update_mode_lines++;
9612 /* Detect case that we need to write or remove a star in the mode line. */
9613 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
9615 w->update_mode_line = Qt;
9616 if (buffer_shared > 1)
9617 update_mode_lines++;
9620 /* If %c is in the mode line, update it if needed. */
9621 if (!NILP (w->column_number_displayed)
9622 /* This alternative quickly identifies a common case
9623 where no change is needed. */
9624 && !(PT == XFASTINT (w->last_point)
9625 && XFASTINT (w->last_modified) >= MODIFF
9626 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
9627 && (XFASTINT (w->column_number_displayed)
9628 != (int) current_column ())) /* iftc */
9629 w->update_mode_line = Qt;
9631 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
9633 /* The variable buffer_shared is set in redisplay_window and
9634 indicates that we redisplay a buffer in different windows. See
9635 there. */
9636 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
9637 || cursor_type_changed);
9639 /* If specs for an arrow have changed, do thorough redisplay
9640 to ensure we remove any arrow that should no longer exist. */
9641 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
9642 || ! EQ (Voverlay_arrow_string, last_arrow_string))
9643 consider_all_windows_p = windows_or_buffers_changed = 1;
9645 /* Normally the message* functions will have already displayed and
9646 updated the echo area, but the frame may have been trashed, or
9647 the update may have been preempted, so display the echo area
9648 again here. Checking message_cleared_p captures the case that
9649 the echo area should be cleared. */
9650 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
9651 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
9652 || (message_cleared_p
9653 && minibuf_level == 0
9654 /* If the mini-window is currently selected, this means the
9655 echo-area doesn't show through. */
9656 && !MINI_WINDOW_P (XWINDOW (selected_window))))
9658 int window_height_changed_p = echo_area_display (0);
9659 must_finish = 1;
9661 /* If we don't display the current message, don't clear the
9662 message_cleared_p flag, because, if we did, we wouldn't clear
9663 the echo area in the next redisplay which doesn't preserve
9664 the echo area. */
9665 if (!display_last_displayed_message_p)
9666 message_cleared_p = 0;
9668 if (fonts_changed_p)
9669 goto retry;
9670 else if (window_height_changed_p)
9672 consider_all_windows_p = 1;
9673 ++update_mode_lines;
9674 ++windows_or_buffers_changed;
9676 /* If window configuration was changed, frames may have been
9677 marked garbaged. Clear them or we will experience
9678 surprises wrt scrolling. */
9679 if (frame_garbaged)
9680 clear_garbaged_frames ();
9683 else if (EQ (selected_window, minibuf_window)
9684 && (current_buffer->clip_changed
9685 || XFASTINT (w->last_modified) < MODIFF
9686 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9687 && resize_mini_window (w, 0))
9689 /* Resized active mini-window to fit the size of what it is
9690 showing if its contents might have changed. */
9691 must_finish = 1;
9692 consider_all_windows_p = 1;
9693 ++windows_or_buffers_changed;
9694 ++update_mode_lines;
9696 /* If window configuration was changed, frames may have been
9697 marked garbaged. Clear them or we will experience
9698 surprises wrt scrolling. */
9699 if (frame_garbaged)
9700 clear_garbaged_frames ();
9704 /* If showing the region, and mark has changed, we must redisplay
9705 the whole window. The assignment to this_line_start_pos prevents
9706 the optimization directly below this if-statement. */
9707 if (((!NILP (Vtransient_mark_mode)
9708 && !NILP (XBUFFER (w->buffer)->mark_active))
9709 != !NILP (w->region_showing))
9710 || (!NILP (w->region_showing)
9711 && !EQ (w->region_showing,
9712 Fmarker_position (XBUFFER (w->buffer)->mark))))
9713 CHARPOS (this_line_start_pos) = 0;
9715 /* Optimize the case that only the line containing the cursor in the
9716 selected window has changed. Variables starting with this_ are
9717 set in display_line and record information about the line
9718 containing the cursor. */
9719 tlbufpos = this_line_start_pos;
9720 tlendpos = this_line_end_pos;
9721 if (!consider_all_windows_p
9722 && CHARPOS (tlbufpos) > 0
9723 && NILP (w->update_mode_line)
9724 && !current_buffer->clip_changed
9725 && !current_buffer->prevent_redisplay_optimizations_p
9726 && FRAME_VISIBLE_P (XFRAME (w->frame))
9727 && !FRAME_OBSCURED_P (XFRAME (w->frame))
9728 /* Make sure recorded data applies to current buffer, etc. */
9729 && this_line_buffer == current_buffer
9730 && current_buffer == XBUFFER (w->buffer)
9731 && NILP (w->force_start)
9732 && NILP (w->optional_new_start)
9733 /* Point must be on the line that we have info recorded about. */
9734 && PT >= CHARPOS (tlbufpos)
9735 && PT <= Z - CHARPOS (tlendpos)
9736 /* All text outside that line, including its final newline,
9737 must be unchanged */
9738 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
9739 CHARPOS (tlendpos)))
9741 if (CHARPOS (tlbufpos) > BEGV
9742 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
9743 && (CHARPOS (tlbufpos) == ZV
9744 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
9745 /* Former continuation line has disappeared by becoming empty */
9746 goto cancel;
9747 else if (XFASTINT (w->last_modified) < MODIFF
9748 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
9749 || MINI_WINDOW_P (w))
9751 /* We have to handle the case of continuation around a
9752 wide-column character (See the comment in indent.c around
9753 line 885).
9755 For instance, in the following case:
9757 -------- Insert --------
9758 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
9759 J_I_ ==> J_I_ `^^' are cursors.
9760 ^^ ^^
9761 -------- --------
9763 As we have to redraw the line above, we should goto cancel. */
9765 struct it it;
9766 int line_height_before = this_line_pixel_height;
9768 /* Note that start_display will handle the case that the
9769 line starting at tlbufpos is a continuation lines. */
9770 start_display (&it, w, tlbufpos);
9772 /* Implementation note: It this still necessary? */
9773 if (it.current_x != this_line_start_x)
9774 goto cancel;
9776 TRACE ((stderr, "trying display optimization 1\n"));
9777 w->cursor.vpos = -1;
9778 overlay_arrow_seen = 0;
9779 it.vpos = this_line_vpos;
9780 it.current_y = this_line_y;
9781 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
9782 display_line (&it);
9784 /* If line contains point, is not continued,
9785 and ends at same distance from eob as before, we win */
9786 if (w->cursor.vpos >= 0
9787 /* Line is not continued, otherwise this_line_start_pos
9788 would have been set to 0 in display_line. */
9789 && CHARPOS (this_line_start_pos)
9790 /* Line ends as before. */
9791 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
9792 /* Line has same height as before. Otherwise other lines
9793 would have to be shifted up or down. */
9794 && this_line_pixel_height == line_height_before)
9796 /* If this is not the window's last line, we must adjust
9797 the charstarts of the lines below. */
9798 if (it.current_y < it.last_visible_y)
9800 struct glyph_row *row
9801 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
9802 int delta, delta_bytes;
9804 if (Z - CHARPOS (tlendpos) == ZV)
9806 /* This line ends at end of (accessible part of)
9807 buffer. There is no newline to count. */
9808 delta = (Z
9809 - CHARPOS (tlendpos)
9810 - MATRIX_ROW_START_CHARPOS (row));
9811 delta_bytes = (Z_BYTE
9812 - BYTEPOS (tlendpos)
9813 - MATRIX_ROW_START_BYTEPOS (row));
9815 else
9817 /* This line ends in a newline. Must take
9818 account of the newline and the rest of the
9819 text that follows. */
9820 delta = (Z
9821 - CHARPOS (tlendpos)
9822 - MATRIX_ROW_START_CHARPOS (row));
9823 delta_bytes = (Z_BYTE
9824 - BYTEPOS (tlendpos)
9825 - MATRIX_ROW_START_BYTEPOS (row));
9828 increment_matrix_positions (w->current_matrix,
9829 this_line_vpos + 1,
9830 w->current_matrix->nrows,
9831 delta, delta_bytes);
9834 /* If this row displays text now but previously didn't,
9835 or vice versa, w->window_end_vpos may have to be
9836 adjusted. */
9837 if ((it.glyph_row - 1)->displays_text_p)
9839 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
9840 XSETINT (w->window_end_vpos, this_line_vpos);
9842 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
9843 && this_line_vpos > 0)
9844 XSETINT (w->window_end_vpos, this_line_vpos - 1);
9845 w->window_end_valid = Qnil;
9847 /* Update hint: No need to try to scroll in update_window. */
9848 w->desired_matrix->no_scrolling_p = 1;
9850 #if GLYPH_DEBUG
9851 *w->desired_matrix->method = 0;
9852 debug_method_add (w, "optimization 1");
9853 #endif
9854 goto update;
9856 else
9857 goto cancel;
9859 else if (/* Cursor position hasn't changed. */
9860 PT == XFASTINT (w->last_point)
9861 /* Make sure the cursor was last displayed
9862 in this window. Otherwise we have to reposition it. */
9863 && 0 <= w->cursor.vpos
9864 && XINT (w->height) > w->cursor.vpos)
9866 if (!must_finish)
9868 do_pending_window_change (1);
9870 /* We used to always goto end_of_redisplay here, but this
9871 isn't enough if we have a blinking cursor. */
9872 if (w->cursor_off_p == w->last_cursor_off_p)
9873 goto end_of_redisplay;
9875 goto update;
9877 /* If highlighting the region, or if the cursor is in the echo area,
9878 then we can't just move the cursor. */
9879 else if (! (!NILP (Vtransient_mark_mode)
9880 && !NILP (current_buffer->mark_active))
9881 && (EQ (selected_window, current_buffer->last_selected_window)
9882 || highlight_nonselected_windows)
9883 && NILP (w->region_showing)
9884 && NILP (Vshow_trailing_whitespace)
9885 && !cursor_in_echo_area)
9887 struct it it;
9888 struct glyph_row *row;
9890 /* Skip from tlbufpos to PT and see where it is. Note that
9891 PT may be in invisible text. If so, we will end at the
9892 next visible position. */
9893 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
9894 NULL, DEFAULT_FACE_ID);
9895 it.current_x = this_line_start_x;
9896 it.current_y = this_line_y;
9897 it.vpos = this_line_vpos;
9899 /* The call to move_it_to stops in front of PT, but
9900 moves over before-strings. */
9901 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
9903 if (it.vpos == this_line_vpos
9904 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
9905 row->enabled_p))
9907 xassert (this_line_vpos == it.vpos);
9908 xassert (this_line_y == it.current_y);
9909 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9910 #if GLYPH_DEBUG
9911 *w->desired_matrix->method = 0;
9912 debug_method_add (w, "optimization 3");
9913 #endif
9914 goto update;
9916 else
9917 goto cancel;
9920 cancel:
9921 /* Text changed drastically or point moved off of line. */
9922 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
9925 CHARPOS (this_line_start_pos) = 0;
9926 consider_all_windows_p |= buffer_shared > 1;
9927 ++clear_face_cache_count;
9930 /* Build desired matrices, and update the display. If
9931 consider_all_windows_p is non-zero, do it for all windows on all
9932 frames. Otherwise do it for selected_window, only. */
9934 if (consider_all_windows_p)
9936 Lisp_Object tail, frame;
9937 int i, n = 0, size = 50;
9938 struct frame **updated
9939 = (struct frame **) alloca (size * sizeof *updated);
9941 /* Clear the face cache eventually. */
9942 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
9944 clear_face_cache (0);
9945 clear_face_cache_count = 0;
9948 /* Recompute # windows showing selected buffer. This will be
9949 incremented each time such a window is displayed. */
9950 buffer_shared = 0;
9952 FOR_EACH_FRAME (tail, frame)
9954 struct frame *f = XFRAME (frame);
9956 if (FRAME_WINDOW_P (f) || f == sf)
9958 #ifdef HAVE_WINDOW_SYSTEM
9959 if (clear_face_cache_count % 50 == 0
9960 && FRAME_WINDOW_P (f))
9961 clear_image_cache (f, 0);
9962 #endif /* HAVE_WINDOW_SYSTEM */
9964 /* Mark all the scroll bars to be removed; we'll redeem
9965 the ones we want when we redisplay their windows. */
9966 if (condemn_scroll_bars_hook)
9967 condemn_scroll_bars_hook (f);
9969 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9970 redisplay_windows (FRAME_ROOT_WINDOW (f));
9972 /* Any scroll bars which redisplay_windows should have
9973 nuked should now go away. */
9974 if (judge_scroll_bars_hook)
9975 judge_scroll_bars_hook (f);
9977 /* If fonts changed, display again. */
9978 /* ??? rms: I suspect it is a mistake to jump all the way
9979 back to retry here. It should just retry this frame. */
9980 if (fonts_changed_p)
9981 goto retry;
9983 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
9985 /* See if we have to hscroll. */
9986 if (hscroll_windows (f->root_window))
9987 goto retry;
9989 /* Prevent various kinds of signals during display
9990 update. stdio is not robust about handling
9991 signals, which can cause an apparent I/O
9992 error. */
9993 if (interrupt_input)
9994 unrequest_sigio ();
9995 STOP_POLLING;
9997 /* Update the display. */
9998 set_window_update_flags (XWINDOW (f->root_window), 1);
9999 pause |= update_frame (f, 0, 0);
10000 if (pause)
10001 break;
10003 if (n == size)
10005 int nbytes = size * sizeof *updated;
10006 struct frame **p = (struct frame **) alloca (2 * nbytes);
10007 bcopy (updated, p, nbytes);
10008 size *= 2;
10011 updated[n++] = f;
10016 /* Do the mark_window_display_accurate after all windows have
10017 been redisplayed because this call resets flags in buffers
10018 which are needed for proper redisplay. */
10019 for (i = 0; i < n; ++i)
10021 struct frame *f = updated[i];
10022 mark_window_display_accurate (f->root_window, 1);
10023 if (frame_up_to_date_hook)
10024 frame_up_to_date_hook (f);
10027 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10029 Lisp_Object mini_window;
10030 struct frame *mini_frame;
10032 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10033 /* Use list_of_error, not Qerror, so that
10034 we catch only errors and don't run the debugger. */
10035 internal_condition_case_1 (redisplay_window_1, selected_window,
10036 list_of_error,
10037 redisplay_window_error);
10039 /* Compare desired and current matrices, perform output. */
10041 update:
10042 /* If fonts changed, display again. */
10043 if (fonts_changed_p)
10044 goto retry;
10046 /* Prevent various kinds of signals during display update.
10047 stdio is not robust about handling signals,
10048 which can cause an apparent I/O error. */
10049 if (interrupt_input)
10050 unrequest_sigio ();
10051 STOP_POLLING;
10053 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10055 if (hscroll_windows (selected_window))
10056 goto retry;
10058 XWINDOW (selected_window)->must_be_updated_p = 1;
10059 pause = update_frame (sf, 0, 0);
10062 /* We may have called echo_area_display at the top of this
10063 function. If the echo area is on another frame, that may
10064 have put text on a frame other than the selected one, so the
10065 above call to update_frame would not have caught it. Catch
10066 it here. */
10067 mini_window = FRAME_MINIBUF_WINDOW (sf);
10068 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10070 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10072 XWINDOW (mini_window)->must_be_updated_p = 1;
10073 pause |= update_frame (mini_frame, 0, 0);
10074 if (!pause && hscroll_windows (mini_window))
10075 goto retry;
10079 /* If display was paused because of pending input, make sure we do a
10080 thorough update the next time. */
10081 if (pause)
10083 /* Prevent the optimization at the beginning of
10084 redisplay_internal that tries a single-line update of the
10085 line containing the cursor in the selected window. */
10086 CHARPOS (this_line_start_pos) = 0;
10088 /* Let the overlay arrow be updated the next time. */
10089 if (!NILP (last_arrow_position))
10091 last_arrow_position = Qt;
10092 last_arrow_string = Qt;
10095 /* If we pause after scrolling, some rows in the current
10096 matrices of some windows are not valid. */
10097 if (!WINDOW_FULL_WIDTH_P (w)
10098 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10099 update_mode_lines = 1;
10101 else
10103 if (!consider_all_windows_p)
10105 /* This has already been done above if
10106 consider_all_windows_p is set. */
10107 mark_window_display_accurate_1 (w, 1);
10109 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10110 last_arrow_string = Voverlay_arrow_string;
10112 if (frame_up_to_date_hook != 0)
10113 frame_up_to_date_hook (sf);
10116 update_mode_lines = 0;
10117 windows_or_buffers_changed = 0;
10118 cursor_type_changed = 0;
10121 /* Start SIGIO interrupts coming again. Having them off during the
10122 code above makes it less likely one will discard output, but not
10123 impossible, since there might be stuff in the system buffer here.
10124 But it is much hairier to try to do anything about that. */
10125 if (interrupt_input)
10126 request_sigio ();
10127 RESUME_POLLING;
10129 /* If a frame has become visible which was not before, redisplay
10130 again, so that we display it. Expose events for such a frame
10131 (which it gets when becoming visible) don't call the parts of
10132 redisplay constructing glyphs, so simply exposing a frame won't
10133 display anything in this case. So, we have to display these
10134 frames here explicitly. */
10135 if (!pause)
10137 Lisp_Object tail, frame;
10138 int new_count = 0;
10140 FOR_EACH_FRAME (tail, frame)
10142 int this_is_visible = 0;
10144 if (XFRAME (frame)->visible)
10145 this_is_visible = 1;
10146 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10147 if (XFRAME (frame)->visible)
10148 this_is_visible = 1;
10150 if (this_is_visible)
10151 new_count++;
10154 if (new_count != number_of_visible_frames)
10155 windows_or_buffers_changed++;
10158 /* Change frame size now if a change is pending. */
10159 do_pending_window_change (1);
10161 /* If we just did a pending size change, or have additional
10162 visible frames, redisplay again. */
10163 if (windows_or_buffers_changed && !pause)
10164 goto retry;
10166 end_of_redisplay:
10167 unbind_to (count, Qnil);
10168 RESUME_POLLING;
10172 /* Redisplay, but leave alone any recent echo area message unless
10173 another message has been requested in its place.
10175 This is useful in situations where you need to redisplay but no
10176 user action has occurred, making it inappropriate for the message
10177 area to be cleared. See tracking_off and
10178 wait_reading_process_input for examples of these situations.
10180 FROM_WHERE is an integer saying from where this function was
10181 called. This is useful for debugging. */
10183 void
10184 redisplay_preserve_echo_area (from_where)
10185 int from_where;
10187 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10189 if (!NILP (echo_area_buffer[1]))
10191 /* We have a previously displayed message, but no current
10192 message. Redisplay the previous message. */
10193 display_last_displayed_message_p = 1;
10194 redisplay_internal (1);
10195 display_last_displayed_message_p = 0;
10197 else
10198 redisplay_internal (1);
10202 /* Function registered with record_unwind_protect in
10203 redisplay_internal. Reset redisplaying_p to the value it had
10204 before redisplay_internal was called, and clear
10205 prevent_freeing_realized_faces_p. */
10207 static Lisp_Object
10208 unwind_redisplay (old_redisplaying_p)
10209 Lisp_Object old_redisplaying_p;
10211 redisplaying_p = XFASTINT (old_redisplaying_p);
10212 return Qnil;
10216 /* Mark the display of window W as accurate or inaccurate. If
10217 ACCURATE_P is non-zero mark display of W as accurate. If
10218 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10219 redisplay_internal is called. */
10221 static void
10222 mark_window_display_accurate_1 (w, accurate_p)
10223 struct window *w;
10224 int accurate_p;
10226 if (BUFFERP (w->buffer))
10228 struct buffer *b = XBUFFER (w->buffer);
10230 w->last_modified
10231 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10232 w->last_overlay_modified
10233 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10234 w->last_had_star
10235 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10237 if (accurate_p)
10239 b->clip_changed = 0;
10240 b->prevent_redisplay_optimizations_p = 0;
10242 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10243 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10244 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10245 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10247 w->current_matrix->buffer = b;
10248 w->current_matrix->begv = BUF_BEGV (b);
10249 w->current_matrix->zv = BUF_ZV (b);
10251 w->last_cursor = w->cursor;
10252 w->last_cursor_off_p = w->cursor_off_p;
10254 if (w == XWINDOW (selected_window))
10255 w->last_point = make_number (BUF_PT (b));
10256 else
10257 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10261 if (accurate_p)
10263 w->window_end_valid = w->buffer;
10264 #if 0 /* This is incorrect with variable-height lines. */
10265 xassert (XINT (w->window_end_vpos)
10266 < (XINT (w->height)
10267 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10268 #endif
10269 w->update_mode_line = Qnil;
10274 /* Mark the display of windows in the window tree rooted at WINDOW as
10275 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10276 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10277 be redisplayed the next time redisplay_internal is called. */
10279 void
10280 mark_window_display_accurate (window, accurate_p)
10281 Lisp_Object window;
10282 int accurate_p;
10284 struct window *w;
10286 for (; !NILP (window); window = w->next)
10288 w = XWINDOW (window);
10289 mark_window_display_accurate_1 (w, accurate_p);
10291 if (!NILP (w->vchild))
10292 mark_window_display_accurate (w->vchild, accurate_p);
10293 if (!NILP (w->hchild))
10294 mark_window_display_accurate (w->hchild, accurate_p);
10297 if (accurate_p)
10299 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
10300 last_arrow_string = Voverlay_arrow_string;
10302 else
10304 /* Force a thorough redisplay the next time by setting
10305 last_arrow_position and last_arrow_string to t, which is
10306 unequal to any useful value of Voverlay_arrow_... */
10307 last_arrow_position = Qt;
10308 last_arrow_string = Qt;
10313 /* Return value in display table DP (Lisp_Char_Table *) for character
10314 C. Since a display table doesn't have any parent, we don't have to
10315 follow parent. Do not call this function directly but use the
10316 macro DISP_CHAR_VECTOR. */
10318 Lisp_Object
10319 disp_char_vector (dp, c)
10320 struct Lisp_Char_Table *dp;
10321 int c;
10323 int code[4], i;
10324 Lisp_Object val;
10326 if (SINGLE_BYTE_CHAR_P (c))
10327 return (dp->contents[c]);
10329 SPLIT_CHAR (c, code[0], code[1], code[2]);
10330 if (code[1] < 32)
10331 code[1] = -1;
10332 else if (code[2] < 32)
10333 code[2] = -1;
10335 /* Here, the possible range of code[0] (== charset ID) is
10336 128..max_charset. Since the top level char table contains data
10337 for multibyte characters after 256th element, we must increment
10338 code[0] by 128 to get a correct index. */
10339 code[0] += 128;
10340 code[3] = -1; /* anchor */
10342 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10344 val = dp->contents[code[i]];
10345 if (!SUB_CHAR_TABLE_P (val))
10346 return (NILP (val) ? dp->defalt : val);
10349 /* Here, val is a sub char table. We return the default value of
10350 it. */
10351 return (dp->defalt);
10356 /***********************************************************************
10357 Window Redisplay
10358 ***********************************************************************/
10360 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10362 static void
10363 redisplay_windows (window)
10364 Lisp_Object window;
10366 while (!NILP (window))
10368 struct window *w = XWINDOW (window);
10370 if (!NILP (w->hchild))
10371 redisplay_windows (w->hchild);
10372 else if (!NILP (w->vchild))
10373 redisplay_windows (w->vchild);
10374 else
10376 displayed_buffer = XBUFFER (w->buffer);
10377 /* Use list_of_error, not Qerror, so that
10378 we catch only errors and don't run the debugger. */
10379 internal_condition_case_1 (redisplay_window_0, window,
10380 list_of_error,
10381 redisplay_window_error);
10384 window = w->next;
10388 static Lisp_Object
10389 redisplay_window_error ()
10391 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10392 return Qnil;
10395 static Lisp_Object
10396 redisplay_window_0 (window)
10397 Lisp_Object window;
10399 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10400 redisplay_window (window, 0);
10401 return Qnil;
10404 static Lisp_Object
10405 redisplay_window_1 (window)
10406 Lisp_Object window;
10408 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10409 redisplay_window (window, 1);
10410 return Qnil;
10414 /* Increment GLYPH until it reaches END or CONDITION fails while
10415 adding (GLYPH)->pixel_width to X. */
10417 #define SKIP_GLYPHS(glyph, end, x, condition) \
10418 do \
10420 (x) += (glyph)->pixel_width; \
10421 ++(glyph); \
10423 while ((glyph) < (end) && (condition))
10426 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10427 DELTA is the number of bytes by which positions recorded in ROW
10428 differ from current buffer positions. */
10430 void
10431 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10432 struct window *w;
10433 struct glyph_row *row;
10434 struct glyph_matrix *matrix;
10435 int delta, delta_bytes, dy, dvpos;
10437 struct glyph *glyph = row->glyphs[TEXT_AREA];
10438 struct glyph *end = glyph + row->used[TEXT_AREA];
10439 /* The first glyph that starts a sequence of glyphs from string. */
10440 struct glyph *string_start;
10441 /* The X coordinate of string_start. */
10442 int string_start_x;
10443 /* The last known character position. */
10444 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10445 /* The last known character position before string_start. */
10446 int string_before_pos;
10447 int x = row->x;
10448 int pt_old = PT - delta;
10450 /* Skip over glyphs not having an object at the start of the row.
10451 These are special glyphs like truncation marks on terminal
10452 frames. */
10453 if (row->displays_text_p)
10454 while (glyph < end
10455 && INTEGERP (glyph->object)
10456 && glyph->charpos < 0)
10458 x += glyph->pixel_width;
10459 ++glyph;
10462 string_start = NULL;
10463 while (glyph < end
10464 && !INTEGERP (glyph->object)
10465 && (!BUFFERP (glyph->object)
10466 || (last_pos = glyph->charpos) < pt_old))
10468 if (! STRINGP (glyph->object))
10470 string_start = NULL;
10471 x += glyph->pixel_width;
10472 ++glyph;
10474 else
10476 string_before_pos = last_pos;
10477 string_start = glyph;
10478 string_start_x = x;
10479 /* Skip all glyphs from string. */
10480 SKIP_GLYPHS (glyph, end, x, STRINGP (glyph->object));
10484 if (string_start
10485 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10487 /* We may have skipped over point because the previous glyphs
10488 are from string. As there's no easy way to know the
10489 character position of the current glyph, find the correct
10490 glyph on point by scanning from string_start again. */
10491 Lisp_Object limit;
10492 Lisp_Object string;
10493 int pos;
10495 limit = make_number (pt_old + 1);
10496 end = glyph;
10497 glyph = string_start;
10498 x = string_start_x;
10499 string = glyph->object;
10500 pos = string_buffer_position (w, string, string_before_pos);
10501 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10502 because we always put cursor after overlay strings. */
10503 while (pos == 0 && glyph < end)
10505 string = glyph->object;
10506 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10507 if (glyph < end)
10508 pos = string_buffer_position (w, glyph->object, string_before_pos);
10511 while (glyph < end)
10513 pos = XINT (Fnext_single_char_property_change
10514 (make_number (pos), Qdisplay, Qnil, limit));
10515 if (pos > pt_old)
10516 break;
10517 /* Skip glyphs from the same string. */
10518 string = glyph->object;
10519 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10520 /* Skip glyphs from an overlay. */
10521 while (glyph < end
10522 && ! string_buffer_position (w, glyph->object, pos))
10524 string = glyph->object;
10525 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10530 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10531 w->cursor.x = x;
10532 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10533 w->cursor.y = row->y + dy;
10535 if (w == XWINDOW (selected_window))
10537 if (!row->continued_p
10538 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
10539 && row->x == 0)
10541 this_line_buffer = XBUFFER (w->buffer);
10543 CHARPOS (this_line_start_pos)
10544 = MATRIX_ROW_START_CHARPOS (row) + delta;
10545 BYTEPOS (this_line_start_pos)
10546 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
10548 CHARPOS (this_line_end_pos)
10549 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
10550 BYTEPOS (this_line_end_pos)
10551 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
10553 this_line_y = w->cursor.y;
10554 this_line_pixel_height = row->height;
10555 this_line_vpos = w->cursor.vpos;
10556 this_line_start_x = row->x;
10558 else
10559 CHARPOS (this_line_start_pos) = 0;
10564 /* Run window scroll functions, if any, for WINDOW with new window
10565 start STARTP. Sets the window start of WINDOW to that position.
10567 We assume that the window's buffer is really current. */
10569 static INLINE struct text_pos
10570 run_window_scroll_functions (window, startp)
10571 Lisp_Object window;
10572 struct text_pos startp;
10574 struct window *w = XWINDOW (window);
10575 SET_MARKER_FROM_TEXT_POS (w->start, startp);
10577 if (current_buffer != XBUFFER (w->buffer))
10578 abort ();
10580 if (!NILP (Vwindow_scroll_functions))
10582 run_hook_with_args_2 (Qwindow_scroll_functions, window,
10583 make_number (CHARPOS (startp)));
10584 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10585 /* In case the hook functions switch buffers. */
10586 if (current_buffer != XBUFFER (w->buffer))
10587 set_buffer_internal_1 (XBUFFER (w->buffer));
10590 return startp;
10594 /* Make sure the line containing the cursor is fully visible.
10595 A value of 1 means there is nothing to be done.
10596 (Either the line is fully visible, or it cannot be made so,
10597 or we cannot tell.)
10598 A value of 0 means the caller should do scrolling
10599 as if point had gone off the screen. */
10601 static int
10602 make_cursor_line_fully_visible (w)
10603 struct window *w;
10605 struct glyph_matrix *matrix;
10606 struct glyph_row *row;
10607 int window_height;
10609 /* It's not always possible to find the cursor, e.g, when a window
10610 is full of overlay strings. Don't do anything in that case. */
10611 if (w->cursor.vpos < 0)
10612 return 1;
10614 matrix = w->desired_matrix;
10615 row = MATRIX_ROW (matrix, w->cursor.vpos);
10617 /* If the cursor row is not partially visible, there's nothing to do. */
10618 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
10619 return 1;
10621 /* If the row the cursor is in is taller than the window's height,
10622 it's not clear what to do, so do nothing. */
10623 window_height = window_box_height (w);
10624 if (row->height >= window_height)
10625 return 1;
10627 return 0;
10629 #if 0
10630 /* This code used to try to scroll the window just enough to make
10631 the line visible. It returned 0 to say that the caller should
10632 allocate larger glyph matrices. */
10634 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
10636 int dy = row->height - row->visible_height;
10637 w->vscroll = 0;
10638 w->cursor.y += dy;
10639 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10641 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
10643 int dy = - (row->height - row->visible_height);
10644 w->vscroll = dy;
10645 w->cursor.y += dy;
10646 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
10649 /* When we change the cursor y-position of the selected window,
10650 change this_line_y as well so that the display optimization for
10651 the cursor line of the selected window in redisplay_internal uses
10652 the correct y-position. */
10653 if (w == XWINDOW (selected_window))
10654 this_line_y = w->cursor.y;
10656 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
10657 redisplay with larger matrices. */
10658 if (matrix->nrows < required_matrix_height (w))
10660 fonts_changed_p = 1;
10661 return 0;
10664 return 1;
10665 #endif /* 0 */
10669 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
10670 non-zero means only WINDOW is redisplayed in redisplay_internal.
10671 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
10672 in redisplay_window to bring a partially visible line into view in
10673 the case that only the cursor has moved.
10675 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
10676 last screen line's vertical height extends past the end of the screen.
10678 Value is
10680 1 if scrolling succeeded
10682 0 if scrolling didn't find point.
10684 -1 if new fonts have been loaded so that we must interrupt
10685 redisplay, adjust glyph matrices, and try again. */
10687 enum
10689 SCROLLING_SUCCESS,
10690 SCROLLING_FAILED,
10691 SCROLLING_NEED_LARGER_MATRICES
10694 static int
10695 try_scrolling (window, just_this_one_p, scroll_conservatively,
10696 scroll_step, temp_scroll_step, last_line_misfit)
10697 Lisp_Object window;
10698 int just_this_one_p;
10699 EMACS_INT scroll_conservatively, scroll_step;
10700 int temp_scroll_step;
10701 int last_line_misfit;
10703 struct window *w = XWINDOW (window);
10704 struct frame *f = XFRAME (w->frame);
10705 struct text_pos scroll_margin_pos;
10706 struct text_pos pos;
10707 struct text_pos startp;
10708 struct it it;
10709 Lisp_Object window_end;
10710 int this_scroll_margin;
10711 int dy = 0;
10712 int scroll_max;
10713 int rc;
10714 int amount_to_scroll = 0;
10715 Lisp_Object aggressive;
10716 int height;
10717 int end_scroll_margin;
10719 #if GLYPH_DEBUG
10720 debug_method_add (w, "try_scrolling");
10721 #endif
10723 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10725 /* Compute scroll margin height in pixels. We scroll when point is
10726 within this distance from the top or bottom of the window. */
10727 if (scroll_margin > 0)
10729 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
10730 this_scroll_margin *= CANON_Y_UNIT (f);
10732 else
10733 this_scroll_margin = 0;
10735 /* Compute how much we should try to scroll maximally to bring point
10736 into view. */
10737 if (scroll_step || scroll_conservatively || temp_scroll_step)
10738 scroll_max = max (scroll_step,
10739 max (scroll_conservatively, temp_scroll_step));
10740 else if (NUMBERP (current_buffer->scroll_down_aggressively)
10741 || NUMBERP (current_buffer->scroll_up_aggressively))
10742 /* We're trying to scroll because of aggressive scrolling
10743 but no scroll_step is set. Choose an arbitrary one. Maybe
10744 there should be a variable for this. */
10745 scroll_max = 10;
10746 else
10747 scroll_max = 0;
10748 scroll_max *= CANON_Y_UNIT (f);
10750 /* Decide whether we have to scroll down. Start at the window end
10751 and move this_scroll_margin up to find the position of the scroll
10752 margin. */
10753 window_end = Fwindow_end (window, Qt);
10755 too_near_end:
10757 CHARPOS (scroll_margin_pos) = XINT (window_end);
10758 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
10760 end_scroll_margin = this_scroll_margin + !!last_line_misfit;
10761 if (end_scroll_margin)
10763 start_display (&it, w, scroll_margin_pos);
10764 move_it_vertically (&it, - end_scroll_margin);
10765 scroll_margin_pos = it.current.pos;
10768 if (PT >= CHARPOS (scroll_margin_pos))
10770 int y0;
10772 /* Point is in the scroll margin at the bottom of the window, or
10773 below. Compute a new window start that makes point visible. */
10775 /* Compute the distance from the scroll margin to PT.
10776 Give up if the distance is greater than scroll_max. */
10777 start_display (&it, w, scroll_margin_pos);
10778 y0 = it.current_y;
10779 move_it_to (&it, PT, 0, it.last_visible_y, -1,
10780 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10782 /* To make point visible, we have to move the window start
10783 down so that the line the cursor is in is visible, which
10784 means we have to add in the height of the cursor line. */
10785 dy = line_bottom_y (&it) - y0;
10787 if (dy > scroll_max)
10788 return SCROLLING_FAILED;
10790 /* Move the window start down. If scrolling conservatively,
10791 move it just enough down to make point visible. If
10792 scroll_step is set, move it down by scroll_step. */
10793 start_display (&it, w, startp);
10795 if (scroll_conservatively)
10796 /* Set AMOUNT_TO_SCROLL to at least one line,
10797 and at most scroll_conservatively lines. */
10798 amount_to_scroll
10799 = min (max (dy, CANON_Y_UNIT (f)),
10800 CANON_Y_UNIT (f) * scroll_conservatively);
10801 else if (scroll_step || temp_scroll_step)
10802 amount_to_scroll = scroll_max;
10803 else
10805 aggressive = current_buffer->scroll_up_aggressively;
10806 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10807 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10808 if (NUMBERP (aggressive))
10809 amount_to_scroll = XFLOATINT (aggressive) * height;
10812 if (amount_to_scroll <= 0)
10813 return SCROLLING_FAILED;
10815 /* If moving by amount_to_scroll leaves STARTP unchanged,
10816 move it down one screen line. */
10818 move_it_vertically (&it, amount_to_scroll);
10819 if (CHARPOS (it.current.pos) == CHARPOS (startp))
10820 move_it_by_lines (&it, 1, 1);
10821 startp = it.current.pos;
10823 else
10825 /* See if point is inside the scroll margin at the top of the
10826 window. */
10827 scroll_margin_pos = startp;
10828 if (this_scroll_margin)
10830 start_display (&it, w, startp);
10831 move_it_vertically (&it, this_scroll_margin);
10832 scroll_margin_pos = it.current.pos;
10835 if (PT < CHARPOS (scroll_margin_pos))
10837 /* Point is in the scroll margin at the top of the window or
10838 above what is displayed in the window. */
10839 int y0;
10841 /* Compute the vertical distance from PT to the scroll
10842 margin position. Give up if distance is greater than
10843 scroll_max. */
10844 SET_TEXT_POS (pos, PT, PT_BYTE);
10845 start_display (&it, w, pos);
10846 y0 = it.current_y;
10847 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
10848 it.last_visible_y, -1,
10849 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10850 dy = it.current_y - y0;
10851 if (dy > scroll_max)
10852 return SCROLLING_FAILED;
10854 /* Compute new window start. */
10855 start_display (&it, w, startp);
10857 if (scroll_conservatively)
10858 amount_to_scroll =
10859 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
10860 else if (scroll_step || temp_scroll_step)
10861 amount_to_scroll = scroll_max;
10862 else
10864 aggressive = current_buffer->scroll_down_aggressively;
10865 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
10866 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
10867 if (NUMBERP (aggressive))
10868 amount_to_scroll = XFLOATINT (aggressive) * height;
10871 if (amount_to_scroll <= 0)
10872 return SCROLLING_FAILED;
10874 move_it_vertically (&it, - amount_to_scroll);
10875 startp = it.current.pos;
10879 /* Run window scroll functions. */
10880 startp = run_window_scroll_functions (window, startp);
10882 /* Display the window. Give up if new fonts are loaded, or if point
10883 doesn't appear. */
10884 if (!try_window (window, startp))
10885 rc = SCROLLING_NEED_LARGER_MATRICES;
10886 else if (w->cursor.vpos < 0)
10888 clear_glyph_matrix (w->desired_matrix);
10889 rc = SCROLLING_FAILED;
10891 else
10893 /* Maybe forget recorded base line for line number display. */
10894 if (!just_this_one_p
10895 || current_buffer->clip_changed
10896 || BEG_UNCHANGED < CHARPOS (startp))
10897 w->base_line_number = Qnil;
10899 /* If cursor ends up on a partially visible line,
10900 treat that as being off the bottom of the screen. */
10901 if (! make_cursor_line_fully_visible (w))
10903 clear_glyph_matrix (w->desired_matrix);
10904 last_line_misfit = 1;
10905 goto too_near_end;
10907 rc = SCROLLING_SUCCESS;
10910 return rc;
10914 /* Compute a suitable window start for window W if display of W starts
10915 on a continuation line. Value is non-zero if a new window start
10916 was computed.
10918 The new window start will be computed, based on W's width, starting
10919 from the start of the continued line. It is the start of the
10920 screen line with the minimum distance from the old start W->start. */
10922 static int
10923 compute_window_start_on_continuation_line (w)
10924 struct window *w;
10926 struct text_pos pos, start_pos;
10927 int window_start_changed_p = 0;
10929 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
10931 /* If window start is on a continuation line... Window start may be
10932 < BEGV in case there's invisible text at the start of the
10933 buffer (M-x rmail, for example). */
10934 if (CHARPOS (start_pos) > BEGV
10935 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
10937 struct it it;
10938 struct glyph_row *row;
10940 /* Handle the case that the window start is out of range. */
10941 if (CHARPOS (start_pos) < BEGV)
10942 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
10943 else if (CHARPOS (start_pos) > ZV)
10944 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
10946 /* Find the start of the continued line. This should be fast
10947 because scan_buffer is fast (newline cache). */
10948 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
10949 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
10950 row, DEFAULT_FACE_ID);
10951 reseat_at_previous_visible_line_start (&it);
10953 /* If the line start is "too far" away from the window start,
10954 say it takes too much time to compute a new window start. */
10955 if (CHARPOS (start_pos) - IT_CHARPOS (it)
10956 < XFASTINT (w->height) * XFASTINT (w->width))
10958 int min_distance, distance;
10960 /* Move forward by display lines to find the new window
10961 start. If window width was enlarged, the new start can
10962 be expected to be > the old start. If window width was
10963 decreased, the new window start will be < the old start.
10964 So, we're looking for the display line start with the
10965 minimum distance from the old window start. */
10966 pos = it.current.pos;
10967 min_distance = INFINITY;
10968 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
10969 distance < min_distance)
10971 min_distance = distance;
10972 pos = it.current.pos;
10973 move_it_by_lines (&it, 1, 0);
10976 /* Set the window start there. */
10977 SET_MARKER_FROM_TEXT_POS (w->start, pos);
10978 window_start_changed_p = 1;
10982 return window_start_changed_p;
10986 /* Try cursor movement in case text has not changed in window WINDOW,
10987 with window start STARTP. Value is
10989 CURSOR_MOVEMENT_SUCCESS if successful
10991 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
10993 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
10994 display. *SCROLL_STEP is set to 1, under certain circumstances, if
10995 we want to scroll as if scroll-step were set to 1. See the code.
10997 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
10998 which case we have to abort this redisplay, and adjust matrices
10999 first. */
11001 enum
11003 CURSOR_MOVEMENT_SUCCESS,
11004 CURSOR_MOVEMENT_CANNOT_BE_USED,
11005 CURSOR_MOVEMENT_MUST_SCROLL,
11006 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11009 static int
11010 try_cursor_movement (window, startp, scroll_step)
11011 Lisp_Object window;
11012 struct text_pos startp;
11013 int *scroll_step;
11015 struct window *w = XWINDOW (window);
11016 struct frame *f = XFRAME (w->frame);
11017 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11019 #if GLYPH_DEBUG
11020 if (inhibit_try_cursor_movement)
11021 return rc;
11022 #endif
11024 /* Handle case where text has not changed, only point, and it has
11025 not moved off the frame. */
11026 if (/* Point may be in this window. */
11027 PT >= CHARPOS (startp)
11028 /* Selective display hasn't changed. */
11029 && !current_buffer->clip_changed
11030 /* Function force-mode-line-update is used to force a thorough
11031 redisplay. It sets either windows_or_buffers_changed or
11032 update_mode_lines. So don't take a shortcut here for these
11033 cases. */
11034 && !update_mode_lines
11035 && !windows_or_buffers_changed
11036 && !cursor_type_changed
11037 /* Can't use this case if highlighting a region. When a
11038 region exists, cursor movement has to do more than just
11039 set the cursor. */
11040 && !(!NILP (Vtransient_mark_mode)
11041 && !NILP (current_buffer->mark_active))
11042 && NILP (w->region_showing)
11043 && NILP (Vshow_trailing_whitespace)
11044 /* Right after splitting windows, last_point may be nil. */
11045 && INTEGERP (w->last_point)
11046 /* This code is not used for mini-buffer for the sake of the case
11047 of redisplaying to replace an echo area message; since in
11048 that case the mini-buffer contents per se are usually
11049 unchanged. This code is of no real use in the mini-buffer
11050 since the handling of this_line_start_pos, etc., in redisplay
11051 handles the same cases. */
11052 && !EQ (window, minibuf_window)
11053 /* When splitting windows or for new windows, it happens that
11054 redisplay is called with a nil window_end_vpos or one being
11055 larger than the window. This should really be fixed in
11056 window.c. I don't have this on my list, now, so we do
11057 approximately the same as the old redisplay code. --gerd. */
11058 && INTEGERP (w->window_end_vpos)
11059 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11060 && (FRAME_WINDOW_P (f)
11061 || !MARKERP (Voverlay_arrow_position)
11062 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
11064 int this_scroll_margin;
11065 struct glyph_row *row = NULL;
11067 #if GLYPH_DEBUG
11068 debug_method_add (w, "cursor movement");
11069 #endif
11071 /* Scroll if point within this distance from the top or bottom
11072 of the window. This is a pixel value. */
11073 this_scroll_margin = max (0, scroll_margin);
11074 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
11075 this_scroll_margin *= CANON_Y_UNIT (f);
11077 /* Start with the row the cursor was displayed during the last
11078 not paused redisplay. Give up if that row is not valid. */
11079 if (w->last_cursor.vpos < 0
11080 || w->last_cursor.vpos >= w->current_matrix->nrows)
11081 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11082 else
11084 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11085 if (row->mode_line_p)
11086 ++row;
11087 if (!row->enabled_p)
11088 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11091 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11093 int scroll_p = 0;
11094 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11096 if (PT > XFASTINT (w->last_point))
11098 /* Point has moved forward. */
11099 while (MATRIX_ROW_END_CHARPOS (row) < PT
11100 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11102 xassert (row->enabled_p);
11103 ++row;
11106 /* The end position of a row equals the start position
11107 of the next row. If PT is there, we would rather
11108 display it in the next line. */
11109 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11110 && MATRIX_ROW_END_CHARPOS (row) == PT
11111 && !cursor_row_p (w, row))
11112 ++row;
11114 /* If within the scroll margin, scroll. Note that
11115 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11116 the next line would be drawn, and that
11117 this_scroll_margin can be zero. */
11118 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11119 || PT > MATRIX_ROW_END_CHARPOS (row)
11120 /* Line is completely visible last line in window
11121 and PT is to be set in the next line. */
11122 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11123 && PT == MATRIX_ROW_END_CHARPOS (row)
11124 && !row->ends_at_zv_p
11125 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11126 scroll_p = 1;
11128 else if (PT < XFASTINT (w->last_point))
11130 /* Cursor has to be moved backward. Note that PT >=
11131 CHARPOS (startp) because of the outer
11132 if-statement. */
11133 while (!row->mode_line_p
11134 && (MATRIX_ROW_START_CHARPOS (row) > PT
11135 || (MATRIX_ROW_START_CHARPOS (row) == PT
11136 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11137 && (row->y > this_scroll_margin
11138 || CHARPOS (startp) == BEGV))
11140 xassert (row->enabled_p);
11141 --row;
11144 /* Consider the following case: Window starts at BEGV,
11145 there is invisible, intangible text at BEGV, so that
11146 display starts at some point START > BEGV. It can
11147 happen that we are called with PT somewhere between
11148 BEGV and START. Try to handle that case. */
11149 if (row < w->current_matrix->rows
11150 || row->mode_line_p)
11152 row = w->current_matrix->rows;
11153 if (row->mode_line_p)
11154 ++row;
11157 /* Due to newlines in overlay strings, we may have to
11158 skip forward over overlay strings. */
11159 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11160 && MATRIX_ROW_END_CHARPOS (row) == PT
11161 && !cursor_row_p (w, row))
11162 ++row;
11164 /* If within the scroll margin, scroll. */
11165 if (row->y < this_scroll_margin
11166 && CHARPOS (startp) != BEGV)
11167 scroll_p = 1;
11170 if (PT < MATRIX_ROW_START_CHARPOS (row)
11171 || PT > MATRIX_ROW_END_CHARPOS (row))
11173 /* if PT is not in the glyph row, give up. */
11174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11176 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
11178 if (PT == MATRIX_ROW_END_CHARPOS (row)
11179 && !row->ends_at_zv_p
11180 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11181 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11182 else if (row->height > window_box_height (w))
11184 /* If we end up in a partially visible line, let's
11185 make it fully visible, except when it's taller
11186 than the window, in which case we can't do much
11187 about it. */
11188 *scroll_step = 1;
11189 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11191 else
11193 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11194 if (!make_cursor_line_fully_visible (w))
11195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11196 else
11197 rc = CURSOR_MOVEMENT_SUCCESS;
11200 else if (scroll_p)
11201 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11202 else
11204 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11205 rc = CURSOR_MOVEMENT_SUCCESS;
11210 return rc;
11213 void
11214 set_vertical_scroll_bar (w)
11215 struct window *w;
11217 int start, end, whole;
11219 /* Calculate the start and end positions for the current window.
11220 At some point, it would be nice to choose between scrollbars
11221 which reflect the whole buffer size, with special markers
11222 indicating narrowing, and scrollbars which reflect only the
11223 visible region.
11225 Note that mini-buffers sometimes aren't displaying any text. */
11226 if (!MINI_WINDOW_P (w)
11227 || (w == XWINDOW (minibuf_window)
11228 && NILP (echo_area_buffer[0])))
11230 struct buffer *buf = XBUFFER (w->buffer);
11231 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11232 start = marker_position (w->start) - BUF_BEGV (buf);
11233 /* I don't think this is guaranteed to be right. For the
11234 moment, we'll pretend it is. */
11235 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11237 if (end < start)
11238 end = start;
11239 if (whole < (end - start))
11240 whole = end - start;
11242 else
11243 start = end = whole = 0;
11245 /* Indicate what this scroll bar ought to be displaying now. */
11246 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11249 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11250 selected_window is redisplayed.
11252 We can return without actually redisplaying the window if
11253 fonts_changed_p is nonzero. In that case, redisplay_internal will
11254 retry. */
11256 static void
11257 redisplay_window (window, just_this_one_p)
11258 Lisp_Object window;
11259 int just_this_one_p;
11261 struct window *w = XWINDOW (window);
11262 struct frame *f = XFRAME (w->frame);
11263 struct buffer *buffer = XBUFFER (w->buffer);
11264 struct buffer *old = current_buffer;
11265 struct text_pos lpoint, opoint, startp;
11266 int update_mode_line;
11267 int tem;
11268 struct it it;
11269 /* Record it now because it's overwritten. */
11270 int current_matrix_up_to_date_p = 0;
11271 /* This is less strict than current_matrix_up_to_date_p.
11272 It indictes that the buffer contents and narrowing are unchanged. */
11273 int buffer_unchanged_p = 0;
11274 int temp_scroll_step = 0;
11275 int count = SPECPDL_INDEX ();
11276 int rc;
11277 int centering_position;
11278 int last_line_misfit = 0;
11280 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11281 opoint = lpoint;
11283 /* W must be a leaf window here. */
11284 xassert (!NILP (w->buffer));
11285 #if GLYPH_DEBUG
11286 *w->desired_matrix->method = 0;
11287 #endif
11289 specbind (Qinhibit_point_motion_hooks, Qt);
11291 reconsider_clip_changes (w, buffer);
11293 /* Has the mode line to be updated? */
11294 update_mode_line = (!NILP (w->update_mode_line)
11295 || update_mode_lines
11296 || buffer->clip_changed
11297 || buffer->prevent_redisplay_optimizations_p);
11299 if (MINI_WINDOW_P (w))
11301 if (w == XWINDOW (echo_area_window)
11302 && !NILP (echo_area_buffer[0]))
11304 if (update_mode_line)
11305 /* We may have to update a tty frame's menu bar or a
11306 tool-bar. Example `M-x C-h C-h C-g'. */
11307 goto finish_menu_bars;
11308 else
11309 /* We've already displayed the echo area glyphs in this window. */
11310 goto finish_scroll_bars;
11312 else if ((w != XWINDOW (minibuf_window)
11313 || minibuf_level == 0)
11314 /* When buffer is nonempty, redisplay window normally. */
11315 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11316 /* Quail displays non-mini buffers in minibuffer window.
11317 In that case, redisplay the window normally. */
11318 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11320 /* W is a mini-buffer window, but it's not active, so clear
11321 it. */
11322 int yb = window_text_bottom_y (w);
11323 struct glyph_row *row;
11324 int y;
11326 for (y = 0, row = w->desired_matrix->rows;
11327 y < yb;
11328 y += row->height, ++row)
11329 blank_row (w, row, y);
11330 goto finish_scroll_bars;
11333 clear_glyph_matrix (w->desired_matrix);
11336 /* Otherwise set up data on this window; select its buffer and point
11337 value. */
11338 /* Really select the buffer, for the sake of buffer-local
11339 variables. */
11340 set_buffer_internal_1 (XBUFFER (w->buffer));
11341 SET_TEXT_POS (opoint, PT, PT_BYTE);
11343 current_matrix_up_to_date_p
11344 = (!NILP (w->window_end_valid)
11345 && !current_buffer->clip_changed
11346 && !current_buffer->prevent_redisplay_optimizations_p
11347 && XFASTINT (w->last_modified) >= MODIFF
11348 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11350 buffer_unchanged_p
11351 = (!NILP (w->window_end_valid)
11352 && !current_buffer->clip_changed
11353 && XFASTINT (w->last_modified) >= MODIFF
11354 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11356 /* When windows_or_buffers_changed is non-zero, we can't rely on
11357 the window end being valid, so set it to nil there. */
11358 if (windows_or_buffers_changed)
11360 /* If window starts on a continuation line, maybe adjust the
11361 window start in case the window's width changed. */
11362 if (XMARKER (w->start)->buffer == current_buffer)
11363 compute_window_start_on_continuation_line (w);
11365 w->window_end_valid = Qnil;
11368 /* Some sanity checks. */
11369 CHECK_WINDOW_END (w);
11370 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11371 abort ();
11372 if (BYTEPOS (opoint) < CHARPOS (opoint))
11373 abort ();
11375 /* If %c is in mode line, update it if needed. */
11376 if (!NILP (w->column_number_displayed)
11377 /* This alternative quickly identifies a common case
11378 where no change is needed. */
11379 && !(PT == XFASTINT (w->last_point)
11380 && XFASTINT (w->last_modified) >= MODIFF
11381 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11382 && (XFASTINT (w->column_number_displayed)
11383 != (int) current_column ())) /* iftc */
11384 update_mode_line = 1;
11386 /* Count number of windows showing the selected buffer. An indirect
11387 buffer counts as its base buffer. */
11388 if (!just_this_one_p)
11390 struct buffer *current_base, *window_base;
11391 current_base = current_buffer;
11392 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11393 if (current_base->base_buffer)
11394 current_base = current_base->base_buffer;
11395 if (window_base->base_buffer)
11396 window_base = window_base->base_buffer;
11397 if (current_base == window_base)
11398 buffer_shared++;
11401 /* Point refers normally to the selected window. For any other
11402 window, set up appropriate value. */
11403 if (!EQ (window, selected_window))
11405 int new_pt = XMARKER (w->pointm)->charpos;
11406 int new_pt_byte = marker_byte_position (w->pointm);
11407 if (new_pt < BEGV)
11409 new_pt = BEGV;
11410 new_pt_byte = BEGV_BYTE;
11411 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11413 else if (new_pt > (ZV - 1))
11415 new_pt = ZV;
11416 new_pt_byte = ZV_BYTE;
11417 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11420 /* We don't use SET_PT so that the point-motion hooks don't run. */
11421 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11424 /* If any of the character widths specified in the display table
11425 have changed, invalidate the width run cache. It's true that
11426 this may be a bit late to catch such changes, but the rest of
11427 redisplay goes (non-fatally) haywire when the display table is
11428 changed, so why should we worry about doing any better? */
11429 if (current_buffer->width_run_cache)
11431 struct Lisp_Char_Table *disptab = buffer_display_table ();
11433 if (! disptab_matches_widthtab (disptab,
11434 XVECTOR (current_buffer->width_table)))
11436 invalidate_region_cache (current_buffer,
11437 current_buffer->width_run_cache,
11438 BEG, Z);
11439 recompute_width_table (current_buffer, disptab);
11443 /* If window-start is screwed up, choose a new one. */
11444 if (XMARKER (w->start)->buffer != current_buffer)
11445 goto recenter;
11447 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11449 /* If someone specified a new starting point but did not insist,
11450 check whether it can be used. */
11451 if (!NILP (w->optional_new_start)
11452 && CHARPOS (startp) >= BEGV
11453 && CHARPOS (startp) <= ZV)
11455 w->optional_new_start = Qnil;
11456 start_display (&it, w, startp);
11457 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11458 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11459 if (IT_CHARPOS (it) == PT)
11460 w->force_start = Qt;
11463 /* Handle case where place to start displaying has been specified,
11464 unless the specified location is outside the accessible range. */
11465 if (!NILP (w->force_start)
11466 || w->frozen_window_start_p)
11468 /* We set this later on if we have to adjust point. */
11469 int new_vpos = -1;
11471 w->force_start = Qnil;
11472 w->vscroll = 0;
11473 w->window_end_valid = Qnil;
11475 /* Forget any recorded base line for line number display. */
11476 if (!buffer_unchanged_p)
11477 w->base_line_number = Qnil;
11479 /* Redisplay the mode line. Select the buffer properly for that.
11480 Also, run the hook window-scroll-functions
11481 because we have scrolled. */
11482 /* Note, we do this after clearing force_start because
11483 if there's an error, it is better to forget about force_start
11484 than to get into an infinite loop calling the hook functions
11485 and having them get more errors. */
11486 if (!update_mode_line
11487 || ! NILP (Vwindow_scroll_functions))
11489 update_mode_line = 1;
11490 w->update_mode_line = Qt;
11491 startp = run_window_scroll_functions (window, startp);
11494 w->last_modified = make_number (0);
11495 w->last_overlay_modified = make_number (0);
11496 if (CHARPOS (startp) < BEGV)
11497 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11498 else if (CHARPOS (startp) > ZV)
11499 SET_TEXT_POS (startp, ZV, ZV_BYTE);
11501 /* Redisplay, then check if cursor has been set during the
11502 redisplay. Give up if new fonts were loaded. */
11503 if (!try_window (window, startp))
11505 w->force_start = Qt;
11506 clear_glyph_matrix (w->desired_matrix);
11507 goto need_larger_matrices;
11510 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
11512 /* If point does not appear, try to move point so it does
11513 appear. The desired matrix has been built above, so we
11514 can use it here. */
11515 new_vpos = window_box_height (w) / 2;
11518 if (!make_cursor_line_fully_visible (w))
11520 /* Point does appear, but on a line partly visible at end of window.
11521 Move it back to a fully-visible line. */
11522 new_vpos = window_box_height (w);
11525 /* If we need to move point for either of the above reasons,
11526 now actually do it. */
11527 if (new_vpos >= 0)
11529 struct glyph_row *row;
11531 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
11532 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
11533 ++row;
11535 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
11536 MATRIX_ROW_START_BYTEPOS (row));
11538 if (w != XWINDOW (selected_window))
11539 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
11540 else if (current_buffer == old)
11541 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11543 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
11545 /* If we are highlighting the region, then we just changed
11546 the region, so redisplay to show it. */
11547 if (!NILP (Vtransient_mark_mode)
11548 && !NILP (current_buffer->mark_active))
11550 clear_glyph_matrix (w->desired_matrix);
11551 if (!try_window (window, startp))
11552 goto need_larger_matrices;
11556 #if GLYPH_DEBUG
11557 debug_method_add (w, "forced window start");
11558 #endif
11559 goto done;
11562 /* Handle case where text has not changed, only point, and it has
11563 not moved off the frame, and we are not retrying after hscroll.
11564 (current_matrix_up_to_date_p is nonzero when retrying.) */
11565 if (current_matrix_up_to_date_p
11566 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
11567 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
11569 switch (rc)
11571 case CURSOR_MOVEMENT_SUCCESS:
11572 goto done;
11574 #if 0 /* try_cursor_movement never returns this value. */
11575 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
11576 goto need_larger_matrices;
11577 #endif
11579 case CURSOR_MOVEMENT_MUST_SCROLL:
11580 goto try_to_scroll;
11582 default:
11583 abort ();
11586 /* If current starting point was originally the beginning of a line
11587 but no longer is, find a new starting point. */
11588 else if (!NILP (w->start_at_line_beg)
11589 && !(CHARPOS (startp) <= BEGV
11590 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
11592 #if GLYPH_DEBUG
11593 debug_method_add (w, "recenter 1");
11594 #endif
11595 goto recenter;
11598 /* Try scrolling with try_window_id. Value is > 0 if update has
11599 been done, it is -1 if we know that the same window start will
11600 not work. It is 0 if unsuccessful for some other reason. */
11601 else if ((tem = try_window_id (w)) != 0)
11603 #if GLYPH_DEBUG
11604 debug_method_add (w, "try_window_id %d", tem);
11605 #endif
11607 if (fonts_changed_p)
11608 goto need_larger_matrices;
11609 if (tem > 0)
11610 goto done;
11612 /* Otherwise try_window_id has returned -1 which means that we
11613 don't want the alternative below this comment to execute. */
11615 else if (CHARPOS (startp) >= BEGV
11616 && CHARPOS (startp) <= ZV
11617 && PT >= CHARPOS (startp)
11618 && (CHARPOS (startp) < ZV
11619 /* Avoid starting at end of buffer. */
11620 || CHARPOS (startp) == BEGV
11621 || (XFASTINT (w->last_modified) >= MODIFF
11622 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
11624 #if GLYPH_DEBUG
11625 debug_method_add (w, "same window start");
11626 #endif
11628 /* Try to redisplay starting at same place as before.
11629 If point has not moved off frame, accept the results. */
11630 if (!current_matrix_up_to_date_p
11631 /* Don't use try_window_reusing_current_matrix in this case
11632 because a window scroll function can have changed the
11633 buffer. */
11634 || !NILP (Vwindow_scroll_functions)
11635 || MINI_WINDOW_P (w)
11636 || !try_window_reusing_current_matrix (w))
11638 IF_DEBUG (debug_method_add (w, "1"));
11639 try_window (window, startp);
11642 if (fonts_changed_p)
11643 goto need_larger_matrices;
11645 if (w->cursor.vpos >= 0)
11647 if (!just_this_one_p
11648 || current_buffer->clip_changed
11649 || BEG_UNCHANGED < CHARPOS (startp))
11650 /* Forget any recorded base line for line number display. */
11651 w->base_line_number = Qnil;
11653 if (!make_cursor_line_fully_visible (w))
11655 clear_glyph_matrix (w->desired_matrix);
11656 last_line_misfit = 1;
11658 /* Drop through and scroll. */
11659 else
11660 goto done;
11662 else
11663 clear_glyph_matrix (w->desired_matrix);
11666 try_to_scroll:
11668 w->last_modified = make_number (0);
11669 w->last_overlay_modified = make_number (0);
11671 /* Redisplay the mode line. Select the buffer properly for that. */
11672 if (!update_mode_line)
11674 update_mode_line = 1;
11675 w->update_mode_line = Qt;
11678 /* Try to scroll by specified few lines. */
11679 if ((scroll_conservatively
11680 || scroll_step
11681 || temp_scroll_step
11682 || NUMBERP (current_buffer->scroll_up_aggressively)
11683 || NUMBERP (current_buffer->scroll_down_aggressively))
11684 && !current_buffer->clip_changed
11685 && CHARPOS (startp) >= BEGV
11686 && CHARPOS (startp) <= ZV)
11688 /* The function returns -1 if new fonts were loaded, 1 if
11689 successful, 0 if not successful. */
11690 int rc = try_scrolling (window, just_this_one_p,
11691 scroll_conservatively,
11692 scroll_step,
11693 temp_scroll_step, last_line_misfit);
11694 switch (rc)
11696 case SCROLLING_SUCCESS:
11697 goto done;
11699 case SCROLLING_NEED_LARGER_MATRICES:
11700 goto need_larger_matrices;
11702 case SCROLLING_FAILED:
11703 break;
11705 default:
11706 abort ();
11710 /* Finally, just choose place to start which centers point */
11712 recenter:
11713 centering_position = window_box_height (w) / 2;
11715 point_at_top:
11716 /* Jump here with centering_position already set to 0. */
11718 #if GLYPH_DEBUG
11719 debug_method_add (w, "recenter");
11720 #endif
11722 /* w->vscroll = 0; */
11724 /* Forget any previously recorded base line for line number display. */
11725 if (!buffer_unchanged_p)
11726 w->base_line_number = Qnil;
11728 /* Move backward half the height of the window. */
11729 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11730 it.current_y = it.last_visible_y;
11731 move_it_vertically_backward (&it, centering_position);
11732 xassert (IT_CHARPOS (it) >= BEGV);
11734 /* The function move_it_vertically_backward may move over more
11735 than the specified y-distance. If it->w is small, e.g. a
11736 mini-buffer window, we may end up in front of the window's
11737 display area. Start displaying at the start of the line
11738 containing PT in this case. */
11739 if (it.current_y <= 0)
11741 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
11742 move_it_vertically (&it, 0);
11743 xassert (IT_CHARPOS (it) <= PT);
11744 it.current_y = 0;
11747 it.current_x = it.hpos = 0;
11749 /* Set startp here explicitly in case that helps avoid an infinite loop
11750 in case the window-scroll-functions functions get errors. */
11751 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
11753 /* Run scroll hooks. */
11754 startp = run_window_scroll_functions (window, it.current.pos);
11756 /* Redisplay the window. */
11757 if (!current_matrix_up_to_date_p
11758 || windows_or_buffers_changed
11759 || cursor_type_changed
11760 /* Don't use try_window_reusing_current_matrix in this case
11761 because it can have changed the buffer. */
11762 || !NILP (Vwindow_scroll_functions)
11763 || !just_this_one_p
11764 || MINI_WINDOW_P (w)
11765 || !try_window_reusing_current_matrix (w))
11766 try_window (window, startp);
11768 /* If new fonts have been loaded (due to fontsets), give up. We
11769 have to start a new redisplay since we need to re-adjust glyph
11770 matrices. */
11771 if (fonts_changed_p)
11772 goto need_larger_matrices;
11774 /* If cursor did not appear assume that the middle of the window is
11775 in the first line of the window. Do it again with the next line.
11776 (Imagine a window of height 100, displaying two lines of height
11777 60. Moving back 50 from it->last_visible_y will end in the first
11778 line.) */
11779 if (w->cursor.vpos < 0)
11781 if (!NILP (w->window_end_valid)
11782 && PT >= Z - XFASTINT (w->window_end_pos))
11784 clear_glyph_matrix (w->desired_matrix);
11785 move_it_by_lines (&it, 1, 0);
11786 try_window (window, it.current.pos);
11788 else if (PT < IT_CHARPOS (it))
11790 clear_glyph_matrix (w->desired_matrix);
11791 move_it_by_lines (&it, -1, 0);
11792 try_window (window, it.current.pos);
11794 else
11796 /* Not much we can do about it. */
11800 /* Consider the following case: Window starts at BEGV, there is
11801 invisible, intangible text at BEGV, so that display starts at
11802 some point START > BEGV. It can happen that we are called with
11803 PT somewhere between BEGV and START. Try to handle that case. */
11804 if (w->cursor.vpos < 0)
11806 struct glyph_row *row = w->current_matrix->rows;
11807 if (row->mode_line_p)
11808 ++row;
11809 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11812 if (!make_cursor_line_fully_visible (w))
11814 /* If vscroll is enabled, disable it and try again. */
11815 if (w->vscroll)
11817 w->vscroll = 0;
11818 clear_glyph_matrix (w->desired_matrix);
11819 goto recenter;
11822 /* If centering point failed to make the whole line visible,
11823 put point at the top instead. That has to make the whole line
11824 visible, if it can be done. */
11825 centering_position = 0;
11826 goto point_at_top;
11829 done:
11831 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11832 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
11833 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
11834 ? Qt : Qnil);
11836 /* Display the mode line, if we must. */
11837 if ((update_mode_line
11838 /* If window not full width, must redo its mode line
11839 if (a) the window to its side is being redone and
11840 (b) we do a frame-based redisplay. This is a consequence
11841 of how inverted lines are drawn in frame-based redisplay. */
11842 || (!just_this_one_p
11843 && !FRAME_WINDOW_P (f)
11844 && !WINDOW_FULL_WIDTH_P (w))
11845 /* Line number to display. */
11846 || INTEGERP (w->base_line_pos)
11847 /* Column number is displayed and different from the one displayed. */
11848 || (!NILP (w->column_number_displayed)
11849 && (XFASTINT (w->column_number_displayed)
11850 != (int) current_column ()))) /* iftc */
11851 /* This means that the window has a mode line. */
11852 && (WINDOW_WANTS_MODELINE_P (w)
11853 || WINDOW_WANTS_HEADER_LINE_P (w)))
11855 display_mode_lines (w);
11857 /* If mode line height has changed, arrange for a thorough
11858 immediate redisplay using the correct mode line height. */
11859 if (WINDOW_WANTS_MODELINE_P (w)
11860 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
11862 fonts_changed_p = 1;
11863 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
11864 = DESIRED_MODE_LINE_HEIGHT (w);
11867 /* If top line height has changed, arrange for a thorough
11868 immediate redisplay using the correct mode line height. */
11869 if (WINDOW_WANTS_HEADER_LINE_P (w)
11870 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
11872 fonts_changed_p = 1;
11873 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
11874 = DESIRED_HEADER_LINE_HEIGHT (w);
11877 if (fonts_changed_p)
11878 goto need_larger_matrices;
11881 if (!line_number_displayed
11882 && !BUFFERP (w->base_line_pos))
11884 w->base_line_pos = Qnil;
11885 w->base_line_number = Qnil;
11888 finish_menu_bars:
11890 /* When we reach a frame's selected window, redo the frame's menu bar. */
11891 if (update_mode_line
11892 && EQ (FRAME_SELECTED_WINDOW (f), window))
11894 int redisplay_menu_p = 0;
11895 int redisplay_tool_bar_p = 0;
11897 if (FRAME_WINDOW_P (f))
11899 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
11900 || defined (USE_GTK)
11901 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
11902 #else
11903 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11904 #endif
11906 else
11907 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
11909 if (redisplay_menu_p)
11910 display_menu_bar (w);
11912 #ifdef HAVE_WINDOW_SYSTEM
11913 #ifdef USE_GTK
11914 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
11915 #else
11916 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
11917 && (FRAME_TOOL_BAR_LINES (f) > 0
11918 || auto_resize_tool_bars_p);
11920 #endif
11922 if (redisplay_tool_bar_p)
11923 redisplay_tool_bar (f);
11924 #endif
11927 /* We go to this label, with fonts_changed_p nonzero,
11928 if it is necessary to try again using larger glyph matrices.
11929 We have to redeem the scroll bar even in this case,
11930 because the loop in redisplay_internal expects that. */
11931 need_larger_matrices:
11933 finish_scroll_bars:
11935 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11937 /* Set the thumb's position and size. */
11938 set_vertical_scroll_bar (w);
11940 /* Note that we actually used the scroll bar attached to this
11941 window, so it shouldn't be deleted at the end of redisplay. */
11942 redeem_scroll_bar_hook (w);
11945 /* Restore current_buffer and value of point in it. */
11946 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
11947 set_buffer_internal_1 (old);
11948 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
11950 unbind_to (count, Qnil);
11954 /* Build the complete desired matrix of WINDOW with a window start
11955 buffer position POS. Value is non-zero if successful. It is zero
11956 if fonts were loaded during redisplay which makes re-adjusting
11957 glyph matrices necessary. */
11960 try_window (window, pos)
11961 Lisp_Object window;
11962 struct text_pos pos;
11964 struct window *w = XWINDOW (window);
11965 struct it it;
11966 struct glyph_row *last_text_row = NULL;
11968 /* Make POS the new window start. */
11969 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
11971 /* Mark cursor position as unknown. No overlay arrow seen. */
11972 w->cursor.vpos = -1;
11973 overlay_arrow_seen = 0;
11975 /* Initialize iterator and info to start at POS. */
11976 start_display (&it, w, pos);
11978 /* Display all lines of W. */
11979 while (it.current_y < it.last_visible_y)
11981 if (display_line (&it))
11982 last_text_row = it.glyph_row - 1;
11983 if (fonts_changed_p)
11984 return 0;
11987 /* If bottom moved off end of frame, change mode line percentage. */
11988 if (XFASTINT (w->window_end_pos) <= 0
11989 && Z != IT_CHARPOS (it))
11990 w->update_mode_line = Qt;
11992 /* Set window_end_pos to the offset of the last character displayed
11993 on the window from the end of current_buffer. Set
11994 window_end_vpos to its row number. */
11995 if (last_text_row)
11997 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
11998 w->window_end_bytepos
11999 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12000 w->window_end_pos
12001 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12002 w->window_end_vpos
12003 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12004 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12005 ->displays_text_p);
12007 else
12009 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12010 w->window_end_pos = make_number (Z - ZV);
12011 w->window_end_vpos = make_number (0);
12014 /* But that is not valid info until redisplay finishes. */
12015 w->window_end_valid = Qnil;
12016 return 1;
12021 /************************************************************************
12022 Window redisplay reusing current matrix when buffer has not changed
12023 ************************************************************************/
12025 /* Try redisplay of window W showing an unchanged buffer with a
12026 different window start than the last time it was displayed by
12027 reusing its current matrix. Value is non-zero if successful.
12028 W->start is the new window start. */
12030 static int
12031 try_window_reusing_current_matrix (w)
12032 struct window *w;
12034 struct frame *f = XFRAME (w->frame);
12035 struct glyph_row *row, *bottom_row;
12036 struct it it;
12037 struct run run;
12038 struct text_pos start, new_start;
12039 int nrows_scrolled, i;
12040 struct glyph_row *last_text_row;
12041 struct glyph_row *last_reused_text_row;
12042 struct glyph_row *start_row;
12043 int start_vpos, min_y, max_y;
12045 #if GLYPH_DEBUG
12046 if (inhibit_try_window_reusing)
12047 return 0;
12048 #endif
12050 if (/* This function doesn't handle terminal frames. */
12051 !FRAME_WINDOW_P (f)
12052 /* Don't try to reuse the display if windows have been split
12053 or such. */
12054 || windows_or_buffers_changed
12055 || cursor_type_changed)
12056 return 0;
12058 /* Can't do this if region may have changed. */
12059 if ((!NILP (Vtransient_mark_mode)
12060 && !NILP (current_buffer->mark_active))
12061 || !NILP (w->region_showing)
12062 || !NILP (Vshow_trailing_whitespace))
12063 return 0;
12065 /* If top-line visibility has changed, give up. */
12066 if (WINDOW_WANTS_HEADER_LINE_P (w)
12067 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12068 return 0;
12070 /* Give up if old or new display is scrolled vertically. We could
12071 make this function handle this, but right now it doesn't. */
12072 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12073 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
12074 return 0;
12076 /* The variable new_start now holds the new window start. The old
12077 start `start' can be determined from the current matrix. */
12078 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12079 start = start_row->start.pos;
12080 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12082 /* Clear the desired matrix for the display below. */
12083 clear_glyph_matrix (w->desired_matrix);
12085 if (CHARPOS (new_start) <= CHARPOS (start))
12087 int first_row_y;
12089 /* Don't use this method if the display starts with an ellipsis
12090 displayed for invisible text. It's not easy to handle that case
12091 below, and it's certainly not worth the effort since this is
12092 not a frequent case. */
12093 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12094 return 0;
12096 IF_DEBUG (debug_method_add (w, "twu1"));
12098 /* Display up to a row that can be reused. The variable
12099 last_text_row is set to the last row displayed that displays
12100 text. Note that it.vpos == 0 if or if not there is a
12101 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12102 start_display (&it, w, new_start);
12103 first_row_y = it.current_y;
12104 w->cursor.vpos = -1;
12105 last_text_row = last_reused_text_row = NULL;
12107 while (it.current_y < it.last_visible_y
12108 && IT_CHARPOS (it) < CHARPOS (start)
12109 && !fonts_changed_p)
12110 if (display_line (&it))
12111 last_text_row = it.glyph_row - 1;
12113 /* A value of current_y < last_visible_y means that we stopped
12114 at the previous window start, which in turn means that we
12115 have at least one reusable row. */
12116 if (it.current_y < it.last_visible_y)
12118 /* IT.vpos always starts from 0; it counts text lines. */
12119 nrows_scrolled = it.vpos;
12121 /* Find PT if not already found in the lines displayed. */
12122 if (w->cursor.vpos < 0)
12124 int dy = it.current_y - first_row_y;
12126 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12127 row = row_containing_pos (w, PT, row, NULL, dy);
12128 if (row)
12129 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12130 dy, nrows_scrolled);
12131 else
12133 clear_glyph_matrix (w->desired_matrix);
12134 return 0;
12138 /* Scroll the display. Do it before the current matrix is
12139 changed. The problem here is that update has not yet
12140 run, i.e. part of the current matrix is not up to date.
12141 scroll_run_hook will clear the cursor, and use the
12142 current matrix to get the height of the row the cursor is
12143 in. */
12144 run.current_y = first_row_y;
12145 run.desired_y = it.current_y;
12146 run.height = it.last_visible_y - it.current_y;
12148 if (run.height > 0 && run.current_y != run.desired_y)
12150 update_begin (f);
12151 rif->update_window_begin_hook (w);
12152 rif->clear_window_mouse_face (w);
12153 rif->scroll_run_hook (w, &run);
12154 rif->update_window_end_hook (w, 0, 0);
12155 update_end (f);
12158 /* Shift current matrix down by nrows_scrolled lines. */
12159 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12160 rotate_matrix (w->current_matrix,
12161 start_vpos,
12162 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12163 nrows_scrolled);
12165 /* Disable lines that must be updated. */
12166 for (i = 0; i < it.vpos; ++i)
12167 (start_row + i)->enabled_p = 0;
12169 /* Re-compute Y positions. */
12170 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12171 max_y = it.last_visible_y;
12172 for (row = start_row + nrows_scrolled;
12173 row < bottom_row;
12174 ++row)
12176 row->y = it.current_y;
12177 row->visible_height = row->height;
12179 if (row->y < min_y)
12180 row->visible_height -= min_y - row->y;
12181 if (row->y + row->height > max_y)
12182 row->visible_height -= row->y + row->height - max_y;
12184 it.current_y += row->height;
12186 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12187 last_reused_text_row = row;
12188 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12189 break;
12192 /* Disable lines in the current matrix which are now
12193 below the window. */
12194 for (++row; row < bottom_row; ++row)
12195 row->enabled_p = 0;
12198 /* Update window_end_pos etc.; last_reused_text_row is the last
12199 reused row from the current matrix containing text, if any.
12200 The value of last_text_row is the last displayed line
12201 containing text. */
12202 if (last_reused_text_row)
12204 w->window_end_bytepos
12205 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12206 w->window_end_pos
12207 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12208 w->window_end_vpos
12209 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12210 w->current_matrix));
12212 else if (last_text_row)
12214 w->window_end_bytepos
12215 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12216 w->window_end_pos
12217 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12218 w->window_end_vpos
12219 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12221 else
12223 /* This window must be completely empty. */
12224 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12225 w->window_end_pos = make_number (Z - ZV);
12226 w->window_end_vpos = make_number (0);
12228 w->window_end_valid = Qnil;
12230 /* Update hint: don't try scrolling again in update_window. */
12231 w->desired_matrix->no_scrolling_p = 1;
12233 #if GLYPH_DEBUG
12234 debug_method_add (w, "try_window_reusing_current_matrix 1");
12235 #endif
12236 return 1;
12238 else if (CHARPOS (new_start) > CHARPOS (start))
12240 struct glyph_row *pt_row, *row;
12241 struct glyph_row *first_reusable_row;
12242 struct glyph_row *first_row_to_display;
12243 int dy;
12244 int yb = window_text_bottom_y (w);
12246 /* Find the row starting at new_start, if there is one. Don't
12247 reuse a partially visible line at the end. */
12248 first_reusable_row = start_row;
12249 while (first_reusable_row->enabled_p
12250 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12251 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12252 < CHARPOS (new_start)))
12253 ++first_reusable_row;
12255 /* Give up if there is no row to reuse. */
12256 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12257 || !first_reusable_row->enabled_p
12258 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12259 != CHARPOS (new_start)))
12260 return 0;
12262 /* We can reuse fully visible rows beginning with
12263 first_reusable_row to the end of the window. Set
12264 first_row_to_display to the first row that cannot be reused.
12265 Set pt_row to the row containing point, if there is any. */
12266 pt_row = NULL;
12267 for (first_row_to_display = first_reusable_row;
12268 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12269 ++first_row_to_display)
12271 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12272 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12273 pt_row = first_row_to_display;
12276 /* Start displaying at the start of first_row_to_display. */
12277 xassert (first_row_to_display->y < yb);
12278 init_to_row_start (&it, w, first_row_to_display);
12280 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12281 - start_vpos);
12282 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12283 - nrows_scrolled);
12284 it.current_y = (first_row_to_display->y - first_reusable_row->y
12285 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
12287 /* Display lines beginning with first_row_to_display in the
12288 desired matrix. Set last_text_row to the last row displayed
12289 that displays text. */
12290 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12291 if (pt_row == NULL)
12292 w->cursor.vpos = -1;
12293 last_text_row = NULL;
12294 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12295 if (display_line (&it))
12296 last_text_row = it.glyph_row - 1;
12298 /* Give up If point isn't in a row displayed or reused. */
12299 if (w->cursor.vpos < 0)
12301 clear_glyph_matrix (w->desired_matrix);
12302 return 0;
12305 /* If point is in a reused row, adjust y and vpos of the cursor
12306 position. */
12307 if (pt_row)
12309 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
12310 w->current_matrix);
12311 w->cursor.y -= first_reusable_row->y;
12314 /* Scroll the display. */
12315 run.current_y = first_reusable_row->y;
12316 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12317 run.height = it.last_visible_y - run.current_y;
12318 dy = run.current_y - run.desired_y;
12320 if (run.height)
12322 struct frame *f = XFRAME (WINDOW_FRAME (w));
12323 update_begin (f);
12324 rif->update_window_begin_hook (w);
12325 rif->clear_window_mouse_face (w);
12326 rif->scroll_run_hook (w, &run);
12327 rif->update_window_end_hook (w, 0, 0);
12328 update_end (f);
12331 /* Adjust Y positions of reused rows. */
12332 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12333 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
12334 max_y = it.last_visible_y;
12335 for (row = first_reusable_row; row < first_row_to_display; ++row)
12337 row->y -= dy;
12338 row->visible_height = row->height;
12339 if (row->y < min_y)
12340 row->visible_height -= min_y - row->y;
12341 if (row->y + row->height > max_y)
12342 row->visible_height -= row->y + row->height - max_y;
12345 /* Scroll the current matrix. */
12346 xassert (nrows_scrolled > 0);
12347 rotate_matrix (w->current_matrix,
12348 start_vpos,
12349 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12350 -nrows_scrolled);
12352 /* Disable rows not reused. */
12353 for (row -= nrows_scrolled; row < bottom_row; ++row)
12354 row->enabled_p = 0;
12356 /* Adjust window end. A null value of last_text_row means that
12357 the window end is in reused rows which in turn means that
12358 only its vpos can have changed. */
12359 if (last_text_row)
12361 w->window_end_bytepos
12362 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12363 w->window_end_pos
12364 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12365 w->window_end_vpos
12366 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12368 else
12370 w->window_end_vpos
12371 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12374 w->window_end_valid = Qnil;
12375 w->desired_matrix->no_scrolling_p = 1;
12377 #if GLYPH_DEBUG
12378 debug_method_add (w, "try_window_reusing_current_matrix 2");
12379 #endif
12380 return 1;
12383 return 0;
12388 /************************************************************************
12389 Window redisplay reusing current matrix when buffer has changed
12390 ************************************************************************/
12392 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12393 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12394 int *, int *));
12395 static struct glyph_row *
12396 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12397 struct glyph_row *));
12400 /* Return the last row in MATRIX displaying text. If row START is
12401 non-null, start searching with that row. IT gives the dimensions
12402 of the display. Value is null if matrix is empty; otherwise it is
12403 a pointer to the row found. */
12405 static struct glyph_row *
12406 find_last_row_displaying_text (matrix, it, start)
12407 struct glyph_matrix *matrix;
12408 struct it *it;
12409 struct glyph_row *start;
12411 struct glyph_row *row, *row_found;
12413 /* Set row_found to the last row in IT->w's current matrix
12414 displaying text. The loop looks funny but think of partially
12415 visible lines. */
12416 row_found = NULL;
12417 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12418 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12420 xassert (row->enabled_p);
12421 row_found = row;
12422 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12423 break;
12424 ++row;
12427 return row_found;
12431 /* Return the last row in the current matrix of W that is not affected
12432 by changes at the start of current_buffer that occurred since W's
12433 current matrix was built. Value is null if no such row exists.
12435 BEG_UNCHANGED us the number of characters unchanged at the start of
12436 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
12437 first changed character in current_buffer. Characters at positions <
12438 BEG + BEG_UNCHANGED are at the same buffer positions as they were
12439 when the current matrix was built. */
12441 static struct glyph_row *
12442 find_last_unchanged_at_beg_row (w)
12443 struct window *w;
12445 int first_changed_pos = BEG + BEG_UNCHANGED;
12446 struct glyph_row *row;
12447 struct glyph_row *row_found = NULL;
12448 int yb = window_text_bottom_y (w);
12450 /* Find the last row displaying unchanged text. */
12451 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12452 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12453 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
12455 if (/* If row ends before first_changed_pos, it is unchanged,
12456 except in some case. */
12457 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
12458 /* When row ends in ZV and we write at ZV it is not
12459 unchanged. */
12460 && !row->ends_at_zv_p
12461 /* When first_changed_pos is the end of a continued line,
12462 row is not unchanged because it may be no longer
12463 continued. */
12464 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
12465 && row->continued_p))
12466 row_found = row;
12468 /* Stop if last visible row. */
12469 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
12470 break;
12472 ++row;
12475 return row_found;
12479 /* Find the first glyph row in the current matrix of W that is not
12480 affected by changes at the end of current_buffer since the
12481 time W's current matrix was built.
12483 Return in *DELTA the number of chars by which buffer positions in
12484 unchanged text at the end of current_buffer must be adjusted.
12486 Return in *DELTA_BYTES the corresponding number of bytes.
12488 Value is null if no such row exists, i.e. all rows are affected by
12489 changes. */
12491 static struct glyph_row *
12492 find_first_unchanged_at_end_row (w, delta, delta_bytes)
12493 struct window *w;
12494 int *delta, *delta_bytes;
12496 struct glyph_row *row;
12497 struct glyph_row *row_found = NULL;
12499 *delta = *delta_bytes = 0;
12501 /* Display must not have been paused, otherwise the current matrix
12502 is not up to date. */
12503 if (NILP (w->window_end_valid))
12504 abort ();
12506 /* A value of window_end_pos >= END_UNCHANGED means that the window
12507 end is in the range of changed text. If so, there is no
12508 unchanged row at the end of W's current matrix. */
12509 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
12510 return NULL;
12512 /* Set row to the last row in W's current matrix displaying text. */
12513 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12515 /* If matrix is entirely empty, no unchanged row exists. */
12516 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12518 /* The value of row is the last glyph row in the matrix having a
12519 meaningful buffer position in it. The end position of row
12520 corresponds to window_end_pos. This allows us to translate
12521 buffer positions in the current matrix to current buffer
12522 positions for characters not in changed text. */
12523 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12524 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12525 int last_unchanged_pos, last_unchanged_pos_old;
12526 struct glyph_row *first_text_row
12527 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12529 *delta = Z - Z_old;
12530 *delta_bytes = Z_BYTE - Z_BYTE_old;
12532 /* Set last_unchanged_pos to the buffer position of the last
12533 character in the buffer that has not been changed. Z is the
12534 index + 1 of the last character in current_buffer, i.e. by
12535 subtracting END_UNCHANGED we get the index of the last
12536 unchanged character, and we have to add BEG to get its buffer
12537 position. */
12538 last_unchanged_pos = Z - END_UNCHANGED + BEG;
12539 last_unchanged_pos_old = last_unchanged_pos - *delta;
12541 /* Search backward from ROW for a row displaying a line that
12542 starts at a minimum position >= last_unchanged_pos_old. */
12543 for (; row > first_text_row; --row)
12545 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
12546 abort ();
12548 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
12549 row_found = row;
12553 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
12554 abort ();
12556 return row_found;
12560 /* Make sure that glyph rows in the current matrix of window W
12561 reference the same glyph memory as corresponding rows in the
12562 frame's frame matrix. This function is called after scrolling W's
12563 current matrix on a terminal frame in try_window_id and
12564 try_window_reusing_current_matrix. */
12566 static void
12567 sync_frame_with_window_matrix_rows (w)
12568 struct window *w;
12570 struct frame *f = XFRAME (w->frame);
12571 struct glyph_row *window_row, *window_row_end, *frame_row;
12573 /* Preconditions: W must be a leaf window and full-width. Its frame
12574 must have a frame matrix. */
12575 xassert (NILP (w->hchild) && NILP (w->vchild));
12576 xassert (WINDOW_FULL_WIDTH_P (w));
12577 xassert (!FRAME_WINDOW_P (f));
12579 /* If W is a full-width window, glyph pointers in W's current matrix
12580 have, by definition, to be the same as glyph pointers in the
12581 corresponding frame matrix. Note that frame matrices have no
12582 marginal areas (see build_frame_matrix). */
12583 window_row = w->current_matrix->rows;
12584 window_row_end = window_row + w->current_matrix->nrows;
12585 frame_row = f->current_matrix->rows + XFASTINT (w->top);
12586 while (window_row < window_row_end)
12588 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
12589 struct glyph *end = window_row->glyphs[LAST_AREA];
12591 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
12592 frame_row->glyphs[TEXT_AREA] = start;
12593 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
12594 frame_row->glyphs[LAST_AREA] = end;
12596 /* Disable frame rows whose corresponding window rows have
12597 been disabled in try_window_id. */
12598 if (!window_row->enabled_p)
12599 frame_row->enabled_p = 0;
12601 ++window_row, ++frame_row;
12606 /* Find the glyph row in window W containing CHARPOS. Consider all
12607 rows between START and END (not inclusive). END null means search
12608 all rows to the end of the display area of W. Value is the row
12609 containing CHARPOS or null. */
12611 struct glyph_row *
12612 row_containing_pos (w, charpos, start, end, dy)
12613 struct window *w;
12614 int charpos;
12615 struct glyph_row *start, *end;
12616 int dy;
12618 struct glyph_row *row = start;
12619 int last_y;
12621 /* If we happen to start on a header-line, skip that. */
12622 if (row->mode_line_p)
12623 ++row;
12625 if ((end && row >= end) || !row->enabled_p)
12626 return NULL;
12628 last_y = window_text_bottom_y (w) - dy;
12630 while (1)
12632 /* Give up if we have gone too far. */
12633 if (end && row >= end)
12634 return NULL;
12635 /* This formerly returned if they were equal.
12636 I think that both quantities are of a "last plus one" type;
12637 if so, when they are equal, the row is within the screen. -- rms. */
12638 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
12639 return NULL;
12641 /* If it is in this row, return this row. */
12642 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
12643 || (MATRIX_ROW_END_CHARPOS (row) == charpos
12644 /* The end position of a row equals the start
12645 position of the next row. If CHARPOS is there, we
12646 would rather display it in the next line, except
12647 when this line ends in ZV. */
12648 && !row->ends_at_zv_p
12649 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12650 && charpos >= MATRIX_ROW_START_CHARPOS (row))
12651 return row;
12652 ++row;
12657 /* Try to redisplay window W by reusing its existing display. W's
12658 current matrix must be up to date when this function is called,
12659 i.e. window_end_valid must not be nil.
12661 Value is
12663 1 if display has been updated
12664 0 if otherwise unsuccessful
12665 -1 if redisplay with same window start is known not to succeed
12667 The following steps are performed:
12669 1. Find the last row in the current matrix of W that is not
12670 affected by changes at the start of current_buffer. If no such row
12671 is found, give up.
12673 2. Find the first row in W's current matrix that is not affected by
12674 changes at the end of current_buffer. Maybe there is no such row.
12676 3. Display lines beginning with the row + 1 found in step 1 to the
12677 row found in step 2 or, if step 2 didn't find a row, to the end of
12678 the window.
12680 4. If cursor is not known to appear on the window, give up.
12682 5. If display stopped at the row found in step 2, scroll the
12683 display and current matrix as needed.
12685 6. Maybe display some lines at the end of W, if we must. This can
12686 happen under various circumstances, like a partially visible line
12687 becoming fully visible, or because newly displayed lines are displayed
12688 in smaller font sizes.
12690 7. Update W's window end information. */
12692 static int
12693 try_window_id (w)
12694 struct window *w;
12696 struct frame *f = XFRAME (w->frame);
12697 struct glyph_matrix *current_matrix = w->current_matrix;
12698 struct glyph_matrix *desired_matrix = w->desired_matrix;
12699 struct glyph_row *last_unchanged_at_beg_row;
12700 struct glyph_row *first_unchanged_at_end_row;
12701 struct glyph_row *row;
12702 struct glyph_row *bottom_row;
12703 int bottom_vpos;
12704 struct it it;
12705 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
12706 struct text_pos start_pos;
12707 struct run run;
12708 int first_unchanged_at_end_vpos = 0;
12709 struct glyph_row *last_text_row, *last_text_row_at_end;
12710 struct text_pos start;
12711 int first_changed_charpos, last_changed_charpos;
12713 #if GLYPH_DEBUG
12714 if (inhibit_try_window_id)
12715 return 0;
12716 #endif
12718 /* This is handy for debugging. */
12719 #if 0
12720 #define GIVE_UP(X) \
12721 do { \
12722 fprintf (stderr, "try_window_id give up %d\n", (X)); \
12723 return 0; \
12724 } while (0)
12725 #else
12726 #define GIVE_UP(X) return 0
12727 #endif
12729 SET_TEXT_POS_FROM_MARKER (start, w->start);
12731 /* Don't use this for mini-windows because these can show
12732 messages and mini-buffers, and we don't handle that here. */
12733 if (MINI_WINDOW_P (w))
12734 GIVE_UP (1);
12736 /* This flag is used to prevent redisplay optimizations. */
12737 if (windows_or_buffers_changed || cursor_type_changed)
12738 GIVE_UP (2);
12740 /* Verify that narrowing has not changed.
12741 Also verify that we were not told to prevent redisplay optimizations.
12742 It would be nice to further
12743 reduce the number of cases where this prevents try_window_id. */
12744 if (current_buffer->clip_changed
12745 || current_buffer->prevent_redisplay_optimizations_p)
12746 GIVE_UP (3);
12748 /* Window must either use window-based redisplay or be full width. */
12749 if (!FRAME_WINDOW_P (f)
12750 && (!line_ins_del_ok
12751 || !WINDOW_FULL_WIDTH_P (w)))
12752 GIVE_UP (4);
12754 /* Give up if point is not known NOT to appear in W. */
12755 if (PT < CHARPOS (start))
12756 GIVE_UP (5);
12758 /* Another way to prevent redisplay optimizations. */
12759 if (XFASTINT (w->last_modified) == 0)
12760 GIVE_UP (6);
12762 /* Verify that window is not hscrolled. */
12763 if (XFASTINT (w->hscroll) != 0)
12764 GIVE_UP (7);
12766 /* Verify that display wasn't paused. */
12767 if (NILP (w->window_end_valid))
12768 GIVE_UP (8);
12770 /* Can't use this if highlighting a region because a cursor movement
12771 will do more than just set the cursor. */
12772 if (!NILP (Vtransient_mark_mode)
12773 && !NILP (current_buffer->mark_active))
12774 GIVE_UP (9);
12776 /* Likewise if highlighting trailing whitespace. */
12777 if (!NILP (Vshow_trailing_whitespace))
12778 GIVE_UP (11);
12780 /* Likewise if showing a region. */
12781 if (!NILP (w->region_showing))
12782 GIVE_UP (10);
12784 /* Can use this if overlay arrow position and or string have changed. */
12785 if (!EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
12786 || !EQ (last_arrow_string, Voverlay_arrow_string))
12787 GIVE_UP (12);
12790 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
12791 only if buffer has really changed. The reason is that the gap is
12792 initially at Z for freshly visited files. The code below would
12793 set end_unchanged to 0 in that case. */
12794 if (MODIFF > SAVE_MODIFF
12795 /* This seems to happen sometimes after saving a buffer. */
12796 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
12798 if (GPT - BEG < BEG_UNCHANGED)
12799 BEG_UNCHANGED = GPT - BEG;
12800 if (Z - GPT < END_UNCHANGED)
12801 END_UNCHANGED = Z - GPT;
12804 /* The position of the first and last character that has been changed. */
12805 first_changed_charpos = BEG + BEG_UNCHANGED;
12806 last_changed_charpos = Z - END_UNCHANGED;
12808 /* If window starts after a line end, and the last change is in
12809 front of that newline, then changes don't affect the display.
12810 This case happens with stealth-fontification. Note that although
12811 the display is unchanged, glyph positions in the matrix have to
12812 be adjusted, of course. */
12813 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
12814 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12815 && ((last_changed_charpos < CHARPOS (start)
12816 && CHARPOS (start) == BEGV)
12817 || (last_changed_charpos < CHARPOS (start) - 1
12818 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
12820 int Z_old, delta, Z_BYTE_old, delta_bytes;
12821 struct glyph_row *r0;
12823 /* Compute how many chars/bytes have been added to or removed
12824 from the buffer. */
12825 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
12826 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
12827 delta = Z - Z_old;
12828 delta_bytes = Z_BYTE - Z_BYTE_old;
12830 /* Give up if PT is not in the window. Note that it already has
12831 been checked at the start of try_window_id that PT is not in
12832 front of the window start. */
12833 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
12834 GIVE_UP (13);
12836 /* If window start is unchanged, we can reuse the whole matrix
12837 as is, after adjusting glyph positions. No need to compute
12838 the window end again, since its offset from Z hasn't changed. */
12839 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12840 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
12841 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes)
12843 /* Adjust positions in the glyph matrix. */
12844 if (delta || delta_bytes)
12846 struct glyph_row *r1
12847 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
12848 increment_matrix_positions (w->current_matrix,
12849 MATRIX_ROW_VPOS (r0, current_matrix),
12850 MATRIX_ROW_VPOS (r1, current_matrix),
12851 delta, delta_bytes);
12854 /* Set the cursor. */
12855 row = row_containing_pos (w, PT, r0, NULL, 0);
12856 if (row)
12857 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12858 else
12859 abort ();
12860 return 1;
12864 /* Handle the case that changes are all below what is displayed in
12865 the window, and that PT is in the window. This shortcut cannot
12866 be taken if ZV is visible in the window, and text has been added
12867 there that is visible in the window. */
12868 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
12869 /* ZV is not visible in the window, or there are no
12870 changes at ZV, actually. */
12871 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
12872 || first_changed_charpos == last_changed_charpos))
12874 struct glyph_row *r0;
12876 /* Give up if PT is not in the window. Note that it already has
12877 been checked at the start of try_window_id that PT is not in
12878 front of the window start. */
12879 if (PT >= MATRIX_ROW_END_CHARPOS (row))
12880 GIVE_UP (14);
12882 /* If window start is unchanged, we can reuse the whole matrix
12883 as is, without changing glyph positions since no text has
12884 been added/removed in front of the window end. */
12885 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
12886 if (TEXT_POS_EQUAL_P (start, r0->start.pos))
12888 /* We have to compute the window end anew since text
12889 can have been added/removed after it. */
12890 w->window_end_pos
12891 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
12892 w->window_end_bytepos
12893 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
12895 /* Set the cursor. */
12896 row = row_containing_pos (w, PT, r0, NULL, 0);
12897 if (row)
12898 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
12899 else
12900 abort ();
12901 return 2;
12905 /* Give up if window start is in the changed area.
12907 The condition used to read
12909 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
12911 but why that was tested escapes me at the moment. */
12912 if (CHARPOS (start) >= first_changed_charpos
12913 && CHARPOS (start) <= last_changed_charpos)
12914 GIVE_UP (15);
12916 /* Check that window start agrees with the start of the first glyph
12917 row in its current matrix. Check this after we know the window
12918 start is not in changed text, otherwise positions would not be
12919 comparable. */
12920 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
12921 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
12922 GIVE_UP (16);
12924 /* Give up if the window ends in strings. Overlay strings
12925 at the end are difficult to handle, so don't try. */
12926 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
12927 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
12928 GIVE_UP (20);
12930 /* Compute the position at which we have to start displaying new
12931 lines. Some of the lines at the top of the window might be
12932 reusable because they are not displaying changed text. Find the
12933 last row in W's current matrix not affected by changes at the
12934 start of current_buffer. Value is null if changes start in the
12935 first line of window. */
12936 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
12937 if (last_unchanged_at_beg_row)
12939 /* Avoid starting to display in the moddle of a character, a TAB
12940 for instance. This is easier than to set up the iterator
12941 exactly, and it's not a frequent case, so the additional
12942 effort wouldn't really pay off. */
12943 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
12944 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
12945 && last_unchanged_at_beg_row > w->current_matrix->rows)
12946 --last_unchanged_at_beg_row;
12948 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
12949 GIVE_UP (17);
12951 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
12952 GIVE_UP (18);
12953 start_pos = it.current.pos;
12955 /* Start displaying new lines in the desired matrix at the same
12956 vpos we would use in the current matrix, i.e. below
12957 last_unchanged_at_beg_row. */
12958 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
12959 current_matrix);
12960 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
12961 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
12963 xassert (it.hpos == 0 && it.current_x == 0);
12965 else
12967 /* There are no reusable lines at the start of the window.
12968 Start displaying in the first line. */
12969 start_display (&it, w, start);
12970 start_pos = it.current.pos;
12973 /* Find the first row that is not affected by changes at the end of
12974 the buffer. Value will be null if there is no unchanged row, in
12975 which case we must redisplay to the end of the window. delta
12976 will be set to the value by which buffer positions beginning with
12977 first_unchanged_at_end_row have to be adjusted due to text
12978 changes. */
12979 first_unchanged_at_end_row
12980 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
12981 IF_DEBUG (debug_delta = delta);
12982 IF_DEBUG (debug_delta_bytes = delta_bytes);
12984 /* Set stop_pos to the buffer position up to which we will have to
12985 display new lines. If first_unchanged_at_end_row != NULL, this
12986 is the buffer position of the start of the line displayed in that
12987 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
12988 that we don't stop at a buffer position. */
12989 stop_pos = 0;
12990 if (first_unchanged_at_end_row)
12992 xassert (last_unchanged_at_beg_row == NULL
12993 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
12995 /* If this is a continuation line, move forward to the next one
12996 that isn't. Changes in lines above affect this line.
12997 Caution: this may move first_unchanged_at_end_row to a row
12998 not displaying text. */
12999 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13000 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13001 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13002 < it.last_visible_y))
13003 ++first_unchanged_at_end_row;
13005 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13006 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13007 >= it.last_visible_y))
13008 first_unchanged_at_end_row = NULL;
13009 else
13011 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13012 + delta);
13013 first_unchanged_at_end_vpos
13014 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13015 xassert (stop_pos >= Z - END_UNCHANGED);
13018 else if (last_unchanged_at_beg_row == NULL)
13019 GIVE_UP (19);
13022 #if GLYPH_DEBUG
13024 /* Either there is no unchanged row at the end, or the one we have
13025 now displays text. This is a necessary condition for the window
13026 end pos calculation at the end of this function. */
13027 xassert (first_unchanged_at_end_row == NULL
13028 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13030 debug_last_unchanged_at_beg_vpos
13031 = (last_unchanged_at_beg_row
13032 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13033 : -1);
13034 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13036 #endif /* GLYPH_DEBUG != 0 */
13039 /* Display new lines. Set last_text_row to the last new line
13040 displayed which has text on it, i.e. might end up as being the
13041 line where the window_end_vpos is. */
13042 w->cursor.vpos = -1;
13043 last_text_row = NULL;
13044 overlay_arrow_seen = 0;
13045 while (it.current_y < it.last_visible_y
13046 && !fonts_changed_p
13047 && (first_unchanged_at_end_row == NULL
13048 || IT_CHARPOS (it) < stop_pos))
13050 if (display_line (&it))
13051 last_text_row = it.glyph_row - 1;
13054 if (fonts_changed_p)
13055 return -1;
13058 /* Compute differences in buffer positions, y-positions etc. for
13059 lines reused at the bottom of the window. Compute what we can
13060 scroll. */
13061 if (first_unchanged_at_end_row
13062 /* No lines reused because we displayed everything up to the
13063 bottom of the window. */
13064 && it.current_y < it.last_visible_y)
13066 dvpos = (it.vpos
13067 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13068 current_matrix));
13069 dy = it.current_y - first_unchanged_at_end_row->y;
13070 run.current_y = first_unchanged_at_end_row->y;
13071 run.desired_y = run.current_y + dy;
13072 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13074 else
13076 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13077 first_unchanged_at_end_row = NULL;
13079 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13082 /* Find the cursor if not already found. We have to decide whether
13083 PT will appear on this window (it sometimes doesn't, but this is
13084 not a very frequent case.) This decision has to be made before
13085 the current matrix is altered. A value of cursor.vpos < 0 means
13086 that PT is either in one of the lines beginning at
13087 first_unchanged_at_end_row or below the window. Don't care for
13088 lines that might be displayed later at the window end; as
13089 mentioned, this is not a frequent case. */
13090 if (w->cursor.vpos < 0)
13092 /* Cursor in unchanged rows at the top? */
13093 if (PT < CHARPOS (start_pos)
13094 && last_unchanged_at_beg_row)
13096 row = row_containing_pos (w, PT,
13097 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13098 last_unchanged_at_beg_row + 1, 0);
13099 if (row)
13100 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13103 /* Start from first_unchanged_at_end_row looking for PT. */
13104 else if (first_unchanged_at_end_row)
13106 row = row_containing_pos (w, PT - delta,
13107 first_unchanged_at_end_row, NULL, 0);
13108 if (row)
13109 set_cursor_from_row (w, row, w->current_matrix, delta,
13110 delta_bytes, dy, dvpos);
13113 /* Give up if cursor was not found. */
13114 if (w->cursor.vpos < 0)
13116 clear_glyph_matrix (w->desired_matrix);
13117 return -1;
13121 /* Don't let the cursor end in the scroll margins. */
13123 int this_scroll_margin, cursor_height;
13125 this_scroll_margin = max (0, scroll_margin);
13126 this_scroll_margin = min (this_scroll_margin,
13127 XFASTINT (w->height) / 4);
13128 this_scroll_margin *= CANON_Y_UNIT (it.f);
13129 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13131 if ((w->cursor.y < this_scroll_margin
13132 && CHARPOS (start) > BEGV)
13133 /* Don't take scroll margin into account at the bottom because
13134 old redisplay didn't do it either. */
13135 || w->cursor.y + cursor_height > it.last_visible_y)
13137 w->cursor.vpos = -1;
13138 clear_glyph_matrix (w->desired_matrix);
13139 return -1;
13143 /* Scroll the display. Do it before changing the current matrix so
13144 that xterm.c doesn't get confused about where the cursor glyph is
13145 found. */
13146 if (dy && run.height)
13148 update_begin (f);
13150 if (FRAME_WINDOW_P (f))
13152 rif->update_window_begin_hook (w);
13153 rif->clear_window_mouse_face (w);
13154 rif->scroll_run_hook (w, &run);
13155 rif->update_window_end_hook (w, 0, 0);
13157 else
13159 /* Terminal frame. In this case, dvpos gives the number of
13160 lines to scroll by; dvpos < 0 means scroll up. */
13161 int first_unchanged_at_end_vpos
13162 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13163 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
13164 int end = (XFASTINT (w->top)
13165 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13166 + window_internal_height (w));
13168 /* Perform the operation on the screen. */
13169 if (dvpos > 0)
13171 /* Scroll last_unchanged_at_beg_row to the end of the
13172 window down dvpos lines. */
13173 set_terminal_window (end);
13175 /* On dumb terminals delete dvpos lines at the end
13176 before inserting dvpos empty lines. */
13177 if (!scroll_region_ok)
13178 ins_del_lines (end - dvpos, -dvpos);
13180 /* Insert dvpos empty lines in front of
13181 last_unchanged_at_beg_row. */
13182 ins_del_lines (from, dvpos);
13184 else if (dvpos < 0)
13186 /* Scroll up last_unchanged_at_beg_vpos to the end of
13187 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13188 set_terminal_window (end);
13190 /* Delete dvpos lines in front of
13191 last_unchanged_at_beg_vpos. ins_del_lines will set
13192 the cursor to the given vpos and emit |dvpos| delete
13193 line sequences. */
13194 ins_del_lines (from + dvpos, dvpos);
13196 /* On a dumb terminal insert dvpos empty lines at the
13197 end. */
13198 if (!scroll_region_ok)
13199 ins_del_lines (end + dvpos, -dvpos);
13202 set_terminal_window (0);
13205 update_end (f);
13208 /* Shift reused rows of the current matrix to the right position.
13209 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13210 text. */
13211 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13212 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13213 if (dvpos < 0)
13215 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13216 bottom_vpos, dvpos);
13217 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13218 bottom_vpos, 0);
13220 else if (dvpos > 0)
13222 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13223 bottom_vpos, dvpos);
13224 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13225 first_unchanged_at_end_vpos + dvpos, 0);
13228 /* For frame-based redisplay, make sure that current frame and window
13229 matrix are in sync with respect to glyph memory. */
13230 if (!FRAME_WINDOW_P (f))
13231 sync_frame_with_window_matrix_rows (w);
13233 /* Adjust buffer positions in reused rows. */
13234 if (delta)
13235 increment_matrix_positions (current_matrix,
13236 first_unchanged_at_end_vpos + dvpos,
13237 bottom_vpos, delta, delta_bytes);
13239 /* Adjust Y positions. */
13240 if (dy)
13241 shift_glyph_matrix (w, current_matrix,
13242 first_unchanged_at_end_vpos + dvpos,
13243 bottom_vpos, dy);
13245 if (first_unchanged_at_end_row)
13246 first_unchanged_at_end_row += dvpos;
13248 /* If scrolling up, there may be some lines to display at the end of
13249 the window. */
13250 last_text_row_at_end = NULL;
13251 if (dy < 0)
13253 /* Scrolling up can leave for example a partially visible line
13254 at the end of the window to be redisplayed. */
13255 /* Set last_row to the glyph row in the current matrix where the
13256 window end line is found. It has been moved up or down in
13257 the matrix by dvpos. */
13258 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13259 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13261 /* If last_row is the window end line, it should display text. */
13262 xassert (last_row->displays_text_p);
13264 /* If window end line was partially visible before, begin
13265 displaying at that line. Otherwise begin displaying with the
13266 line following it. */
13267 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13269 init_to_row_start (&it, w, last_row);
13270 it.vpos = last_vpos;
13271 it.current_y = last_row->y;
13273 else
13275 init_to_row_end (&it, w, last_row);
13276 it.vpos = 1 + last_vpos;
13277 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13278 ++last_row;
13281 /* We may start in a continuation line. If so, we have to
13282 get the right continuation_lines_width and current_x. */
13283 it.continuation_lines_width = last_row->continuation_lines_width;
13284 it.hpos = it.current_x = 0;
13286 /* Display the rest of the lines at the window end. */
13287 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13288 while (it.current_y < it.last_visible_y
13289 && !fonts_changed_p)
13291 /* Is it always sure that the display agrees with lines in
13292 the current matrix? I don't think so, so we mark rows
13293 displayed invalid in the current matrix by setting their
13294 enabled_p flag to zero. */
13295 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13296 if (display_line (&it))
13297 last_text_row_at_end = it.glyph_row - 1;
13301 /* Update window_end_pos and window_end_vpos. */
13302 if (first_unchanged_at_end_row
13303 && first_unchanged_at_end_row->y < it.last_visible_y
13304 && !last_text_row_at_end)
13306 /* Window end line if one of the preserved rows from the current
13307 matrix. Set row to the last row displaying text in current
13308 matrix starting at first_unchanged_at_end_row, after
13309 scrolling. */
13310 xassert (first_unchanged_at_end_row->displays_text_p);
13311 row = find_last_row_displaying_text (w->current_matrix, &it,
13312 first_unchanged_at_end_row);
13313 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13315 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13316 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13317 w->window_end_vpos
13318 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13319 xassert (w->window_end_bytepos >= 0);
13320 IF_DEBUG (debug_method_add (w, "A"));
13322 else if (last_text_row_at_end)
13324 w->window_end_pos
13325 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13326 w->window_end_bytepos
13327 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13328 w->window_end_vpos
13329 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13330 xassert (w->window_end_bytepos >= 0);
13331 IF_DEBUG (debug_method_add (w, "B"));
13333 else if (last_text_row)
13335 /* We have displayed either to the end of the window or at the
13336 end of the window, i.e. the last row with text is to be found
13337 in the desired matrix. */
13338 w->window_end_pos
13339 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13340 w->window_end_bytepos
13341 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13342 w->window_end_vpos
13343 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13344 xassert (w->window_end_bytepos >= 0);
13346 else if (first_unchanged_at_end_row == NULL
13347 && last_text_row == NULL
13348 && last_text_row_at_end == NULL)
13350 /* Displayed to end of window, but no line containing text was
13351 displayed. Lines were deleted at the end of the window. */
13352 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13353 int vpos = XFASTINT (w->window_end_vpos);
13354 struct glyph_row *current_row = current_matrix->rows + vpos;
13355 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13357 for (row = NULL;
13358 row == NULL && vpos >= first_vpos;
13359 --vpos, --current_row, --desired_row)
13361 if (desired_row->enabled_p)
13363 if (desired_row->displays_text_p)
13364 row = desired_row;
13366 else if (current_row->displays_text_p)
13367 row = current_row;
13370 xassert (row != NULL);
13371 w->window_end_vpos = make_number (vpos + 1);
13372 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13373 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13374 xassert (w->window_end_bytepos >= 0);
13375 IF_DEBUG (debug_method_add (w, "C"));
13377 else
13378 abort ();
13380 #if 0 /* This leads to problems, for instance when the cursor is
13381 at ZV, and the cursor line displays no text. */
13382 /* Disable rows below what's displayed in the window. This makes
13383 debugging easier. */
13384 enable_glyph_matrix_rows (current_matrix,
13385 XFASTINT (w->window_end_vpos) + 1,
13386 bottom_vpos, 0);
13387 #endif
13389 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13390 debug_end_vpos = XFASTINT (w->window_end_vpos));
13392 /* Record that display has not been completed. */
13393 w->window_end_valid = Qnil;
13394 w->desired_matrix->no_scrolling_p = 1;
13395 return 3;
13397 #undef GIVE_UP
13402 /***********************************************************************
13403 More debugging support
13404 ***********************************************************************/
13406 #if GLYPH_DEBUG
13408 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13409 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13410 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13413 /* Dump the contents of glyph matrix MATRIX on stderr.
13415 GLYPHS 0 means don't show glyph contents.
13416 GLYPHS 1 means show glyphs in short form
13417 GLYPHS > 1 means show glyphs in long form. */
13419 void
13420 dump_glyph_matrix (matrix, glyphs)
13421 struct glyph_matrix *matrix;
13422 int glyphs;
13424 int i;
13425 for (i = 0; i < matrix->nrows; ++i)
13426 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
13430 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
13431 the glyph row and area where the glyph comes from. */
13433 void
13434 dump_glyph (row, glyph, area)
13435 struct glyph_row *row;
13436 struct glyph *glyph;
13437 int area;
13439 if (glyph->type == CHAR_GLYPH)
13441 fprintf (stderr,
13442 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13443 glyph - row->glyphs[TEXT_AREA],
13444 'C',
13445 glyph->charpos,
13446 (BUFFERP (glyph->object)
13447 ? 'B'
13448 : (STRINGP (glyph->object)
13449 ? 'S'
13450 : '-')),
13451 glyph->pixel_width,
13452 glyph->u.ch,
13453 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
13454 ? glyph->u.ch
13455 : '.'),
13456 glyph->face_id,
13457 glyph->left_box_line_p,
13458 glyph->right_box_line_p);
13460 else if (glyph->type == STRETCH_GLYPH)
13462 fprintf (stderr,
13463 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13464 glyph - row->glyphs[TEXT_AREA],
13465 'S',
13466 glyph->charpos,
13467 (BUFFERP (glyph->object)
13468 ? 'B'
13469 : (STRINGP (glyph->object)
13470 ? 'S'
13471 : '-')),
13472 glyph->pixel_width,
13474 '.',
13475 glyph->face_id,
13476 glyph->left_box_line_p,
13477 glyph->right_box_line_p);
13479 else if (glyph->type == IMAGE_GLYPH)
13481 fprintf (stderr,
13482 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
13483 glyph - row->glyphs[TEXT_AREA],
13484 'I',
13485 glyph->charpos,
13486 (BUFFERP (glyph->object)
13487 ? 'B'
13488 : (STRINGP (glyph->object)
13489 ? 'S'
13490 : '-')),
13491 glyph->pixel_width,
13492 glyph->u.img_id,
13493 '.',
13494 glyph->face_id,
13495 glyph->left_box_line_p,
13496 glyph->right_box_line_p);
13501 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
13502 GLYPHS 0 means don't show glyph contents.
13503 GLYPHS 1 means show glyphs in short form
13504 GLYPHS > 1 means show glyphs in long form. */
13506 void
13507 dump_glyph_row (row, vpos, glyphs)
13508 struct glyph_row *row;
13509 int vpos, glyphs;
13511 if (glyphs != 1)
13513 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
13514 fprintf (stderr, "=======================================================================\n");
13516 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
13517 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
13518 vpos,
13519 MATRIX_ROW_START_CHARPOS (row),
13520 MATRIX_ROW_END_CHARPOS (row),
13521 row->used[TEXT_AREA],
13522 row->contains_overlapping_glyphs_p,
13523 row->enabled_p,
13524 row->truncated_on_left_p,
13525 row->truncated_on_right_p,
13526 row->overlay_arrow_p,
13527 row->continued_p,
13528 MATRIX_ROW_CONTINUATION_LINE_P (row),
13529 row->displays_text_p,
13530 row->ends_at_zv_p,
13531 row->fill_line_p,
13532 row->ends_in_middle_of_char_p,
13533 row->starts_in_middle_of_char_p,
13534 row->mouse_face_p,
13535 row->x,
13536 row->y,
13537 row->pixel_width,
13538 row->height,
13539 row->visible_height,
13540 row->ascent,
13541 row->phys_ascent);
13542 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
13543 row->end.overlay_string_index,
13544 row->continuation_lines_width);
13545 fprintf (stderr, "%9d %5d\n",
13546 CHARPOS (row->start.string_pos),
13547 CHARPOS (row->end.string_pos));
13548 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
13549 row->end.dpvec_index);
13552 if (glyphs > 1)
13554 int area;
13556 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13558 struct glyph *glyph = row->glyphs[area];
13559 struct glyph *glyph_end = glyph + row->used[area];
13561 /* Glyph for a line end in text. */
13562 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
13563 ++glyph_end;
13565 if (glyph < glyph_end)
13566 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
13568 for (; glyph < glyph_end; ++glyph)
13569 dump_glyph (row, glyph, area);
13572 else if (glyphs == 1)
13574 int area;
13576 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13578 char *s = (char *) alloca (row->used[area] + 1);
13579 int i;
13581 for (i = 0; i < row->used[area]; ++i)
13583 struct glyph *glyph = row->glyphs[area] + i;
13584 if (glyph->type == CHAR_GLYPH
13585 && glyph->u.ch < 0x80
13586 && glyph->u.ch >= ' ')
13587 s[i] = glyph->u.ch;
13588 else
13589 s[i] = '.';
13592 s[i] = '\0';
13593 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
13599 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
13600 Sdump_glyph_matrix, 0, 1, "p",
13601 doc: /* Dump the current matrix of the selected window to stderr.
13602 Shows contents of glyph row structures. With non-nil
13603 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
13604 glyphs in short form, otherwise show glyphs in long form. */)
13605 (glyphs)
13606 Lisp_Object glyphs;
13608 struct window *w = XWINDOW (selected_window);
13609 struct buffer *buffer = XBUFFER (w->buffer);
13611 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
13612 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
13613 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
13614 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
13615 fprintf (stderr, "=============================================\n");
13616 dump_glyph_matrix (w->current_matrix,
13617 NILP (glyphs) ? 0 : XINT (glyphs));
13618 return Qnil;
13622 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
13623 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
13626 struct frame *f = XFRAME (selected_frame);
13627 dump_glyph_matrix (f->current_matrix, 1);
13628 return Qnil;
13632 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
13633 doc: /* Dump glyph row ROW to stderr.
13634 GLYPH 0 means don't dump glyphs.
13635 GLYPH 1 means dump glyphs in short form.
13636 GLYPH > 1 or omitted means dump glyphs in long form. */)
13637 (row, glyphs)
13638 Lisp_Object row, glyphs;
13640 struct glyph_matrix *matrix;
13641 int vpos;
13643 CHECK_NUMBER (row);
13644 matrix = XWINDOW (selected_window)->current_matrix;
13645 vpos = XINT (row);
13646 if (vpos >= 0 && vpos < matrix->nrows)
13647 dump_glyph_row (MATRIX_ROW (matrix, vpos),
13648 vpos,
13649 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13650 return Qnil;
13654 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
13655 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
13656 GLYPH 0 means don't dump glyphs.
13657 GLYPH 1 means dump glyphs in short form.
13658 GLYPH > 1 or omitted means dump glyphs in long form. */)
13659 (row, glyphs)
13660 Lisp_Object row, glyphs;
13662 struct frame *sf = SELECTED_FRAME ();
13663 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
13664 int vpos;
13666 CHECK_NUMBER (row);
13667 vpos = XINT (row);
13668 if (vpos >= 0 && vpos < m->nrows)
13669 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
13670 INTEGERP (glyphs) ? XINT (glyphs) : 2);
13671 return Qnil;
13675 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
13676 doc: /* Toggle tracing of redisplay.
13677 With ARG, turn tracing on if and only if ARG is positive. */)
13678 (arg)
13679 Lisp_Object arg;
13681 if (NILP (arg))
13682 trace_redisplay_p = !trace_redisplay_p;
13683 else
13685 arg = Fprefix_numeric_value (arg);
13686 trace_redisplay_p = XINT (arg) > 0;
13689 return Qnil;
13693 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
13694 doc: /* Like `format', but print result to stderr.
13695 usage: (trace-to-stderr STRING &rest OBJECTS) */)
13696 (nargs, args)
13697 int nargs;
13698 Lisp_Object *args;
13700 Lisp_Object s = Fformat (nargs, args);
13701 fprintf (stderr, "%s", SDATA (s));
13702 return Qnil;
13705 #endif /* GLYPH_DEBUG */
13709 /***********************************************************************
13710 Building Desired Matrix Rows
13711 ***********************************************************************/
13713 /* Return a temporary glyph row holding the glyphs of an overlay
13714 arrow. Only used for non-window-redisplay windows. */
13716 static struct glyph_row *
13717 get_overlay_arrow_glyph_row (w)
13718 struct window *w;
13720 struct frame *f = XFRAME (WINDOW_FRAME (w));
13721 struct buffer *buffer = XBUFFER (w->buffer);
13722 struct buffer *old = current_buffer;
13723 const unsigned char *arrow_string = SDATA (Voverlay_arrow_string);
13724 int arrow_len = SCHARS (Voverlay_arrow_string);
13725 const unsigned char *arrow_end = arrow_string + arrow_len;
13726 const unsigned char *p;
13727 struct it it;
13728 int multibyte_p;
13729 int n_glyphs_before;
13731 set_buffer_temp (buffer);
13732 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
13733 it.glyph_row->used[TEXT_AREA] = 0;
13734 SET_TEXT_POS (it.position, 0, 0);
13736 multibyte_p = !NILP (buffer->enable_multibyte_characters);
13737 p = arrow_string;
13738 while (p < arrow_end)
13740 Lisp_Object face, ilisp;
13742 /* Get the next character. */
13743 if (multibyte_p)
13744 it.c = string_char_and_length (p, arrow_len, &it.len);
13745 else
13746 it.c = *p, it.len = 1;
13747 p += it.len;
13749 /* Get its face. */
13750 ilisp = make_number (p - arrow_string);
13751 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
13752 it.face_id = compute_char_face (f, it.c, face);
13754 /* Compute its width, get its glyphs. */
13755 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
13756 SET_TEXT_POS (it.position, -1, -1);
13757 PRODUCE_GLYPHS (&it);
13759 /* If this character doesn't fit any more in the line, we have
13760 to remove some glyphs. */
13761 if (it.current_x > it.last_visible_x)
13763 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
13764 break;
13768 set_buffer_temp (old);
13769 return it.glyph_row;
13773 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
13774 glyphs are only inserted for terminal frames since we can't really
13775 win with truncation glyphs when partially visible glyphs are
13776 involved. Which glyphs to insert is determined by
13777 produce_special_glyphs. */
13779 static void
13780 insert_left_trunc_glyphs (it)
13781 struct it *it;
13783 struct it truncate_it;
13784 struct glyph *from, *end, *to, *toend;
13786 xassert (!FRAME_WINDOW_P (it->f));
13788 /* Get the truncation glyphs. */
13789 truncate_it = *it;
13790 truncate_it.current_x = 0;
13791 truncate_it.face_id = DEFAULT_FACE_ID;
13792 truncate_it.glyph_row = &scratch_glyph_row;
13793 truncate_it.glyph_row->used[TEXT_AREA] = 0;
13794 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
13795 truncate_it.object = make_number (0);
13796 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
13798 /* Overwrite glyphs from IT with truncation glyphs. */
13799 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13800 end = from + truncate_it.glyph_row->used[TEXT_AREA];
13801 to = it->glyph_row->glyphs[TEXT_AREA];
13802 toend = to + it->glyph_row->used[TEXT_AREA];
13804 while (from < end)
13805 *to++ = *from++;
13807 /* There may be padding glyphs left over. Overwrite them too. */
13808 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
13810 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
13811 while (from < end)
13812 *to++ = *from++;
13815 if (to > toend)
13816 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
13820 /* Compute the pixel height and width of IT->glyph_row.
13822 Most of the time, ascent and height of a display line will be equal
13823 to the max_ascent and max_height values of the display iterator
13824 structure. This is not the case if
13826 1. We hit ZV without displaying anything. In this case, max_ascent
13827 and max_height will be zero.
13829 2. We have some glyphs that don't contribute to the line height.
13830 (The glyph row flag contributes_to_line_height_p is for future
13831 pixmap extensions).
13833 The first case is easily covered by using default values because in
13834 these cases, the line height does not really matter, except that it
13835 must not be zero. */
13837 static void
13838 compute_line_metrics (it)
13839 struct it *it;
13841 struct glyph_row *row = it->glyph_row;
13842 int area, i;
13844 if (FRAME_WINDOW_P (it->f))
13846 int i, min_y, max_y;
13848 /* The line may consist of one space only, that was added to
13849 place the cursor on it. If so, the row's height hasn't been
13850 computed yet. */
13851 if (row->height == 0)
13853 if (it->max_ascent + it->max_descent == 0)
13854 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
13855 row->ascent = it->max_ascent;
13856 row->height = it->max_ascent + it->max_descent;
13857 row->phys_ascent = it->max_phys_ascent;
13858 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
13861 /* Compute the width of this line. */
13862 row->pixel_width = row->x;
13863 for (i = 0; i < row->used[TEXT_AREA]; ++i)
13864 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
13866 xassert (row->pixel_width >= 0);
13867 xassert (row->ascent >= 0 && row->height > 0);
13869 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
13870 || MATRIX_ROW_OVERLAPS_PRED_P (row));
13872 /* If first line's physical ascent is larger than its logical
13873 ascent, use the physical ascent, and make the row taller.
13874 This makes accented characters fully visible. */
13875 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
13876 && row->phys_ascent > row->ascent)
13878 row->height += row->phys_ascent - row->ascent;
13879 row->ascent = row->phys_ascent;
13882 /* Compute how much of the line is visible. */
13883 row->visible_height = row->height;
13885 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
13886 max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
13888 if (row->y < min_y)
13889 row->visible_height -= min_y - row->y;
13890 if (row->y + row->height > max_y)
13891 row->visible_height -= row->y + row->height - max_y;
13893 else
13895 row->pixel_width = row->used[TEXT_AREA];
13896 if (row->continued_p)
13897 row->pixel_width -= it->continuation_pixel_width;
13898 else if (row->truncated_on_right_p)
13899 row->pixel_width -= it->truncation_pixel_width;
13900 row->ascent = row->phys_ascent = 0;
13901 row->height = row->phys_height = row->visible_height = 1;
13904 /* Compute a hash code for this row. */
13905 row->hash = 0;
13906 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
13907 for (i = 0; i < row->used[area]; ++i)
13908 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
13909 + row->glyphs[area][i].u.val
13910 + row->glyphs[area][i].face_id
13911 + row->glyphs[area][i].padding_p
13912 + (row->glyphs[area][i].type << 2));
13914 it->max_ascent = it->max_descent = 0;
13915 it->max_phys_ascent = it->max_phys_descent = 0;
13919 /* Append one space to the glyph row of iterator IT if doing a
13920 window-based redisplay. DEFAULT_FACE_P non-zero means let the
13921 space have the default face, otherwise let it have the same face as
13922 IT->face_id. Value is non-zero if a space was added.
13924 This function is called to make sure that there is always one glyph
13925 at the end of a glyph row that the cursor can be set on under
13926 window-systems. (If there weren't such a glyph we would not know
13927 how wide and tall a box cursor should be displayed).
13929 At the same time this space let's a nicely handle clearing to the
13930 end of the line if the row ends in italic text. */
13932 static int
13933 append_space (it, default_face_p)
13934 struct it *it;
13935 int default_face_p;
13937 if (FRAME_WINDOW_P (it->f))
13939 int n = it->glyph_row->used[TEXT_AREA];
13941 if (it->glyph_row->glyphs[TEXT_AREA] + n
13942 < it->glyph_row->glyphs[1 + TEXT_AREA])
13944 /* Save some values that must not be changed.
13945 Must save IT->c and IT->len because otherwise
13946 ITERATOR_AT_END_P wouldn't work anymore after
13947 append_space has been called. */
13948 enum display_element_type saved_what = it->what;
13949 int saved_c = it->c, saved_len = it->len;
13950 int saved_x = it->current_x;
13951 int saved_face_id = it->face_id;
13952 struct text_pos saved_pos;
13953 Lisp_Object saved_object;
13954 struct face *face;
13956 saved_object = it->object;
13957 saved_pos = it->position;
13959 it->what = IT_CHARACTER;
13960 bzero (&it->position, sizeof it->position);
13961 it->object = make_number (0);
13962 it->c = ' ';
13963 it->len = 1;
13965 if (default_face_p)
13966 it->face_id = DEFAULT_FACE_ID;
13967 else if (it->face_before_selective_p)
13968 it->face_id = it->saved_face_id;
13969 face = FACE_FROM_ID (it->f, it->face_id);
13970 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
13972 PRODUCE_GLYPHS (it);
13974 it->current_x = saved_x;
13975 it->object = saved_object;
13976 it->position = saved_pos;
13977 it->what = saved_what;
13978 it->face_id = saved_face_id;
13979 it->len = saved_len;
13980 it->c = saved_c;
13981 return 1;
13985 return 0;
13989 /* Extend the face of the last glyph in the text area of IT->glyph_row
13990 to the end of the display line. Called from display_line.
13991 If the glyph row is empty, add a space glyph to it so that we
13992 know the face to draw. Set the glyph row flag fill_line_p. */
13994 static void
13995 extend_face_to_end_of_line (it)
13996 struct it *it;
13998 struct face *face;
13999 struct frame *f = it->f;
14001 /* If line is already filled, do nothing. */
14002 if (it->current_x >= it->last_visible_x)
14003 return;
14005 /* Face extension extends the background and box of IT->face_id
14006 to the end of the line. If the background equals the background
14007 of the frame, we don't have to do anything. */
14008 if (it->face_before_selective_p)
14009 face = FACE_FROM_ID (it->f, it->saved_face_id);
14010 else
14011 face = FACE_FROM_ID (f, it->face_id);
14013 if (FRAME_WINDOW_P (f)
14014 && face->box == FACE_NO_BOX
14015 && face->background == FRAME_BACKGROUND_PIXEL (f)
14016 && !face->stipple)
14017 return;
14019 /* Set the glyph row flag indicating that the face of the last glyph
14020 in the text area has to be drawn to the end of the text area. */
14021 it->glyph_row->fill_line_p = 1;
14023 /* If current character of IT is not ASCII, make sure we have the
14024 ASCII face. This will be automatically undone the next time
14025 get_next_display_element returns a multibyte character. Note
14026 that the character will always be single byte in unibyte text. */
14027 if (!SINGLE_BYTE_CHAR_P (it->c))
14029 it->face_id = FACE_FOR_CHAR (f, face, 0);
14032 if (FRAME_WINDOW_P (f))
14034 /* If the row is empty, add a space with the current face of IT,
14035 so that we know which face to draw. */
14036 if (it->glyph_row->used[TEXT_AREA] == 0)
14038 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14039 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14040 it->glyph_row->used[TEXT_AREA] = 1;
14043 else
14045 /* Save some values that must not be changed. */
14046 int saved_x = it->current_x;
14047 struct text_pos saved_pos;
14048 Lisp_Object saved_object;
14049 enum display_element_type saved_what = it->what;
14050 int saved_face_id = it->face_id;
14052 saved_object = it->object;
14053 saved_pos = it->position;
14055 it->what = IT_CHARACTER;
14056 bzero (&it->position, sizeof it->position);
14057 it->object = make_number (0);
14058 it->c = ' ';
14059 it->len = 1;
14060 it->face_id = face->id;
14062 PRODUCE_GLYPHS (it);
14064 while (it->current_x <= it->last_visible_x)
14065 PRODUCE_GLYPHS (it);
14067 /* Don't count these blanks really. It would let us insert a left
14068 truncation glyph below and make us set the cursor on them, maybe. */
14069 it->current_x = saved_x;
14070 it->object = saved_object;
14071 it->position = saved_pos;
14072 it->what = saved_what;
14073 it->face_id = saved_face_id;
14078 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14079 trailing whitespace. */
14081 static int
14082 trailing_whitespace_p (charpos)
14083 int charpos;
14085 int bytepos = CHAR_TO_BYTE (charpos);
14086 int c = 0;
14088 while (bytepos < ZV_BYTE
14089 && (c = FETCH_CHAR (bytepos),
14090 c == ' ' || c == '\t'))
14091 ++bytepos;
14093 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14095 if (bytepos != PT_BYTE)
14096 return 1;
14098 return 0;
14102 /* Highlight trailing whitespace, if any, in ROW. */
14104 void
14105 highlight_trailing_whitespace (f, row)
14106 struct frame *f;
14107 struct glyph_row *row;
14109 int used = row->used[TEXT_AREA];
14111 if (used)
14113 struct glyph *start = row->glyphs[TEXT_AREA];
14114 struct glyph *glyph = start + used - 1;
14116 /* Skip over glyphs inserted to display the cursor at the
14117 end of a line, for extending the face of the last glyph
14118 to the end of the line on terminals, and for truncation
14119 and continuation glyphs. */
14120 while (glyph >= start
14121 && glyph->type == CHAR_GLYPH
14122 && INTEGERP (glyph->object))
14123 --glyph;
14125 /* If last glyph is a space or stretch, and it's trailing
14126 whitespace, set the face of all trailing whitespace glyphs in
14127 IT->glyph_row to `trailing-whitespace'. */
14128 if (glyph >= start
14129 && BUFFERP (glyph->object)
14130 && (glyph->type == STRETCH_GLYPH
14131 || (glyph->type == CHAR_GLYPH
14132 && glyph->u.ch == ' '))
14133 && trailing_whitespace_p (glyph->charpos))
14135 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
14137 while (glyph >= start
14138 && BUFFERP (glyph->object)
14139 && (glyph->type == STRETCH_GLYPH
14140 || (glyph->type == CHAR_GLYPH
14141 && glyph->u.ch == ' ')))
14142 (glyph--)->face_id = face_id;
14148 /* Value is non-zero if glyph row ROW in window W should be
14149 used to hold the cursor. */
14151 static int
14152 cursor_row_p (w, row)
14153 struct window *w;
14154 struct glyph_row *row;
14156 int cursor_row_p = 1;
14158 if (PT == MATRIX_ROW_END_CHARPOS (row))
14160 /* If the row ends with a newline from a string, we don't want
14161 the cursor there (if the row is continued it doesn't end in a
14162 newline). */
14163 if (CHARPOS (row->end.string_pos) >= 0
14164 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14165 cursor_row_p = row->continued_p;
14167 /* If the row ends at ZV, display the cursor at the end of that
14168 row instead of at the start of the row below. */
14169 else if (row->ends_at_zv_p)
14170 cursor_row_p = 1;
14171 else
14172 cursor_row_p = 0;
14175 return cursor_row_p;
14179 /* Construct the glyph row IT->glyph_row in the desired matrix of
14180 IT->w from text at the current position of IT. See dispextern.h
14181 for an overview of struct it. Value is non-zero if
14182 IT->glyph_row displays text, as opposed to a line displaying ZV
14183 only. */
14185 static int
14186 display_line (it)
14187 struct it *it;
14189 struct glyph_row *row = it->glyph_row;
14191 /* We always start displaying at hpos zero even if hscrolled. */
14192 xassert (it->hpos == 0 && it->current_x == 0);
14194 /* We must not display in a row that's not a text row. */
14195 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14196 < it->w->desired_matrix->nrows);
14198 /* Is IT->w showing the region? */
14199 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14201 /* Clear the result glyph row and enable it. */
14202 prepare_desired_row (row);
14204 row->y = it->current_y;
14205 row->start = it->current;
14206 row->continuation_lines_width = it->continuation_lines_width;
14207 row->displays_text_p = 1;
14208 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14209 it->starts_in_middle_of_char_p = 0;
14211 /* Arrange the overlays nicely for our purposes. Usually, we call
14212 display_line on only one line at a time, in which case this
14213 can't really hurt too much, or we call it on lines which appear
14214 one after another in the buffer, in which case all calls to
14215 recenter_overlay_lists but the first will be pretty cheap. */
14216 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14218 /* Move over display elements that are not visible because we are
14219 hscrolled. This may stop at an x-position < IT->first_visible_x
14220 if the first glyph is partially visible or if we hit a line end. */
14221 if (it->current_x < it->first_visible_x)
14222 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14223 MOVE_TO_POS | MOVE_TO_X);
14225 /* Get the initial row height. This is either the height of the
14226 text hscrolled, if there is any, or zero. */
14227 row->ascent = it->max_ascent;
14228 row->height = it->max_ascent + it->max_descent;
14229 row->phys_ascent = it->max_phys_ascent;
14230 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14232 /* Loop generating characters. The loop is left with IT on the next
14233 character to display. */
14234 while (1)
14236 int n_glyphs_before, hpos_before, x_before;
14237 int x, i, nglyphs;
14238 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14240 /* Retrieve the next thing to display. Value is zero if end of
14241 buffer reached. */
14242 if (!get_next_display_element (it))
14244 /* Maybe add a space at the end of this line that is used to
14245 display the cursor there under X. Set the charpos of the
14246 first glyph of blank lines not corresponding to any text
14247 to -1. */
14248 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
14249 || row->used[TEXT_AREA] == 0)
14251 row->glyphs[TEXT_AREA]->charpos = -1;
14252 row->displays_text_p = 0;
14254 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14255 && (!MINI_WINDOW_P (it->w)
14256 || (minibuf_level && EQ (it->window, minibuf_window))))
14257 row->indicate_empty_line_p = 1;
14260 it->continuation_lines_width = 0;
14261 row->ends_at_zv_p = 1;
14262 break;
14265 /* Now, get the metrics of what we want to display. This also
14266 generates glyphs in `row' (which is IT->glyph_row). */
14267 n_glyphs_before = row->used[TEXT_AREA];
14268 x = it->current_x;
14270 /* Remember the line height so far in case the next element doesn't
14271 fit on the line. */
14272 if (!it->truncate_lines_p)
14274 ascent = it->max_ascent;
14275 descent = it->max_descent;
14276 phys_ascent = it->max_phys_ascent;
14277 phys_descent = it->max_phys_descent;
14280 PRODUCE_GLYPHS (it);
14282 /* If this display element was in marginal areas, continue with
14283 the next one. */
14284 if (it->area != TEXT_AREA)
14286 row->ascent = max (row->ascent, it->max_ascent);
14287 row->height = max (row->height, it->max_ascent + it->max_descent);
14288 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14289 row->phys_height = max (row->phys_height,
14290 it->max_phys_ascent + it->max_phys_descent);
14291 set_iterator_to_next (it, 1);
14292 continue;
14295 /* Does the display element fit on the line? If we truncate
14296 lines, we should draw past the right edge of the window. If
14297 we don't truncate, we want to stop so that we can display the
14298 continuation glyph before the right margin. If lines are
14299 continued, there are two possible strategies for characters
14300 resulting in more than 1 glyph (e.g. tabs): Display as many
14301 glyphs as possible in this line and leave the rest for the
14302 continuation line, or display the whole element in the next
14303 line. Original redisplay did the former, so we do it also. */
14304 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14305 hpos_before = it->hpos;
14306 x_before = x;
14308 if (/* Not a newline. */
14309 nglyphs > 0
14310 /* Glyphs produced fit entirely in the line. */
14311 && it->current_x < it->last_visible_x)
14313 it->hpos += nglyphs;
14314 row->ascent = max (row->ascent, it->max_ascent);
14315 row->height = max (row->height, it->max_ascent + it->max_descent);
14316 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14317 row->phys_height = max (row->phys_height,
14318 it->max_phys_ascent + it->max_phys_descent);
14319 if (it->current_x - it->pixel_width < it->first_visible_x)
14320 row->x = x - it->first_visible_x;
14322 else
14324 int new_x;
14325 struct glyph *glyph;
14327 for (i = 0; i < nglyphs; ++i, x = new_x)
14329 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14330 new_x = x + glyph->pixel_width;
14332 if (/* Lines are continued. */
14333 !it->truncate_lines_p
14334 && (/* Glyph doesn't fit on the line. */
14335 new_x > it->last_visible_x
14336 /* Or it fits exactly on a window system frame. */
14337 || (new_x == it->last_visible_x
14338 && FRAME_WINDOW_P (it->f))))
14340 /* End of a continued line. */
14342 if (it->hpos == 0
14343 || (new_x == it->last_visible_x
14344 && FRAME_WINDOW_P (it->f)))
14346 /* Current glyph is the only one on the line or
14347 fits exactly on the line. We must continue
14348 the line because we can't draw the cursor
14349 after the glyph. */
14350 row->continued_p = 1;
14351 it->current_x = new_x;
14352 it->continuation_lines_width += new_x;
14353 ++it->hpos;
14354 if (i == nglyphs - 1)
14355 set_iterator_to_next (it, 1);
14357 else if (CHAR_GLYPH_PADDING_P (*glyph)
14358 && !FRAME_WINDOW_P (it->f))
14360 /* A padding glyph that doesn't fit on this line.
14361 This means the whole character doesn't fit
14362 on the line. */
14363 row->used[TEXT_AREA] = n_glyphs_before;
14365 /* Fill the rest of the row with continuation
14366 glyphs like in 20.x. */
14367 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
14368 < row->glyphs[1 + TEXT_AREA])
14369 produce_special_glyphs (it, IT_CONTINUATION);
14371 row->continued_p = 1;
14372 it->current_x = x_before;
14373 it->continuation_lines_width += x_before;
14375 /* Restore the height to what it was before the
14376 element not fitting on the line. */
14377 it->max_ascent = ascent;
14378 it->max_descent = descent;
14379 it->max_phys_ascent = phys_ascent;
14380 it->max_phys_descent = phys_descent;
14382 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
14384 /* A TAB that extends past the right edge of the
14385 window. This produces a single glyph on
14386 window system frames. We leave the glyph in
14387 this row and let it fill the row, but don't
14388 consume the TAB. */
14389 it->continuation_lines_width += it->last_visible_x;
14390 row->ends_in_middle_of_char_p = 1;
14391 row->continued_p = 1;
14392 glyph->pixel_width = it->last_visible_x - x;
14393 it->starts_in_middle_of_char_p = 1;
14395 else
14397 /* Something other than a TAB that draws past
14398 the right edge of the window. Restore
14399 positions to values before the element. */
14400 row->used[TEXT_AREA] = n_glyphs_before + i;
14402 /* Display continuation glyphs. */
14403 if (!FRAME_WINDOW_P (it->f))
14404 produce_special_glyphs (it, IT_CONTINUATION);
14405 row->continued_p = 1;
14407 it->continuation_lines_width += x;
14409 if (nglyphs > 1 && i > 0)
14411 row->ends_in_middle_of_char_p = 1;
14412 it->starts_in_middle_of_char_p = 1;
14415 /* Restore the height to what it was before the
14416 element not fitting on the line. */
14417 it->max_ascent = ascent;
14418 it->max_descent = descent;
14419 it->max_phys_ascent = phys_ascent;
14420 it->max_phys_descent = phys_descent;
14423 break;
14425 else if (new_x > it->first_visible_x)
14427 /* Increment number of glyphs actually displayed. */
14428 ++it->hpos;
14430 if (x < it->first_visible_x)
14431 /* Glyph is partially visible, i.e. row starts at
14432 negative X position. */
14433 row->x = x - it->first_visible_x;
14435 else
14437 /* Glyph is completely off the left margin of the
14438 window. This should not happen because of the
14439 move_it_in_display_line at the start of this
14440 function, unless the text display area of the
14441 window is empty. */
14442 xassert (it->first_visible_x <= it->last_visible_x);
14446 row->ascent = max (row->ascent, it->max_ascent);
14447 row->height = max (row->height, it->max_ascent + it->max_descent);
14448 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14449 row->phys_height = max (row->phys_height,
14450 it->max_phys_ascent + it->max_phys_descent);
14452 /* End of this display line if row is continued. */
14453 if (row->continued_p)
14454 break;
14457 /* Is this a line end? If yes, we're also done, after making
14458 sure that a non-default face is extended up to the right
14459 margin of the window. */
14460 if (ITERATOR_AT_END_OF_LINE_P (it))
14462 int used_before = row->used[TEXT_AREA];
14464 row->ends_in_newline_from_string_p = STRINGP (it->object);
14466 /* Add a space at the end of the line that is used to
14467 display the cursor there. */
14468 append_space (it, 0);
14470 /* Extend the face to the end of the line. */
14471 extend_face_to_end_of_line (it);
14473 /* Make sure we have the position. */
14474 if (used_before == 0)
14475 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
14477 /* Consume the line end. This skips over invisible lines. */
14478 set_iterator_to_next (it, 1);
14479 it->continuation_lines_width = 0;
14480 break;
14483 /* Proceed with next display element. Note that this skips
14484 over lines invisible because of selective display. */
14485 set_iterator_to_next (it, 1);
14487 /* If we truncate lines, we are done when the last displayed
14488 glyphs reach past the right margin of the window. */
14489 if (it->truncate_lines_p
14490 && (FRAME_WINDOW_P (it->f)
14491 ? (it->current_x >= it->last_visible_x)
14492 : (it->current_x > it->last_visible_x)))
14494 /* Maybe add truncation glyphs. */
14495 if (!FRAME_WINDOW_P (it->f))
14497 int i, n;
14499 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14500 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14501 break;
14503 for (n = row->used[TEXT_AREA]; i < n; ++i)
14505 row->used[TEXT_AREA] = i;
14506 produce_special_glyphs (it, IT_TRUNCATION);
14510 row->truncated_on_right_p = 1;
14511 it->continuation_lines_width = 0;
14512 reseat_at_next_visible_line_start (it, 0);
14513 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
14514 it->hpos = hpos_before;
14515 it->current_x = x_before;
14516 break;
14520 /* If line is not empty and hscrolled, maybe insert truncation glyphs
14521 at the left window margin. */
14522 if (it->first_visible_x
14523 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
14525 if (!FRAME_WINDOW_P (it->f))
14526 insert_left_trunc_glyphs (it);
14527 row->truncated_on_left_p = 1;
14530 /* If the start of this line is the overlay arrow-position, then
14531 mark this glyph row as the one containing the overlay arrow.
14532 This is clearly a mess with variable size fonts. It would be
14533 better to let it be displayed like cursors under X. */
14534 if (MARKERP (Voverlay_arrow_position)
14535 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
14536 && (MATRIX_ROW_START_CHARPOS (row)
14537 == marker_position (Voverlay_arrow_position))
14538 && STRINGP (Voverlay_arrow_string)
14539 && ! overlay_arrow_seen)
14541 /* Overlay arrow in window redisplay is a fringe bitmap. */
14542 if (!FRAME_WINDOW_P (it->f))
14544 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
14545 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
14546 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
14547 struct glyph *p = row->glyphs[TEXT_AREA];
14548 struct glyph *p2, *end;
14550 /* Copy the arrow glyphs. */
14551 while (glyph < arrow_end)
14552 *p++ = *glyph++;
14554 /* Throw away padding glyphs. */
14555 p2 = p;
14556 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
14557 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
14558 ++p2;
14559 if (p2 > p)
14561 while (p2 < end)
14562 *p++ = *p2++;
14563 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
14567 overlay_arrow_seen = 1;
14568 row->overlay_arrow_p = 1;
14571 /* Compute pixel dimensions of this line. */
14572 compute_line_metrics (it);
14574 /* Remember the position at which this line ends. */
14575 row->end = it->current;
14577 /* Maybe set the cursor. */
14578 if (it->w->cursor.vpos < 0
14579 && PT >= MATRIX_ROW_START_CHARPOS (row)
14580 && PT <= MATRIX_ROW_END_CHARPOS (row)
14581 && cursor_row_p (it->w, row))
14582 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
14584 /* Highlight trailing whitespace. */
14585 if (!NILP (Vshow_trailing_whitespace))
14586 highlight_trailing_whitespace (it->f, it->glyph_row);
14588 /* Prepare for the next line. This line starts horizontally at (X
14589 HPOS) = (0 0). Vertical positions are incremented. As a
14590 convenience for the caller, IT->glyph_row is set to the next
14591 row to be used. */
14592 it->current_x = it->hpos = 0;
14593 it->current_y += row->height;
14594 ++it->vpos;
14595 ++it->glyph_row;
14596 return row->displays_text_p;
14601 /***********************************************************************
14602 Menu Bar
14603 ***********************************************************************/
14605 /* Redisplay the menu bar in the frame for window W.
14607 The menu bar of X frames that don't have X toolkit support is
14608 displayed in a special window W->frame->menu_bar_window.
14610 The menu bar of terminal frames is treated specially as far as
14611 glyph matrices are concerned. Menu bar lines are not part of
14612 windows, so the update is done directly on the frame matrix rows
14613 for the menu bar. */
14615 static void
14616 display_menu_bar (w)
14617 struct window *w;
14619 struct frame *f = XFRAME (WINDOW_FRAME (w));
14620 struct it it;
14621 Lisp_Object items;
14622 int i;
14624 /* Don't do all this for graphical frames. */
14625 #ifdef HAVE_NTGUI
14626 if (!NILP (Vwindow_system))
14627 return;
14628 #endif
14629 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
14630 if (FRAME_X_P (f))
14631 return;
14632 #endif
14633 #ifdef MAC_OS
14634 if (FRAME_MAC_P (f))
14635 return;
14636 #endif
14638 #ifdef USE_X_TOOLKIT
14639 xassert (!FRAME_WINDOW_P (f));
14640 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
14641 it.first_visible_x = 0;
14642 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
14643 #else /* not USE_X_TOOLKIT */
14644 if (FRAME_WINDOW_P (f))
14646 /* Menu bar lines are displayed in the desired matrix of the
14647 dummy window menu_bar_window. */
14648 struct window *menu_w;
14649 xassert (WINDOWP (f->menu_bar_window));
14650 menu_w = XWINDOW (f->menu_bar_window);
14651 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
14652 MENU_FACE_ID);
14653 it.first_visible_x = 0;
14654 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
14656 else
14658 /* This is a TTY frame, i.e. character hpos/vpos are used as
14659 pixel x/y. */
14660 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
14661 MENU_FACE_ID);
14662 it.first_visible_x = 0;
14663 it.last_visible_x = FRAME_WIDTH (f);
14665 #endif /* not USE_X_TOOLKIT */
14667 if (! mode_line_inverse_video)
14668 /* Force the menu-bar to be displayed in the default face. */
14669 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14671 /* Clear all rows of the menu bar. */
14672 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
14674 struct glyph_row *row = it.glyph_row + i;
14675 clear_glyph_row (row);
14676 row->enabled_p = 1;
14677 row->full_width_p = 1;
14680 /* Display all items of the menu bar. */
14681 items = FRAME_MENU_BAR_ITEMS (it.f);
14682 for (i = 0; i < XVECTOR (items)->size; i += 4)
14684 Lisp_Object string;
14686 /* Stop at nil string. */
14687 string = AREF (items, i + 1);
14688 if (NILP (string))
14689 break;
14691 /* Remember where item was displayed. */
14692 AREF (items, i + 3) = make_number (it.hpos);
14694 /* Display the item, pad with one space. */
14695 if (it.current_x < it.last_visible_x)
14696 display_string (NULL, string, Qnil, 0, 0, &it,
14697 SCHARS (string) + 1, 0, 0, -1);
14700 /* Fill out the line with spaces. */
14701 if (it.current_x < it.last_visible_x)
14702 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
14704 /* Compute the total height of the lines. */
14705 compute_line_metrics (&it);
14710 /***********************************************************************
14711 Mode Line
14712 ***********************************************************************/
14714 /* Redisplay mode lines in the window tree whose root is WINDOW. If
14715 FORCE is non-zero, redisplay mode lines unconditionally.
14716 Otherwise, redisplay only mode lines that are garbaged. Value is
14717 the number of windows whose mode lines were redisplayed. */
14719 static int
14720 redisplay_mode_lines (window, force)
14721 Lisp_Object window;
14722 int force;
14724 int nwindows = 0;
14726 while (!NILP (window))
14728 struct window *w = XWINDOW (window);
14730 if (WINDOWP (w->hchild))
14731 nwindows += redisplay_mode_lines (w->hchild, force);
14732 else if (WINDOWP (w->vchild))
14733 nwindows += redisplay_mode_lines (w->vchild, force);
14734 else if (force
14735 || FRAME_GARBAGED_P (XFRAME (w->frame))
14736 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
14738 struct text_pos lpoint;
14739 struct buffer *old = current_buffer;
14741 /* Set the window's buffer for the mode line display. */
14742 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14743 set_buffer_internal_1 (XBUFFER (w->buffer));
14745 /* Point refers normally to the selected window. For any
14746 other window, set up appropriate value. */
14747 if (!EQ (window, selected_window))
14749 struct text_pos pt;
14751 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
14752 if (CHARPOS (pt) < BEGV)
14753 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14754 else if (CHARPOS (pt) > (ZV - 1))
14755 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
14756 else
14757 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
14760 /* Display mode lines. */
14761 clear_glyph_matrix (w->desired_matrix);
14762 if (display_mode_lines (w))
14764 ++nwindows;
14765 w->must_be_updated_p = 1;
14768 /* Restore old settings. */
14769 set_buffer_internal_1 (old);
14770 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14773 window = w->next;
14776 return nwindows;
14780 /* Display the mode and/or top line of window W. Value is the number
14781 of mode lines displayed. */
14783 static int
14784 display_mode_lines (w)
14785 struct window *w;
14787 Lisp_Object old_selected_window, old_selected_frame;
14788 int n = 0;
14790 old_selected_frame = selected_frame;
14791 selected_frame = w->frame;
14792 old_selected_window = selected_window;
14793 XSETWINDOW (selected_window, w);
14795 /* These will be set while the mode line specs are processed. */
14796 line_number_displayed = 0;
14797 w->column_number_displayed = Qnil;
14799 if (WINDOW_WANTS_MODELINE_P (w))
14801 struct window *sel_w = XWINDOW (old_selected_window);
14803 /* Select mode line face based on the real selected window. */
14804 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
14805 current_buffer->mode_line_format);
14806 ++n;
14809 if (WINDOW_WANTS_HEADER_LINE_P (w))
14811 display_mode_line (w, HEADER_LINE_FACE_ID,
14812 current_buffer->header_line_format);
14813 ++n;
14816 selected_frame = old_selected_frame;
14817 selected_window = old_selected_window;
14818 return n;
14822 /* Display mode or top line of window W. FACE_ID specifies which line
14823 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
14824 FORMAT is the mode line format to display. Value is the pixel
14825 height of the mode line displayed. */
14827 static int
14828 display_mode_line (w, face_id, format)
14829 struct window *w;
14830 enum face_id face_id;
14831 Lisp_Object format;
14833 struct it it;
14834 struct face *face;
14836 init_iterator (&it, w, -1, -1, NULL, face_id);
14837 prepare_desired_row (it.glyph_row);
14839 if (! mode_line_inverse_video)
14840 /* Force the mode-line to be displayed in the default face. */
14841 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
14843 /* Temporarily make frame's keyboard the current kboard so that
14844 kboard-local variables in the mode_line_format will get the right
14845 values. */
14846 push_frame_kboard (it.f);
14847 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
14848 pop_frame_kboard ();
14850 /* Fill up with spaces. */
14851 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
14853 compute_line_metrics (&it);
14854 it.glyph_row->full_width_p = 1;
14855 it.glyph_row->mode_line_p = 1;
14856 it.glyph_row->continued_p = 0;
14857 it.glyph_row->truncated_on_left_p = 0;
14858 it.glyph_row->truncated_on_right_p = 0;
14860 /* Make a 3D mode-line have a shadow at its right end. */
14861 face = FACE_FROM_ID (it.f, face_id);
14862 extend_face_to_end_of_line (&it);
14863 if (face->box != FACE_NO_BOX)
14865 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
14866 + it.glyph_row->used[TEXT_AREA] - 1);
14867 last->right_box_line_p = 1;
14870 return it.glyph_row->height;
14873 /* Alist that caches the results of :propertize.
14874 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
14875 Lisp_Object mode_line_proptrans_alist;
14877 /* List of strings making up the mode-line. */
14878 Lisp_Object mode_line_string_list;
14880 /* Base face property when building propertized mode line string. */
14881 static Lisp_Object mode_line_string_face;
14882 static Lisp_Object mode_line_string_face_prop;
14885 /* Contribute ELT to the mode line for window IT->w. How it
14886 translates into text depends on its data type.
14888 IT describes the display environment in which we display, as usual.
14890 DEPTH is the depth in recursion. It is used to prevent
14891 infinite recursion here.
14893 FIELD_WIDTH is the number of characters the display of ELT should
14894 occupy in the mode line, and PRECISION is the maximum number of
14895 characters to display from ELT's representation. See
14896 display_string for details.
14898 Returns the hpos of the end of the text generated by ELT.
14900 PROPS is a property list to add to any string we encounter.
14902 If RISKY is nonzero, remove (disregard) any properties in any string
14903 we encounter, and ignore :eval and :propertize.
14905 If the global variable `frame_title_ptr' is non-NULL, then the output
14906 is passed to `store_frame_title' instead of `display_string'. */
14908 static int
14909 display_mode_element (it, depth, field_width, precision, elt, props, risky)
14910 struct it *it;
14911 int depth;
14912 int field_width, precision;
14913 Lisp_Object elt, props;
14914 int risky;
14916 int n = 0, field, prec;
14917 int literal = 0;
14919 tail_recurse:
14920 if (depth > 10)
14921 goto invalid;
14923 depth++;
14925 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
14927 case Lisp_String:
14929 /* A string: output it and check for %-constructs within it. */
14930 unsigned char c;
14931 const unsigned char *this, *lisp_string;
14933 if (!NILP (props) || risky)
14935 Lisp_Object oprops, aelt;
14936 oprops = Ftext_properties_at (make_number (0), elt);
14938 if (NILP (Fequal (props, oprops)) || risky)
14940 /* If the starting string has properties,
14941 merge the specified ones onto the existing ones. */
14942 if (! NILP (oprops) && !risky)
14944 Lisp_Object tem;
14946 oprops = Fcopy_sequence (oprops);
14947 tem = props;
14948 while (CONSP (tem))
14950 oprops = Fplist_put (oprops, XCAR (tem),
14951 XCAR (XCDR (tem)));
14952 tem = XCDR (XCDR (tem));
14954 props = oprops;
14957 aelt = Fassoc (elt, mode_line_proptrans_alist);
14958 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
14960 mode_line_proptrans_alist
14961 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
14962 elt = XCAR (aelt);
14964 else
14966 Lisp_Object tem;
14968 elt = Fcopy_sequence (elt);
14969 Fset_text_properties (make_number (0), Flength (elt),
14970 props, elt);
14971 /* Add this item to mode_line_proptrans_alist. */
14972 mode_line_proptrans_alist
14973 = Fcons (Fcons (elt, props),
14974 mode_line_proptrans_alist);
14975 /* Truncate mode_line_proptrans_alist
14976 to at most 50 elements. */
14977 tem = Fnthcdr (make_number (50),
14978 mode_line_proptrans_alist);
14979 if (! NILP (tem))
14980 XSETCDR (tem, Qnil);
14985 this = SDATA (elt);
14986 lisp_string = this;
14988 if (literal)
14990 prec = precision - n;
14991 if (frame_title_ptr)
14992 n += store_frame_title (SDATA (elt), -1, prec);
14993 else if (!NILP (mode_line_string_list))
14994 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
14995 else
14996 n += display_string (NULL, elt, Qnil, 0, 0, it,
14997 0, prec, 0, STRING_MULTIBYTE (elt));
14999 break;
15002 while ((precision <= 0 || n < precision)
15003 && *this
15004 && (frame_title_ptr
15005 || !NILP (mode_line_string_list)
15006 || it->current_x < it->last_visible_x))
15008 const unsigned char *last = this;
15010 /* Advance to end of string or next format specifier. */
15011 while ((c = *this++) != '\0' && c != '%')
15014 if (this - 1 != last)
15016 /* Output to end of string or up to '%'. Field width
15017 is length of string. Don't output more than
15018 PRECISION allows us. */
15019 --this;
15021 prec = chars_in_text (last, this - last);
15022 if (precision > 0 && prec > precision - n)
15023 prec = precision - n;
15025 if (frame_title_ptr)
15026 n += store_frame_title (last, 0, prec);
15027 else if (!NILP (mode_line_string_list))
15029 int bytepos = last - lisp_string;
15030 int charpos = string_byte_to_char (elt, bytepos);
15031 n += store_mode_line_string (NULL,
15032 Fsubstring (elt, make_number (charpos),
15033 make_number (charpos + prec)),
15034 0, 0, 0, Qnil);
15036 else
15038 int bytepos = last - lisp_string;
15039 int charpos = string_byte_to_char (elt, bytepos);
15040 n += display_string (NULL, elt, Qnil, 0, charpos,
15041 it, 0, prec, 0,
15042 STRING_MULTIBYTE (elt));
15045 else /* c == '%' */
15047 const unsigned char *percent_position = this;
15049 /* Get the specified minimum width. Zero means
15050 don't pad. */
15051 field = 0;
15052 while ((c = *this++) >= '0' && c <= '9')
15053 field = field * 10 + c - '0';
15055 /* Don't pad beyond the total padding allowed. */
15056 if (field_width - n > 0 && field > field_width - n)
15057 field = field_width - n;
15059 /* Note that either PRECISION <= 0 or N < PRECISION. */
15060 prec = precision - n;
15062 if (c == 'M')
15063 n += display_mode_element (it, depth, field, prec,
15064 Vglobal_mode_string, props,
15065 risky);
15066 else if (c != 0)
15068 int multibyte;
15069 int bytepos, charpos;
15070 unsigned char *spec;
15072 bytepos = percent_position - lisp_string;
15073 charpos = (STRING_MULTIBYTE (elt)
15074 ? string_byte_to_char (elt, bytepos)
15075 : bytepos);
15077 spec
15078 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15080 if (frame_title_ptr)
15081 n += store_frame_title (spec, field, prec);
15082 else if (!NILP (mode_line_string_list))
15084 int len = strlen (spec);
15085 Lisp_Object tem = make_string (spec, len);
15086 props = Ftext_properties_at (make_number (charpos), elt);
15087 /* Should only keep face property in props */
15088 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15090 else
15092 int nglyphs_before, nwritten;
15094 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15095 nwritten = display_string (spec, Qnil, elt,
15096 charpos, 0, it,
15097 field, prec, 0,
15098 multibyte);
15100 /* Assign to the glyphs written above the
15101 string where the `%x' came from, position
15102 of the `%'. */
15103 if (nwritten > 0)
15105 struct glyph *glyph
15106 = (it->glyph_row->glyphs[TEXT_AREA]
15107 + nglyphs_before);
15108 int i;
15110 for (i = 0; i < nwritten; ++i)
15112 glyph[i].object = elt;
15113 glyph[i].charpos = charpos;
15116 n += nwritten;
15120 else /* c == 0 */
15121 break;
15125 break;
15127 case Lisp_Symbol:
15128 /* A symbol: process the value of the symbol recursively
15129 as if it appeared here directly. Avoid error if symbol void.
15130 Special case: if value of symbol is a string, output the string
15131 literally. */
15133 register Lisp_Object tem;
15135 /* If the variable is not marked as risky to set
15136 then its contents are risky to use. */
15137 if (NILP (Fget (elt, Qrisky_local_variable)))
15138 risky = 1;
15140 tem = Fboundp (elt);
15141 if (!NILP (tem))
15143 tem = Fsymbol_value (elt);
15144 /* If value is a string, output that string literally:
15145 don't check for % within it. */
15146 if (STRINGP (tem))
15147 literal = 1;
15149 if (!EQ (tem, elt))
15151 /* Give up right away for nil or t. */
15152 elt = tem;
15153 goto tail_recurse;
15157 break;
15159 case Lisp_Cons:
15161 register Lisp_Object car, tem;
15163 /* A cons cell: five distinct cases.
15164 If first element is :eval or :propertize, do something special.
15165 If first element is a string or a cons, process all the elements
15166 and effectively concatenate them.
15167 If first element is a negative number, truncate displaying cdr to
15168 at most that many characters. If positive, pad (with spaces)
15169 to at least that many characters.
15170 If first element is a symbol, process the cadr or caddr recursively
15171 according to whether the symbol's value is non-nil or nil. */
15172 car = XCAR (elt);
15173 if (EQ (car, QCeval))
15175 /* An element of the form (:eval FORM) means evaluate FORM
15176 and use the result as mode line elements. */
15178 if (risky)
15179 break;
15181 if (CONSP (XCDR (elt)))
15183 Lisp_Object spec;
15184 spec = safe_eval (XCAR (XCDR (elt)));
15185 n += display_mode_element (it, depth, field_width - n,
15186 precision - n, spec, props,
15187 risky);
15190 else if (EQ (car, QCpropertize))
15192 /* An element of the form (:propertize ELT PROPS...)
15193 means display ELT but applying properties PROPS. */
15195 if (risky)
15196 break;
15198 if (CONSP (XCDR (elt)))
15199 n += display_mode_element (it, depth, field_width - n,
15200 precision - n, XCAR (XCDR (elt)),
15201 XCDR (XCDR (elt)), risky);
15203 else if (SYMBOLP (car))
15205 tem = Fboundp (car);
15206 elt = XCDR (elt);
15207 if (!CONSP (elt))
15208 goto invalid;
15209 /* elt is now the cdr, and we know it is a cons cell.
15210 Use its car if CAR has a non-nil value. */
15211 if (!NILP (tem))
15213 tem = Fsymbol_value (car);
15214 if (!NILP (tem))
15216 elt = XCAR (elt);
15217 goto tail_recurse;
15220 /* Symbol's value is nil (or symbol is unbound)
15221 Get the cddr of the original list
15222 and if possible find the caddr and use that. */
15223 elt = XCDR (elt);
15224 if (NILP (elt))
15225 break;
15226 else if (!CONSP (elt))
15227 goto invalid;
15228 elt = XCAR (elt);
15229 goto tail_recurse;
15231 else if (INTEGERP (car))
15233 register int lim = XINT (car);
15234 elt = XCDR (elt);
15235 if (lim < 0)
15237 /* Negative int means reduce maximum width. */
15238 if (precision <= 0)
15239 precision = -lim;
15240 else
15241 precision = min (precision, -lim);
15243 else if (lim > 0)
15245 /* Padding specified. Don't let it be more than
15246 current maximum. */
15247 if (precision > 0)
15248 lim = min (precision, lim);
15250 /* If that's more padding than already wanted, queue it.
15251 But don't reduce padding already specified even if
15252 that is beyond the current truncation point. */
15253 field_width = max (lim, field_width);
15255 goto tail_recurse;
15257 else if (STRINGP (car) || CONSP (car))
15259 register int limit = 50;
15260 /* Limit is to protect against circular lists. */
15261 while (CONSP (elt)
15262 && --limit > 0
15263 && (precision <= 0 || n < precision))
15265 n += display_mode_element (it, depth, field_width - n,
15266 precision - n, XCAR (elt),
15267 props, risky);
15268 elt = XCDR (elt);
15272 break;
15274 default:
15275 invalid:
15276 if (frame_title_ptr)
15277 n += store_frame_title ("*invalid*", 0, precision - n);
15278 else if (!NILP (mode_line_string_list))
15279 n += store_mode_line_string ("*invalid*", Qnil, 0, 0, precision - n, Qnil);
15280 else
15281 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
15282 precision - n, 0, 0);
15283 return n;
15286 /* Pad to FIELD_WIDTH. */
15287 if (field_width > 0 && n < field_width)
15289 if (frame_title_ptr)
15290 n += store_frame_title ("", field_width - n, 0);
15291 else if (!NILP (mode_line_string_list))
15292 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15293 else
15294 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15295 0, 0, 0);
15298 return n;
15301 /* Store a mode-line string element in mode_line_string_list.
15303 If STRING is non-null, display that C string. Otherwise, the Lisp
15304 string LISP_STRING is displayed.
15306 FIELD_WIDTH is the minimum number of output glyphs to produce.
15307 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15308 with spaces. FIELD_WIDTH <= 0 means don't pad.
15310 PRECISION is the maximum number of characters to output from
15311 STRING. PRECISION <= 0 means don't truncate the string.
15313 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15314 properties to the string.
15316 PROPS are the properties to add to the string.
15317 The mode_line_string_face face property is always added to the string.
15320 static int store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
15321 char *string;
15322 Lisp_Object lisp_string;
15323 int copy_string;
15324 int field_width;
15325 int precision;
15326 Lisp_Object props;
15328 int len;
15329 int n = 0;
15331 if (string != NULL)
15333 len = strlen (string);
15334 if (precision > 0 && len > precision)
15335 len = precision;
15336 lisp_string = make_string (string, len);
15337 if (NILP (props))
15338 props = mode_line_string_face_prop;
15339 else if (!NILP (mode_line_string_face))
15341 Lisp_Object face = Fplist_get (props, Qface);
15342 props = Fcopy_sequence (props);
15343 if (NILP (face))
15344 face = mode_line_string_face;
15345 else
15346 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15347 props = Fplist_put (props, Qface, face);
15349 Fadd_text_properties (make_number (0), make_number (len),
15350 props, lisp_string);
15352 else
15354 len = XFASTINT (Flength (lisp_string));
15355 if (precision > 0 && len > precision)
15357 len = precision;
15358 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
15359 precision = -1;
15361 if (!NILP (mode_line_string_face))
15363 Lisp_Object face;
15364 if (NILP (props))
15365 props = Ftext_properties_at (make_number (0), lisp_string);
15366 face = Fplist_get (props, Qface);
15367 if (NILP (face))
15368 face = mode_line_string_face;
15369 else
15370 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
15371 props = Fcons (Qface, Fcons (face, Qnil));
15372 if (copy_string)
15373 lisp_string = Fcopy_sequence (lisp_string);
15375 if (!NILP (props))
15376 Fadd_text_properties (make_number (0), make_number (len),
15377 props, lisp_string);
15380 if (len > 0)
15382 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15383 n += len;
15386 if (field_width > len)
15388 field_width -= len;
15389 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
15390 if (!NILP (props))
15391 Fadd_text_properties (make_number (0), make_number (field_width),
15392 props, lisp_string);
15393 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
15394 n += field_width;
15397 return n;
15401 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
15402 0, 3, 0,
15403 doc: /* Return the mode-line of selected window as a string.
15404 First optional arg FORMAT specifies a different format string (see
15405 `mode-line-format' for details) to use. If FORMAT is t, return
15406 the buffer's header-line. Second optional arg WINDOW specifies a
15407 different window to use as the context for the formatting.
15408 If third optional arg NO-PROPS is non-nil, string is not propertized. */)
15409 (format, window, no_props)
15410 Lisp_Object format, window, no_props;
15412 struct it it;
15413 int len;
15414 struct window *w;
15415 struct buffer *old_buffer = NULL;
15416 enum face_id face_id = DEFAULT_FACE_ID;
15418 if (NILP (window))
15419 window = selected_window;
15420 CHECK_WINDOW (window);
15421 w = XWINDOW (window);
15422 CHECK_BUFFER (w->buffer);
15424 if (XBUFFER (w->buffer) != current_buffer)
15426 old_buffer = current_buffer;
15427 set_buffer_internal_1 (XBUFFER (w->buffer));
15430 if (NILP (format) || EQ (format, Qt))
15432 face_id = NILP (format)
15433 ? CURRENT_MODE_LINE_FACE_ID (w) :
15434 HEADER_LINE_FACE_ID;
15435 format = NILP (format)
15436 ? current_buffer->mode_line_format
15437 : current_buffer->header_line_format;
15440 init_iterator (&it, w, -1, -1, NULL, face_id);
15442 if (NILP (no_props))
15444 mode_line_string_face =
15445 (face_id == MODE_LINE_FACE_ID ? Qmode_line :
15446 face_id == MODE_LINE_INACTIVE_FACE_ID ? Qmode_line_inactive :
15447 face_id == HEADER_LINE_FACE_ID ? Qheader_line : Qnil);
15449 mode_line_string_face_prop =
15450 NILP (mode_line_string_face) ? Qnil :
15451 Fcons (Qface, Fcons (mode_line_string_face, Qnil));
15453 /* We need a dummy last element in mode_line_string_list to
15454 indicate we are building the propertized mode-line string.
15455 Using mode_line_string_face_prop here GC protects it. */
15456 mode_line_string_list =
15457 Fcons (mode_line_string_face_prop, Qnil);
15458 frame_title_ptr = NULL;
15460 else
15462 mode_line_string_face_prop = Qnil;
15463 mode_line_string_list = Qnil;
15464 frame_title_ptr = frame_title_buf;
15467 push_frame_kboard (it.f);
15468 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15469 pop_frame_kboard ();
15471 if (old_buffer)
15472 set_buffer_internal_1 (old_buffer);
15474 if (NILP (no_props))
15476 Lisp_Object str;
15477 mode_line_string_list = Fnreverse (mode_line_string_list);
15478 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
15479 make_string ("", 0));
15480 mode_line_string_face_prop = Qnil;
15481 mode_line_string_list = Qnil;
15482 return str;
15485 len = frame_title_ptr - frame_title_buf;
15486 if (len > 0 && frame_title_ptr[-1] == '-')
15488 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
15489 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
15491 frame_title_ptr += 3; /* restore last non-dash + two dashes */
15492 if (len > frame_title_ptr - frame_title_buf)
15493 len = frame_title_ptr - frame_title_buf;
15496 frame_title_ptr = NULL;
15497 return make_string (frame_title_buf, len);
15500 /* Write a null-terminated, right justified decimal representation of
15501 the positive integer D to BUF using a minimal field width WIDTH. */
15503 static void
15504 pint2str (buf, width, d)
15505 register char *buf;
15506 register int width;
15507 register int d;
15509 register char *p = buf;
15511 if (d <= 0)
15512 *p++ = '0';
15513 else
15515 while (d > 0)
15517 *p++ = d % 10 + '0';
15518 d /= 10;
15522 for (width -= (int) (p - buf); width > 0; --width)
15523 *p++ = ' ';
15524 *p-- = '\0';
15525 while (p > buf)
15527 d = *buf;
15528 *buf++ = *p;
15529 *p-- = d;
15533 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
15534 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
15535 type of CODING_SYSTEM. Return updated pointer into BUF. */
15537 static unsigned char invalid_eol_type[] = "(*invalid*)";
15539 static char *
15540 decode_mode_spec_coding (coding_system, buf, eol_flag)
15541 Lisp_Object coding_system;
15542 register char *buf;
15543 int eol_flag;
15545 Lisp_Object val;
15546 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
15547 const unsigned char *eol_str;
15548 int eol_str_len;
15549 /* The EOL conversion we are using. */
15550 Lisp_Object eoltype;
15552 val = Fget (coding_system, Qcoding_system);
15553 eoltype = Qnil;
15555 if (!VECTORP (val)) /* Not yet decided. */
15557 if (multibyte)
15558 *buf++ = '-';
15559 if (eol_flag)
15560 eoltype = eol_mnemonic_undecided;
15561 /* Don't mention EOL conversion if it isn't decided. */
15563 else
15565 Lisp_Object eolvalue;
15567 eolvalue = Fget (coding_system, Qeol_type);
15569 if (multibyte)
15570 *buf++ = XFASTINT (AREF (val, 1));
15572 if (eol_flag)
15574 /* The EOL conversion that is normal on this system. */
15576 if (NILP (eolvalue)) /* Not yet decided. */
15577 eoltype = eol_mnemonic_undecided;
15578 else if (VECTORP (eolvalue)) /* Not yet decided. */
15579 eoltype = eol_mnemonic_undecided;
15580 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
15581 eoltype = (XFASTINT (eolvalue) == 0
15582 ? eol_mnemonic_unix
15583 : (XFASTINT (eolvalue) == 1
15584 ? eol_mnemonic_dos : eol_mnemonic_mac));
15588 if (eol_flag)
15590 /* Mention the EOL conversion if it is not the usual one. */
15591 if (STRINGP (eoltype))
15593 eol_str = SDATA (eoltype);
15594 eol_str_len = SBYTES (eoltype);
15596 else if (INTEGERP (eoltype)
15597 && CHAR_VALID_P (XINT (eoltype), 0))
15599 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
15600 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
15601 eol_str = tmp;
15603 else
15605 eol_str = invalid_eol_type;
15606 eol_str_len = sizeof (invalid_eol_type) - 1;
15608 bcopy (eol_str, buf, eol_str_len);
15609 buf += eol_str_len;
15612 return buf;
15615 /* Return a string for the output of a mode line %-spec for window W,
15616 generated by character C. PRECISION >= 0 means don't return a
15617 string longer than that value. FIELD_WIDTH > 0 means pad the
15618 string returned with spaces to that value. Return 1 in *MULTIBYTE
15619 if the result is multibyte text. */
15621 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
15623 static char *
15624 decode_mode_spec (w, c, field_width, precision, multibyte)
15625 struct window *w;
15626 register int c;
15627 int field_width, precision;
15628 int *multibyte;
15630 Lisp_Object obj;
15631 struct frame *f = XFRAME (WINDOW_FRAME (w));
15632 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
15633 struct buffer *b = XBUFFER (w->buffer);
15635 obj = Qnil;
15636 *multibyte = 0;
15638 switch (c)
15640 case '*':
15641 if (!NILP (b->read_only))
15642 return "%";
15643 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15644 return "*";
15645 return "-";
15647 case '+':
15648 /* This differs from %* only for a modified read-only buffer. */
15649 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15650 return "*";
15651 if (!NILP (b->read_only))
15652 return "%";
15653 return "-";
15655 case '&':
15656 /* This differs from %* in ignoring read-only-ness. */
15657 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
15658 return "*";
15659 return "-";
15661 case '%':
15662 return "%";
15664 case '[':
15666 int i;
15667 char *p;
15669 if (command_loop_level > 5)
15670 return "[[[... ";
15671 p = decode_mode_spec_buf;
15672 for (i = 0; i < command_loop_level; i++)
15673 *p++ = '[';
15674 *p = 0;
15675 return decode_mode_spec_buf;
15678 case ']':
15680 int i;
15681 char *p;
15683 if (command_loop_level > 5)
15684 return " ...]]]";
15685 p = decode_mode_spec_buf;
15686 for (i = 0; i < command_loop_level; i++)
15687 *p++ = ']';
15688 *p = 0;
15689 return decode_mode_spec_buf;
15692 case '-':
15694 register int i;
15696 /* Let lots_of_dashes be a string of infinite length. */
15697 if (!NILP (mode_line_string_list))
15698 return "--";
15699 if (field_width <= 0
15700 || field_width > sizeof (lots_of_dashes))
15702 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
15703 decode_mode_spec_buf[i] = '-';
15704 decode_mode_spec_buf[i] = '\0';
15705 return decode_mode_spec_buf;
15707 else
15708 return lots_of_dashes;
15711 case 'b':
15712 obj = b->name;
15713 break;
15715 case 'c':
15717 int col = (int) current_column (); /* iftc */
15718 w->column_number_displayed = make_number (col);
15719 pint2str (decode_mode_spec_buf, field_width, col);
15720 return decode_mode_spec_buf;
15723 case 'F':
15724 /* %F displays the frame name. */
15725 if (!NILP (f->title))
15726 return (char *) SDATA (f->title);
15727 if (f->explicit_name || ! FRAME_WINDOW_P (f))
15728 return (char *) SDATA (f->name);
15729 return "Emacs";
15731 case 'f':
15732 obj = b->filename;
15733 break;
15735 case 'l':
15737 int startpos = XMARKER (w->start)->charpos;
15738 int startpos_byte = marker_byte_position (w->start);
15739 int line, linepos, linepos_byte, topline;
15740 int nlines, junk;
15741 int height = XFASTINT (w->height);
15743 /* If we decided that this buffer isn't suitable for line numbers,
15744 don't forget that too fast. */
15745 if (EQ (w->base_line_pos, w->buffer))
15746 goto no_value;
15747 /* But do forget it, if the window shows a different buffer now. */
15748 else if (BUFFERP (w->base_line_pos))
15749 w->base_line_pos = Qnil;
15751 /* If the buffer is very big, don't waste time. */
15752 if (INTEGERP (Vline_number_display_limit)
15753 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
15755 w->base_line_pos = Qnil;
15756 w->base_line_number = Qnil;
15757 goto no_value;
15760 if (!NILP (w->base_line_number)
15761 && !NILP (w->base_line_pos)
15762 && XFASTINT (w->base_line_pos) <= startpos)
15764 line = XFASTINT (w->base_line_number);
15765 linepos = XFASTINT (w->base_line_pos);
15766 linepos_byte = buf_charpos_to_bytepos (b, linepos);
15768 else
15770 line = 1;
15771 linepos = BUF_BEGV (b);
15772 linepos_byte = BUF_BEGV_BYTE (b);
15775 /* Count lines from base line to window start position. */
15776 nlines = display_count_lines (linepos, linepos_byte,
15777 startpos_byte,
15778 startpos, &junk);
15780 topline = nlines + line;
15782 /* Determine a new base line, if the old one is too close
15783 or too far away, or if we did not have one.
15784 "Too close" means it's plausible a scroll-down would
15785 go back past it. */
15786 if (startpos == BUF_BEGV (b))
15788 w->base_line_number = make_number (topline);
15789 w->base_line_pos = make_number (BUF_BEGV (b));
15791 else if (nlines < height + 25 || nlines > height * 3 + 50
15792 || linepos == BUF_BEGV (b))
15794 int limit = BUF_BEGV (b);
15795 int limit_byte = BUF_BEGV_BYTE (b);
15796 int position;
15797 int distance = (height * 2 + 30) * line_number_display_limit_width;
15799 if (startpos - distance > limit)
15801 limit = startpos - distance;
15802 limit_byte = CHAR_TO_BYTE (limit);
15805 nlines = display_count_lines (startpos, startpos_byte,
15806 limit_byte,
15807 - (height * 2 + 30),
15808 &position);
15809 /* If we couldn't find the lines we wanted within
15810 line_number_display_limit_width chars per line,
15811 give up on line numbers for this window. */
15812 if (position == limit_byte && limit == startpos - distance)
15814 w->base_line_pos = w->buffer;
15815 w->base_line_number = Qnil;
15816 goto no_value;
15819 w->base_line_number = make_number (topline - nlines);
15820 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
15823 /* Now count lines from the start pos to point. */
15824 nlines = display_count_lines (startpos, startpos_byte,
15825 PT_BYTE, PT, &junk);
15827 /* Record that we did display the line number. */
15828 line_number_displayed = 1;
15830 /* Make the string to show. */
15831 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
15832 return decode_mode_spec_buf;
15833 no_value:
15835 char* p = decode_mode_spec_buf;
15836 int pad = field_width - 2;
15837 while (pad-- > 0)
15838 *p++ = ' ';
15839 *p++ = '?';
15840 *p++ = '?';
15841 *p = '\0';
15842 return decode_mode_spec_buf;
15845 break;
15847 case 'm':
15848 obj = b->mode_name;
15849 break;
15851 case 'n':
15852 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
15853 return " Narrow";
15854 break;
15856 case 'p':
15858 int pos = marker_position (w->start);
15859 int total = BUF_ZV (b) - BUF_BEGV (b);
15861 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
15863 if (pos <= BUF_BEGV (b))
15864 return "All";
15865 else
15866 return "Bottom";
15868 else if (pos <= BUF_BEGV (b))
15869 return "Top";
15870 else
15872 if (total > 1000000)
15873 /* Do it differently for a large value, to avoid overflow. */
15874 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15875 else
15876 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
15877 /* We can't normally display a 3-digit number,
15878 so get us a 2-digit number that is close. */
15879 if (total == 100)
15880 total = 99;
15881 sprintf (decode_mode_spec_buf, "%2d%%", total);
15882 return decode_mode_spec_buf;
15886 /* Display percentage of size above the bottom of the screen. */
15887 case 'P':
15889 int toppos = marker_position (w->start);
15890 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
15891 int total = BUF_ZV (b) - BUF_BEGV (b);
15893 if (botpos >= BUF_ZV (b))
15895 if (toppos <= BUF_BEGV (b))
15896 return "All";
15897 else
15898 return "Bottom";
15900 else
15902 if (total > 1000000)
15903 /* Do it differently for a large value, to avoid overflow. */
15904 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
15905 else
15906 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
15907 /* We can't normally display a 3-digit number,
15908 so get us a 2-digit number that is close. */
15909 if (total == 100)
15910 total = 99;
15911 if (toppos <= BUF_BEGV (b))
15912 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
15913 else
15914 sprintf (decode_mode_spec_buf, "%2d%%", total);
15915 return decode_mode_spec_buf;
15919 case 's':
15920 /* status of process */
15921 obj = Fget_buffer_process (w->buffer);
15922 if (NILP (obj))
15923 return "no process";
15924 #ifdef subprocesses
15925 obj = Fsymbol_name (Fprocess_status (obj));
15926 #endif
15927 break;
15929 case 't': /* indicate TEXT or BINARY */
15930 #ifdef MODE_LINE_BINARY_TEXT
15931 return MODE_LINE_BINARY_TEXT (b);
15932 #else
15933 return "T";
15934 #endif
15936 case 'z':
15937 /* coding-system (not including end-of-line format) */
15938 case 'Z':
15939 /* coding-system (including end-of-line type) */
15941 int eol_flag = (c == 'Z');
15942 char *p = decode_mode_spec_buf;
15944 if (! FRAME_WINDOW_P (f))
15946 /* No need to mention EOL here--the terminal never needs
15947 to do EOL conversion. */
15948 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
15949 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
15951 p = decode_mode_spec_coding (b->buffer_file_coding_system,
15952 p, eol_flag);
15954 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
15955 #ifdef subprocesses
15956 obj = Fget_buffer_process (Fcurrent_buffer ());
15957 if (PROCESSP (obj))
15959 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
15960 p, eol_flag);
15961 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
15962 p, eol_flag);
15964 #endif /* subprocesses */
15965 #endif /* 0 */
15966 *p = 0;
15967 return decode_mode_spec_buf;
15971 if (STRINGP (obj))
15973 *multibyte = STRING_MULTIBYTE (obj);
15974 return (char *) SDATA (obj);
15976 else
15977 return "";
15981 /* Count up to COUNT lines starting from START / START_BYTE.
15982 But don't go beyond LIMIT_BYTE.
15983 Return the number of lines thus found (always nonnegative).
15985 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
15987 static int
15988 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
15989 int start, start_byte, limit_byte, count;
15990 int *byte_pos_ptr;
15992 register unsigned char *cursor;
15993 unsigned char *base;
15995 register int ceiling;
15996 register unsigned char *ceiling_addr;
15997 int orig_count = count;
15999 /* If we are not in selective display mode,
16000 check only for newlines. */
16001 int selective_display = (!NILP (current_buffer->selective_display)
16002 && !INTEGERP (current_buffer->selective_display));
16004 if (count > 0)
16006 while (start_byte < limit_byte)
16008 ceiling = BUFFER_CEILING_OF (start_byte);
16009 ceiling = min (limit_byte - 1, ceiling);
16010 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16011 base = (cursor = BYTE_POS_ADDR (start_byte));
16012 while (1)
16014 if (selective_display)
16015 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16017 else
16018 while (*cursor != '\n' && ++cursor != ceiling_addr)
16021 if (cursor != ceiling_addr)
16023 if (--count == 0)
16025 start_byte += cursor - base + 1;
16026 *byte_pos_ptr = start_byte;
16027 return orig_count;
16029 else
16030 if (++cursor == ceiling_addr)
16031 break;
16033 else
16034 break;
16036 start_byte += cursor - base;
16039 else
16041 while (start_byte > limit_byte)
16043 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16044 ceiling = max (limit_byte, ceiling);
16045 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16046 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16047 while (1)
16049 if (selective_display)
16050 while (--cursor != ceiling_addr
16051 && *cursor != '\n' && *cursor != 015)
16053 else
16054 while (--cursor != ceiling_addr && *cursor != '\n')
16057 if (cursor != ceiling_addr)
16059 if (++count == 0)
16061 start_byte += cursor - base + 1;
16062 *byte_pos_ptr = start_byte;
16063 /* When scanning backwards, we should
16064 not count the newline posterior to which we stop. */
16065 return - orig_count - 1;
16068 else
16069 break;
16071 /* Here we add 1 to compensate for the last decrement
16072 of CURSOR, which took it past the valid range. */
16073 start_byte += cursor - base + 1;
16077 *byte_pos_ptr = limit_byte;
16079 if (count < 0)
16080 return - orig_count + count;
16081 return orig_count - count;
16087 /***********************************************************************
16088 Displaying strings
16089 ***********************************************************************/
16091 /* Display a NUL-terminated string, starting with index START.
16093 If STRING is non-null, display that C string. Otherwise, the Lisp
16094 string LISP_STRING is displayed.
16096 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16097 FACE_STRING. Display STRING or LISP_STRING with the face at
16098 FACE_STRING_POS in FACE_STRING:
16100 Display the string in the environment given by IT, but use the
16101 standard display table, temporarily.
16103 FIELD_WIDTH is the minimum number of output glyphs to produce.
16104 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16105 with spaces. If STRING has more characters, more than FIELD_WIDTH
16106 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16108 PRECISION is the maximum number of characters to output from
16109 STRING. PRECISION < 0 means don't truncate the string.
16111 This is roughly equivalent to printf format specifiers:
16113 FIELD_WIDTH PRECISION PRINTF
16114 ----------------------------------------
16115 -1 -1 %s
16116 -1 10 %.10s
16117 10 -1 %10s
16118 20 10 %20.10s
16120 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16121 display them, and < 0 means obey the current buffer's value of
16122 enable_multibyte_characters.
16124 Value is the number of glyphs produced. */
16126 static int
16127 display_string (string, lisp_string, face_string, face_string_pos,
16128 start, it, field_width, precision, max_x, multibyte)
16129 unsigned char *string;
16130 Lisp_Object lisp_string;
16131 Lisp_Object face_string;
16132 int face_string_pos;
16133 int start;
16134 struct it *it;
16135 int field_width, precision, max_x;
16136 int multibyte;
16138 int hpos_at_start = it->hpos;
16139 int saved_face_id = it->face_id;
16140 struct glyph_row *row = it->glyph_row;
16142 /* Initialize the iterator IT for iteration over STRING beginning
16143 with index START. */
16144 reseat_to_string (it, string, lisp_string, start,
16145 precision, field_width, multibyte);
16147 /* If displaying STRING, set up the face of the iterator
16148 from LISP_STRING, if that's given. */
16149 if (STRINGP (face_string))
16151 int endptr;
16152 struct face *face;
16154 it->face_id
16155 = face_at_string_position (it->w, face_string, face_string_pos,
16156 0, it->region_beg_charpos,
16157 it->region_end_charpos,
16158 &endptr, it->base_face_id, 0);
16159 face = FACE_FROM_ID (it->f, it->face_id);
16160 it->face_box_p = face->box != FACE_NO_BOX;
16163 /* Set max_x to the maximum allowed X position. Don't let it go
16164 beyond the right edge of the window. */
16165 if (max_x <= 0)
16166 max_x = it->last_visible_x;
16167 else
16168 max_x = min (max_x, it->last_visible_x);
16170 /* Skip over display elements that are not visible. because IT->w is
16171 hscrolled. */
16172 if (it->current_x < it->first_visible_x)
16173 move_it_in_display_line_to (it, 100000, it->first_visible_x,
16174 MOVE_TO_POS | MOVE_TO_X);
16176 row->ascent = it->max_ascent;
16177 row->height = it->max_ascent + it->max_descent;
16178 row->phys_ascent = it->max_phys_ascent;
16179 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16181 /* This condition is for the case that we are called with current_x
16182 past last_visible_x. */
16183 while (it->current_x < max_x)
16185 int x_before, x, n_glyphs_before, i, nglyphs;
16187 /* Get the next display element. */
16188 if (!get_next_display_element (it))
16189 break;
16191 /* Produce glyphs. */
16192 x_before = it->current_x;
16193 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
16194 PRODUCE_GLYPHS (it);
16196 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
16197 i = 0;
16198 x = x_before;
16199 while (i < nglyphs)
16201 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16203 if (!it->truncate_lines_p
16204 && x + glyph->pixel_width > max_x)
16206 /* End of continued line or max_x reached. */
16207 if (CHAR_GLYPH_PADDING_P (*glyph))
16209 /* A wide character is unbreakable. */
16210 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
16211 it->current_x = x_before;
16213 else
16215 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
16216 it->current_x = x;
16218 break;
16220 else if (x + glyph->pixel_width > it->first_visible_x)
16222 /* Glyph is at least partially visible. */
16223 ++it->hpos;
16224 if (x < it->first_visible_x)
16225 it->glyph_row->x = x - it->first_visible_x;
16227 else
16229 /* Glyph is off the left margin of the display area.
16230 Should not happen. */
16231 abort ();
16234 row->ascent = max (row->ascent, it->max_ascent);
16235 row->height = max (row->height, it->max_ascent + it->max_descent);
16236 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16237 row->phys_height = max (row->phys_height,
16238 it->max_phys_ascent + it->max_phys_descent);
16239 x += glyph->pixel_width;
16240 ++i;
16243 /* Stop if max_x reached. */
16244 if (i < nglyphs)
16245 break;
16247 /* Stop at line ends. */
16248 if (ITERATOR_AT_END_OF_LINE_P (it))
16250 it->continuation_lines_width = 0;
16251 break;
16254 set_iterator_to_next (it, 1);
16256 /* Stop if truncating at the right edge. */
16257 if (it->truncate_lines_p
16258 && it->current_x >= it->last_visible_x)
16260 /* Add truncation mark, but don't do it if the line is
16261 truncated at a padding space. */
16262 if (IT_CHARPOS (*it) < it->string_nchars)
16264 if (!FRAME_WINDOW_P (it->f))
16266 int i, n;
16268 if (it->current_x > it->last_visible_x)
16270 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16271 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16272 break;
16273 for (n = row->used[TEXT_AREA]; i < n; ++i)
16275 row->used[TEXT_AREA] = i;
16276 produce_special_glyphs (it, IT_TRUNCATION);
16279 produce_special_glyphs (it, IT_TRUNCATION);
16281 it->glyph_row->truncated_on_right_p = 1;
16283 break;
16287 /* Maybe insert a truncation at the left. */
16288 if (it->first_visible_x
16289 && IT_CHARPOS (*it) > 0)
16291 if (!FRAME_WINDOW_P (it->f))
16292 insert_left_trunc_glyphs (it);
16293 it->glyph_row->truncated_on_left_p = 1;
16296 it->face_id = saved_face_id;
16298 /* Value is number of columns displayed. */
16299 return it->hpos - hpos_at_start;
16304 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
16305 appears as an element of LIST or as the car of an element of LIST.
16306 If PROPVAL is a list, compare each element against LIST in that
16307 way, and return 1/2 if any element of PROPVAL is found in LIST.
16308 Otherwise return 0. This function cannot quit.
16309 The return value is 2 if the text is invisible but with an ellipsis
16310 and 1 if it's invisible and without an ellipsis. */
16313 invisible_p (propval, list)
16314 register Lisp_Object propval;
16315 Lisp_Object list;
16317 register Lisp_Object tail, proptail;
16319 for (tail = list; CONSP (tail); tail = XCDR (tail))
16321 register Lisp_Object tem;
16322 tem = XCAR (tail);
16323 if (EQ (propval, tem))
16324 return 1;
16325 if (CONSP (tem) && EQ (propval, XCAR (tem)))
16326 return NILP (XCDR (tem)) ? 1 : 2;
16329 if (CONSP (propval))
16331 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
16333 Lisp_Object propelt;
16334 propelt = XCAR (proptail);
16335 for (tail = list; CONSP (tail); tail = XCDR (tail))
16337 register Lisp_Object tem;
16338 tem = XCAR (tail);
16339 if (EQ (propelt, tem))
16340 return 1;
16341 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
16342 return NILP (XCDR (tem)) ? 1 : 2;
16347 return 0;
16351 /***********************************************************************
16352 Glyph Display
16353 ***********************************************************************/
16355 #ifdef HAVE_WINDOW_SYSTEM
16357 #if GLYPH_DEBUG
16359 void
16360 dump_glyph_string (s)
16361 struct glyph_string *s;
16363 fprintf (stderr, "glyph string\n");
16364 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
16365 s->x, s->y, s->width, s->height);
16366 fprintf (stderr, " ybase = %d\n", s->ybase);
16367 fprintf (stderr, " hl = %d\n", s->hl);
16368 fprintf (stderr, " left overhang = %d, right = %d\n",
16369 s->left_overhang, s->right_overhang);
16370 fprintf (stderr, " nchars = %d\n", s->nchars);
16371 fprintf (stderr, " extends to end of line = %d\n",
16372 s->extends_to_end_of_line_p);
16373 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
16374 fprintf (stderr, " bg width = %d\n", s->background_width);
16377 #endif /* GLYPH_DEBUG */
16379 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
16380 of XChar2b structures for S; it can't be allocated in
16381 init_glyph_string because it must be allocated via `alloca'. W
16382 is the window on which S is drawn. ROW and AREA are the glyph row
16383 and area within the row from which S is constructed. START is the
16384 index of the first glyph structure covered by S. HL is a
16385 face-override for drawing S. */
16387 #ifdef HAVE_NTGUI
16388 #define OPTIONAL_HDC(hdc) hdc,
16389 #define DECLARE_HDC(hdc) HDC hdc;
16390 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
16391 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
16392 #endif
16394 #ifndef OPTIONAL_HDC
16395 #define OPTIONAL_HDC(hdc)
16396 #define DECLARE_HDC(hdc)
16397 #define ALLOCATE_HDC(hdc, f)
16398 #define RELEASE_HDC(hdc, f)
16399 #endif
16401 static void
16402 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
16403 struct glyph_string *s;
16404 DECLARE_HDC (hdc)
16405 XChar2b *char2b;
16406 struct window *w;
16407 struct glyph_row *row;
16408 enum glyph_row_area area;
16409 int start;
16410 enum draw_glyphs_face hl;
16412 bzero (s, sizeof *s);
16413 s->w = w;
16414 s->f = XFRAME (w->frame);
16415 #ifdef HAVE_NTGUI
16416 s->hdc = hdc;
16417 #endif
16418 s->display = FRAME_X_DISPLAY (s->f);
16419 s->window = FRAME_X_WINDOW (s->f);
16420 s->char2b = char2b;
16421 s->hl = hl;
16422 s->row = row;
16423 s->area = area;
16424 s->first_glyph = row->glyphs[area] + start;
16425 s->height = row->height;
16426 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
16428 /* Display the internal border below the tool-bar window. */
16429 if (s->w == XWINDOW (s->f->tool_bar_window))
16430 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
16432 s->ybase = s->y + row->ascent;
16436 /* Append the list of glyph strings with head H and tail T to the list
16437 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
16439 static INLINE void
16440 append_glyph_string_lists (head, tail, h, t)
16441 struct glyph_string **head, **tail;
16442 struct glyph_string *h, *t;
16444 if (h)
16446 if (*head)
16447 (*tail)->next = h;
16448 else
16449 *head = h;
16450 h->prev = *tail;
16451 *tail = t;
16456 /* Prepend the list of glyph strings with head H and tail T to the
16457 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
16458 result. */
16460 static INLINE void
16461 prepend_glyph_string_lists (head, tail, h, t)
16462 struct glyph_string **head, **tail;
16463 struct glyph_string *h, *t;
16465 if (h)
16467 if (*head)
16468 (*head)->prev = t;
16469 else
16470 *tail = t;
16471 t->next = *head;
16472 *head = h;
16477 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
16478 Set *HEAD and *TAIL to the resulting list. */
16480 static INLINE void
16481 append_glyph_string (head, tail, s)
16482 struct glyph_string **head, **tail;
16483 struct glyph_string *s;
16485 s->next = s->prev = NULL;
16486 append_glyph_string_lists (head, tail, s, s);
16490 /* Get face and two-byte form of character glyph GLYPH on frame F.
16491 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
16492 a pointer to a realized face that is ready for display. */
16494 static INLINE struct face *
16495 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
16496 struct frame *f;
16497 struct glyph *glyph;
16498 XChar2b *char2b;
16499 int *two_byte_p;
16501 struct face *face;
16503 xassert (glyph->type == CHAR_GLYPH);
16504 face = FACE_FROM_ID (f, glyph->face_id);
16506 if (two_byte_p)
16507 *two_byte_p = 0;
16509 if (!glyph->multibyte_p)
16511 /* Unibyte case. We don't have to encode, but we have to make
16512 sure to use a face suitable for unibyte. */
16513 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16515 else if (glyph->u.ch < 128
16516 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
16518 /* Case of ASCII in a face known to fit ASCII. */
16519 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
16521 else
16523 int c1, c2, charset;
16525 /* Split characters into bytes. If c2 is -1 afterwards, C is
16526 really a one-byte character so that byte1 is zero. */
16527 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
16528 if (c2 > 0)
16529 STORE_XCHAR2B (char2b, c1, c2);
16530 else
16531 STORE_XCHAR2B (char2b, 0, c1);
16533 /* Maybe encode the character in *CHAR2B. */
16534 if (charset != CHARSET_ASCII)
16536 struct font_info *font_info
16537 = FONT_INFO_FROM_ID (f, face->font_info_id);
16538 if (font_info)
16539 glyph->font_type
16540 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
16544 /* Make sure X resources of the face are allocated. */
16545 xassert (face != NULL);
16546 PREPARE_FACE_FOR_DISPLAY (f, face);
16547 return face;
16551 /* Fill glyph string S with composition components specified by S->cmp.
16553 FACES is an array of faces for all components of this composition.
16554 S->gidx is the index of the first component for S.
16555 OVERLAPS_P non-zero means S should draw the foreground only, and
16556 use its physical height for clipping.
16558 Value is the index of a component not in S. */
16560 static int
16561 fill_composite_glyph_string (s, faces, overlaps_p)
16562 struct glyph_string *s;
16563 struct face **faces;
16564 int overlaps_p;
16566 int i;
16568 xassert (s);
16570 s->for_overlaps_p = overlaps_p;
16572 s->face = faces[s->gidx];
16573 s->font = s->face->font;
16574 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16576 /* For all glyphs of this composition, starting at the offset
16577 S->gidx, until we reach the end of the definition or encounter a
16578 glyph that requires the different face, add it to S. */
16579 ++s->nchars;
16580 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
16581 ++s->nchars;
16583 /* All glyph strings for the same composition has the same width,
16584 i.e. the width set for the first component of the composition. */
16586 s->width = s->first_glyph->pixel_width;
16588 /* If the specified font could not be loaded, use the frame's
16589 default font, but record the fact that we couldn't load it in
16590 the glyph string so that we can draw rectangles for the
16591 characters of the glyph string. */
16592 if (s->font == NULL)
16594 s->font_not_found_p = 1;
16595 s->font = FRAME_FONT (s->f);
16598 /* Adjust base line for subscript/superscript text. */
16599 s->ybase += s->first_glyph->voffset;
16601 xassert (s->face && s->face->gc);
16603 /* This glyph string must always be drawn with 16-bit functions. */
16604 s->two_byte_p = 1;
16606 return s->gidx + s->nchars;
16610 /* Fill glyph string S from a sequence of character glyphs.
16612 FACE_ID is the face id of the string. START is the index of the
16613 first glyph to consider, END is the index of the last + 1.
16614 OVERLAPS_P non-zero means S should draw the foreground only, and
16615 use its physical height for clipping.
16617 Value is the index of the first glyph not in S. */
16619 static int
16620 fill_glyph_string (s, face_id, start, end, overlaps_p)
16621 struct glyph_string *s;
16622 int face_id;
16623 int start, end, overlaps_p;
16625 struct glyph *glyph, *last;
16626 int voffset;
16627 int glyph_not_available_p;
16629 xassert (s->f == XFRAME (s->w->frame));
16630 xassert (s->nchars == 0);
16631 xassert (start >= 0 && end > start);
16633 s->for_overlaps_p = overlaps_p,
16634 glyph = s->row->glyphs[s->area] + start;
16635 last = s->row->glyphs[s->area] + end;
16636 voffset = glyph->voffset;
16638 glyph_not_available_p = glyph->glyph_not_available_p;
16640 while (glyph < last
16641 && glyph->type == CHAR_GLYPH
16642 && glyph->voffset == voffset
16643 /* Same face id implies same font, nowadays. */
16644 && glyph->face_id == face_id
16645 && glyph->glyph_not_available_p == glyph_not_available_p)
16647 int two_byte_p;
16649 s->face = get_glyph_face_and_encoding (s->f, glyph,
16650 s->char2b + s->nchars,
16651 &two_byte_p);
16652 s->two_byte_p = two_byte_p;
16653 ++s->nchars;
16654 xassert (s->nchars <= end - start);
16655 s->width += glyph->pixel_width;
16656 ++glyph;
16659 s->font = s->face->font;
16660 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16662 /* If the specified font could not be loaded, use the frame's font,
16663 but record the fact that we couldn't load it in
16664 S->font_not_found_p so that we can draw rectangles for the
16665 characters of the glyph string. */
16666 if (s->font == NULL || glyph_not_available_p)
16668 s->font_not_found_p = 1;
16669 s->font = FRAME_FONT (s->f);
16672 /* Adjust base line for subscript/superscript text. */
16673 s->ybase += voffset;
16675 xassert (s->face && s->face->gc);
16676 return glyph - s->row->glyphs[s->area];
16680 /* Fill glyph string S from image glyph S->first_glyph. */
16682 static void
16683 fill_image_glyph_string (s)
16684 struct glyph_string *s;
16686 xassert (s->first_glyph->type == IMAGE_GLYPH);
16687 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
16688 xassert (s->img);
16689 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
16690 s->font = s->face->font;
16691 s->width = s->first_glyph->pixel_width;
16693 /* Adjust base line for subscript/superscript text. */
16694 s->ybase += s->first_glyph->voffset;
16698 /* Fill glyph string S from a sequence of stretch glyphs.
16700 ROW is the glyph row in which the glyphs are found, AREA is the
16701 area within the row. START is the index of the first glyph to
16702 consider, END is the index of the last + 1.
16704 Value is the index of the first glyph not in S. */
16706 static int
16707 fill_stretch_glyph_string (s, row, area, start, end)
16708 struct glyph_string *s;
16709 struct glyph_row *row;
16710 enum glyph_row_area area;
16711 int start, end;
16713 struct glyph *glyph, *last;
16714 int voffset, face_id;
16716 xassert (s->first_glyph->type == STRETCH_GLYPH);
16718 glyph = s->row->glyphs[s->area] + start;
16719 last = s->row->glyphs[s->area] + end;
16720 face_id = glyph->face_id;
16721 s->face = FACE_FROM_ID (s->f, face_id);
16722 s->font = s->face->font;
16723 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
16724 s->width = glyph->pixel_width;
16725 voffset = glyph->voffset;
16727 for (++glyph;
16728 (glyph < last
16729 && glyph->type == STRETCH_GLYPH
16730 && glyph->voffset == voffset
16731 && glyph->face_id == face_id);
16732 ++glyph)
16733 s->width += glyph->pixel_width;
16735 /* Adjust base line for subscript/superscript text. */
16736 s->ybase += voffset;
16738 /* The case that face->gc == 0 is handled when drawing the glyph
16739 string by calling PREPARE_FACE_FOR_DISPLAY. */
16740 xassert (s->face);
16741 return glyph - s->row->glyphs[s->area];
16745 /* EXPORT for RIF:
16746 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
16747 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
16748 assumed to be zero. */
16750 void
16751 x_get_glyph_overhangs (glyph, f, left, right)
16752 struct glyph *glyph;
16753 struct frame *f;
16754 int *left, *right;
16756 *left = *right = 0;
16758 if (glyph->type == CHAR_GLYPH)
16760 XFontStruct *font;
16761 struct face *face;
16762 struct font_info *font_info;
16763 XChar2b char2b;
16764 XCharStruct *pcm;
16766 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
16767 font = face->font;
16768 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
16769 if (font /* ++KFS: Should this be font_info ? */
16770 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
16772 if (pcm->rbearing > pcm->width)
16773 *right = pcm->rbearing - pcm->width;
16774 if (pcm->lbearing < 0)
16775 *left = -pcm->lbearing;
16781 /* Return the index of the first glyph preceding glyph string S that
16782 is overwritten by S because of S's left overhang. Value is -1
16783 if no glyphs are overwritten. */
16785 static int
16786 left_overwritten (s)
16787 struct glyph_string *s;
16789 int k;
16791 if (s->left_overhang)
16793 int x = 0, i;
16794 struct glyph *glyphs = s->row->glyphs[s->area];
16795 int first = s->first_glyph - glyphs;
16797 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
16798 x -= glyphs[i].pixel_width;
16800 k = i + 1;
16802 else
16803 k = -1;
16805 return k;
16809 /* Return the index of the first glyph preceding glyph string S that
16810 is overwriting S because of its right overhang. Value is -1 if no
16811 glyph in front of S overwrites S. */
16813 static int
16814 left_overwriting (s)
16815 struct glyph_string *s;
16817 int i, k, x;
16818 struct glyph *glyphs = s->row->glyphs[s->area];
16819 int first = s->first_glyph - glyphs;
16821 k = -1;
16822 x = 0;
16823 for (i = first - 1; i >= 0; --i)
16825 int left, right;
16826 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16827 if (x + right > 0)
16828 k = i;
16829 x -= glyphs[i].pixel_width;
16832 return k;
16836 /* Return the index of the last glyph following glyph string S that is
16837 not overwritten by S because of S's right overhang. Value is -1 if
16838 no such glyph is found. */
16840 static int
16841 right_overwritten (s)
16842 struct glyph_string *s;
16844 int k = -1;
16846 if (s->right_overhang)
16848 int x = 0, i;
16849 struct glyph *glyphs = s->row->glyphs[s->area];
16850 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16851 int end = s->row->used[s->area];
16853 for (i = first; i < end && s->right_overhang > x; ++i)
16854 x += glyphs[i].pixel_width;
16856 k = i;
16859 return k;
16863 /* Return the index of the last glyph following glyph string S that
16864 overwrites S because of its left overhang. Value is negative
16865 if no such glyph is found. */
16867 static int
16868 right_overwriting (s)
16869 struct glyph_string *s;
16871 int i, k, x;
16872 int end = s->row->used[s->area];
16873 struct glyph *glyphs = s->row->glyphs[s->area];
16874 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
16876 k = -1;
16877 x = 0;
16878 for (i = first; i < end; ++i)
16880 int left, right;
16881 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
16882 if (x - left < 0)
16883 k = i;
16884 x += glyphs[i].pixel_width;
16887 return k;
16891 /* Get face and two-byte form of character C in face FACE_ID on frame
16892 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
16893 means we want to display multibyte text. DISPLAY_P non-zero means
16894 make sure that X resources for the face returned are allocated.
16895 Value is a pointer to a realized face that is ready for display if
16896 DISPLAY_P is non-zero. */
16898 static INLINE struct face *
16899 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
16900 struct frame *f;
16901 int c, face_id;
16902 XChar2b *char2b;
16903 int multibyte_p, display_p;
16905 struct face *face = FACE_FROM_ID (f, face_id);
16907 if (!multibyte_p)
16909 /* Unibyte case. We don't have to encode, but we have to make
16910 sure to use a face suitable for unibyte. */
16911 STORE_XCHAR2B (char2b, 0, c);
16912 face_id = FACE_FOR_CHAR (f, face, c);
16913 face = FACE_FROM_ID (f, face_id);
16915 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
16917 /* Case of ASCII in a face known to fit ASCII. */
16918 STORE_XCHAR2B (char2b, 0, c);
16920 else
16922 int c1, c2, charset;
16924 /* Split characters into bytes. If c2 is -1 afterwards, C is
16925 really a one-byte character so that byte1 is zero. */
16926 SPLIT_CHAR (c, charset, c1, c2);
16927 if (c2 > 0)
16928 STORE_XCHAR2B (char2b, c1, c2);
16929 else
16930 STORE_XCHAR2B (char2b, 0, c1);
16932 /* Maybe encode the character in *CHAR2B. */
16933 if (face->font != NULL)
16935 struct font_info *font_info
16936 = FONT_INFO_FROM_ID (f, face->font_info_id);
16937 if (font_info)
16938 rif->encode_char (c, char2b, font_info, 0);
16942 /* Make sure X resources of the face are allocated. */
16943 #ifdef HAVE_X_WINDOWS
16944 if (display_p)
16945 #endif
16947 xassert (face != NULL);
16948 PREPARE_FACE_FOR_DISPLAY (f, face);
16951 return face;
16955 /* Set background width of glyph string S. START is the index of the
16956 first glyph following S. LAST_X is the right-most x-position + 1
16957 in the drawing area. */
16959 static INLINE void
16960 set_glyph_string_background_width (s, start, last_x)
16961 struct glyph_string *s;
16962 int start;
16963 int last_x;
16965 /* If the face of this glyph string has to be drawn to the end of
16966 the drawing area, set S->extends_to_end_of_line_p. */
16967 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
16969 if (start == s->row->used[s->area]
16970 && s->area == TEXT_AREA
16971 && ((s->hl == DRAW_NORMAL_TEXT
16972 && (s->row->fill_line_p
16973 || s->face->background != default_face->background
16974 || s->face->stipple != default_face->stipple
16975 || s->row->mouse_face_p))
16976 || s->hl == DRAW_MOUSE_FACE
16977 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
16978 && s->row->fill_line_p)))
16979 s->extends_to_end_of_line_p = 1;
16981 /* If S extends its face to the end of the line, set its
16982 background_width to the distance to the right edge of the drawing
16983 area. */
16984 if (s->extends_to_end_of_line_p)
16985 s->background_width = last_x - s->x + 1;
16986 else
16987 s->background_width = s->width;
16991 /* Compute overhangs and x-positions for glyph string S and its
16992 predecessors, or successors. X is the starting x-position for S.
16993 BACKWARD_P non-zero means process predecessors. */
16995 static void
16996 compute_overhangs_and_x (s, x, backward_p)
16997 struct glyph_string *s;
16998 int x;
16999 int backward_p;
17001 if (backward_p)
17003 while (s)
17005 if (rif->compute_glyph_string_overhangs)
17006 rif->compute_glyph_string_overhangs (s);
17007 x -= s->width;
17008 s->x = x;
17009 s = s->prev;
17012 else
17014 while (s)
17016 if (rif->compute_glyph_string_overhangs)
17017 rif->compute_glyph_string_overhangs (s);
17018 s->x = x;
17019 x += s->width;
17020 s = s->next;
17027 /* The following macros are only called from draw_glyphs below.
17028 They reference the following parameters of that function directly:
17029 `w', `row', `area', and `overlap_p'
17030 as well as the following local variables:
17031 `s', `f', and `hdc' (in W32) */
17033 #ifdef HAVE_NTGUI
17034 /* On W32, silently add local `hdc' variable to argument list of
17035 init_glyph_string. */
17036 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17037 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
17038 #else
17039 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
17040 init_glyph_string (s, char2b, w, row, area, start, hl)
17041 #endif
17043 /* Add a glyph string for a stretch glyph to the list of strings
17044 between HEAD and TAIL. START is the index of the stretch glyph in
17045 row area AREA of glyph row ROW. END is the index of the last glyph
17046 in that glyph row area. X is the current output position assigned
17047 to the new glyph string constructed. HL overrides that face of the
17048 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17049 is the right-most x-position of the drawing area. */
17051 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
17052 and below -- keep them on one line. */
17053 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17054 do \
17056 s = (struct glyph_string *) alloca (sizeof *s); \
17057 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17058 START = fill_stretch_glyph_string (s, row, area, START, END); \
17059 append_glyph_string (&HEAD, &TAIL, s); \
17060 s->x = (X); \
17062 while (0)
17065 /* Add a glyph string for an image glyph to the list of strings
17066 between HEAD and TAIL. START is the index of the image glyph in
17067 row area AREA of glyph row ROW. END is the index of the last glyph
17068 in that glyph row area. X is the current output position assigned
17069 to the new glyph string constructed. HL overrides that face of the
17070 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
17071 is the right-most x-position of the drawing area. */
17073 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17074 do \
17076 s = (struct glyph_string *) alloca (sizeof *s); \
17077 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
17078 fill_image_glyph_string (s); \
17079 append_glyph_string (&HEAD, &TAIL, s); \
17080 ++START; \
17081 s->x = (X); \
17083 while (0)
17086 /* Add a glyph string for a sequence of character glyphs to the list
17087 of strings between HEAD and TAIL. START is the index of the first
17088 glyph in row area AREA of glyph row ROW that is part of the new
17089 glyph string. END is the index of the last glyph in that glyph row
17090 area. X is the current output position assigned to the new glyph
17091 string constructed. HL overrides that face of the glyph; e.g. it
17092 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
17093 right-most x-position of the drawing area. */
17095 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17096 do \
17098 int c, face_id; \
17099 XChar2b *char2b; \
17101 c = (row)->glyphs[area][START].u.ch; \
17102 face_id = (row)->glyphs[area][START].face_id; \
17104 s = (struct glyph_string *) alloca (sizeof *s); \
17105 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
17106 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
17107 append_glyph_string (&HEAD, &TAIL, s); \
17108 s->x = (X); \
17109 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
17111 while (0)
17114 /* Add a glyph string for a composite sequence to the list of strings
17115 between HEAD and TAIL. START is the index of the first glyph in
17116 row area AREA of glyph row ROW that is part of the new glyph
17117 string. END is the index of the last glyph in that glyph row area.
17118 X is the current output position assigned to the new glyph string
17119 constructed. HL overrides that face of the glyph; e.g. it is
17120 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
17121 x-position of the drawing area. */
17123 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
17124 do { \
17125 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
17126 int face_id = (row)->glyphs[area][START].face_id; \
17127 struct face *base_face = FACE_FROM_ID (f, face_id); \
17128 struct composition *cmp = composition_table[cmp_id]; \
17129 int glyph_len = cmp->glyph_len; \
17130 XChar2b *char2b; \
17131 struct face **faces; \
17132 struct glyph_string *first_s = NULL; \
17133 int n; \
17135 base_face = base_face->ascii_face; \
17136 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
17137 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
17138 /* At first, fill in `char2b' and `faces'. */ \
17139 for (n = 0; n < glyph_len; n++) \
17141 int c = COMPOSITION_GLYPH (cmp, n); \
17142 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
17143 faces[n] = FACE_FROM_ID (f, this_face_id); \
17144 get_char_face_and_encoding (f, c, this_face_id, \
17145 char2b + n, 1, 1); \
17148 /* Make glyph_strings for each glyph sequence that is drawable by \
17149 the same face, and append them to HEAD/TAIL. */ \
17150 for (n = 0; n < cmp->glyph_len;) \
17152 s = (struct glyph_string *) alloca (sizeof *s); \
17153 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
17154 append_glyph_string (&(HEAD), &(TAIL), s); \
17155 s->cmp = cmp; \
17156 s->gidx = n; \
17157 s->x = (X); \
17159 if (n == 0) \
17160 first_s = s; \
17162 n = fill_composite_glyph_string (s, faces, overlaps_p); \
17165 ++START; \
17166 s = first_s; \
17167 } while (0)
17170 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
17171 of AREA of glyph row ROW on window W between indices START and END.
17172 HL overrides the face for drawing glyph strings, e.g. it is
17173 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
17174 x-positions of the drawing area.
17176 This is an ugly monster macro construct because we must use alloca
17177 to allocate glyph strings (because draw_glyphs can be called
17178 asynchronously). */
17180 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
17181 do \
17183 HEAD = TAIL = NULL; \
17184 while (START < END) \
17186 struct glyph *first_glyph = (row)->glyphs[area] + START; \
17187 switch (first_glyph->type) \
17189 case CHAR_GLYPH: \
17190 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
17191 HL, X, LAST_X); \
17192 break; \
17194 case COMPOSITE_GLYPH: \
17195 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
17196 HL, X, LAST_X); \
17197 break; \
17199 case STRETCH_GLYPH: \
17200 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
17201 HL, X, LAST_X); \
17202 break; \
17204 case IMAGE_GLYPH: \
17205 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
17206 HL, X, LAST_X); \
17207 break; \
17209 default: \
17210 abort (); \
17213 set_glyph_string_background_width (s, START, LAST_X); \
17214 (X) += s->width; \
17217 while (0)
17220 /* Draw glyphs between START and END in AREA of ROW on window W,
17221 starting at x-position X. X is relative to AREA in W. HL is a
17222 face-override with the following meaning:
17224 DRAW_NORMAL_TEXT draw normally
17225 DRAW_CURSOR draw in cursor face
17226 DRAW_MOUSE_FACE draw in mouse face.
17227 DRAW_INVERSE_VIDEO draw in mode line face
17228 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
17229 DRAW_IMAGE_RAISED draw an image with a raised relief around it
17231 If OVERLAPS_P is non-zero, draw only the foreground of characters
17232 and clip to the physical height of ROW.
17234 Value is the x-position reached, relative to AREA of W. */
17236 static int
17237 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
17238 struct window *w;
17239 int x;
17240 struct glyph_row *row;
17241 enum glyph_row_area area;
17242 int start, end;
17243 enum draw_glyphs_face hl;
17244 int overlaps_p;
17246 struct glyph_string *head, *tail;
17247 struct glyph_string *s;
17248 int last_x, area_width;
17249 int x_reached;
17250 int i, j;
17251 struct frame *f = XFRAME (WINDOW_FRAME (w));
17252 DECLARE_HDC (hdc);
17254 ALLOCATE_HDC (hdc, f);
17256 /* Let's rather be paranoid than getting a SEGV. */
17257 end = min (end, row->used[area]);
17258 start = max (0, start);
17259 start = min (end, start);
17261 /* Translate X to frame coordinates. Set last_x to the right
17262 end of the drawing area. */
17263 if (row->full_width_p)
17265 /* X is relative to the left edge of W, without scroll bars
17266 or fringes. */
17267 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
17269 x += window_left_x;
17270 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
17271 last_x = window_left_x + area_width;
17273 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
17275 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
17276 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
17277 last_x += width;
17278 else
17279 x -= width;
17282 x += FRAME_INTERNAL_BORDER_WIDTH (f);
17283 /* ++KFS: W32 and MAC versions had -= in next line (bug??) */
17284 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
17286 else
17288 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
17289 area_width = window_box_width (w, area);
17290 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
17293 /* Build a doubly-linked list of glyph_string structures between
17294 head and tail from what we have to draw. Note that the macro
17295 BUILD_GLYPH_STRINGS will modify its start parameter. That's
17296 the reason we use a separate variable `i'. */
17297 i = start;
17298 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
17299 if (tail)
17300 x_reached = tail->x + tail->background_width;
17301 else
17302 x_reached = x;
17304 /* If there are any glyphs with lbearing < 0 or rbearing > width in
17305 the row, redraw some glyphs in front or following the glyph
17306 strings built above. */
17307 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
17309 int dummy_x = 0;
17310 struct glyph_string *h, *t;
17312 /* Compute overhangs for all glyph strings. */
17313 if (rif->compute_glyph_string_overhangs)
17314 for (s = head; s; s = s->next)
17315 rif->compute_glyph_string_overhangs (s);
17317 /* Prepend glyph strings for glyphs in front of the first glyph
17318 string that are overwritten because of the first glyph
17319 string's left overhang. The background of all strings
17320 prepended must be drawn because the first glyph string
17321 draws over it. */
17322 i = left_overwritten (head);
17323 if (i >= 0)
17325 j = i;
17326 BUILD_GLYPH_STRINGS (j, start, h, t,
17327 DRAW_NORMAL_TEXT, dummy_x, last_x);
17328 start = i;
17329 compute_overhangs_and_x (t, head->x, 1);
17330 prepend_glyph_string_lists (&head, &tail, h, t);
17333 /* Prepend glyph strings for glyphs in front of the first glyph
17334 string that overwrite that glyph string because of their
17335 right overhang. For these strings, only the foreground must
17336 be drawn, because it draws over the glyph string at `head'.
17337 The background must not be drawn because this would overwrite
17338 right overhangs of preceding glyphs for which no glyph
17339 strings exist. */
17340 i = left_overwriting (head);
17341 if (i >= 0)
17343 BUILD_GLYPH_STRINGS (i, start, h, t,
17344 DRAW_NORMAL_TEXT, dummy_x, last_x);
17345 for (s = h; s; s = s->next)
17346 s->background_filled_p = 1;
17347 compute_overhangs_and_x (t, head->x, 1);
17348 prepend_glyph_string_lists (&head, &tail, h, t);
17351 /* Append glyphs strings for glyphs following the last glyph
17352 string tail that are overwritten by tail. The background of
17353 these strings has to be drawn because tail's foreground draws
17354 over it. */
17355 i = right_overwritten (tail);
17356 if (i >= 0)
17358 BUILD_GLYPH_STRINGS (end, i, h, t,
17359 DRAW_NORMAL_TEXT, x, last_x);
17360 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17361 append_glyph_string_lists (&head, &tail, h, t);
17364 /* Append glyph strings for glyphs following the last glyph
17365 string tail that overwrite tail. The foreground of such
17366 glyphs has to be drawn because it writes into the background
17367 of tail. The background must not be drawn because it could
17368 paint over the foreground of following glyphs. */
17369 i = right_overwriting (tail);
17370 if (i >= 0)
17372 BUILD_GLYPH_STRINGS (end, i, h, t,
17373 DRAW_NORMAL_TEXT, x, last_x);
17374 for (s = h; s; s = s->next)
17375 s->background_filled_p = 1;
17376 compute_overhangs_and_x (h, tail->x + tail->width, 0);
17377 append_glyph_string_lists (&head, &tail, h, t);
17381 /* Draw all strings. */
17382 for (s = head; s; s = s->next)
17383 rif->draw_glyph_string (s);
17385 if (area == TEXT_AREA
17386 && !row->full_width_p
17387 /* When drawing overlapping rows, only the glyph strings'
17388 foreground is drawn, which doesn't erase a cursor
17389 completely. */
17390 && !overlaps_p)
17392 int x0 = head ? head->x : x;
17393 int x1 = tail ? tail->x + tail->background_width : x;
17395 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
17396 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
17398 /* ++KFS: W32 and MAC versions had following test here:
17399 if (!row->full_width_p && XFASTINT (w->left_margin_width) != 0)
17402 if (XFASTINT (w->left_margin_width) != 0)
17404 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
17405 x0 -= left_area_width;
17406 x1 -= left_area_width;
17409 notice_overwritten_cursor (w, area, x0, x1,
17410 row->y, MATRIX_ROW_BOTTOM_Y (row));
17413 /* Value is the x-position up to which drawn, relative to AREA of W.
17414 This doesn't include parts drawn because of overhangs. */
17415 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
17416 if (!row->full_width_p)
17418 /* ++KFS: W32 and MAC versions only had this test here:
17419 if (area > LEFT_MARGIN_AREA)
17422 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
17423 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
17424 if (area > TEXT_AREA)
17425 x_reached -= window_box_width (w, TEXT_AREA);
17428 RELEASE_HDC (hdc, f);
17430 return x_reached;
17434 /* Store one glyph for IT->char_to_display in IT->glyph_row.
17435 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17437 static INLINE void
17438 append_glyph (it)
17439 struct it *it;
17441 struct glyph *glyph;
17442 enum glyph_row_area area = it->area;
17444 xassert (it->glyph_row);
17445 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
17447 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17448 if (glyph < it->glyph_row->glyphs[area + 1])
17450 glyph->charpos = CHARPOS (it->position);
17451 glyph->object = it->object;
17452 glyph->pixel_width = it->pixel_width;
17453 glyph->voffset = it->voffset;
17454 glyph->type = CHAR_GLYPH;
17455 glyph->multibyte_p = it->multibyte_p;
17456 glyph->left_box_line_p = it->start_of_box_run_p;
17457 glyph->right_box_line_p = it->end_of_box_run_p;
17458 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17459 || it->phys_descent > it->descent);
17460 glyph->padding_p = 0;
17461 glyph->glyph_not_available_p = it->glyph_not_available_p;
17462 glyph->face_id = it->face_id;
17463 glyph->u.ch = it->char_to_display;
17464 glyph->font_type = FONT_TYPE_UNKNOWN;
17465 ++it->glyph_row->used[area];
17469 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
17470 Called from x_produce_glyphs when IT->glyph_row is non-null. */
17472 static INLINE void
17473 append_composite_glyph (it)
17474 struct it *it;
17476 struct glyph *glyph;
17477 enum glyph_row_area area = it->area;
17479 xassert (it->glyph_row);
17481 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17482 if (glyph < it->glyph_row->glyphs[area + 1])
17484 glyph->charpos = CHARPOS (it->position);
17485 glyph->object = it->object;
17486 glyph->pixel_width = it->pixel_width;
17487 glyph->voffset = it->voffset;
17488 glyph->type = COMPOSITE_GLYPH;
17489 glyph->multibyte_p = it->multibyte_p;
17490 glyph->left_box_line_p = it->start_of_box_run_p;
17491 glyph->right_box_line_p = it->end_of_box_run_p;
17492 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
17493 || it->phys_descent > it->descent);
17494 glyph->padding_p = 0;
17495 glyph->glyph_not_available_p = 0;
17496 glyph->face_id = it->face_id;
17497 glyph->u.cmp_id = it->cmp_id;
17498 glyph->font_type = FONT_TYPE_UNKNOWN;
17499 ++it->glyph_row->used[area];
17504 /* Change IT->ascent and IT->height according to the setting of
17505 IT->voffset. */
17507 static INLINE void
17508 take_vertical_position_into_account (it)
17509 struct it *it;
17511 if (it->voffset)
17513 if (it->voffset < 0)
17514 /* Increase the ascent so that we can display the text higher
17515 in the line. */
17516 it->ascent += abs (it->voffset);
17517 else
17518 /* Increase the descent so that we can display the text lower
17519 in the line. */
17520 it->descent += it->voffset;
17525 /* Produce glyphs/get display metrics for the image IT is loaded with.
17526 See the description of struct display_iterator in dispextern.h for
17527 an overview of struct display_iterator. */
17529 static void
17530 produce_image_glyph (it)
17531 struct it *it;
17533 struct image *img;
17534 struct face *face;
17536 xassert (it->what == IT_IMAGE);
17538 face = FACE_FROM_ID (it->f, it->face_id);
17539 img = IMAGE_FROM_ID (it->f, it->image_id);
17540 xassert (img);
17542 /* Make sure X resources of the face and image are loaded. */
17543 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17544 prepare_image_for_display (it->f, img);
17546 it->ascent = it->phys_ascent = image_ascent (img, face);
17547 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
17548 it->pixel_width = img->width + 2 * img->hmargin;
17550 it->nglyphs = 1;
17552 if (face->box != FACE_NO_BOX)
17554 if (face->box_line_width > 0)
17556 it->ascent += face->box_line_width;
17557 it->descent += face->box_line_width;
17560 if (it->start_of_box_run_p)
17561 it->pixel_width += abs (face->box_line_width);
17562 if (it->end_of_box_run_p)
17563 it->pixel_width += abs (face->box_line_width);
17566 take_vertical_position_into_account (it);
17568 if (it->glyph_row)
17570 struct glyph *glyph;
17571 enum glyph_row_area area = it->area;
17573 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17574 if (glyph < it->glyph_row->glyphs[area + 1])
17576 glyph->charpos = CHARPOS (it->position);
17577 glyph->object = it->object;
17578 glyph->pixel_width = it->pixel_width;
17579 glyph->voffset = it->voffset;
17580 glyph->type = IMAGE_GLYPH;
17581 glyph->multibyte_p = it->multibyte_p;
17582 glyph->left_box_line_p = it->start_of_box_run_p;
17583 glyph->right_box_line_p = it->end_of_box_run_p;
17584 glyph->overlaps_vertically_p = 0;
17585 glyph->padding_p = 0;
17586 glyph->glyph_not_available_p = 0;
17587 glyph->face_id = it->face_id;
17588 glyph->u.img_id = img->id;
17589 glyph->font_type = FONT_TYPE_UNKNOWN;
17590 ++it->glyph_row->used[area];
17596 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
17597 of the glyph, WIDTH and HEIGHT are the width and height of the
17598 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
17599 ascent of the glyph (0 <= ASCENT <= 1). */
17601 static void
17602 append_stretch_glyph (it, object, width, height, ascent)
17603 struct it *it;
17604 Lisp_Object object;
17605 int width, height;
17606 double ascent;
17608 struct glyph *glyph;
17609 enum glyph_row_area area = it->area;
17611 xassert (ascent >= 0 && ascent <= 1);
17613 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
17614 if (glyph < it->glyph_row->glyphs[area + 1])
17616 glyph->charpos = CHARPOS (it->position);
17617 glyph->object = object;
17618 glyph->pixel_width = width;
17619 glyph->voffset = it->voffset;
17620 glyph->type = STRETCH_GLYPH;
17621 glyph->multibyte_p = it->multibyte_p;
17622 glyph->left_box_line_p = it->start_of_box_run_p;
17623 glyph->right_box_line_p = it->end_of_box_run_p;
17624 glyph->overlaps_vertically_p = 0;
17625 glyph->padding_p = 0;
17626 glyph->glyph_not_available_p = 0;
17627 glyph->face_id = it->face_id;
17628 glyph->u.stretch.ascent = height * ascent;
17629 glyph->u.stretch.height = height;
17630 glyph->font_type = FONT_TYPE_UNKNOWN;
17631 ++it->glyph_row->used[area];
17636 /* Produce a stretch glyph for iterator IT. IT->object is the value
17637 of the glyph property displayed. The value must be a list
17638 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
17639 being recognized:
17641 1. `:width WIDTH' specifies that the space should be WIDTH *
17642 canonical char width wide. WIDTH may be an integer or floating
17643 point number.
17645 2. `:relative-width FACTOR' specifies that the width of the stretch
17646 should be computed from the width of the first character having the
17647 `glyph' property, and should be FACTOR times that width.
17649 3. `:align-to HPOS' specifies that the space should be wide enough
17650 to reach HPOS, a value in canonical character units.
17652 Exactly one of the above pairs must be present.
17654 4. `:height HEIGHT' specifies that the height of the stretch produced
17655 should be HEIGHT, measured in canonical character units.
17657 5. `:relative-height FACTOR' specifies that the height of the
17658 stretch should be FACTOR times the height of the characters having
17659 the glyph property.
17661 Either none or exactly one of 4 or 5 must be present.
17663 6. `:ascent ASCENT' specifies that ASCENT percent of the height
17664 of the stretch should be used for the ascent of the stretch.
17665 ASCENT must be in the range 0 <= ASCENT <= 100. */
17667 #define NUMVAL(X) \
17668 ((INTEGERP (X) || FLOATP (X)) \
17669 ? XFLOATINT (X) \
17670 : - 1)
17673 static void
17674 produce_stretch_glyph (it)
17675 struct it *it;
17677 /* (space :width WIDTH :height HEIGHT. */
17678 Lisp_Object prop, plist;
17679 int width = 0, height = 0;
17680 double ascent = 0;
17681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17682 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
17684 PREPARE_FACE_FOR_DISPLAY (it->f, face);
17686 /* List should start with `space'. */
17687 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
17688 plist = XCDR (it->object);
17690 /* Compute the width of the stretch. */
17691 if (prop = Fplist_get (plist, QCwidth),
17692 NUMVAL (prop) > 0)
17693 /* Absolute width `:width WIDTH' specified and valid. */
17694 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
17695 else if (prop = Fplist_get (plist, QCrelative_width),
17696 NUMVAL (prop) > 0)
17698 /* Relative width `:relative-width FACTOR' specified and valid.
17699 Compute the width of the characters having the `glyph'
17700 property. */
17701 struct it it2;
17702 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
17704 it2 = *it;
17705 if (it->multibyte_p)
17707 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
17708 - IT_BYTEPOS (*it));
17709 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
17711 else
17712 it2.c = *p, it2.len = 1;
17714 it2.glyph_row = NULL;
17715 it2.what = IT_CHARACTER;
17716 x_produce_glyphs (&it2);
17717 width = NUMVAL (prop) * it2.pixel_width;
17719 else if (prop = Fplist_get (plist, QCalign_to),
17720 NUMVAL (prop) > 0)
17721 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
17722 else
17723 /* Nothing specified -> width defaults to canonical char width. */
17724 width = CANON_X_UNIT (it->f);
17726 /* Compute height. */
17727 if (prop = Fplist_get (plist, QCheight),
17728 NUMVAL (prop) > 0)
17729 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
17730 else if (prop = Fplist_get (plist, QCrelative_height),
17731 NUMVAL (prop) > 0)
17732 height = FONT_HEIGHT (font) * NUMVAL (prop);
17733 else
17734 height = FONT_HEIGHT (font);
17736 /* Compute percentage of height used for ascent. If
17737 `:ascent ASCENT' is present and valid, use that. Otherwise,
17738 derive the ascent from the font in use. */
17739 if (prop = Fplist_get (plist, QCascent),
17740 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
17741 ascent = NUMVAL (prop) / 100.0;
17742 else
17743 ascent = (double) FONT_BASE (font) / FONT_HEIGHT (font);
17745 if (width <= 0)
17746 width = 1;
17747 if (height <= 0)
17748 height = 1;
17750 if (it->glyph_row)
17752 Lisp_Object object = it->stack[it->sp - 1].string;
17753 if (!STRINGP (object))
17754 object = it->w->buffer;
17755 append_stretch_glyph (it, object, width, height, ascent);
17758 it->pixel_width = width;
17759 it->ascent = it->phys_ascent = height * ascent;
17760 it->descent = it->phys_descent = height - it->ascent;
17761 it->nglyphs = 1;
17763 if (face->box != FACE_NO_BOX)
17765 if (face->box_line_width > 0)
17767 it->ascent += face->box_line_width;
17768 it->descent += face->box_line_width;
17771 if (it->start_of_box_run_p)
17772 it->pixel_width += abs (face->box_line_width);
17773 if (it->end_of_box_run_p)
17774 it->pixel_width += abs (face->box_line_width);
17777 take_vertical_position_into_account (it);
17780 /* RIF:
17781 Produce glyphs/get display metrics for the display element IT is
17782 loaded with. See the description of struct display_iterator in
17783 dispextern.h for an overview of struct display_iterator. */
17785 void
17786 x_produce_glyphs (it)
17787 struct it *it;
17789 it->glyph_not_available_p = 0;
17791 if (it->what == IT_CHARACTER)
17793 XChar2b char2b;
17794 XFontStruct *font;
17795 struct face *face = FACE_FROM_ID (it->f, it->face_id);
17796 XCharStruct *pcm;
17797 int font_not_found_p;
17798 struct font_info *font_info;
17799 int boff; /* baseline offset */
17800 /* We may change it->multibyte_p upon unibyte<->multibyte
17801 conversion. So, save the current value now and restore it
17802 later.
17804 Note: It seems that we don't have to record multibyte_p in
17805 struct glyph because the character code itself tells if or
17806 not the character is multibyte. Thus, in the future, we must
17807 consider eliminating the field `multibyte_p' in the struct
17808 glyph. */
17809 int saved_multibyte_p = it->multibyte_p;
17811 /* Maybe translate single-byte characters to multibyte, or the
17812 other way. */
17813 it->char_to_display = it->c;
17814 if (!ASCII_BYTE_P (it->c))
17816 if (unibyte_display_via_language_environment
17817 && SINGLE_BYTE_CHAR_P (it->c)
17818 && (it->c >= 0240
17819 || !NILP (Vnonascii_translation_table)))
17821 it->char_to_display = unibyte_char_to_multibyte (it->c);
17822 it->multibyte_p = 1;
17823 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17824 face = FACE_FROM_ID (it->f, it->face_id);
17826 else if (!SINGLE_BYTE_CHAR_P (it->c)
17827 && !it->multibyte_p)
17829 it->multibyte_p = 1;
17830 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
17831 face = FACE_FROM_ID (it->f, it->face_id);
17835 /* Get font to use. Encode IT->char_to_display. */
17836 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
17837 &char2b, it->multibyte_p, 0);
17838 font = face->font;
17840 /* When no suitable font found, use the default font. */
17841 font_not_found_p = font == NULL;
17842 if (font_not_found_p)
17844 font = FRAME_FONT (it->f);
17845 boff = FRAME_BASELINE_OFFSET (it->f);
17846 font_info = NULL;
17848 else
17850 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
17851 boff = font_info->baseline_offset;
17852 if (font_info->vertical_centering)
17853 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
17856 if (it->char_to_display >= ' '
17857 && (!it->multibyte_p || it->char_to_display < 128))
17859 /* Either unibyte or ASCII. */
17860 int stretched_p;
17862 it->nglyphs = 1;
17864 pcm = rif->per_char_metric (font, &char2b,
17865 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
17866 it->ascent = FONT_BASE (font) + boff;
17867 it->descent = FONT_DESCENT (font) - boff;
17869 if (pcm)
17871 it->phys_ascent = pcm->ascent + boff;
17872 it->phys_descent = pcm->descent - boff;
17873 it->pixel_width = pcm->width;
17875 else
17877 it->glyph_not_available_p = 1;
17878 it->phys_ascent = FONT_BASE (font) + boff;
17879 it->phys_descent = FONT_DESCENT (font) - boff;
17880 it->pixel_width = FONT_WIDTH (font);
17883 /* If this is a space inside a region of text with
17884 `space-width' property, change its width. */
17885 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
17886 if (stretched_p)
17887 it->pixel_width *= XFLOATINT (it->space_width);
17889 /* If face has a box, add the box thickness to the character
17890 height. If character has a box line to the left and/or
17891 right, add the box line width to the character's width. */
17892 if (face->box != FACE_NO_BOX)
17894 int thick = face->box_line_width;
17896 if (thick > 0)
17898 it->ascent += thick;
17899 it->descent += thick;
17901 else
17902 thick = -thick;
17904 if (it->start_of_box_run_p)
17905 it->pixel_width += thick;
17906 if (it->end_of_box_run_p)
17907 it->pixel_width += thick;
17910 /* If face has an overline, add the height of the overline
17911 (1 pixel) and a 1 pixel margin to the character height. */
17912 if (face->overline_p)
17913 it->ascent += 2;
17915 take_vertical_position_into_account (it);
17917 /* If we have to actually produce glyphs, do it. */
17918 if (it->glyph_row)
17920 if (stretched_p)
17922 /* Translate a space with a `space-width' property
17923 into a stretch glyph. */
17924 double ascent = (double) FONT_BASE (font)
17925 / FONT_HEIGHT (font);
17926 append_stretch_glyph (it, it->object, it->pixel_width,
17927 it->ascent + it->descent, ascent);
17929 else
17930 append_glyph (it);
17932 /* If characters with lbearing or rbearing are displayed
17933 in this line, record that fact in a flag of the
17934 glyph row. This is used to optimize X output code. */
17935 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
17936 it->glyph_row->contains_overlapping_glyphs_p = 1;
17939 else if (it->char_to_display == '\n')
17941 /* A newline has no width but we need the height of the line. */
17942 it->pixel_width = 0;
17943 it->nglyphs = 0;
17944 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17945 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17947 if (face->box != FACE_NO_BOX
17948 && face->box_line_width > 0)
17950 it->ascent += face->box_line_width;
17951 it->descent += face->box_line_width;
17954 else if (it->char_to_display == '\t')
17956 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
17957 int x = it->current_x + it->continuation_lines_width;
17958 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
17960 /* If the distance from the current position to the next tab
17961 stop is less than a canonical character width, use the
17962 tab stop after that. */
17963 if (next_tab_x - x < CANON_X_UNIT (it->f))
17964 next_tab_x += tab_width;
17966 it->pixel_width = next_tab_x - x;
17967 it->nglyphs = 1;
17968 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
17969 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
17971 if (it->glyph_row)
17973 double ascent = (double) it->ascent / (it->ascent + it->descent);
17974 append_stretch_glyph (it, it->object, it->pixel_width,
17975 it->ascent + it->descent, ascent);
17978 else
17980 /* A multi-byte character. Assume that the display width of the
17981 character is the width of the character multiplied by the
17982 width of the font. */
17984 /* If we found a font, this font should give us the right
17985 metrics. If we didn't find a font, use the frame's
17986 default font and calculate the width of the character
17987 from the charset width; this is what old redisplay code
17988 did. */
17990 pcm = rif->per_char_metric (font, &char2b,
17991 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
17993 if (font_not_found_p || !pcm)
17995 int charset = CHAR_CHARSET (it->char_to_display);
17997 it->glyph_not_available_p = 1;
17998 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
17999 * CHARSET_WIDTH (charset));
18000 it->phys_ascent = FONT_BASE (font) + boff;
18001 it->phys_descent = FONT_DESCENT (font) - boff;
18003 else
18005 it->pixel_width = pcm->width;
18006 it->phys_ascent = pcm->ascent + boff;
18007 it->phys_descent = pcm->descent - boff;
18008 if (it->glyph_row
18009 && (pcm->lbearing < 0
18010 || pcm->rbearing > pcm->width))
18011 it->glyph_row->contains_overlapping_glyphs_p = 1;
18013 it->nglyphs = 1;
18014 it->ascent = FONT_BASE (font) + boff;
18015 it->descent = FONT_DESCENT (font) - boff;
18016 if (face->box != FACE_NO_BOX)
18018 int thick = face->box_line_width;
18020 if (thick > 0)
18022 it->ascent += thick;
18023 it->descent += thick;
18025 else
18026 thick = - thick;
18028 if (it->start_of_box_run_p)
18029 it->pixel_width += thick;
18030 if (it->end_of_box_run_p)
18031 it->pixel_width += thick;
18034 /* If face has an overline, add the height of the overline
18035 (1 pixel) and a 1 pixel margin to the character height. */
18036 if (face->overline_p)
18037 it->ascent += 2;
18039 take_vertical_position_into_account (it);
18041 if (it->glyph_row)
18042 append_glyph (it);
18044 it->multibyte_p = saved_multibyte_p;
18046 else if (it->what == IT_COMPOSITION)
18048 /* Note: A composition is represented as one glyph in the
18049 glyph matrix. There are no padding glyphs. */
18050 XChar2b char2b;
18051 XFontStruct *font;
18052 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18053 XCharStruct *pcm;
18054 int font_not_found_p;
18055 struct font_info *font_info;
18056 int boff; /* baseline offset */
18057 struct composition *cmp = composition_table[it->cmp_id];
18059 /* Maybe translate single-byte characters to multibyte. */
18060 it->char_to_display = it->c;
18061 if (unibyte_display_via_language_environment
18062 && SINGLE_BYTE_CHAR_P (it->c)
18063 && (it->c >= 0240
18064 || (it->c >= 0200
18065 && !NILP (Vnonascii_translation_table))))
18067 it->char_to_display = unibyte_char_to_multibyte (it->c);
18070 /* Get face and font to use. Encode IT->char_to_display. */
18071 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
18072 face = FACE_FROM_ID (it->f, it->face_id);
18073 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
18074 &char2b, it->multibyte_p, 0);
18075 font = face->font;
18077 /* When no suitable font found, use the default font. */
18078 font_not_found_p = font == NULL;
18079 if (font_not_found_p)
18081 font = FRAME_FONT (it->f);
18082 boff = FRAME_BASELINE_OFFSET (it->f);
18083 font_info = NULL;
18085 else
18087 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18088 boff = font_info->baseline_offset;
18089 if (font_info->vertical_centering)
18090 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18093 /* There are no padding glyphs, so there is only one glyph to
18094 produce for the composition. Important is that pixel_width,
18095 ascent and descent are the values of what is drawn by
18096 draw_glyphs (i.e. the values of the overall glyphs composed). */
18097 it->nglyphs = 1;
18099 /* If we have not yet calculated pixel size data of glyphs of
18100 the composition for the current face font, calculate them
18101 now. Theoretically, we have to check all fonts for the
18102 glyphs, but that requires much time and memory space. So,
18103 here we check only the font of the first glyph. This leads
18104 to incorrect display very rarely, and C-l (recenter) can
18105 correct the display anyway. */
18106 if (cmp->font != (void *) font)
18108 /* Ascent and descent of the font of the first character of
18109 this composition (adjusted by baseline offset). Ascent
18110 and descent of overall glyphs should not be less than
18111 them respectively. */
18112 int font_ascent = FONT_BASE (font) + boff;
18113 int font_descent = FONT_DESCENT (font) - boff;
18114 /* Bounding box of the overall glyphs. */
18115 int leftmost, rightmost, lowest, highest;
18116 int i, width, ascent, descent;
18118 cmp->font = (void *) font;
18120 /* Initialize the bounding box. */
18121 if (font_info
18122 && (pcm = rif->per_char_metric (font, &char2b,
18123 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
18125 width = pcm->width;
18126 ascent = pcm->ascent;
18127 descent = pcm->descent;
18129 else
18131 width = FONT_WIDTH (font);
18132 ascent = FONT_BASE (font);
18133 descent = FONT_DESCENT (font);
18136 rightmost = width;
18137 lowest = - descent + boff;
18138 highest = ascent + boff;
18139 leftmost = 0;
18141 if (font_info
18142 && font_info->default_ascent
18143 && CHAR_TABLE_P (Vuse_default_ascent)
18144 && !NILP (Faref (Vuse_default_ascent,
18145 make_number (it->char_to_display))))
18146 highest = font_info->default_ascent + boff;
18148 /* Draw the first glyph at the normal position. It may be
18149 shifted to right later if some other glyphs are drawn at
18150 the left. */
18151 cmp->offsets[0] = 0;
18152 cmp->offsets[1] = boff;
18154 /* Set cmp->offsets for the remaining glyphs. */
18155 for (i = 1; i < cmp->glyph_len; i++)
18157 int left, right, btm, top;
18158 int ch = COMPOSITION_GLYPH (cmp, i);
18159 int face_id = FACE_FOR_CHAR (it->f, face, ch);
18161 face = FACE_FROM_ID (it->f, face_id);
18162 get_char_face_and_encoding (it->f, ch, face->id,
18163 &char2b, it->multibyte_p, 0);
18164 font = face->font;
18165 if (font == NULL)
18167 font = FRAME_FONT (it->f);
18168 boff = FRAME_BASELINE_OFFSET (it->f);
18169 font_info = NULL;
18171 else
18173 font_info
18174 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
18175 boff = font_info->baseline_offset;
18176 if (font_info->vertical_centering)
18177 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
18180 if (font_info
18181 && (pcm = rif->per_char_metric (font, &char2b,
18182 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
18184 width = pcm->width;
18185 ascent = pcm->ascent;
18186 descent = pcm->descent;
18188 else
18190 width = FONT_WIDTH (font);
18191 ascent = 1;
18192 descent = 0;
18195 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
18197 /* Relative composition with or without
18198 alternate chars. */
18199 left = (leftmost + rightmost - width) / 2;
18200 btm = - descent + boff;
18201 if (font_info && font_info->relative_compose
18202 && (! CHAR_TABLE_P (Vignore_relative_composition)
18203 || NILP (Faref (Vignore_relative_composition,
18204 make_number (ch)))))
18207 if (- descent >= font_info->relative_compose)
18208 /* One extra pixel between two glyphs. */
18209 btm = highest + 1;
18210 else if (ascent <= 0)
18211 /* One extra pixel between two glyphs. */
18212 btm = lowest - 1 - ascent - descent;
18215 else
18217 /* A composition rule is specified by an integer
18218 value that encodes global and new reference
18219 points (GREF and NREF). GREF and NREF are
18220 specified by numbers as below:
18222 0---1---2 -- ascent
18226 9--10--11 -- center
18228 ---3---4---5--- baseline
18230 6---7---8 -- descent
18232 int rule = COMPOSITION_RULE (cmp, i);
18233 int gref, nref, grefx, grefy, nrefx, nrefy;
18235 COMPOSITION_DECODE_RULE (rule, gref, nref);
18236 grefx = gref % 3, nrefx = nref % 3;
18237 grefy = gref / 3, nrefy = nref / 3;
18239 left = (leftmost
18240 + grefx * (rightmost - leftmost) / 2
18241 - nrefx * width / 2);
18242 btm = ((grefy == 0 ? highest
18243 : grefy == 1 ? 0
18244 : grefy == 2 ? lowest
18245 : (highest + lowest) / 2)
18246 - (nrefy == 0 ? ascent + descent
18247 : nrefy == 1 ? descent - boff
18248 : nrefy == 2 ? 0
18249 : (ascent + descent) / 2));
18252 cmp->offsets[i * 2] = left;
18253 cmp->offsets[i * 2 + 1] = btm + descent;
18255 /* Update the bounding box of the overall glyphs. */
18256 right = left + width;
18257 top = btm + descent + ascent;
18258 if (left < leftmost)
18259 leftmost = left;
18260 if (right > rightmost)
18261 rightmost = right;
18262 if (top > highest)
18263 highest = top;
18264 if (btm < lowest)
18265 lowest = btm;
18268 /* If there are glyphs whose x-offsets are negative,
18269 shift all glyphs to the right and make all x-offsets
18270 non-negative. */
18271 if (leftmost < 0)
18273 for (i = 0; i < cmp->glyph_len; i++)
18274 cmp->offsets[i * 2] -= leftmost;
18275 rightmost -= leftmost;
18278 cmp->pixel_width = rightmost;
18279 cmp->ascent = highest;
18280 cmp->descent = - lowest;
18281 if (cmp->ascent < font_ascent)
18282 cmp->ascent = font_ascent;
18283 if (cmp->descent < font_descent)
18284 cmp->descent = font_descent;
18287 it->pixel_width = cmp->pixel_width;
18288 it->ascent = it->phys_ascent = cmp->ascent;
18289 it->descent = it->phys_descent = cmp->descent;
18291 if (face->box != FACE_NO_BOX)
18293 int thick = face->box_line_width;
18295 if (thick > 0)
18297 it->ascent += thick;
18298 it->descent += thick;
18300 else
18301 thick = - thick;
18303 if (it->start_of_box_run_p)
18304 it->pixel_width += thick;
18305 if (it->end_of_box_run_p)
18306 it->pixel_width += thick;
18309 /* If face has an overline, add the height of the overline
18310 (1 pixel) and a 1 pixel margin to the character height. */
18311 if (face->overline_p)
18312 it->ascent += 2;
18314 take_vertical_position_into_account (it);
18316 if (it->glyph_row)
18317 append_composite_glyph (it);
18319 else if (it->what == IT_IMAGE)
18320 produce_image_glyph (it);
18321 else if (it->what == IT_STRETCH)
18322 produce_stretch_glyph (it);
18324 /* Accumulate dimensions. Note: can't assume that it->descent > 0
18325 because this isn't true for images with `:ascent 100'. */
18326 xassert (it->ascent >= 0 && it->descent >= 0);
18327 if (it->area == TEXT_AREA)
18328 it->current_x += it->pixel_width;
18330 it->descent += it->extra_line_spacing;
18332 it->max_ascent = max (it->max_ascent, it->ascent);
18333 it->max_descent = max (it->max_descent, it->descent);
18334 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
18335 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
18338 /* EXPORT for RIF:
18339 Output LEN glyphs starting at START at the nominal cursor position.
18340 Advance the nominal cursor over the text. The global variable
18341 updated_window contains the window being updated, updated_row is
18342 the glyph row being updated, and updated_area is the area of that
18343 row being updated. */
18345 void
18346 x_write_glyphs (start, len)
18347 struct glyph *start;
18348 int len;
18350 int x, hpos;
18352 xassert (updated_window && updated_row);
18353 BLOCK_INPUT;
18355 /* Write glyphs. */
18357 hpos = start - updated_row->glyphs[updated_area];
18358 x = draw_glyphs (updated_window, output_cursor.x,
18359 updated_row, updated_area,
18360 hpos, hpos + len,
18361 DRAW_NORMAL_TEXT, 0);
18363 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
18364 if (updated_area == TEXT_AREA
18365 && updated_window->phys_cursor_on_p
18366 && updated_window->phys_cursor.vpos == output_cursor.vpos
18367 && updated_window->phys_cursor.hpos >= hpos
18368 && updated_window->phys_cursor.hpos < hpos + len)
18369 updated_window->phys_cursor_on_p = 0;
18371 UNBLOCK_INPUT;
18373 /* Advance the output cursor. */
18374 output_cursor.hpos += len;
18375 output_cursor.x = x;
18379 /* EXPORT for RIF:
18380 Insert LEN glyphs from START at the nominal cursor position. */
18382 void
18383 x_insert_glyphs (start, len)
18384 struct glyph *start;
18385 int len;
18387 struct frame *f;
18388 struct window *w;
18389 int line_height, shift_by_width, shifted_region_width;
18390 struct glyph_row *row;
18391 struct glyph *glyph;
18392 int frame_x, frame_y, hpos;
18394 xassert (updated_window && updated_row);
18395 BLOCK_INPUT;
18396 w = updated_window;
18397 f = XFRAME (WINDOW_FRAME (w));
18399 /* Get the height of the line we are in. */
18400 row = updated_row;
18401 line_height = row->height;
18403 /* Get the width of the glyphs to insert. */
18404 shift_by_width = 0;
18405 for (glyph = start; glyph < start + len; ++glyph)
18406 shift_by_width += glyph->pixel_width;
18408 /* Get the width of the region to shift right. */
18409 shifted_region_width = (window_box_width (w, updated_area)
18410 - output_cursor.x
18411 - shift_by_width);
18413 /* Shift right. */
18414 frame_x = window_box_left (w, updated_area) + output_cursor.x;
18415 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
18417 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
18418 line_height, shift_by_width);
18420 /* Write the glyphs. */
18421 hpos = start - row->glyphs[updated_area];
18422 draw_glyphs (w, output_cursor.x, row, updated_area,
18423 hpos, hpos + len,
18424 DRAW_NORMAL_TEXT, 0);
18426 /* Advance the output cursor. */
18427 output_cursor.hpos += len;
18428 output_cursor.x += shift_by_width;
18429 UNBLOCK_INPUT;
18433 /* EXPORT for RIF:
18434 Erase the current text line from the nominal cursor position
18435 (inclusive) to pixel column TO_X (exclusive). The idea is that
18436 everything from TO_X onward is already erased.
18438 TO_X is a pixel position relative to updated_area of
18439 updated_window. TO_X == -1 means clear to the end of this area. */
18441 void
18442 x_clear_end_of_line (to_x)
18443 int to_x;
18445 struct frame *f;
18446 struct window *w = updated_window;
18447 int max_x, min_y, max_y;
18448 int from_x, from_y, to_y;
18450 xassert (updated_window && updated_row);
18451 f = XFRAME (w->frame);
18453 if (updated_row->full_width_p)
18455 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
18456 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
18457 && !w->pseudo_window_p)
18458 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
18460 else
18461 max_x = window_box_width (w, updated_area);
18462 max_y = window_text_bottom_y (w);
18464 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
18465 of window. For TO_X > 0, truncate to end of drawing area. */
18466 if (to_x == 0)
18467 return;
18468 else if (to_x < 0)
18469 to_x = max_x;
18470 else
18471 to_x = min (to_x, max_x);
18473 to_y = min (max_y, output_cursor.y + updated_row->height);
18475 /* Notice if the cursor will be cleared by this operation. */
18476 if (!updated_row->full_width_p)
18477 notice_overwritten_cursor (w, updated_area,
18478 output_cursor.x, -1,
18479 updated_row->y,
18480 MATRIX_ROW_BOTTOM_Y (updated_row));
18482 from_x = output_cursor.x;
18484 /* Translate to frame coordinates. */
18485 if (updated_row->full_width_p)
18487 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
18488 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
18490 else
18492 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
18493 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
18496 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
18497 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
18498 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
18500 /* Prevent inadvertently clearing to end of the X window. */
18501 if (to_x > from_x && to_y > from_y)
18503 BLOCK_INPUT;
18504 rif->clear_frame_area (f, from_x, from_y,
18505 to_x - from_x, to_y - from_y);
18506 UNBLOCK_INPUT;
18510 #endif /* HAVE_WINDOW_SYSTEM */
18514 /***********************************************************************
18515 Cursor types
18516 ***********************************************************************/
18518 /* Value is the internal representation of the specified cursor type
18519 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
18520 of the bar cursor. */
18522 enum text_cursor_kinds
18523 get_specified_cursor_type (arg, width)
18524 Lisp_Object arg;
18525 int *width;
18527 enum text_cursor_kinds type;
18529 if (NILP (arg))
18530 return NO_CURSOR;
18532 if (EQ (arg, Qbox))
18533 return FILLED_BOX_CURSOR;
18535 if (EQ (arg, Qhollow))
18536 return HOLLOW_BOX_CURSOR;
18538 if (EQ (arg, Qbar))
18540 *width = 2;
18541 return BAR_CURSOR;
18544 if (CONSP (arg)
18545 && EQ (XCAR (arg), Qbar)
18546 && INTEGERP (XCDR (arg))
18547 && XINT (XCDR (arg)) >= 0)
18549 *width = XINT (XCDR (arg));
18550 return BAR_CURSOR;
18553 if (EQ (arg, Qhbar))
18555 *width = 2;
18556 return HBAR_CURSOR;
18559 if (CONSP (arg)
18560 && EQ (XCAR (arg), Qhbar)
18561 && INTEGERP (XCDR (arg))
18562 && XINT (XCDR (arg)) >= 0)
18564 *width = XINT (XCDR (arg));
18565 return HBAR_CURSOR;
18568 /* Treat anything unknown as "hollow box cursor".
18569 It was bad to signal an error; people have trouble fixing
18570 .Xdefaults with Emacs, when it has something bad in it. */
18571 type = HOLLOW_BOX_CURSOR;
18573 return type;
18576 /* Set the default cursor types for specified frame. */
18577 void
18578 set_frame_cursor_types (f, arg)
18579 struct frame *f;
18580 Lisp_Object arg;
18582 int width;
18583 Lisp_Object tem;
18585 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
18586 FRAME_CURSOR_WIDTH (f) = width;
18588 /* By default, set up the blink-off state depending on the on-state. */
18590 tem = Fassoc (arg, Vblink_cursor_alist);
18591 if (!NILP (tem))
18593 FRAME_BLINK_OFF_CURSOR (f)
18594 = get_specified_cursor_type (XCDR (tem), &width);
18595 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
18597 else
18598 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
18602 /* Return the cursor we want to be displayed in window W. Return
18603 width of bar/hbar cursor through WIDTH arg. Return with
18604 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
18605 (i.e. if the `system caret' should track this cursor).
18607 In a mini-buffer window, we want the cursor only to appear if we
18608 are reading input from this window. For the selected window, we
18609 want the cursor type given by the frame parameter or buffer local
18610 setting of cursor-type. If explicitly marked off, draw no cursor.
18611 In all other cases, we want a hollow box cursor. */
18613 enum text_cursor_kinds
18614 get_window_cursor_type (w, width, active_cursor)
18615 struct window *w;
18616 int *width;
18617 int *active_cursor;
18619 struct frame *f = XFRAME (w->frame);
18620 struct buffer *b = XBUFFER (w->buffer);
18621 int cursor_type = DEFAULT_CURSOR;
18622 Lisp_Object alt_cursor;
18623 int non_selected = 0;
18625 *active_cursor = 1;
18627 /* Echo area */
18628 if (cursor_in_echo_area
18629 && FRAME_HAS_MINIBUF_P (f)
18630 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
18632 if (w == XWINDOW (echo_area_window))
18634 *width = FRAME_CURSOR_WIDTH (f);
18635 return FRAME_DESIRED_CURSOR (f);
18638 *active_cursor = 0;
18639 non_selected = 1;
18642 /* Nonselected window or nonselected frame. */
18643 else if (w != XWINDOW (f->selected_window)
18644 #ifdef HAVE_WINDOW_SYSTEM
18645 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
18646 #endif
18649 *active_cursor = 0;
18651 if (MINI_WINDOW_P (w) && minibuf_level == 0)
18652 return NO_CURSOR;
18654 non_selected = 1;
18657 /* Never display a cursor in a window in which cursor-type is nil. */
18658 if (NILP (b->cursor_type))
18659 return NO_CURSOR;
18661 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
18662 if (non_selected)
18664 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
18665 return get_specified_cursor_type (alt_cursor, width);
18668 /* Get the normal cursor type for this window. */
18669 if (EQ (b->cursor_type, Qt))
18671 cursor_type = FRAME_DESIRED_CURSOR (f);
18672 *width = FRAME_CURSOR_WIDTH (f);
18674 else
18675 cursor_type = get_specified_cursor_type (b->cursor_type, width);
18677 /* Use normal cursor if not blinked off. */
18678 if (!w->cursor_off_p)
18679 return cursor_type;
18681 /* Cursor is blinked off, so determine how to "toggle" it. */
18683 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
18684 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
18685 return get_specified_cursor_type (XCDR (alt_cursor), width);
18687 /* Then see if frame has specified a specific blink off cursor type. */
18688 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
18690 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
18691 return FRAME_BLINK_OFF_CURSOR (f);
18694 /* Finally perform built-in cursor blinking:
18695 filled box <-> hollow box
18696 wide [h]bar <-> narrow [h]bar
18697 narrow [h]bar <-> no cursor
18698 other type <-> no cursor */
18700 if (cursor_type == FILLED_BOX_CURSOR)
18701 return HOLLOW_BOX_CURSOR;
18703 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
18705 *width = 1;
18706 return cursor_type;
18709 return NO_CURSOR;
18713 #ifdef HAVE_WINDOW_SYSTEM
18715 /* Notice when the text cursor of window W has been completely
18716 overwritten by a drawing operation that outputs glyphs in AREA
18717 starting at X0 and ending at X1 in the line starting at Y0 and
18718 ending at Y1. X coordinates are area-relative. X1 < 0 means all
18719 the rest of the line after X0 has been written. Y coordinates
18720 are window-relative. */
18722 static void
18723 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
18724 struct window *w;
18725 enum glyph_row_area area;
18726 int x0, y0, x1, y1;
18728 if (area == TEXT_AREA && w->phys_cursor_on_p)
18730 int cx0 = w->phys_cursor.x;
18731 int cx1 = cx0 + w->phys_cursor_width;
18732 int cy0 = w->phys_cursor.y;
18733 int cy1 = cy0 + w->phys_cursor_height;
18735 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
18737 /* The cursor image will be completely removed from the
18738 screen if the output area intersects the cursor area in
18739 y-direction. When we draw in [y0 y1[, and some part of
18740 the cursor is at y < y0, that part must have been drawn
18741 before. When scrolling, the cursor is erased before
18742 actually scrolling, so we don't come here. When not
18743 scrolling, the rows above the old cursor row must have
18744 changed, and in this case these rows must have written
18745 over the cursor image.
18747 Likewise if part of the cursor is below y1, with the
18748 exception of the cursor being in the first blank row at
18749 the buffer and window end because update_text_area
18750 doesn't draw that row. (Except when it does, but
18751 that's handled in update_text_area.) */
18753 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
18754 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
18755 w->phys_cursor_on_p = 0;
18760 #endif /* HAVE_WINDOW_SYSTEM */
18763 /************************************************************************
18764 Mouse Face
18765 ************************************************************************/
18767 #ifdef HAVE_WINDOW_SYSTEM
18769 /* EXPORT for RIF:
18770 Fix the display of area AREA of overlapping row ROW in window W. */
18772 void
18773 x_fix_overlapping_area (w, row, area)
18774 struct window *w;
18775 struct glyph_row *row;
18776 enum glyph_row_area area;
18778 int i, x;
18780 BLOCK_INPUT;
18782 if (area == LEFT_MARGIN_AREA)
18783 x = 0;
18784 else if (area == TEXT_AREA)
18785 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
18786 else
18787 x = (window_box_width (w, LEFT_MARGIN_AREA)
18788 + window_box_width (w, TEXT_AREA));
18790 for (i = 0; i < row->used[area];)
18792 if (row->glyphs[area][i].overlaps_vertically_p)
18794 int start = i, start_x = x;
18798 x += row->glyphs[area][i].pixel_width;
18799 ++i;
18801 while (i < row->used[area]
18802 && row->glyphs[area][i].overlaps_vertically_p);
18804 draw_glyphs (w, start_x, row, area,
18805 start, i,
18806 DRAW_NORMAL_TEXT, 1);
18808 else
18810 x += row->glyphs[area][i].pixel_width;
18811 ++i;
18815 UNBLOCK_INPUT;
18819 /* EXPORT:
18820 Draw the cursor glyph of window W in glyph row ROW. See the
18821 comment of draw_glyphs for the meaning of HL. */
18823 void
18824 draw_phys_cursor_glyph (w, row, hl)
18825 struct window *w;
18826 struct glyph_row *row;
18827 enum draw_glyphs_face hl;
18829 /* If cursor hpos is out of bounds, don't draw garbage. This can
18830 happen in mini-buffer windows when switching between echo area
18831 glyphs and mini-buffer. */
18832 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
18834 int on_p = w->phys_cursor_on_p;
18835 int x1;
18836 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
18837 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
18838 hl, 0);
18839 w->phys_cursor_on_p = on_p;
18841 if (hl == DRAW_CURSOR)
18842 w->phys_cursor_width = x1 - w->phys_cursor.x;
18843 /* When we erase the cursor, and ROW is overlapped by other
18844 rows, make sure that these overlapping parts of other rows
18845 are redrawn. */
18846 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
18848 if (row > w->current_matrix->rows
18849 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
18850 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
18852 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
18853 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
18854 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
18860 /* EXPORT:
18861 Erase the image of a cursor of window W from the screen. */
18863 void
18864 erase_phys_cursor (w)
18865 struct window *w;
18867 struct frame *f = XFRAME (w->frame);
18868 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
18869 int hpos = w->phys_cursor.hpos;
18870 int vpos = w->phys_cursor.vpos;
18871 int mouse_face_here_p = 0;
18872 struct glyph_matrix *active_glyphs = w->current_matrix;
18873 struct glyph_row *cursor_row;
18874 struct glyph *cursor_glyph;
18875 enum draw_glyphs_face hl;
18877 /* No cursor displayed or row invalidated => nothing to do on the
18878 screen. */
18879 if (w->phys_cursor_type == NO_CURSOR)
18880 goto mark_cursor_off;
18882 /* VPOS >= active_glyphs->nrows means that window has been resized.
18883 Don't bother to erase the cursor. */
18884 if (vpos >= active_glyphs->nrows)
18885 goto mark_cursor_off;
18887 /* If row containing cursor is marked invalid, there is nothing we
18888 can do. */
18889 cursor_row = MATRIX_ROW (active_glyphs, vpos);
18890 if (!cursor_row->enabled_p)
18891 goto mark_cursor_off;
18893 /* If row is completely invisible, don't attempt to delete a cursor which
18894 isn't there. This can happen if cursor is at top of a window, and
18895 we switch to a buffer with a header line in that window. */
18896 if (cursor_row->visible_height <= 0)
18897 goto mark_cursor_off;
18899 /* This can happen when the new row is shorter than the old one.
18900 In this case, either draw_glyphs or clear_end_of_line
18901 should have cleared the cursor. Note that we wouldn't be
18902 able to erase the cursor in this case because we don't have a
18903 cursor glyph at hand. */
18904 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
18905 goto mark_cursor_off;
18907 /* If the cursor is in the mouse face area, redisplay that when
18908 we clear the cursor. */
18909 if (! NILP (dpyinfo->mouse_face_window)
18910 && w == XWINDOW (dpyinfo->mouse_face_window)
18911 && (vpos > dpyinfo->mouse_face_beg_row
18912 || (vpos == dpyinfo->mouse_face_beg_row
18913 && hpos >= dpyinfo->mouse_face_beg_col))
18914 && (vpos < dpyinfo->mouse_face_end_row
18915 || (vpos == dpyinfo->mouse_face_end_row
18916 && hpos < dpyinfo->mouse_face_end_col))
18917 /* Don't redraw the cursor's spot in mouse face if it is at the
18918 end of a line (on a newline). The cursor appears there, but
18919 mouse highlighting does not. */
18920 && cursor_row->used[TEXT_AREA] > hpos)
18921 mouse_face_here_p = 1;
18923 /* Maybe clear the display under the cursor. */
18924 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
18926 int x, y;
18927 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
18929 cursor_glyph = get_phys_cursor_glyph (w);
18930 if (cursor_glyph == NULL)
18931 goto mark_cursor_off;
18933 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
18934 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
18936 rif->clear_frame_area (f, x, y,
18937 cursor_glyph->pixel_width, cursor_row->visible_height);
18940 /* Erase the cursor by redrawing the character underneath it. */
18941 if (mouse_face_here_p)
18942 hl = DRAW_MOUSE_FACE;
18943 else
18944 hl = DRAW_NORMAL_TEXT;
18945 draw_phys_cursor_glyph (w, cursor_row, hl);
18947 mark_cursor_off:
18948 w->phys_cursor_on_p = 0;
18949 w->phys_cursor_type = NO_CURSOR;
18953 /* EXPORT:
18954 Display or clear cursor of window W. If ON is zero, clear the
18955 cursor. If it is non-zero, display the cursor. If ON is nonzero,
18956 where to put the cursor is specified by HPOS, VPOS, X and Y. */
18958 void
18959 display_and_set_cursor (w, on, hpos, vpos, x, y)
18960 struct window *w;
18961 int on, hpos, vpos, x, y;
18963 struct frame *f = XFRAME (w->frame);
18964 int new_cursor_type;
18965 int new_cursor_width;
18966 int active_cursor;
18967 struct glyph_matrix *current_glyphs;
18968 struct glyph_row *glyph_row;
18969 struct glyph *glyph;
18971 /* This is pointless on invisible frames, and dangerous on garbaged
18972 windows and frames; in the latter case, the frame or window may
18973 be in the midst of changing its size, and x and y may be off the
18974 window. */
18975 if (! FRAME_VISIBLE_P (f)
18976 || FRAME_GARBAGED_P (f)
18977 || vpos >= w->current_matrix->nrows
18978 || hpos >= w->current_matrix->matrix_w)
18979 return;
18981 /* If cursor is off and we want it off, return quickly. */
18982 if (!on && !w->phys_cursor_on_p)
18983 return;
18985 current_glyphs = w->current_matrix;
18986 glyph_row = MATRIX_ROW (current_glyphs, vpos);
18987 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
18989 /* If cursor row is not enabled, we don't really know where to
18990 display the cursor. */
18991 if (!glyph_row->enabled_p)
18993 w->phys_cursor_on_p = 0;
18994 return;
18997 xassert (interrupt_input_blocked);
18999 /* Set new_cursor_type to the cursor we want to be displayed. */
19000 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
19002 /* If cursor is currently being shown and we don't want it to be or
19003 it is in the wrong place, or the cursor type is not what we want,
19004 erase it. */
19005 if (w->phys_cursor_on_p
19006 && (!on
19007 || w->phys_cursor.x != x
19008 || w->phys_cursor.y != y
19009 || new_cursor_type != w->phys_cursor_type
19010 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
19011 && new_cursor_width != w->phys_cursor_width)))
19012 erase_phys_cursor (w);
19014 /* Don't check phys_cursor_on_p here because that flag is only set
19015 to zero in some cases where we know that the cursor has been
19016 completely erased, to avoid the extra work of erasing the cursor
19017 twice. In other words, phys_cursor_on_p can be 1 and the cursor
19018 still not be visible, or it has only been partly erased. */
19019 if (on)
19021 w->phys_cursor_ascent = glyph_row->ascent;
19022 w->phys_cursor_height = glyph_row->height;
19024 /* Set phys_cursor_.* before x_draw_.* is called because some
19025 of them may need the information. */
19026 w->phys_cursor.x = x;
19027 w->phys_cursor.y = glyph_row->y;
19028 w->phys_cursor.hpos = hpos;
19029 w->phys_cursor.vpos = vpos;
19032 rif->draw_window_cursor (w, glyph_row, x, y,
19033 new_cursor_type, new_cursor_width,
19034 on, active_cursor);
19038 /* Switch the display of W's cursor on or off, according to the value
19039 of ON. */
19041 static void
19042 update_window_cursor (w, on)
19043 struct window *w;
19044 int on;
19046 /* Don't update cursor in windows whose frame is in the process
19047 of being deleted. */
19048 if (w->current_matrix)
19050 BLOCK_INPUT;
19051 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
19052 w->phys_cursor.x, w->phys_cursor.y);
19053 UNBLOCK_INPUT;
19058 /* Call update_window_cursor with parameter ON_P on all leaf windows
19059 in the window tree rooted at W. */
19061 static void
19062 update_cursor_in_window_tree (w, on_p)
19063 struct window *w;
19064 int on_p;
19066 while (w)
19068 if (!NILP (w->hchild))
19069 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
19070 else if (!NILP (w->vchild))
19071 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
19072 else
19073 update_window_cursor (w, on_p);
19075 w = NILP (w->next) ? 0 : XWINDOW (w->next);
19080 /* EXPORT:
19081 Display the cursor on window W, or clear it, according to ON_P.
19082 Don't change the cursor's position. */
19084 void
19085 x_update_cursor (f, on_p)
19086 struct frame *f;
19087 int on_p;
19089 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
19093 /* EXPORT:
19094 Clear the cursor of window W to background color, and mark the
19095 cursor as not shown. This is used when the text where the cursor
19096 is is about to be rewritten. */
19098 void
19099 x_clear_cursor (w)
19100 struct window *w;
19102 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
19103 update_window_cursor (w, 0);
19107 /* EXPORT:
19108 Display the active region described by mouse_face_* according to DRAW. */
19110 void
19111 show_mouse_face (dpyinfo, draw)
19112 Display_Info *dpyinfo;
19113 enum draw_glyphs_face draw;
19115 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
19116 struct frame *f = XFRAME (WINDOW_FRAME (w));
19118 if (/* If window is in the process of being destroyed, don't bother
19119 to do anything. */
19120 w->current_matrix != NULL
19121 /* Don't update mouse highlight if hidden */
19122 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
19123 /* Recognize when we are called to operate on rows that don't exist
19124 anymore. This can happen when a window is split. */
19125 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
19127 int phys_cursor_on_p = w->phys_cursor_on_p;
19128 struct glyph_row *row, *first, *last;
19130 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
19131 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
19133 for (row = first; row <= last && row->enabled_p; ++row)
19135 int start_hpos, end_hpos, start_x;
19137 /* For all but the first row, the highlight starts at column 0. */
19138 if (row == first)
19140 start_hpos = dpyinfo->mouse_face_beg_col;
19141 start_x = dpyinfo->mouse_face_beg_x;
19143 else
19145 start_hpos = 0;
19146 start_x = 0;
19149 if (row == last)
19150 end_hpos = dpyinfo->mouse_face_end_col;
19151 else
19152 end_hpos = row->used[TEXT_AREA];
19154 if (end_hpos > start_hpos)
19156 draw_glyphs (w, start_x, row, TEXT_AREA,
19157 start_hpos, end_hpos,
19158 draw, 0);
19160 row->mouse_face_p
19161 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
19165 /* When we've written over the cursor, arrange for it to
19166 be displayed again. */
19167 if (phys_cursor_on_p && !w->phys_cursor_on_p)
19169 BLOCK_INPUT;
19170 display_and_set_cursor (w, 1,
19171 w->phys_cursor.hpos, w->phys_cursor.vpos,
19172 w->phys_cursor.x, w->phys_cursor.y);
19173 UNBLOCK_INPUT;
19177 /* Change the mouse cursor. */
19178 if (draw == DRAW_NORMAL_TEXT)
19179 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
19180 else if (draw == DRAW_MOUSE_FACE)
19181 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
19182 else
19183 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
19186 /* EXPORT:
19187 Clear out the mouse-highlighted active region.
19188 Redraw it un-highlighted first. Value is non-zero if mouse
19189 face was actually drawn unhighlighted. */
19192 clear_mouse_face (dpyinfo)
19193 Display_Info *dpyinfo;
19195 int cleared = 0;
19197 if (!NILP (dpyinfo->mouse_face_window))
19199 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
19200 cleared = 1;
19203 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
19204 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
19205 dpyinfo->mouse_face_window = Qnil;
19206 dpyinfo->mouse_face_overlay = Qnil;
19207 return cleared;
19211 /* EXPORT:
19212 Non-zero if physical cursor of window W is within mouse face. */
19215 cursor_in_mouse_face_p (w)
19216 struct window *w;
19218 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
19219 int in_mouse_face = 0;
19221 if (WINDOWP (dpyinfo->mouse_face_window)
19222 && XWINDOW (dpyinfo->mouse_face_window) == w)
19224 int hpos = w->phys_cursor.hpos;
19225 int vpos = w->phys_cursor.vpos;
19227 if (vpos >= dpyinfo->mouse_face_beg_row
19228 && vpos <= dpyinfo->mouse_face_end_row
19229 && (vpos > dpyinfo->mouse_face_beg_row
19230 || hpos >= dpyinfo->mouse_face_beg_col)
19231 && (vpos < dpyinfo->mouse_face_end_row
19232 || hpos < dpyinfo->mouse_face_end_col
19233 || dpyinfo->mouse_face_past_end))
19234 in_mouse_face = 1;
19237 return in_mouse_face;
19243 /* Find the glyph matrix position of buffer position CHARPOS in window
19244 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
19245 current glyphs must be up to date. If CHARPOS is above window
19246 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
19247 of last line in W. In the row containing CHARPOS, stop before glyphs
19248 having STOP as object. */
19250 #if 0 /* This is a version of fast_find_position that's more correct
19251 in the presence of hscrolling, for example. I didn't install
19252 it right away because the problem fixed is minor, it failed
19253 in 20.x as well, and I think it's too risky to install
19254 so near the release of 21.1. 2001-09-25 gerd. */
19256 static int
19257 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
19258 struct window *w;
19259 int charpos;
19260 int *hpos, *vpos, *x, *y;
19261 Lisp_Object stop;
19263 struct glyph_row *row, *first;
19264 struct glyph *glyph, *end;
19265 int i, past_end = 0;
19267 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19268 row = row_containing_pos (w, charpos, first, NULL, 0);
19269 if (row == NULL)
19271 if (charpos < MATRIX_ROW_START_CHARPOS (first))
19273 *x = *y = *hpos = *vpos = 0;
19274 return 0;
19276 else
19278 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
19279 past_end = 1;
19283 *x = row->x;
19284 *y = row->y;
19285 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19287 glyph = row->glyphs[TEXT_AREA];
19288 end = glyph + row->used[TEXT_AREA];
19290 /* Skip over glyphs not having an object at the start of the row.
19291 These are special glyphs like truncation marks on terminal
19292 frames. */
19293 if (row->displays_text_p)
19294 while (glyph < end
19295 && INTEGERP (glyph->object)
19296 && !EQ (stop, glyph->object)
19297 && glyph->charpos < 0)
19299 *x += glyph->pixel_width;
19300 ++glyph;
19303 while (glyph < end
19304 && !INTEGERP (glyph->object)
19305 && !EQ (stop, glyph->object)
19306 && (!BUFFERP (glyph->object)
19307 || glyph->charpos < charpos))
19309 *x += glyph->pixel_width;
19310 ++glyph;
19313 *hpos = glyph - row->glyphs[TEXT_AREA];
19314 return past_end;
19317 #else /* not 0 */
19319 static int
19320 fast_find_position (w, pos, hpos, vpos, x, y, stop)
19321 struct window *w;
19322 int pos;
19323 int *hpos, *vpos, *x, *y;
19324 Lisp_Object stop;
19326 int i;
19327 int lastcol;
19328 int maybe_next_line_p = 0;
19329 int line_start_position;
19330 int yb = window_text_bottom_y (w);
19331 struct glyph_row *row, *best_row;
19332 int row_vpos, best_row_vpos;
19333 int current_x;
19335 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19336 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
19338 while (row->y < yb)
19340 if (row->used[TEXT_AREA])
19341 line_start_position = row->glyphs[TEXT_AREA]->charpos;
19342 else
19343 line_start_position = 0;
19345 if (line_start_position > pos)
19346 break;
19347 /* If the position sought is the end of the buffer,
19348 don't include the blank lines at the bottom of the window. */
19349 else if (line_start_position == pos
19350 && pos == BUF_ZV (XBUFFER (w->buffer)))
19352 maybe_next_line_p = 1;
19353 break;
19355 else if (line_start_position > 0)
19357 best_row = row;
19358 best_row_vpos = row_vpos;
19361 if (row->y + row->height >= yb)
19362 break;
19364 ++row;
19365 ++row_vpos;
19368 /* Find the right column within BEST_ROW. */
19369 lastcol = 0;
19370 current_x = best_row->x;
19371 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
19373 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
19374 int charpos = glyph->charpos;
19376 if (BUFFERP (glyph->object))
19378 if (charpos == pos)
19380 *hpos = i;
19381 *vpos = best_row_vpos;
19382 *x = current_x;
19383 *y = best_row->y;
19384 return 1;
19386 else if (charpos > pos)
19387 break;
19389 else if (EQ (glyph->object, stop))
19390 break;
19392 if (charpos > 0)
19393 lastcol = i;
19394 current_x += glyph->pixel_width;
19397 /* If we're looking for the end of the buffer,
19398 and we didn't find it in the line we scanned,
19399 use the start of the following line. */
19400 if (maybe_next_line_p)
19402 ++best_row;
19403 ++best_row_vpos;
19404 lastcol = 0;
19405 current_x = best_row->x;
19408 *vpos = best_row_vpos;
19409 *hpos = lastcol + 1;
19410 *x = current_x;
19411 *y = best_row->y;
19412 return 0;
19415 #endif /* not 0 */
19418 /* Find the position of the glyph for position POS in OBJECT in
19419 window W's current matrix, and return in *X, *Y the pixel
19420 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
19422 RIGHT_P non-zero means return the position of the right edge of the
19423 glyph, RIGHT_P zero means return the left edge position.
19425 If no glyph for POS exists in the matrix, return the position of
19426 the glyph with the next smaller position that is in the matrix, if
19427 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
19428 exists in the matrix, return the position of the glyph with the
19429 next larger position in OBJECT.
19431 Value is non-zero if a glyph was found. */
19433 static int
19434 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
19435 struct window *w;
19436 int pos;
19437 Lisp_Object object;
19438 int *hpos, *vpos, *x, *y;
19439 int right_p;
19441 int yb = window_text_bottom_y (w);
19442 struct glyph_row *r;
19443 struct glyph *best_glyph = NULL;
19444 struct glyph_row *best_row = NULL;
19445 int best_x = 0;
19447 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
19448 r->enabled_p && r->y < yb;
19449 ++r)
19451 struct glyph *g = r->glyphs[TEXT_AREA];
19452 struct glyph *e = g + r->used[TEXT_AREA];
19453 int gx;
19455 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
19456 if (EQ (g->object, object))
19458 if (g->charpos == pos)
19460 best_glyph = g;
19461 best_x = gx;
19462 best_row = r;
19463 goto found;
19465 else if (best_glyph == NULL
19466 || ((abs (g->charpos - pos)
19467 < abs (best_glyph->charpos - pos))
19468 && (right_p
19469 ? g->charpos < pos
19470 : g->charpos > pos)))
19472 best_glyph = g;
19473 best_x = gx;
19474 best_row = r;
19479 found:
19481 if (best_glyph)
19483 *x = best_x;
19484 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
19486 if (right_p)
19488 *x += best_glyph->pixel_width;
19489 ++*hpos;
19492 *y = best_row->y;
19493 *vpos = best_row - w->current_matrix->rows;
19496 return best_glyph != NULL;
19500 /* Take proper action when mouse has moved to the mode or header line
19501 or marginal area AREA of window W, x-position X and y-position Y.
19502 X is relative to the start of the text display area of W, so the
19503 width of bitmap areas and scroll bars must be subtracted to get a
19504 position relative to the start of the mode line. */
19506 static void
19507 note_mode_line_or_margin_highlight (w, x, y, area)
19508 struct window *w;
19509 int x, y;
19510 enum window_part area;
19512 struct frame *f = XFRAME (w->frame);
19513 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19514 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
19515 int charpos;
19516 Lisp_Object string, help, map, pos;
19518 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
19519 string = mode_line_string (w, x, y, area, &charpos);
19520 else
19521 string = marginal_area_string (w, x, y, area, &charpos);
19523 if (STRINGP (string))
19525 pos = make_number (charpos);
19527 /* If we're on a string with `help-echo' text property, arrange
19528 for the help to be displayed. This is done by setting the
19529 global variable help_echo_string to the help string. */
19530 help = Fget_text_property (pos, Qhelp_echo, string);
19531 if (!NILP (help))
19533 help_echo_string = help;
19534 XSETWINDOW (help_echo_window, w);
19535 help_echo_object = string;
19536 help_echo_pos = charpos;
19539 /* Change the mouse pointer according to what is under X/Y. */
19540 map = Fget_text_property (pos, Qlocal_map, string);
19541 if (!KEYMAPP (map))
19542 map = Fget_text_property (pos, Qkeymap, string);
19543 if (KEYMAPP (map))
19544 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19547 rif->define_frame_cursor (f, cursor);
19551 /* EXPORT:
19552 Take proper action when the mouse has moved to position X, Y on
19553 frame F as regards highlighting characters that have mouse-face
19554 properties. Also de-highlighting chars where the mouse was before.
19555 X and Y can be negative or out of range. */
19557 void
19558 note_mouse_highlight (f, x, y)
19559 struct frame *f;
19560 int x, y;
19562 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
19563 enum window_part part;
19564 Lisp_Object window;
19565 struct window *w;
19566 Cursor cursor = No_Cursor;
19567 struct buffer *b;
19569 /* When a menu is active, don't highlight because this looks odd. */
19570 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
19571 if (popup_activated ())
19572 return;
19573 #endif
19575 if (NILP (Vmouse_highlight)
19576 || !f->glyphs_initialized_p)
19577 return;
19579 dpyinfo->mouse_face_mouse_x = x;
19580 dpyinfo->mouse_face_mouse_y = y;
19581 dpyinfo->mouse_face_mouse_frame = f;
19583 if (dpyinfo->mouse_face_defer)
19584 return;
19586 if (gc_in_progress)
19588 dpyinfo->mouse_face_deferred_gc = 1;
19589 return;
19592 /* Which window is that in? */
19593 window = window_from_coordinates (f, x, y, &part, 1);
19595 /* If we were displaying active text in another window, clear that. */
19596 if (! EQ (window, dpyinfo->mouse_face_window))
19597 clear_mouse_face (dpyinfo);
19599 /* Not on a window -> return. */
19600 if (!WINDOWP (window))
19601 return;
19603 /* Reset help_echo_string. It will get recomputed below. */
19604 /* ++KFS: X version didn't do this, but it looks harmless. */
19605 help_echo_string = Qnil;
19607 /* Convert to window-relative pixel coordinates. */
19608 w = XWINDOW (window);
19609 frame_to_window_pixel_xy (w, &x, &y);
19611 /* Handle tool-bar window differently since it doesn't display a
19612 buffer. */
19613 if (EQ (window, f->tool_bar_window))
19615 note_tool_bar_highlight (f, x, y);
19616 return;
19619 /* Mouse is on the mode, header line or margin? */
19620 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
19621 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
19623 note_mode_line_or_margin_highlight (w, x, y, part);
19624 return;
19627 if (part == ON_VERTICAL_BORDER)
19628 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
19629 else
19630 cursor = FRAME_X_OUTPUT (f)->text_cursor;
19632 /* Are we in a window whose display is up to date?
19633 And verify the buffer's text has not changed. */
19634 b = XBUFFER (w->buffer);
19635 if (part == ON_TEXT
19636 && EQ (w->window_end_valid, w->buffer)
19637 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
19638 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
19640 int hpos, vpos, pos, i, area;
19641 struct glyph *glyph;
19642 Lisp_Object object;
19643 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
19644 Lisp_Object *overlay_vec = NULL;
19645 int len, noverlays;
19646 struct buffer *obuf;
19647 int obegv, ozv, same_region;
19649 /* Find the glyph under X/Y. */
19650 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
19652 /* Clear mouse face if X/Y not over text. */
19653 if (glyph == NULL
19654 || area != TEXT_AREA
19655 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
19657 #if defined (HAVE_NTGUI)
19658 /* ++KFS: Why is this necessary on W32 ? */
19659 clear_mouse_face (dpyinfo);
19660 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
19661 #else
19662 if (clear_mouse_face (dpyinfo))
19663 cursor = No_Cursor;
19664 #endif
19665 goto set_cursor;
19668 pos = glyph->charpos;
19669 object = glyph->object;
19670 if (!STRINGP (object) && !BUFFERP (object))
19671 goto set_cursor;
19673 /* If we get an out-of-range value, return now; avoid an error. */
19674 if (BUFFERP (object) && pos > BUF_Z (b))
19675 goto set_cursor;
19677 /* Make the window's buffer temporarily current for
19678 overlays_at and compute_char_face. */
19679 obuf = current_buffer;
19680 current_buffer = b;
19681 obegv = BEGV;
19682 ozv = ZV;
19683 BEGV = BEG;
19684 ZV = Z;
19686 /* Is this char mouse-active or does it have help-echo? */
19687 position = make_number (pos);
19689 if (BUFFERP (object))
19691 /* Put all the overlays we want in a vector in overlay_vec.
19692 Store the length in len. If there are more than 10, make
19693 enough space for all, and try again. */
19694 len = 10;
19695 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19696 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
19697 if (noverlays > len)
19699 len = noverlays;
19700 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
19701 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
19704 /* Sort overlays into increasing priority order. */
19705 noverlays = sort_overlays (overlay_vec, noverlays, w);
19707 else
19708 noverlays = 0;
19710 same_region = (EQ (window, dpyinfo->mouse_face_window)
19711 && vpos >= dpyinfo->mouse_face_beg_row
19712 && vpos <= dpyinfo->mouse_face_end_row
19713 && (vpos > dpyinfo->mouse_face_beg_row
19714 || hpos >= dpyinfo->mouse_face_beg_col)
19715 && (vpos < dpyinfo->mouse_face_end_row
19716 || hpos < dpyinfo->mouse_face_end_col
19717 || dpyinfo->mouse_face_past_end));
19719 if (same_region)
19720 cursor = No_Cursor;
19722 /* Check mouse-face highlighting. */
19723 if (! same_region
19724 /* If there exists an overlay with mouse-face overlapping
19725 the one we are currently highlighting, we have to
19726 check if we enter the overlapping overlay, and then
19727 highlight only that. */
19728 || (OVERLAYP (dpyinfo->mouse_face_overlay)
19729 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
19731 /* Find the highest priority overlay that has a mouse-face
19732 property. */
19733 overlay = Qnil;
19734 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
19736 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
19737 if (!NILP (mouse_face))
19738 overlay = overlay_vec[i];
19741 /* If we're actually highlighting the same overlay as
19742 before, there's no need to do that again. */
19743 if (!NILP (overlay)
19744 && EQ (overlay, dpyinfo->mouse_face_overlay))
19745 goto check_help_echo;
19747 dpyinfo->mouse_face_overlay = overlay;
19749 /* Clear the display of the old active region, if any. */
19750 if (clear_mouse_face (dpyinfo))
19751 cursor = No_Cursor;
19753 /* If no overlay applies, get a text property. */
19754 if (NILP (overlay))
19755 mouse_face = Fget_text_property (position, Qmouse_face, object);
19757 /* Handle the overlay case. */
19758 if (!NILP (overlay))
19760 /* Find the range of text around this char that
19761 should be active. */
19762 Lisp_Object before, after;
19763 int ignore;
19765 before = Foverlay_start (overlay);
19766 after = Foverlay_end (overlay);
19767 /* Record this as the current active region. */
19768 fast_find_position (w, XFASTINT (before),
19769 &dpyinfo->mouse_face_beg_col,
19770 &dpyinfo->mouse_face_beg_row,
19771 &dpyinfo->mouse_face_beg_x,
19772 &dpyinfo->mouse_face_beg_y, Qnil);
19774 dpyinfo->mouse_face_past_end
19775 = !fast_find_position (w, XFASTINT (after),
19776 &dpyinfo->mouse_face_end_col,
19777 &dpyinfo->mouse_face_end_row,
19778 &dpyinfo->mouse_face_end_x,
19779 &dpyinfo->mouse_face_end_y, Qnil);
19780 dpyinfo->mouse_face_window = window;
19782 dpyinfo->mouse_face_face_id
19783 = face_at_buffer_position (w, pos, 0, 0,
19784 &ignore, pos + 1,
19785 !dpyinfo->mouse_face_hidden);
19787 /* Display it as active. */
19788 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19789 cursor = No_Cursor;
19791 /* Handle the text property case. */
19792 else if (!NILP (mouse_face) && BUFFERP (object))
19794 /* Find the range of text around this char that
19795 should be active. */
19796 Lisp_Object before, after, beginning, end;
19797 int ignore;
19799 beginning = Fmarker_position (w->start);
19800 end = make_number (BUF_Z (XBUFFER (object))
19801 - XFASTINT (w->window_end_pos));
19802 before
19803 = Fprevious_single_property_change (make_number (pos + 1),
19804 Qmouse_face,
19805 object, beginning);
19806 after
19807 = Fnext_single_property_change (position, Qmouse_face,
19808 object, end);
19810 /* Record this as the current active region. */
19811 fast_find_position (w, XFASTINT (before),
19812 &dpyinfo->mouse_face_beg_col,
19813 &dpyinfo->mouse_face_beg_row,
19814 &dpyinfo->mouse_face_beg_x,
19815 &dpyinfo->mouse_face_beg_y, Qnil);
19816 dpyinfo->mouse_face_past_end
19817 = !fast_find_position (w, XFASTINT (after),
19818 &dpyinfo->mouse_face_end_col,
19819 &dpyinfo->mouse_face_end_row,
19820 &dpyinfo->mouse_face_end_x,
19821 &dpyinfo->mouse_face_end_y, Qnil);
19822 dpyinfo->mouse_face_window = window;
19824 if (BUFFERP (object))
19825 dpyinfo->mouse_face_face_id
19826 = face_at_buffer_position (w, pos, 0, 0,
19827 &ignore, pos + 1,
19828 !dpyinfo->mouse_face_hidden);
19830 /* Display it as active. */
19831 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19832 cursor = No_Cursor;
19834 else if (!NILP (mouse_face) && STRINGP (object))
19836 Lisp_Object b, e;
19837 int ignore;
19839 b = Fprevious_single_property_change (make_number (pos + 1),
19840 Qmouse_face,
19841 object, Qnil);
19842 e = Fnext_single_property_change (position, Qmouse_face,
19843 object, Qnil);
19844 if (NILP (b))
19845 b = make_number (0);
19846 if (NILP (e))
19847 e = make_number (SCHARS (object) - 1);
19848 fast_find_string_pos (w, XINT (b), object,
19849 &dpyinfo->mouse_face_beg_col,
19850 &dpyinfo->mouse_face_beg_row,
19851 &dpyinfo->mouse_face_beg_x,
19852 &dpyinfo->mouse_face_beg_y, 0);
19853 fast_find_string_pos (w, XINT (e), object,
19854 &dpyinfo->mouse_face_end_col,
19855 &dpyinfo->mouse_face_end_row,
19856 &dpyinfo->mouse_face_end_x,
19857 &dpyinfo->mouse_face_end_y, 1);
19858 dpyinfo->mouse_face_past_end = 0;
19859 dpyinfo->mouse_face_window = window;
19860 dpyinfo->mouse_face_face_id
19861 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
19862 glyph->face_id, 1);
19863 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19864 cursor = No_Cursor;
19866 else if (STRINGP (object) && NILP (mouse_face))
19868 /* A string which doesn't have mouse-face, but
19869 the text ``under'' it might have. */
19870 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
19871 int start = MATRIX_ROW_START_CHARPOS (r);
19873 pos = string_buffer_position (w, object, start);
19874 if (pos > 0)
19875 mouse_face = get_char_property_and_overlay (make_number (pos),
19876 Qmouse_face,
19877 w->buffer,
19878 &overlay);
19879 if (!NILP (mouse_face) && !NILP (overlay))
19881 Lisp_Object before = Foverlay_start (overlay);
19882 Lisp_Object after = Foverlay_end (overlay);
19883 int ignore;
19885 /* Note that we might not be able to find position
19886 BEFORE in the glyph matrix if the overlay is
19887 entirely covered by a `display' property. In
19888 this case, we overshoot. So let's stop in
19889 the glyph matrix before glyphs for OBJECT. */
19890 fast_find_position (w, XFASTINT (before),
19891 &dpyinfo->mouse_face_beg_col,
19892 &dpyinfo->mouse_face_beg_row,
19893 &dpyinfo->mouse_face_beg_x,
19894 &dpyinfo->mouse_face_beg_y,
19895 object);
19897 dpyinfo->mouse_face_past_end
19898 = !fast_find_position (w, XFASTINT (after),
19899 &dpyinfo->mouse_face_end_col,
19900 &dpyinfo->mouse_face_end_row,
19901 &dpyinfo->mouse_face_end_x,
19902 &dpyinfo->mouse_face_end_y,
19903 Qnil);
19904 dpyinfo->mouse_face_window = window;
19905 dpyinfo->mouse_face_face_id
19906 = face_at_buffer_position (w, pos, 0, 0,
19907 &ignore, pos + 1,
19908 !dpyinfo->mouse_face_hidden);
19910 /* Display it as active. */
19911 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
19912 cursor = No_Cursor;
19917 check_help_echo:
19919 /* Look for a `help-echo' property. */
19921 Lisp_Object help, overlay;
19923 /* Check overlays first. */
19924 help = overlay = Qnil;
19925 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
19927 overlay = overlay_vec[i];
19928 help = Foverlay_get (overlay, Qhelp_echo);
19931 if (!NILP (help))
19933 help_echo_string = help;
19934 help_echo_window = window;
19935 help_echo_object = overlay;
19936 help_echo_pos = pos;
19938 else
19940 Lisp_Object object = glyph->object;
19941 int charpos = glyph->charpos;
19943 /* Try text properties. */
19944 if (STRINGP (object)
19945 && charpos >= 0
19946 && charpos < SCHARS (object))
19948 help = Fget_text_property (make_number (charpos),
19949 Qhelp_echo, object);
19950 if (NILP (help))
19952 /* If the string itself doesn't specify a help-echo,
19953 see if the buffer text ``under'' it does. */
19954 struct glyph_row *r
19955 = MATRIX_ROW (w->current_matrix, vpos);
19956 int start = MATRIX_ROW_START_CHARPOS (r);
19957 int pos = string_buffer_position (w, object, start);
19958 if (pos > 0)
19960 help = Fget_char_property (make_number (pos),
19961 Qhelp_echo, w->buffer);
19962 if (!NILP (help))
19964 charpos = pos;
19965 object = w->buffer;
19970 else if (BUFFERP (object)
19971 && charpos >= BEGV
19972 && charpos < ZV)
19973 help = Fget_text_property (make_number (charpos), Qhelp_echo,
19974 object);
19976 if (!NILP (help))
19978 help_echo_string = help;
19979 help_echo_window = window;
19980 help_echo_object = object;
19981 help_echo_pos = charpos;
19986 BEGV = obegv;
19987 ZV = ozv;
19988 current_buffer = obuf;
19991 set_cursor:
19993 #ifndef HAVE_CARBON
19994 if (cursor != No_Cursor)
19995 #else
19996 if (bcmp (&cursor, &No_Cursor, sizeof (Cursor)))
19997 #endif
19998 rif->define_frame_cursor (f, cursor);
20002 /* EXPORT for RIF:
20003 Clear any mouse-face on window W. This function is part of the
20004 redisplay interface, and is called from try_window_id and similar
20005 functions to ensure the mouse-highlight is off. */
20007 void
20008 x_clear_window_mouse_face (w)
20009 struct window *w;
20011 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20012 Lisp_Object window;
20014 BLOCK_INPUT;
20015 XSETWINDOW (window, w);
20016 if (EQ (window, dpyinfo->mouse_face_window))
20017 clear_mouse_face (dpyinfo);
20018 UNBLOCK_INPUT;
20022 /* EXPORT:
20023 Just discard the mouse face information for frame F, if any.
20024 This is used when the size of F is changed. */
20026 void
20027 cancel_mouse_face (f)
20028 struct frame *f;
20030 Lisp_Object window;
20031 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20033 window = dpyinfo->mouse_face_window;
20034 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
20036 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20037 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20038 dpyinfo->mouse_face_window = Qnil;
20043 #endif /* HAVE_WINDOW_SYSTEM */
20046 /***********************************************************************
20047 Exposure Events
20048 ***********************************************************************/
20050 #ifdef HAVE_WINDOW_SYSTEM
20052 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
20053 which intersects rectangle R. R is in window-relative coordinates. */
20055 static void
20056 expose_area (w, row, r, area)
20057 struct window *w;
20058 struct glyph_row *row;
20059 XRectangle *r;
20060 enum glyph_row_area area;
20062 struct glyph *first = row->glyphs[area];
20063 struct glyph *end = row->glyphs[area] + row->used[area];
20064 struct glyph *last;
20065 int first_x, start_x, x;
20067 if (area == TEXT_AREA && row->fill_line_p)
20068 /* If row extends face to end of line write the whole line. */
20069 draw_glyphs (w, 0, row, area,
20070 0, row->used[area],
20071 DRAW_NORMAL_TEXT, 0);
20072 else
20074 /* Set START_X to the window-relative start position for drawing glyphs of
20075 AREA. The first glyph of the text area can be partially visible.
20076 The first glyphs of other areas cannot. */
20077 if (area == LEFT_MARGIN_AREA)
20078 start_x = 0;
20079 else if (area == TEXT_AREA)
20080 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
20081 else
20082 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
20083 + window_box_width (w, TEXT_AREA));
20084 x = start_x;
20086 /* Find the first glyph that must be redrawn. */
20087 while (first < end
20088 && x + first->pixel_width < r->x)
20090 x += first->pixel_width;
20091 ++first;
20094 /* Find the last one. */
20095 last = first;
20096 first_x = x;
20097 while (last < end
20098 && x < r->x + r->width)
20100 x += last->pixel_width;
20101 ++last;
20104 /* Repaint. */
20105 if (last > first)
20106 draw_glyphs (w, first_x - start_x, row, area,
20107 first - row->glyphs[area], last - row->glyphs[area],
20108 DRAW_NORMAL_TEXT, 0);
20113 /* Redraw the parts of the glyph row ROW on window W intersecting
20114 rectangle R. R is in window-relative coordinates. Value is
20115 non-zero if mouse-face was overwritten. */
20117 static int
20118 expose_line (w, row, r)
20119 struct window *w;
20120 struct glyph_row *row;
20121 XRectangle *r;
20123 xassert (row->enabled_p);
20125 if (row->mode_line_p || w->pseudo_window_p)
20126 draw_glyphs (w, 0, row, TEXT_AREA,
20127 0, row->used[TEXT_AREA],
20128 DRAW_NORMAL_TEXT, 0);
20129 else
20131 if (row->used[LEFT_MARGIN_AREA])
20132 expose_area (w, row, r, LEFT_MARGIN_AREA);
20133 if (row->used[TEXT_AREA])
20134 expose_area (w, row, r, TEXT_AREA);
20135 if (row->used[RIGHT_MARGIN_AREA])
20136 expose_area (w, row, r, RIGHT_MARGIN_AREA);
20137 draw_row_fringe_bitmaps (w, row);
20140 return row->mouse_face_p;
20144 /* Redraw those parts of glyphs rows during expose event handling that
20145 overlap other rows. Redrawing of an exposed line writes over parts
20146 of lines overlapping that exposed line; this function fixes that.
20148 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
20149 row in W's current matrix that is exposed and overlaps other rows.
20150 LAST_OVERLAPPING_ROW is the last such row. */
20152 static void
20153 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
20154 struct window *w;
20155 struct glyph_row *first_overlapping_row;
20156 struct glyph_row *last_overlapping_row;
20158 struct glyph_row *row;
20160 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
20161 if (row->overlapping_p)
20163 xassert (row->enabled_p && !row->mode_line_p);
20165 if (row->used[LEFT_MARGIN_AREA])
20166 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
20168 if (row->used[TEXT_AREA])
20169 x_fix_overlapping_area (w, row, TEXT_AREA);
20171 if (row->used[RIGHT_MARGIN_AREA])
20172 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
20177 /* Return non-zero if W's cursor intersects rectangle R. */
20179 static int
20180 phys_cursor_in_rect_p (w, r)
20181 struct window *w;
20182 XRectangle *r;
20184 XRectangle cr, result;
20185 struct glyph *cursor_glyph;
20187 cursor_glyph = get_phys_cursor_glyph (w);
20188 if (cursor_glyph)
20190 cr.x = w->phys_cursor.x;
20191 cr.y = w->phys_cursor.y;
20192 cr.width = cursor_glyph->pixel_width;
20193 cr.height = w->phys_cursor_height;
20194 /* ++KFS: W32 version used W32-specific IntersectRect here, but
20195 I assume the effect is the same -- and this is portable. */
20196 return x_intersect_rectangles (&cr, r, &result);
20198 else
20199 return 0;
20203 /* EXPORT:
20204 Draw a vertical window border to the right of window W if W doesn't
20205 have vertical scroll bars. */
20207 void
20208 x_draw_vertical_border (w)
20209 struct window *w;
20211 struct frame *f = XFRAME (WINDOW_FRAME (w));
20213 /* Redraw borders between horizontally adjacent windows. Don't
20214 do it for frames with vertical scroll bars because either the
20215 right scroll bar of a window, or the left scroll bar of its
20216 neighbor will suffice as a border. */
20217 if (!WINDOW_RIGHTMOST_P (w)
20218 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
20220 int x0, x1, y0, y1;
20222 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
20223 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
20224 y1 -= 1;
20226 rif->draw_vertical_window_border (w, x1, y0, y1);
20231 /* Redraw the part of window W intersection rectangle FR. Pixel
20232 coordinates in FR are frame-relative. Call this function with
20233 input blocked. Value is non-zero if the exposure overwrites
20234 mouse-face. */
20236 static int
20237 expose_window (w, fr)
20238 struct window *w;
20239 XRectangle *fr;
20241 struct frame *f = XFRAME (w->frame);
20242 XRectangle wr, r;
20243 int mouse_face_overwritten_p = 0;
20245 /* If window is not yet fully initialized, do nothing. This can
20246 happen when toolkit scroll bars are used and a window is split.
20247 Reconfiguring the scroll bar will generate an expose for a newly
20248 created window. */
20249 if (w->current_matrix == NULL)
20250 return 0;
20252 /* When we're currently updating the window, display and current
20253 matrix usually don't agree. Arrange for a thorough display
20254 later. */
20255 if (w == updated_window)
20257 SET_FRAME_GARBAGED (f);
20258 return 0;
20261 /* Frame-relative pixel rectangle of W. */
20262 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
20263 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
20264 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
20265 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
20267 if (x_intersect_rectangles (fr, &wr, &r))
20269 int yb = window_text_bottom_y (w);
20270 struct glyph_row *row;
20271 int cursor_cleared_p;
20272 struct glyph_row *first_overlapping_row, *last_overlapping_row;
20274 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
20275 r.x, r.y, r.width, r.height));
20277 /* Convert to window coordinates. */
20278 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
20279 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
20281 /* Turn off the cursor. */
20282 if (!w->pseudo_window_p
20283 && phys_cursor_in_rect_p (w, &r))
20285 x_clear_cursor (w);
20286 cursor_cleared_p = 1;
20288 else
20289 cursor_cleared_p = 0;
20291 /* Update lines intersecting rectangle R. */
20292 first_overlapping_row = last_overlapping_row = NULL;
20293 for (row = w->current_matrix->rows;
20294 row->enabled_p;
20295 ++row)
20297 int y0 = row->y;
20298 int y1 = MATRIX_ROW_BOTTOM_Y (row);
20300 if ((y0 >= r.y && y0 < r.y + r.height)
20301 || (y1 > r.y && y1 < r.y + r.height)
20302 || (r.y >= y0 && r.y < y1)
20303 || (r.y + r.height > y0 && r.y + r.height < y1))
20305 if (row->overlapping_p)
20307 if (first_overlapping_row == NULL)
20308 first_overlapping_row = row;
20309 last_overlapping_row = row;
20312 if (expose_line (w, row, &r))
20313 mouse_face_overwritten_p = 1;
20316 if (y1 >= yb)
20317 break;
20320 /* Display the mode line if there is one. */
20321 if (WINDOW_WANTS_MODELINE_P (w)
20322 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
20323 row->enabled_p)
20324 && row->y < r.y + r.height)
20326 if (expose_line (w, row, &r))
20327 mouse_face_overwritten_p = 1;
20330 if (!w->pseudo_window_p)
20332 /* Fix the display of overlapping rows. */
20333 if (first_overlapping_row)
20334 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
20336 /* Draw border between windows. */
20337 x_draw_vertical_border (w);
20339 /* Turn the cursor on again. */
20340 if (cursor_cleared_p)
20341 update_window_cursor (w, 1);
20345 #ifdef HAVE_CARBON
20346 /* Display scroll bar for this window. */
20347 if (!NILP (w->vertical_scroll_bar))
20349 /* ++KFS:
20350 If this doesn't work here (maybe some header files are missing),
20351 make a function in macterm.c and call it to do the job! */
20352 ControlHandle ch
20353 = SCROLL_BAR_CONTROL_HANDLE (XSCROLL_BAR (w->vertical_scroll_bar));
20355 Draw1Control (ch);
20357 #endif
20359 return mouse_face_overwritten_p;
20364 /* Redraw (parts) of all windows in the window tree rooted at W that
20365 intersect R. R contains frame pixel coordinates. Value is
20366 non-zero if the exposure overwrites mouse-face. */
20368 static int
20369 expose_window_tree (w, r)
20370 struct window *w;
20371 XRectangle *r;
20373 struct frame *f = XFRAME (w->frame);
20374 int mouse_face_overwritten_p = 0;
20376 while (w && !FRAME_GARBAGED_P (f))
20378 if (!NILP (w->hchild))
20379 mouse_face_overwritten_p
20380 |= expose_window_tree (XWINDOW (w->hchild), r);
20381 else if (!NILP (w->vchild))
20382 mouse_face_overwritten_p
20383 |= expose_window_tree (XWINDOW (w->vchild), r);
20384 else
20385 mouse_face_overwritten_p |= expose_window (w, r);
20387 w = NILP (w->next) ? NULL : XWINDOW (w->next);
20390 return mouse_face_overwritten_p;
20394 /* EXPORT:
20395 Redisplay an exposed area of frame F. X and Y are the upper-left
20396 corner of the exposed rectangle. W and H are width and height of
20397 the exposed area. All are pixel values. W or H zero means redraw
20398 the entire frame. */
20400 void
20401 expose_frame (f, x, y, w, h)
20402 struct frame *f;
20403 int x, y, w, h;
20405 XRectangle r;
20406 int mouse_face_overwritten_p = 0;
20408 TRACE ((stderr, "expose_frame "));
20410 /* No need to redraw if frame will be redrawn soon. */
20411 if (FRAME_GARBAGED_P (f))
20413 TRACE ((stderr, " garbaged\n"));
20414 return;
20417 #ifdef HAVE_CARBON
20418 /* MAC_TODO: this is a kludge, but if scroll bars are not activated
20419 or deactivated here, for unknown reasons, activated scroll bars
20420 are shown in deactivated frames in some instances. */
20421 if (f == FRAME_MAC_DISPLAY_INFO (f)->x_focus_frame)
20422 activate_scroll_bars (f);
20423 else
20424 deactivate_scroll_bars (f);
20425 #endif
20427 /* If basic faces haven't been realized yet, there is no point in
20428 trying to redraw anything. This can happen when we get an expose
20429 event while Emacs is starting, e.g. by moving another window. */
20430 if (FRAME_FACE_CACHE (f) == NULL
20431 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
20433 TRACE ((stderr, " no faces\n"));
20434 return;
20437 if (w == 0 || h == 0)
20439 r.x = r.y = 0;
20440 r.width = CANON_X_UNIT (f) * f->width;
20441 r.height = CANON_Y_UNIT (f) * f->height;
20443 else
20445 r.x = x;
20446 r.y = y;
20447 r.width = w;
20448 r.height = h;
20451 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
20452 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
20454 if (WINDOWP (f->tool_bar_window))
20455 mouse_face_overwritten_p
20456 |= expose_window (XWINDOW (f->tool_bar_window), &r);
20458 #ifdef HAVE_X_WINDOWS
20459 #ifndef MSDOS
20460 #ifndef USE_X_TOOLKIT
20461 if (WINDOWP (f->menu_bar_window))
20462 mouse_face_overwritten_p
20463 |= expose_window (XWINDOW (f->menu_bar_window), &r);
20464 #endif /* not USE_X_TOOLKIT */
20465 #endif
20466 #endif
20468 /* Some window managers support a focus-follows-mouse style with
20469 delayed raising of frames. Imagine a partially obscured frame,
20470 and moving the mouse into partially obscured mouse-face on that
20471 frame. The visible part of the mouse-face will be highlighted,
20472 then the WM raises the obscured frame. With at least one WM, KDE
20473 2.1, Emacs is not getting any event for the raising of the frame
20474 (even tried with SubstructureRedirectMask), only Expose events.
20475 These expose events will draw text normally, i.e. not
20476 highlighted. Which means we must redo the highlight here.
20477 Subsume it under ``we love X''. --gerd 2001-08-15 */
20478 /* Included in Windows version because Windows most likely does not
20479 do the right thing if any third party tool offers
20480 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
20481 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
20483 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20484 if (f == dpyinfo->mouse_face_mouse_frame)
20486 int x = dpyinfo->mouse_face_mouse_x;
20487 int y = dpyinfo->mouse_face_mouse_y;
20488 clear_mouse_face (dpyinfo);
20489 note_mouse_highlight (f, x, y);
20495 /* EXPORT:
20496 Determine the intersection of two rectangles R1 and R2. Return
20497 the intersection in *RESULT. Value is non-zero if RESULT is not
20498 empty. */
20501 x_intersect_rectangles (r1, r2, result)
20502 XRectangle *r1, *r2, *result;
20504 XRectangle *left, *right;
20505 XRectangle *upper, *lower;
20506 int intersection_p = 0;
20508 /* Rearrange so that R1 is the left-most rectangle. */
20509 if (r1->x < r2->x)
20510 left = r1, right = r2;
20511 else
20512 left = r2, right = r1;
20514 /* X0 of the intersection is right.x0, if this is inside R1,
20515 otherwise there is no intersection. */
20516 if (right->x <= left->x + left->width)
20518 result->x = right->x;
20520 /* The right end of the intersection is the minimum of the
20521 the right ends of left and right. */
20522 result->width = (min (left->x + left->width, right->x + right->width)
20523 - result->x);
20525 /* Same game for Y. */
20526 if (r1->y < r2->y)
20527 upper = r1, lower = r2;
20528 else
20529 upper = r2, lower = r1;
20531 /* The upper end of the intersection is lower.y0, if this is inside
20532 of upper. Otherwise, there is no intersection. */
20533 if (lower->y <= upper->y + upper->height)
20535 result->y = lower->y;
20537 /* The lower end of the intersection is the minimum of the lower
20538 ends of upper and lower. */
20539 result->height = (min (lower->y + lower->height,
20540 upper->y + upper->height)
20541 - result->y);
20542 intersection_p = 1;
20546 return intersection_p;
20549 #endif /* HAVE_WINDOW_SYSTEM */
20552 /***********************************************************************
20553 Initialization
20554 ***********************************************************************/
20556 void
20557 syms_of_xdisp ()
20559 Vwith_echo_area_save_vector = Qnil;
20560 staticpro (&Vwith_echo_area_save_vector);
20562 Vmessage_stack = Qnil;
20563 staticpro (&Vmessage_stack);
20565 Qinhibit_redisplay = intern ("inhibit-redisplay");
20566 staticpro (&Qinhibit_redisplay);
20568 message_dolog_marker1 = Fmake_marker ();
20569 staticpro (&message_dolog_marker1);
20570 message_dolog_marker2 = Fmake_marker ();
20571 staticpro (&message_dolog_marker2);
20572 message_dolog_marker3 = Fmake_marker ();
20573 staticpro (&message_dolog_marker3);
20575 #if GLYPH_DEBUG
20576 defsubr (&Sdump_frame_glyph_matrix);
20577 defsubr (&Sdump_glyph_matrix);
20578 defsubr (&Sdump_glyph_row);
20579 defsubr (&Sdump_tool_bar_row);
20580 defsubr (&Strace_redisplay);
20581 defsubr (&Strace_to_stderr);
20582 #endif
20583 #ifdef HAVE_WINDOW_SYSTEM
20584 defsubr (&Stool_bar_lines_needed);
20585 #endif
20586 defsubr (&Sformat_mode_line);
20588 staticpro (&Qmenu_bar_update_hook);
20589 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
20591 staticpro (&Qoverriding_terminal_local_map);
20592 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
20594 staticpro (&Qoverriding_local_map);
20595 Qoverriding_local_map = intern ("overriding-local-map");
20597 staticpro (&Qwindow_scroll_functions);
20598 Qwindow_scroll_functions = intern ("window-scroll-functions");
20600 staticpro (&Qredisplay_end_trigger_functions);
20601 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
20603 staticpro (&Qinhibit_point_motion_hooks);
20604 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
20606 QCdata = intern (":data");
20607 staticpro (&QCdata);
20608 Qdisplay = intern ("display");
20609 staticpro (&Qdisplay);
20610 Qspace_width = intern ("space-width");
20611 staticpro (&Qspace_width);
20612 Qraise = intern ("raise");
20613 staticpro (&Qraise);
20614 Qspace = intern ("space");
20615 staticpro (&Qspace);
20616 Qmargin = intern ("margin");
20617 staticpro (&Qmargin);
20618 Qleft_margin = intern ("left-margin");
20619 staticpro (&Qleft_margin);
20620 Qright_margin = intern ("right-margin");
20621 staticpro (&Qright_margin);
20622 Qalign_to = intern ("align-to");
20623 staticpro (&Qalign_to);
20624 QCalign_to = intern (":align-to");
20625 staticpro (&QCalign_to);
20626 Qrelative_width = intern ("relative-width");
20627 staticpro (&Qrelative_width);
20628 QCrelative_width = intern (":relative-width");
20629 staticpro (&QCrelative_width);
20630 QCrelative_height = intern (":relative-height");
20631 staticpro (&QCrelative_height);
20632 QCeval = intern (":eval");
20633 staticpro (&QCeval);
20634 QCpropertize = intern (":propertize");
20635 staticpro (&QCpropertize);
20636 QCfile = intern (":file");
20637 staticpro (&QCfile);
20638 Qfontified = intern ("fontified");
20639 staticpro (&Qfontified);
20640 Qfontification_functions = intern ("fontification-functions");
20641 staticpro (&Qfontification_functions);
20642 Qtrailing_whitespace = intern ("trailing-whitespace");
20643 staticpro (&Qtrailing_whitespace);
20644 Qimage = intern ("image");
20645 staticpro (&Qimage);
20646 Qmessage_truncate_lines = intern ("message-truncate-lines");
20647 staticpro (&Qmessage_truncate_lines);
20648 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
20649 staticpro (&Qcursor_in_non_selected_windows);
20650 Qgrow_only = intern ("grow-only");
20651 staticpro (&Qgrow_only);
20652 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
20653 staticpro (&Qinhibit_menubar_update);
20654 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
20655 staticpro (&Qinhibit_eval_during_redisplay);
20656 Qposition = intern ("position");
20657 staticpro (&Qposition);
20658 Qbuffer_position = intern ("buffer-position");
20659 staticpro (&Qbuffer_position);
20660 Qobject = intern ("object");
20661 staticpro (&Qobject);
20662 Qbar = intern ("bar");
20663 staticpro (&Qbar);
20664 Qhbar = intern ("hbar");
20665 staticpro (&Qhbar);
20666 Qbox = intern ("box");
20667 staticpro (&Qbox);
20668 Qhollow = intern ("hollow");
20669 staticpro (&Qhollow);
20670 Qrisky_local_variable = intern ("risky-local-variable");
20671 staticpro (&Qrisky_local_variable);
20672 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
20673 staticpro (&Qinhibit_free_realized_faces);
20675 list_of_error = Fcons (intern ("error"), Qnil);
20676 staticpro (&list_of_error);
20678 last_arrow_position = Qnil;
20679 last_arrow_string = Qnil;
20680 staticpro (&last_arrow_position);
20681 staticpro (&last_arrow_string);
20683 echo_buffer[0] = echo_buffer[1] = Qnil;
20684 staticpro (&echo_buffer[0]);
20685 staticpro (&echo_buffer[1]);
20687 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
20688 staticpro (&echo_area_buffer[0]);
20689 staticpro (&echo_area_buffer[1]);
20691 Vmessages_buffer_name = build_string ("*Messages*");
20692 staticpro (&Vmessages_buffer_name);
20694 mode_line_proptrans_alist = Qnil;
20695 staticpro (&mode_line_proptrans_alist);
20697 mode_line_string_list = Qnil;
20698 staticpro (&mode_line_string_list);
20700 help_echo_string = Qnil;
20701 staticpro (&help_echo_string);
20702 help_echo_object = Qnil;
20703 staticpro (&help_echo_object);
20704 help_echo_window = Qnil;
20705 staticpro (&help_echo_window);
20706 previous_help_echo_string = Qnil;
20707 staticpro (&previous_help_echo_string);
20708 help_echo_pos = -1;
20710 #ifdef HAVE_WINDOW_SYSTEM
20711 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
20712 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
20713 For example, if a block cursor is over a tab, it will be drawn as
20714 wide as that tab on the display. */);
20715 x_stretch_cursor_p = 0;
20716 #endif
20718 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
20719 doc: /* Non-nil means highlight trailing whitespace.
20720 The face used for trailing whitespace is `trailing-whitespace'. */);
20721 Vshow_trailing_whitespace = Qnil;
20723 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
20724 doc: /* Non-nil means don't actually do any redisplay.
20725 This is used for internal purposes. */);
20726 Vinhibit_redisplay = Qnil;
20728 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
20729 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
20730 Vglobal_mode_string = Qnil;
20732 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
20733 doc: /* Marker for where to display an arrow on top of the buffer text.
20734 This must be the beginning of a line in order to work.
20735 See also `overlay-arrow-string'. */);
20736 Voverlay_arrow_position = Qnil;
20738 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
20739 doc: /* String to display as an arrow. See also `overlay-arrow-position'. */);
20740 Voverlay_arrow_string = Qnil;
20742 DEFVAR_INT ("scroll-step", &scroll_step,
20743 doc: /* *The number of lines to try scrolling a window by when point moves out.
20744 If that fails to bring point back on frame, point is centered instead.
20745 If this is zero, point is always centered after it moves off frame.
20746 If you want scrolling to always be a line at a time, you should set
20747 `scroll-conservatively' to a large value rather than set this to 1. */);
20749 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
20750 doc: /* *Scroll up to this many lines, to bring point back on screen.
20751 A value of zero means to scroll the text to center point vertically
20752 in the window. */);
20753 scroll_conservatively = 0;
20755 DEFVAR_INT ("scroll-margin", &scroll_margin,
20756 doc: /* *Number of lines of margin at the top and bottom of a window.
20757 Recenter the window whenever point gets within this many lines
20758 of the top or bottom of the window. */);
20759 scroll_margin = 0;
20761 #if GLYPH_DEBUG
20762 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
20763 #endif
20765 DEFVAR_BOOL ("truncate-partial-width-windows",
20766 &truncate_partial_width_windows,
20767 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
20768 truncate_partial_width_windows = 1;
20770 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
20771 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
20772 Any other value means to use the appropriate face, `mode-line',
20773 `header-line', or `menu' respectively. */);
20774 mode_line_inverse_video = 1;
20776 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
20777 doc: /* *Maximum buffer size for which line number should be displayed.
20778 If the buffer is bigger than this, the line number does not appear
20779 in the mode line. A value of nil means no limit. */);
20780 Vline_number_display_limit = Qnil;
20782 DEFVAR_INT ("line-number-display-limit-width",
20783 &line_number_display_limit_width,
20784 doc: /* *Maximum line width (in characters) for line number display.
20785 If the average length of the lines near point is bigger than this, then the
20786 line number may be omitted from the mode line. */);
20787 line_number_display_limit_width = 200;
20789 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
20790 doc: /* *Non-nil means highlight region even in nonselected windows. */);
20791 highlight_nonselected_windows = 0;
20793 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
20794 doc: /* Non-nil if more than one frame is visible on this display.
20795 Minibuffer-only frames don't count, but iconified frames do.
20796 This variable is not guaranteed to be accurate except while processing
20797 `frame-title-format' and `icon-title-format'. */);
20799 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
20800 doc: /* Template for displaying the title bar of visible frames.
20801 \(Assuming the window manager supports this feature.)
20802 This variable has the same structure as `mode-line-format' (which see),
20803 and is used only on frames for which no explicit name has been set
20804 \(see `modify-frame-parameters'). */);
20805 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
20806 doc: /* Template for displaying the title bar of an iconified frame.
20807 \(Assuming the window manager supports this feature.)
20808 This variable has the same structure as `mode-line-format' (which see),
20809 and is used only on frames for which no explicit name has been set
20810 \(see `modify-frame-parameters'). */);
20811 Vicon_title_format
20812 = Vframe_title_format
20813 = Fcons (intern ("multiple-frames"),
20814 Fcons (build_string ("%b"),
20815 Fcons (Fcons (empty_string,
20816 Fcons (intern ("invocation-name"),
20817 Fcons (build_string ("@"),
20818 Fcons (intern ("system-name"),
20819 Qnil)))),
20820 Qnil)));
20822 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
20823 doc: /* Maximum number of lines to keep in the message log buffer.
20824 If nil, disable message logging. If t, log messages but don't truncate
20825 the buffer when it becomes large. */);
20826 Vmessage_log_max = make_number (50);
20828 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
20829 doc: /* Functions called before redisplay, if window sizes have changed.
20830 The value should be a list of functions that take one argument.
20831 Just before redisplay, for each frame, if any of its windows have changed
20832 size since the last redisplay, or have been split or deleted,
20833 all the functions in the list are called, with the frame as argument. */);
20834 Vwindow_size_change_functions = Qnil;
20836 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
20837 doc: /* List of Functions to call before redisplaying a window with scrolling.
20838 Each function is called with two arguments, the window
20839 and its new display-start position. Note that the value of `window-end'
20840 is not valid when these functions are called. */);
20841 Vwindow_scroll_functions = Qnil;
20843 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
20844 doc: /* *Non-nil means autoselect window with mouse pointer. */);
20845 mouse_autoselect_window = 0;
20847 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
20848 doc: /* *Non-nil means automatically resize tool-bars.
20849 This increases a tool-bar's height if not all tool-bar items are visible.
20850 It decreases a tool-bar's height when it would display blank lines
20851 otherwise. */);
20852 auto_resize_tool_bars_p = 1;
20854 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
20855 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
20856 auto_raise_tool_bar_buttons_p = 1;
20858 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
20859 doc: /* *Margin around tool-bar buttons in pixels.
20860 If an integer, use that for both horizontal and vertical margins.
20861 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
20862 HORZ specifying the horizontal margin, and VERT specifying the
20863 vertical margin. */);
20864 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
20866 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
20867 doc: /* *Relief thickness of tool-bar buttons. */);
20868 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
20870 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
20871 doc: /* List of functions to call to fontify regions of text.
20872 Each function is called with one argument POS. Functions must
20873 fontify a region starting at POS in the current buffer, and give
20874 fontified regions the property `fontified'. */);
20875 Vfontification_functions = Qnil;
20876 Fmake_variable_buffer_local (Qfontification_functions);
20878 DEFVAR_BOOL ("unibyte-display-via-language-environment",
20879 &unibyte_display_via_language_environment,
20880 doc: /* *Non-nil means display unibyte text according to language environment.
20881 Specifically this means that unibyte non-ASCII characters
20882 are displayed by converting them to the equivalent multibyte characters
20883 according to the current language environment. As a result, they are
20884 displayed according to the current fontset. */);
20885 unibyte_display_via_language_environment = 0;
20887 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
20888 doc: /* *Maximum height for resizing mini-windows.
20889 If a float, it specifies a fraction of the mini-window frame's height.
20890 If an integer, it specifies a number of lines. */);
20891 Vmax_mini_window_height = make_float (0.25);
20893 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
20894 doc: /* *How to resize mini-windows.
20895 A value of nil means don't automatically resize mini-windows.
20896 A value of t means resize them to fit the text displayed in them.
20897 A value of `grow-only', the default, means let mini-windows grow
20898 only, until their display becomes empty, at which point the windows
20899 go back to their normal size. */);
20900 Vresize_mini_windows = Qgrow_only;
20902 DEFVAR_LISP ("cursor-in-non-selected-windows",
20903 &Vcursor_in_non_selected_windows,
20904 doc: /* *Cursor type to display in non-selected windows.
20905 t means to use hollow box cursor. See `cursor-type' for other values. */);
20906 Vcursor_in_non_selected_windows = Qt;
20908 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
20909 doc: /* Alist specifying how to blink the cursor off.
20910 Each element has the form (ON-STATE . OFF-STATE). Whenever the
20911 `cursor-type' frame-parameter or variable equals ON-STATE,
20912 comparing using `equal', Emacs uses OFF-STATE to specify
20913 how to blink it off. */);
20914 Vblink_cursor_alist = Qnil;
20916 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
20917 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
20918 automatic_hscrolling_p = 1;
20920 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
20921 doc: /* *How many columns away from the window edge point is allowed to get
20922 before automatic hscrolling will horizontally scroll the window. */);
20923 hscroll_margin = 5;
20925 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
20926 doc: /* *How many columns to scroll the window when point gets too close to the edge.
20927 When point is less than `automatic-hscroll-margin' columns from the window
20928 edge, automatic hscrolling will scroll the window by the amount of columns
20929 determined by this variable. If its value is a positive integer, scroll that
20930 many columns. If it's a positive floating-point number, it specifies the
20931 fraction of the window's width to scroll. If it's nil or zero, point will be
20932 centered horizontally after the scroll. Any other value, including negative
20933 numbers, are treated as if the value were zero.
20935 Automatic hscrolling always moves point outside the scroll margin, so if
20936 point was more than scroll step columns inside the margin, the window will
20937 scroll more than the value given by the scroll step.
20939 Note that the lower bound for automatic hscrolling specified by `scroll-left'
20940 and `scroll-right' overrides this variable's effect. */);
20941 Vhscroll_step = make_number (0);
20943 DEFVAR_LISP ("image-types", &Vimage_types,
20944 doc: /* List of supported image types.
20945 Each element of the list is a symbol for a supported image type. */);
20946 Vimage_types = Qnil;
20948 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
20949 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
20950 Bind this around calls to `message' to let it take effect. */);
20951 message_truncate_lines = 0;
20953 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
20954 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
20955 Can be used to update submenus whose contents should vary. */);
20956 Vmenu_bar_update_hook = Qnil;
20958 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
20959 doc: /* Non-nil means don't update menu bars. Internal use only. */);
20960 inhibit_menubar_update = 0;
20962 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
20963 doc: /* Non-nil means don't eval Lisp during redisplay. */);
20964 inhibit_eval_during_redisplay = 0;
20966 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
20967 doc: /* Non-nil means don't free realized faces. Internal use only. */);
20968 inhibit_free_realized_faces = 0;
20970 #if GLYPH_DEBUG
20971 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
20972 doc: /* Inhibit try_window_id display optimization. */);
20973 inhibit_try_window_id = 0;
20975 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
20976 doc: /* Inhibit try_window_reusing display optimization. */);
20977 inhibit_try_window_reusing = 0;
20979 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
20980 doc: /* Inhibit try_cursor_movement display optimization. */);
20981 inhibit_try_cursor_movement = 0;
20982 #endif /* GLYPH_DEBUG */
20986 /* Initialize this module when Emacs starts. */
20988 void
20989 init_xdisp ()
20991 Lisp_Object root_window;
20992 struct window *mini_w;
20994 current_header_line_height = current_mode_line_height = -1;
20996 CHARPOS (this_line_start_pos) = 0;
20998 mini_w = XWINDOW (minibuf_window);
20999 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
21001 if (!noninteractive)
21003 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
21004 int i;
21006 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
21007 set_window_height (root_window,
21008 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
21010 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
21011 set_window_height (minibuf_window, 1, 0);
21013 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
21014 mini_w->width = make_number (FRAME_WIDTH (f));
21016 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
21017 scratch_glyph_row.glyphs[TEXT_AREA + 1]
21018 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
21020 /* The default ellipsis glyphs `...'. */
21021 for (i = 0; i < 3; ++i)
21022 default_invis_vector[i] = make_number ('.');
21026 /* Allocate the buffer for frame titles.
21027 Also used for `format-mode-line'. */
21028 int size = 100;
21029 frame_title_buf = (char *) xmalloc (size);
21030 frame_title_buf_end = frame_title_buf + size;
21031 frame_title_ptr = NULL;
21034 help_echo_showing_p = 0;